diff --git a/render-wasm/docs/cjk-layout.md b/render-wasm/docs/cjk-layout.md new file mode 100644 index 0000000000..bb20f14bc2 --- /dev/null +++ b/render-wasm/docs/cjk-layout.md @@ -0,0 +1,248 @@ +# Chinese and Korean core layout + +Paragraph `text-language` selects a layout policy. Writing mode remains a +whole-shape value. UI availability never changes either attribute. + +## Named Chinese presets + +| Tags | Preset | +| --- | --- | +| `zh`, `zh-Hans`, `zh-Hant`, unknown regions | Original basic punctuation policy | +| `zh-Hans-CN`, `zh-Hant-CN`, `zh-CN`, `zh-SG` | Mainland: adjacent punctuation compression; quarter-em Han/Latin gaps | +| `zh-Hant-TW`, `zh-TW` | Taiwan: solid punctuation; quarter-em Han/Latin gaps | +| `zh-Hant-HK`, `zh-HK`, `zh-MO` | Hong Kong: centered punctuation spacing; quarter-em Han/Latin gaps | + +Script does not imply region. Changing a profile does not convert characters. +These presets select a limited set of styles; they do not claim that every +publication in a region uses the same settings. + +Compression applies to eligible adjacent full-width punctuation. Vertical +colon, semicolon, question and exclamation marks keep their full frame. +Proportional horizontal punctuation is not compressed again. Vertical cells +retain at least a half-em frame. The Taiwan and Hong Kong presets allow a +single comma or full stop to hang by at most half an em vertically; adjacent +punctuation and horizontal text do not hang. + +Chinese justification expands eligible character gaps by at most half an em. +The final line/column remains unexpanded. These bounds are explicit POC +choices, not numeric requirements imposed by a regional standard. + +## Korean modes + +- Missing `korean-wrap`, or `syllable`: syllable wrapping with punctuation rules. +- `keep-words`: keep adjoining letters and digits together across scripts and + style runs. Punctuation keeps its own opportunities and restrictions. +- `spaces-only`: preserve the earlier experiment. It remains readable in the + UI for saved text but is not offered for new selections by default. + +Fitting words move whole. Overlong words may use safe grapheme boundaries. +Nonbreaking spaces/joiners, numeric prefixes/suffixes, paired dashes/ellipses, +annotations and punctuation restrictions remain separate from letter ties. +Korean justification first expands spaces, at most half an em per gap. +Syllable mode may then expand eligible character gaps by at most one eighth +of an em. Word mode never expands inside a word. Original spaces remain in +source text; no Japanese word-space substitution applies. + +## Japanese line adjustment + +Japanese justification uses the JLREQ §3.8.4 ordered expansion in both +orientations (`jlreq_ordered_expansion`). Gaps widen by pair-table stage: +Western word spaces, then Japanese/Western gaps up to half an em, then +solid kana and kanji up to a quarter em. Slack left after the three stages +spreads evenly over the same gaps. Punctuation aki, gaps beside a ruby base, +gaps inside a Western word and gaps across a warichu note take none. The +final line or column stays unexpanded; distribution keeps its own path. + +Squeezing (oikomi) follows the line adjustment setting in both orientations. +Push-in first squeezes whenever the aki on the line can hold the next unit. +Push-out first squeezes only when pushing out cannot resolve a kinsoku +break, which horizontally means the break would move back over more than +four kinsoku pairs. Horizontal text keeps a unit wider than the box whole +on its own line instead. Push-out only never squeezes. + +## Horizontal emergency wrapping and offsets + +SkParagraph can ignore word joins during emergency wrapping. The horizontal +plan measures candidate units and replaces joins with zero-width break +opportunities only inside overlong units, at safe grapheme boundaries. +Both marker characters occupy three UTF-8 bytes and one UTF-16 code unit. +Their offset maps therefore stay identical for drawing, hit testing, carets, +selection, text paths and export. Width participates in the plan cache key; +resizing a box restores whole-word joins when the word fits again. + +If an indivisible Chinese or Korean punctuation/annotation unit exceeds the +box width, the plan lets it overflow together. It first measures surrounding +text at the original width, then records those line starts with layout-only +line separators before giving Skia enough physical width for the unit. +Ordinary text keeps its original wrap budget. Source text stays unchanged; +the plan's offset map accounts for every inserted separator. + +Painting, strokes, caret/selection geometry, hit testing, annotations, paths +and exports share the plan and its alignment offset. The width cache also +rebuilds these breaks on resize. Legacy Korean `spaces-only` keeps its earlier +behavior; the supported modes do not claim complete CSS `keep-all` conformance. + +## Pinyin readings + +The Chinese layout panel offers typed Pinyin readings when the selected +paragraph has an explicit Chinese profile. In the WASM text editor, select +one character or a group, in any text styles, then enter its reading. A +reading over spans of several styles, such as characters with and without +emphasis marks, gives them one `ruby-group`; the renderer draws one base +over the spans of a group (`Paragraph::ruby_groups`), whose first span holds +the reading and its presentation. To edit an existing reading, select its +whole base. Selections across paragraphs, or that cut a reading or warichu +note, do not offer a reading field, which prevents repeated readings. For +character-by-character readings, annotate each character separately: +neighbours with the same reading but no shared group stay separate bases. + +Readings use the saved `ruby` attributes and the shared annotation renderer. +They support size, alignment, overhang, side, hiding and annotation clearance. +Adjacent reading spans recover the style covering each base even when Skia +merges their styles. Emphasis marks retain their per-base placement across +wraps; text bounds include the marks so individual exports keep them whole. +Without a text selection, presentation changes affect existing readings. +Horizontal readings form one Latin group above or below the base; vertical +readings form one sideways Latin group beside it. A reading longer than its +base overhangs an adjacent comma, full stop or middle dot (or kana) without +its own reading, by at most one ruby character and half the neighbour, in +both orientations. Han neighbours take no overhang. What overhang cannot +absorb, or all of it under ruby-overhang `none`, spreads the base. Pronunciation is entered +by the author; the editor does not generate or distribute syllables. + +Composed and combining tone marks retain their source text and shaping. +Glyphs sharing a cluster retain the same complete source range. Tests use +`src/fonts/pinyin-test.ttf`, an 8 KB subset of the repository's Work Sans +Regular font, with letters, umlauts and combining tone marks. The subset +retains its copyright and SIL Open Font License metadata; its family name +is Pinyin Test. FontTools generated it from the Pinyin test strings. + +## Emphasis and underline positions + +Chinese profiles expose emphasis shape, emphasis position, underline position +and annotation clearance independently of Pinyin. Position controls do not +turn on underlining; use the existing underline control for that. + +Optional span `text-emphasis-position` stores `auto`, `over-right`, +`over-left`, `under-right` or `under-left`. The first part selects the +horizontal side; the second selects the vertical side. Auto uses below/right +for Chinese and preserves above/right for Japanese, Korean and plain text. + +Optional `text-underline-position` stores `auto`, `under`, `left`, `right`, +`under-left` or `under-right`. Under places horizontal underlines below the +base rect; left/right select the vertical side. Chinese auto uses below/left. +Other auto values preserve the earlier baseline offset and vertical left +side. Explicit values can select other styles without changing the language. +These are supported POC choices, not a claim of complete decoration support. + +Automatic clearance reserves each occupied side. Stacked annotations retain +the largest reading and mark sizes across mixed-font spans, preserving the +prior Japanese column widths. Underlines remain next to +the base; readings move outside an underline on their side, and emphasis +moves outside a reading on its side. The underline band reserves 0.18 em, +a POC bound around the existing 1/9-em gap and stroke width. Disabling +clearance retains fixed line-height behavior and can allow overlap/clipping. + +Horizontal marks are shaped once per span and placed by their measured +box (advance by ascent and descent at half the base size). Paint, text +bounds and selection use that one box, so bounds cover exactly what paints. +Position data carries the mark's em box centred on it, because SVG export +draws the mark at a font size of that box's width. Vertical marks already +centre the glyph and their position-data box on the same point. + +Decoration edits that intersect a note, or a reading without a group, apply +to its whole base, so changing a side cannot duplicate its reading on split +spans. A grouped reading keeps one base when an edit splits its spans. Other text +styles remain unchanged. Selection snapshots, Plugin API properties, +CSS handoff and saved content retain the independent positions. CSS auto +positions inherit the Chinese paragraph defaults; explicit positions emit +the corresponding CSS keyword pairs. The binary writer uses former span +header bytes 14/15; header size and prior offsets stay unchanged. Unknown +bytes decode to auto. Both WASM consumers share that writer. + +Defaults and positions were checked on 2026-10-20 against +[CSS Text Decoration, emphasis](https://www.w3.org/TR/css-text-decor-3/#text-emphasis-position-property) +and [underline positions](https://www.w3.org/TR/css-text-decor-3/#text-underline-position-property). +Japanese/Korean vertical auto underlines retain this branch's existing left +side rather than adopting the spec's suggested right-side style. + +## Evidence and release checks + +Renderer tests cover regional geometry, horizontal/vertical wrapping, +style boundaries, decomposed Jamo, punctuation, justification, stable offsets +and distinct wrap protocol bytes. Editor splits preserve the complete wrap +mode. Both WASM consumers use the same writer and byte values: 0 syllable, +1 experiment, 2 keep-words; unknown values fall back to syllable wrapping. +The renderer decodes the byte through the exported `RawKoreanWrap` enum. + +Real Pinyin, Zhuyin and Korean IME checks and native-reader review remain +release checks. Font glyph coverage and localized forms still depend on the +chosen fonts. Dedicated Zhuyin layout is described below. + +References checked on 2026-10-19: + +- [CLREQ draft, 2026-10-07](https://www.w3.org/TR/2026/DNOTE-clreq-20261007/), + sections 6.1 and 6.3.2: punctuation boundaries, compression and hanging. +- [KLREQ draft, 2026-03-21](https://www.w3.org/TR/2026/DNOTE-klreq-20260321/), + sections 7.1 and 7.2: syllable/word wrapping and justification restrictions. +- [CSS Text 3](https://www.w3.org/TR/css-text-3/#word-break-property): + letter-breaking policy is distinct from punctuation and overflow handling. + +### Mandarin Zhuyin annotations + +An optional span flag, `ruby-zhuyin`, selects dedicated right-side Zhuyin. +Absent or false retains normal ruby. Each span maps one Han character +(optionally with one variation selector) to one typed Mandarin syllable in +`ruby`. Enter 1–3 U+3105–U+3129 symbols and at most one tone: U+02C9, U+02CA, +U+02C7, U+02CB or U+02D9. Neutral tone may precede or follow the symbols; +other tones must follow them. The editor validates this syntax; it does not +check pronunciation, infer mappings or generate readings. Unsupported API +content keeps its text and falls back to ordinary ruby. + +Symbols remain upright to the base's right in horizontal and vertical text. +Their size is 0.3 em. A 0.6 em strip holds symbols and tones, including an +empty tone lane for unmarked first tone. Non-neutral tones sit outside the +upper right of the last symbol; neutral tone sits above the symbol stack. +The stack centres vertically on the base. These rules follow CLREQ +sections 5.5.3.2–5.5.3.3, retrieved 2026-10-20: +. The strip is wider than the reference's +half-em minimum to contain a full-width tone fallback glyph. + +Horizontal layout reserves the strip in the inline advance. Vertical Auto +clearance reserves it in the right gutter, outside a same-side underline +and inside same-side emphasis. Fixed clearance may allow overlaps, as with +ordinary ruby. Zhuyin ignores ruby size, alignment, overhang and side; +hiding a reading removes its room without clearing saved text or mode. + +The mode uses bit 1 of span header byte 13; bit 0 keeps over/under for normal +ruby. Header size stays unchanged. Unknown flag bits fall back to ordinary +over ruby. Position-data direction 5 carries a single upright annotation +glyph with offsets into the original ruby string and its drawing baseline. +Both layout paths use it for SVG; canvas, PNG, PDF and vertical outlines use +the same shaped glyph positions. HTML handoff uses positioned symbol boxes; +font metrics can make it differ from the renderer's ink-centred placement. + +This slice supports Mandarin, one base per span. Group-to-syllable mapping, +dialectal symbols/tones, automatic pronunciation, native IME acceptance and +native-reader approval remain outside this slice. Changing a base into a +group preserves the reading and uses normal ruby until a single base is +restored. Existing Pinyin and Japanese readings keep their saved behavior. + +## Renderer modules + +- `shapes/text_policy.rs`: `TextLayoutPolicy` (`Plain`, `Japanese`, + `Chinese(ChineseRegion)`, `Korean(KoreanWrap)`), parsed once per + paragraph from `LanguageTag`. Layout code asks it named questions. +- `shapes/jlreq.rs`: JLREQ classes (`JlreqClass`), pair rules and kinsoku. +- `shapes/line_breaks.rs`: horizontal layout-text rewrite, `OffsetMap` and + `PolicyBoundaries`. +- `shapes/cjk_chars.rs`: shared Han, kana, hangul and punctuation ranges. +- `shapes/text_attrs.rs`, `text_ruby.rs`, `text_warichu.rs` and + `text_allocation.rs`: span attributes, ruby rules, warichu and the + justify/squeeze allocators both axes share. +- `shapes/text_shaping/`: glyph shaping, orientation and font tables. +- `text_vertical/flow/`: JLREQ column planning, with the pairwise policies + in `flow/pairwise.rs`. Horizontal and vertical helpers use the same + `jlreq_*` and `pairwise_*` prefixes. +- `wasm/text/layout_attrs.rs`: the `Raw*` enums the frontend writes, including + `RawKoreanWrap`. diff --git a/render-wasm/scripts/build-cjk-test-font.py b/render-wasm/scripts/build-cjk-test-font.py new file mode 100644 index 0000000000..9b6620f8c2 --- /dev/null +++ b/render-wasm/scripts/build-cjk-test-font.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Build the synthetic CJK language fixture (requires fonttools). + +The one Han character has distinct JAN, ZHS and KOR localized glyphs and +advances. This tests language propagation without a system font dependency. +All outlines are original rectangles; no external font data is included. +""" + +from pathlib import Path + +from fontTools.feaLib.builder import addOpenTypeFeaturesFromString +from fontTools.fontBuilder import FontBuilder +from fontTools.pens.ttGlyphPen import TTGlyphPen + + +def main(): + builder = FontBuilder(1000, isTTF=True) + names = [".notdef", "base", "japanese", "chinese", "korean", "square", "space", "thirdEm"] + builder.setupGlyphOrder(names) + cmap = {ord(char): "square" for char in "文あっ한국어문장한글한「」,。"} + cmap.update({ord(char): "square" for char in "ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙㄧㄨㄩㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦˉˊˇˋ˙"}) + cmap.update({0x4E2D: "base", 0x20: "space", 0x2004: "thirdEm"}) + builder.setupCharacterMap(cmap) + glyphs = {} + for index, name in enumerate(names): + pen = TTGlyphPen(None) + pen.moveTo((50, 0)) + pen.lineTo((350 + index * 20, 0)) + pen.lineTo((350 + index * 20, 700)) + pen.lineTo((50, 700)) + pen.closePath() + glyphs[name] = pen.glyph() + builder.setupGlyf(glyphs) + metrics = {name: (400 + index * 100, 50) for index, name in enumerate(names)} + metrics.update({"square": (1000, 50), "space": (500, 50), "thirdEm": (333, 50)}) + builder.setupHorizontalMetrics(metrics) + builder.setupHorizontalHeader(ascent=800, descent=-200) + builder.setupNameTable({ + "familyName": "Penpot CJK Test", + "styleName": "Regular", + "uniqueFontIdentifier": "PenpotCJKTest", + "fullName": "Penpot CJK Test Regular", + "psName": "PenpotCJKTest-Regular", + }) + builder.setupOS2(sTypoAscender=800, sTypoDescender=-200, + usWinAscent=800, usWinDescent=200) + builder.setupPost() + builder.setupMaxp() + addOpenTypeFeaturesFromString(builder.font, """ + languagesystem DFLT dflt; + languagesystem hani JAN; + languagesystem hani ZHS; + languagesystem hani KOR; + feature locl { + script hani; + language JAN; sub base by japanese; + language ZHS; sub base by chinese; + language KOR; sub base by korean; + } locl; + """) + builder.font["head"].created = 3800000000 + builder.font["head"].modified = 3800000000 + builder.font.recalcTimestamp = False + builder.save(Path(__file__).resolve().parents[1] / "src/fonts/cjk-locl-test.ttf") + + +if __name__ == "__main__": + main() diff --git a/render-wasm/src/fonts/cjk-locl-test.ttf b/render-wasm/src/fonts/cjk-locl-test.ttf new file mode 100644 index 0000000000..c749061291 Binary files /dev/null and b/render-wasm/src/fonts/cjk-locl-test.ttf differ diff --git a/render-wasm/src/fonts/pinyin-test.ttf b/render-wasm/src/fonts/pinyin-test.ttf new file mode 100644 index 0000000000..26a34601e7 Binary files /dev/null and b/render-wasm/src/fonts/pinyin-test.ttf differ diff --git a/render-wasm/src/globals.rs b/render-wasm/src/globals.rs index cc9e718bdf..bc4ab2099a 100644 --- a/render-wasm/src/globals.rs +++ b/render-wasm/src/globals.rs @@ -56,17 +56,30 @@ pub(crate) fn has_render_state() -> bool { unsafe { !RENDER_STATE.is_null() } } +/// The render resources in use: in tests, the ones this thread installed with +/// [`TestRenderResourcesGuard`], else the global ones. +#[inline(always)] +fn render_resources() -> *mut RenderResources { + #[cfg(test)] + { + let installed = TEST_RENDER_RESOURCES.with(std::cell::Cell::get); + if !installed.is_null() { + return installed; + } + } + unsafe { RENDER_RESOURCES } +} + #[inline(always)] pub(crate) fn has_render_resources() -> bool { - unsafe { !RENDER_RESOURCES.is_null() } + !render_resources().is_null() } #[inline(always)] pub(crate) fn get_resources() -> &'static mut RenderResources { - unsafe { - assert!(!RENDER_RESOURCES.is_null(), "Render Resources is null"); - &mut *RENDER_RESOURCES - } + let resources = render_resources(); + assert!(!resources.is_null(), "Render Resources is null"); + unsafe { &mut *resources } } #[inline(always)] @@ -120,7 +133,16 @@ macro_rules! with_current_shape { }; } -/// Scoped override of the global render resources pointer for unit tests. +#[cfg(test)] +thread_local! { + /// Render resources a test installed for its own thread. Tests run on + /// parallel threads, and Skia font collections are not thread-safe, so + /// another test must never reach them. + static TEST_RENDER_RESOURCES: std::cell::Cell<*mut RenderResources> = + const { std::cell::Cell::new(std::ptr::null_mut()) }; +} + +/// Scoped override of the render resources for the current test thread. #[cfg(test)] pub(crate) struct TestRenderResourcesGuard { prev: *mut RenderResources, @@ -136,8 +158,7 @@ impl TestRenderResourcesGuard { let lock = TEST_RENDER_RESOURCES_LOCK .lock() .unwrap_or_else(|poisoned| poisoned.into_inner()); - let prev = unsafe { RENDER_RESOURCES }; - unsafe { RENDER_RESOURCES = resources as *mut _ }; + let prev = TEST_RENDER_RESOURCES.replace(resources as *mut _); Self { prev, _lock: lock } } } @@ -145,7 +166,7 @@ impl TestRenderResourcesGuard { #[cfg(test)] impl Drop for TestRenderResourcesGuard { fn drop(&mut self) { - unsafe { RENDER_RESOURCES = self.prev }; + TEST_RENDER_RESOURCES.set(self.prev); } } @@ -184,7 +205,7 @@ fn resources_init_headless() { } /// Initializes DesignState. -fn design_init() { +pub(crate) fn design_init() { unsafe { let design_state = State::new(); DESIGN_STATE = Box::into_raw(Box::new(design_state)); diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index 9072be3f08..e63fec7256 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -1596,8 +1596,12 @@ impl RenderState { let rebound_text_content = stored_text_content.paint_content_for_selrect(shape.selrect()); let text_content = rebound_text_content.as_ref(); - let mut paragraph_builders = - text_content.paragraph_builder_group_from_text(None); + // Vertical text paints from its cached vertical layout. + let mut paragraph_builders = if text_content.is_vertical() { + Vec::new() + } else { + text_content.paragraph_builder_group_from_text(None) + }; text::render( Some(self), None, @@ -1749,7 +1753,9 @@ impl RenderState { // Plain fill (no strokes / parent shadows): reuse cached layout // paragraphs when valid. Skip builder rebuild + Skia layout. - let can_use_layout_cache = !shape.has_visible_strokes() + // The full text pass paints the developer text grid. + let can_use_layout_cache = !self.options.is_text_grid_visible() + && !shape.has_visible_strokes() && parent_shadows.is_none() && (skip_effects || (shape.blur.is_none() @@ -1769,191 +1775,41 @@ impl RenderState { let rebound_text_content = stored_text_content.paint_content_for_selrect(shape.selrect()); let text_content = rebound_text_content.as_ref(); - let count_inner_strokes = shape.count_visible_inner_strokes(); - // Erode the main text fill by 1px when there are inner strokes, to avoid a visible seam at the glyph edge. - let text_fill_inset = (count_inner_strokes > 0).then(|| 1.0 / self.get_scale()); - let text_stroke_blur_outset = - Stroke::max_bounds_width(shape.visible_strokes(), false); - let mut paragraph_builders = - text_content.paragraph_builder_group_from_text(None); - let stroke_kinds: Vec = - shape.visible_strokes().rev().map(|s| s.kind).collect(); - let (mut stroke_paragraphs_list, stroke_opacities): (Vec<_>, Vec<_>) = shape - .visible_strokes() - .rev() - .map(|stroke| { - text::stroke_paragraph_builder_group_from_text( - text_content, - stroke, - &shape.selrect(), - None, - ) - }) - .unzip(); - if skip_effects { - // Fast path: render fills and strokes only (skip shadows/blur). - text::render( - Some(self), - None, + if text_content.is_vertical() { + text::render_vertical_text( + self, &shape, - &mut paragraph_builders, - Some(fills_surface_id), - None, - None, - text_fill_inset, - None, + text_content, + fills_surface_id, + strokes_surface_id, + skip_effects, )?; - - for (i, (stroke_paragraphs, layer_opacity)) in stroke_paragraphs_list - .iter_mut() - .zip(stroke_opacities.iter()) - .enumerate() - { - if stroke_kinds[i] == StrokeKind::Inner { - let mut fill_builders = - text_content.paragraph_builder_group_from_text(None); - text::render_inner_stroke( - Some(self), - None, - &shape, - stroke_paragraphs, - &mut fill_builders, - Some(strokes_surface_id), - None, - text_stroke_blur_outset, - *layer_opacity, - )?; - } else if stroke_kinds[i] == StrokeKind::Outer { - text::render_outer_stroke( - Some(self), - None, - &shape, - stroke_paragraphs, - Some(strokes_surface_id), - None, - text_stroke_blur_outset, - *layer_opacity, - )?; - } else { - text::render_with_bounds_outset( - Some(self), - None, - &shape, - stroke_paragraphs, - Some(strokes_surface_id), - None, - None, - text_stroke_blur_outset, - None, - *layer_opacity, - )?; - } - } - - if shape.has_visible_strokes() && text_content.has_non_ascii() { - let mut emoji_builders = text_content.paragraph_builder_group_opaque(); - let mut deco_builders = - text_content.paragraph_builder_group_from_text(None); - text::render_emoji_overlay( - self, - &shape, - &mut emoji_builders, - &mut deco_builders, - strokes_surface_id, - None, - ); - } } else { - let shape_scale = self.get_scale(); - let mut drop_shadows = if skip_drop_shadows { - Vec::new() - } else { + let count_inner_strokes = shape.count_visible_inner_strokes(); + // Erode the main text fill by 1px when there are inner strokes, to avoid a visible seam at the glyph edge. + let text_fill_inset = + (count_inner_strokes > 0).then(|| 1.0 / self.get_scale()); + let text_stroke_blur_outset = + Stroke::max_bounds_width(shape.visible_strokes(), false); + let mut paragraph_builders = + text_content.paragraph_builder_group_from_text(None); + let stroke_kinds: Vec = + shape.visible_strokes().rev().map(|s| s.kind).collect(); + let (mut stroke_paragraphs_list, stroke_opacities): (Vec<_>, Vec<_>) = shape - .drop_shadows_visible() - .filter(|s| s.is_perceptible_at_scale(shape_scale)) - .map(|shadow| { - let mut paint = skia_safe::Paint::default(); - paint.set_image_filter(shadow.get_drop_shadow_filter()); - paint - }) - .collect() - }; - - if !skip_drop_shadows { - if let Some(inherited_shadows) = self.get_inherited_drop_shadows() { - drop_shadows.extend(inherited_shadows); - } - } - - let inner_shadows = shape.inner_shadow_paints(); - let blur_filter = shape.image_filter(1.); - let mut paragraphs_with_shadows = - text_content.paragraph_builder_group_from_text(Some(true)); - let (mut stroke_paragraphs_with_shadows_list, _shadow_opacities): ( - Vec<_>, - Vec<_>, - ) = shape - .visible_strokes() - .rev() - .map(|stroke| { - text::stroke_paragraph_builder_group_from_text( - text_content, - stroke, - &shape.selrect(), - Some(true), - ) - }) - .unzip(); - - if let Some(parent_shadows) = parent_shadows { - if !skip_drop_shadows { - if !shape.has_visible_strokes() { - for shadow in parent_shadows { - text::render( - Some(self), - None, - &shape, - &mut paragraphs_with_shadows, - text_drop_shadows_surface_id.into(), - Some(&shadow), - blur_filter.as_ref(), - None, - None, - )?; - } - } else { - shadows::render_text_shadows( - self, - &shape, - &mut paragraphs_with_shadows, - &mut stroke_paragraphs_with_shadows_list, - text_drop_shadows_surface_id.into(), - &parent_shadows, - &blur_filter, - &stroke_kinds, + .visible_strokes() + .rev() + .map(|stroke| { + text::stroke_paragraph_builder_group_from_text( text_content, - )?; - } - } - } else { - // 1. Text drop shadows - if !shape.has_visible_strokes() { - for shadow in &drop_shadows { - text::render( - Some(self), + stroke, + &shape.selrect(), None, - &shape, - &mut paragraphs_with_shadows, - text_drop_shadows_surface_id.into(), - Some(shadow), - blur_filter.as_ref(), - None, - None, - )?; - } - } - - // 2. Text fills + ) + }) + .unzip(); + if skip_effects { + // Fast path: render fills and strokes only (skip shadows/blur). text::render( Some(self), None, @@ -1961,25 +1817,11 @@ impl RenderState { &mut paragraph_builders, Some(fills_surface_id), None, - blur_filter.as_ref(), + None, text_fill_inset, None, )?; - // 3. Stroke drop shadows - shadows::render_text_shadows( - self, - &shape, - &mut paragraphs_with_shadows, - &mut stroke_paragraphs_with_shadows_list, - text_drop_shadows_surface_id.into(), - &drop_shadows, - &blur_filter, - &stroke_kinds, - text_content, - )?; - - // 4. Stroke fills for (i, (stroke_paragraphs, layer_opacity)) in stroke_paragraphs_list .iter_mut() .zip(stroke_opacities.iter()) @@ -1995,7 +1837,7 @@ impl RenderState { stroke_paragraphs, &mut fill_builders, Some(strokes_surface_id), - blur_filter.as_ref(), + None, text_stroke_blur_outset, *layer_opacity, )?; @@ -2006,7 +1848,7 @@ impl RenderState { &shape, stroke_paragraphs, Some(strokes_surface_id), - blur_filter.as_ref(), + None, text_stroke_blur_outset, *layer_opacity, )?; @@ -2018,7 +1860,7 @@ impl RenderState { stroke_paragraphs, Some(strokes_surface_id), None, - blur_filter.as_ref(), + None, text_stroke_blur_outset, None, *layer_opacity, @@ -2026,7 +1868,7 @@ impl RenderState { } } - if shape.has_visible_strokes() && text_content.has_non_ascii() { + if shape.has_visible_strokes() && text_content.has_emoji() { let mut emoji_builders = text_content.paragraph_builder_group_opaque(); let mut deco_builders = @@ -2037,41 +1879,236 @@ impl RenderState { &mut emoji_builders, &mut deco_builders, strokes_surface_id, - blur_filter.as_ref(), + None, ); } + } else { + let shape_scale = self.get_scale(); + let mut drop_shadows = if skip_drop_shadows { + Vec::new() + } else { + shape + .drop_shadows_visible() + .filter(|s| s.is_perceptible_at_scale(shape_scale)) + .map(|shadow| { + let mut paint = skia_safe::Paint::default(); + paint.set_image_filter(shadow.get_drop_shadow_filter()); + paint + }) + .collect() + }; - // 5. Stroke inner shadows - shadows::render_text_shadows( - self, - &shape, - &mut paragraphs_with_shadows, - &mut stroke_paragraphs_with_shadows_list, - Some(innershadows_surface_id), - &inner_shadows, - &blur_filter, - &stroke_kinds, - text_content, - )?; + if !skip_drop_shadows { + if let Some(inherited_shadows) = self.get_inherited_drop_shadows() { + drop_shadows.extend(inherited_shadows); + } + } - // 6. Fill Inner shadows - if !shape.has_visible_strokes() { - for shadow in &inner_shadows { - text::render( - Some(self), - None, + let inner_shadows = shape.inner_shadow_paints(); + let blur_filter = shape.image_filter(1.); + // Shadow builder groups only serve shadow passes. + let needs_shadow_groups = parent_shadows.is_some() + || !drop_shadows.is_empty() + || !inner_shadows.is_empty(); + let mut paragraphs_with_shadows = if needs_shadow_groups { + text_content.paragraph_builder_group_from_text(Some(true)) + } else { + Vec::new() + }; + let (mut stroke_paragraphs_with_shadows_list, _shadow_opacities): ( + Vec<_>, + Vec<_>, + ) = if needs_shadow_groups { + shape + .visible_strokes() + .rev() + .map(|stroke| { + text::stroke_paragraph_builder_group_from_text( + text_content, + stroke, + &shape.selrect(), + Some(true), + ) + }) + .unzip() + } else { + (Vec::new(), Vec::new()) + }; + + if let Some(parent_shadows) = parent_shadows { + if !skip_drop_shadows { + if !shape.has_visible_strokes() { + for shadow in parent_shadows { + text::render( + Some(self), + None, + &shape, + &mut paragraphs_with_shadows, + text_drop_shadows_surface_id.into(), + Some(&shadow), + blur_filter.as_ref(), + None, + None, + )?; + } + } else { + shadows::render_text_shadows( + self, + &shape, + &mut paragraphs_with_shadows, + &mut stroke_paragraphs_with_shadows_list, + text_drop_shadows_surface_id.into(), + &parent_shadows, + &blur_filter, + &stroke_kinds, + text_content, + )?; + } + } + } else { + // 1. Text drop shadows + if !shape.has_visible_strokes() { + for shadow in &drop_shadows { + text::render( + Some(self), + None, + &shape, + &mut paragraphs_with_shadows, + text_drop_shadows_surface_id.into(), + Some(shadow), + blur_filter.as_ref(), + None, + None, + )?; + } + } + + // 2. Text fills + text::render( + Some(self), + None, + &shape, + &mut paragraph_builders, + Some(fills_surface_id), + None, + blur_filter.as_ref(), + text_fill_inset, + None, + )?; + + // 3. Stroke drop shadows + shadows::render_text_shadows( + self, + &shape, + &mut paragraphs_with_shadows, + &mut stroke_paragraphs_with_shadows_list, + text_drop_shadows_surface_id.into(), + &drop_shadows, + &blur_filter, + &stroke_kinds, + text_content, + )?; + + // 4. Stroke fills + for (i, (stroke_paragraphs, layer_opacity)) in + stroke_paragraphs_list + .iter_mut() + .zip(stroke_opacities.iter()) + .enumerate() + { + if stroke_kinds[i] == StrokeKind::Inner { + let mut fill_builders = + text_content.paragraph_builder_group_from_text(None); + text::render_inner_stroke( + Some(self), + None, + &shape, + stroke_paragraphs, + &mut fill_builders, + Some(strokes_surface_id), + blur_filter.as_ref(), + text_stroke_blur_outset, + *layer_opacity, + )?; + } else if stroke_kinds[i] == StrokeKind::Outer { + text::render_outer_stroke( + Some(self), + None, + &shape, + stroke_paragraphs, + Some(strokes_surface_id), + blur_filter.as_ref(), + text_stroke_blur_outset, + *layer_opacity, + )?; + } else { + text::render_with_bounds_outset( + Some(self), + None, + &shape, + stroke_paragraphs, + Some(strokes_surface_id), + None, + blur_filter.as_ref(), + text_stroke_blur_outset, + None, + *layer_opacity, + )?; + } + } + + if shape.has_visible_strokes() && text_content.has_emoji() { + let mut emoji_builders = + text_content.paragraph_builder_group_opaque(); + let mut deco_builders = + text_content.paragraph_builder_group_from_text(None); + text::render_emoji_overlay( + self, &shape, - &mut paragraphs_with_shadows, - Some(innershadows_surface_id), - Some(shadow), + &mut emoji_builders, + &mut deco_builders, + strokes_surface_id, blur_filter.as_ref(), - None, - None, - )?; + ); + } + + // 5. Stroke inner shadows + shadows::render_text_shadows( + self, + &shape, + &mut paragraphs_with_shadows, + &mut stroke_paragraphs_with_shadows_list, + Some(innershadows_surface_id), + &inner_shadows, + &blur_filter, + &stroke_kinds, + text_content, + )?; + + // 6. Fill Inner shadows + if !shape.has_visible_strokes() { + for shadow in &inner_shadows { + text::render( + Some(self), + None, + &shape, + &mut paragraphs_with_shadows, + Some(innershadows_surface_id), + Some(shadow), + blur_filter.as_ref(), + None, + None, + )?; + } } } } } + + if !text_content.is_vertical() && self.options.is_text_grid_visible() { + let canvas = self.surfaces.canvas_and_mark_dirty(fills_surface_id); + text::paint_horizontal_grid(canvas, &shape, text_content); + } } // end layout-cache miss fallback } _ => { @@ -4914,10 +4951,6 @@ impl RenderState { (self.viewbox.zoom - self.cached_viewbox.zoom).abs() > f32::EPSILON } - pub fn mark_touched(&mut self, uuid: Uuid) { - self.mark_touched_with_prev(uuid, None); - } - pub fn mark_touched_with_prev(&mut self, uuid: Uuid, prev_extrect: Option) { if self.touched_ids.insert(uuid) { if let Some(rect) = prev_extrect.filter(|r| !r.is_empty()) { diff --git a/render-wasm/src/render/fonts.rs b/render-wasm/src/render/fonts.rs index b1a09531cc..e8d35bad4e 100644 --- a/render-wasm/src/render/fonts.rs +++ b/render-wasm/src/render/fonts.rs @@ -1,7 +1,9 @@ use skia_safe::{self as skia, textlayout, Font, FontMgr}; -use std::collections::{HashMap, HashSet}; +use std::collections::{BTreeSet, HashMap, HashSet}; use crate::error::{Error, Result}; +use crate::shapes::cjk_chars::{is_hangul, is_kana}; +use crate::shapes::text_policy::LanguageTag; use crate::shapes::{FontFamily, FontStyle}; use crate::uuid::Uuid; @@ -19,13 +21,114 @@ fn default_font_uuid() -> Uuid { Uuid::nil() } +/// CJK region whose glyph forms a fallback face draws, read from its family +/// name (`Noto Sans JP`, `Noto Sans TC`, ...). The declaration order is the +/// fallback order when no language prefers a face. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum CjkFace { + SimplifiedChinese, + TraditionalChinese, + HongKongChinese, + Japanese, + Korean, +} + +impl CjkFace { + fn from_family_name(name: &str) -> Option { + match name.rsplit(' ').next()? { + "SC" => Some(Self::SimplifiedChinese), + "TC" => Some(Self::TraditionalChinese), + "HK" => Some(Self::HongKongChinese), + "JP" => Some(Self::Japanese), + "KR" => Some(Self::Korean), + _ => None, + } + } + + /// Faces drawing Han and CJK punctuation in the forms `language` (a BCP 47 + /// tag) expects, best first. Without a CJK language, kana or hangul in + /// `text` decide. + fn preferred(language: &str, text: &str) -> &'static [Self] { + let tag = LanguageTag::parse(language); + if tag.is("ja") { + &[Self::Japanese] + } else if tag.is("ko") { + &[Self::Korean] + } else if tag.is("zh") { + if tag.has_region(&["HK", "MO"]) { + &[Self::HongKongChinese, Self::TraditionalChinese] + } else if tag.has_region(&["TW"]) || tag.has_script("Hant") { + &[Self::TraditionalChinese, Self::HongKongChinese] + } else { + &[Self::SimplifiedChinese] + } + } else if text.chars().any(is_kana) { + &[Self::Japanese] + } else if text.chars().any(is_hangul) { + &[Self::Korean] + } else { + &[] + } + } +} + +/// Families that take part in character fallback, with the CJK region of +/// those that draw Han. +#[derive(Debug, Default)] +pub struct FallbackFonts { + families: BTreeSet, + cjk_faces: HashMap, +} + +impl FallbackFonts { + /// Fallback families without CJK regions, in name order. + #[cfg(test)] + pub fn from_families(families: impl IntoIterator) -> Self { + Self { + families: families.into_iter().collect(), + cjk_faces: HashMap::new(), + } + } + + fn insert(&mut self, alias: String, cjk_face: Option) { + if let Some(cjk_face) = cjk_face { + self.cjk_faces.insert(alias.clone(), cjk_face); + } + self.families.insert(alias); + } + + #[cfg(test)] + pub fn contains(&self, alias: &str) -> bool { + self.families.contains(alias) + } + + /// Fallback families for `text` in `language`: the CJK faces the language + /// prefers, then the non-CJK faces, then the other CJK faces. CJK faces + /// share Han characters with different glyph forms, so the first one + /// decides how they look. + pub fn for_text(&self, language: &str, text: &str) -> Vec { + let preferred = CjkFace::preferred(language, text); + let mut families: Vec<&String> = self.families.iter().collect(); + families.sort_by_key(|alias| match self.cjk_faces.get(*alias) { + Some(face) => match preferred.iter().position(|p| p == face) { + Some(rank) => (0, rank), + None => (2, *face as usize), + }, + None => (1, 0), + }); + families.into_iter().cloned().collect() + } +} + pub struct FontStore { font_mgr: FontMgr, font_provider: textlayout::TypefaceFontProvider, font_collection: textlayout::FontCollection, debug_font: Font, ui_font: Font, - fallback_fonts: HashSet, + fallback_fonts: FallbackFonts, + /// CJK region of each registered family that draws Han. + cjk_faces: HashMap, /// Source URL registered when the font was fetched (SVG export references /// this in `@font-face` rules). source_urls: HashMap, @@ -62,7 +165,8 @@ impl FontStore { font_collection, debug_font, ui_font, - fallback_fonts: HashSet::new(), + fallback_fonts: FallbackFonts::default(), + cjk_faces: HashMap::new(), source_urls: HashMap::new(), }) } @@ -95,7 +199,11 @@ impl FontStore { is_emoji: bool, is_fallback: bool, ) -> Result<()> { + let alias = format!("{}", family); if self.has_family(&family, is_emoji) { + if is_fallback { + self.insert_fallback(alias); + } return Ok(()); } @@ -106,23 +214,39 @@ impl FontStore { "Failed to create typeface".to_string(), ))?; - let alias = format!("{}", family); let font_name = if is_emoji { DEFAULT_EMOJI_FONT } else { alias.as_str() }; + if let Some(cjk_face) = CjkFace::from_family_name(&typeface.family_name()) { + self.cjk_faces.insert(alias.clone(), cjk_face); + } self.font_provider.register_typeface(typeface, font_name); self.font_collection.clear_caches(); if is_fallback { - self.fallback_fonts.insert(alias); + self.insert_fallback(alias); } Ok(()) } + fn insert_fallback(&mut self, alias: String) { + let cjk_face = self.cjk_faces.get(&alias).copied(); + self.fallback_fonts.insert(alias, cjk_face); + } + + /// Upgrade an already-uploaded family to take part in character fallback. + /// Font bytes are cached apart from a face's role, so a family may arrive + /// as a document font and later be requested as a fallback. + pub fn mark_as_fallback(&mut self, family: &FontFamily) { + if self.has_family(family, false) { + self.insert_fallback(format!("{}", family)); + } + } + pub fn has_family(&self, family: &FontFamily, is_emoji: bool) -> bool { let alias = format!("{}", family); let font_name = if is_emoji { @@ -133,7 +257,7 @@ impl FontStore { self.font_provider.family_names().any(|x| x == font_name) } - pub fn get_fallback(&self) -> &HashSet { + pub fn get_fallback(&self) -> &FallbackFonts { &self.fallback_fonts } @@ -309,4 +433,76 @@ mod tests { assert!(css.is_empty()); } + + const TEST_FONT: &[u8] = include_bytes!("../fonts/sourcesanspro-regular.ttf"); + + fn cjk_fallbacks() -> FallbackFonts { + let mut fallbacks = FallbackFonts::default(); + fallbacks.insert("arabic".to_string(), None); + fallbacks.insert("jp".to_string(), Some(CjkFace::Japanese)); + fallbacks.insert("kr".to_string(), Some(CjkFace::Korean)); + fallbacks.insert("sc".to_string(), Some(CjkFace::SimplifiedChinese)); + fallbacks.insert("tc".to_string(), Some(CjkFace::TraditionalChinese)); + fallbacks.insert("hk".to_string(), Some(CjkFace::HongKongChinese)); + fallbacks + } + + #[test] + fn cjk_face_comes_from_the_family_region_suffix() { + assert_eq!( + CjkFace::from_family_name("Noto Sans JP"), + Some(CjkFace::Japanese) + ); + assert_eq!( + CjkFace::from_family_name("Noto Sans HK"), + Some(CjkFace::HongKongChinese) + ); + assert_eq!(CjkFace::from_family_name("Noto Sans Arabic"), None); + assert_eq!(CjkFace::from_family_name(""), None); + } + + #[test] + fn paragraph_language_puts_its_cjk_face_first() { + let fallbacks = cjk_fallbacks(); + assert_eq!(fallbacks.for_text("ja", "東京")[0], "jp"); + assert_eq!(fallbacks.for_text("ko", "漢字")[0], "kr"); + assert_eq!(fallbacks.for_text("zh-Hans", "东京")[0], "sc"); + assert_eq!(fallbacks.for_text("zh-Hant-TW", "東京")[..2], ["tc", "hk"]); + assert_eq!(fallbacks.for_text("zh-Hant-HK", "東京")[..2], ["hk", "tc"]); + assert_eq!(fallbacks.for_text("zh_hant", "東京")[0], "tc"); + } + + #[test] + fn untagged_text_prefers_the_face_of_its_kana_or_hangul() { + let fallbacks = cjk_fallbacks(); + assert_eq!(fallbacks.for_text("", "東京へ行く")[0], "jp"); + assert_eq!(fallbacks.for_text("en", "서울")[0], "kr"); + } + + #[test] + fn fallback_order_is_stable_without_a_preference() { + let fallbacks = cjk_fallbacks(); + assert_eq!( + fallbacks.for_text("", "東京"), + ["arabic", "sc", "tc", "hk", "jp", "kr"] + ); + // Non-CJK faces follow the preferred face, other CJK faces come last. + assert_eq!( + fallbacks.for_text("ja", "東京"), + ["jp", "arabic", "sc", "tc", "hk", "kr"] + ); + } + + #[test] + fn uploaded_family_can_be_upgraded_to_fallback() { + let mut store = FontStore::try_new().unwrap(); + let family = FontFamily::new(Uuid::nil(), 400, FontStyle::Normal); + let alias = format!("{}", family); + + store.add(family, TEST_FONT, false, false).unwrap(); + assert!(!store.get_fallback().contains(&alias)); + + store.mark_as_fallback(&family); + assert!(store.get_fallback().contains(&alias)); + } } diff --git a/render-wasm/src/render/options.rs b/render-wasm/src/render/options.rs index 727073f18e..841082fa20 100644 --- a/render-wasm/src/render/options.rs +++ b/render-wasm/src/render/options.rs @@ -3,6 +3,7 @@ const DEBUG_VISIBLE: u32 = 0x01; const PROFILE_REBUILD_TILES: u32 = 0x02; const TEXT_EDITOR_V3: u32 = 0x04; const SHOW_WASM_INFO: u32 = 0x08; +const TEXT_GRID_VISIBLE: u32 = 0x10; // Render performance options // This is the extra area used for tile rendering (tiles beyond viewport). @@ -121,4 +122,10 @@ impl RenderOptions { pub fn show_wasm_info(&self) -> bool { self.flags & SHOW_WASM_INFO == SHOW_WASM_INFO } + + /// jlreq-style character-frame grid overlay for Japanese vertical + /// text: a developer aid, never exported. + pub fn is_text_grid_visible(&self) -> bool { + self.flags & TEXT_GRID_VISIBLE == TEXT_GRID_VISIBLE + } } diff --git a/render-wasm/src/render/svg/mod.rs b/render-wasm/src/render/svg/mod.rs index fee187db54..270d47e62b 100644 --- a/render-wasm/src/render/svg/mod.rs +++ b/render-wasm/src/render/svg/mod.rs @@ -1,6 +1,7 @@ use skia_safe::{self as skia}; use std::borrow::Cow; +use std::cell::Cell; use std::collections::HashSet; use crate::error::Result; @@ -53,6 +54,44 @@ fn svg_page_bounds(shape: &Shape, tree: ShapesPoolRef, scale: f32) -> skia::Rect } } +thread_local! { + /// Set while an SVG document is being written. + static WRITING_SVG: Cell = const { Cell::new(false) }; +} + +/// True while an SVG export draws. `SkSVGDevice` rebuilds `` from glyph +/// IDs through the font's cmap, which loses vertical alternates and maps +/// shared glyphs to the wrong code point, so vertical text draws outlines. +pub(crate) fn writing_svg() -> bool { + WRITING_SVG.with(Cell::get) +} + +/// Glyph output for vertical text drawn on the current canvas: outlines +/// while an SVG export draws, text blobs otherwise. +pub(crate) fn vertical_glyph_output() -> crate::shapes::text_vertical::GlyphOutput { + if writing_svg() { + crate::shapes::text_vertical::GlyphOutput::Outlines + } else { + crate::shapes::text_vertical::GlyphOutput::Text + } +} + +/// Marks the SVG export as running until dropped. +struct WritingSvg; + +impl WritingSvg { + fn start() -> Self { + WRITING_SVG.with(|writing| writing.set(true)); + Self + } +} + +impl Drop for WritingSvg { + fn drop(&mut self) { + WRITING_SVG.with(|writing| writing.set(false)); + } +} + /// Renders a shape tree to an SVG document and returns the raw SVG bytes. /// /// Dedicated vector-SVG render path. Leaf content (paths, fills, …) is emitted @@ -98,7 +137,9 @@ pub(crate) fn render_tree_to_svg( let page_h = bounds.height() * scale; let rect = skia::Rect::from_xywh(0., 0., page_w, page_h); + let writing = WritingSvg::start(); let (defs, body) = render_body(shared, id, tree, scale, rect, -bounds.left(), -bounds.top())?; + drop(writing); let mut aliases = HashSet::new(); collect_font_aliases(tree, id, &mut aliases); diff --git a/render-wasm/src/render/svg/tests.rs b/render-wasm/src/render/svg/tests.rs index 96178a7c0a..55f8135e8e 100644 --- a/render-wasm/src/render/svg/tests.rs +++ b/render-wasm/src/render/svg/tests.rs @@ -4043,3 +4043,75 @@ fn raster_export_of_masked_group_draws_inner_shadow() { // Outside the circle the mask still clears everything. assert_eq!(raster_pixel(&png, (3, 3)).a(), 0); } + +#[test] +fn exports_vertical_text_as_glyph_outlines() { + let mut pool = ShapesPool::new(); + let id = uid(1); + add_solid_text( + &mut pool, + id, + (0.0, 0.0, 240.0, 560.0), + "HOLA", + 200.0, + skia::Color::from_rgb(0, 63, 255), + ); + let shape = pool.get_mut(&id).expect("text shape"); + if let crate::shapes::Type::Text(content) = &mut shape.shape_type { + for paragraph in content.paragraphs_mut() { + paragraph.set_writing_mode(crate::shapes::WritingMode::VerticalRl); + } + } + + let svg = render(&pool, id); + assert!( + !svg.contains(": {svg}" + ); + assert!( + svg.contains(" Result<()> { let Type::Text(text_content) = &shape.shape_type else { return Ok(()); }; + // The vertical pass paints span fills; clips and luminance masks need an + // opaque black silhouette. + let mut silhouette = skia_safe::Paint::default(); + silhouette.set_color(skia_safe::Color::BLACK); + silhouette.set_anti_alias(true); + if text_vertical::paint_text_vertical_with( + canvas, + text_content, + &shape.selrect(), + shape.vertical_align(), + &silhouette, + text_vertical::GlyphOutput::Outlines, + ) { + return Ok(()); + } let text_content = text_content.new_bounds(shape.selrect()); let mut mask_builders = text_content.paragraph_builder_group_opaque(); text::render_overlay_emoji(canvas, shape, &mut mask_builders, None, None, None, None)?; @@ -325,6 +340,21 @@ fn paint_text_stroke_opaque(canvas: &Canvas, shape: &Shape, stroke: &Stroke) -> let Type::Text(text_content) = &shape.shape_type else { return Ok(()); }; + if text_content.is_vertical() { + // One cached layout serves every stroke paint. + let (stroke_paints, _) = text::get_text_stroke_paints(stroke, &shape.selrect(), false); + for paint in &stroke_paints { + text_vertical::paint_text_vertical_with( + canvas, + text_content, + &shape.selrect(), + shape.vertical_align(), + paint, + text_vertical::GlyphOutput::Outlines, + ); + } + return Ok(()); + } let text_content = text_content.new_bounds(shape.selrect()); let stroke_blur_outset = Stroke::max_bounds_width(shape.visible_strokes(), false); let (mut stroke_paragraphs, _) = text::stroke_paragraph_builder_group_from_text( diff --git a/render-wasm/src/render/text.rs b/render-wasm/src/render/text.rs index 491eb83248..7ca2138422 100644 --- a/render-wasm/src/render/text.rs +++ b/render-wasm/src/render/text.rs @@ -1,22 +1,163 @@ use super::{filters, RenderState, Shape, SurfaceId, DEFAULT_EMOJI_FONT}; +use crate::shapes::text_paragraph::{HorizontalBuilder, LayoutParagraph}; use crate::{ error::Result, math::{Point, Rect}, shapes::{ - add_text_with_tabs, calculate_text_layout_data, set_paint_fill, vertical_align_offset, - ListMarker, ListStyleType, Paragraph as TextParagraph, ParagraphBuilderGroup, - ParagraphLayout, Stroke, StrokeKind, TextContent, TextDecorationSegment, TextDirection, - LIST_MARKER_CENTER_FACTOR, LIST_MARKER_GAP_FACTOR, LIST_MARKER_MIN_RADIUS, - LIST_MARKER_RADIUS_FACTOR, + add_horizontal_span, calculate_text_layout_data, set_paint_fill, text_vertical, + vertical_align_offset, HorizontalOffsets, HorizontalSpanRange, ListMarker, ListStyleType, + Paragraph as TextParagraph, ParagraphBuilderGroup, ParagraphLayout, Stroke, StrokeKind, + TextContent, TextDecorationSegment, TextDirection, LIST_MARKER_CENTER_FACTOR, + LIST_MARKER_GAP_FACTOR, LIST_MARKER_MIN_RADIUS, LIST_MARKER_RADIUS_FACTOR, }, utils::{get_fallback_fonts, get_font_collection}, }; use skia_safe::{ self as skia, canvas::SaveLayerRec, - textlayout::{ParagraphBuilder, StyleMetrics, TextDecoration}, + textlayout::{RectHeightStyle, RectWidthStyle, StyleMetrics, TextDecoration}, Canvas, ImageFilter, Paint, }; +use std::borrow::Cow; +use text_vertical::GlyphOutput; + +/// Vertical text: shadows, fill, strokes and the debug grid, all painted from +/// one layout. Rebinds the content to the selrect: stored text bounds +/// describe the measured content and can be taller than a fixed shape, whose +/// height is the column-wrap budget. +pub fn render_vertical_text( + state: &mut RenderState, + shape: &Shape, + text_content: &TextContent, + fills_surface_id: SurfaceId, + strokes_surface_id: SurfaceId, + skip_effects: bool, +) -> Result<()> { + let blur_filter = (!skip_effects).then(|| shape.image_filter(1.0)).flatten(); + let bounds = shape.selrect(); + let vertical_align = shape.vertical_align(); + let layout = text_content.vertical_layout(&bounds); + + let skip_shadows = skip_effects || state.should_skip_drop_shadows(); + if !skip_shadows { + let mut drop_shadows = shape.drop_shadow_paints(); + if let Some(inherited_shadows) = state.get_inherited_drop_shadows() { + drop_shadows.extend(inherited_shadows); + } + if !drop_shadows.is_empty() { + let canvas = state.surfaces.canvas_and_mark_dirty(fills_surface_id); + for shadow in &drop_shadows { + text_vertical::paint_drop_shadow( + canvas, + &layout, + &bounds, + vertical_align, + shadow, + GlyphOutput::Text, + ); + } + } + } + + render_to_surface( + state, + shape, + fills_surface_id, + blur_filter.as_ref(), + 0.0, + |canvas| { + paint_vertical_fill( + canvas, + shape, + &layout, + &bounds, + blur_filter.as_ref(), + GlyphOutput::Text, + ) + }, + )?; + + let strokes: Vec<&Stroke> = shape.visible_strokes().rev().collect(); + if !strokes.is_empty() { + let selrect = shape.selrect(); + let canvas = state.surfaces.canvas_and_mark_dirty(strokes_surface_id); + for stroke in strokes { + text_vertical::paint_stroke( + canvas, + &layout, + &bounds, + vertical_align, + stroke, + &selrect, + blur_filter.as_ref(), + GlyphOutput::Text, + ); + } + } + + if state.options.is_text_grid_visible() { + let canvas = state.surfaces.canvas_and_mark_dirty(fills_surface_id); + text_vertical::paint_grid(canvas, &layout, &bounds, vertical_align); + } + + Ok(()) +} + +/// Paint a viewport-only debug grid over SkParagraph horizontal text: blue +/// line boxes, green per-scalar tight rects from SkParagraph, amber +/// baselines. Matches the vertical text grid and shows horizontal annotation +/// anchors. +pub fn paint_horizontal_grid(canvas: &Canvas, shape: &Shape, text_content: &TextContent) { + let mut builders = text_content.paragraph_builder_group_from_text(None); + let layout = calculate_text_layout_data(shape, text_content, &mut builders, true); + + let mut line_paint = Paint::default(); + line_paint.set_anti_alias(true); + line_paint.set_style(skia::PaintStyle::Stroke); + line_paint.set_stroke_width(1.0); + line_paint.set_color(skia::Color::from_argb(0xAA, 0x2F, 0x80, 0xED)); + + let mut glyph_paint = Paint::default(); + glyph_paint.set_anti_alias(true); + glyph_paint.set_style(skia::PaintStyle::Stroke); + glyph_paint.set_stroke_width(1.0); + glyph_paint.set_color(skia::Color::from_argb(0x99, 0x27, 0xAE, 0x60)); + + let mut baseline_paint = Paint::default(); + baseline_paint.set_anti_alias(true); + baseline_paint.set_stroke_width(1.0); + baseline_paint.set_color(skia::Color::from_argb(0xCC, 0xEB, 0x57, 0x57)); + + for paragraph in &layout.paragraphs { + for line in paragraph.paragraph.get_line_metrics() { + let baseline = line.baseline as f32; + let top = paragraph.y + baseline - line.ascent as f32; + let left = paragraph.x + line.left as f32; + let width = line.width as f32; + canvas.draw_rect( + Rect::from_xywh(left, top, width, line.height as f32), + &line_paint, + ); + canvas.draw_line( + (left, paragraph.y + baseline), + (left + width, paragraph.y + baseline), + &baseline_paint, + ); + + for offset in line.start_index..line.end_index { + for textbox in paragraph.paragraph.get_rects_for_range( + offset..offset + 1, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ) { + let mut rect = textbox.rect; + rect.offset((paragraph.x, paragraph.y)); + canvas.draw_rect(rect, &glyph_paint); + } + } + } + } +} pub fn stroke_paragraph_builder_group_from_text( text_content: &TextContent, @@ -30,11 +171,42 @@ pub fn stroke_paragraph_builder_group_from_text( let remove_stroke_alpha = use_shadow.unwrap_or(false) && !stroke.is_transparent(); let mut group_layer_opacity: Option = None; - for paragraph in text_content.paragraphs() { - let mut stroke_paragraphs_map: std::collections::HashMap = + let plans = text_content.horizontal_plans(); + let adjustments = text_content.horizontal_line_adjustments(); + for (index, (paragraph, plan)) in text_content + .paragraphs() + .iter() + .zip(plans.iter()) + .enumerate() + { + let adjustment = adjustments + .as_ref() + .and_then(|adjustments| adjustments[index].as_ref()); + let mut stroke_paragraphs_map: std::collections::HashMap = std::collections::HashMap::new(); - for span in paragraph.children().iter() { + for (span_index, (((span, text), sheds), extra)) in paragraph + .children() + .iter() + .zip(&plan.texts) + .zip(&plan.pair_spacing) + .zip(&plan.ruby_spacing.adjustments) + .enumerate() + { + let sheds: Cow<'_, [(usize, f32)]> = adjustment + .map_or(Cow::Borrowed(sheds.as_slice()), |a| { + Cow::Owned(sheds.iter().chain(&a.sheds[span_index]).copied().collect()) + }); + let extra: Cow<'_, [(usize, f32)]> = + adjustment.map_or(Cow::Borrowed(extra.as_slice()), |a| { + Cow::Owned( + extra + .iter() + .chain(&a.spacing[span_index]) + .copied() + .collect(), + ) + }); let (stroke_paints, stroke_layer_opacity) = get_text_stroke_paints(stroke, bounds, remove_stroke_alpha); @@ -42,12 +214,20 @@ pub fn stroke_paragraph_builder_group_from_text( group_layer_opacity = stroke_layer_opacity; } - let text: String = span.apply_text_transform(); - for (paint_idx, stroke_paint) in stroke_paints.iter().enumerate() { let builder = stroke_paragraphs_map.entry(paint_idx).or_insert_with(|| { - let paragraph_style = paragraph.paragraph_to_style(); - ParagraphBuilder::new(¶graph_style, fonts) + let mut paragraph_style = paragraph.paragraph_to_style(); + if plan.has_atomic_overflow() || adjustment.is_some() { + paragraph_style.set_apply_rounding_hack(false); + if matches!( + paragraph.text_align(), + crate::shapes::TextAlign::Justify + | crate::shapes::TextAlign::Distribute + ) { + paragraph_style.set_text_align(skia::textlayout::TextAlign::Left); + } + } + HorizontalBuilder::new(¶graph_style, fonts) }); let stroke_paint = stroke_paint.clone(); let remove_alpha = use_shadow.unwrap_or(false) && !span.is_transparent(); @@ -58,11 +238,11 @@ pub fn stroke_paragraph_builder_group_from_text( paragraph.line_height(), ); builder.push_style(&stroke_style); - add_text_with_tabs(builder, &text, span.font_size); + add_horizontal_span(builder, span, text, &sheds, &extra, &stroke_style, fonts); } } - let stroke_paragraphs: Vec = (0..stroke_paragraphs_map.len()) + let stroke_paragraphs: ParagraphBuilderGroup = (0..stroke_paragraphs_map.len()) .filter_map(|i| stroke_paragraphs_map.remove(&i)) .collect(); @@ -72,7 +252,7 @@ pub fn stroke_paragraph_builder_group_from_text( (paragraph_group, group_layer_opacity) } -fn get_text_stroke_paints( +pub(crate) fn get_text_stroke_paints( stroke: &Stroke, bounds: &Rect, remove_stroke_alpha: bool, @@ -141,7 +321,7 @@ pub fn render_with_bounds_outset( render_state: Option<&mut RenderState>, canvas: Option<&Canvas>, shape: &Shape, - paragraph_builders: &mut [Vec], + paragraph_builders: &mut [ParagraphBuilderGroup], surface_id: Option, shadow: Option<&Paint>, blur: Option<&ImageFilter>, @@ -169,7 +349,7 @@ pub fn render_with_bounds_outset( pub fn render_with_bounds_outset_overlay_emoji( canvas: &Canvas, shape: &Shape, - paragraph_builders: &mut [Vec], + paragraph_builders: &mut [ParagraphBuilderGroup], shadow: Option<&Paint>, blur: Option<&ImageFilter>, stroke_bounds_outset: f32, @@ -191,12 +371,77 @@ pub fn render_with_bounds_outset_overlay_emoji( ) } +/// Run `paint` on `target_surface`. With a blur, it paints into a filter +/// surface sized to the blurred text bounds (grown by `stroke_bounds_outset`) +/// when those bounds are usable. +fn render_to_surface( + render_state: &mut RenderState, + shape: &Shape, + target_surface: SurfaceId, + blur: Option<&ImageFilter>, + stroke_bounds_outset: f32, + mut paint: impl FnMut(&Canvas), +) -> Result<()> { + if let Some(blur_filter) = blur { + let mut text_bounds = shape + .get_text_content() + .calculate_bounds(shape, false) + .to_rect(); + if stroke_bounds_outset > 0.0 { + text_bounds.inset((-stroke_bounds_outset, -stroke_bounds_outset)); + } + let bounds = blur_filter.compute_fast_bounds(text_bounds); + if bounds.is_finite() + && bounds.width() > 0.0 + && bounds.height() > 0.0 + && filters::render_with_filter_surface( + render_state, + bounds, + target_surface, + |state, temp_surface| { + paint(state.surfaces.canvas(temp_surface)); + Ok(()) + }, + )? + { + return Ok(()); + } + } + paint(render_state.surfaces.canvas_and_mark_dirty(target_surface)); + Ok(()) +} + +/// Fill pass of vertical text: `layout` painted inside the shape's layer +/// blur. +pub fn paint_vertical_fill( + canvas: &Canvas, + shape: &Shape, + layout: &text_vertical::VerticalLayout, + bounds: &Rect, + blur: Option<&ImageFilter>, + output: GlyphOutput, +) { + if let Some(blur_filter) = blur { + let mut blur_paint = Paint::default(); + blur_paint.set_image_filter(blur_filter.clone()); + canvas.save_layer( + &SaveLayerRec::default() + .bounds(&shape.layer_bounds()) + .paint(&blur_paint), + ); + } + text_vertical::paint_layout(canvas, layout, bounds, shape.vertical_align(), output); + if blur.is_some() { + canvas.restore(); + } +} + #[allow(clippy::too_many_arguments)] fn render_with_bounds_outset_inner( render_state: Option<&mut RenderState>, canvas: Option<&Canvas>, shape: &Shape, - paragraph_builders: &mut [Vec], + paragraph_builders: &mut [ParagraphBuilderGroup], surface_id: Option, shadow: Option<&Paint>, blur: Option<&ImageFilter>, @@ -206,55 +451,25 @@ fn render_with_bounds_outset_inner( overlay_emoji: bool, ) -> Result<()> { if let Some(render_state) = render_state { - let target_surface = surface_id.unwrap_or(SurfaceId::Fills); - - if let Some(blur_filter) = blur { - let mut text_bounds = shape - .get_text_content() - .calculate_bounds(shape, false) - .to_rect(); - if stroke_bounds_outset > 0.0 { - text_bounds.inset((-stroke_bounds_outset, -stroke_bounds_outset)); - } - let bounds = blur_filter.compute_fast_bounds(text_bounds); - if bounds.is_finite() && bounds.width() > 0.0 && bounds.height() > 0.0 { - let blur_filter_clone = blur_filter.clone(); - if filters::render_with_filter_surface( - render_state, - bounds, - target_surface, - |state, temp_surface| { - let temp_canvas = state.surfaces.canvas(temp_surface); - render_text_on_canvas( - temp_canvas, - shape, - paragraph_builders, - shadow, - Some(&blur_filter_clone), - fill_inset, - layer_opacity, - false, - ); - Ok(()) - }, - )? { - return Ok(()); - } - } - } - - let canvas = render_state.surfaces.canvas_and_mark_dirty(target_surface); - render_text_on_canvas( - canvas, + return render_to_surface( + render_state, shape, - paragraph_builders, - shadow, + surface_id.unwrap_or(SurfaceId::Fills), blur, - fill_inset, - layer_opacity, - false, + stroke_bounds_outset, + |canvas| { + render_text_on_canvas( + canvas, + shape, + paragraph_builders, + shadow, + blur, + fill_inset, + layer_opacity, + false, + ) + }, ); - return Ok(()); } if let Some(canvas) = canvas { @@ -277,7 +492,7 @@ pub fn render( render_state: Option<&mut RenderState>, canvas: Option<&Canvas>, shape: &Shape, - paragraph_builders: &mut [Vec], + paragraph_builders: &mut [ParagraphBuilderGroup], surface_id: Option, shadow: Option<&Paint>, blur: Option<&ImageFilter>, @@ -303,7 +518,7 @@ pub fn render( pub fn render_overlay_emoji( canvas: &Canvas, shape: &Shape, - paragraph_builders: &mut [Vec], + paragraph_builders: &mut [ParagraphBuilderGroup], shadow: Option<&Paint>, blur: Option<&ImageFilter>, fill_inset: Option, @@ -334,6 +549,9 @@ pub fn try_paint_from_layout_cache( layout_cache_rotation_only: bool, ) -> Result { let text_content = shape.get_text_content(); + if !text_content.can_paint_from_layout_cache() { + return Ok(false); + } let cache_usable = if layout_cache_rotation_only { text_content.layout_cache_versions_match() } else { @@ -358,6 +576,34 @@ pub fn try_paint_from_layout_cache( Ok(false) } +/// Paint the warichu, emphasis marks and ruby of the laid-out horizontal +/// paragraph `index` at `origin`, with the paint of this pass. +fn paint_horizontal_annotations( + canvas: &Canvas, + text_content: &TextContent, + index: usize, + laid_out: &LayoutParagraph, + origin: (f32, f32), +) { + paint_horizontal_notes_and_marks(canvas, text_content, index, laid_out, origin); + crate::shapes::paint_horizontal_ruby(canvas, text_content, index, laid_out, origin.0, origin.1); +} + +/// The warichu and emphasis marks of `paint_horizontal_annotations`. +fn paint_horizontal_notes_and_marks( + canvas: &Canvas, + text_content: &TextContent, + index: usize, + laid_out: &LayoutParagraph, + (x, y): (f32, f32), +) { + let plans = text_content.horizontal_plans(); + if let (Some(paragraph), Some(plan)) = (text_content.paragraphs().get(index), plans.get(index)) + { + crate::shapes::paint_horizontal_warichu(canvas, paragraph, plan, laid_out, x, y); + crate::shapes::paint_horizontal_emphasis(canvas, paragraph, plan, laid_out, x, y); + } +} fn paint_from_cached_layout(canvas: &Canvas, shape: &Shape, text_content: &TextContent) { let selrect = shape.selrect(); // Absolute image/gradient shaders were baked at `layout_paint_origin`. Paint @@ -385,16 +631,26 @@ fn paint_from_cached_layout(canvas: &Canvas, shape: &Shape, text_content: &TextC let vertical_offset = vertical_align_offset(selrect.height(), total_text_height, shape.vertical_align()); + let plans = text_content.horizontal_plans(); let top = base_y + vertical_offset; let mut y_accum = top; for (index, group) in paragraphs.iter().enumerate() { let Some(paragraph) = group.first() else { continue; }; + let x = x + plans + .get(index) + .zip(text_content.paragraphs().get(index)) + .map_or(0.0, |(plan, model)| plan.x_offset(model, selrect.width())); paragraph.paint(canvas, (x, y_accum)); - if draw_decorations { - if let Some(text_paragraph) = text_content.paragraphs().get(index) { - for deco in decoration_segments(paragraph, text_paragraph, x, y_accum) { + paint_horizontal_annotations(canvas, text_content, index, paragraph, (x, y_accum)); + if let (Some(text_paragraph), Some(plan)) = + (text_content.paragraphs().get(index), plans.get(index)) + { + if draw_decorations { + for deco in + decoration_segments(paragraph, text_paragraph, &plan.offsets, x, y_accum) + { draw_decoration_segment(canvas, &deco); } } @@ -421,7 +677,7 @@ fn paint_from_cached_layout(canvas: &Canvas, shape: &Shape, text_content: &TextC fn render_text_on_canvas( canvas: &Canvas, shape: &Shape, - paragraph_builders: &mut [Vec], + paragraph_builders: &mut [ParagraphBuilderGroup], shadow: Option<&Paint>, blur: Option<&ImageFilter>, fill_inset: Option, @@ -498,7 +754,7 @@ fn render_text_on_canvas( pub fn paint_text_paragraphs( canvas: &Canvas, shape: &Shape, - paragraph_builder_groups: &mut [Vec], + paragraph_builder_groups: &mut [ParagraphBuilderGroup], ) { paint_text_with_emoji_overlay(canvas, shape, paragraph_builder_groups, false); } @@ -507,7 +763,7 @@ pub fn paint_text_paragraphs( fn paint_text( canvas: &Canvas, shape: &Shape, - paragraph_builder_groups: &mut [Vec], + paragraph_builder_groups: &mut [ParagraphBuilderGroup], ) { paint_text_with_emoji_overlay(canvas, shape, paragraph_builder_groups, false); } @@ -533,23 +789,47 @@ pub fn paint_text_mask(canvas: &Canvas, shape: &Shape) { fn paint_text_with_emoji_overlay( canvas: &Canvas, shape: &Shape, - paragraph_builder_groups: &mut [Vec], + paragraph_builder_groups: &mut [ParagraphBuilderGroup], overlay_emoji: bool, ) { let text_content = shape.get_text_content(); + + // Vertical writing paints through the vertical pass. Stored text bounds + // describe the measured content and can be taller than a fixed shape, so + // the selrect is the column-wrap budget. + if crate::shapes::text_vertical::paint_text_vertical( + canvas, + text_content, + &shape.selrect(), + shape.vertical_align(), + crate::render::svg::vertical_glyph_output(), + ) { + return; + } + let mut layout_info = calculate_text_layout_data(shape, text_content, paragraph_builder_groups, true); for para in &mut layout_info.paragraphs { para.paragraph.paint(canvas, (para.x, para.y)); - + let (index, origin) = (para.source_paragraph, (para.x, para.y)); + paint_horizontal_notes_and_marks(canvas, text_content, index, ¶.paragraph, origin); + // The emoji overlay goes over the marks and under the ruby, which + // takes the paint of this pass like warichu and emphasis. if overlay_emoji { paint_emoji_overlay(canvas, para); } - if let Some(marker) = ¶.marker { draw_list_marker(canvas, marker); } + crate::shapes::paint_horizontal_ruby( + canvas, + text_content, + index, + ¶.paragraph, + origin.0, + origin.1, + ); for deco in ¶.decorations { draw_decoration_segment(canvas, deco); @@ -557,7 +837,7 @@ fn paint_text_with_emoji_overlay( } } -fn paragraph_has_emoji(paragraph: &mut skia::textlayout::Paragraph) -> bool { +fn paragraph_has_emoji(paragraph: &mut LayoutParagraph) -> bool { let line_bounds: Vec<(usize, usize)> = paragraph .get_line_metrics() .iter() @@ -569,7 +849,7 @@ fn paragraph_has_emoji(paragraph: &mut skia::textlayout::Paragraph) -> bool { } fn line_emoji_ranges( - paragraph: &mut skia::textlayout::Paragraph, + paragraph: &mut LayoutParagraph, line_start: usize, line_end: usize, ) -> Vec<(usize, usize)> { @@ -783,8 +1063,8 @@ fn paint_emoji_opaque( pub fn render_emoji_overlay( render_state: &mut RenderState, shape: &Shape, - emoji_builders: &mut [Vec], - deco_builders: &mut [Vec], + emoji_builders: &mut [ParagraphBuilderGroup], + deco_builders: &mut [ParagraphBuilderGroup], surface_id: SurfaceId, blur: Option<&ImageFilter>, ) { @@ -841,7 +1121,7 @@ pub fn render_emoji_overlay( fn draw_text( canvas: &Canvas, shape: &Shape, - paragraph_builder_groups: &mut [Vec], + paragraph_builder_groups: &mut [ParagraphBuilderGroup], layer_opacity: Option, overlay_emoji: bool, ) { @@ -875,9 +1155,9 @@ fn draw_text( fn render_masked_stroke_on_canvas( canvas: &Canvas, shape: &Shape, - mask_builders: &mut [Vec], - stroke_builders: &mut [Vec], - fill_builders: Option<&mut [Vec]>, + mask_builders: &mut [ParagraphBuilderGroup], + stroke_builders: &mut [ParagraphBuilderGroup], + fill_builders: Option<&mut [ParagraphBuilderGroup]>, stroke_mask_blend: skia::BlendMode, blur: Option<&ImageFilter>, layer_opacity: Option, @@ -955,9 +1235,9 @@ fn render_masked_stroke( render_state: Option<&mut RenderState>, canvas: Option<&Canvas>, shape: &Shape, - mask_builders: &mut [Vec], - stroke_builders: &mut [Vec], - mut fill_builders: Option<&mut [Vec]>, + mask_builders: &mut [ParagraphBuilderGroup], + stroke_builders: &mut [ParagraphBuilderGroup], + mut fill_builders: Option<&mut [ParagraphBuilderGroup]>, stroke_mask_blend: skia::BlendMode, surface_id: Option, blur: Option<&ImageFilter>, @@ -1037,8 +1317,8 @@ pub fn render_inner_stroke( render_state: Option<&mut RenderState>, canvas: Option<&Canvas>, shape: &Shape, - stroke_builders: &mut [Vec], - fill_builders: &mut [Vec], + stroke_builders: &mut [ParagraphBuilderGroup], + fill_builders: &mut [ParagraphBuilderGroup], surface_id: Option, blur: Option<&ImageFilter>, stroke_bounds_outset: f32, @@ -1065,7 +1345,7 @@ pub fn render_outer_stroke( render_state: Option<&mut RenderState>, canvas: Option<&Canvas>, shape: &Shape, - stroke_builders: &mut [Vec], + stroke_builders: &mut [ParagraphBuilderGroup], surface_id: Option, blur: Option<&ImageFilter>, stroke_bounds_outset: f32, @@ -1095,7 +1375,7 @@ fn draw_list_marker(canvas: &Canvas, marker: &ListMarker) { /// stands for a paragraph with no laid-out Skia paragraph; it keeps the /// paragraphs in step with `text_paragraphs`. pub fn list_markers<'a>( - skia_paragraphs: impl IntoIterator>, + skia_paragraphs: impl IntoIterator>, text_paragraphs: &[TextParagraph], x: f32, top: f32, @@ -1129,7 +1409,7 @@ pub fn list_markers<'a>( /// An empty paragraph has no style run, so it takes the paint of `previous` /// (the last list item painted in the same pass) and its own span's size. pub fn list_marker( - skia_paragraph: &skia::textlayout::Paragraph, + skia_paragraph: &LayoutParagraph, text_paragraph: &TextParagraph, x: f32, y_accum: f32, @@ -1177,38 +1457,62 @@ fn draw_decoration_segment(canvas: &Canvas, deco: &TextDecorationSegment) { canvas.draw_rect(deco.rect(), &decoration_paint); } -/// One decorated span clipped to a line: UTF-16 range, decoration and the -/// Skia style run it falls in (paint + font metrics). -type LineDecoration<'a> = (usize, usize, TextDecoration, &'a StyleMetrics<'a>); +/// One decorated span clipped to a line: UTF-16 range, decoration, the span's +/// offsets and the Skia style run it falls in (paint + font metrics). +type LineDecoration<'a> = ( + usize, + usize, + TextDecoration, + &'a HorizontalSpanRange, + &'a StyleMetrics<'a>, +); /// UTF-16 ranges of the spans that ask for a decoration we draw. -fn decorated_span_ranges(text_paragraph: &TextParagraph) -> Vec<(usize, usize, TextDecoration)> { - let mut ranges = Vec::new(); - let mut offset = 0; - for span in text_paragraph.children() { - let len = span.apply_text_transform().encode_utf16().count(); - match span.text_decoration { +/// Builder-text range of every underlined or struck span. Warichu spans +/// collapse to a placeholder and get no bar. +fn decorated_span_ranges( + text_paragraph: &TextParagraph, + offsets: &HorizontalOffsets, +) -> Vec<(usize, usize, TextDecoration)> { + text_paragraph + .children() + .iter() + .zip(&offsets.ranges) + .filter_map(|(span, range)| match span.text_decoration { Some(kind) - if kind == TextDecoration::UNDERLINE || kind == TextDecoration::LINE_THROUGH => + if !range.warichu + && (kind == TextDecoration::UNDERLINE + || kind == TextDecoration::LINE_THROUGH) => { - ranges.push((offset, offset + len, kind)) + Some((range.builder_start, range.builder_end, kind)) } - _ => {} - } - offset += len; - } - ranges + _ => None, + }) + .collect() } -/// Style run covering `offset`; runs are keyed by their start index. +/// Builder-text range of the span holding UTF-16 `offset`. +fn span_range_at(offsets: &HorizontalOffsets, offset: usize) -> Option<&HorizontalSpanRange> { + let index = offsets + .ranges + .partition_point(|range| range.builder_start <= offset) + .saturating_sub(1); + offsets.ranges.get(index) +} + +/// Style run of a line covering the UTF-8 `byte_anchor`. Runs are keyed by +/// their UTF-8 start, unlike the UTF-16 line indices. An anchor before the +/// first run belongs to a span continued from an earlier line, which the +/// line's first run carries on. fn style_metric_at<'a>( style_metrics: &[(usize, &'a StyleMetrics<'a>)], - offset: usize, + byte_anchor: usize, ) -> Option<&'a StyleMetrics<'a>> { style_metrics .iter() .rev() - .find(|(start, _)| *start <= offset) + .find(|(start, _)| *start <= byte_anchor) + .or_else(|| style_metrics.first()) .map(|(_, metrics)| *metrics) } @@ -1218,22 +1522,20 @@ fn style_metric_at<'a>( /// how Skia grouped the line into style runs; the runs only supply the paint /// and font metrics covering each segment. pub fn decoration_segments( - skia_paragraph: &skia::textlayout::Paragraph, + skia_paragraph: &LayoutParagraph, text_paragraph: &TextParagraph, + offsets: &HorizontalOffsets, x: f32, y_accum: f32, ) -> Vec { - let decorated = decorated_span_ranges(text_paragraph); + let decorated = decorated_span_ranges(text_paragraph, offsets); if decorated.is_empty() { return Vec::new(); } let mut segments = Vec::new(); for line in &skia_paragraph.get_line_metrics() { - let style_metrics: Vec<_> = line - .get_style_metrics(line.start_index..line.end_index) - .into_iter() - .collect(); + let style_metrics = line.get_style_metrics(0..usize::MAX); let line_baseline = y_accum + line.baseline as f32; let line_decorations: Vec> = decorated @@ -1244,15 +1546,16 @@ pub fn decoration_segments( if seg_start >= seg_end { return None; } - let metrics = style_metric_at(&style_metrics, seg_start)?; - Some((seg_start, seg_end, kind, metrics)) + let range = span_range_at(offsets, seg_start)?; + let metrics = style_metric_at(&style_metrics, range.style_anchor_start)?; + Some((seg_start, seg_end, kind, range, metrics)) }) .collect(); let (max_underline_thickness, underline_y, max_strike_thickness, strike_y) = calculate_decoration_metrics(&line_decorations, line_baseline); - for (seg_start, seg_end, kind, metrics) in line_decorations { + for (seg_start, seg_end, kind, range, metrics) in line_decorations { let rects = skia_paragraph.get_rects_for_range( seg_start..seg_end, skia::textlayout::RectHeightStyle::Tight, @@ -1265,11 +1568,26 @@ pub fn decoration_segments( } None => (0.0, 0.0), }; - let (y, thickness) = if kind == TextDecoration::LINE_THROUGH { + let (mut y, thickness) = if kind == TextDecoration::LINE_THROUGH { (strike_y, max_strike_thickness) } else { (underline_y, max_underline_thickness) }; + if kind == TextDecoration::UNDERLINE { + if let Some(span) = text_paragraph + .children() + .get(range.span) + .filter(|span| span.underline_below_ink()) + { + let bottom = rects + .iter() + .map(|r| r.rect.bottom()) + .fold(f32::NEG_INFINITY, f32::max); + if bottom.is_finite() { + y = Some(y_accum + bottom + span.font_size / 9.0 + thickness / 2.0); + } + } + } segments.push(TextDecorationSegment { kind, text_style: (*metrics.text_style).clone(), @@ -1292,7 +1610,7 @@ fn calculate_decoration_metrics( let mut underline_y = None; let mut max_strike_thickness: f32 = 0.0; let mut strike_y = None; - for (_seg_start, _seg_end, kind, style_metric) in line_decorations.iter() { + for (_seg_start, _seg_end, kind, _range, style_metric) in line_decorations.iter() { let font_metrics = style_metric.font_metrics; let font_size = font_metrics .cap_height @@ -1387,7 +1705,7 @@ mod tests { ]); assert_eq!( - decorated_span_ranges(¶), + decorated_span_ranges(¶, &HorizontalOffsets::new(¶)), vec![ (6, 11, TextDecoration::UNDERLINE), (18, 24, TextDecoration::LINE_THROUGH), @@ -1409,7 +1727,7 @@ mod tests { // The emoji takes two UTF-16 units and `ß` uppercases to `SS`. assert_eq!( - decorated_span_ranges(¶), + decorated_span_ranges(¶, &HorizontalOffsets::new(¶)), vec![ (2, 9, TextDecoration::UNDERLINE), (9, 10, TextDecoration::UNDERLINE), @@ -1417,6 +1735,37 @@ mod tests { ); } + #[test] + fn decorated_ranges_follow_inserted_kinsoku_characters() { + // A word joiner goes before 。, so the next span starts one unit later. + let mut para = paragraph(vec![ + span("あ。", None, None), + span("い", Some(TextDecoration::UNDERLINE), None), + ]); + para.set_language("ja".to_string()); + + assert_eq!( + decorated_span_ranges(¶, &HorizontalOffsets::new(¶)), + vec![(3, 4, TextDecoration::UNDERLINE)] + ); + } + + #[test] + fn decorated_ranges_skip_warichu_placeholders() { + let mut note = span("割注入り", None, None); + note.warichu = true; + let para = paragraph(vec![ + note, + span("い", Some(TextDecoration::UNDERLINE), None), + ]); + + // The warichu span collapses to a three-unit placeholder. + assert_eq!( + decorated_span_ranges(¶, &HorizontalOffsets::new(¶)), + vec![(3, 4, TextDecoration::UNDERLINE)] + ); + } + #[test] fn undecorated_paragraphs_have_no_ranges() { let para = paragraph(vec![ @@ -1424,7 +1773,7 @@ mod tests { span("none", Some(TextDecoration::NO_DECORATION), None), ]); - assert!(decorated_span_ranges(¶).is_empty()); + assert!(decorated_span_ranges(¶, &HorizontalOffsets::new(¶)).is_empty()); } fn sized_span(text: &str, font_size: f32, fills: Vec) -> TextSpan { diff --git a/render-wasm/src/render/vector.rs b/render-wasm/src/render/vector.rs index 9dbc5651a5..6e20a0ae8d 100644 --- a/render-wasm/src/render/vector.rs +++ b/render-wasm/src/render/vector.rs @@ -5,7 +5,8 @@ use skia_safe::{self as skia, Canvas, Matrix, Paint, RRect}; use crate::error::Result; use crate::shapes::{ circle_segments_local, merge_fills, radius_to_sigma, rect_segments_local, stroke_to_path, - BlurType, Fill, Frame, Path, Rect, Shape, SolidColor, Stroke, StrokeKind, StrokeStyle, Type, + BlurType, Fill, Frame, Path, Rect, Shape, SolidColor, Stroke, StrokeKind, StrokeStyle, + TextContent, Type, }; use crate::state::ShapesPoolRef; use crate::uuid::Uuid; @@ -66,6 +67,46 @@ impl<'a> VectorRenderer<'a> { } shape.image_filter(1.) } + + /// Vertical text: shadows, fill and strokes painted from one layout. + fn draw_vertical_text( + &mut self, + shape: &Shape, + text_content: &TextContent, + blur_filter: Option<&skia::ImageFilter>, + ) { + use crate::shapes::text_vertical; + + let bounds = text_content.bounds(); + let vertical_align = shape.vertical_align(); + let layout = text_content.vertical_layout(&bounds); + let output = crate::render::svg::vertical_glyph_output(); + + for shadow in &shape.drop_shadow_paints() { + text_vertical::paint_drop_shadow( + self.canvas, + &layout, + &bounds, + vertical_align, + shadow, + output, + ); + } + text::paint_vertical_fill(self.canvas, shape, &layout, &bounds, blur_filter, output); + let selrect = shape.selrect(); + for stroke in shape.visible_strokes().rev() { + text_vertical::paint_stroke( + self.canvas, + &layout, + &bounds, + vertical_align, + stroke, + &selrect, + blur_filter, + output, + ); + } + } } impl ShapeRenderer for VectorRenderer<'_> { @@ -200,9 +241,15 @@ impl ShapeRenderer for VectorRenderer<'_> { }; let text_content = text_content.new_bounds(shape.selrect()); + let blur_filter = self.layer_blur_filter(shape); + + if text_content.is_vertical() { + self.draw_vertical_text(shape, &text_content, blur_filter.as_ref()); + return Ok(()); + } + let mut paragraph_builders = text_content.paragraph_builder_group_from_text(Some(self.silhouette)); - let blur_filter = self.layer_blur_filter(shape); // Text drop shadows: one filter layer per shadow over fill + stroke // silhouettes (mirrors GPU `render_text_shadows`). Spread grows the diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index 5802f9f6fb..e561b44097 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -11,12 +11,16 @@ use std::iter::once; mod blend; mod blurs; mod bools; +pub mod cjk_chars; mod corners; mod fills; mod fonts; mod frames; +mod gpos_vpal; mod groups; +pub mod jlreq; mod layouts; +pub mod line_breaks; pub mod modifiers; mod paths; mod rects; @@ -27,7 +31,17 @@ mod strokes; mod svg_attrs; mod svgraw; mod text; +mod text_allocation; +mod text_attrs; +mod text_horizontal; +pub mod text_paragraph; pub mod text_paths; +pub mod text_policy; +mod text_ruby; +mod text_shaping; +pub mod text_vertical; +mod text_warichu; +mod text_zhuyin; mod transform; pub use blend::*; @@ -49,6 +63,8 @@ pub use strokes::*; pub use svg_attrs::*; pub use svgraw::*; pub use text::*; +pub use text_attrs::*; +pub(crate) use text_horizontal::*; pub use transform::*; use crate::math::{self, Bounds, Matrix, Point}; @@ -435,6 +451,7 @@ impl Shape { pub fn set_vertical_align(&mut self, align: VerticalAlign) { self.vertical_align = align; + self.invalidate_extrect(); } pub fn vertical_align(&self) -> VerticalAlign { @@ -1545,7 +1562,8 @@ impl Shape { pub fn clear_text(&mut self) { self.invalidate_extrect(); if let Type::Text(old_text_content) = &self.shape_type { - let new_text_content = TextContent::new(self.selrect, old_text_content.grow_type()); + let mut new_text_content = TextContent::new(self.selrect, old_text_content.grow_type()); + new_text_content.set_line_adjustment(old_text_content.line_adjustment()); self.shape_type = Type::Text(new_text_content); } } @@ -2193,6 +2211,16 @@ impl Shape { true } + pub fn drop_shadow_paints(&self) -> Vec { + self.drop_shadows_visible() + .map(|shadow| { + let mut paint = skia_safe::Paint::default(); + paint.set_image_filter(shadow.get_drop_shadow_filter()); + paint + }) + .collect() + } + pub fn inner_shadow_paints(&self) -> Vec { let inner_shadows: Vec<&Shadow> = self.inner_shadows_visible().collect(); diff --git a/render-wasm/src/shapes/cjk_chars.rs b/render-wasm/src/shapes/cjk_chars.rs new file mode 100644 index 0000000000..3d53551d1f --- /dev/null +++ b/render-wasm/src/shapes/cjk_chars.rs @@ -0,0 +1,64 @@ +//! Character ranges and punctuation sets of CJK text, shared by the layout +//! policies, the JLREQ classes, Zhuyin and font fallback. Where two users +//! need different ranges on purpose, each keeps its own function here with +//! a note on why. The JLREQ class tables (`jlreq`) and the vertical upright +//! table (`text_shaping::is_upright_char`) follow their own references and +//! stay with their users. + +/// Han ideographs of the BMP unified blocks: Extension A and the URO. +pub fn is_bmp_unified_han(c: char) -> bool { + matches!(u32::from(c), 0x3400..=0x4DBF | 0x4E00..=0x9FFF) +} + +/// Han ideographs for the Chinese and Korean presets: the BMP unified +/// blocks, the compatibility ideographs and Extensions B to H. +pub fn is_han(c: char) -> bool { + is_bmp_unified_han(c) || matches!(u32::from(c), 0xF900..=0xFAFF | 0x20000..=0x323AF) +} + +/// A base a Zhuyin reading may annotate. Besides `is_han`, it takes the +/// Yijing hexagram symbols (U+4DC0..U+4DFF) between Extension A and the URO, +/// which the Zhuyin check has always accepted, so saved readings keep their +/// mode. +pub fn is_zhuyin_base(c: char) -> bool { + is_han(c) || matches!(u32::from(c), 0x4DC0..=0x4DFF) +} + +/// Assigned hiragana, including the voiced marks and the iteration marks. +pub fn is_hiragana(c: char) -> bool { + matches!(u32::from(c), 0x3041..=0x309F) +} + +/// Katakana, its phonetic extensions and half-width katakana. +pub fn is_katakana(c: char) -> bool { + matches!(u32::from(c), 0x30A0..=0x30FF | 0x31F0..=0x31FF | 0xFF66..=0xFF9F) +} + +/// Kana that mark untagged text as Japanese for font choice: the whole +/// Hiragana block (with its unassigned U+3040) and `is_katakana`. +pub fn is_kana(c: char) -> bool { + u32::from(c) == 0x3040 || is_hiragana(c) || is_katakana(c) +} + +/// Hangul syllables, conjoining jamo and compatibility jamo. +pub fn is_hangul(c: char) -> bool { + matches!(u32::from(c), 0x1100..=0x11FF | 0x3130..=0x318F | 0xAC00..=0xD7AF) +} + +/// Full-width opening brackets with a removable leading half-em. +pub const FULLWIDTH_OPENING_BRACKETS: &str = "([{〈《「『【〔〖〘〚"; + +/// Full-width closing brackets with a removable trailing half-em. +pub const FULLWIDTH_CLOSING_BRACKETS: &str = ")]}〉》」』】〕〗〙〛"; + +/// Full-width commas and full stops. +pub const COMMAS_AND_FULL_STOPS: &str = ",。、."; + +/// CJK and ASCII punctuation, brackets and quotes. A comma or full stop next +/// to one of these does not hang alone. +pub fn is_punctuation(c: char) -> bool { + COMMAS_AND_FULL_STOPS.contains(c) + || FULLWIDTH_OPENING_BRACKETS.contains(c) + || FULLWIDTH_CLOSING_BRACKETS.contains(c) + || "!?:;…‥—―‘’“”.,;!?:()[]{}".contains(c) +} diff --git a/render-wasm/src/shapes/gpos_vpal.rs b/render-wasm/src/shapes/gpos_vpal.rs new file mode 100644 index 0000000000..2ab8b833b8 --- /dev/null +++ b/render-wasm/src/shapes/gpos_vpal.rs @@ -0,0 +1,285 @@ +//! Minimal `GPOS` parser for the `vpal` (proportional vertical alternate +//! metrics) feature. Vertical layout places cells by `vmtx` advances and +//! applies these deltas itself: SkShaper shapes on a horizontal line, so +//! HarfBuzz never applies vertical GPOS positioning. +//! +//! Reads only single-adjustment lookups (SinglePos, direct or behind an +//! Extension lookup): `vpal` is metrics-only, and real fonts (Noto CJK, +//! Source Han) encode it this way. + +use std::collections::HashMap; + +/// Font-unit deltas for one glyph. `y_placement` is y-up (positive lifts +/// the ink toward the flow start); `y_advance` is negative when the +/// vertical advance shrinks. +#[derive(Debug, Clone, Copy, Default, PartialEq)] +pub struct VpalDelta { + pub y_placement: i16, + pub y_advance: i16, +} + +fn read_u16(data: &[u8], offset: usize) -> Option { + Some(u16::from_be_bytes([ + *data.get(offset)?, + *data.get(offset + 1)?, + ])) +} + +fn read_i16(data: &[u8], offset: usize) -> Option { + read_u16(data, offset).map(|v| v as i16) +} + +fn read_u32(data: &[u8], offset: usize) -> Option { + Some(u32::from_be_bytes([ + *data.get(offset)?, + *data.get(offset + 1)?, + *data.get(offset + 2)?, + *data.get(offset + 3)?, + ])) +} + +/// Glyph ids covered by a Coverage table, in coverage-index order. +fn parse_coverage(data: &[u8], offset: usize) -> Option> { + match read_u16(data, offset)? { + 1 => { + let count = read_u16(data, offset + 2)? as usize; + (0..count) + .map(|i| read_u16(data, offset + 4 + i * 2)) + .collect() + } + 2 => { + let range_count = read_u16(data, offset + 2)? as usize; + let mut glyphs = Vec::new(); + for i in 0..range_count { + let o = offset + 4 + i * 6; + let start = read_u16(data, o)?; + let end = read_u16(data, o + 2)?; + if end < start { + return None; + } + glyphs.extend(start..=end); + } + Some(glyphs) + } + _ => None, + } +} + +/// A ValueRecord holds one i16 per set low bit of `value_format`, in bit +/// order (xPlacement, yPlacement, xAdvance, yAdvance, then four device +/// offsets). Returns only the y deltas. +fn parse_value_record(data: &[u8], offset: usize, value_format: u16) -> Option { + let mut delta = VpalDelta::default(); + let mut o = offset; + for bit in 0..8 { + if value_format & (1 << bit) == 0 { + continue; + } + match bit { + 1 => delta.y_placement = read_i16(data, o)?, + 3 => delta.y_advance = read_i16(data, o)?, + _ => {} + } + o += 2; + } + Some(delta) +} + +fn value_record_size(value_format: u16) -> usize { + (value_format & 0x00FF).count_ones() as usize * 2 +} + +/// Accumulate a SinglePos subtable into `deltas`. +fn parse_single_pos( + data: &[u8], + offset: usize, + deltas: &mut HashMap, +) -> Option<()> { + let format = read_u16(data, offset)?; + let coverage = parse_coverage(data, offset + read_u16(data, offset + 2)? as usize)?; + let value_format = read_u16(data, offset + 4)?; + match format { + 1 => { + let value = parse_value_record(data, offset + 6, value_format)?; + for glyph in coverage { + let entry = deltas.entry(glyph).or_default(); + entry.y_placement += value.y_placement; + entry.y_advance += value.y_advance; + } + } + 2 => { + let count = read_u16(data, offset + 6)? as usize; + let size = value_record_size(value_format); + for (i, glyph) in coverage.into_iter().take(count).enumerate() { + let value = parse_value_record(data, offset + 8 + i * size, value_format)?; + let entry = deltas.entry(glyph).or_default(); + entry.y_placement += value.y_placement; + entry.y_advance += value.y_advance; + } + } + _ => {} + } + Some(()) +} + +/// Parse the `vpal` deltas out of a raw `GPOS` table. Returns `None` when +/// the table is malformed or carries no `vpal` feature. +pub fn parse_vpal(gpos: &[u8]) -> Option> { + let feature_list = read_u16(gpos, 6)? as usize; + let lookup_list = read_u16(gpos, 8)? as usize; + + // Collect the lookup indices of every feature tagged `vpal`, + // regardless of script: the deltas are per-glyph metrics. + let feature_count = read_u16(gpos, feature_list)? as usize; + let mut lookup_indices = Vec::new(); + for i in 0..feature_count { + let record = feature_list + 2 + i * 6; + if gpos.get(record..record.checked_add(4)?)? != b"vpal" { + continue; + } + let feature = feature_list + read_u16(gpos, record + 4)? as usize; + let index_count = read_u16(gpos, feature + 2)? as usize; + for j in 0..index_count { + lookup_indices.push(read_u16(gpos, feature + 4 + j * 2)? as usize); + } + } + lookup_indices.sort_unstable(); + lookup_indices.dedup(); + if lookup_indices.is_empty() { + return None; + } + + let lookup_count = read_u16(gpos, lookup_list)? as usize; + let mut deltas = HashMap::new(); + for index in lookup_indices { + if index >= lookup_count { + continue; + } + let lookup = lookup_list + read_u16(gpos, lookup_list + 2 + index * 2)? as usize; + let lookup_type = read_u16(gpos, lookup)?; + let subtable_count = read_u16(gpos, lookup + 4)? as usize; + for s in 0..subtable_count { + let subtable = lookup + read_u16(gpos, lookup + 6 + s * 2)? as usize; + match lookup_type { + 1 => { + parse_single_pos(gpos, subtable, &mut deltas)?; + } + // ExtensionPos: { format: u16, extensionLookupType: u16, + // extensionOffset: u32 (from the subtable start) }. + 9 if read_u16(gpos, subtable + 2)? == 1 => { + let inner = subtable + read_u32(gpos, subtable + 4)? as usize; + parse_single_pos(gpos, inner, &mut deltas)?; + } + _ => {} + } + } + } + if deltas.is_empty() { + None + } else { + Some(deltas) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + // Subset of Noto Sans JP: 〱あく「」、。 with `vert` alternates and the + // real `vpal` deltas (Extension→SinglePos, both coverage and both + // SinglePos formats, value formats 0x8 and 0xA). + const VPAL_TEST_FONT: &[u8] = include_bytes!("../fonts/notosansjp-vpal-test.ttf"); + + fn gpos_table() -> Vec { + // Locate the GPOS table in the raw sfnt directory. + let data = VPAL_TEST_FONT; + let num_tables = u16::from_be_bytes([data[4], data[5]]) as usize; + for i in 0..num_tables { + let record = 12 + i * 16; + if &data[record..record + 4] == b"GPOS" { + let offset = u32::from_be_bytes([ + data[record + 8], + data[record + 9], + data[record + 10], + data[record + 11], + ]) as usize; + let length = u32::from_be_bytes([ + data[record + 12], + data[record + 13], + data[record + 14], + data[record + 15], + ]) as usize; + return data[offset..offset + length].to_vec(); + } + } + panic!("test font has no GPOS table"); + } + + #[test] + fn parses_noto_vpal_deltas() { + let deltas = parse_vpal(&gpos_table()).expect("vpal deltas"); + // Vertical alternates: 、→8 。→9 「→10 」→11 あ→12 く→13. + assert_eq!( + deltas.get(&8), + Some(&VpalDelta { + y_placement: 0, + y_advance: -500 + }), + "、 halves its vertical advance" + ); + assert_eq!( + deltas.get(&10), + Some(&VpalDelta { + y_placement: 481, + y_advance: -500 + }), + "「 lifts its trailing-half ink into the compressed cell" + ); + assert_eq!( + deltas.get(&12), + Some(&VpalDelta { + y_placement: 39, + y_advance: -58 + }), + "あ tightens slightly" + ); + // Base (horizontal) glyphs carry no vpal. + assert!(!deltas.contains_key(&6), "base あ glyph is uncovered"); + } + + #[test] + fn missing_vpal_returns_none() { + // A GPOS header with an empty feature list. + let gpos = [ + 0x00, 0x01, 0x00, 0x00, // version 1.0 + 0x00, 0x0A, // scriptList + 0x00, 0x0C, // featureList + 0x00, 0x0E, // lookupList + 0x00, 0x00, // scriptCount + 0x00, 0x00, // featureCount + 0x00, 0x00, // lookupCount + ]; + assert!(parse_vpal(&gpos).is_none()); + } + + #[test] + fn truncated_table_is_rejected() { + let table = gpos_table(); + for len in [0usize, 4, 9, 16] { + assert!(parse_vpal(&table[..len.min(table.len())]).is_none()); + } + } + + #[test] + fn truncated_feature_records_are_rejected() { + let gpos = [ + 0x00, 0x01, 0x00, 0x00, // version 1.0 + 0x00, 0x0A, // scriptList + 0x00, 0x0A, // featureList + 0x00, 0x0A, // lookupList + 0x00, 0x02, // featureCount without matching records + ]; + + assert!(parse_vpal(&gpos).is_none()); + } +} diff --git a/render-wasm/src/shapes/jlreq.rs b/render-wasm/src/shapes/jlreq.rs new file mode 100644 index 0000000000..2300f143d5 --- /dev/null +++ b/render-wasm/src/shapes/jlreq.rs @@ -0,0 +1,885 @@ +//! JLREQ (Japanese) character classes, pair-rule tables and kinsoku rules, +//! shared by the horizontal and vertical engines. +//! +//! JLREQ defines thirty layout classes. Classes 20–24 and 28–30 are +//! virtual classes for inline composites: [`classify`] handles single +//! characters, and callers assign the virtual classes when building +//! reference marks, ruby, grouped numerals, warichu, or tate-chu-yoko. + +use super::cjk_chars::{is_bmp_unified_han, is_hiragana, is_katakana}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[repr(u8)] +pub enum JlreqClass { + OpeningBracket = 0, + ClosingBracket, + Hyphen, + DividingPunctuation, + MiddleDot, + FullStop, + Comma, + Inseparable, + IterationMark, + ProlongedSoundMark, + SmallKana, + PrefixedAbbreviation, + PostfixedAbbreviation, + IdeographicSpace, + Hiragana, + Katakana, + MathSymbol, + MathOperator, + Ideographic, + ReferenceMark, + OrnamentedComplex, + SimpleRuby, + JukugoRuby, + GroupedNumeral, + UnitSymbol, + WesternWordSpace, + Western, + WarichuOpening, + WarichuClosing, + TateChuYoko, +} + +impl JlreqClass { + pub const COUNT: usize = 30; + + pub const ALL: [Self; Self::COUNT] = [ + Self::OpeningBracket, + Self::ClosingBracket, + Self::Hyphen, + Self::DividingPunctuation, + Self::MiddleDot, + Self::FullStop, + Self::Comma, + Self::Inseparable, + Self::IterationMark, + Self::ProlongedSoundMark, + Self::SmallKana, + Self::PrefixedAbbreviation, + Self::PostfixedAbbreviation, + Self::IdeographicSpace, + Self::Hiragana, + Self::Katakana, + Self::MathSymbol, + Self::MathOperator, + Self::Ideographic, + Self::ReferenceMark, + Self::OrnamentedComplex, + Self::SimpleRuby, + Self::JukugoRuby, + Self::GroupedNumeral, + Self::UnitSymbol, + Self::WesternWordSpace, + Self::Western, + Self::WarichuOpening, + Self::WarichuClosing, + Self::TateChuYoko, + ]; + + pub const fn index(self) -> usize { + self as usize + } + + pub const fn forbids_line_start(self) -> bool { + matches!( + self, + Self::ClosingBracket + | Self::Hyphen + | Self::DividingPunctuation + | Self::MiddleDot + | Self::FullStop + | Self::Comma + | Self::IterationMark + | Self::ProlongedSoundMark + | Self::SmallKana + | Self::WarichuClosing + ) + } + + pub const fn forbids_line_end(self) -> bool { + matches!(self, Self::OpeningBracket | Self::WarichuOpening) + } + + pub const fn is_japanese_letter(self) -> bool { + matches!(self, Self::Hiragana | Self::Katakana | Self::Ideographic) + } + + /// Kana, kanji and the marks that read as part of a word: small kana, + /// the prolonged sound mark and iteration marks. + pub const fn is_japanese_text(self) -> bool { + self.is_japanese_letter() + || matches!( + self, + Self::SmallKana | Self::ProlongedSoundMark | Self::IterationMark + ) + } + + pub const fn is_western_run(self) -> bool { + matches!( + self, + Self::GroupedNumeral | Self::UnitSymbol | Self::Western + ) + } + + pub const fn is_emphasis_prohibited(self) -> bool { + matches!( + self, + Self::OpeningBracket | Self::ClosingBracket | Self::FullStop | Self::Comma + ) + } + + /// Half-width punctuation followed by a half-em of aki that completes + /// its character frame. Consecutive punctuation may drop that aki; the + /// glyph body stays half-width. + pub const fn is_trailing_aki_punctuation(self) -> bool { + matches!(self, Self::ClosingBracket | Self::FullStop | Self::Comma) + } + + /// Aki (in em) a full-width font builds into the glyph before its ink. + pub const fn embedded_leading_aki_em(self) -> f32 { + match self { + Self::OpeningBracket => 0.5, + Self::MiddleDot => 0.25, + _ => 0.0, + } + } + + /// Aki (in em) a full-width font builds into the glyph after its ink. + pub const fn embedded_trailing_aki_em(self) -> f32 { + match self { + Self::ClosingBracket | Self::FullStop | Self::Comma => 0.5, + Self::MiddleDot => 0.25, + _ => 0.0, + } + } +} + +/// Whether the half-em aki between `before` and `after` goes (JLREQ §3.1.4): +/// closing sequences set solid, closing→opening keeps one half-em, opening +/// sequences set solid after the first bracket. Returns (shed the trailing +/// aki of `before`, shed the leading aki of `after`); a closing mark owns the +/// reduction, so both halves never go. +pub fn shed_pair_aki(before: JlreqClass, after: JlreqClass) -> (bool, bool) { + let preferred = pair_rule(before, after).preferred_em + f32::EPSILON; + if before.is_trailing_aki_punctuation() { + (0.5 + after.embedded_leading_aki_em() > preferred, false) + } else if after == JlreqClass::OpeningBracket { + (false, before.embedded_trailing_aki_em() + 0.5 > preferred) + } else { + (false, false) + } +} + +/// Which built-in half-em aki of a character goes. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum AkiShed { + /// The aki after a closing mark. + Trailing, + /// The aki before an opening bracket. + Leading, +} + +/// Class a character takes for punctuation aki, or `None` when its built-in +/// aki is unknown: punctuation set proportionally (`proportional`, from +/// `palt` or `vpal` in the flow direction) and curly quotes, which are +/// full-width in some fonts and proportional in others. A ruby base counts +/// as simple ruby (cl-22). +pub fn aki_class(ch: char, ruby_base: bool, proportional: bool) -> Option { + if ruby_base { + return Some(JlreqClass::SimpleRuby); + } + let curly_quote = matches!(ch, '‘' | '’' | '“' | '”'); + (!proportional && !curly_quote).then(|| classify(ch)) +} + +/// The half-em aki that go in a run of characters classed by `aki_class` +/// (JLREQ §3.1.4, see `shed_pair_aki`), as (character index, which aki). +/// A character of unknown aki (`None`) sheds nothing and makes its +/// neighbours keep theirs. +pub fn punctuation_aki_sheds(classes: &[Option]) -> Vec<(usize, AkiShed)> { + let mut sheds = Vec::new(); + for (index, pair) in classes.windows(2).enumerate() { + let [Some(before), Some(after)] = *pair else { + continue; + }; + match shed_pair_aki(before, after) { + (true, _) => sheds.push((index, AkiShed::Trailing)), + (_, true) => sheds.push((index + 1, AkiShed::Leading)), + _ => {} + } + } + sheds +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct PairRule { + /// Preferred extra spacing between the two character frames, in em. + pub preferred_em: f32, + /// Smallest spacing allowed during oikomi, in em. + pub minimum_em: f32, + /// Largest spacing allowed during oidashi/justification, in em. + pub maximum_em: f32, + pub break_allowed: bool, + /// Whether horizontal SkParagraph needs a WORD JOINER to block a break + /// in this pair. Its Unicode breaker already keeps Western/numeral runs + /// whole. + pub suppress_break_with_joiner: bool, + /// Lower values are adjusted first; zero means not adjustable. + pub shrink_priority: u8, + pub expand_priority: u8, +} + +impl PairRule { + const SOLID: Self = Self { + preferred_em: 0.0, + minimum_em: 0.0, + maximum_em: 0.0, + break_allowed: true, + suppress_break_with_joiner: false, + shrink_priority: 0, + expand_priority: 0, + }; +} + +const fn generated_pair_rules() -> [[PairRule; JlreqClass::COUNT]; JlreqClass::COUNT] { + let mut table = [[PairRule::SOLID; JlreqClass::COUNT]; JlreqClass::COUNT]; + let mut before_index = 0; + while before_index < JlreqClass::COUNT { + let before = JlreqClass::ALL[before_index]; + let mut after_index = 0; + while after_index < JlreqClass::COUNT { + let after = JlreqClass::ALL[after_index]; + let mut rule = PairRule::SOLID; + + rule.suppress_break_with_joiner = + before.forbids_line_end() || after.forbids_line_start(); + rule.break_allowed = !rule.suppress_break_with_joiner; + if before_index == after_index + && matches!(before, JlreqClass::GroupedNumeral | JlreqClass::Western) + { + rule.break_allowed = false; + } + + if matches!(before, JlreqClass::WesternWordSpace) { + rule.preferred_em = 1.0 / 3.0; + rule.minimum_em = 0.25; + rule.maximum_em = 0.5; + rule.shrink_priority = 1; + rule.expand_priority = 1; + } else if (before.is_japanese_text() && after.is_western_run()) + || (before.is_western_run() && after.is_japanese_text()) + { + rule.preferred_em = 0.25; + rule.minimum_em = 0.125; + rule.maximum_em = 0.5; + rule.shrink_priority = 5; + rule.expand_priority = 2; + } else if matches!( + (before, after), + ( + JlreqClass::ClosingBracket | JlreqClass::Comma | JlreqClass::FullStop, + JlreqClass::TateChuYoko + ) | (JlreqClass::TateChuYoko, JlreqClass::OpeningBracket) + ) { + rule.preferred_em = 0.5; + rule.minimum_em = 0.0; + rule.maximum_em = 0.5; + rule.shrink_priority = 3; + } else if matches!( + (before, after), + (JlreqClass::TateChuYoko, JlreqClass::TateChuYoko) + ) { + // Adjacent cl-30 entries are always separate TCY composites + // (characters inside one composite are atomic). + rule.maximum_em = 0.25; + rule.expand_priority = 3; + } else if matches!(before, JlreqClass::DividingPunctuation) + && !matches!( + after, + JlreqClass::ClosingBracket + | JlreqClass::DividingPunctuation + | JlreqClass::IdeographicSpace + | JlreqClass::WesternWordSpace + | JlreqClass::FullStop + | JlreqClass::Comma + | JlreqClass::MiddleDot + ) + { + // A sentence-ending question/exclamation mark carries one em + // after it, unless punctuation or a typed space follows. Line + // planning may drop it at the line edge. + rule.preferred_em = 1.0; + rule.minimum_em = 0.0; + rule.maximum_em = 1.0; + rule.shrink_priority = 2; + } else if before.is_trailing_aki_punctuation() + && matches!(after, JlreqClass::OpeningBracket) + { + // Keep one half-em between the two half-width glyph bodies, + // not the sum of both characters' aki. + rule.preferred_em = 0.5; + rule.minimum_em = 0.0; + rule.maximum_em = 0.5; + rule.shrink_priority = 4; + } else if before.is_trailing_aki_punctuation() && after.is_trailing_aki_punctuation() { + // Consecutive closing punctuation sets solid internally; the + // last character in the sequence supplies the trailing aki. + } else if matches!(before, JlreqClass::OpeningBracket) + && matches!(after, JlreqClass::OpeningBracket) + { + // Consecutive opening brackets set solid internally; the first + // character in the sequence supplies the leading aki. + } else if (before.is_trailing_aki_punctuation() + && matches!(after, JlreqClass::MiddleDot)) + || (matches!(before, JlreqClass::MiddleDot) + && matches!(after, JlreqClass::OpeningBracket)) + { + rule.preferred_em = 0.25; + rule.minimum_em = 0.0; + rule.maximum_em = 0.25; + rule.shrink_priority = 3; + } else if before.is_trailing_aki_punctuation() { + rule.preferred_em = 0.5; + rule.minimum_em = if matches!(before, JlreqClass::FullStop) { + 0.5 + } else { + 0.0 + }; + rule.maximum_em = 0.5; + rule.shrink_priority = if matches!(before, JlreqClass::FullStop) { + 0 + } else { + 4 + }; + } else if matches!(after, JlreqClass::OpeningBracket) { + rule.preferred_em = 0.5; + rule.minimum_em = 0.0; + rule.maximum_em = 0.5; + rule.shrink_priority = 4; + } else if matches!(before, JlreqClass::MiddleDot) + || matches!(after, JlreqClass::MiddleDot) + { + rule.preferred_em = 0.25; + rule.minimum_em = 0.0; + rule.maximum_em = 0.25; + rule.shrink_priority = 3; + } else if before.is_japanese_letter() && after.is_japanese_letter() { + // Solid Japanese text is the general third-stage expansion + // point. The planner passes this quarter-em cap only in + // JLREQ's final equal-expansion fallback. + rule.maximum_em = 0.25; + rule.expand_priority = 3; + } + + table[before_index][after_index] = rule; + after_index += 1; + } + before_index += 1; + } + table +} + +pub const PAIR_RULES: [[PairRule; JlreqClass::COUNT]; JlreqClass::COUNT] = generated_pair_rules(); + +pub const fn pair_rule(before: JlreqClass, after: JlreqClass) -> PairRule { + PAIR_RULES[before.index()][after.index()] +} + +// JLREQ Appendix A class tables. They are wider than the `cjk_chars` +// punctuation sets the pairwise policies use. +const OPENING_BRACKETS: &str = "(〔[{〈《「『【〖〘〚‘“〝«⦅⦅「"; +const CLOSING_BRACKETS: &str = ")〕]}〉》」』】〗〙〛’”〟〞»⦆⦆」"; +const HYPHENS: &str = "‐゠–〜~"; +const DIVIDING_PUNCTUATION: &str = "!?‼⁇⁈⁉"; +const MIDDLE_DOTS: &str = "・・:;"; +const FULL_STOPS: &str = "。.。"; +const COMMAS: &str = "、,、"; +const INSEPARABLE: &str = "—―…‥〳〴〵"; +const ITERATION_MARKS: &str = "々〻ゝゞヽヾ"; +const SMALL_KANA: &str = concat!( + "ぁぃぅぇぉっゃゅょゎゕゖ", + "ァィゥェォッャュョヮヵヶㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ", + "ァィゥェォャュョッ" +); +const PREFIXED_ABBREVIATIONS: &str = "¥¥$$££##€"; +const POSTFIXED_ABBREVIATIONS: &str = concat!("°′″℃¢¢%%‰‱ℓ", "㌃㌍㌔㌘㌢㌣㌦㌧㌫㌶㌻㍉㍊㍍㍑㍗"); +const MATH_SYMBOLS: &str = "==≠≒≃≅≈≡≢<<>>≦≧≤≥≪≫≶≷⋚⋛∈∋⊆⊇⊂⊃∪∩⊄⊅⊊⊋∉⌅⌆∧∨⇒⇔↔∥∦∽∝⊥⊕⊗"; +const MATH_OPERATORS: &str = "++-−-÷×±∓∗∙√∫∬∭∑∏"; + +pub fn classify(c: char) -> JlreqClass { + // Common letters skip the table scans. + match c { + c if is_bmp_unified_han(c) => JlreqClass::Ideographic, + 'a'..='z' | 'A'..='Z' => JlreqClass::Western, + _ => classify_by_tables(c), + } +} + +fn classify_by_tables(c: char) -> JlreqClass { + if OPENING_BRACKETS.contains(c) { + JlreqClass::OpeningBracket + } else if CLOSING_BRACKETS.contains(c) { + JlreqClass::ClosingBracket + } else if HYPHENS.contains(c) { + JlreqClass::Hyphen + } else if DIVIDING_PUNCTUATION.contains(c) { + JlreqClass::DividingPunctuation + } else if MIDDLE_DOTS.contains(c) { + JlreqClass::MiddleDot + } else if FULL_STOPS.contains(c) { + JlreqClass::FullStop + } else if COMMAS.contains(c) { + JlreqClass::Comma + } else if INSEPARABLE.contains(c) { + JlreqClass::Inseparable + } else if ITERATION_MARKS.contains(c) { + JlreqClass::IterationMark + } else if c == 'ー' || c == 'ー' { + JlreqClass::ProlongedSoundMark + } else if SMALL_KANA.contains(c) { + JlreqClass::SmallKana + } else if PREFIXED_ABBREVIATIONS.contains(c) { + JlreqClass::PrefixedAbbreviation + } else if POSTFIXED_ABBREVIATIONS.contains(c) { + JlreqClass::PostfixedAbbreviation + } else if c == '\u{3000}' { + JlreqClass::IdeographicSpace + } else if MATH_SYMBOLS.contains(c) { + JlreqClass::MathSymbol + } else if MATH_OPERATORS.contains(c) { + JlreqClass::MathOperator + } else if c == ' ' || c == '\t' || c == '\u{00a0}' { + JlreqClass::WesternWordSpace + } else if c.is_ascii_digit() { + JlreqClass::GroupedNumeral + } else if is_hiragana(c) { + JlreqClass::Hiragana + } else if is_katakana(c) { + JlreqClass::Katakana + } else if is_ideographic(c) { + JlreqClass::Ideographic + } else if is_squared_unit(c) { + JlreqClass::PostfixedAbbreviation + } else if is_ideographic_symbol(c) { + JlreqClass::Ideographic + } else { + JlreqClass::Western + } +} + +/// Digits that group with an adjacent prefixed or postfixed abbreviation. +fn is_numeral(c: char) -> bool { + c.is_ascii_digit() || matches!(u32::from(c), 0xFF10..=0xFF19) +} + +/// Character pairs kept on one line beyond the class table: two identical +/// inseparable marks (`——`, `……`), an abbreviation with its numeral +/// (`¥100`, `60㎏`, `100%`), per JLREQ §3.1.10, and digits of one +/// number, full-width ones included (`2026`), which JLREQ would let break +/// as ideographs. ASCII digit pairs are already grouped numerals. +pub fn keeps_together(before: char, after: char) -> bool { + keeps_together_classified((before, classify(before)), (after, classify(after))) +} + +/// `keeps_together` for characters already paired with their classes. +pub fn keeps_together_classified( + (before, before_class): (char, JlreqClass), + (after, after_class): (char, JlreqClass), +) -> bool { + (before == after && before_class == JlreqClass::Inseparable) + || (is_numeral(before) + && is_numeral(after) + && !(before.is_ascii_digit() && after.is_ascii_digit())) + || (is_numeral(before) && after_class == JlreqClass::PostfixedAbbreviation) + || (before_class == JlreqClass::PrefixedAbbreviation && is_numeral(after)) +} + +/// Kinsoku shori: a character that may not start a line. +pub fn forbidden_at_line_start(c: char) -> bool { + classify(c).forbids_line_start() +} + +/// Kinsoku shori: a character that may not end a line. +pub fn forbidden_at_line_end(c: char) -> bool { + classify(c).forbids_line_end() +} + +/// Most kinsoku pairs oidashi moves a break back over. Past that, pushing +/// out does not resolve the break, and the line may squeeze instead. +pub const MAX_KINSOKU_SHIFT: usize = 4; + +/// True when kinsoku forbids a break between `before` and `after`. +pub fn kinsoku_blocks(before: char, after: char) -> bool { + forbidden_at_line_end(before) || forbidden_at_line_start(after) +} + +/// Whether a line may break between two adjacent characters: the pair table +/// (kinsoku, Western and numeral runs) plus `keeps_together`. +pub fn break_allowed_between(before: char, after: char) -> bool { + pair_rule(classify(before), classify(after)).break_allowed && !keeps_together(before, after) +} + +/// True for CJK text characters: kana, Han ideographs, and the CJK symbols +/// and punctuation, katakana extension and half/full-width form blocks. +/// Hangul is not included. +pub fn is_cjk_text_char(c: char) -> bool { + classify(c).is_japanese_letter() + || matches!(u32::from(c), 0x3000..=0x30FF | 0x31F0..=0x31FF | 0xFF00..=0xFFEF) +} + +/// JLREQ cl-19 ideographs. Wider than `cjk_chars::is_han` (radicals, strokes, +/// the compatibility supplement and a few marks), as JLREQ Appendix A lists. +fn is_ideographic(c: char) -> bool { + matches!(u32::from(c), + 0x2E80..=0x2FDF + | 0x31C0..=0x31EF + | 0x3400..=0x4DBF + | 0x4E00..=0x9FFF + | 0xF900..=0xFAFF + | 0x20000..=0x2FA1F + | 0x30000..=0x323AF + ) || matches!(c, '〃' | '仝' | '〆' | '♂' | '♀') +} + +/// Squared Latin unit abbreviations (㎏, ㎡, ㏄, …), cl-13 like %. ㏍ is a +/// company mark, cl-19. +fn is_squared_unit(c: char) -> bool { + matches!(u32::from(c), 0x3380..=0x33DF) && c != '㏍' +} + +/// Symbols JLREQ sets as full-width ideographic characters (cl-19): the +/// full-width forms, CJK symbols, circled and parenthesized characters, +/// Roman numerals, arrows, shapes and squared words. Latin-1 symbols such +/// as © or † stay Western, as they also appear in Latin text. +fn is_ideographic_symbol(c: char) -> bool { + matches!(u32::from(c), + 0xFF01..=0xFF5E + | 0x2160..=0x217F + | 0x2190..=0x21FF + | 0x2460..=0x24FF + | 0x25A0..=0x25FF + | 0x2600..=0x26FF + | 0x2776..=0x277F + | 0x3000..=0x303F + | 0x3200..=0x33FF + ) || matches!( + c, + '※' | '⁂' | '⁑' | '℡' | '✓' | '❖' | '⤴' | '⤵' | '⦿' | 'ゟ' | 'ヿ' + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn classes_forbidden_at_start() { + for c in "、。」』)ーっゃァッ々・!?".chars() { + assert!(forbidden_at_line_start(c), "expected start-forbidden: {c}"); + } + for c in "あ漢A1「( ".chars() { + assert!(!forbidden_at_line_start(c), "not start-forbidden: {c}"); + } + } + + #[test] + fn classes_forbidden_at_end() { + for c in "「『([【〈《".chars() { + assert!(forbidden_at_line_end(c), "expected end-forbidden: {c}"); + } + for c in "あ漢A1」)。".chars() { + assert!(!forbidden_at_line_end(c), "not end-forbidden: {c}"); + } + } + + #[test] + fn aki_class_leaves_out_curly_quotes_and_proportional_punctuation() { + assert_eq!(aki_class('“', false, false), None); + assert_eq!(aki_class('」', false, true), None); + assert_eq!( + aki_class('」', false, false), + Some(JlreqClass::ClosingBracket) + ); + assert_eq!(aki_class('」', true, false), Some(JlreqClass::SimpleRuby)); + } + + #[test] + fn punctuation_aki_sheds_the_trailing_aki_of_a_closing_mark() { + use JlreqClass::*; + let classes = [Some(ClosingBracket), Some(OpeningBracket)]; + assert_eq!( + punctuation_aki_sheds(&classes), + vec![(0, AkiShed::Trailing)] + ); + } + + #[test] + fn punctuation_aki_sheds_the_leading_aki_of_a_second_opening_bracket() { + use JlreqClass::*; + let classes = [Some(OpeningBracket), Some(OpeningBracket)]; + assert_eq!(punctuation_aki_sheds(&classes), vec![(1, AkiShed::Leading)]); + } + + #[test] + fn punctuation_aki_sheds_nothing_next_to_an_unknown_aki() { + use JlreqClass::*; + let classes = [Some(ClosingBracket), None, Some(OpeningBracket)]; + assert!(punctuation_aki_sheds(&classes).is_empty()); + } + + #[test] + fn class_model_contains_all_thirty_jlreq_classes_in_order() { + assert_eq!(JlreqClass::ALL.len(), 30); + for (index, class) in JlreqClass::ALL.iter().enumerate() { + assert_eq!(class.index(), index); + } + } + + #[test] + fn classifies_representative_jlreq_characters() { + let cases = [ + ('「', JlreqClass::OpeningBracket), + ('」', JlreqClass::ClosingBracket), + ('〜', JlreqClass::Hyphen), + ('!', JlreqClass::DividingPunctuation), + ('・', JlreqClass::MiddleDot), + ('。', JlreqClass::FullStop), + ('、', JlreqClass::Comma), + ('…', JlreqClass::Inseparable), + ('々', JlreqClass::IterationMark), + ('ー', JlreqClass::ProlongedSoundMark), + ('ょ', JlreqClass::SmallKana), + ('¥', JlreqClass::PrefixedAbbreviation), + ('%', JlreqClass::PostfixedAbbreviation), + ('\u{3000}', JlreqClass::IdeographicSpace), + ('あ', JlreqClass::Hiragana), + ('ア', JlreqClass::Katakana), + ('≠', JlreqClass::MathSymbol), + ('+', JlreqClass::MathOperator), + ('漢', JlreqClass::Ideographic), + ('2', JlreqClass::GroupedNumeral), + ('㎏', JlreqClass::PostfixedAbbreviation), + (' ', JlreqClass::WesternWordSpace), + ('A', JlreqClass::Western), + ]; + for (character, expected) in cases { + assert_eq!(classify(character), expected, "wrong class for {character}"); + } + } + + #[test] + fn generated_rules_cover_every_class_pair() { + assert_eq!(PAIR_RULES.len(), JlreqClass::COUNT); + assert!(PAIR_RULES.iter().all(|row| row.len() == JlreqClass::COUNT)); + } + + #[test] + fn generated_rules_encode_kinsoku_and_atomic_runs() { + assert!(!pair_rule(JlreqClass::OpeningBracket, JlreqClass::Ideographic).break_allowed); + assert!(!pair_rule(JlreqClass::Ideographic, JlreqClass::ClosingBracket).break_allowed); + assert!(!pair_rule(JlreqClass::GroupedNumeral, JlreqClass::GroupedNumeral).break_allowed); + assert!(pair_rule(JlreqClass::Ideographic, JlreqClass::Ideographic).break_allowed); + } + + #[test] + fn generated_rules_encode_script_and_tcy_spacing() { + let script = pair_rule(JlreqClass::Ideographic, JlreqClass::Western); + assert_eq!(script.preferred_em, 0.25); + assert_eq!(script.minimum_em, 0.125); + assert_eq!(script.maximum_em, 0.5); + + let tcy = pair_rule(JlreqClass::Comma, JlreqClass::TateChuYoko); + assert_eq!(tcy.preferred_em, 0.5); + assert_eq!( + pair_rule(JlreqClass::TateChuYoko, JlreqClass::Comma).preferred_em, + 0.0 + ); + assert_eq!( + pair_rule(JlreqClass::OpeningBracket, JlreqClass::TateChuYoko).preferred_em, + 0.0 + ); + assert_eq!( + pair_rule(JlreqClass::TateChuYoko, JlreqClass::OpeningBracket).preferred_em, + 0.5 + ); + assert_eq!( + pair_rule(JlreqClass::Ideographic, JlreqClass::TateChuYoko).preferred_em, + 0.0 + ); + assert_eq!( + pair_rule(JlreqClass::TateChuYoko, JlreqClass::Ideographic).preferred_em, + 0.0 + ); + let adjacent_tcy = pair_rule(JlreqClass::TateChuYoko, JlreqClass::TateChuYoko); + assert_eq!(adjacent_tcy.preferred_em, 0.0); + assert_eq!(adjacent_tcy.maximum_em, 0.25); + assert_eq!(adjacent_tcy.expand_priority, 3); + } + + #[test] + fn generated_rules_encode_punctuation_sequences() { + assert_eq!( + pair_rule(JlreqClass::Ideographic, JlreqClass::OpeningBracket).preferred_em, + 0.5 + ); + assert_eq!( + pair_rule(JlreqClass::ClosingBracket, JlreqClass::Ideographic).preferred_em, + 0.5 + ); + assert_eq!( + pair_rule(JlreqClass::FullStop, JlreqClass::ClosingBracket).preferred_em, + 0.0 + ); + assert_eq!( + pair_rule(JlreqClass::ClosingBracket, JlreqClass::OpeningBracket).preferred_em, + 0.5 + ); + assert_eq!( + pair_rule(JlreqClass::ClosingBracket, JlreqClass::MiddleDot).preferred_em, + 0.25 + ); + assert_eq!( + pair_rule(JlreqClass::MiddleDot, JlreqClass::OpeningBracket).preferred_em, + 0.25 + ); + assert_eq!( + pair_rule(JlreqClass::DividingPunctuation, JlreqClass::Ideographic).preferred_em, + 1.0 + ); + assert_eq!( + pair_rule( + JlreqClass::DividingPunctuation, + JlreqClass::DividingPunctuation + ) + .preferred_em, + 0.0 + ); + assert_eq!( + pair_rule( + JlreqClass::DividingPunctuation, + JlreqClass::IdeographicSpace + ) + .preferred_em, + 0.0 + ); + let solid_japanese = pair_rule(JlreqClass::Ideographic, JlreqClass::Hiragana); + assert_eq!(solid_japanese.preferred_em, 0.0); + assert_eq!(solid_japanese.maximum_em, 0.25); + assert_eq!(solid_japanese.expand_priority, 3); + let ruby_adjacency = pair_rule(JlreqClass::SimpleRuby, JlreqClass::JukugoRuby); + assert_eq!(ruby_adjacency.preferred_em, 0.0); + assert_eq!(ruby_adjacency.maximum_em, 0.0); + assert_eq!(ruby_adjacency.expand_priority, 0); + } + + #[test] + fn classify_follows_jlreq_appendix_a() { + let cases = [ + ('—', JlreqClass::Inseparable), + ('〝', JlreqClass::OpeningBracket), + ('〟', JlreqClass::ClosingBracket), + ('〞', JlreqClass::ClosingBracket), + ('«', JlreqClass::OpeningBracket), + ('»', JlreqClass::ClosingBracket), + ('⦅', JlreqClass::OpeningBracket), + ('⦆', JlreqClass::ClosingBracket), + ('⦅', JlreqClass::OpeningBracket), + ('⦆', JlreqClass::ClosingBracket), + ('A', JlreqClass::Ideographic), + ('z', JlreqClass::Ideographic), + ('1', JlreqClass::Ideographic), + ('〇', JlreqClass::Ideographic), + ('〒', JlreqClass::Ideographic), + ('※', JlreqClass::Ideographic), + ('①', JlreqClass::Ideographic), + ('㈱', JlreqClass::Ideographic), + ('○', JlreqClass::Ideographic), + ('★', JlreqClass::Ideographic), + ('→', JlreqClass::Ideographic), + ('Ⅳ', JlreqClass::Ideographic), + ('㏍', JlreqClass::Ideographic), + ('≒', JlreqClass::MathSymbol), + ('≡', JlreqClass::MathSymbol), + ('≪', JlreqClass::MathSymbol), + ('≈', JlreqClass::MathSymbol), + ('∝', JlreqClass::MathSymbol), + ('↔', JlreqClass::MathSymbol), + ('€', JlreqClass::PrefixedAbbreviation), + ('㎡', JlreqClass::PostfixedAbbreviation), + ('㏄', JlreqClass::PostfixedAbbreviation), + ('㌔', JlreqClass::PostfixedAbbreviation), + ('ℓ', JlreqClass::PostfixedAbbreviation), + ('。', JlreqClass::FullStop), + ('、', JlreqClass::Comma), + ('「', JlreqClass::OpeningBracket), + ('」', JlreqClass::ClosingBracket), + ('ァ', JlreqClass::SmallKana), + ('ッ', JlreqClass::SmallKana), + ('ー', JlreqClass::ProlongedSoundMark), + ('\u{30000}', JlreqClass::Ideographic), + ('\u{31350}', JlreqClass::Ideographic), + ]; + for (character, expected) in cases { + assert_eq!(classify(character), expected, "wrong class for {character}"); + } + } + + #[test] + fn fast_classes_match_the_tables() { + let fast = ('\u{4E00}'..='\u{9FFF}') + .chain('\u{3400}'..='\u{4DBF}') + .chain('a'..='z') + .chain('A'..='Z'); + for c in fast { + assert_eq!(classify(c), classify_by_tables(c), "{c:?}"); + } + } + + #[test] + fn ascii_and_latin_symbols_stay_western() { + for character in ['(', ')', '[', ']', 'A', '©', '†', 'α'] { + assert_eq!(classify(character), JlreqClass::Western, "{character}"); + } + } + + #[test] + fn only_identical_inseparable_pairs_are_unbreakable() { + assert!(!JlreqClass::Inseparable.forbids_line_start()); + assert!(break_allowed_between('あ', '…')); + assert!(!break_allowed_between('…', '…')); + assert!(!break_allowed_between('—', '—')); + assert!(break_allowed_between('—', '…')); + } + + #[test] + fn abbreviations_bind_only_to_numerals() { + assert!(!JlreqClass::PostfixedAbbreviation.forbids_line_start()); + assert!(!JlreqClass::PrefixedAbbreviation.forbids_line_end()); + assert!(!break_allowed_between('0', '㎏')); + assert!(!break_allowed_between('0', '%')); + assert!(!break_allowed_between('$', '5')); + assert!(!break_allowed_between('¥', '1')); + assert!(break_allowed_between('#', 'は')); + assert!(break_allowed_between('漢', '%')); + } + + #[test] + fn breaks_follow_kinsoku_and_western_runs() { + assert!(!break_allowed_between('あ', 'っ')); + assert!(!break_allowed_between('「', 'あ')); + assert!(!break_allowed_between('P', 'e')); + assert!(!break_allowed_between('2', '0')); + assert!(break_allowed_between('あ', 'い')); + } + + #[test] + fn digit_runs_stay_together() { + assert!(!break_allowed_between('2', '0')); + assert!(!break_allowed_between('2', '0')); + assert!(break_allowed_between('6', '年')); + assert!(break_allowed_between('年', '2')); + } +} diff --git a/render-wasm/src/shapes/line_breaks.rs b/render-wasm/src/shapes/line_breaks.rs new file mode 100644 index 0000000000..ce41703b05 --- /dev/null +++ b/render-wasm/src/shapes/line_breaks.rs @@ -0,0 +1,807 @@ +//! Layout-text rewrite for horizontal CJK line breaking. +//! +//! skia-safe exposes no ICU BreakIterator, so the layout policy steers +//! skparagraph's breaker by editing the text handed to it. U+2060 WORD +//! JOINER suppresses a break the policy forbids (Japanese kinsoku shori, +//! Chinese and Korean punctuation, nonbreaking units and annotations). +//! U+200B ZERO WIDTH SPACE reopens a break that UAX #14 forbids but the +//! policy allows, such as before … and ‥ in JLREQ, or an emergency break +//! inside an overlong unit. U+2028 LINE SEPARATOR keeps the original wrap +//! budget around an indivisible unit that overflows its box. In Japanese +//! text the same transform inserts the JLREQ quarter-em space at +//! Japanese/Western boundaries and sets Western word spaces to one third em. +//! +//! Inserted characters shift every UTF-16 offset the laid-out paragraph +//! reports (position-data, carets, selection rects). [`OffsetMap`] +//! translates between original and shifted offsets. It depends only on the +//! span texts, so any consumer can recompute it and match the builders. + +/// Zero-width character; its UAX #14 class forbids a break on either side. +pub const WORD_JOINER: char = '\u{2060}'; +/// ZERO WIDTH SPACE: opens a break UAX #14 forbids but JLREQ allows, before +/// an inseparable mark such as … (JLREQ only keeps two of them together). +pub const BREAK_OPPORTUNITY: char = '\u{200B}'; +/// Layout-only line separator used to retain the original wrap budget when +/// an indivisible CJK unit needs more width than its container. +pub const FORCED_LINE_BREAK: char = '\u{2028}'; + +/// True for the zero-width characters this pass inserts to steer breaks; +/// they take no letter-spacing and count as no character. +pub fn is_inserted_zero_width(ch: char) -> bool { + ch == WORD_JOINER || ch == BREAK_OPPORTUNITY || ch == FORCED_LINE_BREAK +} +/// Unicode FOUR-PER-EM SPACE, used for the preferred Japanese/Western gap. +pub const JAPANESE_WESTERN_SPACE: char = '\u{2005}'; +/// Unicode THREE-PER-EM SPACE, used for Western word spaces. +pub const WESTERN_WORD_SPACE: char = '\u{2004}'; +/// Unicode IDEOGRAPHIC SPACE, the one-em space after a sentence-ending +/// question or exclamation mark. +pub const IDEOGRAPHIC_SPACE: char = '\u{3000}'; + +use super::jlreq::{classify, pair_rule, JlreqClass}; +use super::text_policy::TextLayoutPolicy; +use unicode_segmentation::UnicodeSegmentation; + +/// One grapheme of a paragraph's text with the policy's rules for the +/// boundary before it. The first grapheme allows both kinds of break. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct PolicyBoundary { + /// UTF-16 offset of the grapheme in the text. + pub utf16: usize, + /// First character of the grapheme, which the policy rules read. + pub first: char, + /// `TextLayoutPolicy::break_allowed` before this grapheme. + pub allowed: bool, + /// `TextLayoutPolicy::emergency_break_allowed` before this grapheme. + pub emergency: bool, +} + +/// Break boundaries of a whole paragraph text (all spans, transformed), so +/// font and style runs cannot split a grapheme or create a boundary. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct PolicyBoundaries { + pub graphemes: Vec, + /// UTF-16 length of the text. + pub length: usize, +} + +impl PolicyBoundaries { + pub fn new(text: &str, policy: TextLayoutPolicy) -> Self { + let mut length = 0; + let mut previous: Option = None; + let graphemes = text + .graphemes(true) + .map(|grapheme| { + let first = grapheme.chars().next().unwrap(); + let boundary = PolicyBoundary { + utf16: length, + first, + allowed: previous.is_none_or(|before| policy.break_allowed(before, first)), + emergency: previous + .is_none_or(|before| policy.emergency_break_allowed(before, first)), + }; + previous = Some(first); + length += grapheme.encode_utf16().count(); + boundary + }) + .collect(); + Self { graphemes, length } + } + + /// (allowed, emergency) at a UTF-16 offset: a grapheme start, or the + /// text end, which allows both. `None` inside a grapheme. + pub fn at(&self, utf16: usize) -> Option<(bool, bool)> { + if utf16 == self.length { + return Some((true, true)); + } + self.graphemes + .binary_search_by_key(&utf16, |g| g.utf16) + .ok() + .map(|index| { + ( + self.graphemes[index].allowed, + self.graphemes[index].emergency, + ) + }) + } +} + +/// Translates between original UTF-16 offsets (the source-of-truth span text) +/// and layout-text UTF-16 offsets (the text handed to skparagraph). +#[derive(Debug, Clone, Default, PartialEq)] +pub struct OffsetMap { + /// Ascending shifted UTF-16 indices of inserted (not substituted) chars. + inserted: Vec, +} + +impl OffsetMap { + #[cfg(test)] + pub fn is_empty(&self) -> bool { + self.inserted.is_empty() + } + + /// True when the character at a shifted offset was inserted by the + /// transform. + pub fn is_inserted(&self, shifted: usize) -> bool { + self.inserted.binary_search(&shifted).is_ok() + } + + /// Original offset for a shifted offset. An offset on an inserted + /// character resolves to the boundary where it was inserted. + pub fn to_original(&self, shifted: usize) -> usize { + shifted - self.inserted.partition_point(|&p| p < shifted) + } + + /// Shifted offset for an original offset. A boundary that received an + /// inserted character resolves after it, so carets skip synthetic spacing. + pub fn to_shifted(&self, original: usize) -> usize { + // The i-th insertion lands before `original` when it sits at or + // before `original + i`; `inserted[i] - i` never decreases, so the + // count is a partition point. + let (mut low, mut high) = (0, self.inserted.len()); + while low < high { + let middle = (low + high) / 2; + if self.inserted[middle] - middle <= original { + low = middle + 1; + } else { + high = middle; + } + } + original + low + } +} + +/// Applies the horizontal Japanese layout-text transform (the `Japanese` +/// policy of `apply_policy_with_breaks`): inserts WORD +/// JOINER wherever a break would violate kinsoku, inserts a quarter-em space +/// between Japanese letters and Western letters or digits, and sets +/// breakable ASCII spaces to one third em. Span boundaries are transparent; +/// grapheme clusters are not, so nothing lands inside one. `ruby_breaks[span] +/// == Some(boundaries)` forbids breaks at every internal scalar boundary of +/// that span except those UTF-16 offsets. Returns `None` when nothing changes. +#[cfg(test)] +fn apply_to_span_texts_with_ruby_breaks( + span_texts: &[String], + ruby_breaks: &[Option>], +) -> Option<(Vec, OffsetMap)> { + apply_policy_to_span_texts(span_texts, ruby_breaks, TextLayoutPolicy::Japanese) +} + +#[cfg(test)] +pub fn apply_policy_to_span_texts( + span_texts: &[String], + ruby_breaks: &[Option>], + policy: TextLayoutPolicy, +) -> Option<(Vec, OffsetMap)> { + apply_policy_with_breaks(span_texts, ruby_breaks, &[], policy, &[], &[]) +} + +/// `joined[span]` is true when the span continues the ruby base of the span +/// before it, so its start is internal to that base too. +pub fn apply_policy_with_breaks( + span_texts: &[String], + ruby_breaks: &[Option>], + joined: &[bool], + policy: TextLayoutPolicy, + emergency_breaks: &[usize], + forced_breaks: &[usize], +) -> Option<(Vec, OffsetMap)> { + let jlreq = policy.uses_jlreq(); + let source = span_texts.concat(); + let boundaries: Vec = source.grapheme_indices(true).map(|(at, _)| at).collect(); + let mut source_byte = 0; + let mut source_utf16 = 0; + let mut inserted: Vec = Vec::new(); + let mut out: Vec = Vec::with_capacity(span_texts.len()); + // Base character of the previous grapheme cluster, with its class. + let mut prev: Option<(char, JlreqClass)> = None; + let mut changed = false; + // Running position in shifted UTF-16 coordinates. + let mut shifted_pos: usize = 0; + + for (span_index, text) in span_texts.iter().enumerate() { + let mut shifted_text = String::with_capacity(text.len() + 4); + let mut local_utf16 = 0usize; + for c in text.chars() { + let at_boundary = boundaries.binary_search(&source_byte).is_ok(); + source_byte += c.len_utf8(); + let source_at = source_utf16; + source_utf16 += c.len_utf16(); + if !at_boundary { + shifted_text.push(c); + shifted_pos += c.len_utf16(); + local_utf16 += c.len_utf16(); + continue; + } + let current = (c, classify(c)); + let inside_base = local_utf16 > 0 || joined.get(span_index).copied().unwrap_or(false); + let ruby_forbids_break = inside_base + && ruby_breaks + .get(span_index) + .and_then(Option::as_ref) + .is_some_and(|breaks| !breaks.contains(&local_utf16)); + let forbid_break = + ruby_forbids_break || prev.is_some_and(|p| policy.needs_joiner(p, current)); + let mut insert = |ch: char| { + shifted_text.push(ch); + inserted.push(shifted_pos); + shifted_pos += 1; + changed = true; + }; + if jlreq && prev.is_some_and(|p| takes_em_space_after(p, current)) { + insert(IDEOGRAPHIC_SPACE); + } + if jlreq && prev.is_some_and(|p| is_japanese_western_boundary(p, current)) { + insert(JAPANESE_WESTERN_SPACE); + } + // After an inserted space, the joiner keeps the break blocked. + if forced_breaks.binary_search(&source_at).is_ok() { + insert(FORCED_LINE_BREAK); + } else if forbid_break { + insert(if emergency_breaks.binary_search(&source_at).is_ok() { + BREAK_OPPORTUNITY + } else { + WORD_JOINER + }); + } else if jlreq + && current.1 == JlreqClass::Inseparable + && prev.is_some_and(|p| pair_rule(p.1, current.1).break_allowed) + { + insert(BREAK_OPPORTUNITY); + } + let layout_char = if policy.normalizes_western_spaces() && c == ' ' { + changed = true; + WESTERN_WORD_SPACE + } else { + c + }; + shifted_text.push(layout_char); + shifted_pos += c.len_utf16(); + local_utf16 += c.len_utf16(); + prev = Some(current); + } + out.push(shifted_text); + } + + if !changed { + return None; + } + Some((out, OffsetMap { inserted })) +} + +/// JLREQ §3.2.6 boundary between Japanese text and a Western letter or +/// digit. Western symbols, brackets and emoji set solid. +fn is_japanese_western_boundary( + (before, before_class): (char, JlreqClass), + (after, after_class): (char, JlreqClass), +) -> bool { + (before_class.is_japanese_text() && after_class.is_western_run() && after.is_alphanumeric()) + || (before_class.is_western_run() + && before.is_alphanumeric() + && after_class.is_japanese_text()) +} + +/// A question or exclamation mark that ends a sentence takes a one-em space +/// (JLREQ §3.1.6), as in vertical layout. +fn takes_em_space_after( + (_, before_class): (char, JlreqClass), + (_, after_class): (char, JlreqClass), +) -> bool { + before_class == JlreqClass::DividingPunctuation + && pair_rule(before_class, after_class).preferred_em >= 1.0 +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shapes::jlreq::{forbidden_at_line_end, forbidden_at_line_start}; + use skia_safe::textlayout::{ + FontCollection, ParagraphBuilder, ParagraphStyle, TextStyle, TypefaceFontProvider, + }; + use skia_safe::FontMgr; + + const TEST_FONT: &[u8] = include_bytes!("../fonts/sourcesanspro-regular.ttf"); + + fn strings(texts: &[&str]) -> Vec { + texts.iter().map(|t| t.to_string()).collect() + } + + fn apply(texts: &[&str]) -> (Vec, OffsetMap) { + apply_to_span_texts_with_ruby_breaks(&strings(texts), &[]) + .expect("expected kinsoku insertions") + } + + // ----------------------------------------------------------------- + // Insertion + // ----------------------------------------------------------------- + + #[test] + fn western_word_space_uses_one_third_em_character() { + let (texts, map) = apply(&["hello world"]); + assert_eq!(texts, vec![format!("hello{WESTERN_WORD_SPACE}world")]); + assert!( + map.inserted.is_empty(), + "substitution does not shift offsets" + ); + } + + #[test] + fn no_insertion_for_plain_cjk_text() { + assert!( + apply_to_span_texts_with_ruby_breaks(&strings(&["国境の長いトンネル"]), &[]).is_none() + ); + } + + #[test] + fn inserts_before_forbidden_start_char() { + let (texts, map) = apply(&["雪国。"]); + assert_eq!(texts, vec!["雪国\u{2060}。".to_string()]); + assert_eq!(map.inserted, vec![2]); + } + + #[test] + fn inserts_after_forbidden_end_char() { + let (texts, _) = apply(&["「雪"]); + assert_eq!(texts, vec!["「\u{2060}雪".to_string()]); + } + + #[test] + fn no_insertion_at_paragraph_start() { + // A leading start-forbidden char has no break before it to suppress. + let (texts, _) = apply(&["。あ。"]); + assert_eq!(texts, vec!["。あ\u{2060}。".to_string()]); + } + + #[test] + fn insertion_spans_boundary() { + // The pair (end of span 0, start of span 1) is evaluated; the + // joiner lands at the head of span 1. + let (texts, _) = apply(&["雪国", "。です"]); + assert_eq!( + texts, + vec!["雪国".to_string(), "\u{2060}。です".to_string()] + ); + } + + #[test] + fn inserts_quarter_em_at_japanese_western_boundaries() { + let (texts, map) = apply(&["日本Penpot版"]); + assert_eq!( + texts, + vec![format!( + "日本{JAPANESE_WESTERN_SPACE}Penpot{JAPANESE_WESTERN_SPACE}版" + )] + ); + assert_eq!(map.inserted, vec![2, 9]); + assert_eq!(map.to_original(map.to_shifted(2)), 2); + assert_eq!(map.to_original(map.to_shifted(8)), 8); + } + + #[test] + fn japanese_western_spacing_crosses_span_boundary() { + let (texts, map) = apply(&["日本", "Penpot"]); + assert_eq!( + texts, + vec![ + "日本".to_string(), + format!("{JAPANESE_WESTERN_SPACE}Penpot") + ] + ); + assert_eq!(map.inserted, vec![2]); + } + + #[test] + fn no_insertion_inside_a_variation_sequence() { + assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["葛\u{E0100}城"]), &[]).is_none()); + } + + #[test] + fn no_insertion_before_halfwidth_voiced_marks() { + assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["ガギ"]), &[]).is_none()); + } + + #[test] + fn no_insertion_inside_an_emoji_zwj_sequence() { + assert!( + apply_to_span_texts_with_ruby_breaks(&strings(&["👩\u{200D}💻\u{200D}あ"]), &[]) + .is_none() + ); + } + + #[test] + fn grapheme_extenders_keep_the_base_as_previous_character() { + let (texts, _) = apply(&["葛\u{E0100}Penpot"]); + assert_eq!( + texts, + vec![format!("葛\u{E0100}{JAPANESE_WESTERN_SPACE}Penpot")] + ); + } + + #[test] + fn ruby_joiners_stay_outside_grapheme_clusters() { + let texts = strings(&["葛\u{E0100}城"]); + let (shifted, _) = apply_to_span_texts_with_ruby_breaks(&texts, &[Some(Vec::new())]) + .expect("expected a ruby joiner"); + assert_eq!(shifted, vec!["葛\u{E0100}\u{2060}城".to_string()]); + } + + #[test] + fn no_japanese_western_space_beside_symbols() { + assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["あ😀い"]), &[]).is_none()); + assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["(注)あ"]), &[]).is_none()); + } + + #[test] + fn full_width_digits_stay_together() { + let (texts, _) = apply(&["2026年"]); + assert_eq!( + texts, + vec!["2\u{2060}0\u{2060}2\u{2060}6年".to_string()] + ); + } + + #[test] + fn numerals_keep_their_unit() { + let (texts, _) = apply(&["体重60㎏"]); + assert_eq!( + texts, + vec![format!("体重{JAPANESE_WESTERN_SPACE}60\u{2060}㎏")] + ); + } + + #[test] + fn prefixed_signs_bind_only_to_numerals() { + let (texts, _) = apply(&["C# は"]); + assert_eq!(texts, vec![format!("C#{WESTERN_WORD_SPACE}は")]); + } + + #[test] + fn a_line_may_start_with_an_inseparable_mark() { + // UAX #14 forbids the break before …; the zero-width space reopens it. + let (texts, _) = apply(&["あ…"]); + assert_eq!(texts, vec!["あ\u{200B}…".to_string()]); + let (texts, _) = apply(&["あ……"]); + assert_eq!(texts, vec!["あ\u{200B}…\u{2060}…".to_string()]); + let (texts, _) = apply(&["「……"]); + assert_eq!(texts, vec!["「\u{2060}…\u{2060}…".to_string()]); + } + + #[test] + fn japanese_western_space_follows_long_vowels_and_small_kana() { + let (texts, _) = apply(&["ユーザーID"]); + assert_eq!( + texts, + vec![format!( + "ユ\u{2060}ーザ\u{2060}ー{JAPANESE_WESTERN_SPACE}ID" + )] + ); + } + + #[test] + fn japanese_western_space_keeps_a_forbidden_break_blocked() { + let (texts, _) = apply(&["Excelっぽい"]); + assert_eq!( + texts, + vec![format!("Excel{JAPANESE_WESTERN_SPACE}\u{2060}っぽい")] + ); + } + + #[test] + fn dividing_punctuation_takes_an_em_space_before_text() { + let (texts, _) = apply(&["え?はい"]); + assert_eq!(texts, vec![format!("え\u{2060}?{IDEOGRAPHIC_SPACE}はい")]); + } + + #[test] + fn dividing_punctuation_adds_no_space_in_a_sequence_or_before_a_typed_space() { + let (texts, _) = apply(&["え!?はい"]); + assert_eq!( + texts, + vec![format!("え\u{2060}!\u{2060}?{IDEOGRAPHIC_SPACE}はい")] + ); + let (texts, _) = apply(&["え? はい"]); + assert_eq!(texts, vec!["え\u{2060}? はい".to_string()]); + let (texts, _) = apply(&["え?」"]); + assert_eq!(texts, vec!["え\u{2060}?\u{2060}」".to_string()]); + } + + #[test] + fn offset_map_lookups_match_a_linear_scan() { + let linear_to_shifted = |inserted: &[usize], original: usize| { + let mut shifted = original; + for &p in inserted { + if p <= shifted { + shifted += 1; + } else { + break; + } + } + shifted + }; + for inserted in [ + vec![], + vec![0], + vec![2, 3, 4], + vec![1, 5, 6, 10], + vec![0, 1, 2], + ] { + let map = OffsetMap { + inserted: inserted.clone(), + }; + for offset in 0..16 { + assert_eq!( + map.to_shifted(offset), + linear_to_shifted(&inserted, offset), + "to_shifted({offset}) for {inserted:?}" + ); + assert_eq!( + map.to_original(offset), + offset - inserted.iter().take_while(|&&p| p < offset).count(), + "to_original({offset}) for {inserted:?}" + ); + } + } + } + + #[test] + fn consecutive_forbidden_chars() { + let (texts, map) = apply(&["た」。"]); + assert_eq!(texts, vec!["た\u{2060}」\u{2060}。".to_string()]); + assert_eq!(map.inserted, vec![1, 3]); + } + + #[test] + fn small_kana_and_prolonged_sound() { + let (texts, _) = apply(&["コーヒー"]); + assert_eq!(texts, vec!["コ\u{2060}ーヒ\u{2060}ー".to_string()]); + let (texts, _) = apply(&["ちょっと"]); + assert_eq!(texts, vec!["ち\u{2060}ょ\u{2060}っと".to_string()]); + } + + // ----------------------------------------------------------------- + // Offset map + // ----------------------------------------------------------------- + + #[test] + fn offset_map_round_trips_every_original_index() { + let original = "「こんにちは」と彼は言った。『吾輩は猫である』(夏目漱石)!?コーヒー。"; + let (_, map) = apply(&[original]); + let len = original.encode_utf16().count(); + for i in 0..=len { + assert_eq!( + map.to_original(map.to_shifted(i)), + i, + "round-trip failed at original index {i}" + ); + } + } + + #[test] + fn offset_map_recovers_original_characters() { + let original = "た」。あ"; + let (texts, map) = apply(&[original]); + let shifted = texts.concat(); + let shifted_units: Vec = shifted.encode_utf16().collect(); + let original_units: Vec = original.encode_utf16().collect(); + for (s_idx, unit) in shifted_units.iter().enumerate() { + if *unit == WORD_JOINER as u16 { + continue; + } + assert_eq!(original_units[map.to_original(s_idx)], *unit); + } + } + + #[test] + fn offset_map_on_joiner_resolves_to_boundary() { + // "雪国。" → joiner at shifted 2; both sides of the joiner map + // to original boundary 2. + let (_, map) = apply(&["雪国。"]); + assert_eq!(map.to_original(2), 2); + assert_eq!(map.to_original(3), 2); + assert_eq!(map.to_shifted(2), 3); + } + + // ----------------------------------------------------------------- + // Real skparagraph layout + // ----------------------------------------------------------------- + + fn font_collection() -> FontCollection { + let font_mgr = FontMgr::new(); + let typeface = font_mgr + .new_from_data(skia_safe::Data::new_copy(TEST_FONT), None) + .expect("failed to load test font"); + let mut provider = TypefaceFontProvider::new(); + provider.register_typeface(typeface, Some("TestFont")); + let mut collection = FontCollection::new(); + collection.set_asset_font_manager(Some(provider.into())); + collection.set_default_font_manager(FontMgr::new(), None); + collection + } + + /// Lays out `text` at `width` and returns each line as a UTF-16 + /// (start, end) range. + fn layout_lines(text: &str, width: f32, letter_spacing: f32) -> Vec<(usize, usize)> { + let collection = font_collection(); + let paragraph_style = ParagraphStyle::default(); + let mut builder = ParagraphBuilder::new(¶graph_style, collection); + let mut style = TextStyle::default(); + style.set_font_families(&["TestFont"]); + style.set_font_size(20.0); + style.set_letter_spacing(letter_spacing); + builder.push_style(&style); + builder.add_text(text); + let mut paragraph = builder.build(); + paragraph.layout(width); + paragraph + .get_line_metrics() + .iter() + .map(|line| (line.start_index, line.end_index)) + .collect() + } + + #[test] + fn layout_breaks_before_an_ellipsis() { + // JLREQ Fig 81: という……そういう breaks as という|……そういう; only the + // two dots of …… stay together. + let (texts, map) = apply(&["という……そういう"]); + let shifted = texts.concat(); + assert_eq!(shifted.matches(BREAK_OPPORTUNITY).count(), 1); + assert!(shifted.contains("う\u{200B}…\u{2060}…")); + let lines = layout_lines(&shifted, 20.0 * 4.0 + 1.0, 0.0); + let second = map.to_original(lines[1].0); + assert_eq!(second, 3, "the second line starts at ……: {lines:?}"); + } + + fn utf16_chars(text: &str) -> Vec { + // BMP-only fixtures: one char per UTF-16 unit. + text.chars().collect() + } + + /// Asserts no line starts with a start-forbidden char nor ends with + /// an end-forbidden char, in ORIGINAL text coordinates. + fn assert_kinsoku_clean(original: &str, lines: &[(usize, usize)], map: &OffsetMap) { + let chars = utf16_chars(original); + for (i, (start, end)) in lines.iter().enumerate() { + let orig_start = map.to_original(*start); + let orig_end = map.to_original(*end); + if i > 0 { + let first = chars[orig_start]; + assert!( + !forbidden_at_line_start(first), + "line {i} starts with forbidden char {first} (text {original})" + ); + } + if i < lines.len() - 1 && orig_end > orig_start { + // The last char actually rendered on the line (end may + // include trailing whitespace-like positions). + let last = chars[orig_end - 1]; + assert!( + !forbidden_at_line_end(last), + "line {i} ends with forbidden char {last} (text {original})" + ); + } + } + } + + /// Lines mapped back to original offsets must tile the original + /// text exactly: contiguous, in order, full coverage. + fn assert_lines_tile_original(original: &str, lines: &[(usize, usize)], map: &OffsetMap) { + let mut expected_start = 0; + for (start, end) in lines { + let orig_start = map.to_original(*start); + let orig_end = map.to_original(*end); + assert_eq!(orig_start, expected_start, "line ranges must be contiguous"); + expected_start = orig_end; + } + assert_eq!(expected_start, original.encode_utf16().count()); + } + + fn fixture_lines(original: &str, width: f32) -> (Vec<(usize, usize)>, OffsetMap) { + let (texts, map) = apply(&[original]); + let lines = layout_lines(&texts.concat(), width, 0.0); + (lines, map) + } + + fn measure_width(text: &str) -> f32 { + let collection = font_collection(); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), collection); + let mut style = TextStyle::default(); + style.set_font_families(&["TestFont"]); + style.set_font_size(20.0); + builder.push_style(&style); + builder.add_text(text); + let mut p = builder.build(); + p.layout(f32::MAX); + p.longest_line() + } + + #[test] + fn joiner_is_zero_width_in_layout() { + let plain = layout_lines("国国", f32::MAX, 0.0); + let joined = layout_lines("国\u{2060}国", f32::MAX, 0.0); + assert_eq!(plain.len(), 1); + assert_eq!(joined.len(), 1); + + let collection = font_collection(); + let measure = |text: &str| { + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), collection.clone()); + let mut style = TextStyle::default(); + style.set_font_families(&["TestFont"]); + style.set_font_size(20.0); + builder.push_style(&style); + builder.add_text(text); + let mut p = builder.build(); + p.layout(f32::MAX); + p.longest_line() + }; + let diff = (measure("国国") - measure("国\u{2060}国")).abs(); + assert!(diff < 0.01, "WORD JOINER must not add width, diff {diff}"); + } + + #[test] + fn suppresses_breaks_on_kinsoku_fixtures() { + let fixtures = [ + // kinsoku-line-start + "これは長い文章です、句読点や閉じ括弧」が行頭に来てはいけません。小さい「ゃゅょっ」も同様です。", + // kinsoku-line-end + "開き括弧「や『それに(と[や【は行末に置けないので、次の行に送り込まれます。", + // prolonged-sound + "コーヒーとケーキ、サーバーとルーター。人々の時々の心。", + // small-kana-sokuon + "ちょっと待ってください。キャッシュとクッキーをチェックする。", + // long-paragraph-wrap + "国境の長いトンネルを抜けると雪国であった。夜の底が白くなった。信号所に汽車が止まった。向側の座席から娘が立って来て、島村の前のガラス窓を落した。雪の冷気が流れこんだ。", + ]; + // Vary the width to move the breaks. Each width must exceed the + // longest joined run, or skparagraph falls back to an emergency + // mid-run break. + let char_width = measure_width("国"); + for width in [9.0, 12.0, 16.5, 24.0].map(|n: f32| n * char_width) { + for original in fixtures { + let (lines, map) = fixture_lines(original, width); + assert!(lines.len() > 1, "fixture must wrap at width {width}"); + assert_kinsoku_clean(original, &lines, &map); + assert_lines_tile_original(original, &lines, &map); + } + } + } + + #[test] + fn mixed_latin_cjk_fixture() { + let original = "Penpotは2024年にWASMレンダラーを導入した。価格は¥1,500(税込)です!"; + let char_width = measure_width("国"); + for width in [9.0, 13.0, 18.0].map(|n: f32| n * char_width) { + let (lines, map) = fixture_lines(original, width); + assert_kinsoku_clean(original, &lines, &map); + assert_lines_tile_original(original, &lines, &map); + } + } + + #[test] + fn joiner_becomes_visible_under_letter_spacing() { + // skparagraph applies letter-spacing per cluster, INCLUDING the + // zero-width joiner, which would double the tracking at every + // suppressed break, so the horizontal builder gives inserted + // joiners zero letter-spacing. If this test fails (Skia stops + // spacing ignorables), that special style can go. + let collection = font_collection(); + let measure = |text: &str| { + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), collection.clone()); + let mut style = TextStyle::default(); + style.set_font_families(&["TestFont"]); + style.set_font_size(20.0); + style.set_letter_spacing(5.0); + builder.push_style(&style); + builder.add_text(text); + let mut p = builder.build(); + p.layout(f32::MAX); + p.longest_line() + }; + let diff = (measure("国\u{2060}国") - measure("国国")).abs(); + assert!( + diff > 0.01, + "letter-spacing no longer affects the joiner; the joiner \ + style in add_text_with_sheds can be removed" + ); + } +} diff --git a/render-wasm/src/shapes/modifiers.rs b/render-wasm/src/shapes/modifiers.rs index 1898ed7276..3076ef017f 100644 --- a/render-wasm/src/shapes/modifiers.rs +++ b/render-wasm/src/shapes/modifiers.rs @@ -13,7 +13,7 @@ use crate::error::Result; use crate::shapes; use crate::shapes::{ ConstraintH, ConstraintV, Frame, Group, GrowType, Layout, Modifier, PixelPrecision, Shape, - TransformEntry, TransformEntrySource, Type, + TransformEntry, TransformEntrySource, Type, VerticalAlign, }; use crate::state::{ShapesPoolRef, State}; use crate::uuid::Uuid; @@ -268,15 +268,34 @@ fn propagate_transform( ); match text_content.grow_type() { GrowType::AutoHeight => { + let width_before = text_content.size.width; let height_before = text_content.size.height; - let new_height = if width_changed { + let (new_width, new_height) = if text_content.is_vertical() { + // Vertical auto-height keeps the physical height (the + // column wrap axis) and grows width as columns are added. + if height_changed { + let mut clone = text_content.clone(); + clone.update_layout(resized_selrect); + (clone.size.width, shape_bounds_after.height()) + } else { + (width_before, shape_bounds_after.height()) + } + } else if width_changed { let mut clone = text_content.clone(); clone.update_layout(resized_selrect); - clone.size.height + (shape_bounds_after.width(), clone.size.height) } else { - height_before + (shape_bounds_after.width(), height_before) }; - if !is_close_to(height_before, new_height) && reflowed_shapes.insert(shape.id) { + // Reflow only when the grow axis (the WASM-computed + // dimension) changes; the resize itself sets the wrap + // axis. + let grow_axis_changed = if text_content.is_vertical() { + !is_close_to(width_before, new_width) + } else { + !is_close_to(height_before, new_height) + }; + if grow_axis_changed && reflowed_shapes.insert(shape.id) { entries.push_back(Modifier::reflow(shape.id, false)); if let Some(parent_id) = shape.parent_id { @@ -287,12 +306,25 @@ fn propagate_transform( } } } - let resize_transform = math::resize_matrix( + let mut resize_transform = math::resize_matrix( &shape_bounds_after, &shape_bounds_after, - shape_bounds_after.width(), + new_width, new_height, ); + if text_content.is_vertical() { + // Reflow adds columns toward the block end. Keep the + // right/center/left anchor set by vertical alignment. + let anchor = match shape.vertical_align() { + VerticalAlign::Top => shape_bounds_after.ne, + VerticalAlign::Center => { + (shape_bounds_after.nw + shape_bounds_after.ne) * 0.5 + } + VerticalAlign::Bottom => shape_bounds_after.nw, + }; + resize_transform + .post_translate(anchor - resize_transform.map_point(anchor)); + } shape_bounds_after = shape_bounds_after.transform(&resize_transform); transform.post_concat(&resize_transform); } @@ -580,6 +612,50 @@ mod tests { use crate::shapes::*; use crate::state::ShapesPool; + #[test] + fn vertical_auto_height_resize_keeps_the_right_edge() { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let rect = math::Rect::from_xywh(10.0, 20.0, 100.0, 200.0); + let mut content = TextContent::new(rect, GrowType::AutoHeight); + let mut paragraph = Paragraph::new( + TextAlign::Left, + TextDirection::LTR, + None, + None, + 1.2, + 0.0, + vec![TextSpan { + text: "あいうえおかきくけこ".to_string(), + font_size: 20.0, + ..TextSpan::default() + }], + ); + paragraph.set_writing_mode(WritingMode::VerticalRl); + content.add_paragraph(paragraph); + let size = content.update_layout(rect); + let id = Uuid::new_v4(); + let mut state = State::new(); + state.shapes.initialize(1); + let shape = state.shapes.add_shape(id); + shape.set_selrect(10.0, 20.0, 10.0 + size.width, 220.0); + shape.set_shape_type(Type::Text(content)); + let before = shape.bounds(); + let transform = math::resize_matrix(&before, &before, size.width, 60.0); + let result = propagate_modifiers( + &state, + &[TransformEntry::from_input(id, transform)], + PixelPrecision::Disabled, + ) + .unwrap(); + let transform = result.iter().find(|e| e.id == id).unwrap().transform; + let after = before.transform(&transform); + assert!(after.width() > before.width()); + assert!(is_close_to(after.height(), 60.0)); + assert!(is_close_to(after.ne.x, before.ne.x)); + } + #[test] fn test_propagate_shape() { let parent_id = Uuid::new_v4(); diff --git a/render-wasm/src/shapes/text.rs b/render-wasm/src/shapes/text.rs index d3a15eb34c..dd02ec07ed 100644 --- a/render-wasm/src/shapes/text.rs +++ b/render-wasm/src/shapes/text.rs @@ -1,4 +1,5 @@ use crate::render::text::{decoration_segments, list_marker, list_markers}; +use crate::shapes::text_paragraph::{HorizontalBuilder, LayoutParagraph}; use crate::{ math::{Bounds, Matrix, Rect}, render::{default_font, DEFAULT_EMOJI_FONT}, @@ -21,19 +22,25 @@ use skia_safe::{ }; use std::borrow::Cow; -use std::cell::Cell; +use std::cell::{Cell, RefCell}; use std::collections::HashSet; use std::rc::Rc; use unicode_segmentation::UnicodeSegmentation; -use super::FontFamily; +use super::text_attrs::*; +use super::text_horizontal::*; +use super::text_policy::{KoreanWrap, TextLayoutPolicy}; +use super::{FontFamily, FontStyle}; use crate::math::Point; +use crate::render::FallbackFonts; +#[cfg(test)] +use crate::shapes::line_breaks; use crate::shapes::{self, merge_fills, Shape, Type, VerticalAlign}; use crate::utils::{get_fallback_fonts, get_font_collection}; use crate::Uuid; // TODO: maybe move this to the wasm module? -pub type ParagraphBuilderGroup = Vec; +pub type ParagraphBuilderGroup = Vec; /// True when the modifier changes the text layout container (resize), as opposed /// to rotation/move where glyph layout can be reused. @@ -45,9 +52,15 @@ pub fn modifier_changes_text_layout(base: &Shape, modifier: &Matrix) -> bool { let after = before.transform(modifier); match text_content.grow_type() { GrowType::AutoWidth => !crate::math::is_close_to(before.height(), after.height()), - GrowType::AutoHeight | GrowType::Fixed => { - !crate::math::is_close_to(before.width(), after.width()) + GrowType::AutoHeight if text_content.is_vertical() => { + !crate::math::is_close_to(before.height(), after.height()) } + GrowType::AutoHeight => !crate::math::is_close_to(before.width(), after.width()), + GrowType::Fixed if text_content.is_vertical() => { + !crate::math::is_close_to(before.width(), after.width()) + || !crate::math::is_close_to(before.height(), after.height()) + } + GrowType::Fixed => !crate::math::is_close_to(before.width(), after.width()), } } @@ -230,7 +243,7 @@ impl TextPositionWithAffinity { #[derive(Debug)] pub struct TextContentLayoutResult( Vec, - Vec>, + Vec>, TextContentSize, ); @@ -248,13 +261,71 @@ struct CachedExtrect { bottom: f32, } +/// Vertical layout with the content version and box it was laid out for. +#[derive(Clone)] +struct VerticalLayoutCache { + version: u64, + rect: Rect, + layout: Rc, +} + +impl std::fmt::Debug for VerticalLayoutCache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("VerticalLayoutCache") + .field("version", &self.version) + .field("rect", &self.rect) + .finish_non_exhaustive() + } +} + +/// Horizontal paragraph plans with the content version they were built for. +#[derive(Clone)] +struct HorizontalPlansCache { + version: u64, + width: f32, + plans: Rc>, +} + +/// Line adjustment of one horizontal paragraph at the layout width, +/// per span: the sheds that squeeze its lines and the letter-spacing that +/// justifies them. +#[derive(Debug, Clone, Default)] +pub(crate) struct HorizontalAdjustment { + pub(crate) sheds: Vec>, + pub(crate) spacing: Vec>, +} + +/// Per paragraph; `None` for paragraphs laid out by Skia alone. +type HorizontalAdjustments = Rc>>; + +#[derive(Debug, Clone)] +struct LineAdjustmentCache { + version: u64, + width: f32, + adjustments: HorizontalAdjustments, +} + +impl std::fmt::Debug for HorizontalPlansCache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("HorizontalPlansCache") + .field("version", &self.version) + .finish_non_exhaustive() + } +} + #[derive(Debug)] pub struct TextContentLayout { pub paragraph_builders: Vec, /// Shared across shape clones (e.g. modifier transforms) so rotation/pan /// can paint without rebuilding Skia layout. Cleared builders on clone are OK. - pub paragraphs: Rc>>, + pub paragraphs: Rc>>, cached_extrect: Cell>, + /// Last vertical layout, shared across clones like `paragraphs`. + vertical: RefCell>, + /// Last horizontal paragraph plans, shared across clones. + horizontal: RefCell>, + /// Last horizontal line adjustment, shared across clones. + line_adjustment: RefCell>, } impl Default for TextContentLayout { @@ -269,6 +340,9 @@ impl Clone for TextContentLayout { paragraph_builders: vec![], paragraphs: Rc::clone(&self.paragraphs), cached_extrect: Cell::new(self.cached_extrect.get()), + vertical: RefCell::new(self.vertical.borrow().clone()), + horizontal: RefCell::new(self.horizontal.borrow().clone()), + line_adjustment: RefCell::new(self.line_adjustment.borrow().clone()), } } } @@ -285,13 +359,16 @@ impl TextContentLayout { paragraph_builders: vec![], paragraphs: Rc::new(Vec::new()), cached_extrect: Cell::new(None), + vertical: RefCell::new(None), + horizontal: RefCell::new(None), + line_adjustment: RefCell::new(None), } } pub fn set( &mut self, paragraph_builders: Vec, - paragraphs: Vec>, + paragraphs: Vec>, ) { self.paragraph_builders = paragraph_builders; self.paragraphs = Rc::new(paragraphs); @@ -368,7 +445,7 @@ pub fn vertical_align_offset(container_h: f32, content_h: f32, valign: VerticalA } } -fn intersects(paragraph: &skia_safe::textlayout::Paragraph, x: f32, y: f32) -> bool { +fn intersects(paragraph: &LayoutParagraph, x: f32, y: f32) -> bool { if y < 0.0 || y > paragraph.height() { return false; } @@ -383,34 +460,40 @@ fn intersects(paragraph: &skia_safe::textlayout::Paragraph, x: f32, y: f32) -> b } fn paragraph_intersects<'a>( - paragraphs: impl Iterator, + paragraphs: impl Iterator, x_pos: f32, y_pos: f32, ) -> bool { paragraphs - .scan(0.0_f32, |height, p| { + .scan(0.0_f32, |height, (p, x_offset)| { let prev_height = *height; *height += p.height(); - Some((prev_height, p)) + Some((prev_height, p, x_offset)) }) - .any(|(height, p)| intersects(p, x_pos, y_pos - height)) + .any(|(height, p, x_offset)| intersects(p, x_pos - x_offset, y_pos - height)) } // Performs a text auto layout without width limits. // This should be the same as text_auto_layout. -pub fn build_paragraphs_from_paragraph_builders( +pub(crate) fn build_paragraphs_from_paragraph_builders( paragraph_builders: &mut [ParagraphBuilderGroup], width: f32, -) -> Vec> { + plans: &[HorizontalParagraphPlan], +) -> Vec> { let paragraphs = paragraph_builders .iter_mut() - .map(|builders| { + .enumerate() + .map(|(index, builders)| { builders .iter_mut() .map(|builder| { - let mut paragraph = builder.build(); + let mut paragraph = LayoutParagraph::build(builder); // For auto-width, always layout with infinite width first to get intrinsic width - paragraph.layout(width); + paragraph.layout( + plans + .get(index) + .map_or(width, |plan| plan.layout_width(width)), + ); paragraph }) .collect() @@ -420,15 +503,20 @@ pub fn build_paragraphs_from_paragraph_builders( } /// Calculate the normalized line height from paragraph builders -pub fn calculate_normalized_line_height( +pub(crate) fn calculate_normalized_line_height( paragraph_builders: &mut [ParagraphBuilderGroup], width: f32, + plans: &[HorizontalParagraphPlan], ) -> f32 { let mut normalized_line_height = 0.0; - for paragraph_builder_group in paragraph_builders.iter_mut() { + for (index, paragraph_builder_group) in paragraph_builders.iter_mut().enumerate() { for paragraph_builder in paragraph_builder_group.iter_mut() { - let mut paragraph = paragraph_builder.build(); - paragraph.layout(width); + let mut paragraph = LayoutParagraph::build(paragraph_builder); + paragraph.layout( + plans + .get(index) + .map_or(width, |plan| plan.layout_width(width)), + ); let baseline = paragraph.ideographic_baseline(); if baseline > normalized_line_height { normalized_line_height = baseline; @@ -443,11 +531,14 @@ pub struct TextContent { pub paragraphs: Vec, pub bounds: Rect, pub grow_type: GrowType, + /// How over-long lines are fitted, for the whole shape. + line_adjustment: LineAdjustment, pub size: TextContentSize, pub layout: TextContentLayout, content_version: u64, layout_version: u64, layout_width: Option, + layout_height: Option, /// Canvas origin used when absolute fill shaders (image/gradient) were baked /// into cached Skia paragraphs. Kept across move clones so paint can /// translate glyphs + shaders together. See `cached_layout_paint_offset`. @@ -473,11 +564,13 @@ impl TextContent { paragraphs: Vec::new(), bounds, grow_type, + line_adjustment: LineAdjustment::default(), size: TextContentSize::default(), layout: TextContentLayout::new(), content_version: 0, layout_version: 0, layout_width: None, + layout_height: None, layout_paint_origin: None, layout_paint_size: None, } @@ -490,11 +583,13 @@ impl TextContent { paragraphs, bounds, grow_type, + line_adjustment: self.line_adjustment, size: TextContentSize::new_with_size(bounds.width(), bounds.height()), layout: TextContentLayout::new(), content_version: 0, layout_version: 0, layout_width: None, + layout_height: None, layout_paint_origin: None, layout_paint_size: None, } @@ -570,11 +665,15 @@ impl TextContent { &self.paragraphs } - pub fn has_non_ascii(&self) -> bool { + /// True when some span holds an emoji: an emoji code point, or an emoji + /// presentation selector or keycap mark (`©️`, `#️⃣`). Color emoji need an + /// overlay above strokes. + pub fn has_emoji(&self) -> bool { self.paragraphs .iter() .flat_map(|p| p.children()) - .any(|span| !span.text.is_ascii()) + .flat_map(|span| span.text.chars()) + .any(|c| super::text_shaping::is_emoji_char(c) || matches!(c, '\u{FE0F}' | '\u{20E3}')) } pub fn paragraphs_mut(&mut self) -> &mut Vec { @@ -590,10 +689,278 @@ impl TextContent { self.size.normalized_line_height } + /// Vertical layout for the box `rect`: its height is the column-wrap + /// budget and span paints resolve against it. Cached until the content or + /// the box changes; clones share it. + pub fn vertical_layout(&self, rect: &Rect) -> Rc { + if let Some(entry) = self.layout.vertical.borrow().as_ref() { + if entry.version == self.content_version && entry.rect == *rect { + return Rc::clone(&entry.layout); + } + } + let layout = Rc::new(super::text_vertical::layout_for_rect(self, rect)); + *self.layout.vertical.borrow_mut() = Some(VerticalLayoutCache { + version: self.content_version, + rect: *rect, + layout: Rc::clone(&layout), + }); + layout + } + + /// True when a horizontal paragraph gets its lines adjusted here rather + /// than by Skia: Japanese squeezing or policy-based CJK justification. + fn adjusts_lines(&self, paragraph: &Paragraph, plan: &HorizontalParagraphPlan) -> bool { + if paragraph.text_align() == TextAlign::Distribute { + return true; + } + (uses_japanese_layout(paragraph, &plan.texts) + && (paragraph.text_align() == TextAlign::Justify + || self.squeezes_lines(paragraph, plan))) + || (paragraph.layout_policy().bounded_justify() + && paragraph.text_align() == TextAlign::Justify) + } + + /// True when line adjustment may squeeze lines of a horizontal Japanese + /// paragraph: always under push-in first; under push-out first only + /// when some kinsoku run is too long for push-out to resolve, as in + /// vertical columns; never under push-out only. + fn squeezes_lines(&self, paragraph: &Paragraph, plan: &HorizontalParagraphPlan) -> bool { + paragraph.layout_policy().uses_jlreq() + && match self.line_adjustment { + LineAdjustment::PushInFirst => true, + LineAdjustment::PushOutFirst => horizontal_push_out_may_fail(paragraph, plan), + LineAdjustment::PushOutOnly => false, + } + } + + /// Squeeze sheds and justify spacing of each horizontal paragraph at + /// the layout width, cached until the content or the width + /// changes. `None` when no paragraph needs them. + pub(crate) fn horizontal_line_adjustments(&self) -> Option { + if self.is_vertical() || self.grow_type() == GrowType::AutoWidth { + return None; + } + let plans = self.horizontal_plans(); + let adjusted: Vec = self + .paragraphs + .iter() + .zip(plans.iter()) + .map(|(paragraph, plan)| self.adjusts_lines(paragraph, plan)) + .collect(); + if !adjusted.contains(&true) { + return None; + } + let width = self.get_width(self.bounds().width()); + if let Some(entry) = self.layout.line_adjustment.borrow().as_ref() { + if entry.version == self.content_version && entry.width == width { + return Some(Rc::clone(&entry.adjustments)); + } + } + let adjustments: HorizontalAdjustments = Rc::new( + self.paragraphs + .iter() + .zip(plans.iter()) + .zip(&adjusted) + .map(|((paragraph, plan), adjusted)| { + adjusted.then(|| self.adjust_horizontal_lines(paragraph, plan, width)) + }) + .collect(), + ); + *self.layout.line_adjustment.borrow_mut() = Some(LineAdjustmentCache { + version: self.content_version, + width, + adjustments: Rc::clone(&adjustments), + }); + Some(adjustments) + } + + fn adjust_horizontal_lines( + &self, + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, + width: f32, + ) -> HorizontalAdjustment { + let lay_out = |extra_sheds: &[Vec<(usize, f32)>], window: std::ops::Range| { + let mut laid_out = LayoutParagraph::build(&mut self.horizontal_measure_builder( + paragraph, + plan, + extra_sheds, + window, + )); + laid_out.layout(plan.layout_width(width)); + laid_out + }; + let (sheds, laid_out) = if self.squeezes_lines(paragraph, plan) { + horizontal_squeeze_sheds(paragraph, plan, width, self.line_adjustment, &lay_out) + } else { + let sheds = vec![Vec::new(); plan.texts.len()]; + let laid_out = lay_out(&sheds, FULL_WINDOW); + (sheds, laid_out) + }; + let spacing = if paragraph.text_align() == TextAlign::Distribute { + horizontal_pairwise_justify_spacing(paragraph, &laid_out, plan, width) + } else if paragraph.text_align() == TextAlign::Justify { + if paragraph.layout_policy().uses_jlreq() { + horizontal_jlreq_justify_spacing(paragraph, &laid_out, plan, width) + } else { + horizontal_pairwise_justify_spacing(paragraph, &laid_out, plan, width) + } + } else { + vec![Vec::new(); plan.texts.len()] + }; + HorizontalAdjustment { sheds, spacing } + } + + /// Left-aligned builder of the builder UTF-16 `window` of one paragraph + /// (`FULL_WINDOW` for all of it) with plain fills, its plan's sheds plus + /// `extra_sheds`, for measuring line adjustment. + fn horizontal_measure_builder( + &self, + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, + extra_sheds: &[Vec<(usize, f32)>], + window: std::ops::Range, + ) -> HorizontalBuilder { + let fonts = get_font_collection(); + let fallback_fonts = get_fallback_fonts(); + let mut paragraph_style = paragraph.paragraph_to_style(); + paragraph_style.set_apply_rounding_hack(false); + paragraph_style.set_text_align(skia::textlayout::TextAlign::Left); + let mut builder = HorizontalBuilder::new(¶graph_style, fonts); + for (span_index, ((span, text), sheds)) in paragraph + .children() + .iter() + .zip(&plan.texts) + .zip(&plan.pair_spacing) + .enumerate() + { + let combined: Vec<(usize, f32)> = sheds + .iter() + .chain(extra_sheds.get(span_index).into_iter().flatten()) + .copied() + .collect(); + let ruby = &plan.ruby_spacing.adjustments[span_index]; + let (text, combined, ruby) = if window == FULL_WINDOW { + (text.as_str(), combined, ruby.clone()) + } else { + let Some(range) = plan.offsets.ranges.iter().find(|r| r.span == span_index) else { + continue; + }; + let Some((slice, from)) = window_slice(text, range, &window) else { + continue; + }; + let len = slice.chars().count(); + ( + slice, + window_entries(&combined, from, len), + window_entries(ruby, from, len), + ) + }; + let text_style = span.to_style_with_paint( + &self.bounds(), + fallback_fonts, + false, + paragraph.line_height(), + None, + ); + builder.push_style(&text_style); + add_horizontal_span( + &mut builder, + span, + text, + &combined, + &ruby, + &text_style, + fonts, + ); + } + builder + } + + /// Horizontal passes of every paragraph (kinsoku texts, offsets, aki + /// sheds, ruby spacing), cached until content or width changes; clones + /// share them. A font load bumps the content version for shaped spacing. + pub(crate) fn horizontal_plans(&self) -> Rc> { + let width = if self.grow_type() == GrowType::AutoWidth { + f32::MAX + } else { + self.bounds().width() + }; + if let Some(entry) = self.layout.horizontal.borrow().as_ref() { + if entry.version == self.content_version && entry.width == width { + return Rc::clone(&entry.plans); + } + } + let plans: Rc> = Rc::new( + self.paragraphs + .iter() + .map(|paragraph| { + let mut plan = HorizontalParagraphPlan::new(paragraph); + plan.relax_overlong_words(paragraph, width, |plan| { + let sheds = vec![Vec::new(); plan.texts.len()]; + let mut measured = + LayoutParagraph::build(&mut self.horizontal_measure_builder( + paragraph, + plan, + &sheds, + FULL_WINDOW, + )); + measured.layout(width); + measured + }); + plan.retain_atomic_overflow(paragraph, width, |plan, width| { + let sheds = vec![Vec::new(); plan.texts.len()]; + let mut measured = + LayoutParagraph::build(&mut self.horizontal_measure_builder( + paragraph, + plan, + &sheds, + FULL_WINDOW, + )); + measured.layout(width); + measured + }); + plan + }) + .collect(), + ); + *self.layout.horizontal.borrow_mut() = Some(HorizontalPlansCache { + version: self.content_version, + width, + plans: Rc::clone(&plans), + }); + plans + } + + /// Writing mode applies to the whole shape: the first paragraph sets it. + pub fn is_vertical(&self) -> bool { + self.paragraphs + .first() + .is_some_and(|p| p.writing_mode() == WritingMode::VerticalRl) + } + + /// Vertical writing paints from its own layout, not the cached + /// horizontal paragraphs. Horizontal ruby, warichu and emphasis marks + /// paint from the cached paragraphs like the base text. + pub fn can_paint_from_layout_cache(&self) -> bool { + !self.is_vertical() + } + pub fn grow_type(&self) -> GrowType { self.grow_type } + pub fn line_adjustment(&self) -> LineAdjustment { + self.line_adjustment + } + + pub fn set_line_adjustment(&mut self, line_adjustment: LineAdjustment) { + if self.line_adjustment != line_adjustment { + self.line_adjustment = line_adjustment; + self.content_version = self.content_version.wrapping_add(1); + } + } + pub fn set_grow_type(&mut self, grow_type: GrowType) { if self.grow_type != grow_type { self.grow_type = grow_type; @@ -601,6 +968,101 @@ impl TextContent { } } + /// Emphasis paints outside the base glyph metrics. Use its placement + /// geometry for culling and export bounds as well as position data. + fn horizontal_emphasis_bounds(&self, selrect: &Rect, valign: VerticalAlign) -> Option { + if !self + .paragraphs + .iter() + .flat_map(Paragraph::children) + .any(|span| !span.text_emphasis.is_none()) + { + return None; + } + let total_height: f32 = self + .layout + .paragraphs + .iter() + .filter_map(|group| group.first()) + .map(|paragraph| paragraph.height()) + .sum(); + let mut y = selrect.y() + vertical_align_offset(selrect.height(), total_height, valign); + let plans = self.horizontal_plans(); + let mut bounds = Rect::new_empty(); + for ((group, model), plan) in self + .layout + .paragraphs + .iter() + .zip(&self.paragraphs) + .zip(plans.iter()) + { + let Some(paragraph) = group.first() else { + continue; + }; + let x = selrect.x() + plan.x_offset(model, selrect.width()); + for (_, ink_box) in horizontal_emphasis_mark_boxes(model, &plan.offsets, paragraph) { + bounds.join(ink_box.with_offset((x, y))); + } + y += paragraph.height(); + } + (!bounds.is_empty()).then_some(bounds) + } + + /// Readings paint outside the base glyph metrics, like emphasis marks. + /// Their ink joins the culling and export bounds. + fn horizontal_ruby_bounds(&self, selrect: &Rect, valign: VerticalAlign) -> Option { + if !self + .paragraphs + .iter() + .flat_map(Paragraph::children) + .any(TextSpan::has_ruby) + { + return None; + } + let total_height: f32 = self + .layout + .paragraphs + .iter() + .filter_map(|group| group.first()) + .map(|paragraph| paragraph.height()) + .sum(); + let mut y = selrect.y() + vertical_align_offset(selrect.height(), total_height, valign); + let plans = self.horizontal_plans(); + let mut bounds = Rect::new_empty(); + for (index, ((group, model), plan)) in self + .layout + .paragraphs + .iter() + .zip(&self.paragraphs) + .zip(plans.iter()) + .enumerate() + { + let Some(paragraph) = group.first() else { + continue; + }; + let x = selrect.x() + plan.x_offset(model, selrect.width()); + if let Some(ink) = horizontal_ruby_ink_bounds(self, index, paragraph, x, y) { + bounds.join(ink); + } + y += paragraph.height(); + } + (!bounds.is_empty()).then_some(bounds) + } + + /// Bounds of the marks and readings painted outside the glyph metrics. + fn horizontal_annotation_bounds(&self, selrect: &Rect, valign: VerticalAlign) -> Option { + match ( + self.horizontal_emphasis_bounds(selrect, valign), + self.horizontal_ruby_bounds(selrect, valign), + ) { + (Some(mut emphasis), Some(ruby)) => { + emphasis.join(ruby); + Some(emphasis) + } + (emphasis, ruby) => emphasis.or(ruby), + } + } + /// Compute a tight text rect from laid-out Skia paragraphs using glyph /// metrics (fm.top for overshoot, line descent for bottom, line left/width /// for horizontal extent). @@ -624,8 +1086,10 @@ impl TextContent { let mut has_lines = false; let mut y_accum = base_y + vertical_offset; - for group in paragraphs.iter() { + let plans = self.horizontal_plans(); + for (index, group) in paragraphs.iter().enumerate() { if let Some(paragraph) = group.first() { + let x = x + plans[index].x_offset(&self.paragraphs[index], selrect.width()); let line_metrics = paragraph.get_line_metrics(); for line in &line_metrics { let line_baseline = y_accum + line.baseline as f32; @@ -659,6 +1123,9 @@ impl TextContent { } let mut rect = Rect::from_ltrb(min_x, min_y, max_x, max_y); + if let Some(annotations) = self.horizontal_annotation_bounds(selrect, valign) { + rect.join(annotations); + } if let Some(markers) = self.list_markers_rect(selrect, valign) { rect.join(markers); } @@ -699,8 +1166,16 @@ impl TextContent { // AutoWidth paragraphs are laid out with f32::MAX, so line metrics // (line.left) reflect alignment within that huge width and are // unusable for tight bounds. Fall back to content_rect. + // Vertical writing takes its bounds from content_rect; the + // skparagraph line metrics below describe an unused horizontal layout. + if self.is_vertical() { + return self.vertical_extrect(selrect, valign); + } if self.grow_type() == GrowType::AutoWidth { let mut rect = self.content_rect(selrect, valign); + if let Some(annotations) = self.horizontal_annotation_bounds(selrect, valign) { + rect.join(annotations); + } if let Some(markers) = self.list_markers_rect(selrect, valign) { rect.join(markers); } @@ -785,7 +1260,35 @@ impl TextContent { bounds } + /// Content rect of vertical text grown to the laid-out column block, which + /// overflows a fixed shape that is too narrow for its columns. + fn vertical_extrect(&self, selrect: &Rect, valign: VerticalAlign) -> Rect { + let mut rect = self.content_rect(selrect, valign); + let layout = self.vertical_layout(selrect); + let left = selrect.left() + + super::text_vertical::block_axis_offset(selrect.width(), layout.width, valign); + rect.join(Rect::from_xywh( + left, + selrect.top(), + layout.width, + layout.height, + )); + rect + } + pub fn content_rect(&self, selrect: &Rect, valign: VerticalAlign) -> Rect { + // Auto-grow bounds follow the same block anchor as the vertical paint. + if self.is_vertical() { + let (width, height) = match self.grow_type() { + GrowType::AutoWidth => (self.size.width, self.size.height), + GrowType::AutoHeight => (self.size.width, selrect.height()), + GrowType::Fixed => (selrect.width(), selrect.height()), + }; + let x = selrect.x() + + super::text_vertical::block_axis_offset(selrect.width(), width, valign); + return Rect::from_xywh(x, selrect.y(), width, height); + } + let x = selrect.x(); let mut y = selrect.y(); @@ -817,10 +1320,31 @@ impl TextContent { self.bounds = Rect::from_ltrb(p1.x, p1.y, p2.x, p2.y); } + /// Caret position for a selrect-local point. Vertical text wraps its + /// columns at the `selrect` height, as painting does; stored bounds can + /// be taller than a fixed shape. pub fn get_caret_position_from_shape_coords( &self, point: &Point, + selrect: &Rect, + vertical_align: VerticalAlign, ) -> Option { + // Vertical writing: resolve through the vertical pass. The content + // block is right-anchored. + if self.is_vertical() { + let layout = self.vertical_layout(selrect); + let cx = point.x + - super::text_vertical::block_axis_offset( + selrect.width(), + layout.width, + vertical_align, + ); + let (paragraph, offset) = super::text_vertical::caret_from_point(&layout, cx, point.y)?; + return Some(TextPositionWithAffinity::new_downstream_affinity( + paragraph, offset, + )); + } + let mut offset_y = 0.0; let layout_paragraphs = self.layout.paragraphs.iter().flatten(); @@ -842,13 +1366,30 @@ impl TextContent { // Skia's get_glyph_position_at_coordinate expects coordinates relative to // the paragraph's top-left. For multi-paragraph or wrapped text, each // paragraph has its own origin; subtract start_y so we pass paragraph-local coords. - let para_pt = Point::new(point.x, point.y - start_y); + let plans = self.horizontal_plans(); + let x_offset = plans + .get(paragraph_index) + .zip(self.paragraphs().get(paragraph_index)) + .map_or(0.0, |(plan, model)| plan.x_offset(model, selrect.width())); + let para_pt = Point::new(point.x - x_offset, point.y - start_y); + if let Some(paragraph) = self.paragraphs().get(paragraph_index) { + if let Some(original_position) = + horizontal_warichu_hit_test(paragraph, layout_paragraph, para_pt) + { + return Some(TextPositionWithAffinity::new_downstream_affinity( + paragraph_index, + original_position, + )); + } + } let position_with_affinity = layout_paragraph.get_glyph_position_at_coordinate((para_pt.x, para_pt.y)); - if let Some(paragraph) = self.paragraphs().get(paragraph_index) { - // Skia reports UTF-16 code units, the model counts characters. - let offset = - paragraph.utf16_offset_to_char(position_with_affinity.position as usize); + if let Some(plan) = self.horizontal_plans().get(paragraph_index) { + // Skia reports builder-text UTF-16 offsets (transformed and + // kinsoku-shifted); the model counts source characters. + let offset = plan + .offsets + .builder_to_source(position_with_affinity.position as usize); return Some(TextPositionWithAffinity::new( position_with_affinity, @@ -886,9 +1427,11 @@ impl TextContent { point: &Point, view_matrix: &Matrix, shape_matrix: &Matrix, + selrect: &Rect, + vertical_align: VerticalAlign, ) -> Option { let shape_rel_point = Shape::get_relative_point(point, view_matrix, shape_matrix)?; - self.get_caret_position_from_shape_coords(&shape_rel_point) + self.get_caret_position_from_shape_coords(&shape_rel_point, selrect, vertical_align) } /// Builds the ParagraphBuilders necessary to render @@ -972,15 +1515,65 @@ impl TextContent { let fonts = get_font_collection(); let fallback_fonts = get_fallback_fonts(); let mut paragraph_group = Vec::new(); + let plans = self.horizontal_plans(); + let adjustments = align_override + .is_none() + .then(|| self.horizontal_line_adjustments()) + .flatten(); - for paragraph in self.paragraphs() { + for (paragraph_index, (paragraph, plan)) in + self.paragraphs().iter().zip(plans.iter()).enumerate() + { + let adjustment = adjustments + .as_ref() + .and_then(|adjustments| adjustments.get(paragraph_index)) + .and_then(Option::as_ref); let mut paragraph_style = paragraph.paragraph_to_style(); + if self.adjusts_lines(paragraph, plan) || plan.has_atomic_overflow() { + // Line adjustment gives characters runs of their own; rounding + // each run up would wrap the adjusted lines. The measuring + // pass shares this style so both break alike. + paragraph_style.set_apply_rounding_hack(false); + } if let Some(align) = align_override { paragraph_style.set_text_align(align); + } else if adjustment.is_some() + && matches!( + paragraph.text_align(), + TextAlign::Justify | TextAlign::Distribute + ) + { + // The justify spacing already fills the lines. + paragraph_style.set_text_align(skia::textlayout::TextAlign::Left); } - let mut builder = ParagraphBuilder::new(¶graph_style, fonts); + let mut builder = HorizontalBuilder::new(¶graph_style, fonts); let mut has_text = false; - for span in paragraph.children() { + for (span_index, (((span, text), plan_sheds), ruby_extra)) in paragraph + .children() + .iter() + .zip(&plan.texts) + .zip(&plan.pair_spacing) + .zip(&plan.ruby_spacing.adjustments) + .enumerate() + { + let squeeze = adjustment.and_then(|adjustment| adjustment.sheds.get(span_index)); + let justify = adjustment.and_then(|adjustment| adjustment.spacing.get(span_index)); + let combined_sheds: Vec<(usize, f32)>; + let sheds: &[(usize, f32)] = match squeeze { + Some(squeeze) if !squeeze.is_empty() => { + combined_sheds = plan_sheds.iter().chain(squeeze).copied().collect(); + &combined_sheds + } + _ => plan_sheds, + }; + let combined: Vec<(usize, f32)>; + let extra: &[(usize, f32)] = match justify { + Some(justify) if !justify.is_empty() => { + combined = ruby_extra.iter().chain(justify).copied().collect(); + &combined + } + _ => ruby_extra, + }; let text_style = if let Some((layer, image_fill)) = opaque_image_layer { let mut style = span.to_style( &self.bounds(), @@ -1038,12 +1631,11 @@ impl TextContent { fill_layer, ) }; - let text: String = span.apply_text_transform(); if !text.is_empty() { has_text = true; } builder.push_style(&text_style); - add_text_with_tabs(&mut builder, &text, span.font_size); + add_horizontal_span(&mut builder, span, text, sheds, extra, &text_style, fonts); } if !has_text { builder.add_text(" "); @@ -1066,11 +1658,17 @@ impl TextContent { None, ); - let normalized_line_height = - calculate_normalized_line_height(&mut measure_builders, f32::MAX); + let normalized_line_height = calculate_normalized_line_height( + &mut measure_builders, + f32::MAX, + &self.horizontal_plans(), + ); - let measure_paragraphs = - build_paragraphs_from_paragraph_builders(&mut measure_builders, f32::MAX); + let measure_paragraphs = build_paragraphs_from_paragraph_builders( + &mut measure_builders, + f32::MAX, + &self.horizontal_plans(), + ); let content_width = measure_paragraphs .iter() @@ -1082,8 +1680,11 @@ impl TextContent { // Re-layout at the intrinsic width (without the HTML margin slack). let mut paragraph_builders = self.paragraph_builder_group_from_text(None); - let paragraphs = - build_paragraphs_from_paragraph_builders(&mut paragraph_builders, content_width); + let paragraphs = build_paragraphs_from_paragraph_builders( + &mut paragraph_builders, + content_width, + &self.horizontal_plans(), + ); let height = paragraphs .iter() .flatten() @@ -1107,10 +1708,17 @@ impl TextContent { let width = self.width(); let mut paragraph_builders = self.paragraph_builder_group_from_text(None); - let normalized_line_height = - calculate_normalized_line_height(&mut paragraph_builders, width); + let normalized_line_height = calculate_normalized_line_height( + &mut paragraph_builders, + width, + &self.horizontal_plans(), + ); - let paragraphs = build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width); + let paragraphs = build_paragraphs_from_paragraph_builders( + &mut paragraph_builders, + width, + &self.horizontal_plans(), + ); let height = paragraphs .iter() .flatten() @@ -1131,10 +1739,17 @@ impl TextContent { let width = self.width(); let mut paragraph_builders = self.paragraph_builder_group_from_text(None); - let normalized_line_height = - calculate_normalized_line_height(&mut paragraph_builders, width); + let normalized_line_height = calculate_normalized_line_height( + &mut paragraph_builders, + width, + &self.horizontal_plans(), + ); - let paragraphs = build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width); + let paragraphs = build_paragraphs_from_paragraph_builders( + &mut paragraph_builders, + width, + &self.horizontal_plans(), + ); let paragraph_height = paragraphs .iter() .flatten() @@ -1160,8 +1775,21 @@ impl TextContent { } pub fn get_height(&self, width: f32) -> f32 { - let mut paragraph_builders = self.paragraph_builder_group_from_text(None); - let paragraphs = build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width); + // A prospective width needs its own plan: frozen overflow breaks + // from the current bounds must not determine a resized box's height. + let mut content = self.clone(); + content.set_xywh( + self.bounds.x(), + self.bounds.y(), + width, + self.bounds.height(), + ); + let mut paragraph_builders = content.paragraph_builder_group_from_text(None); + let paragraphs = build_paragraphs_from_paragraph_builders( + &mut paragraph_builders, + width, + &content.horizontal_plans(), + ); let paragraph_height = paragraphs .iter() .flatten() @@ -1248,6 +1876,7 @@ impl TextContent { pub fn force_next_layout_update(&mut self) { self.layout_width = None; + self.layout_height = None; self.layout_paint_origin = None; self.layout_paint_size = None; self.layout.cached_extrect.set(None); @@ -1262,11 +1891,20 @@ impl TextContent { // match the container we are laying out for. self.set_xywh(selrect.x(), selrect.y(), selrect.width(), selrect.height()); - // Auto-width ignores selrect width so get-text-dimensions can reuse the cached layout. - let layout_matches_container = self.grow_type() == GrowType::AutoWidth - || self - .layout_width - .is_some_and(|w| (w - selrect.width()).abs() < f32::EPSILON); + // Vertical columns wrap by height, horizontal lines by width. + let width_matches = self + .layout_width + .is_some_and(|w| (w - selrect.width()).abs() < f32::EPSILON); + let height_matches = self + .layout_height + .is_some_and(|h| (h - selrect.height()).abs() < f32::EPSILON); + let layout_matches_container = match self.grow_type() { + GrowType::AutoWidth => true, + GrowType::AutoHeight if self.is_vertical() => height_matches, + GrowType::AutoHeight => width_matches, + GrowType::Fixed if self.is_vertical() => width_matches && height_matches, + GrowType::Fixed => width_matches, + }; if !self.layout.needs_update() && self.layout_version == self.content_version @@ -1277,30 +1915,65 @@ impl TextContent { } self.size.set_size(selrect.width(), selrect.height()); + self.layout_height = Some(selrect.height()); - match self.grow_type() { - GrowType::AutoHeight => { - let result = self.text_layout_auto_height(); - self.layout_width = Some(result.2.width); - self.set_layout_from_result(result, selrect.width(), selrect.height()); + if self.is_vertical() { + // Vertical writing takes sizes from the vertical pass, so the + // horizontal skparagraph layout is skipped. Empty paragraph slots + // mark the layout as done. Auto-width fits both axes without + // wrapping. Auto-height wraps at the shape height and grows width + // as columns advance right-to-left. Fixed keeps both dimensions. + self.layout.set( + Vec::new(), + (0..self.paragraphs.len().max(1)) + .map(|_| Vec::new()) + .collect(), + ); + self.layout_width = Some(selrect.width()); + match self.grow_type() { + GrowType::AutoWidth => { + let layout = self.vertical_layout(&selrect); + self.size.width = layout.width.ceil().max(DEFAULT_TEXT_CONTENT_SIZE); + self.size.height = layout.height.ceil().max(DEFAULT_TEXT_CONTENT_SIZE); + self.size.max_width = self.size.width; + } + GrowType::AutoHeight => { + let layout = self.vertical_layout(&selrect); + self.size.width = layout.width.ceil().max(DEFAULT_TEXT_CONTENT_SIZE); + self.size.height = selrect.height(); + self.size.max_width = self.size.width; + } + GrowType::Fixed => {} } - GrowType::AutoWidth => { - let result = self.text_layout_auto_width(); - self.layout_width = Some(result.2.width); - self.set_layout_from_result(result, selrect.width(), selrect.height()); - } - GrowType::Fixed => { - let result = self.text_layout_fixed(); - self.layout_width = Some(result.2.width); - self.set_layout_from_result(result, selrect.width(), selrect.height()); + } else { + match self.grow_type() { + GrowType::AutoHeight => { + let result = self.text_layout_auto_height(); + self.layout_width = Some(result.2.width); + self.set_layout_from_result(result, selrect.width(), selrect.height()); + } + GrowType::AutoWidth => { + let result = self.text_layout_auto_width(); + self.layout_width = Some(result.2.width); + self.set_layout_from_result(result, selrect.width(), selrect.height()); + } + GrowType::Fixed => { + let result = self.text_layout_fixed(); + self.layout_width = Some(result.2.width); + self.set_layout_from_result(result, selrect.width(), selrect.height()); + } } } - if self.is_empty() { + if self.is_empty() && self.grow_type() != GrowType::Fixed { let (placeholder_width, placeholder_height) = self.placeholder_dimensions(selrect); - self.size.width = placeholder_width; - self.size.height = placeholder_height; - self.size.max_width = placeholder_width; + if self.grow_type() == GrowType::AutoWidth || self.is_vertical() { + self.size.width = placeholder_width; + self.size.max_width = placeholder_width; + } + if self.grow_type() == GrowType::AutoWidth || !self.is_vertical() { + self.size.height = placeholder_height; + } } self.layout_version = self.content_version; @@ -1338,6 +2011,18 @@ impl TextContent { /// If that fails we fall back to the previous WASM size or the incoming /// selrect dimensions. fn placeholder_dimensions(&self, selrect: Rect) -> (f32, f32) { + if self.is_vertical() { + let layout = self.vertical_layout(&selrect); + let font_size = self + .paragraphs + .first() + .and_then(|p| p.children().first()) + .map_or(14.0, |s| s.font_size); + return ( + layout.width.ceil().max(DEFAULT_TEXT_CONTENT_SIZE), + font_size, + ); + } if let Some(paragraph) = self.paragraphs.first() { if let Some(span) = paragraph.children().first() { let fonts = get_font_collection(); @@ -1355,7 +2040,7 @@ impl TextContent { builder.push_style(&text_style); builder.add_text("0"); - let mut paragraph_layout = builder.build(); + let mut paragraph_layout = LayoutParagraph::build(&mut builder); paragraph_layout.layout(f32::MAX); let width = paragraph_layout.max_intrinsic_width(); @@ -1404,17 +2089,39 @@ impl TextContent { let result = matrix.map_point((x_pos, y_pos)); + // Vertical writing: hit-test against the laid-out cells (absolute + // coordinates, right-anchored to the selrect). + if self.is_vertical() { + let layout = self.vertical_layout(&shape.selrect); + return super::text_vertical::intersects( + &layout, + &shape.selrect, + shape.vertical_align(), + result.x, + result.y, + ); + } + // Change coords to content space let x_pos = result.x - rect.x(); let y_pos = result.y - rect.y(); + let plans = self.horizontal_plans(); if !self.layout.paragraphs.is_empty() { // Reuse stored laid-out paragraphs paragraph_intersects( self.layout .paragraphs .iter() - .flat_map(|group| group.first()), + .enumerate() + .filter_map(|(index, group)| { + group.first().map(|paragraph| { + ( + paragraph, + plans[index].x_offset(&self.paragraphs[index], rect.width()), + ) + }) + }), x_pos, y_pos, ) @@ -1422,9 +2129,20 @@ impl TextContent { let width = self.width(); let mut paragraph_builders = self.paragraph_builder_group_from_text(None); let paragraphs = - build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width); + build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width, &plans); - paragraph_intersects(paragraphs.iter().flatten(), x_pos, y_pos) + paragraph_intersects( + paragraphs.iter().enumerate().filter_map(|(index, group)| { + group.first().map(|paragraph| { + ( + paragraph, + plans[index].x_offset(&self.paragraphs[index], width), + ) + }) + }), + x_pos, + y_pos, + ) } } } @@ -1435,18 +2153,43 @@ impl Default for TextContent { paragraphs: vec![], bounds: Rect::default(), grow_type: GrowType::Fixed, + line_adjustment: LineAdjustment::default(), size: TextContentSize::default(), layout: TextContentLayout::new(), content_version: 0, layout_version: 0, layout_width: None, + layout_height: None, layout_paint_origin: None, layout_paint_size: None, } } } -pub type TextAlign = skia::textlayout::TextAlign; +#[derive(Debug, Default, PartialEq, Clone, Copy)] +pub enum TextAlign { + #[default] + Left, + Center, + Right, + Justify, + Distribute, + Start, + End, +} + +impl From for skia::textlayout::TextAlign { + fn from(value: TextAlign) -> Self { + match value { + TextAlign::Left | TextAlign::Distribute => Self::Left, + TextAlign::Center => Self::Center, + TextAlign::Right => Self::Right, + TextAlign::Justify => Self::Justify, + TextAlign::Start => Self::Start, + TextAlign::End => Self::End, + } + } +} pub type TextDirection = skia::textlayout::TextDirection; pub type TextDecoration = skia::textlayout::TextDecoration; @@ -1472,6 +2215,15 @@ pub struct Paragraph { text_direction: TextDirection, text_decoration: Option, text_transform: Option, + writing_mode: WritingMode, + text_orientation: TextOrientation, + language: String, + korean_wrap: KoreanWrap, + /// Policy `language` and `korean_wrap` select, parsed once when either + /// changes and copied to every span. + layout_policy: TextLayoutPolicy, + /// Paragraph in the content tree last exchanged with the editor host. + source_position: u32, line_height: f32, letter_spacing: f32, list_style_type: ListStyleType, @@ -1485,6 +2237,12 @@ impl Default for Paragraph { text_direction: TextDirection::LTR, text_decoration: None, text_transform: None, + writing_mode: WritingMode::default(), + text_orientation: TextOrientation::default(), + language: String::new(), + korean_wrap: KoreanWrap::Syllable, + layout_policy: TextLayoutPolicy::Plain, + source_position: u32::MAX, line_height: 1.0, letter_spacing: 0.0, list_style_type: ListStyleType::None, @@ -1509,6 +2267,12 @@ impl Paragraph { text_direction, text_decoration, text_transform, + writing_mode: WritingMode::default(), + text_orientation: TextOrientation::default(), + language: String::new(), + korean_wrap: KoreanWrap::Syllable, + layout_policy: TextLayoutPolicy::Plain, + source_position: u32::MAX, line_height, letter_spacing, list_style_type: ListStyleType::None, @@ -1521,15 +2285,112 @@ impl Paragraph { self } + pub fn writing_mode(&self) -> WritingMode { + self.writing_mode + } + + pub fn language(&self) -> &str { + &self.language + } + + pub fn set_language(&mut self, language: String) { + self.language = language; + self.update_layout_policy(); + } + + pub fn korean_wrap(&self) -> KoreanWrap { + self.korean_wrap + } + + pub fn set_korean_wrap(&mut self, value: KoreanWrap) { + self.korean_wrap = value; + self.update_layout_policy(); + } + + pub fn layout_policy(&self) -> TextLayoutPolicy { + self.layout_policy + } + + fn update_layout_policy(&mut self) { + self.layout_policy = TextLayoutPolicy::for_language(&self.language, self.korean_wrap); + for span in &mut self.children { + span.set_language(&self.language, self.layout_policy); + } + } + + pub fn source_position(&self) -> u32 { + self.source_position + } + + pub fn set_source_position(&mut self, position: u32) { + self.source_position = position; + } + + pub fn set_writing_mode(&mut self, writing_mode: WritingMode) { + self.writing_mode = writing_mode; + } + + pub fn text_orientation(&self) -> TextOrientation { + self.text_orientation + } + + pub fn set_text_orientation(&mut self, text_orientation: TextOrientation) { + self.text_orientation = text_orientation; + } + pub fn children(&self) -> &[TextSpan] { &self.children } + /// Index ranges of the spans whose text forms one ruby base: a span with + /// a reading and the spans after it that join it + /// (`TextSpan::shares_ruby_base_with`). A reading over characters of + /// several styles, such as some with emphasis marks, is one base. + pub fn ruby_groups(&self) -> Vec> { + let mut groups: Vec> = Vec::new(); + for (index, span) in self.children.iter().enumerate() { + if !span.has_ruby() { + continue; + } + match groups.last_mut() { + Some(group) + if group.end == index + && span.shares_ruby_base_with(&self.children[index - 1]) => + { + group.end = index + 1 + } + _ => groups.push(index..index + 1), + } + } + groups + } + + /// Index in `ruby_groups` of the base of each span, or None for a span + /// without a reading. + pub fn ruby_group_of_spans(&self) -> Vec> { + let mut group_of = vec![None; self.children.len()]; + for (group, spans) in self.ruby_groups().into_iter().enumerate() { + for span in spans { + group_of[span] = Some(group); + } + } + group_of + } + pub fn children_mut(&mut self) -> &mut Vec { &mut self.children } + /// Move spans into this paragraph, inheriting its shaping language. + pub fn extend_children(&mut self, spans: impl IntoIterator) { + self.children.extend(spans.into_iter().map(|mut span| { + span.set_language(&self.language, self.layout_policy); + span + })); + } + fn set_span_positions(&mut self, index: u32) { + self.source_position = index; for (span_index, span) in self.children.iter_mut().enumerate() { span.set_position(index, span_index as u32); } @@ -1561,8 +2422,8 @@ impl Paragraph { utf16 } - /// Translate a UTF-16 offset coming from Skia into a character offset. - /// An offset inside a character rounds up, so it never splits a glyph. + /// Inverse of `char_offset_to_utf16`. An offset inside a character rounds + /// up, so it never splits a glyph. pub fn utf16_offset_to_char(&self, utf16_offset: usize) -> usize { let (mut low, mut high) = (0, self.char_count()); while low < high { @@ -1637,11 +2498,21 @@ impl Paragraph { self.list_style_type } + /// Span texts as fed to the paragraph builders (text-transform applied, + /// Japanese spacing normalized, kinsoku break suppressions inserted), + /// plus the map from original to builder-text UTF-16 offsets. Consumers + /// of laid-out offsets must translate through the map. Only paragraphs + /// with an explicit typography profile are transformed. + #[cfg(test)] + pub fn layout_span_texts(&self) -> (Vec, line_breaks::OffsetMap) { + layout_span_texts(self) + } + pub fn paragraph_to_style(&self) -> ParagraphStyle { let mut style = ParagraphStyle::default(); style.set_height(self.line_height); - style.set_text_align(self.text_align); + style.set_text_align(self.text_align.into()); style.set_text_direction(self.text_direction); style.set_replace_tab_characters(false); style.set_apply_rounding_hack(true); @@ -1657,28 +2528,6 @@ impl Paragraph { } } -/// Capitalize the first letter of each word, preserving all original whitespace. -/// Matches CSS `text-transform: capitalize` behavior: a "word" starts after -/// any non-letter character (whitespace, punctuation, digits, symbols). -fn capitalize_words(text: &str) -> String { - let mut result = String::with_capacity(text.len()); - let mut capitalize_next = true; - for c in text.chars() { - if c.is_alphabetic() { - if capitalize_next { - result.extend(c.to_uppercase()); - } else { - result.push(c); - } - capitalize_next = false; - } else { - result.push(c); - capitalize_next = true; - } - } - result -} - /// Add `text`, pushing every '\t' as a one em wide placeholder. pub fn add_text_with_tabs(builder: &mut ParagraphBuilder, text: &str, font_size: f32) { let tab = PlaceholderStyle::new( @@ -1697,30 +2546,105 @@ pub fn add_text_with_tabs(builder: &mut ParagraphBuilder, text: &str, font_size: } } -/// Filter control characters below U+0020, preserving tabs and line breaks. -/// Browser-dependent: Firefox drops them, others replace with space. -fn process_ignored_chars(text: &str, browser: u8) -> String { - text.chars() - .filter_map(|c| { - if c == '\t' || c == '\n' || c == '\r' || c == '\u{2028}' || c == '\u{2029}' { - return Some(c); - } - if c < '\u{0020}' { - if browser == Browser::Firefox as u8 { - None - } else { - Some(' ') - } - } else { - Some(c) - } +/// Text after browser filtering and CSS text-transform, plus the source +/// UTF-16 range behind each output scalar. One source scalar can yield +/// several (`ß` uppercases to `SS`); the ranges let vertical layout wrap and +/// export those glyphs as one source unit. +#[derive(Debug, Clone, PartialEq)] +pub struct AppliedTextTransform { + pub text: String, + pub(crate) source_ranges: Vec<(std::ops::Range, std::ops::Range)>, +} + +impl AppliedTextTransform { + pub fn source_utf16_range( + &self, + transformed: std::ops::Range, + ) -> std::ops::Range { + // Output ranges are contiguous and ascending: skip to the first one + // that ends past `transformed.start`. + let first = self + .source_ranges + .partition_point(|(output, _)| output.end <= transformed.start); + let mut ranges = self.source_ranges[first..] + .iter() + .take_while(|(output, _)| output.start < transformed.end) + .filter(|(output, _)| output.end > transformed.start) + .map(|(_, source)| source.clone()); + let Some(first) = ranges.next() else { + return 0..0; + }; + ranges.fold(first, |range, source| { + range.start.min(source.start)..range.end.max(source.end) }) - .collect() + } +} + +fn apply_text_transform_with_source_ranges( + text: &str, + browser: u8, + transform: Option, +) -> AppliedTextTransform { + let mut output = String::with_capacity(text.len()); + let mut source_ranges = Vec::new(); + let mut source_utf16 = 0usize; + let mut output_utf16 = 0usize; + let mut capitalize_next = true; + + for source_char in text.chars() { + let source_start = source_utf16; + source_utf16 += source_char.len_utf16(); + + // Control characters below U+0020 are filtered (Firefox drops them, + // other browsers show a space); tabs and line breaks are kept. + let processed = if matches!(source_char, '\t' | '\n' | '\r' | '\u{2028}' | '\u{2029}') + || source_char >= '\u{0020}' + { + Some(source_char) + } else if browser == Browser::Firefox as u8 { + None + } else { + Some(' ') + }; + let Some(processed) = processed else { + continue; + }; + + let transformed: String = match transform { + Some(TextTransform::Uppercase) => processed.to_uppercase().collect(), + Some(TextTransform::Lowercase) => processed.to_lowercase().collect(), + Some(TextTransform::Capitalize) if processed.is_alphabetic() && capitalize_next => { + capitalize_next = false; + processed.to_uppercase().collect() + } + Some(TextTransform::Capitalize) => { + capitalize_next = !processed.is_alphabetic(); + processed.to_string() + } + None => processed.to_string(), + }; + + for transformed_char in transformed.chars() { + let transformed_start = output_utf16; + output_utf16 += transformed_char.len_utf16(); + source_ranges.push((transformed_start..output_utf16, source_start..source_utf16)); + output.push(transformed_char); + } + } + + AppliedTextTransform { + text: output, + source_ranges, + } } #[derive(Debug, PartialEq, Clone)] pub struct TextSpan { pub text: String, + /// Effective paragraph language, copied when the paragraph is decoded. + pub language: String, + /// The paragraph's layout policy, copied with `language`. + pub layout_policy: TextLayoutPolicy, pub font_family: FontFamily, pub font_size: f32, pub line_height: f32, @@ -1730,11 +2654,69 @@ pub struct TextSpan { pub text_decoration: Option, pub text_transform: Option, pub text_direction: TextDirection, + pub text_orientation: TextOrientation, + pub text_combine_upright: TextCombineUpright, + /// Emphasis mark (圏点 / bouten) applied to each base character. + pub text_emphasis: TextEmphasis, + pub text_emphasis_position: TextEmphasisPosition, + pub text_underline_position: TextUnderlinePosition, + /// Ruby (furigana) annotation for this span; empty means no ruby. + pub ruby: String, + pub ruby_zhuyin: bool, + /// The span continues the reading of the span before it: the frontend + /// gives the spans of one reading the same group, so their text forms + /// one base (`Paragraph::ruby_groups`). + pub ruby_joins_previous: bool, + pub ruby_size: RubySize, + pub ruby_align: RubyAlign, + pub ruby_overhang: RubyOverhang, + pub ruby_side: AnnotationSide, + /// Warichu (割注): two half-size lines stacked in one column position. + pub warichu: bool, + pub font_features: FontFeatures, + pub annotation_clearance: AnnotationClearance, pub fills: Vec, pub paragraph_position: u32, pub span_position: u32, } +impl Default for TextSpan { + fn default() -> Self { + Self { + text: String::default(), + language: String::new(), + layout_policy: TextLayoutPolicy::Plain, + font_family: FontFamily::new(Uuid::nil(), 400, FontStyle::Normal), + font_size: 14.0, + line_height: 1.2, + letter_spacing: 0.0, + font_weight: 400, + font_variant_id: Uuid::nil(), + text_decoration: None, + text_transform: None, + text_direction: TextDirection::LTR, + text_orientation: TextOrientation::default(), + text_combine_upright: TextCombineUpright::default(), + text_emphasis: TextEmphasis::default(), + text_emphasis_position: TextEmphasisPosition::default(), + text_underline_position: TextUnderlinePosition::default(), + ruby: String::default(), + ruby_zhuyin: false, + ruby_joins_previous: false, + ruby_size: RubySize::default(), + ruby_align: RubyAlign::default(), + ruby_overhang: RubyOverhang::default(), + ruby_side: AnnotationSide::default(), + warichu: false, + font_features: FontFeatures::default(), + annotation_clearance: AnnotationClearance::default(), + fills: vec![], + paragraph_position: u32::MAX, + span_position: u32::MAX, + } + } +} + impl TextSpan { /// Fill at `layer` counting from the bottom (`0` = last / bottommost fill). pub fn fills_from_bottom(&self, layer: usize) -> Option<&shapes::Fill> { @@ -1771,8 +2753,7 @@ impl TextSpan { font_weight, font_variant_id, fills, - paragraph_position: u32::MAX, - span_position: u32::MAX, + ..Self::default() } } @@ -1785,10 +2766,154 @@ impl TextSpan { self.span_position = span; } + /// Ruby annotation text, without surrounding whitespace. + pub fn ruby_text(&self) -> &str { + self.ruby.trim() + } + + /// True when the span shows a reading. A warichu span shows none: the + /// note replaces the base text, so a reading has nothing to annotate. + pub fn has_ruby(&self) -> bool { + !self.warichu && !self.ruby_text().is_empty() + } + + /// True when this span continues the reading of `previous`, the span + /// before it: it joins that reading and shows the same one, so their text + /// forms one base whose first span sets the presentation. Neighbours with + /// the same reading stay apart without the join, as in 天天 (tiān tiān). + /// Zhuyin annotates one character, so its spans stand alone. + pub fn shares_ruby_base_with(&self, previous: &TextSpan) -> bool { + self.ruby_joins_previous + && self.has_ruby() + && previous.has_ruby() + && !self.is_zhuyin() + && !previous.is_zhuyin() + && self.ruby_text() == previous.ruby_text() + } + + pub fn ruby_font_size(&self) -> f32 { + self.font_size + * if self.is_zhuyin() { + super::text_zhuyin::FONT_SCALE + } else { + self.ruby_size.scale() + } + } + + pub fn is_zhuyin(&self) -> bool { + super::text_zhuyin::syllable(self).is_some() + } + + pub fn effective_ruby_side(&self) -> AnnotationSide { + if self.is_zhuyin() { + AnnotationSide::Over + } else { + self.ruby_side + } + } + + pub fn ruby_cross_room_em(&self) -> f32 { + if self.is_zhuyin() { + super::text_zhuyin::STRIP_WIDTH + } else { + self.ruby_size.scale() + } + } + + /// Warichu needs at least two characters to fill its two sub-lines. + pub fn is_warichu(&self) -> bool { + self.warichu && self.text.chars().count() >= 2 + } + + /// Room, in em of the span, that automatic annotation clearance adds to + /// the line height: the ruby at its size plus the emphasis marks. `None` + /// keeps the set line height, so annotations sit in the line gap. + pub fn annotation_room_em(&self) -> f32 { + self.annotation_side_room_em(AnnotationSide::Over, false) + + self.annotation_side_room_em(AnnotationSide::Under, false) + } + + /// Sets the shaping language and the layout policy of its paragraph. + pub fn set_language(&mut self, language: &str, policy: TextLayoutPolicy) { + self.language.clear(); + self.language.push_str(language); + self.layout_policy = policy; + } + + pub fn emphasis_side(&self, vertical: bool) -> AnnotationSide { + self.text_emphasis_position + .side(vertical, self.layout_policy) + } + + pub fn underline_below_ink(&self) -> bool { + self.text_underline_position.below_ink(self.layout_policy) + } + + /// Only readings on the same side stack with emphasis marks. + pub fn stacks_emphasis_outside_ruby(&self, vertical: bool) -> bool { + self.annotation_clearance.is_auto() + && self.has_ruby() + && (!self.is_zhuyin() || vertical) + && self.effective_ruby_side() == self.emphasis_side(vertical) + } + + pub fn decoration_room_em(&self, side: AnnotationSide, vertical: bool) -> f32 { + let underline = self + .text_decoration + .is_some_and(|kind| kind.contains(TextDecoration::UNDERLINE)); + let underline_side = if vertical { + self.text_underline_position.side() + } else { + AnnotationSide::Under + }; + let positioned = self.text_underline_position != TextUnderlinePosition::Auto + || self.text_emphasis_position != TextEmphasisPosition::Auto + || self.layout_policy.uses_clreq_positions(); + if self.annotation_clearance.is_auto() && underline && positioned && side == underline_side + { + 0.18 + } else { + 0.0 + } + } + + pub fn ruby_decoration_offset(&self, vertical: bool) -> f32 { + self.font_size * self.decoration_room_em(self.effective_ruby_side(), vertical) + } + + pub fn emphasis_outside_offset(&self, vertical: bool) -> f32 { + let ruby = if self.stacks_emphasis_outside_ruby(vertical) { + self.font_size * self.ruby_cross_room_em() + } else { + 0.0 + }; + ruby + self.font_size * self.decoration_room_em(self.emphasis_side(vertical), vertical) + } + + pub fn annotation_side_room_em(&self, side: AnnotationSide, vertical: bool) -> f32 { + if !self.annotation_clearance.is_auto() { + return 0.0; + } + let ruby = if self.has_ruby() + && (!self.is_zhuyin() || vertical) + && self.effective_ruby_side() == side + { + self.ruby_cross_room_em() + } else { + 0.0 + }; + let emphasis = if !self.text_emphasis.is_none() && self.emphasis_side(vertical) == side { + super::text_attrs::EMPHASIS_FONT_SCALE + } else { + 0.0 + }; + ruby + emphasis + self.decoration_room_em(side, vertical) + } + pub fn to_style( &self, content_bounds: &Rect, - fallback_fonts: &HashSet, + fallback_fonts: &FallbackFonts, remove_alpha: bool, paragraph_line_height: f32, ) -> skia::textlayout::TextStyle { @@ -1804,12 +2929,15 @@ impl TextSpan { fn to_style_with_paint( &self, content_bounds: &Rect, - fallback_fonts: &HashSet, + fallback_fonts: &FallbackFonts, remove_alpha: bool, paragraph_line_height: f32, fill_layer_from_bottom: Option, ) -> skia::textlayout::TextStyle { let mut style = skia::textlayout::TextStyle::default(); + if !self.language.is_empty() { + style.set_locale(&self.language); + } let paint = if remove_alpha { let mut paint = paint::Paint::default(); paint.set_color(skia::Color::BLACK); @@ -1828,7 +2956,8 @@ impl TextSpan { merge_fills(&self.fills, *content_bounds) }; - let max_line_height = f32::max(paragraph_line_height, self.line_height); + let max_line_height = + f32::max(paragraph_line_height, self.line_height) + self.annotation_room_em(); style.set_height(max_line_height); style.set_height_override(true); style.set_foreground_paint(&paint); @@ -1846,10 +2975,15 @@ impl TextSpan { DEFAULT_EMOJI_FONT.to_string(), ]; - font_families.extend(fallback_fonts.iter().cloned()); + font_families.extend(fallback_fonts.for_text(&self.language, &self.text)); style.set_font_families(&font_families); style.set_font_size(self.font_size); style.set_letter_spacing(self.letter_spacing); + match self.font_features { + FontFeatures::None => {} + FontFeatures::Palt => style.add_font_feature("palt", 1), + FontFeatures::Vpal => style.add_font_feature("vpal", 1), + } style.set_half_leading(true); style @@ -1858,7 +2992,7 @@ impl TextSpan { pub fn to_stroke_style( &self, stroke_paint: &Paint, - fallback_fonts: &HashSet, + fallback_fonts: &FallbackFonts, remove_alpha: bool, paragraph_line_height: f32, ) -> skia::textlayout::TextStyle { @@ -1893,13 +3027,20 @@ impl TextSpan { } pub fn transform_text(&self, text: &str) -> String { - let text = process_ignored_chars(text, crate::globals::current_browser()); - match self.text_transform { - Some(TextTransform::Uppercase) => text.to_uppercase(), - Some(TextTransform::Lowercase) => text.to_lowercase(), - Some(TextTransform::Capitalize) => capitalize_words(&text), - None => text, - } + apply_text_transform_with_source_ranges( + text, + crate::globals::current_browser(), + self.text_transform, + ) + .text + } + + pub fn apply_text_transform_with_source_ranges(&self) -> AppliedTextTransform { + apply_text_transform_with_source_ranges( + &self.text, + crate::globals::current_browser(), + self.text_transform, + ) } pub fn apply_text_transform(&self) -> String { @@ -1933,7 +3074,8 @@ pub struct PositionData { #[derive(Debug)] pub struct ParagraphLayout { - pub paragraph: skia::textlayout::Paragraph, + pub paragraph: LayoutParagraph, + pub source_paragraph: usize, pub x: f32, pub y: f32, pub decorations: Vec, @@ -1946,7 +3088,24 @@ pub struct TextLayoutData { pub paragraphs: Vec, } -fn direction_to_int(direction: TextDirection) -> u32 { +// Position-data `direction` values. 0 and 1 are the horizontal rtl and ltr +// runs (`direction_to_int`); the others mark vertical strips and annotation +// boxes the frontend reads differently. + +/// A vertical-rl strip. +pub const DIRECTION_VERTICAL_RL: u32 = 2; + +/// A vertical ruby strip; offsets index the *ruby* string. +pub const DIRECTION_VERTICAL_RUBY: u32 = 3; + +/// One emphasis mark (圏点) box, in any mode. +pub const DIRECTION_EMPHASIS_MARK: u32 = 4; + +/// A single upright Zhuyin glyph, in any mode; offsets index the *ruby* +/// string and the box carries its drawing baseline. +pub const DIRECTION_ZHUYIN: u32 = 5; + +pub(crate) fn direction_to_int(direction: TextDirection) -> u32 { match direction { TextDirection::RTL => 0, TextDirection::LTR => 1, @@ -1967,18 +3126,19 @@ pub fn calculate_text_layout_data( let mut position_data: Vec = Vec::new(); let mut previous_line_height = text_content.normalized_line_height(); let text_paragraphs = text_content.paragraphs(); + let plans = text_content.horizontal_plans(); // 1. Build + layout each paragraph once, recording heights as we go. let mut paragraph_heights: Vec = Vec::new(); - let mut built_groups: Vec> = + let mut built_groups: Vec> = Vec::with_capacity(paragraph_builder_groups.len()); - for paragraph_builder_group in paragraph_builder_groups.iter_mut() { + for (index, paragraph_builder_group) in paragraph_builder_groups.iter_mut().enumerate() { let group_len = paragraph_builder_group.len(); let mut paragraph_offset_y = previous_line_height; - let mut group_paragraphs: Vec = Vec::with_capacity(group_len); + let mut group_paragraphs: Vec = Vec::with_capacity(group_len); for (builder_index, paragraph_builder) in paragraph_builder_group.iter_mut().enumerate() { - let mut skia_paragraph = paragraph_builder.build(); - skia_paragraph.layout(text_width); + let mut skia_paragraph = LayoutParagraph::build(paragraph_builder); + skia_paragraph.layout(plans[index].layout_width(text_width)); if builder_index == group_len - 1 { if skia_paragraph.get_line_metrics().is_empty() { paragraph_offset_y = skia_paragraph.ideographic_baseline(); @@ -2009,17 +3169,21 @@ pub fn calculate_text_layout_data( if previous_markers.len() <= builder_index { previous_markers.resize(builder_index + 1, None); } + // Bullets hang from the box edge; only the text takes the plan offset. + let marker_x = x; + let x = x + plans[i].x_offset(&text_paragraphs[i], text_width); let decorations = text_paragraphs .get(i) - .map(|text_paragraph| { - decoration_segments(&skia_paragraph, text_paragraph, x, y_accum) + .zip(plans.get(i)) + .map(|(text_paragraph, plan)| { + decoration_segments(&skia_paragraph, text_paragraph, &plan.offsets, x, y_accum) }) .unwrap_or_default(); let marker = text_paragraphs.get(i).and_then(|text_paragraph| { list_marker( &skia_paragraph, text_paragraph, - x, + marker_x, y_accum, text_width, previous_markers[builder_index].as_ref(), @@ -2030,6 +3194,7 @@ pub fn calculate_text_layout_data( } paragraph_layouts.push(ParagraphLayout { paragraph: skia_paragraph, + source_paragraph: i, x, y: y_accum, decorations, @@ -2041,57 +3206,19 @@ pub fn calculate_text_layout_data( // Calculate position data from paragraph_layouts if !skip_position_data { - for (paragraph_index, para_layout) in paragraph_layouts.iter().enumerate() { - let current_y = para_layout.y; - let text_paragraph = text_paragraphs.get(paragraph_index); - if let Some(text_para) = text_paragraph { - let mut span_ranges: Vec<(usize, usize, usize)> = vec![]; - let mut cur = 0; - for (span_index, span) in text_para.children().iter().enumerate() { - let text: String = span.apply_text_transform(); - let text_len = text.encode_utf16().count(); - span_ranges.push((cur, cur + text_len, span_index)); - cur += text_len; - } - for (start, end, span_index) in span_ranges { - let rects = para_layout.paragraph.get_rects_for_range( - start..end, - RectHeightStyle::Tight, - RectWidthStyle::Tight, - ); - - for textbox in rects { - let direction = textbox.direct; - let mut rect = textbox.rect; - let cy = rect.top + rect.height() / 2.0; - - // Get byte positions from Skia's transformed text layout - let start_pos = para_layout - .paragraph - .get_glyph_position_at_coordinate((rect.left + 0.1, cy)) - .position as usize - - start; - - let end_pos = para_layout - .paragraph - .get_glyph_position_at_coordinate((rect.right - 0.1, cy)) - .position as usize - - start; - - rect.offset((x, current_y)); - position_data.push(PositionData { - paragraph: paragraph_index as u32, - span: span_index as u32, - start_pos: start_pos as u32, - end_pos: end_pos as u32, - x: rect.x(), - y: rect.y(), - width: rect.width(), - height: rect.height(), - direction: direction_to_int(direction), - }); - } - } + for para_layout in ¶graph_layouts { + let paragraph_index = para_layout.source_paragraph; + if let (Some(text_para), Some(plan)) = ( + text_paragraphs.get(paragraph_index), + plans.get(paragraph_index), + ) { + let entries = HorizontalPositionEntries { + paragraph_index, + paragraph: text_para, + offsets: &plan.offsets, + layout: para_layout, + }; + position_data.extend(entries.collect()); } } } @@ -2102,11 +3229,160 @@ pub fn calculate_text_layout_data( } } +/// Position data of one laid-out horizontal paragraph: strips of the spans +/// outside warichu, one strip per warichu sub-line, and one box per +/// emphasis mark. +struct HorizontalPositionEntries<'a> { + paragraph_index: usize, + paragraph: &'a Paragraph, + /// Ranges in builder-text (kinsoku-shifted) space; the map translates + /// exported positions back to span offsets. + offsets: &'a HorizontalOffsets, + layout: &'a ParagraphLayout, +} + +impl HorizontalPositionEntries<'_> { + fn collect(&self) -> Vec { + let mut entries = Vec::new(); + // Tabs are placeholders too; this keys warichu boxes by span. + let warichu_rects = super::text_horizontal::horizontal_warichu_placeholders( + self.paragraph, + &self.layout.paragraph, + ); + for range in &self.offsets.ranges { + if range.warichu { + let rect = warichu_rects + .iter() + .find(|(span, _)| *span == range.span) + .map(|(_, rect)| *rect); + if let Some(rect) = rect { + entries.extend(self.warichu_entries(range, rect)); + } + } else { + entries.extend(self.span_entries(range)); + } + } + for mut entry in + horizontal_zhuyin_entries(self.paragraph, self.offsets, &self.layout.paragraph) + { + entry.paragraph = self.paragraph_index as u32; + entry.x += self.layout.x; + entry.y += self.layout.y; + entries.push(entry); + } + for (placement, ink_box) in + horizontal_emphasis_mark_boxes(self.paragraph, self.offsets, &self.layout.paragraph) + { + if let Some(span) = self.paragraph.children().get(placement.span) { + entries.push(self.entry( + placement.span, + placement.range.clone(), + horizontal_emphasis_mark_em_box(span, &ink_box), + DIRECTION_EMPHASIS_MARK, + )); + } + } + entries + } + + /// An entry for `range` of `span`, with `rect` in the laid-out paragraph. + fn entry( + &self, + span: usize, + range: std::ops::Range, + mut rect: Rect, + direction: u32, + ) -> PositionData { + rect.offset((self.layout.x, self.layout.y)); + PositionData { + paragraph: self.paragraph_index as u32, + span: span as u32, + start_pos: range.start as u32, + end_pos: range.end as u32, + x: rect.x(), + y: rect.y(), + width: rect.width(), + height: rect.height(), + direction, + } + } + + /// One strip per warichu sub-line: the top half of the placeholder holds + /// the first line, the bottom half the second. + fn warichu_entries(&self, range: &HorizontalSpanRange, rect: Rect) -> Vec { + let Some(span) = self.paragraph.children().get(range.span) else { + return Vec::new(); + }; + let text = span.apply_text_transform(); + let split = warichu_text_lines(&text).0.encode_utf16().count(); + let end = range.source_end - range.source_start; + let half = rect.height() / 2.0; + let ltr = direction_to_int(TextDirection::LTR); + let top = Rect::from_xywh(rect.x(), rect.y(), rect.width(), half); + let bottom = Rect::from_xywh(rect.x(), rect.y() + half, rect.width(), half); + vec![ + self.entry(range.span, 0..split, top, ltr), + self.entry(range.span, split..end, bottom, ltr), + ] + } + + /// One strip per laid-out rect of a span, with its source offsets read + /// from the glyphs at the rect's edges. + fn span_entries(&self, range: &HorizontalSpanRange) -> Vec { + let laid_out = &self.layout.paragraph; + let to_span_offset = |builder_position: usize| { + let within = builder_position + .saturating_sub(range.builder_start) + .min(range.builder_end - range.builder_start); + self.offsets + .offset_map + .to_original(range.shifted_start + within) + - range.source_start + }; + laid_out + .get_rects_for_range( + range.builder_start..range.builder_end, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ) + .into_iter() + .map(|textbox| { + let rect = textbox.rect; + let cy = rect.top + rect.height() / 2.0; + let position_at = + |x: f32| laid_out.get_glyph_position_at_coordinate((x, cy)).position as usize; + let start_pos = to_span_offset(position_at(rect.left + 0.1)); + let end_pos = to_span_offset(position_at(rect.right - 0.1)); + self.entry( + range.span, + start_pos..end_pos, + rect, + direction_to_int(textbox.direct), + ) + }) + .collect() + } +} + pub fn calculate_position_data( shape: &Shape, text_content: &TextContent, skip_position_data: bool, ) -> Vec { + // Vertical writing generates position data from the vertical cells and + // needs no horizontal layout. + if text_content.is_vertical() { + if skip_position_data { + return Vec::new(); + } + let layout = text_content.vertical_layout(&shape.selrect); + return super::text_vertical::position_data( + &layout, + &shape.selrect, + shape.vertical_align(), + ); + } + let mut text_content = text_content.clone(); text_content.update_layout(shape.selrect); @@ -2127,6 +3403,151 @@ mod tests { use crate::globals::TestRenderResourcesGuard; use crate::render::RenderResources; + fn vertical_content(text: &str, grow_type: GrowType) -> TextContent { + let mut content = TextContent::new(Rect::from_xywh(10.0, 20.0, 100.0, 200.0), grow_type); + let mut paragraph = test_paragraph(&[text]); + paragraph.set_writing_mode(WritingMode::VerticalRl); + paragraph.children_mut()[0].font_size = 20.0; + content.add_paragraph(paragraph); + content + } + + #[test] + fn vertical_auto_height_reflows_when_only_height_changes() { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let mut content = vertical_content("あいうえおかきくけこ", GrowType::AutoHeight); + let tall = content.update_layout(Rect::from_xywh(10.0, 20.0, 100.0, 240.0)); + let short = content.update_layout(Rect::from_xywh(10.0, 20.0, 100.0, 60.0)); + assert!(short.width > tall.width); + assert_eq!(short.height, 60.0); + let restored = content.update_layout(Rect::from_xywh(10.0, 20.0, 100.0, 240.0)); + assert_eq!(restored.width, tall.width); + } + + #[test] + fn vertical_height_modifier_requires_layout() { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let content = vertical_content("あいうえお", GrowType::AutoHeight); + let shape = text_shape_with_cached_layout(content); + assert!(modifier_changes_text_layout( + &shape, + &Matrix::scale((1.0, 0.5)) + )); + } + + #[test] + fn empty_auto_height_preserves_the_wrap_budget() { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let mut vertical = vertical_content("", GrowType::AutoHeight); + let size = vertical.update_layout(Rect::from_xywh(10.0, 20.0, 100.0, 200.0)); + assert_eq!(size.height, 200.0); + assert!(size.width >= 20.0); + let mut horizontal = vertical_content("", GrowType::AutoHeight); + horizontal.paragraphs_mut()[0].set_writing_mode(WritingMode::HorizontalTb); + let size = horizontal.update_layout(Rect::from_xywh(10.0, 20.0, 100.0, 200.0)); + assert_eq!(size.width, 100.0); + } + + #[test] + fn empty_fixed_text_preserves_its_container() { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let mut content = vertical_content("", GrowType::Fixed); + let size = content.update_layout(Rect::from_xywh(10.0, 20.0, 100.0, 200.0)); + assert_eq!((size.width, size.height), (100.0, 200.0)); + } + + #[test] + fn a_warichu_span_shows_no_ruby_and_reserves_no_room_for_it() { + let span = TextSpan { + text: "割注".to_string(), + ruby: "よみ".to_string(), + warichu: true, + annotation_clearance: AnnotationClearance::Auto, + ..TextSpan::default() + }; + assert!(!span.has_ruby()); + assert_eq!(span.annotation_room_em(), 0.0); + } + + fn reading_span(text: &str, ruby: &str, joins_previous: bool) -> TextSpan { + TextSpan { + text: text.to_string(), + ruby: ruby.to_string(), + ruby_joins_previous: joins_previous, + ..TextSpan::default() + } + } + + fn paragraph_of(spans: Vec) -> Paragraph { + Paragraph::new( + TextAlign::Left, + TextDirection::LTR, + None, + None, + 1.2, + 0.0, + spans, + ) + } + + #[test] + fn a_reading_over_spans_of_other_styles_is_one_base() { + let emphasized = TextSpan { + text_emphasis: TextEmphasis::FilledSesame, + ..reading_span("本", "にほん", true) + }; + let paragraph = paragraph_of(vec![ + reading_span("日", "にほん", false), + emphasized, + reading_span("語", "", false), + ]); + + assert_eq!(paragraph.ruby_groups(), vec![0..2]); + assert_eq!( + paragraph.ruby_group_of_spans(), + vec![Some(0), Some(0), None] + ); + } + + #[test] + fn neighbours_with_the_same_reading_stay_apart_without_the_join() { + let paragraph = paragraph_of(vec![ + reading_span("天", "tiān", false), + reading_span("天", "tiān", false), + ]); + + assert_eq!(paragraph.ruby_groups(), vec![0..1, 1..2]); + } + + #[test] + fn a_join_to_another_reading_starts_a_new_base() { + let paragraph = paragraph_of(vec![ + reading_span("日", "に", false), + reading_span("本", "ほん", true), + ]); + + assert_eq!(paragraph.ruby_groups(), vec![0..1, 1..2]); + } + + #[test] + fn zhuyin_readings_never_join() { + let zhuyin = |joins_previous| TextSpan { + ruby_zhuyin: true, + ..reading_span("天", "ㄊㄧㄢ", joins_previous) + }; + let paragraph = paragraph_of(vec![zhuyin(false), zhuyin(true)]); + + assert_eq!(paragraph.ruby_groups(), vec![0..1, 1..2]); + } + #[test] fn vertical_align_top_keeps_the_content_at_the_origin() { assert_eq!(vertical_align_offset(200.0, 60.0, VerticalAlign::Top), 0.0); @@ -2160,100 +3581,107 @@ mod tests { ); } + fn capitalize(text: &str) -> String { + apply_text_transform_with_source_ranges( + text, + Browser::Chrome as u8, + Some(TextTransform::Capitalize), + ) + .text + } + + fn filter_ignored_chars(text: &str, browser: u8) -> String { + apply_text_transform_with_source_ranges(text, browser, None).text + } + #[test] fn capitalize_basic_words() { - assert_eq!(capitalize_words("hello world"), "Hello World"); + assert_eq!(capitalize("hello world"), "Hello World"); } #[test] fn capitalize_preserves_leading_whitespace() { - assert_eq!(capitalize_words(" hello"), " Hello"); + assert_eq!(capitalize(" hello"), " Hello"); } #[test] fn capitalize_preserves_trailing_whitespace() { - assert_eq!(capitalize_words("hello "), "Hello "); + assert_eq!(capitalize("hello "), "Hello "); } #[test] fn capitalize_preserves_multiple_spaces() { - assert_eq!(capitalize_words("hello world"), "Hello World"); + assert_eq!(capitalize("hello world"), "Hello World"); } #[test] fn capitalize_whitespace_only() { - assert_eq!(capitalize_words(" "), " "); - assert_eq!(capitalize_words(" "), " "); + assert_eq!(capitalize(" "), " "); + assert_eq!(capitalize(" "), " "); } #[test] fn capitalize_empty_string() { - assert_eq!(capitalize_words(""), ""); + assert_eq!(capitalize(""), ""); } #[test] fn capitalize_single_char() { - assert_eq!(capitalize_words("a"), "A"); + assert_eq!(capitalize("a"), "A"); } #[test] fn capitalize_already_uppercase() { - assert_eq!(capitalize_words("HELLO WORLD"), "HELLO WORLD"); + assert_eq!(capitalize("HELLO WORLD"), "HELLO WORLD"); } #[test] fn capitalize_preserves_tabs_and_newlines() { - assert_eq!(capitalize_words("hello\tworld"), "Hello\tWorld"); - assert_eq!(capitalize_words("hello\nworld"), "Hello\nWorld"); + assert_eq!(capitalize("hello\tworld"), "Hello\tWorld"); + assert_eq!(capitalize("hello\nworld"), "Hello\nWorld"); } #[test] fn capitalize_after_punctuation() { - assert_eq!(capitalize_words("(readonly)"), "(Readonly)"); - assert_eq!(capitalize_words("hello-world"), "Hello-World"); - assert_eq!(capitalize_words("one/two/three"), "One/Two/Three"); + assert_eq!(capitalize("(readonly)"), "(Readonly)"); + assert_eq!(capitalize("hello-world"), "Hello-World"); + assert_eq!(capitalize("one/two/three"), "One/Two/Three"); } #[test] fn capitalize_after_digits() { - assert_eq!(capitalize_words("item1name"), "Item1Name"); + assert_eq!(capitalize("item1name"), "Item1Name"); } #[test] - fn process_ignored_chars_preserves_spaces() { - assert_eq!(process_ignored_chars("hello world", 0), "hello world"); + fn ignored_chars_preserves_spaces() { + assert_eq!(filter_ignored_chars("hello world", 0), "hello world"); } #[test] - fn process_ignored_chars_preserves_line_breaks() { - assert_eq!(process_ignored_chars("hello\nworld", 0), "hello\nworld"); - assert_eq!(process_ignored_chars("hello\rworld", 0), "hello\rworld"); + fn ignored_chars_preserves_line_breaks() { + assert_eq!(filter_ignored_chars("hello\nworld", 0), "hello\nworld"); + assert_eq!(filter_ignored_chars("hello\rworld", 0), "hello\rworld"); } #[test] - fn process_ignored_chars_preserves_tabs() { - assert_eq!(process_ignored_chars("hello\tworld", 0), "hello\tworld"); + fn ignored_chars_preserves_tabs() { + assert_eq!(filter_ignored_chars("hello\tworld", 0), "hello\tworld"); assert_eq!( - process_ignored_chars("hello\tworld", Browser::Firefox as u8), + filter_ignored_chars("hello\tworld", Browser::Firefox as u8), "hello\tworld" ); } #[test] - fn process_ignored_chars_replaces_control_chars_chrome() { + fn ignored_chars_replaces_control_chars_chrome() { // U+0001 (SOH) should become space in non-Firefox - assert_eq!( - process_ignored_chars("a\x01b", Browser::Chrome as u8), - "a b" - ); + assert_eq!(filter_ignored_chars("a\x01b", Browser::Chrome as u8), "a b"); } #[test] - fn process_ignored_chars_removes_control_chars_firefox() { - assert_eq!( - process_ignored_chars("a\x01b", Browser::Firefox as u8), - "ab" - ); + fn ignored_chars_removes_control_chars_firefox() { + assert_eq!(filter_ignored_chars("a\x01b", Browser::Firefox as u8), "ab"); } fn test_paragraph(texts: &[&str]) -> Paragraph { @@ -2292,7 +3720,11 @@ mod tests { let para = test_paragraph(&["Añadir"]); for offset in 0..=6 { assert_eq!(para.char_offset_to_utf16(offset), offset); - assert_eq!(para.utf16_offset_to_char(offset), offset); + assert_eq!( + HorizontalOffsets::new(¶).source_to_builder(offset), + offset + ); + assert_eq!(horizontal_builder_to_source(¶, offset), offset); } } @@ -2305,16 +3737,30 @@ mod tests { assert_eq!(para.char_offset_to_utf16(2), 3); assert_eq!(para.char_offset_to_utf16(3), 4); - assert_eq!(para.utf16_offset_to_char(0), 0); - assert_eq!(para.utf16_offset_to_char(1), 1); - assert_eq!(para.utf16_offset_to_char(3), 2); - assert_eq!(para.utf16_offset_to_char(4), 3); + assert_eq!(horizontal_builder_to_source(¶, 0), 0); + assert_eq!(horizontal_builder_to_source(¶, 1), 1); + assert_eq!(horizontal_builder_to_source(¶, 3), 2); + assert_eq!(horizontal_builder_to_source(¶, 4), 3); } #[test] - fn utf16_offset_inside_a_surrogate_pair_rounds_to_a_char_boundary() { - let para = test_paragraph(&["a😀b"]); + fn utf16_offsets_map_back_to_char_offsets() { + let para = test_paragraph(&["a", "😀b"]); + assert_eq!(para.utf16_offset_to_char(0), 0); + assert_eq!(para.utf16_offset_to_char(1), 1); assert_eq!(para.utf16_offset_to_char(2), 2); + assert_eq!(para.utf16_offset_to_char(3), 2); + assert_eq!(para.utf16_offset_to_char(4), 3); + + let mut para = test_paragraph(&["Straße"]); + para.children_mut()[0].text_transform = Some(TextTransform::Uppercase); + assert_eq!(para.utf16_offset_to_char(7), 6); + } + + #[test] + fn builder_offset_inside_a_surrogate_pair_rounds_to_a_char_boundary() { + let para = test_paragraph(&["a😀b"]); + assert_eq!(horizontal_builder_to_source(¶, 2), 2); } #[test] @@ -2326,7 +3772,10 @@ mod tests { assert_eq!(para.char_offset_to_utf16(4), 4); assert_eq!(para.char_offset_to_utf16(6), 7); assert_eq!(para.char_offset_to_utf16(5), 6); - assert_eq!(para.utf16_offset_to_char(7), 6); + assert_eq!(HorizontalOffsets::new(¶).source_to_builder(4), 4); + assert_eq!(HorizontalOffsets::new(¶).source_to_builder(5), 6); + assert_eq!(HorizontalOffsets::new(¶).source_to_builder(6), 7); + assert_eq!(horizontal_builder_to_source(¶, 7), 6); } #[test] @@ -2350,6 +3799,18 @@ mod tests { assert_eq!(para.prev_grapheme_offset(3), 1); } + #[test] + fn builder_range_covers_the_whole_glyph() { + let para = test_paragraph(&["a😀b"]); + let len_at = |offset| { + HorizontalOffsets::new(¶).source_to_builder(offset + 1) + - HorizontalOffsets::new(¶).source_to_builder(offset) + }; + assert_eq!(len_at(0), 1); + assert_eq!(len_at(1), 2); + assert_eq!(len_at(2), 1); + } + fn sample_text_content() -> TextContent { let bounds = Rect::from_xywh(0.0, 0.0, 200.0, 100.0); let mut content = TextContent::new(bounds, GrowType::Fixed); @@ -2409,6 +3870,9 @@ mod tests { #[test] fn has_usable_paint_layout_false_when_rotated_and_resized() { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); let mut content = sample_text_content(); content.layout.paragraphs = Rc::new(vec![vec![]]); content.layout_width = Some(200.0); @@ -2731,4 +4195,20 @@ mod tests { layout.clear(); assert!(layout.needs_update()); } + + #[test] + fn transformed_text_maps_expanded_scalars_to_their_source_range() { + let transformed = apply_text_transform_with_source_ranges( + "AßB", + Browser::Chrome as u8, + Some(TextTransform::Uppercase), + ); + + assert_eq!(transformed.text, "ASSB"); + assert_eq!(transformed.source_utf16_range(0..1), 0..1); + assert_eq!(transformed.source_utf16_range(1..2), 1..2); + assert_eq!(transformed.source_utf16_range(2..3), 1..2); + assert_eq!(transformed.source_utf16_range(1..3), 1..2); + assert_eq!(transformed.source_utf16_range(3..4), 2..3); + } } diff --git a/render-wasm/src/shapes/text_allocation.rs b/render-wasm/src/shapes/text_allocation.rs new file mode 100644 index 0000000000..09392ee320 --- /dev/null +++ b/render-wasm/src/shapes/text_allocation.rs @@ -0,0 +1,164 @@ +//! Equal division of an adjustment over capped opportunities, shared by the +//! horizontal and vertical justification and squeezing passes. + +use crate::shapes::jlreq::{pair_rule, JlreqClass}; +use crate::shapes::text_policy::TextLayoutPolicy; + +/// Amounts below this count as zero when dividing adjustments. +const ALLOCATION_EPSILON: f32 = 0.0001; + +/// Divide `amount` equally across capped opportunities, redistributing the +/// remainder whenever one opportunity reaches its cap. +pub(crate) fn capped_equal_allocations( + capacities: &[(usize, f32)], + amount: f32, +) -> Vec<(usize, f32)> { + let mut allocations: Vec<(usize, f32)> = capacities.iter().map(|(i, _)| (*i, 0.0)).collect(); + let mut remaining = amount.max(0.0); + while remaining > ALLOCATION_EPSILON { + let active: Vec = capacities + .iter() + .enumerate() + .filter_map(|(slot, (_, cap))| { + (allocations[slot].1 + ALLOCATION_EPSILON < *cap).then_some(slot) + }) + .collect(); + if active.is_empty() { + break; + } + let share = remaining / active.len() as f32; + let mut used = 0.0; + for slot in active { + let cap = capacities[slot].1; + let delta = share.min(cap - allocations[slot].1); + allocations[slot].1 += delta; + used += delta; + } + if used <= ALLOCATION_EPSILON { + break; + } + remaining -= used; + } + allocations +} + +/// Priority and pixel cap of the justification gap between `before` and +/// `after`, at the smaller adjacent font size `em`, or `None` when the gap +/// takes no expansion. Distribution (`distribute_budget`, the line or column +/// budget) fills every gap at one priority, capped only by that budget; +/// otherwise the policy bounds the gap (`TextLayoutPolicy::expansion_em`). +pub(crate) fn bounded_expansion( + policy: TextLayoutPolicy, + before: char, + after: char, + em: f32, + distribute_budget: Option, +) -> Option<(u8, f32)> { + let (priority, capacity) = match distribute_budget { + Some(budget) => (1, budget / em.max(0.01)), + None => policy.expansion_em(before, after), + }; + (priority > 0).then_some((priority, capacity * em)) +} + +/// Divide `slack` over bounded justification `opportunities` (index, +/// priority, cap in px): priority 1 first, then priority 2 with what is +/// left, equally within a priority. Returns (index, amount) per allocation. +pub(crate) fn allocate_bounded_expansion( + opportunities: &[(usize, u8, f32)], + slack: f32, +) -> Vec<(usize, f32)> { + let mut remaining = slack; + let mut allocations = Vec::new(); + for priority in 1..=2 { + let caps: Vec<_> = opportunities + .iter() + .filter(|(_, p, _)| *p == priority) + .map(|(index, _, cap)| (*index, *cap)) + .collect(); + for (index, amount) in capped_equal_allocations(&caps, remaining) { + allocations.push((index, amount)); + remaining -= amount; + } + } + allocations +} + +/// Priority and pixel cap of the JLREQ justification gap between classes +/// `before` and `after`, which already hold `spacing_em` of pair spacing, at +/// the smaller adjacent font size `em`; `None` when the pair table gives the +/// gap no expansion. Both axes classify their gaps with this. +pub(crate) fn jlreq_expansion_gap( + before: JlreqClass, + after: JlreqClass, + spacing_em: f32, + em: f32, +) -> Option<(u8, f32)> { + let rule = pair_rule(before, after); + (rule.expand_priority > 0).then(|| { + ( + rule.expand_priority, + (rule.maximum_em - spacing_em).max(0.0) * em, + ) + }) +} + +/// JLREQ ordered expansion (§3.8.4) of one line or column's `slack` over +/// its `gaps` (index, priority, cap in px) from `jlreq_expansion_gap`: +/// stages 1–3 widen gaps in priority order, equally within a stage and up +/// to each cap; a fourth stage spreads what is left evenly over every gap. +/// Returns (index, amount) per allocation; an index may repeat. +pub(crate) fn jlreq_ordered_expansion(gaps: &[(usize, u8, f32)], slack: f32) -> Vec<(usize, f32)> { + let mut remaining = slack.max(0.0); + let mut allocations = Vec::new(); + for priority in 1..=3 { + let caps: Vec<(usize, f32)> = gaps + .iter() + .filter(|(_, p, _)| *p == priority) + .map(|(index, _, cap)| (*index, *cap)) + .collect(); + for (index, amount) in capped_equal_allocations(&caps, remaining) { + allocations.push((index, amount)); + remaining -= amount; + } + } + if remaining > ALLOCATION_EPSILON && !gaps.is_empty() { + let extra = remaining / gaps.len() as f32; + allocations.extend(gaps.iter().map(|(index, _, _)| (*index, extra))); + } + allocations +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ordered_expansion_fills_each_stage_before_the_next() { + // Inter-script (stage 2, half an em) before solid kana (stage 3). + let script = jlreq_expansion_gap(JlreqClass::Hiragana, JlreqClass::Western, 0.25, 20.0); + let kana = jlreq_expansion_gap(JlreqClass::Hiragana, JlreqClass::Hiragana, 0.0, 20.0); + assert_eq!(script, Some((2, 5.0))); + assert_eq!(kana, Some((3, 5.0))); + assert_eq!( + jlreq_expansion_gap(JlreqClass::Comma, JlreqClass::Hiragana, 0.5, 20.0), + None, + "punctuation aki takes no justification" + ); + let gaps = [(0, 3, 5.0), (1, 2, 5.0), (2, 3, 5.0)]; + let total = |allocations: &[(usize, f32)], index: usize| -> f32 { + allocations + .iter() + .filter(|(at, _)| *at == index) + .map(|(_, amount)| amount) + .sum() + }; + let small = jlreq_ordered_expansion(&gaps, 4.0); + assert_eq!((total(&small, 0), total(&small, 1)), (0.0, 4.0)); + let medium = jlreq_ordered_expansion(&gaps, 9.0); + assert_eq!((total(&medium, 0), total(&medium, 1)), (2.0, 5.0)); + let large = jlreq_ordered_expansion(&gaps, 18.0); + assert!((total(&large, 0) - 6.0).abs() < 0.001); + assert!((total(&large, 1) - 6.0).abs() < 0.001); + } +} diff --git a/render-wasm/src/shapes/text_attrs.rs b/render-wasm/src/shapes/text_attrs.rs new file mode 100644 index 0000000000..804b9c21f2 --- /dev/null +++ b/render-wasm/src/shapes/text_attrs.rs @@ -0,0 +1,256 @@ +//! Typographic span and paragraph attributes shared by the horizontal and +//! vertical engines: writing mode, orientation, emphasis, decoration +//! positions, annotation clearance, font features, line adjustment and the +//! ruby presentation enums. + +use crate::shapes::jlreq::classify; +use crate::shapes::text_policy::TextLayoutPolicy; + +pub const EMPHASIS_FONT_SCALE: f32 = 0.5; + +/// Whether an emphasis mark goes on `character`: never on whitespace nor on +/// the punctuation JLREQ leaves unmarked. +pub(crate) fn emphasis_char_allowed(character: char) -> bool { + !character.is_whitespace() && !classify(character).is_emphasis_prohibited() +} + +/// Block flow direction of a paragraph. Horizontal uses skparagraph; +/// vertical-rl uses the custom vertical pass: columns top to bottom, +/// advancing right to left. +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum WritingMode { + #[default] + HorizontalTb, + VerticalRl, +} + +/// How a line slightly too long for its box is fitted (JLREQ §3.8.1; Adobe's +/// Kinsoku Type). Squeezing reduces punctuation aki to keep the next +/// character; pushing out moves characters to the next line. Alignment then +/// decides what happens to the space a line leaves over. +// The variants are Adobe's Kinsoku Type values and the JS keys. +#[allow(clippy::enum_variant_names)] +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum LineAdjustment { + /// Squeeze first; push out when the aki cannot absorb the overflow. + #[default] + PushInFirst, + /// Push out first; squeeze only when kinsoku leaves no other break. + PushOutFirst, + /// Never squeeze. + PushOutOnly, +} + +/// Glyph orientation inside vertical flow: `Mixed` rotates non-CJK runs +/// sideways, `Upright` keeps every character upright. Ignored in +/// horizontal writing. +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum TextOrientation { + #[default] + Mixed, + Upright, +} + +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum TextCombineUpright { + #[default] + None, + All, + /// Runs of up to 4 ASCII or full-width digits combine into one upright + /// cell, so a lone digit stands upright (CSS `digits`). + Digits, + /// Like `Digits`, for runs of up to 2 (CSS `digits 2`). + Digits2, + /// Like `Digits`, for runs of up to 3. + Digits3, +} + +impl TextCombineUpright { + /// Longest digit run that combines, when digits mode is active. + pub fn digits_max(self) -> Option { + match self { + TextCombineUpright::Digits => Some(4), + TextCombineUpright::Digits2 => Some(2), + TextCombineUpright::Digits3 => Some(3), + _ => None, + } + } +} + +/// Independent emphasis positions in the two writing directions. +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum TextEmphasisPosition { + #[default] + Auto, + OverRight, + OverLeft, + UnderRight, + UnderLeft, +} + +impl TextEmphasisPosition { + /// Side of the marks; `Auto` takes the default of the paragraph policy. + pub fn side(self, vertical: bool, policy: TextLayoutPolicy) -> AnnotationSide { + let under = if vertical { + matches!(self, Self::OverLeft | Self::UnderLeft) + } else { + matches!(self, Self::UnderRight | Self::UnderLeft) + || (self == Self::Auto && policy.uses_clreq_positions()) + }; + if under { + AnnotationSide::Under + } else { + AnnotationSide::Over + } + } +} + +/// CSS underline position: horizontal depth and vertical side are independent. +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum TextUnderlinePosition { + #[default] + Auto, + Under, + Left, + Right, + UnderLeft, + UnderRight, +} + +impl TextUnderlinePosition { + pub fn side(self) -> AnnotationSide { + if matches!(self, Self::Right | Self::UnderRight) { + AnnotationSide::Over + } else { + AnnotationSide::Under + } + } + + /// Whether a horizontal underline sits below the ink; `Auto` takes the + /// default of the paragraph policy. + pub fn below_ink(self, policy: TextLayoutPolicy) -> bool { + matches!(self, Self::Under | Self::UnderLeft | Self::UnderRight) + || (self == Self::Auto && policy.uses_clreq_positions()) + } +} + +/// Emphasis mark (圏点 / bouten) applied per span, mirroring CSS +/// `text-emphasis-style`. Position is a separate style with a language default. +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum TextEmphasis { + #[default] + None, + FilledDot, + OpenDot, + FilledCircle, + OpenCircle, + FilledSesame, + OpenSesame, +} + +impl TextEmphasis { + pub fn is_none(self) -> bool { + matches!(self, TextEmphasis::None) + } + + /// The glyph drawn as the emphasis mark, following the CSS + /// `text-emphasis-style` character mapping. + pub fn mark_char(self) -> Option { + match self { + TextEmphasis::None => None, + TextEmphasis::FilledDot => Some('•'), + TextEmphasis::OpenDot => Some('◦'), + TextEmphasis::FilledCircle => Some('●'), + TextEmphasis::OpenCircle => Some('○'), + TextEmphasis::FilledSesame => Some('﹅'), + TextEmphasis::OpenSesame => Some('﹆'), + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum FontFeatures { + #[default] + None, + Palt, + Vpal, +} + +/// Whether ruby and emphasis on the same side stack. `Auto` places emphasis +/// outside over-side ruby and reserves room for both; `None` lets them share +/// one layer. +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum AnnotationClearance { + #[default] + None, + Auto, +} + +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum RubySize { + #[default] + Half, + Third, + Quarter, +} + +impl RubySize { + pub fn scale(self) -> f32 { + match self { + Self::Half => 0.5, + Self::Third => 1.0 / 3.0, + Self::Quarter => 0.25, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum RubyAlign { + #[default] + SpaceAround, + Center, + Start, + SpaceBetween, +} + +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum RubyOverhang { + #[default] + Auto, + None, +} + +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub enum AnnotationSide { + #[default] + Over, + Under, +} + +impl AnnotationClearance { + pub fn is_auto(self) -> bool { + matches!(self, AnnotationClearance::Auto) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn emphasis_excludes_whitespace_and_japanese_punctuation() { + for character in " \t\n、。,.「」『』()[]【】〔〕〈〉《》‘’“”".chars() + { + assert!( + !emphasis_char_allowed(character), + "emphasis must skip {character:?}" + ); + } + for character in "漢あA1・!?".chars() { + assert!( + emphasis_char_allowed(character), + "emphasis should mark {character:?}" + ); + } + } +} diff --git a/render-wasm/src/shapes/text_horizontal/emphasis.rs b/render-wasm/src/shapes/text_horizontal/emphasis.rs new file mode 100644 index 0000000000..cb431eace7 --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/emphasis.rs @@ -0,0 +1,218 @@ +use super::*; +use crate::shapes::text_paragraph::LayoutParagraph; + +#[derive(Debug, Clone)] +pub(crate) struct HorizontalEmphasisPlacement { + pub(crate) span: usize, + /// UTF-16 range of the base character in the span's transformed text. + pub(crate) range: std::ops::Range, + pub(crate) mark: char, + /// Base character rect in the laid-out paragraph. + pub(crate) rect: skia::Rect, + /// Top of the base character's em box (`horizontal_base_em_top`). + pub(crate) em_top: f32, +} + +/// Place one emphasis mark beside each eligible character, from the laid-out +/// rect of each character so marks follow SkParagraph's wrapping and bidi. +pub(crate) fn horizontal_emphasis_placements( + paragraph: &Paragraph, + offsets: &HorizontalOffsets, + laid_out: &LayoutParagraph, +) -> Vec { + let offset_map = &offsets.offset_map; + let lines = laid_out.get_line_metrics(); + let mut placements = Vec::new(); + + for range in offsets.ranges.iter().filter(|range| !range.warichu) { + let Some(span) = paragraph.children().get(range.span) else { + continue; + }; + let Some(mark) = span.text_emphasis.mark_char() else { + continue; + }; + let transformed = span.apply_text_transform(); + let mut local_utf16 = 0usize; + for character in transformed.chars() { + let next_utf16 = local_utf16 + character.len_utf16(); + if emphasis_char_allowed(character) { + let shifted_start = offset_map.to_shifted(range.source_start + local_utf16); + let shifted_end = offset_map.to_shifted(range.source_start + next_utf16); + let builder_start = + range.builder_start + shifted_start.saturating_sub(range.shifted_start); + let builder_end = + range.builder_start + shifted_end.saturating_sub(range.shifted_start); + let scalar_rect = laid_out + .get_rects_for_range( + builder_start..builder_end, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ) + .into_iter() + .map(|textbox| textbox.rect) + .reduce(|mut rect, next| { + rect.join(next); + rect + }); + if let Some(rect) = scalar_rect { + placements.push(HorizontalEmphasisPlacement { + span: range.span, + range: local_utf16..next_utf16, + mark, + rect, + em_top: horizontal_base_em_top( + laid_out, + &lines, + builder_start, + span.font_size, + rect, + ), + }); + } + } + local_utf16 = next_utf16; + } + } + placements +} + +/// One emphasis mark shaped at its span's style. Paint, bounds and position +/// data all place this measurement, so they agree on where the mark is. +pub(crate) struct HorizontalEmphasisMark { + paragraph: LayoutParagraph, + /// Tight rect of the mark in `paragraph`. + pub(crate) ink: skia::Rect, +} + +impl HorizontalEmphasisMark { + /// Shape `mark` at the style of `range` in the laid-out paragraph. + pub(crate) fn measure( + laid_out: &LayoutParagraph, + range: &HorizontalSpanRange, + span: &TextSpan, + mark: char, + ) -> Option { + let mut style = horizontal_span_style(laid_out, range)?; + style.set_font_size(span.font_size * EMPHASIS_FONT_SCALE); + style.set_height(1.0); + style.set_height_override(true); + style.set_letter_spacing(0.0); + let paragraph = mini_paragraph(&mark.to_string(), &style, get_font_collection()); + let ink = paragraph + .get_rects_for_range( + 0..mark.len_utf16(), + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ) + .into_iter() + .map(|textbox| textbox.rect) + .reduce(|mut rect, next| { + rect.join(next); + rect + }) + .unwrap_or_else(|| skia::Rect::from_wh(paragraph.longest_line(), paragraph.height())); + Some(Self { paragraph, ink }) + } +} + +/// Box of a horizontal emphasis mark whose ink is `ink`, in the laid-out +/// paragraph: centred beside its base character on the selected side, +/// outside stacked annotations. This is exactly where the mark paints. +pub(crate) fn horizontal_emphasis_mark_box( + span: &TextSpan, + placement: &HorizontalEmphasisPlacement, + ink: &skia::Rect, +) -> skia::Rect { + let offset = span.emphasis_outside_offset(false); + let top = if span.emphasis_side(false) == AnnotationSide::Over { + placement.em_top - offset - ink.height() + } else { + placement.rect.bottom() + offset + }; + skia::Rect::from_xywh( + placement.rect.center_x() - ink.width() / 2.0, + top, + ink.width(), + ink.height(), + ) +} + +/// Em box of a mark painted in `ink_box`, centred on it. Position data +/// carries it: SVG export draws the mark at a font size of its width. +pub(crate) fn horizontal_emphasis_mark_em_box(span: &TextSpan, ink_box: &skia::Rect) -> skia::Rect { + let size = span.font_size * EMPHASIS_FONT_SCALE; + skia::Rect::from_xywh( + ink_box.center_x() - size / 2.0, + ink_box.center_y() - size / 2.0, + size, + size, + ) +} + +/// Call `f` with each placed mark, its span, its shaped mark and the box +/// it paints in, shaping one mark per span range. +fn for_each_horizontal_emphasis_mark( + paragraph: &Paragraph, + offsets: &HorizontalOffsets, + laid_out: &LayoutParagraph, + mut f: impl FnMut(&TextSpan, HorizontalEmphasisPlacement, &HorizontalEmphasisMark, skia::Rect), +) { + let mut placements = horizontal_emphasis_placements(paragraph, offsets, laid_out); + for range in offsets.ranges.iter().filter(|range| !range.warichu) { + let Some(span) = paragraph.children().get(range.span) else { + continue; + }; + let Some(mark) = span.text_emphasis.mark_char() else { + continue; + }; + let Some(shaped) = HorizontalEmphasisMark::measure(laid_out, range, span, mark) else { + continue; + }; + let (own, rest) = placements + .into_iter() + .partition(|placement| placement.span == range.span && placement.mark == mark); + placements = rest; + for placement in own { + let ink_box = horizontal_emphasis_mark_box(span, &placement, &shaped.ink); + f(span, placement, &shaped, ink_box); + } + } +} + +/// Every horizontal emphasis mark with the box it paints in, in the +/// laid-out paragraph. Text bounds join these boxes. +pub(crate) fn horizontal_emphasis_mark_boxes( + paragraph: &Paragraph, + offsets: &HorizontalOffsets, + laid_out: &LayoutParagraph, +) -> Vec<(HorizontalEmphasisPlacement, skia::Rect)> { + let mut boxes = Vec::new(); + for_each_horizontal_emphasis_mark(paragraph, offsets, laid_out, |_, placement, _, ink_box| { + boxes.push((placement, ink_box)); + }); + boxes +} + +/// Paint horizontal emphasis marks on their configured side of the base. +/// Draw-only: the base paragraph keeps its own metrics. +pub(crate) fn paint_horizontal_emphasis( + canvas: &skia::Canvas, + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, + laid_out: &LayoutParagraph, + x: f32, + y: f32, +) { + for_each_horizontal_emphasis_mark( + paragraph, + &plan.offsets, + laid_out, + |_, _, shaped, ink_box| { + let origin = ( + x + ink_box.left - shaped.ink.left, + y + ink_box.top - shaped.ink.top, + ); + shaped.paragraph.paint(canvas, origin); + }, + ); +} diff --git a/render-wasm/src/shapes/text_horizontal/justify.rs b/render-wasm/src/shapes/text_horizontal/justify.rs new file mode 100644 index 0000000000..2d214da6db --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/justify.rs @@ -0,0 +1,720 @@ +// Line adjustment of horizontal CJK text. SkParagraph only expands around +// ideographs, which bunches the space next to kanji. Japanese justification +// (JLREQ §3.8) widens each line's gaps in the pair-table order vertical +// columns use, and Japanese squeezing (oikomi) reduces punctuation aki to +// keep the next character. The other policies justify over their pairwise +// expansion opportunities with bounded amounts. + +use crate::shapes::jlreq::{classify, kinsoku_blocks, MAX_KINSOKU_SHIFT}; +use crate::shapes::text_allocation::{ + allocate_bounded_expansion, bounded_expansion, capped_equal_allocations, jlreq_expansion_gap, + jlreq_ordered_expansion, +}; +use crate::shapes::text_paragraph::LayoutParagraph; +use crate::shapes::LineAdjustment; + +use super::*; + +/// Slack kept back from each justified line so float noise never wraps it. +const JUSTIFY_EPSILON: f32 = 0.05; + +/// How far a character may pass the line end and still count as fitting. +const FIT_TOLERANCE: f32 = 0.01; + +/// True for a character that belongs to a Western word: no space opens +/// between two of them. +fn is_word_char(ch: char) -> bool { + !ch.is_whitespace() && !is_cjk_text_char(ch) && !line_breaks::is_inserted_zero_width(ch) +} + +/// One real character of a horizontal Japanese paragraph for justification. +struct JustifyChar { + /// UTF-16 offset in the builder text. + utf16: usize, + span: usize, + /// Char index in the span's layout text. + index: usize, + /// Pair-table class: ruby bases count as simple ruby, as vertically. + class: JlreqClass, + font_size: f32, + /// A warichu note sits between it and the previous character. + after_note: bool, +} + +/// The characters justification spaces, in builder order. Characters the +/// layout pass inserted (word joiners, break opportunities and the spaces +/// that set the preferred inter-script and question-mark gaps) belong to +/// the gap between their neighbours, not to a gap of their own. +fn justify_chars(paragraph: &Paragraph, plan: &HorizontalParagraphPlan) -> Vec { + let spans = paragraph.children(); + let offset_map = &plan.offsets.offset_map; + let mut chars = Vec::new(); + let mut after_note = false; + for range in &plan.offsets.ranges { + if range.warichu { + after_note = true; + continue; + } + let (Some(span), Some(text)) = (spans.get(range.span), plan.texts.get(range.span)) else { + continue; + }; + let mut utf16 = range.builder_start; + for (index, ch) in text.chars().enumerate() { + let shifted = range.shifted_start + (utf16 - range.builder_start); + if !line_breaks::is_inserted_zero_width(ch) && !offset_map.is_inserted(shifted) { + let class = if span.has_ruby() { + JlreqClass::SimpleRuby + } else if ch == line_breaks::WESTERN_WORD_SPACE { + JlreqClass::WesternWordSpace + } else { + classify(ch) + }; + chars.push(JustifyChar { + utf16, + span: range.span, + index, + class, + font_size: span.font_size, + after_note, + }); + after_note = false; + } + utf16 += ch.len_utf16(); + } + } + chars +} + +/// Per-span letter-spacing (char index in the layout text, amount) that +/// justifies `laid_out`, the paragraph of `plan` laid out unjustified at +/// `width`. Every line but the last takes the JLREQ ordered expansion that +/// vertical columns use (`jlreq_ordered_expansion`): gaps widen in pair-table +/// priority, Western word spaces, then Japanese/Western gaps, then solid +/// kana and kanji, and any slack left spreads over all of them. Gaps inside +/// a Western word, beside a ruby base, around punctuation aki or across a +/// warichu note take none; neither do inserted joiners. Each amount lands +/// after the character before its gap. +pub(crate) fn horizontal_jlreq_justify_spacing( + paragraph: &Paragraph, + laid_out: &LayoutParagraph, + plan: &HorizontalParagraphPlan, + width: f32, +) -> Vec> { + let mut spacing: Vec> = vec![Vec::new(); plan.texts.len()]; + let chars = justify_chars(paragraph, plan); + let lines = laid_out.get_line_metrics(); + let Some((_, justified)) = lines.split_last() else { + return spacing; + }; + for line in justified { + let slack = width - line.width as f32 - JUSTIFY_EPSILON; + if slack <= 0.0 { + continue; + } + let start = chars.partition_point(|c| c.utf16 < line.start_index); + let end = chars.partition_point(|c| c.utf16 < line.end_excluding_whitespaces); + // (char before the gap, priority, cap) + let gaps: Vec<(usize, u8, f32)> = (start..end.saturating_sub(1)) + .filter(|at| !chars[at + 1].after_note) + .filter_map(|at| { + let (before, after) = (&chars[at], &chars[at + 1]); + let em = before.font_size.min(after.font_size); + let spacing_em = pair_rule(before.class, after.class).preferred_em; + jlreq_expansion_gap(before.class, after.class, spacing_em, em) + .map(|(priority, cap)| (at, priority, cap)) + }) + .collect(); + let mut amounts: Vec<(usize, f32)> = Vec::new(); + for (at, amount) in jlreq_ordered_expansion(&gaps, slack) { + match amounts.iter_mut().find(|(index, _)| *index == at) { + Some((_, total)) => *total += amount, + None => amounts.push((at, amount)), + } + } + for (at, amount) in amounts { + if amount > 0.0 { + spacing[chars[at].span].push((chars[at].index, amount)); + } + } + } + spacing +} + +/// Pairwise, bounded justification; explicit distribution fills every +/// line without a per-gap cap. Annotation and grapheme interiors stay solid. +pub(crate) fn horizontal_pairwise_justify_spacing( + paragraph: &Paragraph, + laid_out: &LayoutParagraph, + plan: &HorizontalParagraphPlan, + width: f32, +) -> Vec> { + use unicode_segmentation::UnicodeSegmentation; + let distribute = paragraph.text_align() == crate::shapes::TextAlign::Distribute; + let policy = paragraph.layout_policy(); + let chars = line_chars(paragraph, plan); + let source: String = chars.iter().map(|c| c.ch).collect(); + let original: Vec<_> = paragraph + .children() + .iter() + .flat_map(|span| span.text.chars()) + .collect(); + let starts: Vec<_> = source + .graphemes(true) + .scan(0usize, |at, g| { + let start = *at; + *at += g.chars().count(); + Some(start) + }) + .collect(); + let mut result = vec![Vec::new(); plan.texts.len()]; + // Ruby base of each span: distribution never opens a base. + let group_of = paragraph.ruby_group_of_spans(); + let notes: Vec<_> = plan + .offsets + .ranges + .iter() + .filter(|range| range.warichu) + .collect(); + let lines = laid_out.get_line_metrics(); + let count = if distribute { + lines.len() + } else { + lines.len().saturating_sub(1) + }; + for line in lines.iter().take(count) { + let remaining = (width - line.width as f32 - JUSTIFY_EPSILON).max(0.0); + let mut opportunities = Vec::new(); + let start = chars.partition_point(|c| c.utf16 < line.start_index); + let end = chars.partition_point(|c| c.utf16 < line.end_excluding_whitespaces); + for index in start..end.saturating_sub(1) { + let (before, after) = (&chars[index], &chars[index + 1]); + let source_before = plan.offsets.builder_to_source(before.utf16); + let source_after = plan.offsets.builder_to_source(after.utf16); + let nonbreaking = distribute + && original + .get(source_before..=source_after) + .is_some_and(|pair| { + pair.iter() + .any(|ch| matches!(ch, '\u{00a0}' | '\u{202f}' | '\u{2060}')) + }); + let note = notes.get(notes.partition_point(|r| r.builder_end <= before.utf16)); + if before.utf16 < line.start_index + || after.utf16 >= line.end_excluding_whitespaces + || (!distribute && after.joined) + || nonbreaking + || starts.binary_search(&(index + 1)).is_err() + || (distribute + && group_of[before.span].is_some() + && group_of[before.span] == group_of[after.span]) + || note.is_some_and(|r| { + r.builder_start > before.utf16 && r.builder_start < after.utf16 + }) + { + continue; + } + let em = before.font_size.min(after.font_size); + if let Some((priority, cap)) = + bounded_expansion(policy, before.ch, after.ch, em, distribute.then_some(width)) + { + opportunities.push((index, priority, cap)); + } + } + for (index, amount) in allocate_bounded_expansion(&opportunities, remaining) { + let ch = &chars[index]; + result[ch.span].push((ch.index, amount)); + } + } + result +} + +/// Lays a paragraph out with extra per-span sheds (char index, amount). +/// Lays out a paragraph with extra sheds, restricted to a builder UTF-16 +/// window (`FULL_WINDOW` for all of it). +pub(crate) type LayOutWithSheds<'a> = + dyn Fn(&[Vec<(usize, f32)>], std::ops::Range) -> LayoutParagraph + 'a; + +/// Builder UTF-16 units first laid out from a line start: a few lines of +/// text. The window doubles until it holds the lines a decision needs. +const SQUEEZE_WINDOW: usize = 512; + +/// One real character of a horizontal paragraph in builder order. +struct LineChar { + /// UTF-16 offset in the builder text. + utf16: usize, + span: usize, + /// Char index in the span's layout text. + index: usize, + ch: char, + class: Option, + font_size: f32, + /// A WORD JOINER before it forbids a break. + joined: bool, +} + +fn line_chars(paragraph: &Paragraph, plan: &HorizontalParagraphPlan) -> Vec { + let spans = paragraph.children(); + let mut chars = Vec::new(); + for range in &plan.offsets.ranges { + let (Some(span), Some(text)) = (spans.get(range.span), plan.texts.get(range.span)) else { + continue; + }; + if range.warichu { + continue; + } + let proportional = span.font_features == FontFeatures::Palt; + let mut utf16 = range.builder_start; + let mut joined = false; + for (index, ch) in text.chars().enumerate() { + if ch == line_breaks::WORD_JOINER { + joined = true; + } else if !line_breaks::is_inserted_zero_width(ch) { + chars.push(LineChar { + utf16, + span: range.span, + index, + ch, + class: aki_class(ch, span.has_ruby(), proportional), + font_size: span.font_size, + joined, + }); + joined = false; + } + utf16 += ch.len_utf16(); + } + } + chars +} + +/// Chars `[start, end)` that start a line at `start` and move as one: a +/// word joiner or a Western word keeps the next character with them. +fn line_start_unit(chars: &[LineChar], start: usize) -> std::ops::Range { + let mut end = start + 1; + while end < chars.len() + && (chars[end].joined || (is_word_char(chars[end - 1].ch) && is_word_char(chars[end].ch))) + { + end += 1; + } + start..end +} + +/// Squeezable spacing (char, priority, px) after each char of `range`: what +/// the JLREQ pair table lets oikomi take between it and the next char. +fn squeeze_capacities(chars: &[LineChar], range: std::ops::Range) -> Vec<(usize, u8, f32)> { + range + .filter(|at| at + 1 < chars.len()) + .filter_map(|at| { + let (before, after) = (&chars[at], &chars[at + 1]); + let rule = pair_rule(before.class?, after.class?); + let em = before.font_size.min(after.font_size); + let capacity = (rule.preferred_em - rule.minimum_em) * em; + (rule.shrink_priority > 0 && capacity > 0.0).then_some(( + at, + rule.shrink_priority, + capacity, + )) + }) + .collect() +} + +/// The trailing aki a closing bracket or comma sheds at a line end. +fn line_end_aki(ch: &LineChar) -> f32 { + match ch.class { + Some(JlreqClass::ClosingBracket | JlreqClass::Comma) => ch.font_size / 2.0, + _ => 0.0, + } +} + +/// True when some line of the paragraph could squeeze under +/// `LineAdjustment::PushOutFirst`: only a run of more kinsoku pairs than +/// oidashi moves over (`MAX_KINSOKU_SHIFT`) leaves push-out unresolved. +pub(crate) fn horizontal_push_out_may_fail( + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, +) -> bool { + let chars = line_chars(paragraph, plan); + let mut run = 0usize; + chars.windows(2).any(|pair| { + run = if kinsoku_blocks(pair[0].ch, pair[1].ch) { + run + 1 + } else { + 0 + }; + run > MAX_KINSOKU_SHIFT + }) +} + +/// Whether SkParagraph's push-out of `unit`, the characters that start the +/// next line, is one vertical oidashi accepts (`push_out_resolves`). Without +/// kinsoku the line would break before the first unit character that does +/// not fit `room`, the space the line leaves; moving that break back to the +/// unit start must cross at most `MAX_KINSOKU_SHIFT` kinsoku pairs. Lines +/// never start inside the unit, since joiners keep it whole, and a unit +/// wider than the box overflows on its own line, so the shift limit is the +/// only way push-out fails here. `widths` holds each unit character's width. +fn push_out_resolves( + chars: &[LineChar], + unit: std::ops::Range, + widths: &[f32], + room: f32, +) -> bool { + let mut used = 0.0; + let mut natural = unit.end; + for (at, width) in unit.clone().zip(widths) { + if used + width - line_end_aki(&chars[at]) > room + FIT_TOLERANCE { + natural = at; + break; + } + used += width; + } + let shifts = (unit.start + 1..=natural.min(unit.end - 1)) + .filter(|at| kinsoku_blocks(chars[at - 1].ch, chars[*at].ch)) + .count(); + natural == unit.start || shifts <= MAX_KINSOKU_SHIFT +} + +/// Per-span sheds (char index, amount) that squeeze lines of a horizontal +/// Japanese paragraph to keep the character that would start the next line +/// (JLREQ §3.8 oikomi). Each line, in order, takes the unit starting the next +/// line when the pair table's squeezable aki on the line covers the +/// overflow, reduced in priority order and equally within a priority. A +/// trailing closing bracket or comma pulled to the line end sheds its aki +/// there. Under `PushInFirst` every line may squeeze; under `PushOutFirst` +/// only a line whose push-out does not resolve kinsoku (`push_out_resolves`), +/// as vertical columns do; `PushOutOnly` never squeezes. `lay_out` lays the +/// paragraph out with extra sheds; returns the sheds and the final layout. +pub(crate) fn horizontal_squeeze_sheds( + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, + width: f32, + adjustment: LineAdjustment, + lay_out: &LayOutWithSheds, +) -> (Vec>, LayoutParagraph) { + if adjustment == LineAdjustment::PushOutOnly { + let sheds = vec![Vec::new(); plan.texts.len()]; + let laid_out = lay_out(&sheds, FULL_WINDOW); + return (sheds, laid_out); + } + let chars = line_chars(paragraph, plan); + let mut sheds: Vec> = vec![Vec::new(); plan.texts.len()]; + let total = plan + .offsets + .ranges + .iter() + .map(|range| range.builder_end) + .max() + .unwrap_or(0); + // Lines are broken greedily, so a line and the decision about it depend + // only on the text from its start. Lay out that text alone, enough of it + // for the line, the next one and what decides the next break: relaying + // out the whole paragraph per line made long paragraphs quadratic. + let lay_out_from = |sheds: &[Vec<(usize, f32)>], start: usize| { + let mut size = SQUEEZE_WINDOW; + loop { + let end = start.saturating_add(size).min(total); + let laid_out = lay_out(sheds, start..end); + if end >= total || laid_out.get_line_metrics().len() >= 3 { + return laid_out; + } + size *= 2; + } + }; + // `window` is laid out from `window_start` with the current sheds; its + // lines stay valid until a squeeze changes the sheds, so lines without a + // candidate move on inside it instead of laying out again. + let mut window_start = 0usize; + let mut window = lay_out_from(&sheds, window_start); + let mut line_index = 0usize; + for _ in 0..chars.len() + 1 { + let window_lines = window.get_line_metrics(); + // A line is decided by the next one and what follows it: past that, + // lay out again from the line. + let reached_end = window_lines + .last() + .is_some_and(|last| window_start + last.end_index >= total); + if line_index > 0 && line_index + 2 >= window_lines.len() && !reached_end { + window_start += window_lines.get(line_index).map_or_else( + || window_lines.last().map_or(0, |last| last.end_index), + |line| line.start_index, + ); + window = lay_out_from(&sheds, window_start); + line_index = 0; + continue; + } + let start = window_start + + window_lines + .get(line_index) + .map_or(0, |line| line.start_index); + let laid_out = &window; + let lines: Vec<_> = window_lines.iter().skip(line_index).collect(); + if lines.len() < 2 { + break; + } + let (line, next) = (lines[0], lines[1]); + let next_start = window_start + next.start_index; + let Some(unit_start) = chars.iter().position(|c| c.utf16 >= next_start) else { + break; + }; + let unit = line_start_unit(&chars, unit_start); + let unit_end_utf16 = chars + .get(unit.end) + .map_or(window_start + next.end_index, |c| c.utf16); + let unit_width = laid_out + .get_rects_for_range( + chars[unit.start].utf16 - window_start..unit_end_utf16 - window_start, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ) + .iter() + .map(|rect| rect.rect.width()) + .sum::(); + let last = &chars[unit.end - 1]; + let trailing_aki = line_end_aki(last); + let needed = line.width as f32 + unit_width - trailing_aki - width + JUSTIFY_EPSILON; + if needed > 0.0 && adjustment == LineAdjustment::PushOutFirst { + let widths: Vec = unit + .clone() + .map(|at| { + let end = chars.get(at + 1).map_or(unit_end_utf16, |c| c.utf16); + laid_out + .get_rects_for_range( + chars[at].utf16 - window_start..end - window_start, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ) + .iter() + .map(|rect| rect.rect.width()) + .sum::() + }) + .collect(); + if push_out_resolves(&chars, unit.clone(), &widths, width - line.width as f32) { + line_index += 1; + continue; + } + } + let line_chars_end = unit.start; + let line_chars_start = chars + .iter() + .position(|c| c.utf16 >= start) + .unwrap_or(line_chars_end); + let capacities = squeeze_capacities(&chars, line_chars_start..line_chars_end); + let capacity: f32 = capacities.iter().map(|(_, _, cap)| cap).sum(); + if needed <= 0.0 || capacity < needed { + line_index += 1; + continue; + } + let mut candidate = sheds.clone(); + let mut remaining = needed; + for priority in 1..=5 { + let caps: Vec<(usize, f32)> = capacities + .iter() + .filter(|(_, p, _)| *p == priority) + .map(|(at, _, cap)| (*at, *cap)) + .collect(); + for (at, amount) in capped_equal_allocations(&caps, remaining) { + candidate[chars[at].span].push((chars[at].index, amount)); + remaining -= amount; + } + if remaining <= 0.0 { + break; + } + } + if trailing_aki > 0.0 { + candidate[last.span].push((last.index, trailing_aki)); + } + // The check needs this line only: lay out to the end of the pulled + // unit plus a line's worth of what follows. + let check_end = unit_end_utf16 + .saturating_add(unit_end_utf16 - start) + .min(total); + let squeezed = lay_out(&candidate, start..check_end); + let squeezed_lines = squeezed.get_line_metrics(); + let kept = squeezed_lines + .first() + .is_some_and(|squeezed_line| start + squeezed_line.end_index >= unit_end_utf16); + if kept { + sheds = candidate; + if check_end >= total && squeezed_lines.len() < 2 { + break; + } + window_start = start; + window = squeezed; + line_index = 1; + } else { + line_index += 1; + } + } + let laid_out = lay_out(&sheds, FULL_WINDOW); + (sheds, laid_out) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shapes::{TextAlign, TextDirection}; + use skia::textlayout::{FontCollection, ParagraphBuilder, ParagraphStyle, TextStyle}; + use std::cell::Cell; + + type LayOutFull<'a> = dyn Fn(&[Vec<(usize, f32)>]) -> LayoutParagraph + 'a; + + /// The squeeze pass as it was before windowing: one layout of the whole + /// paragraph per line. + fn full_relayout_sheds( + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, + width: f32, + lay_out: &LayOutFull, + ) -> Vec> { + let chars = line_chars(paragraph, plan); + let mut sheds: Vec> = vec![Vec::new(); plan.texts.len()]; + let mut laid_out = lay_out(&sheds); + let mut line_index = 0usize; + for _ in 0..chars.len() + 1 { + let lines = laid_out.get_line_metrics(); + if line_index + 1 >= lines.len() { + break; + } + let (line, next) = (&lines[line_index], &lines[line_index + 1]); + let Some(unit_start) = chars.iter().position(|c| c.utf16 >= next.start_index) else { + break; + }; + let unit = line_start_unit(&chars, unit_start); + let unit_end_utf16 = chars.get(unit.end).map_or(next.end_index, |c| c.utf16); + let unit_width = laid_out + .get_rects_for_range( + chars[unit.start].utf16..unit_end_utf16, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ) + .iter() + .map(|rect| rect.rect.width()) + .sum::(); + let last = &chars[unit.end - 1]; + let trailing_aki = match last.class { + Some(JlreqClass::ClosingBracket | JlreqClass::Comma) => last.font_size / 2.0, + _ => 0.0, + }; + let needed = line.width as f32 + unit_width - trailing_aki - width + JUSTIFY_EPSILON; + let line_chars_end = unit.start; + let line_chars_start = chars + .iter() + .position(|c| c.utf16 >= line.start_index) + .unwrap_or(line_chars_end); + let capacities = squeeze_capacities(&chars, line_chars_start..line_chars_end); + let total: f32 = capacities.iter().map(|(_, _, cap)| cap).sum(); + if needed <= 0.0 || total < needed { + line_index += 1; + continue; + } + let mut candidate = sheds.clone(); + let mut remaining = needed; + for priority in 1..=5 { + let caps: Vec<(usize, f32)> = capacities + .iter() + .filter(|(_, p, _)| *p == priority) + .map(|(at, _, cap)| (*at, *cap)) + .collect(); + for (at, amount) in capped_equal_allocations(&caps, remaining) { + candidate[chars[at].span].push((chars[at].index, amount)); + remaining -= amount; + } + if remaining <= 0.0 { + break; + } + } + if trailing_aki > 0.0 { + candidate[last.span].push((last.index, trailing_aki)); + } + let squeezed = lay_out(&candidate); + if squeezed + .get_line_metrics() + .get(line_index) + .is_some_and(|squeezed_line| squeezed_line.end_index >= unit_end_utf16) + { + sheds = candidate; + laid_out = squeezed; + } + line_index += 1; + } + sheds + } + + #[test] + fn windowed_squeeze_matches_full_relayout_and_lays_out_the_paragraph_once() { + crate::globals::design_init(); + let text = "あい、うえ、おかきくけこさしすせそ「かぎ」、と。".repeat(40); + let span = TextSpan { + text, + font_size: 16.0, + line_height: 1.0, + ..TextSpan::default() + }; + let mut paragraph = Paragraph::new( + TextAlign::default(), + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + vec![span], + ); + paragraph.set_language("ja".to_string()); + let plan = HorizontalParagraphPlan::new(¶graph); + let mut style = TextStyle::default(); + style.set_font_size(16.0); + let mut fonts = FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut paragraph_style = ParagraphStyle::default(); + paragraph_style.set_apply_rounding_hack(false); + let width = 16.0 * 7.6; + let full_layouts = Cell::new(0usize); + let lay_out = |extra: &[Vec<(usize, f32)>], window: std::ops::Range| { + if window == FULL_WINDOW { + full_layouts.set(full_layouts.get() + 1); + } + let sheds: Vec<(usize, f32)> = plan.pair_spacing[0] + .iter() + .chain(extra.first().into_iter().flatten()) + .copied() + .collect(); + let (text, from) = + window_slice(&plan.texts[0], &plan.offsets.ranges[0], &window).unwrap_or(("", 0)); + let sheds = window_entries(&sheds, from, text.chars().count()); + let mut builder = ParagraphBuilder::new(¶graph_style, &fonts); + builder.push_style(&style); + add_horizontal_span( + &mut builder, + ¶graph.children()[0], + text, + &sheds, + &[], + &style, + &fonts, + ); + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(width); + laid_out + }; + let (windowed, _) = horizontal_squeeze_sheds( + ¶graph, + &plan, + width, + LineAdjustment::PushInFirst, + &lay_out, + ); + assert_eq!( + full_layouts.get(), + 1, + "the whole paragraph is laid out once" + ); + let reference = full_relayout_sheds(¶graph, &plan, width, &|extra| { + lay_out(extra, FULL_WINDOW) + }); + assert!(!reference[0].is_empty(), "the text squeezes some lines"); + assert_eq!(windowed.len(), reference.len()); + for (got, want) in windowed[0].iter().zip(&reference[0]) { + assert_eq!(got.0, want.0); + assert!((got.1 - want.1).abs() < 0.01, "{got:?} vs {want:?}"); + } + assert_eq!(windowed[0].len(), reference[0].len()); + } +} diff --git a/render-wasm/src/shapes/text_horizontal/mod.rs b/render-wasm/src/shapes/text_horizontal/mod.rs new file mode 100644 index 0000000000..2144fba601 --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/mod.rs @@ -0,0 +1,451 @@ +// Horizontal CJK layout on top of SkParagraph: the policy's layout texts +// (break joiners and spacing characters), punctuation and inter-script +// spacing, offset maps between source, layout and builder text, line +// adjustment, and the ruby, emphasis and warichu painters. + +use crate::shapes::text_paragraph::{LayoutParagraph, SpacedParagraphBuilder}; + +mod emphasis; +mod justify; +mod offsets; +mod overflow; +mod ruby; +mod warichu; + +#[cfg(test)] +mod tests; + +pub(crate) use emphasis::*; +pub(crate) use justify::*; +pub(crate) use offsets::*; +pub(crate) use ruby::*; +pub(crate) use warichu::*; + +use super::text::{add_text_with_tabs, Paragraph, TextContent, TextSpan}; +use super::text_attrs::*; +use super::text_ruby::*; +use super::text_shaping::{ + is_upright_char, shape_segment_with_fallbacks, single_glyph_blob, span_font_families, ShapedRun, +}; +use super::text_warichu::*; +use crate::globals::get_resources; +use crate::math::Point; +use crate::shapes::jlreq::{ + aki_class, is_cjk_text_char, pair_rule, punctuation_aki_sheds, AkiShed, JlreqClass, +}; +use crate::shapes::{line_breaks, merge_fills}; +use crate::utils::{get_fallback_fonts, get_font_collection}; +use skia_safe::{ + self as skia, + textlayout::{ + ParagraphBuilder, ParagraphStyle, PlaceholderAlignment, PlaceholderStyle, RectHeightStyle, + RectWidthStyle, TextBaseline, + }, + Canvas, Contains, Font, FontMgr, GlyphId, +}; + +/// True when a paragraph with layout texts `texts` needs the Japanese passes: +/// its policy is Japanese and it has ruby or a CJK text character. +pub(crate) fn uses_japanese_layout(paragraph: &Paragraph, texts: &[String]) -> bool { + paragraph.layout_policy().uses_jlreq() + && (paragraph.children().iter().any(TextSpan::has_ruby) + || texts.iter().any(|text| text.chars().any(is_cjk_text_char))) +} + +#[cfg(test)] +pub(crate) fn layout_span_texts(paragraph: &Paragraph) -> (Vec, line_breaks::OffsetMap) { + layout_span_texts_with_breaks(paragraph, &[], &[]) +} + +fn layout_span_texts_with_breaks( + paragraph: &Paragraph, + emergency: &[usize], + forced: &[usize], +) -> (Vec, line_breaks::OffsetMap) { + let texts: Vec = paragraph + .children() + .iter() + .map(TextSpan::apply_text_transform) + .collect(); + // Text without Japanese characters or ruby keeps plain Skia layout. + let policy = paragraph.layout_policy(); + if policy.plans_breaks() || uses_japanese_layout(paragraph, &texts) { + let ruby_breaks: Vec>> = paragraph + .children() + .iter() + .map(|span| span.has_ruby().then(Vec::new)) + .collect(); + // A base that crosses spans stays whole across their boundaries. + let group_of = paragraph.ruby_group_of_spans(); + let joined: Vec = group_of + .iter() + .enumerate() + .map(|(index, group)| index > 0 && group.is_some() && *group == group_of[index - 1]) + .collect(); + let shifted = line_breaks::apply_policy_with_breaks( + &texts, + &ruby_breaks, + &joined, + policy, + emergency, + forced, + ); + if let Some((shifted, map)) = shifted { + return (shifted, map); + } + } + (texts, line_breaks::OffsetMap::default()) +} + +/// Advance changes between adjacent characters of the layout `texts`, as +/// (char index, amount) per span: positive narrows the character, negative +/// widens it. Japanese sheds JLREQ punctuation aki; the other policies use +/// their pairwise punctuation compression and script spacing. +pub(crate) fn horizontal_pair_spacing( + paragraph: &Paragraph, + texts: &[String], +) -> Vec> { + if !paragraph.layout_policy().uses_jlreq() { + return horizontal_pairwise_spacing(paragraph, texts); + } + jlreq_aki_sheds(paragraph, texts) +} + +/// (char index, amount) per span of the layout `texts` for the characters +/// whose advance loses a half-em to `punctuation_aki_sheds`: a closing mark +/// sheds half of its own em, and the character before an opening bracket +/// gives up half of the bracket's em. Inserted word joiners are transparent; +/// `palt` spans set punctuation proportionally. +fn jlreq_aki_sheds(paragraph: &Paragraph, texts: &[String]) -> Vec> { + let spans = paragraph.children(); + let half_em = |span: usize| spans.get(span).map_or(0.0, |span| span.font_size * 0.5); + // (span, char index in the span text) of every real character. + let mut chars: Vec<(usize, usize)> = Vec::new(); + let mut classes: Vec> = Vec::new(); + for (span_index, text) in texts.iter().enumerate() { + let span = spans.get(span_index); + let ruby_base = span.is_some_and(TextSpan::has_ruby); + let proportional = span.is_some_and(|span| span.font_features == FontFeatures::Palt); + for (index, ch) in text.chars().enumerate() { + if line_breaks::is_inserted_zero_width(ch) { + continue; + } + chars.push((span_index, index)); + classes.push(aki_class(ch, ruby_base, proportional)); + } + } + let mut sheds: Vec> = vec![Vec::new(); texts.len()]; + for (at, shed) in punctuation_aki_sheds(&classes) { + let (span, amount_span, index) = match shed { + AkiShed::Trailing => (chars[at].0, chars[at].0, chars[at].1), + AkiShed::Leading => (chars[at - 1].0, chars[at].0, chars[at - 1].1), + }; + sheds[span].push((index, half_em(amount_span))); + } + sheds +} + +/// Signed advance changes: positive removes punctuation space, negative adds +/// the regional Han/Latin gap. Source and builder text stay unchanged. +fn horizontal_pairwise_spacing(paragraph: &Paragraph, texts: &[String]) -> Vec> { + let mut result = vec![Vec::new(); texts.len()]; + let policy = paragraph.layout_policy(); + let mut previous: Option<(usize, usize, char)> = None; + for (span, text) in texts.iter().enumerate() { + for (index, ch) in text.chars().enumerate() { + if line_breaks::is_inserted_zero_width(ch) { + continue; + } + if let Some((before_span, before_index, before)) = previous { + let a = ¶graph.children()[before_span]; + let b = ¶graph.children()[span]; + if !a.has_ruby() && !b.has_ruby() && !a.is_warichu() && !b.is_warichu() { + let em = a.font_size.min(b.font_size); + let spacing = policy.pair_spacing_em(before, ch, false); + // `palt` already sets punctuation proportionally. + let compression = if a.font_features == FontFeatures::Palt + || b.font_features == FontFeatures::Palt + { + 0.0 + } else { + spacing.compression + }; + let amount = (compression - spacing.script_gap) * em; + if amount != 0.0 { + result[before_span].push((before_index, amount)); + } + } + } + previous = Some((span, index, ch)); + } + } + result +} + +/// Add a span to a horizontal paragraph builder. A warichu span becomes one +/// inline placeholder so its two lines wrap as a unit; its glyphs are painted +/// after layout. The characters at `sheds` (from `horizontal_pair_spacing`) +/// set narrower by their amounts; `extra` (from `horizontal_ruby_spacing`) +/// adds letter-spacing to characters. +pub(crate) fn add_horizontal_span( + builder: &mut impl SpacedParagraphBuilder, + span: &TextSpan, + builder_text: &str, + sheds: &[(usize, f32)], + extra: &[(usize, f32)], + text_style: &skia::textlayout::TextStyle, + fonts: &skia::textlayout::FontCollection, +) { + if span.is_warichu() { + let builder = builder.paragraph_builder(); + if builder_text.starts_with(line_breaks::FORCED_LINE_BREAK) { + builder.add_text(line_breaks::FORCED_LINE_BREAK.to_string()); + } + let text = span.apply_text_transform(); + let (first, second) = warichu_text_lines(&text); + let mut mini_style = text_style.clone(); + mini_style.set_font_size(span.font_size * WARICHU_FONT_SCALE); + mini_style.set_height(1.0); + mini_style.set_height_override(true); + mini_style.set_letter_spacing(span.letter_spacing * WARICHU_FONT_SCALE); + let measure = |line: &str| mini_paragraph(line, &mini_style, fonts).longest_line(); + let width = measure(first).max(measure(second)).max(0.01); + let height = span.font_size.max(0.01); + builder.add_placeholder(&PlaceholderStyle::new( + width, + height, + PlaceholderAlignment::Middle, + TextBaseline::Alphabetic, + height, + )); + // SkParagraph does not expose a placeholder's TextStyle through line + // metrics. A near-zero, inkless NBSP carries the span style for the + // paint pass; the zero-width space after it allows a line break after + // the atomic placeholder. + let mut anchor_style = text_style.clone(); + anchor_style.set_font_size(0.01); + anchor_style.set_height(0.01); + anchor_style.set_height_override(true); + anchor_style.set_letter_spacing(0.0); + builder.push_style(&anchor_style); + builder.add_text(HORIZONTAL_WARICHU_STYLE_ANCHOR.to_string()); + builder.add_text(HORIZONTAL_WARICHU_BREAK_ANCHOR.to_string()); + } else { + add_text_with_sheds(builder, span, builder_text, sheds, extra, text_style); + } +} + +/// Builder UTF-16 window that covers a whole paragraph. +pub(crate) const FULL_WINDOW: std::ops::Range = 0..usize::MAX; + +/// Part of a span's builder `text` inside the builder UTF-16 `window`, with +/// the char index of its first character, or None when the span lies +/// outside the window. A warichu box is atomic: kept whole when it starts +/// inside the window. +pub(crate) fn window_slice<'a>( + text: &'a str, + range: &HorizontalSpanRange, + window: &std::ops::Range, +) -> Option<(&'a str, usize)> { + if range.builder_end <= window.start || range.builder_start >= window.end { + return None; + } + if range.warichu { + return (range.builder_start >= window.start).then_some((text, 0)); + } + let mut utf16 = range.builder_start; + let mut from = None; + let mut to = text.len(); + for (index, (byte, ch)) in text.char_indices().enumerate() { + if from.is_none() && utf16 >= window.start { + from = Some((byte, index)); + } + if utf16 >= window.end { + to = byte; + break; + } + utf16 += ch.len_utf16(); + } + let (from_byte, from_char) = from?; + Some((&text[from_byte..to.max(from_byte)], from_char)) +} + +/// Per-char `entries` of a span shifted to a slice starting at char `from` +/// and `len` chars long. +pub(crate) fn window_entries( + entries: &[(usize, f32)], + from: usize, + len: usize, +) -> Vec<(usize, f32)> { + entries + .iter() + .filter(|(index, _)| *index >= from && *index < from + len) + .map(|(index, amount)| (index - from, *amount)) + .collect() +} + +/// Add `text`, adjusting the letter-spacing of single characters so the +/// builder text stays unchanged: those at `sheds` lose their amount and +/// those in `extra` take its added spacing. Skia tracks every cluster, +/// including the zero-width word joiners the kinsoku pass inserts, so those +/// take no letter-spacing. The builder records how far each adjusted +/// character departs from the span tracking. +fn add_text_with_sheds( + builder: &mut impl SpacedParagraphBuilder, + span: &TextSpan, + text: &str, + sheds: &[(usize, f32)], + extra: &[(usize, f32)], + text_style: &skia::textlayout::TextStyle, +) { + let tracking = text_style.letter_spacing(); + let count = text.chars().count(); + let mut amounts = vec![(0.0, 0.0); count]; + for &(index, amount) in extra { + if let Some((added, _)) = amounts.get_mut(index) { + *added += amount; + } + } + for &(index, amount) in sheds { + if let Some((_, shed)) = amounts.get_mut(index) { + *shed += amount; + } + } + let mut piece_start = 0; + // UTF-16 offset of the span text in the builder, read at the first piece. + let mut builder_start = None; + let mut utf16 = 0; + for (index, (byte, ch)) in text.char_indices().enumerate() { + let at = utf16; + utf16 += ch.len_utf16(); + let (added, shed) = amounts[index]; + let letter_spacing = if line_breaks::is_inserted_zero_width(ch) { + 0.0 + } else { + tracking + added - shed + }; + if letter_spacing == tracking { + continue; + } + let start = *builder_start.get_or_insert_with(|| { + builder + .paragraph_builder() + .get_text() + .encode_utf16() + .count() + }); + builder.add_extra_spacing(start + at, letter_spacing - tracking); + let mut style = text_style.clone(); + style.set_letter_spacing(letter_spacing); + let end = byte + ch.len_utf8(); + let paragraph_builder = builder.paragraph_builder(); + add_text_with_tabs(paragraph_builder, &text[piece_start..byte], span.font_size); + paragraph_builder.push_style(&style); + paragraph_builder.add_text(&text[byte..end]); + paragraph_builder.pop(); + piece_start = end; + } + add_text_with_tabs( + builder.paragraph_builder(), + &text[piece_start..], + span.font_size, + ); +} + +/// The horizontal passes of one paragraph that layout, painting, position +/// data and the editor share: layout texts, offsets, pair spacing +/// (punctuation aki and script gaps) and long-ruby spacing. `TextContent::horizontal_plans` caches them. +pub(crate) struct HorizontalParagraphPlan { + pub(crate) texts: Vec, + pub(crate) offsets: HorizontalOffsets, + pub(crate) pair_spacing: Vec>, + pub(crate) ruby_spacing: HorizontalRubySpacing, + emergency_breaks: Vec, + overflow_width: Option, +} + +impl HorizontalParagraphPlan { + pub(crate) fn new(paragraph: &Paragraph) -> Self { + Self::with_emergency_breaks(paragraph, &[]) + } + + pub(super) fn with_emergency_breaks(paragraph: &Paragraph, emergency: &[usize]) -> Self { + Self::with_breaks(paragraph, emergency, &[]) + } + + pub(super) fn with_breaks( + paragraph: &Paragraph, + emergency: &[usize], + forced: &[usize], + ) -> Self { + let (texts, offset_map) = layout_span_texts_with_breaks(paragraph, emergency, forced); + let pair_spacing = horizontal_pair_spacing(paragraph, &texts); + let ruby_spacing = horizontal_ruby_spacing(paragraph, &texts, &offset_map); + let offsets = HorizontalOffsets::from_layout_texts(paragraph, &texts, offset_map); + Self { + texts, + offsets, + pair_spacing, + ruby_spacing, + emergency_breaks: emergency.to_vec(), + overflow_width: None, + } + } +} + +/// Single-line paragraph for an annotation run. Laid out unbounded: a +/// re-layout at its own `longest_line()` can wrap the last glyph. +fn mini_paragraph( + text: &str, + style: &skia::textlayout::TextStyle, + fonts: &skia::textlayout::FontCollection, +) -> LayoutParagraph { + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), fonts); + builder.push_style(style); + builder.add_text(text); + let mut paragraph = LayoutParagraph::build(&mut builder); + paragraph.layout(f32::MAX); + paragraph +} + +pub(crate) fn horizontal_span_style( + laid_out: &LayoutParagraph, + range: &HorizontalSpanRange, +) -> Option { + // Lines use UTF-16, while their style entries use UTF-8 run starts. + // Skia can merge neighboring spans into one run, so an exact lookup at + // the later span's anchor may be empty. Recover the preceding style on + // the line containing the base, without carrying a style across lines. + let lines = laid_out.get_line_metrics(); + let line = lines.iter().find(|line| { + line.start_index <= range.style_anchor_utf16 + && range.style_anchor_utf16 < line.end_including_newline + })?; + line.get_style_metrics(0..range.style_anchor_start.saturating_add(1)) + .last() + .map(|(_, metric)| metric.text_style.clone()) +} + +/// Top of the em box of a horizontal base laid out in `rect`, whose builder +/// text starts at UTF-16 `base_start`. Annotations attach to this box: the +/// ideographic em box (OS/2 typo metrics) of the font that shaped the base, +/// centred on the line baseline. Skia's tight rect spans the hhea ascent and +/// descent instead, which in CJK faces reach past the em box. +pub(crate) fn horizontal_base_em_top( + laid_out: &LayoutParagraph, + lines: &[skia::textlayout::LineMetrics], + base_start: usize, + font_size: f32, + rect: skia::Rect, +) -> f32 { + let middle = rect.center_y(); + let Some(line) = lines.iter().find(|line| { + let baseline = line.baseline as f32; + middle >= baseline - line.ascent as f32 && middle <= baseline + line.descent as f32 + }) else { + return rect.top.max(rect.bottom - font_size); + }; + let font = laid_out.font_at_utf16(base_start.max(line.start_index)); + let (ascent, descent) = super::text_shaping::upright_centre_metrics(&font); + line.baseline as f32 + (ascent + descent - font_size) / 2.0 +} diff --git a/render-wasm/src/shapes/text_horizontal/offsets.rs b/render-wasm/src/shapes/text_horizontal/offsets.rs new file mode 100644 index 0000000000..29a5358a25 --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/offsets.rs @@ -0,0 +1,258 @@ +use super::*; + +#[derive(Debug, Clone)] +pub(crate) struct HorizontalSpanRange { + pub span: usize, + pub builder_start: usize, + pub builder_end: usize, + pub shifted_start: usize, + pub source_start: usize, + pub source_end: usize, + pub warichu: bool, + /// UTF-8 byte anchor for indexed Skia style metrics. + pub style_anchor_start: usize, + /// UTF-16 anchor for choosing the line containing those metrics. + pub style_anchor_utf16: usize, +} + +/// Offset maps of one horizontal paragraph between source characters, the +/// kinsoku-adjusted layout text and the builder text. Building it runs the +/// kinsoku pass; reuse it for every lookup in the paragraph. +pub(crate) struct HorizontalOffsets { + /// Map between the transformed text and the kinsoku-adjusted text. + pub(crate) offset_map: line_breaks::OffsetMap, + pub(crate) ranges: Vec, + /// Transformed-text UTF-16 offset of every source character boundary. + boundaries: Vec, +} + +impl HorizontalOffsets { + #[cfg(test)] + pub(crate) fn new(paragraph: &Paragraph) -> Self { + let (span_texts, offset_map) = paragraph.layout_span_texts(); + Self::from_layout_texts(paragraph, &span_texts, offset_map) + } + + /// Offsets for layout `span_texts` already built by `layout_span_texts`. + pub(super) fn from_layout_texts( + paragraph: &Paragraph, + span_texts: &[String], + offset_map: line_breaks::OffsetMap, + ) -> Self { + let ranges = span_ranges(paragraph, span_texts, &offset_map); + Self { + offset_map, + ranges, + boundaries: source_char_boundaries(paragraph), + } + } + + /// Builder-text UTF-16 offset of a paragraph source character offset. + pub(crate) fn source_to_builder(&self, source_char_offset: usize) -> usize { + let boundaries = &self.boundaries; + let source_utf16 = boundaries + .get(source_char_offset) + .copied() + .unwrap_or_else(|| boundaries.last().copied().unwrap_or(0)); + self.transformed_to_builder(source_utf16) + } + + pub(super) fn transformed_to_builder(&self, source_utf16: usize) -> usize { + let at = self + .ranges + .partition_point(|range| range.source_end < source_utf16); + let Some(range) = self + .ranges + .get(at) + .filter(|range| source_utf16 >= range.source_start) + else { + return self + .ranges + .last() + .map(|range| range.builder_end) + .unwrap_or(0); + }; + if range.warichu { + return if source_utf16 >= range.source_end { + range.builder_end + } else { + range.builder_start + }; + } + let shifted = self.offset_map.to_shifted(source_utf16); + range.builder_start + shifted.saturating_sub(range.shifted_start) + } + + /// Paragraph source character offset of a builder-text UTF-16 offset. + pub(crate) fn builder_to_source(&self, builder_offset: usize) -> usize { + let source_utf16 = self.builder_to_transformed(builder_offset); + let boundaries = &self.boundaries; + boundaries + .partition_point(|boundary| *boundary < source_utf16) + .min(boundaries.len().saturating_sub(1)) + } + + pub(super) fn builder_to_transformed(&self, builder_offset: usize) -> usize { + self.ranges + .iter() + .find(|range| { + builder_offset >= range.builder_start && builder_offset <= range.builder_end + }) + .map(|range| { + if range.warichu { + if builder_offset > range.builder_start { + range.source_end + } else { + range.source_start + } + } else { + let within = builder_offset + .saturating_sub(range.builder_start) + .min(range.builder_end - range.builder_start); + self.offset_map.to_original(range.shifted_start + within) + } + }) + .unwrap_or_else(|| { + self.ranges + .last() + .map(|range| range.source_end) + .unwrap_or(0) + }) + } + + /// Builder-text ranges of the selected source characters + /// `[source_start, source_end)` outside warichu spans. + pub(crate) fn normal_selection_ranges( + &self, + paragraph: &Paragraph, + source_start: usize, + source_end: usize, + ) -> Vec> { + let mut span_start = 0usize; + paragraph + .children() + .iter() + .filter_map(|span| { + let span_end = span_start + span.text.chars().count(); + let selected_start = source_start.max(span_start); + let selected_end = source_end.min(span_end); + span_start = span_end; + if span.is_warichu() || selected_start >= selected_end { + return None; + } + Some(self.source_to_builder(selected_start)..self.source_to_builder(selected_end)) + }) + .collect() + } +} + +#[cfg(test)] +pub(crate) fn horizontal_span_ranges(paragraph: &Paragraph) -> Vec { + HorizontalOffsets::new(paragraph).ranges +} + +#[cfg(test)] +pub(crate) fn horizontal_builder_to_source(paragraph: &Paragraph, builder_offset: usize) -> usize { + HorizontalOffsets::new(paragraph).builder_to_source(builder_offset) +} + +/// Ranges shared by layout, position data and editor mapping. `shifted_*` +/// addresses the kinsoku-adjusted text; `builder_*` addresses the builder +/// text, where each warichu span collapses to one placeholder. +fn span_ranges( + paragraph: &Paragraph, + span_texts: &[String], + offset_map: &line_breaks::OffsetMap, +) -> Vec { + let mut builder_cursor = 0usize; + let mut builder_byte_cursor = 0usize; + let mut shifted_cursor = 0usize; + paragraph + .children() + .iter() + .zip(span_texts) + .enumerate() + .map(|(span_index, (span, text))| { + let shifted_start = shifted_cursor; + shifted_cursor += text.encode_utf16().count(); + let shifted_end = shifted_cursor; + let source_start = offset_map.to_original(shifted_start); + let source_end = offset_map.to_original(shifted_end); + let warichu = span.is_warichu(); + let leading_break = + usize::from(warichu && text.starts_with(line_breaks::FORCED_LINE_BREAK)); + builder_cursor += leading_break; + builder_byte_cursor += leading_break * line_breaks::FORCED_LINE_BREAK.len_utf8(); + let builder_start = builder_cursor; + let (style_anchor_start, style_anchor_utf16) = if warichu { + ( + builder_byte_cursor + '\u{FFFC}'.len_utf8(), + builder_start + 1, + ) + } else { + // A forced separator belongs to the preceding line and has + // no style metrics. Anchor annotations to the first source + // character rather than a leading layout-only control. + let mut shifted = shifted_start; + let (bytes, utf16) = text + .chars() + .take_while(|ch| { + let inserted = offset_map.is_inserted(shifted); + shifted += ch.len_utf16(); + inserted + }) + .fold((0, 0), |(bytes, utf16), ch| { + (bytes + ch.len_utf8(), utf16 + ch.len_utf16()) + }); + (builder_byte_cursor + bytes, builder_start + utf16) + }; + builder_cursor += if warichu { + HORIZONTAL_WARICHU_BUILDER_LEN + } else { + shifted_end - shifted_start + }; + builder_byte_cursor += if warichu { + '\u{FFFC}'.len_utf8() + + HORIZONTAL_WARICHU_STYLE_ANCHOR.len_utf8() + + HORIZONTAL_WARICHU_BREAK_ANCHOR.len_utf8() + } else { + // `add_text_with_tabs` stores every tab as a U+FFFC placeholder. + text.len() + text.matches('\t').count() * ('\u{FFFC}'.len_utf8() - 1) + }; + HorizontalSpanRange { + span: span_index, + builder_start, + builder_end: builder_cursor, + shifted_start, + source_start, + source_end, + warichu, + style_anchor_start, + style_anchor_utf16, + } + }) + .collect() +} + +/// Transformed-text UTF-16 offset of every source character boundary: the +/// space the kinsoku offset map reports as "original". Differs from the +/// source UTF-16 when a transform changes the text length (`ß` -> `SS`). +fn source_char_boundaries(paragraph: &Paragraph) -> Vec { + let mut boundaries = vec![0usize]; + let mut span_base = 0usize; + for span in paragraph.children() { + let applied = span.apply_text_transform_with_source_ranges(); + let mut ranges = applied.source_ranges.iter().peekable(); + let mut source_utf16 = 0usize; + let mut output_end = 0usize; + for character in span.text.chars() { + source_utf16 += character.len_utf16(); + while let Some((output, _)) = ranges.next_if(|(_, source)| source.end <= source_utf16) { + output_end = output.end; + } + boundaries.push(span_base + output_end); + } + span_base += applied.text.encode_utf16().count(); + } + boundaries +} diff --git a/render-wasm/src/shapes/text_horizontal/overflow.rs b/render-wasm/src/shapes/text_horizontal/overflow.rs new file mode 100644 index 0000000000..97a01aa8ff --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/overflow.rs @@ -0,0 +1,258 @@ +//! Repair SkParagraph's emergency splits without changing the wrap budget. + +use super::*; +use crate::shapes::line_breaks::PolicyBoundaries; +use crate::shapes::text_paragraph::LayoutParagraph; + +/// The paragraph's span texts after text transform, joined. +fn transformed_text(paragraph: &Paragraph) -> String { + paragraph + .children() + .iter() + .map(TextSpan::apply_text_transform) + .collect() +} + +/// UTF-16 ranges, in the transformed text, of the ruby bases +/// (`Paragraph::ruby_groups`) and warichu spans that must stay whole. +fn annotation_ranges(paragraph: &Paragraph) -> Vec> { + let group_of = paragraph.ruby_group_of_spans(); + let mut offset = 0; + let mut ranges: Vec<(Option, std::ops::Range)> = Vec::new(); + for (index, span) in paragraph.children().iter().enumerate() { + let start = offset; + offset += span.apply_text_transform().encode_utf16().count(); + let group = group_of[index]; + match ranges.last_mut() { + Some((last_group, range)) if group.is_some() && *last_group == group => { + range.end = offset + } + _ if group.is_some() || span.is_warichu() => ranges.push((group, start..offset)), + _ => {} + } + } + ranges.into_iter().map(|(_, range)| range).collect() +} + +/// True when `position` falls strictly inside one of the ascending +/// `annotations` ranges. +fn within_annotation(annotations: &[std::ops::Range], position: usize) -> bool { + annotations + .get(annotations.partition_point(|r| r.end <= position)) + .is_some_and(|r| r.start < position) +} + +/// A finite, real wrap width; auto-width and unbounded layout skip repairs. +fn is_wrap_width(width: f32) -> bool { + width.is_finite() && width > 0.0 && width != f32::MAX +} + +impl HorizontalParagraphPlan { + /// SkParagraph's emergency fallback can ignore punctuation joins inside + /// an overlong word. Measure whole units once, then reopen only safe + /// grapheme boundaries of units that cannot fit an empty line. The + /// replacement markers have identical UTF-8/UTF-16 lengths, so every + /// source/builder offset remains identical to the unrelaxed plan. + pub(crate) fn relax_overlong_words( + &mut self, + paragraph: &Paragraph, + width: f32, + measure: impl FnOnce(&Self) -> LayoutParagraph, + ) { + let policy = paragraph.layout_policy(); + if !policy.reopens_overlong_words() || !is_wrap_width(width) { + return; + } + let boundaries = PolicyBoundaries::new(&transformed_text(paragraph), policy); + let graphemes = &boundaries.graphemes; + let annotations = annotation_ranges(paragraph); + let mut units = Vec::new(); + let mut start = 0; + for (at, grapheme) in graphemes.iter().enumerate().skip(1) { + if grapheme.allowed { + if at - start > 1 { + units.push(start..at); + } + start = at; + } + } + if graphemes.len() - start > 1 { + units.push(start..graphemes.len()); + } + if units.is_empty() { + return; + } + let measured = measure(self); + let line_starts: Vec<_> = measured + .get_line_metrics() + .iter() + .skip(1) + .map(|line| line.start_index) + .collect(); + let mut emergency = Vec::new(); + for unit in units { + let first = graphemes[unit.start].utf16; + let last = graphemes + .get(unit.end) + .map_or(boundaries.length, |g| g.utf16); + let from = self.offsets.transformed_to_builder(first); + let to = self.offsets.transformed_to_builder(last); + let split = line_starts.partition_point(|at| *at <= from); + if line_starts.get(split).is_none_or(|at| *at >= to) { + continue; + } + let extent: f32 = measured + .get_rects_for_range(from..to, RectHeightStyle::Tight, RectWidthStyle::Tight) + .iter() + .map(|box_| box_.rect.width()) + .sum(); + if extent <= width + 0.01 { + continue; + } + for grapheme in &graphemes[unit.start + 1..unit.end] { + if grapheme.emergency && !within_annotation(&annotations, grapheme.utf16) { + emergency.push(grapheme.utf16); + } + } + } + if !emergency.is_empty() { + *self = Self::with_emergency_breaks(paragraph, &emergency); + } + } + + /// Keep an indivisible unit together even when Skia's last-resort wrapping + /// ignores its joiners. Isolate overflowing units, measure the surrounding + /// text at the original width, and freeze those legal line starts before + /// increasing Skia's physical width. All consumers use this same plan. + pub(crate) fn retain_atomic_overflow( + &mut self, + paragraph: &Paragraph, + width: f32, + mut measure: impl FnMut(&Self, f32) -> LayoutParagraph, + ) { + let policy = paragraph.layout_policy(); + if !policy.keeps_overlong_units() || !is_wrap_width(width) { + return; + } + let boundaries = PolicyBoundaries::new(&transformed_text(paragraph), policy); + let graphemes = &boundaries.graphemes; + let length = boundaries.length; + let annotations = annotation_ranges(paragraph); + let hard_break = |c| matches!(c, '\n' | '\r' | '\u{2028}' | '\u{2029}'); + let mut units = Vec::new(); + let mut start = 0; + for at in 1..graphemes.len() { + let position = graphemes[at].utf16; + let (before, after) = (graphemes[at - 1].first, graphemes[at].first); + if hard_break(before) + || hard_break(after) + || (!within_annotation(&annotations, position) && graphemes[at].emergency) + { + if at - start > 1 { + units.push(graphemes[start].utf16..position); + } + start = at; + } + } + if graphemes.len() - start > 1 { + units.push(graphemes[start].utf16..length); + } + if units.is_empty() { + return; + } + let bounded = measure(self, width); + let starts: Vec<_> = bounded + .get_line_metrics() + .iter() + .skip(1) + .map(|line| self.offsets.builder_to_transformed(line.start_index)) + .collect(); + units.retain(|unit| { + starts + .get(starts.partition_point(|at| *at <= unit.start)) + .is_some_and(|at| *at < unit.end) + }); + if units.is_empty() { + return; + } + let natural = measure(self, f32::MAX); + let mut physical_width = width; + units.retain(|unit| { + let from = self.offsets.transformed_to_builder(unit.start); + let to = self.offsets.transformed_to_builder(unit.end); + let extent: f32 = natural + .get_rects_for_range(from..to, RectHeightStyle::Tight, RectWidthStyle::Tight) + .iter() + .map(|box_| box_.rect.width()) + .sum(); + if extent <= width + 0.01 { + return false; + } + physical_width = physical_width.max(extent); + true + }); + if units.is_empty() { + return; + } + let can_insert = |at: usize| { + at > 0 + && at < length + && graphemes + .binary_search_by_key(&at, |g| g.utf16) + .ok() + .is_some_and(|i| { + !hard_break(graphemes[i].first) && !hard_break(graphemes[i - 1].first) + }) + }; + let mut forced: Vec<_> = units + .iter() + .flat_map(|unit| [unit.start, unit.end]) + .filter(|at| can_insert(*at)) + .collect(); + forced.sort_unstable(); + forced.dedup(); + let isolated = Self::with_breaks(paragraph, &self.emergency_breaks, &forced); + let bounded = measure(&isolated, width); + physical_width = physical_width.max(bounded.longest_line()); + forced.extend( + bounded + .get_line_metrics() + .iter() + .skip(1) + .map(|line| isolated.offsets.builder_to_transformed(line.start_index)) + .filter(|at| { + can_insert(*at) + && units + .get(units.partition_point(|unit| unit.end <= *at)) + .is_none_or(|unit| *at <= unit.start) + }), + ); + forced.sort_unstable(); + forced.dedup(); + *self = Self::with_breaks(paragraph, &self.emergency_breaks, &forced); + // Avoid a floating-point edge reintroducing the very fallback being + // repaired. The epsilon changes physical width, not the wrap budget. + self.overflow_width = Some(physical_width + 0.01); + } + + pub(crate) fn layout_width(&self, width: f32) -> f32 { + self.overflow_width.unwrap_or(width) + } + + /// Skia aligns against the physical width; translate it back to the box. + pub(crate) fn x_offset(&self, paragraph: &Paragraph, width: f32) -> f32 { + use crate::shapes::{TextAlign, TextDirection}; + let slack = width - self.layout_width(width); + match paragraph.text_align() { + TextAlign::Center => slack * 0.5, + TextAlign::Right => slack, + TextAlign::Start if paragraph.text_direction() == TextDirection::RTL => slack, + TextAlign::End if paragraph.text_direction() == TextDirection::LTR => slack, + _ => 0.0, + } + } + + pub(crate) fn has_atomic_overflow(&self) -> bool { + self.overflow_width.is_some() + } +} diff --git a/render-wasm/src/shapes/text_horizontal/ruby.rs b/render-wasm/src/shapes/text_horizontal/ruby.rs new file mode 100644 index 0000000000..d7bcf3dced --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/ruby.rs @@ -0,0 +1,767 @@ +use crate::render::FallbackFonts; +use crate::shapes::text_paragraph::LayoutParagraph; + +use super::*; + +/// Top-left of the Zhuyin strip beside a horizontal base whose em box starts +/// at `em_top`. +pub(crate) fn horizontal_zhuyin_origin( + span: &TextSpan, + rect: skia::Rect, + em_top: f32, +) -> skia::Point { + skia::Point::new( + rect.right() - span.font_size * super::super::text_zhuyin::STRIP_WIDTH, + em_top, + ) +} + +/// Zhuyin glyphs of a horizontal base whose builder text starts at UTF-16 +/// `base_start` and whose first line rect is `rect`, with the strip's +/// top-left in the laid-out paragraph. Position data, painting and bounds +/// share it. +fn horizontal_zhuyin_glyphs( + span: &TextSpan, + syllable: &super::super::text_zhuyin::Syllable, + laid_out: &LayoutParagraph, + lines: &[skia::textlayout::LineMetrics], + base_start: usize, + rect: skia::Rect, + fonts: (&skia::textlayout::TypefaceFontProvider, &FallbackFonts), +) -> (skia::Point, Vec) { + let em_top = horizontal_base_em_top(laid_out, lines, base_start, span.font_size, rect); + let origin = horizontal_zhuyin_origin(span, rect, em_top); + let glyphs = super::super::text_zhuyin::shape(span, syllable, fonts.0, fonts.1); + (origin, glyphs) +} + +pub(crate) fn horizontal_zhuyin_entries( + paragraph: &Paragraph, + offsets: &HorizontalOffsets, + laid_out: &LayoutParagraph, +) -> Vec { + if !paragraph.children().iter().any(TextSpan::is_zhuyin) { + return Vec::new(); + } + let provider = get_resources().fonts.font_provider(); + let fallbacks = get_fallback_fonts(); + let lines = laid_out.get_line_metrics(); + let mut entries = Vec::new(); + for (index, range) in horizontal_ruby_targets(paragraph, offsets) { + let base_start = range.start; + let span = ¶graph.children()[index]; + let Some(syllable) = super::super::text_zhuyin::syllable(span) else { + continue; + }; + let rects = + laid_out.get_rects_for_range(range, RectHeightStyle::Tight, RectWidthStyle::Tight); + let Some(rect) = rects.first() else { + continue; + }; + let (origin, glyphs) = horizontal_zhuyin_glyphs( + span, + &syllable, + laid_out, + &lines, + base_start, + rect.rect, + (provider, fallbacks), + ); + let size = span.ruby_font_size(); + for glyph in glyphs { + entries.push(crate::shapes::PositionData { + paragraph: 0, + span: index as u32, + start_pos: glyph.range.start as u32, + end_pos: glyph.range.end as u32, + x: origin.x + glyph.origin.x, + y: origin.y + glyph.origin.y - size, + width: size, + height: size, + direction: crate::shapes::DIRECTION_ZHUYIN, + }); + } + } + entries +} + +/// Extra spacing for long horizontal ruby, per span of a paragraph. +#[derive(Debug, Clone, Default)] +pub(crate) struct HorizontalRubySpacing { + /// Letter-spacing added to characters of the layout texts: (char index, + /// amount) per span. + pub adjustments: Vec>, + /// Gap added between the base characters of each ruby span. + pub base_gaps: Vec, + /// Spacing added before each ruby base, on the previous character: half + /// a gap, or none at a paragraph start, where the base takes it after. + pub leading_gaps: Vec, + /// Overhang room (before, after) of each ruby span over its neighbours. + pub rooms: Vec<(f32, f32)>, +} + +/// The span that owns a character of the layout `texts`, its char index and +/// the character, searching from (span, index) in `step` direction and +/// skipping inserted word joiners. +fn layout_neighbour(texts: &[String], span: usize, forward: bool) -> Option<(usize, usize, char)> { + let real = |(index, ch): (usize, char)| { + (!line_breaks::is_inserted_zero_width(ch)).then_some((index, ch)) + }; + if forward { + texts + .iter() + .enumerate() + .skip(span + 1) + .find_map(|(owner, text)| { + text.chars() + .enumerate() + .find_map(real) + .map(|(index, ch)| (owner, index, ch)) + }) + } else { + texts + .iter() + .enumerate() + .take(span) + .rev() + .find_map(|(owner, text)| { + let chars: Vec = text.chars().collect(); + chars + .iter() + .copied() + .enumerate() + .rev() + .find_map(real) + .map(|(index, ch)| (owner, index, ch)) + }) + } +} + +/// Spacing that makes room for horizontal ruby longer than its base. The +/// reading overhangs its neighbours within their `ruby_overhang_room`, less +/// what an earlier reading already hangs over the same character (JLREQ +/// Fig 138); the rest spreads the base (JLREQ §3.3.8): an equal gap between base +/// characters and half a gap at each end, the leading half on the previous +/// character. +pub(crate) fn horizontal_ruby_spacing( + paragraph: &Paragraph, + texts: &[String], + offset_map: &line_breaks::OffsetMap, +) -> HorizontalRubySpacing { + if !paragraph.children().iter().any(TextSpan::has_ruby) { + return HorizontalRubySpacing::empty(texts.len()); + } + horizontal_ruby_spacing_with( + paragraph, + texts, + offset_map, + get_resources().fonts.font_provider(), + get_fallback_fonts(), + ) +} + +impl HorizontalRubySpacing { + fn empty(spans: usize) -> Self { + Self { + adjustments: vec![Vec::new(); spans], + base_gaps: vec![0.0; spans], + leading_gaps: vec![0.0; spans], + rooms: vec![(0.0, 0.0); spans], + } + } +} + +/// `horizontal_ruby_spacing` with explicit fonts. +pub(super) fn horizontal_ruby_spacing_with( + paragraph: &Paragraph, + texts: &[String], + offset_map: &line_breaks::OffsetMap, + font_provider: &skia::textlayout::TypefaceFontProvider, + fallback_families: &FallbackFonts, +) -> HorizontalRubySpacing { + let spans = paragraph.children(); + let mut spacing = HorizontalRubySpacing::empty(texts.len()); + let fallback_mgr = FontMgr::from(font_provider.clone()); + let advances = |span: &TextSpan, text: &str, font_size: f32| -> Vec { + shape_segment_with_fallbacks( + text, + font_size, + &span_font_families(span, fallback_families), + font_provider, + false, + span.font_features, + &fallback_mgr, + &span.language, + ) + .iter() + .flat_map(|run| run.advances.iter().copied()) + .collect() + }; + let mut claims = OverhangClaims::new(); + // UTF-16 start of each span in the layout text. + let span_starts: Vec = texts + .iter() + .scan(0usize, |start, text| { + let span_start = *start; + *start += text.encode_utf16().count(); + Some(span_start) + }) + .collect(); + for group in paragraph.ruby_groups() { + // The first span holds the reading and its presentation. + let index = group.start; + let span = &spans[index]; + // Source characters of the base, as (span, char index); inserted + // spacing and joiners are not. + let base: Vec<(usize, usize)> = group + .clone() + .flat_map(|member| { + let mut utf16 = span_starts[member]; + texts[member] + .chars() + .enumerate() + .filter(move |(_, ch)| { + let inserted = offset_map.is_inserted(utf16); + utf16 += ch.len_utf16(); + !inserted + }) + .map(move |(char_index, _)| (member, char_index)) + }) + .collect(); + let Some(&last) = base.last() else { + continue; + }; + if span.is_zhuyin() { + // Inter-character annotations consume inline space, not ruby's + // above/below leading. Reserve the strip even at a line end. + for (member, char_index) in &base { + spacing.adjustments[*member].push(( + *char_index, + span.font_size * super::super::text_zhuyin::STRIP_WIDTH, + )); + } + continue; + } + let ruby_font_size = span.ruby_font_size(); + let base_width: f32 = group + .clone() + .map(|member| { + let member_span = &spans[member]; + let chars = base.iter().filter(|(owner, _)| *owner == member).count(); + advances( + member_span, + &member_span.apply_text_transform(), + member_span.font_size, + ) + .iter() + .sum::() + + member_span.letter_spacing * chars as f32 + }) + .sum(); + // The painter sets the reading in pieces (see `RubyLine::paint`). + let shaped = shape_segment_with_fallbacks( + span.ruby_text(), + ruby_font_size, + &span_font_families(span, fallback_families), + font_provider, + false, + span.font_features, + &fallback_mgr, + &span.language, + ); + let (_, pieces, _) = + horizontal_ruby_pieces(&ruby_glyph_refs(span.ruby_text(), &shaped, ruby_font_size)); + let ruby_length: f32 = pieces.iter().sum(); + let previous = layout_neighbour(texts, group.start, false); + let next = layout_neighbour(texts, group.end - 1, true); + let room = |neighbour: Option<(usize, usize, char)>| { + neighbour.map_or(0.0, |(owner, _, ch)| { + ruby_overhang_room( + span.ruby_overhang, + Some(ch), + spans[owner].has_ruby(), + spans[owner].font_size, + ruby_font_size, + ) + }) + }; + let key = |(owner, char_index, _): (usize, usize, char)| (owner, char_index); + let rooms = claims.room( + previous.map(key), + next.map(key), + (room(previous), room(next)), + ruby_length - base_width, + ); + spacing.rooms[index] = rooms; + let gap = long_ruby_gap(ruby_length, base_width, rooms, base.len()); + if gap <= 0.0 { + continue; + } + spacing.base_gaps[index] = gap; + for &(member, char_index) in &base { + let amount = if (member, char_index) == last { + gap / 2.0 + } else { + gap + }; + spacing.adjustments[member].push((char_index, amount)); + } + match previous { + Some((owner, char_index, _)) => { + spacing.adjustments[owner].push((char_index, gap / 2.0)); + spacing.leading_gaps[index] = gap / 2.0; + } + None => spacing.adjustments[last.0].push((last.1, gap / 2.0)), + } + } + spacing +} + +/// Split `total` glyphs across segments proportionally to their extents +/// (rounded per segment, remainder to the last) so no glyph is dropped. +pub(super) fn split_counts_by_extent(extents: &[f32], total: usize) -> Vec { + let mut counts = vec![0usize; extents.len()]; + if extents.is_empty() || total == 0 { + return counts; + } + let sum: f32 = extents.iter().sum(); + if sum <= 0.0 { + counts[extents.len() - 1] = total; + return counts; + } + let mut assigned = 0usize; + let last = extents.len() - 1; + for (index, extent) in extents.iter().enumerate() { + let count = if index == last { + total - assigned + } else { + (((extent / sum) * total as f32).round() as usize).min(total - assigned) + }; + counts[index] = count; + assigned += count; + } + counts +} + +/// (first span index, builder-text range) of every base whose ruby is +/// painted: visible ruby on a base that is not warichu. A base covers the +/// spans of its `Paragraph::ruby_groups` entry; its first span holds the +/// reading and its presentation. +pub(super) fn horizontal_ruby_targets( + paragraph: &Paragraph, + offsets: &HorizontalOffsets, +) -> Vec<(usize, std::ops::Range)> { + paragraph + .ruby_groups() + .into_iter() + .filter_map(|spans| { + let first = offsets.ranges.get(spans.start)?; + let last = offsets.ranges.get(spans.end - 1)?; + let start = offsets.transformed_to_builder(first.source_start); + (start < last.builder_end).then_some((first.span, start..last.builder_end)) + }) + .collect() +} + +/// Rects of a base range joined per line: a base that crosses spans or +/// font runs gets one rect for each run, but its reading spreads over the +/// whole line part. +fn base_line_rects(rects: Vec) -> Vec { + let mut lines: Vec = Vec::new(); + for rect in rects { + match lines.last_mut() { + Some(line) + if rect.rect.top() < line.rect.bottom() && rect.rect.bottom() > line.rect.top() => + { + line.rect.join(rect.rect) + } + _ => lines.push(rect), + } + } + lines +} + +/// Paint of the pass that laid out the base: fill, stroke, shadow or mask. +/// `fallback` covers a base Skia kept no style metrics for. +pub(super) fn horizontal_ruby_paint( + laid_out: &LayoutParagraph, + range: &HorizontalSpanRange, + fallback: skia::Paint, +) -> skia::Paint { + horizontal_span_style(laid_out, range).map_or(fallback, |style| style.foreground()) +} + +/// Ink edge of `glyph` (bottom when `over`, else top), used to attach ruby +/// ink to its base. Font-wide ascent/descent include leading, which leaves +/// ruby detached in many Japanese faces. +fn horizontal_ruby_ink_edge(font: &Font, glyph: GlyphId, fallback: f32, over: bool) -> f32 { + let mut bounds = [skia::Rect::default()]; + font.get_bounds(&[glyph], &mut bounds, None); + let bound = bounds[0]; + if bound.right > bound.left && bound.bottom > bound.top { + if over { + bound.bottom + } else { + bound.top + } + } else { + fallback + } +} + +/// One shaped ruby glyph: (run index, glyph index in the run, advance, +/// whether it belongs to a Western word). +pub(super) type RubyGlyphRef = (usize, usize, f32, bool); + +/// The glyphs of a reading shaped from `ruby_text`, in order. +pub(super) fn ruby_glyph_refs( + ruby_text: &str, + shaped: &[ShapedRun], + ruby_font_size: f32, +) -> Vec { + shaped + .iter() + .enumerate() + .flat_map(|(run_index, run)| { + (0..run.glyphs.len()).map(move |glyph| { + let advance = run.advances.get(glyph).copied().unwrap_or(ruby_font_size); + let western = run + .clusters + .get(glyph) + .and_then(|cluster| ruby_text.get(*cluster as usize..)) + .and_then(|rest| rest.chars().next()) + .is_some_and(|ch| !is_upright_char(ch)); + (run_index, glyph, advance, western) + }) + }) + .collect() +} + +/// Pieces of a horizontal reading (`ruby_pieces`): each upright glyph in a +/// slot as wide as the widest upright glyph, each Western word whole. +/// Returns the slot, the piece extents and each glyph's piece and offset. +pub(super) fn horizontal_ruby_pieces( + glyphs: &[RubyGlyphRef], +) -> (f32, Vec, Vec<(usize, f32)>) { + let slot = glyphs + .iter() + .filter(|(_, _, _, western)| !western) + .map(|(_, _, advance, _)| *advance) + .fold(0.0f32, f32::max) + .max(1.0); + let flags: Vec<(bool, f32)> = glyphs + .iter() + .map(|(_, _, advance, western)| (*western, *advance)) + .collect(); + let (extents, placement) = ruby_pieces(&flags, slot); + (slot, extents, placement) +} + +/// Ink edge shared by the glyphs of one ruby line: the lowest ink bottom for +/// over ruby, the highest ink top for under ruby. One edge for the whole line +/// keeps marks whose ink sits mid-height, such as ー and ・, on the line's +/// baseline instead of pulling each of them onto the base. Western letters +/// (Pinyin, romaji) take the font's descent and cap height instead of their +/// ink, so every reading keeps one baseline whatever its descenders, ascenders +/// or tone marks. +pub(super) fn ruby_line_ink_edge(shaped: &[ShapedRun], glyphs: &[RubyGlyphRef], over: bool) -> f32 { + let edges = glyphs.iter().map(|(run_index, glyph, _, western)| { + let run = &shaped[*run_index]; + let (_, metrics) = run.font.metrics(); + if *western { + if over { + return metrics.descent; + } + if metrics.cap_height > 0.0 { + return -metrics.cap_height; + } + } + let fallback = if over { + metrics.descent + } else { + metrics.ascent + }; + horizontal_ruby_ink_edge(&run.font, run.glyphs[*glyph], fallback, over) + }); + let edge = if over { + edges.fold(f32::MIN, f32::max) + } else { + edges.fold(f32::MAX, f32::min) + }; + if edge.is_finite() && edge.abs() < f32::MAX { + edge + } else { + 0.0 + } +} + +/// Paint ruby for one laid-out horizontal paragraph. Draw-only: base rects +/// come from `get_rects_for_range`, and the ruby is shaped at +/// `ruby_font_size` and spread over each line's rect with the vertical path's +/// jlreq distribution (`distribute_ruby_pieces`). Lines are not reflowed; ruby +/// draws in the line's leading. +pub(crate) fn paint_horizontal_ruby( + canvas: &Canvas, + text_content: &TextContent, + paragraph_index: usize, + laid_out: &LayoutParagraph, + x: f32, + y: f32, +) { + visit_horizontal_ruby_glyphs( + text_content, + paragraph_index, + laid_out, + x, + y, + |font, glyph, origin, paint| { + if let Some(blob) = single_glyph_blob(font, glyph) { + canvas.draw_text_blob(&blob, origin, paint); + } + }, + ); +} + +/// Ink bounds of the horizontal readings (ruby and Zhuyin) of a paragraph +/// laid out at (`x`, `y`), or None without readings. +pub(crate) fn horizontal_ruby_ink_bounds( + text_content: &TextContent, + paragraph_index: usize, + laid_out: &LayoutParagraph, + x: f32, + y: f32, +) -> Option { + let mut bounds: Option = None; + visit_horizontal_ruby_glyphs( + text_content, + paragraph_index, + laid_out, + x, + y, + |font, glyph, origin, _| { + let mut ink = [skia::Rect::default()]; + font.get_bounds(&[glyph], &mut ink, None); + let ink = ink[0].with_offset(origin); + if !ink.is_empty() { + bounds = Some(match bounds { + Some(mut joined) => { + joined.join(ink); + joined + } + None => ink, + }); + } + }, + ); + bounds +} + +/// Each reading glyph of a horizontal paragraph laid out at (`x`, `y`): its +/// font, glyph id, baseline origin and paint. Painting and bounds share it, +/// so exports keep exactly the ink the canvas draws. +fn visit_horizontal_ruby_glyphs( + text_content: &TextContent, + paragraph_index: usize, + laid_out: &LayoutParagraph, + x: f32, + y: f32, + mut visit: impl FnMut(&skia::Font, skia::GlyphId, (f32, f32), &skia::Paint), +) { + let Some(paragraph) = text_content.paragraphs().get(paragraph_index) else { + return; + }; + if !paragraph.children().iter().any(TextSpan::has_ruby) { + return; + } + let font_provider = get_resources().fonts.font_provider(); + let fallback_mgr = FontMgr::from(font_provider.clone()); + let fallback_families = get_fallback_fonts(); + let bounds = text_content.bounds(); + let plans = text_content.horizontal_plans(); + let Some(plan) = plans.get(paragraph_index) else { + return; + }; + let offsets = &plan.offsets; + let lines = laid_out.get_line_metrics(); + + for (span_index, span_range) in horizontal_ruby_targets(paragraph, offsets) { + let span = ¶graph.children()[span_index]; + let base_start = span_range.start; + let paint = horizontal_ruby_paint( + laid_out, + &offsets.ranges[span_index], + merge_fills(&span.fills, bounds), + ); + let rects = base_line_rects(laid_out.get_rects_for_range( + span_range, + skia::textlayout::RectHeightStyle::Tight, + skia::textlayout::RectWidthStyle::Tight, + )); + if let Some(syllable) = super::super::text_zhuyin::syllable(span) { + if let Some(rect) = rects.first() { + let (origin, glyphs) = horizontal_zhuyin_glyphs( + span, + &syllable, + laid_out, + &lines, + base_start, + rect.rect, + (font_provider, fallback_families), + ); + for glyph in glyphs { + visit( + &glyph.run.font, + glyph.run.glyphs[glyph.glyph], + (x + origin.x + glyph.origin.x, y + origin.y + glyph.origin.y), + &paint, + ); + } + } + continue; + } + let shaped = shape_segment_with_fallbacks( + span.ruby_text(), + span.ruby_font_size(), + &span_font_families(span, fallback_families), + font_provider, + false, + span.font_features, + &fallback_mgr, + &span.language, + ); + let glyphs = ruby_glyph_refs(span.ruby_text(), &shaped, span.ruby_font_size()); + let extents: Vec = rects.iter().map(|rect| rect.rect.width()).collect(); + let counts = split_counts_by_extent(&extents, glyphs.len()); + let mut assigned = 0usize; + for (rect_box, count) in rects.iter().zip(counts) { + let slice = &glyphs[assigned..assigned + count]; + assigned += count; + if slice.is_empty() { + continue; + } + let leading_gap = plan.ruby_spacing.leading_gaps[span_index]; + let room = room_inside_line( + &lines, + rect_box.rect, + rect_box.rect.left() - leading_gap, + rect_box.rect.width() + leading_gap, + plan.ruby_spacing.rooms[span_index], + ); + let ruby_line = RubyLine { + span, + shaped: &shaped, + glyphs: slice, + base: rect_box.rect, + em_top: horizontal_base_em_top( + laid_out, + &lines, + base_start, + span.font_size, + rect_box.rect, + ), + leading_gap, + room, + }; + ruby_line.visit_glyphs(x, y, |font, glyph, origin| { + visit(font, glyph, origin, &paint) + }); + } + } +} + +/// The ruby glyphs over one line's rect of a base. +pub(super) struct RubyLine<'a> { + pub span: &'a TextSpan, + pub shaped: &'a [ShapedRun], + pub glyphs: &'a [RubyGlyphRef], + /// Base rect in the laid-out paragraph. + pub base: skia::Rect, + /// Top of the base em box (`horizontal_base_em_top`). + pub em_top: f32, + /// Spacing before the base, which sits on the previous character. + pub leading_gap: f32, + /// Overhang room (before, after) inside the line. + pub room: (f32, f32), +} + +impl RubyLine<'_> { + /// Glyphs set in pieces (`horizontal_ruby_pieces`), distributed over the + /// base and the spacing before it, with their baseline origins. + fn visit_glyphs( + &self, + x: f32, + y: f32, + mut visit: impl FnMut(&skia::Font, skia::GlyphId, (f32, f32)), + ) { + let baseline = self.baseline(y); + for ((run_index, glyph, _, _), left) in self.glyphs.iter().zip(self.glyph_lefts()) { + let run = &self.shaped[*run_index]; + visit(&run.font, run.glyphs[*glyph], (x + left, baseline)); + } + } + + /// Left edge of each glyph: upright glyphs centred in their slot, the + /// letters of a Western word at their own advances. + pub(super) fn glyph_lefts(&self) -> Vec { + let (slot, pieces, placement) = horizontal_ruby_pieces(self.glyphs); + let piece_lefts = distribute_ruby_pieces( + self.base.left() - self.leading_gap, + self.base.width() + self.leading_gap, + &pieces, + self.span.ruby_align, + self.room, + ); + self.glyphs + .iter() + .zip(placement) + .map(|((_, _, advance, western), (piece, offset))| { + let centring = if *western { + 0.0 + } else { + (slot - advance) / 2.0 + }; + piece_lefts[piece] + offset + centring + }) + .collect() + } + + /// Baseline of the line that attaches its ink to the base: above the base + /// em for over ruby, below the base rect for under ruby. + pub(super) fn baseline(&self, y: f32) -> f32 { + let over = self.span.ruby_side == AnnotationSide::Over; + let edge = ruby_line_ink_edge(self.shaped, self.glyphs, over); + if over { + y + self.em_top - edge - self.span.ruby_decoration_offset(false) + } else { + y + self.base.bottom() - edge + self.span.ruby_decoration_offset(false) + } + } +} + +/// Overhang room of a horizontal ruby base, without the sides at its line's +/// edges: ruby never sticks out past the first or last character. +fn room_inside_line( + lines: &[skia::textlayout::LineMetrics], + rect: skia::Rect, + left: f32, + width: f32, + room: (f32, f32), +) -> (f32, f32) { + let middle = rect.center_y(); + let Some(line) = lines.iter().find(|line| { + let baseline = line.baseline as f32; + middle >= baseline - line.ascent as f32 && middle <= baseline + line.descent as f32 + }) else { + return room; + }; + let line_left = line.left as f32; + let line_right = line_left + line.width as f32; + ( + if left <= line_left + 0.5 { 0.0 } else { room.0 }, + if left + width >= line_right - 0.5 { + 0.0 + } else { + room.1 + }, + ) +} diff --git a/render-wasm/src/shapes/text_horizontal/tests.rs b/render-wasm/src/shapes/text_horizontal/tests.rs new file mode 100644 index 0000000000..c3e0480b92 --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/tests.rs @@ -0,0 +1,2943 @@ +use super::justify::{horizontal_jlreq_justify_spacing, horizontal_squeeze_sheds}; +use super::ruby::{ + horizontal_ruby_paint, horizontal_ruby_spacing_with, horizontal_ruby_targets, ruby_glyph_refs, + split_counts_by_extent, RubyGlyphRef, RubyLine, +}; +use super::*; +use crate::shapes::text_paragraph::LayoutParagraph; +use crate::shapes::text_policy::TextLayoutPolicy; +use crate::shapes::{LineAdjustment, TextAlign, TextDirection, TextTransform}; +use crate::Uuid; + +fn cjk_horizontal_layout( + texts: &[&str], + language: &str, + wrap: crate::shapes::text_policy::KoreanWrap, + width: f32, +) -> (Paragraph, HorizontalParagraphPlan, LayoutParagraph) { + use crate::shapes::text_vertical::test_support::{provider, EM}; + init_state(); + let provider = provider(include_bytes!("../../fonts/cjk-locl-test.ttf")); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some(provider.into())); + let spans = texts + .iter() + .map(|text| { + let mut span = make_span(text, 0.0); + span.font_size = EM; + span + }) + .collect(); + let mut paragraph = make_paragraph(spans, 0.0); + paragraph.set_language(language.to_owned()); + paragraph.set_korean_wrap(wrap); + let (plan, layout) = cjk_layout_paragraph(¶graph, width, &fonts); + (paragraph, plan, layout) +} + +fn cjk_layout_paragraph( + paragraph: &Paragraph, + width: f32, + fonts: &skia::textlayout::FontCollection, +) -> (HorizontalParagraphPlan, LayoutParagraph) { + let mut plan = HorizontalParagraphPlan::new(paragraph); + let lay_out = |plan: &HorizontalParagraphPlan, width: f32| { + let mut paragraph_style = paragraph.paragraph_to_style(); + paragraph_style.set_apply_rounding_hack(false); + let mut builder = ParagraphBuilder::new(¶graph_style, fonts); + for (index, span) in paragraph.children().iter().enumerate() { + let style = span.to_style( + &crate::math::Rect::from_xywh(0.0, 0.0, width, 200.0), + &crate::render::FallbackFonts::default(), + false, + 1.0, + ); + builder.push_style(&style); + add_horizontal_span( + &mut builder, + span, + &plan.texts[index], + &plan.pair_spacing[index], + &plan.ruby_spacing.adjustments[index], + &style, + fonts, + ); + builder.pop(); + } + let mut layout = LayoutParagraph::build(&mut builder); + layout.layout(width); + layout + }; + plan.relax_overlong_words(paragraph, width, |plan| lay_out(plan, width)); + plan.retain_atomic_overflow(paragraph, width, lay_out); + let layout = lay_out(&plan, plan.layout_width(width)); + (plan, layout) +} + +#[test] +fn horizontal_chinese_regions_apply_compression_across_styles() { + use crate::shapes::text_policy::KoreanWrap; + let (_, cn, cn_layout) = cjk_horizontal_layout( + &["文,", "。文"], + "zh-Hans-CN", + KoreanWrap::Syllable, + 1000.0, + ); + let (_, tw, tw_layout) = cjk_horizontal_layout( + &["文,", "。文"], + "zh-Hant-TW", + KoreanWrap::Syllable, + 1000.0, + ); + assert!(!cn.pair_spacing[0].is_empty()); + assert!(tw.pair_spacing.iter().all(Vec::is_empty)); + assert!((tw_layout.longest_line() - cn_layout.longest_line() - 10.0).abs() < 0.1); + assert_eq!(cn.texts.concat().replace('\u{2060}', ""), "文,。文"); +} + +#[test] +fn horizontal_korean_emergency_wrap_does_not_start_with_closing_punctuation() { + use crate::shapes::text_policy::KoreanWrap; + let (_, plan, layout) = + cjk_horizontal_layout(&["한한", "한한", "。"], "ko", KoreanWrap::KeepWords, 42.0); + let source: Vec<_> = "한한한한。".encode_utf16().collect(); + for line in layout.get_line_metrics() { + let start = plan.offsets.offset_map.to_original(line.start_index); + assert_ne!(source.get(start), Some(&(0x3002u16))); + } +} + +#[test] +fn word_mode_moves_a_fitting_word_whole_across_style_runs() { + use crate::shapes::text_policy::KoreanWrap; + let (_, plan, layout) = + cjk_horizontal_layout(&["한글 ", "한", "국어"], "ko", KoreanWrap::KeepWords, 72.0); + let starts: Vec<_> = layout + .get_line_metrics() + .iter() + .map(|line| plan.offsets.builder_to_source(line.start_index)) + .collect(); + assert_eq!(starts, vec![0, 3]); + assert!( + !plan.texts.concat().contains('\u{200b}'), + "a fitting word keeps all its joins" + ); +} + +#[test] +fn emergency_word_boundaries_keep_identical_offsets_and_graphemes() { + use crate::shapes::text_policy::KoreanWrap; + let (paragraph, plan, layout) = + cjk_horizontal_layout(&["ᄒ", "ᅡᆫ한。"], "ko", KoreanWrap::KeepWords, 42.0); + let original = HorizontalParagraphPlan::new(¶graph); + assert!(plan.texts.concat().contains('\u{200b}')); + for at in 0..=7 { + assert_eq!( + plan.offsets.source_to_builder(at), + original.offsets.source_to_builder(at) + ); + } + for line in layout.get_line_metrics() { + let at = plan.offsets.builder_to_source(line.start_index); + assert!( + at == 0 || at == 3, + "no break inside a decomposed syllable or before closing punctuation: {at}" + ); + } + let (_, wide, _) = cjk_horizontal_layout(&["ᄒ", "ᅡᆫ한。"], "ko", KoreanWrap::KeepWords, 200.0); + assert!( + !wide.texts.concat().contains('\u{200b}'), + "resizing restores whole-word joins" + ); +} + +#[test] +fn cjk_emergency_wrapping_keeps_safe_boundaries_across_box_widths() { + use crate::shapes::text_policy::KoreanWrap; + use unicode_segmentation::UnicodeSegmentation; + + let cases: &[(&str, KoreanWrap, &[&str], &[f32])] = &[ + ( + "zh-Hans-CN", + KoreanWrap::Syllable, + &["文(", "中文)", ",中文"], + &[65.0, 72.0, 85.0, 102.0, 140.0], + ), + ( + "zh-Hant-TW", + KoreanWrap::Syllable, + &["文(", "中文)", ",中文"], + &[65.0, 72.0, 85.0, 102.0, 140.0], + ), + ( + "zh-Hant-HK", + KoreanWrap::Syllable, + &["文(", "中文)", ",中文"], + &[65.0, 72.0, 85.0, 102.0, 140.0], + ), + ( + "ko", + KoreanWrap::KeepWords, + &["한글 ", "한국어", "。"], + &[42.0, 52.0, 62.0, 72.0, 82.0, 100.0], + ), + ( + "ko", + KoreanWrap::Syllable, + &["한", "글(한", "글)와 1,234%"], + &[65.0, 72.0, 85.0, 102.0, 140.0], + ), + ( + "ko", + KoreanWrap::KeepWords, + &["한 ", "ᄒ", "ᅡᆫ글。"], + &[82.0, 100.0, 142.0], + ), + ]; + let mut violations = Vec::new(); + for &(language, wrap, texts, widths) in cases { + let source = texts.concat(); + let chars: Vec<_> = source.chars().collect(); + let boundaries: Vec<_> = source + .graphemes(true) + .scan(0, |at, g| { + let start = *at; + *at += g.chars().count(); + Some(start) + }) + .collect(); + for &width in widths { + let (paragraph, plan, layout) = cjk_horizontal_layout(texts, language, wrap, width); + for line in layout.get_line_metrics().into_iter().skip(1) { + let at = plan.offsets.builder_to_source(line.start_index); + if at < chars.len() + && (boundaries.binary_search(&at).is_err() + || (at > 0 + && !paragraph + .layout_policy() + .emergency_break_allowed(chars[at - 1], chars[at]))) + { + violations.push(format!( + "{language} {wrap:?} {width}px: break at {at} in {source}" + )); + } + } + } + } + assert!(violations.is_empty(), "{}", violations.join("\n")); +} + +#[test] +fn narrower_than_atomic_unit_layout_keeps_source_offsets_and_finite_geometry() { + use crate::shapes::text_policy::KoreanWrap; + // Overflow preserves both the punctuation rule and editable positions. + for language in ["zh-Hant-TW", "ko"] { + for width in [1.0, 10.0, 19.0, 20.0, 30.0, 39.0, 39.999] { + let (paragraph, plan, layout) = + cjk_horizontal_layout(&["文", "。"], language, KoreanWrap::KeepWords, width); + let original = HorizontalParagraphPlan::new(¶graph); + for at in 0..=2 { + let builder = plan.offsets.source_to_builder(at); + assert_eq!(plan.offsets.builder_to_source(builder), at); + assert_eq!(builder, original.offsets.source_to_builder(at)); + } + assert_eq!(layout.get_line_metrics().len(), 1); + assert!(layout.height().is_finite() && layout.height() > 0.0); + for line in layout.get_line_metrics() { + assert!(line.width.is_finite() && line.width >= 0.0); + assert!(line.baseline.is_finite()); + } + } + } +} + +#[test] +fn horizontal_cjk_justification_is_bounded_and_word_mode_only_expands_spaces() { + use crate::shapes::text_policy::KoreanWrap; + let (paragraph, plan, layout) = + cjk_horizontal_layout(&["文文", "文文"], "zh-Hant-TW", KoreanWrap::Syllable, 52.0); + let extras = + super::justify::horizontal_pairwise_justify_spacing(¶graph, &layout, &plan, 52.0); + assert!(extras[1].is_empty(), "last line stays unexpanded"); + let total: f32 = extras[0].iter().map(|(_, gap)| *gap).sum(); + assert!(total > 9.0 && total <= 10.0); + let (paragraph, plan, layout) = + cjk_horizontal_layout(&["한 한 한 한"], "ko", KoreanWrap::KeepWords, 72.0); + let extras = + super::justify::horizontal_pairwise_justify_spacing(¶graph, &layout, &plan, 72.0); + assert!(!extras[0].is_empty()); + for (at, amount) in &extras[0] { + assert_eq!(plan.texts[0].chars().nth(*at), Some(' ')); + assert!(*amount <= 10.0); + } + let (paragraph, plan, layout) = + cjk_horizontal_layout(&["한한한한"], "ko", KoreanWrap::KeepWords, 52.0); + assert!( + super::justify::horizontal_pairwise_justify_spacing(¶graph, &layout, &plan, 52.0) + .iter() + .all(Vec::is_empty) + ); +} + +#[test] +fn distribution_fills_the_last_line_between_hangul_graphemes_across_styles() { + use crate::shapes::text_policy::KoreanWrap; + let (paragraph, plan, layout) = + cjk_horizontal_layout(&["기획", "편집"], "ko", KoreanWrap::KeepWords, 310.0); + let mut paragraph = Paragraph::new( + TextAlign::Distribute, + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + paragraph.children().to_vec(), + ); + paragraph.set_language("ko".to_owned()); + paragraph.set_korean_wrap(KoreanWrap::KeepWords); + let extras = + super::justify::horizontal_pairwise_justify_spacing(¶graph, &layout, &plan, 310.0); + let total: f32 = extras.iter().flatten().map(|(_, amount)| amount).sum(); + assert!((layout.longest_line() + total - 310.0).abs() < 0.1); + let gaps: Vec<_> = extras.iter().flatten().map(|(_, amount)| *amount).collect(); + assert_eq!(gaps.len(), 3); + assert!(gaps.windows(2).all(|p| (p[0] - p[1]).abs() < 0.001)); +} + +#[test] +fn distribution_keeps_graphemes_nonbreaking_pairs_and_ruby_bases_solid() { + use crate::shapes::text_policy::KoreanWrap; + init_state(); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some( + crate::shapes::text_vertical::test_support::provider(include_bytes!( + "../../fonts/cjk-locl-test.ttf" + )) + .into(), + )); + for (texts, expected_gaps) in [ + (vec!["ᄒ", "ᅡᆫ국"], 1), + (vec!["한\u{2060}", "글"], 0), + (vec!["한\u{00a0}글"], 0), + (vec!["한"], 0), + (vec![""], 0), + ] { + let spans = texts.iter().map(|text| make_span(text, 0.0)).collect(); + let mut paragraph = Paragraph::new( + TextAlign::Distribute, + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + spans, + ); + paragraph.set_language("ko".to_owned()); + paragraph.set_korean_wrap(KoreanWrap::KeepWords); + let (plan, layout) = cjk_layout_paragraph(¶graph, 310.0, &fonts); + let extras = + super::justify::horizontal_pairwise_justify_spacing(¶graph, &layout, &plan, 310.0); + assert_eq!( + extras.iter().map(Vec::len).sum::(), + expected_gaps, + "{texts:?}" + ); + } + let mut ruby = make_span("한글", 0.0); + crate::globals::init_headless(1, 1); + ruby.ruby = "hangeul".to_owned(); + let paragraph = Paragraph::new( + TextAlign::Distribute, + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + vec![ruby], + ); + let (plan, layout) = cjk_layout_paragraph(¶graph, 310.0, &fonts); + assert!( + super::justify::horizontal_pairwise_justify_spacing(¶graph, &layout, &plan, 310.0) + .iter() + .all(Vec::is_empty) + ); +} + +#[test] +fn chinese_proportional_punctuation_is_not_compressed_twice() { + init_state(); + let mut span = make_span(",。", 0.0); + span.font_features = FontFeatures::Palt; + let mut paragraph = make_paragraph(vec![span], 0.0); + paragraph.set_language("zh-Hans-CN".into()); + let plan = HorizontalParagraphPlan::new(¶graph); + assert!(plan.pair_spacing.iter().all(Vec::is_empty)); +} + +#[test] +fn untagged_cjk_keeps_plain_layout() { + init_state(); + for text in ["中文,。", "日本 hello world", "한국 漢字"] { + let paragraph = Paragraph::new( + TextAlign::Left, + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + vec![make_span(text, 0.0)], + ); + let (texts, offsets) = paragraph.layout_span_texts(); + assert_eq!(texts, vec![text.to_string()]); + assert!(offsets.is_empty()); + assert!(horizontal_pair_spacing(¶graph, &texts)[0].is_empty()); + } +} + +#[test] +fn explicit_profiles_use_distinct_rules_across_styles() { + init_state(); + let mut paragraph = make_paragraph( + vec![make_span("中A 文「", 0.0), make_span("字」っ", 0.0)], + 0.0, + ); + paragraph.set_language("zh-Hans".to_string()); + let (texts, map) = paragraph.layout_span_texts(); + assert_eq!(texts.concat(), "中A 文「\u{2060}字\u{2060}」っ"); + for offset in 0..="中A 文「字」っ".encode_utf16().count() { + assert_eq!(map.to_original(map.to_shifted(offset)), offset); + } + let plan = HorizontalParagraphPlan::new(¶graph); + assert!(plan.pair_spacing.iter().all(Vec::is_empty)); + + let mut paragraph = + make_paragraph(vec![make_span("한", 0.0), make_span("국어 문장", 0.0)], 0.0); + paragraph.set_language("ko".to_string()); + assert_eq!(paragraph.layout_span_texts().0.concat(), "한국어 문장"); + paragraph.set_korean_wrap(crate::shapes::text_policy::KoreanWrap::SpacesOnly); + assert_eq!( + paragraph.layout_span_texts().0.concat(), + "한\u{2060}국\u{2060}어 문\u{2060}장" + ); +} + +#[test] +fn korean_joiners_stay_outside_decomposed_syllables_and_emoji() { + init_state(); + let mut paragraph = make_paragraph(vec![make_span("ᄒ", 0.0), make_span("ᅡᆫ국 👩‍💻", 0.0)], 0.0); + paragraph.set_language("ko".to_string()); + paragraph.set_korean_wrap(crate::shapes::text_policy::KoreanWrap::SpacesOnly); + assert_eq!(paragraph.layout_span_texts().0.concat(), "한\u{2060}국 👩‍💻"); +} + +#[test] +fn same_paragraph_wraps_differently_by_profile_in_both_orientations() { + use crate::shapes::text_vertical::test_support::{ + content_of, layout_with_height, provider, EM, + }; + use crate::shapes::GrowType; + + init_state(); + let font_provider = provider(include_bytes!("../../fonts/cjk-locl-test.ttf")); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some(font_provider.clone().into())); + let mut horizontal_ends = Vec::new(); + let mut vertical_ends = Vec::new(); + for language in ["ja", "zh-Hans", "ko"] { + let mut span = make_span("文文 あっ文文", 0.0); + span.font_size = EM; + let mut paragraph = make_paragraph(vec![span], 0.0); + paragraph.set_language(language.to_string()); + paragraph.set_korean_wrap(crate::shapes::text_policy::KoreanWrap::SpacesOnly); + let plan = HorizontalParagraphPlan::new(¶graph); + let content = content_of(vec![paragraph.clone()], EM * 5.45, GrowType::Fixed); + let span = ¶graph.children()[0]; + let style = span.to_style( + &content.bounds(), + &crate::render::FallbackFonts::default(), + false, + 1.0, + ); + let mut builder = ParagraphBuilder::new(¶graph.paragraph_to_style(), &fonts); + builder.push_style(&style); + add_horizontal_span( + &mut builder, + span, + &plan.texts[0], + &plan.pair_spacing[0], + &[], + &style, + &fonts, + ); + let mut horizontal = LayoutParagraph::build(&mut builder); + horizontal.layout(EM * 5.45); + let (_, offset_map) = paragraph.layout_span_texts(); + horizontal_ends.push(offset_map.to_original(horizontal.get_line_metrics()[0].end_index)); + let vertical = layout_with_height(&font_provider, &content, EM * 5.45); + vertical_ends.push( + vertical + .cells + .iter() + .filter(|cell| cell.column == 0) + .map(|cell| cell.end) + .max() + .unwrap(), + ); + } + assert_eq!(horizontal_ends, vec![6, 5, 3]); + assert_eq!(vertical_ends, vec![6, 5, 3]); +} + +#[test] +fn horizontal_ruby_ranges_use_utf16_across_spans() { + init_state(); + let mut astral = make_span("𠀀", 0.0); + astral.ruby = "あ".to_string(); + let mut kanji = make_span("漢", 0.0); + kanji.ruby = "かん".to_string(); + let paragraph = make_paragraph(vec![astral, kanji], 0.0); + let offsets = HorizontalOffsets::new(¶graph); + + assert_eq!( + horizontal_ruby_targets(¶graph, &offsets), + vec![(0, 0..2), (1, 2..3)] + ); +} + +#[test] +fn split_counts_by_extent_drops_no_glyph() { + assert_eq!(split_counts_by_extent(&[60.0, 40.0], 5), vec![3, 2]); + assert_eq!(split_counts_by_extent(&[100.0], 4), vec![4]); + assert_eq!(split_counts_by_extent(&[0.0, 0.0], 3), vec![0, 3]); + assert_eq!( + split_counts_by_extent(&[1.0, 1.0, 1.0], 2) + .iter() + .sum::(), + 2 + ); + assert!(split_counts_by_extent(&[], 3).is_empty()); +} + +// apply_text_transform reads the browser from the design state. +fn init_state() { + crate::globals::design_init(); +} + +fn make_span(text: &str, letter_spacing: f32) -> TextSpan { + TextSpan { + text: text.to_string(), + font_size: 16.0, + line_height: 1.0, + letter_spacing, + ..TextSpan::default() + } +} + +fn make_paragraph(spans: Vec, letter_spacing: f32) -> Paragraph { + let mut paragraph = Paragraph::new( + TextAlign::default(), + TextDirection::LTR, + None, + None, + 1.0, + letter_spacing, + spans, + ); + paragraph.set_language("ja".to_string()); + paragraph +} + +#[test] +fn proportional_line_spacing_preserves_fractional_baselines() { + use crate::shapes::text_vertical::test_support::provider; + init_state(); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some( + provider(include_bytes!("../../fonts/cjk-locl-test.ttf")).into(), + )); + for size in [18.0, 36.0] { + let mut span = make_span("한글\n한글\n한글", 0.0); + span.font_size = size; + let paragraph = Paragraph::new( + TextAlign::Left, + TextDirection::LTR, + None, + None, + 1.6, + 0.0, + vec![span], + ); + let mut builder = ParagraphBuilder::new(¶graph.paragraph_to_style(), &fonts); + let style = paragraph.children()[0].to_style( + &crate::math::Rect::from_xywh(0.0, 0.0, 300.0, 300.0), + &crate::render::FallbackFonts::default(), + false, + paragraph.line_height(), + ); + builder.push_style(&style); + builder.add_text(¶graph.children()[0].text); + let mut layout = LayoutParagraph::build(&mut builder); + layout.layout(300.0); + let lines = layout.get_line_metrics(); + assert_eq!(lines.len(), 3); + for pair in lines.windows(2) { + let step = (pair[1].baseline - pair[0].baseline) as f32; + assert!( + (step - size * 1.6).abs() < 0.001, + "{size}px: {step}, lines: {lines:?}" + ); + } + } +} + +#[test] +fn layout_span_texts_applies_kinsoku() { + init_state(); + let paragraph = make_paragraph(vec![make_span("雪国", 0.0), make_span("。です", 0.0)], 0.0); + let (texts, map) = paragraph.layout_span_texts(); + assert_eq!( + texts, + vec!["雪国".to_string(), "\u{2060}。です".to_string()] + ); + assert!(!map.is_empty()); + assert_eq!(map.to_original(3), 2); +} + +#[test] +fn layout_span_texts_applies_kinsoku_under_paragraph_letter_spacing() { + init_state(); + let paragraph = make_paragraph(vec![make_span("雪国。", 0.0)], 2.0); + let (texts, _) = paragraph.layout_span_texts(); + assert_eq!(texts, vec!["雪国\u{2060}。".to_string()]); +} + +#[test] +fn layout_span_texts_applies_kinsoku_under_span_letter_spacing() { + init_state(); + let paragraph = make_paragraph(vec![make_span("雪国。", 1.5)], 0.0); + let (texts, _) = paragraph.layout_span_texts(); + assert_eq!(texts, vec!["雪国\u{2060}。".to_string()]); +} + +#[test] +fn inserted_joiners_take_no_letter_spacing() { + init_state(); + let measure = |text: &str| { + let span = make_span(text, 5.0); + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(span.font_size); + style.set_letter_spacing(span.letter_spacing); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), &fonts); + builder.push_style(&style); + add_horizontal_span(&mut builder, &span, text, &[], &[], &style, &fonts); + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(f32::MAX); + laid_out.longest_line() + }; + assert!((measure("A\u{2060}B") - measure("AB")).abs() < 0.01); +} + +#[test] +fn layout_span_texts_respects_text_transform() { + init_state(); + let mut span = make_span("hello。", 0.0); + span.text_transform = Some(TextTransform::Uppercase); + let paragraph = make_paragraph(vec![span], 0.0); + let (texts, _) = paragraph.layout_span_texts(); + assert_eq!(texts, vec!["HELLO\u{2060}。".to_string()]); +} + +#[test] +fn layout_span_texts_identity_map_for_plain_text() { + init_state(); + let paragraph = make_paragraph(vec![make_span("helloworld", 0.0)], 0.0); + let (texts, map) = paragraph.layout_span_texts(); + assert_eq!(texts, vec!["helloworld".to_string()]); + assert!(map.is_empty()); + assert_eq!(map.to_original(5), 5); + assert_eq!(map.to_shifted(5), 5); +} + +#[test] +fn layout_span_texts_leaves_text_without_japanese_unchanged() { + init_state(); + let paragraph = make_paragraph(vec![make_span("hello world foo", 0.0)], 0.0); + let (texts, map) = paragraph.layout_span_texts(); + assert_eq!(texts, vec!["hello world foo".to_string()]); + assert!(map.is_empty()); +} + +#[test] +fn layout_span_texts_sets_western_spaces_inside_japanese_text() { + init_state(); + let paragraph = make_paragraph(vec![make_span("日本 hello world", 0.0)], 0.0); + let (texts, _) = paragraph.layout_span_texts(); + assert_eq!( + texts, + vec![format!( + "日本{}hello{}world", + line_breaks::WESTERN_WORD_SPACE, + line_breaks::WESTERN_WORD_SPACE + )] + ); +} + +#[test] +fn layout_span_texts_keeps_ruby_atomic_without_japanese_text() { + init_state(); + let mut group = make_span("ab", 0.0); + group.ruby = "x".to_string(); + let paragraph = make_paragraph(vec![group], 0.0); + assert_eq!( + paragraph.layout_span_texts().0, + vec!["a\u{2060}b".to_string()] + ); +} + +fn test_ruby_spacing(paragraph: &Paragraph) -> HorizontalRubySpacing { + let (texts, offset_map) = paragraph.layout_span_texts(); + let provider = skia::textlayout::TypefaceFontProvider::new(); + let typeface = FontMgr::new() + .new_from_data( + skia_safe::Data::new_copy(include_bytes!("../../fonts/sourcesanspro-regular.ttf")), + None, + ) + .expect("test font"); + let mut provider = provider; + provider.register_typeface(typeface, Some("sourcesanspro")); + horizontal_ruby_spacing_with( + paragraph, + &texts, + &offset_map, + &provider, + &crate::render::FallbackFonts::from_families(["sourcesanspro".to_string()]), + ) +} + +#[test] +fn horizontal_ruby_overhang_room_covers_kana_neighbours_only() { + init_state(); + let mut base = make_span("字", 0.0); + base.ruby = "じじじ".to_string(); + let paragraph = make_paragraph(vec![make_span("か", 0.0), base, make_span("漢", 0.0)], 0.0); + let spacing = test_ruby_spacing(¶graph); + assert_eq!(spacing.rooms[1], (8.0, 0.0)); +} + +#[test] +fn horizontal_long_ruby_spreads_its_base_without_overhang() { + init_state(); + let mut base = make_span("i", 0.0); + base.ruby = "MMMMMMMM".to_string(); + base.ruby_overhang = RubyOverhang::None; + let paragraph = make_paragraph(vec![make_span("a", 0.0), base, make_span("b", 0.0)], 0.0); + let spacing = test_ruby_spacing(¶graph); + let gap = spacing.base_gaps[1]; + assert!(gap > 0.0, "the base grows under a long reading"); + assert_eq!(spacing.adjustments[1], vec![(0, gap / 2.0)]); + assert_eq!(spacing.adjustments[0], vec![(0, gap / 2.0)]); + assert_eq!(spacing.leading_gaps[1], gap / 2.0); +} + +#[test] +fn horizontal_ruby_base_at_paragraph_start_takes_all_spacing_after() { + init_state(); + let mut base = make_span("i", 0.0); + base.ruby = "MMMMMMMM".to_string(); + base.ruby_overhang = RubyOverhang::None; + let paragraph = make_paragraph(vec![base, make_span("b", 0.0)], 0.0); + let spacing = test_ruby_spacing(¶graph); + let gap = spacing.base_gaps[0]; + assert!(gap > 0.0); + assert_eq!( + spacing.leading_gaps[0], 0.0, + "nothing sticks out before the line start" + ); + assert_eq!(spacing.adjustments[0], vec![(0, gap / 2.0), (0, gap / 2.0)]); +} + +#[test] +fn horizontal_long_ruby_counts_only_source_characters_of_its_base() { + init_state(); + let mut base = make_span("第3", 0.0); + base.ruby = "MMMMMMMMMMMM".to_string(); + base.ruby_overhang = RubyOverhang::None; + let paragraph = make_paragraph(vec![base], 0.0); + let (texts, _) = paragraph.layout_span_texts(); + assert!( + texts[0].chars().count() > 2, + "the layout text has inserted characters" + ); + let last = texts[0].chars().count() - 1; + let spacing = test_ruby_spacing(¶graph); + let gap = spacing.base_gaps[0]; + assert_eq!( + spacing.adjustments[0], + vec![(0, gap), (last, gap / 2.0), (last, gap / 2.0)] + ); +} + +#[test] +fn horizontal_short_ruby_adds_no_spacing() { + init_state(); + let mut base = make_span("MMMM", 0.0); + base.ruby = "i".to_string(); + let paragraph = make_paragraph(vec![base], 0.0); + let spacing = test_ruby_spacing(¶graph); + assert_eq!(spacing.base_gaps, vec![0.0]); + assert!(spacing.adjustments[0].is_empty()); +} + +#[test] +fn annotation_room_follows_ruby_size_and_clearance() { + let mut span = make_span("漢字", 0.0); + span.ruby = "かんじ".to_string(); + assert_eq!(span.annotation_room_em(), 0.0, "none keeps the line height"); + span.annotation_clearance = AnnotationClearance::Auto; + span.ruby_size = RubySize::Quarter; + assert_eq!(span.annotation_room_em(), 0.25); + span.text_emphasis = TextEmphasis::FilledDot; + assert_eq!(span.annotation_room_em(), 0.75); +} + +#[test] +fn horizontal_plans_are_cached_until_content_or_width_changes() { + init_state(); + let mut content = super::super::text::TextContent::new( + crate::math::Rect::from_xywh(0.0, 0.0, 200.0, 40.0), + crate::shapes::GrowType::Fixed, + ); + content.add_paragraph(make_paragraph(vec![make_span("雪国。", 0.0)], 0.0)); + let first = content.horizontal_plans(); + + assert!(std::rc::Rc::ptr_eq(&first, &content.horizontal_plans())); + assert!( + std::rc::Rc::ptr_eq(&first, &content.clone().horizontal_plans()), + "clones share the cached plans" + ); + assert_eq!(first[0].texts, vec!["雪国\u{2060}。".to_string()]); + + content.paragraphs_mut()[0].children_mut()[0].text = "です".to_string(); + let edited = content.horizontal_plans(); + assert!(!std::rc::Rc::ptr_eq(&first, &edited)); + assert_eq!(edited[0].texts, vec!["です".to_string()]); + + content.set_xywh(10.0, 20.0, 200.0, 80.0); + assert!(std::rc::Rc::ptr_eq(&edited, &content.horizontal_plans())); + content.set_xywh(10.0, 20.0, 100.0, 80.0); + let resized = content.horizontal_plans(); + assert!(!std::rc::Rc::ptr_eq(&edited, &resized)); + assert!(std::rc::Rc::ptr_eq(&resized, &content.horizontal_plans())); +} + +#[test] +fn horizontal_annotations_can_paint_from_the_layout_cache() { + let content_with = |span: TextSpan| { + let mut content = super::super::text::TextContent::new( + crate::math::Rect::from_xywh(0.0, 0.0, 200.0, 40.0), + crate::shapes::GrowType::Fixed, + ); + content.add_paragraph(make_paragraph(vec![span], 0.0)); + content + }; + let mut ruby = make_span("漢字", 0.0); + ruby.ruby = "かんじ".to_string(); + let mut emphasis = make_span("強調", 0.0); + emphasis.text_emphasis = TextEmphasis::FilledDot; + let mut warichu = make_span("割注入り", 0.0); + warichu.warichu = true; + for span in [ruby, emphasis, warichu] { + assert!(content_with(span).can_paint_from_layout_cache()); + } + + let mut vertical = content_with(make_span("縦", 0.0)); + vertical.paragraphs_mut()[0].set_writing_mode(WritingMode::VerticalRl); + assert!(!vertical.can_paint_from_layout_cache()); +} + +#[test] +fn emoji_detection_ignores_japanese_text() { + let content_with = |text: &str| { + let mut content = super::super::text::TextContent::new( + crate::math::Rect::from_xywh(0.0, 0.0, 200.0, 40.0), + crate::shapes::GrowType::Fixed, + ); + content.add_paragraph(make_paragraph(vec![make_span(text, 0.0)], 0.0)); + content + }; + assert!(!content_with("日本語のテキスト、「括弧」").has_emoji()); + assert!(content_with("あ😀").has_emoji()); + assert!(content_with("#\u{FE0F}\u{20E3}").has_emoji()); + assert!(content_with("©\u{FE0F}").has_emoji()); +} + +#[test] +fn horizontal_ruby_is_atomic() { + init_state(); + let mut group = make_span("日本", 0.0); + group.ruby = "にほん".to_string(); + let paragraph = make_paragraph(vec![group], 0.0); + assert_eq!( + paragraph.layout_span_texts().0, + vec!["日\u{2060}本".to_string()] + ); +} + +#[test] +fn horizontal_warichu_collapses_to_one_builder_position() { + init_state(); + let mut warichu = make_span("割注入り", 0.0); + warichu.warichu = true; + let paragraph = make_paragraph(vec![warichu, make_span("後", 0.0)], 0.0); + + let ranges = horizontal_span_ranges(¶graph); + assert_eq!(ranges[0].builder_start..ranges[0].builder_end, 0..3); + assert_eq!(ranges[1].builder_start..ranges[1].builder_end, 3..4); + assert_eq!(HorizontalOffsets::new(¶graph).source_to_builder(2), 0); + assert_eq!(HorizontalOffsets::new(¶graph).source_to_builder(4), 3); + assert_eq!(HorizontalOffsets::new(¶graph).source_to_builder(5), 4); + assert_eq!(horizontal_builder_to_source(¶graph, 1), 4); + assert_eq!(horizontal_builder_to_source(¶graph, 2), 4); + assert_eq!(horizontal_builder_to_source(¶graph, 3), 4); + assert_eq!(horizontal_builder_to_source(¶graph, 4), 5); + assert_eq!( + HorizontalOffsets::new(¶graph).normal_selection_ranges(¶graph, 1, 5), + vec![3..4] + ); +} + +#[test] +fn horizontal_builder_mapping_preserves_non_bmp_boundaries() { + init_state(); + let paragraph = make_paragraph(vec![make_span("😀A", 0.0)], 0.0); + + assert_eq!(HorizontalOffsets::new(¶graph).source_to_builder(1), 2); + assert_eq!(horizontal_builder_to_source(¶graph, 2), 1); + assert_eq!(HorizontalOffsets::new(¶graph).source_to_builder(2), 3); + assert_eq!(horizontal_builder_to_source(¶graph, 3), 2); +} + +#[test] +fn horizontal_warichu_builder_emits_one_styled_placeholder() { + init_state(); + let mut span = make_span("割注入り", 0.0); + span.warichu = true; + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(span.font_size); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), &fonts); + builder.push_style(&style); + add_horizontal_span(&mut builder, &span, &span.text, &[], &[], &style, &fonts); + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(200.0); + + let placeholders = laid_out.get_rects_for_placeholders(); + assert_eq!(placeholders.len(), 1); + assert!(placeholders[0].rect.width() > 0.0); + assert!(placeholders[0].rect.height() > 0.0); + let has_style = laid_out + .get_line_metrics() + .iter() + .any(|line| !line.get_style_metrics(3..5).is_empty()); + assert!( + has_style, + "the paint pass must recover the placeholder style" + ); +} + +#[test] +fn horizontal_warichu_allows_wrapping_after_the_atomic_box() { + init_state(); + let mut span = make_span("割注入り", 0.0); + span.warichu = true; + let following = make_span("A", 0.0); + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(span.font_size); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), &fonts); + builder.push_style(&style); + add_horizontal_span(&mut builder, &span, &span.text, &[], &[], &style, &fonts); + builder.push_style(&style); + builder.add_text(&following.text); + + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(16.1); + + assert_eq!(laid_out.get_rects_for_placeholders().len(), 1); + assert_eq!(laid_out.get_line_metrics().len(), 2); +} + +#[test] +fn warichu_sub_lines_paint_on_one_line() { + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + for (text, size, letter_spacing) in [ + ("書き下ろし", 13.0, 0.75), + ("浅煎り・", 13.0, 0.0), + ("税込・", 53.0, -1.5), + ("架空の", 7.5, 0.0), + ] { + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(size); + style.set_height(1.0); + style.set_height_override(true); + style.set_letter_spacing(letter_spacing); + let paragraph = mini_paragraph(text, &style, &fonts); + assert_eq!(paragraph.line_number(), 1, "{text:?} must not wrap"); + } +} + +#[test] +fn horizontal_pair_spacing_follow_punctuation_pairs() { + init_state(); + let texts = vec!["あ。」い、".to_string(), "「「う".to_string()]; + let paragraph = make_paragraph(texts.iter().map(|text| make_span(text, 0.0)).collect(), 0.0); + assert_eq!( + horizontal_pair_spacing(¶graph, &texts), + vec![vec![(1, 8.0), (4, 8.0)], vec![(0, 8.0)]], + "。 before 」, 、 before 「 across spans, and 「 before 「" + ); +} + +#[test] +fn leading_aki_shed_uses_the_bracket_font_size() { + init_state(); + let texts = vec!["「".to_string(), "「う".to_string()]; + let mut large = make_span("「う", 0.0); + large.font_size = 32.0; + let paragraph = make_paragraph(vec![make_span("「", 0.0), large], 0.0); + assert_eq!( + horizontal_pair_spacing(¶graph, &texts), + vec![vec![(0, 16.0)], vec![]], + "the 16px 「 gives up half of the next 32px bracket's em" + ); +} + +#[test] +fn horizontal_pair_spacing_skip_curly_quotes() { + init_state(); + for text in ["“‘Hi’”", "あ“い”。", "あ‘い’、"] { + let texts = vec![text.to_string()]; + let paragraph = make_paragraph(vec![make_span(text, 0.0)], 0.0); + assert_eq!( + horizontal_pair_spacing(¶graph, &texts), + vec![Vec::<(usize, f32)>::new()], + "no aki shed in {text:?}" + ); + } +} + +#[test] +fn horizontal_pair_spacing_skip_word_joiners_and_palt() { + init_state(); + let joined = vec!["。\u{2060}」".to_string()]; + let paragraph = make_paragraph(vec![make_span(&joined[0], 0.0)], 0.0); + assert_eq!( + horizontal_pair_spacing(¶graph, &joined), + vec![vec![(0, 8.0)]] + ); + + let mut palt = make_span("。」", 0.0); + palt.font_features = FontFeatures::Palt; + let paragraph = make_paragraph(vec![palt], 0.0); + assert_eq!( + horizontal_pair_spacing(¶graph, &["。」".to_string()]), + vec![Vec::<(usize, f32)>::new()] + ); +} + +#[test] +fn horizontal_shed_sets_the_closing_mark_half_an_em_narrower() { + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let width = |span: TextSpan| { + let mut content = super::super::text::TextContent::new( + crate::math::Rect::from_xywh(0.0, 0.0, 400.0, 100.0), + crate::shapes::GrowType::Fixed, + ); + content.add_paragraph(make_paragraph(vec![span], 0.0)); + let mut groups = content.paragraph_builder_group_from_text(None); + let mut paragraph = groups[0][0].build(); + paragraph.layout(1000.0); + paragraph.max_intrinsic_width() + }; + let mut palt = make_span("。」", 0.0); + palt.font_features = FontFeatures::Palt; + let solid = width(make_span("。」", 0.0)); + let spaced = width(palt); + assert!( + (spaced - solid - 8.0).abs() < 0.01, + "the period sheds half of its 16px em: {spaced} vs {solid}" + ); +} + +#[test] +fn horizontal_ruby_targets_use_builder_offsets() { + init_state(); + let mut note = make_span("割注入り", 0.0); + note.warichu = true; + let mut base = make_span("漢字", 0.0); + base.ruby = "かんじ".to_string(); + let paragraph = make_paragraph(vec![note, base], 0.0); + let offsets = HorizontalOffsets::new(¶graph); + assert_eq!( + horizontal_ruby_targets(¶graph, &offsets), + vec![(1, 3..6)], + "the warichu span takes three builder units; 漢\u{2060}字 carries a joiner" + ); +} + +#[test] +fn horizontal_ruby_paint_follows_the_pass_paint() { + init_state(); + let mut base = make_span("漢字", 0.0); + base.ruby = "かんじ".to_string(); + let paragraph = make_paragraph(vec![base.clone()], 0.0); + let mut stroke = skia::Paint::default(); + stroke.set_style(skia::PaintStyle::Stroke); + stroke.set_color(skia::Color::RED); + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(base.font_size); + style.set_foreground_paint(&stroke); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), &fonts); + builder.push_style(&style); + add_horizontal_span(&mut builder, &base, &base.text, &[], &[], &style, &fonts); + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(400.0); + + let offsets = HorizontalOffsets::new(¶graph); + let paint = horizontal_ruby_paint(&laid_out, &offsets.ranges[0], skia::Paint::default()); + assert_eq!(paint.style(), skia::PaintStyle::Stroke); + assert_eq!(paint.color(), skia::Color::RED); +} + +#[test] +fn horizontal_position_data_skips_tab_placeholders_before_warichu() { + init_state(); + let mut warichu = make_span("割注入り", 0.0); + warichu.warichu = true; + let mut content = super::super::text::TextContent::new( + crate::math::Rect::from_xywh(0.0, 0.0, 400.0, 100.0), + crate::shapes::GrowType::Fixed, + ); + content.add_paragraph(make_paragraph(vec![make_span("A\tB", 0.0), warichu], 0.0)); + let mut shape = crate::shapes::Shape::new(Uuid::nil()); + shape.set_selrect(0.0, 0.0, 400.0, 100.0); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + + let data = super::super::text::calculate_position_data(&shape, &content, false); + + let text_right = data + .iter() + .filter(|entry| entry.span == 0) + .map(|entry| entry.x + entry.width) + .fold(0.0f32, f32::max); + let warichu_left = data + .iter() + .filter(|entry| entry.span == 1) + .map(|entry| entry.x) + .fold(f32::MAX, f32::min); + assert!( + warichu_left >= text_right - 0.5, + "the warichu strips start at {warichu_left}, inside A\tB which ends at {text_right}" + ); +} + +#[test] +fn horizontal_position_data_carries_warichu_lines_and_emphasis_marks() { + init_state(); + let mut emphasized = make_span("A、B", 0.0); + emphasized.text_emphasis = TextEmphasis::FilledDot; + let mut warichu = make_span("割注入り", 0.0); + warichu.warichu = true; + let mut content = super::super::text::TextContent::new( + crate::math::Rect::from_xywh(0.0, 0.0, 400.0, 100.0), + crate::shapes::GrowType::Fixed, + ); + content.add_paragraph(make_paragraph(vec![emphasized, warichu], 0.0)); + let mut shape = crate::shapes::Shape::new(Uuid::nil()); + shape.set_selrect(0.0, 0.0, 400.0, 100.0); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + + let data = super::super::text::calculate_position_data(&shape, &content, false); + + let warichu_lines: Vec<(u32, u32)> = data + .iter() + .filter(|entry| entry.span == 1) + .map(|entry| (entry.start_pos, entry.end_pos)) + .collect(); + assert_eq!(warichu_lines, vec![(0, 2), (2, 4)]); + let marks: Vec<(u32, u32)> = data + .iter() + .filter(|entry| entry.direction == crate::shapes::DIRECTION_EMPHASIS_MARK) + .map(|entry| (entry.start_pos, entry.end_pos)) + .collect(); + assert_eq!(marks, vec![(0, 1), (2, 3)], "A and B are marked; 、 is not"); +} + +#[test] +fn horizontal_emphasis_mark_stacks_outside_auto_clearance_ruby() { + let placement = HorizontalEmphasisPlacement { + span: 0, + range: 0..1, + mark: '•', + rect: skia::Rect::from_xywh(10.0, 40.0, 16.0, 20.0), + em_top: 40.0, + }; + let plain = make_span("漢", 0.0); + let mut stacked = make_span("漢", 0.0); + stacked.ruby = "かん".to_string(); + stacked.annotation_clearance = AnnotationClearance::Auto; + + let size = plain.font_size * EMPHASIS_FONT_SCALE; + let ink = skia::Rect::from_wh(size, size); + let plain_box = horizontal_emphasis_mark_box(&plain, &placement, &ink); + let stacked_box = horizontal_emphasis_mark_box(&stacked, &placement, &ink); + + assert_eq!(plain_box.width(), plain.font_size * EMPHASIS_FONT_SCALE); + assert_eq!(plain_box.center_x(), placement.rect.center_x()); + assert_eq!( + stacked_box.bottom, + plain_box.bottom - stacked.ruby_font_size(), + "the mark sits outside the ruby layer" + ); +} + +#[test] +fn horizontal_emphasis_tracks_eligible_unicode_characters() { + init_state(); + let mut span = make_span("A😀。 B", 0.0); + span.text_emphasis = TextEmphasis::FilledDot; + let paragraph = make_paragraph(vec![span], 0.0); + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(16.0); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), &fonts); + let (texts, _) = paragraph.layout_span_texts(); + for (span, text) in paragraph.children().iter().zip(texts) { + builder.push_style(&style); + add_horizontal_span(&mut builder, span, &text, &[], &[], &style, &fonts); + } + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(200.0); + + let placements = + horizontal_emphasis_placements(¶graph, &HorizontalOffsets::new(¶graph), &laid_out); + assert_eq!(placements.len(), 3, "A, emoji and B receive one mark each"); + assert!(placements + .iter() + .all(|placement| placement.rect.width() > 0.0)); + assert!(horizontal_span_style(&laid_out, &horizontal_span_ranges(¶graph)[0]).is_some()); +} + +#[test] +fn horizontal_emphasis_recovers_each_non_ascii_span_style() { + init_state(); + let mut first = make_span("漢", 0.0); + first.text_emphasis = TextEmphasis::FilledDot; + let mut second = make_span("字", 0.0); + second.text_emphasis = TextEmphasis::OpenCircle; + let paragraph = make_paragraph(vec![first, second], 0.0); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), &fonts); + let (texts, _) = paragraph.layout_span_texts(); + for (index, (span, text)) in paragraph.children().iter().zip(texts).enumerate() { + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(if index == 0 { 16.0 } else { 24.0 }); + builder.push_style(&style); + add_horizontal_span(&mut builder, span, &text, &[], &[], &style, &fonts); + } + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(200.0); + + let ranges = horizontal_span_ranges(¶graph); + assert_eq!( + horizontal_span_style(&laid_out, &ranges[0]) + .unwrap() + .font_size(), + 16.0 + ); + assert_eq!( + horizontal_span_style(&laid_out, &ranges[1]) + .unwrap() + .font_size(), + 24.0 + ); +} + +#[test] +fn horizontal_ruby_glyphs_share_one_baseline() { + // JLREQ Fig 123: ー in コーデックス has its ink mid-height. Attaching + // each glyph's own ink bottom to the base dropped it onto the base line; + // 「 here has the same shape problem as ー. + use crate::shapes::text_vertical::test_support::{provider, EM, VPAL_TEST_FONT}; + use crate::shapes::{FontFamily, FontStyle}; + use skia_safe::FontMgr; + + let font_provider = provider(VPAL_TEST_FONT); + let family = format!("{}", FontFamily::new(Uuid::nil(), 400, FontStyle::Normal)); + let shaped = shape_segment_with_fallbacks( + "あ「", + EM / 2.0, + &[family], + &font_provider, + false, + FontFeatures::None, + &FontMgr::from(font_provider.clone()), + "ja", + ); + let glyphs: Vec = shaped + .iter() + .enumerate() + .flat_map(|(run, shaped_run)| { + (0..shaped_run.glyphs.len()).map(move |g| (run, g, EM / 2.0, false)) + }) + .collect(); + assert_eq!(glyphs.len(), 2); + let span = TextSpan { + ruby: "あ「".to_string(), + ..make_span("漢字", 0.0) + }; + let base = skia::Rect::from_xywh(0.0, 0.0, EM * 2.0, EM); + let line = |glyphs: &[RubyGlyphRef]| { + RubyLine { + span: &span, + shaped: &shaped, + glyphs, + base, + em_top: base.top(), + leading_gap: 0.0, + room: (0.0, 0.0), + } + .baseline(0.0) + }; + + let both = line(&glyphs); + assert_eq!( + both, + line(&glyphs[..1]), + "the line sits where あ alone sits" + ); + let alone = line(&glyphs[1..]); + assert!( + alone > both + EM / 20.0, + "「 alone would have dropped onto the base: {alone} vs {both}" + ); +} + +#[test] +fn latin_readings_share_one_baseline_whatever_their_letters() { + // Pinyin over 友聊: the descender of "yu" lifted it above "lia" when each + // reading attached its own ink bottom to the base. + use crate::shapes::text_vertical::test_support::{provider, EM}; + use crate::shapes::{FontFamily, FontStyle}; + use skia_safe::FontMgr; + + let font_provider = provider(include_bytes!("../../fonts/sourcesanspro-regular.ttf")); + let family = format!("{}", FontFamily::new(Uuid::nil(), 400, FontStyle::Normal)); + let size = EM / 2.0; + let shape = |text: &str| { + shape_segment_with_fallbacks( + text, + size, + std::slice::from_ref(&family), + &font_provider, + false, + FontFeatures::None, + &FontMgr::from(font_provider.clone()), + "zh-Hans", + ) + }; + let base = skia::Rect::from_xywh(0.0, 0.0, EM, EM); + for side in [AnnotationSide::Over, AnnotationSide::Under] { + let baseline = |text: &str| { + let span = TextSpan { + ruby: text.to_string(), + ruby_side: side, + font_size: EM, + ..make_span("友", 0.0) + }; + let shaped = shape(text); + let glyphs = ruby_glyph_refs(text, &shaped, size); + assert!(glyphs.iter().all(|(_, _, _, western)| *western)); + let line = RubyLine { + span: &span, + shaped: &shaped, + glyphs: &glyphs, + base, + em_top: base.top(), + leading_gap: 0.0, + room: (0.0, 0.0), + }; + (line.baseline(0.0), shaped) + }; + let (descender, shaped) = baseline("yu"); + let (plain, _) = baseline("lia"); + assert!( + (descender - plain).abs() < 0.01, + "{side:?}: yu at {descender}, lia at {plain}" + ); + if side == AnnotationSide::Over { + let mut bounds = [skia::Rect::default()]; + shaped[0] + .font + .get_bounds(&shaped[0].glyphs[..1], &mut bounds, None); + assert!( + descender + bounds[0].bottom <= base.top() + 0.01, + "the descender of y reaches into the base" + ); + } + } +} + +#[test] +fn emphasis_position_overrides_the_language_and_stacks_on_its_own_side() { + let placement = super::emphasis::HorizontalEmphasisPlacement { + span: 0, + range: 0..1, + mark: '•', + rect: skia::Rect::from_xywh(0.0, 20.0, 20.0, 20.0), + em_top: 20.0, + }; + for (language, position, under) in [ + ("ja", TextEmphasisPosition::Auto, false), + ("ko", TextEmphasisPosition::Auto, false), + ("", TextEmphasisPosition::Auto, false), + ("zh-Hant-TW", TextEmphasisPosition::Auto, true), + ("zh-Hans", TextEmphasisPosition::OverLeft, false), + ("ja", TextEmphasisPosition::UnderRight, true), + ] { + let mut span = make_span("中文", 0.0); + span.set_language( + language, + TextLayoutPolicy::for_language(language, Default::default()), + ); + span.text_emphasis = TextEmphasis::FilledDot; + span.text_emphasis_position = position; + span.annotation_clearance = AnnotationClearance::Auto; + let ink = skia::Rect::from_wh(8.0, 8.0); + let plain = super::emphasis::horizontal_emphasis_mark_box(&span, &placement, &ink); + assert_eq!(plain.top() >= placement.rect.bottom(), under); + span.ruby = "zhōng wén".to_string(); + for side in [AnnotationSide::Over, AnnotationSide::Under] { + span.ruby_side = side; + let stacked = super::emphasis::horizontal_emphasis_mark_box(&span, &placement, &ink); + let expected = if (side == AnnotationSide::Under) == under { + span.ruby_font_size() + } else { + 0.0 + }; + assert!(((stacked.top() - plain.top()).abs() - expected).abs() < 0.01); + } + } +} + +#[test] +fn chinese_underline_stays_below_the_base_ink() { + for position in [ + TextUnderlinePosition::Auto, + TextUnderlinePosition::Under, + TextUnderlinePosition::UnderRight, + ] { + let (mut paragraph, plan, layout) = cjk_horizontal_layout( + &["文"], + "zh-Hans-CN", + crate::shapes::text_policy::KoreanWrap::Syllable, + 200.0, + ); + let span = &mut paragraph.children_mut()[0]; + span.text_decoration = Some(skia::textlayout::TextDecoration::UNDERLINE); + span.text_underline_position = position; + let segments = + crate::render::text::decoration_segments(&layout, ¶graph, &plan.offsets, 0.0, 0.0); + assert_eq!(segments.len(), 1); + let bottom = + layout.get_rects_for_range(0..1, RectHeightStyle::Tight, RectWidthStyle::Tight)[0] + .rect + .bottom(); + assert!(segments[0].y - segments[0].thickness / 2.0 >= bottom); + } +} + +#[test] +fn cjk_underline_takes_the_paint_of_its_own_span() { + use crate::shapes::text_vertical::test_support::{provider, EM}; + init_state(); + let provider = provider(include_bytes!("../../fonts/cjk-locl-test.ttf")); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some(provider.into())); + let dark = skia::Color::from_rgb(0x15, 0x10, 0x35); + let pink = skia::Color::from_rgb(0xf2, 0x3a, 0xe5); + let spans = [ + ("文文", dark, false), + ("文文", pink, true), + ("文", dark, false), + ] + .into_iter() + .map(|(text, color, underline)| { + let mut span = make_span(text, 0.0); + span.font_size = EM; + span.fills = vec![crate::shapes::Fill::Solid(crate::shapes::SolidColor(color))]; + if underline { + span.text_decoration = Some(skia::textlayout::TextDecoration::UNDERLINE); + } + span + }) + .collect(); + let mut paragraph = make_paragraph(spans, 0.0); + paragraph.set_language("zh-Hans-CN".to_owned()); + // Three ems per line: the underlined span starts mid-line and wraps. + let (plan, layout) = cjk_layout_paragraph(¶graph, EM * 3.0, &fonts); + assert_eq!(layout.get_line_metrics().len(), 2); + + let segments = + crate::render::text::decoration_segments(&layout, ¶graph, &plan.offsets, 0.0, 0.0); + assert_eq!(segments.len(), 2); + for segment in segments { + assert_eq!(segment.text_style.foreground().color(), pink); + } +} + +#[test] +fn below_emphasis_clears_an_underline_on_the_same_side() { + let span = TextSpan { + language: "zh-Hans".to_string(), + layout_policy: TextLayoutPolicy::for_language("zh-Hans", Default::default()), + text_emphasis: TextEmphasis::FilledDot, + text_decoration: Some(skia::textlayout::TextDecoration::UNDERLINE), + text_underline_position: TextUnderlinePosition::Under, + annotation_clearance: AnnotationClearance::Auto, + ..make_span("文", 0.0) + }; + let placement = super::emphasis::HorizontalEmphasisPlacement { + span: 0, + range: 0..1, + mark: '•', + rect: skia::Rect::from_xywh(0.0, 20.0, 16.0, 18.0), + em_top: 20.0, + }; + let ink = skia::Rect::from_wh(8.0, 8.0); + let mark = super::emphasis::horizontal_emphasis_mark_box(&span, &placement, &ink); + assert!(mark.top() > placement.rect.bottom() + span.font_size / 9.0); +} + +#[test] +fn chinese_horizontal_emphasis_defaults_below_the_base() { + let span = TextSpan { + language: "zh-Hans-CN".to_string(), + layout_policy: TextLayoutPolicy::for_language("zh-Hans-CN", Default::default()), + text_emphasis: TextEmphasis::FilledDot, + ..make_span("中文", 0.0) + }; + let placement = super::emphasis::HorizontalEmphasisPlacement { + span: 0, + range: 0..1, + mark: '•', + rect: skia::Rect::from_xywh(0.0, 20.0, 16.0, 18.0), + em_top: 20.0, + }; + let ink = skia::Rect::from_wh(8.0, 8.0); + let mark = super::emphasis::horizontal_emphasis_mark_box(&span, &placement, &ink); + assert!(mark.top() >= placement.rect.bottom()); +} + +#[test] +fn horizontal_pinyin_tones_keep_canonical_shaping_and_group_alignment() { + use crate::shapes::text_vertical::test_support::{provider, EM, PINYIN_TEST_FONT}; + use crate::shapes::{FontFamily, FontStyle}; + + let font_provider = provider(PINYIN_TEST_FONT); + let family = format!("{}", FontFamily::new(Uuid::nil(), 400, FontStyle::Normal)); + let mut shapes = Vec::new(); + for reading in ["nǐ hǎo", "ni\u{30c} ha\u{30c}o"] { + let shaped = shape_segment_with_fallbacks( + reading, + EM / 2.0, + std::slice::from_ref(&family), + &font_provider, + false, + FontFeatures::None, + &FontMgr::from(font_provider.clone()), + "zh-Hans-CN", + ); + assert_eq!( + shaped + .iter() + .map(|run| run.text.as_str()) + .collect::(), + reading + ); + let glyphs = ruby_glyph_refs(reading, &shaped, EM / 2.0); + assert!(glyphs.iter().all(|(_, _, _, western)| *western)); + assert!(shaped + .iter() + .flat_map(|run| &run.glyphs) + .all(|glyph| *glyph != 0)); + let span = TextSpan { + ruby: reading.to_string(), + ..make_span("你好", 0.0) + }; + let base = skia::Rect::from_xywh(0.0, 0.0, EM * 2.0, EM); + let lefts = RubyLine { + span: &span, + shaped: &shaped, + glyphs: &glyphs, + base, + em_top: base.top(), + leading_gap: 0.0, + room: (0.0, 0.0), + } + .glyph_lefts(); + for (index, pair) in lefts.windows(2).enumerate() { + assert!((pair[1] - pair[0] - glyphs[index].2).abs() < 0.01); + } + let width: f32 = glyphs.iter().map(|(_, _, advance, _)| advance).sum(); + assert!((lefts[0] - (base.width() - width) / 2.0).abs() < 0.01); + shapes.push(( + shaped + .iter() + .flat_map(|run| run.glyphs.clone()) + .collect::>(), + width, + )); + } + assert_eq!(shapes[0].0, shapes[1].0); + assert!((shapes[0].1 - shapes[1].1).abs() < 0.01); +} + +#[test] +fn horizontal_western_ruby_keeps_its_word_whole() { + // JLREQ Fig 112: "editor" over 編集者 is centred as one word, never + // spread letter by letter. + use crate::shapes::text_vertical::test_support::{provider, EM, TEST_FONT}; + use crate::shapes::{FontFamily, FontStyle}; + use skia_safe::FontMgr; + + let font_provider = provider(TEST_FONT); + let family = format!("{}", FontFamily::new(Uuid::nil(), 400, FontStyle::Normal)); + let shaped = shape_segment_with_fallbacks( + "editor", + EM / 2.0, + &[family], + &font_provider, + false, + FontFeatures::None, + &FontMgr::from(font_provider.clone()), + "ja", + ); + let glyphs = ruby_glyph_refs("editor", &shaped, EM / 2.0); + assert!(glyphs.iter().all(|(_, _, _, western)| *western)); + let span = TextSpan { + ruby: "editor".to_string(), + ..make_span("漢字字", 0.0) + }; + let base = skia::Rect::from_xywh(0.0, 0.0, EM * 3.0, EM); + let lefts = RubyLine { + span: &span, + shaped: &shaped, + glyphs: &glyphs, + base, + em_top: base.top(), + leading_gap: 0.0, + room: (0.0, 0.0), + } + .glyph_lefts(); + + for (index, pair) in lefts.windows(2).enumerate() { + assert!((pair[1] - pair[0] - glyphs[index].2).abs() < 0.01); + } + let word: f32 = glyphs.iter().map(|(_, _, advance, _)| advance).sum(); + let after = base.right() - (lefts[0] + word); + assert!( + (lefts[0] - after).abs() < 0.01, + "centred: {} vs {after}", + lefts[0] + ); +} + +#[test] +fn horizontal_readings_never_meet_over_a_shared_kana() { + init_state(); + // JLREQ Fig 138: に暁(あかつき)の趣(おもむき)を. The later reading may not + // hang over the の the earlier one already covers. The test face sets + // kana as narrow tofu, so the first reading is doubled to fill its room. + let paragraph = make_paragraph( + vec![ + make_span("に", 0.0), + TextSpan { + ruby: "あかつきあかつき".to_string(), + ..make_span("暁", 0.0) + }, + make_span("の", 0.0), + TextSpan { + ruby: "おもむき".to_string(), + ..make_span("趣", 0.0) + }, + make_span("を", 0.0), + ], + 0.0, + ); + let spacing = test_ruby_spacing(¶graph); + assert!(spacing.rooms[1].1 > 0.0, "あかつき may hang over の"); + assert_eq!(spacing.rooms[3].0, 0.0, "おもむき may not"); +} + +#[test] +fn horizontal_justify_spreads_every_line_evenly() { + // JLREQ Fig 90: SkParagraph expands only around kanji; our spacing fills + // each line but the last, with the same breaks, never inside a word. + init_state(); + let text = "あいうえおかきくABCけこさしすせそたちつてとなにぬねの"; + let paragraph = make_paragraph(vec![make_span(text, 0.0)], 0.0); + let plan = HorizontalParagraphPlan::new(¶graph); + let width = 16.0 * 7.6; + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(16.0); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + // As `paragraph_builders` sets it for justified Japanese paragraphs. + let mut paragraph_style = ParagraphStyle::default(); + paragraph_style.set_apply_rounding_hack(false); + let lay_out = |extra: &[(usize, f32)]| { + let mut builder = ParagraphBuilder::new(¶graph_style, &fonts); + builder.push_style(&style); + add_horizontal_span( + &mut builder, + ¶graph.children()[0], + &plan.texts[0], + &plan.pair_spacing[0], + extra, + &style, + &fonts, + ); + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(width); + laid_out + }; + + let natural = lay_out(&[]); + let spacing = horizontal_jlreq_justify_spacing(¶graph, &natural, &plan, width); + let justified = lay_out(&spacing[0]); + + let natural_lines = natural.get_line_metrics(); + let lines = justified.get_line_metrics(); + assert!(lines.len() > 2, "the text wraps"); + assert_eq!(lines.len(), natural_lines.len(), "same line count"); + for (line, before) in lines.iter().zip(&natural_lines) { + assert_eq!(line.start_index, before.start_index, "same breaks"); + } + let (last, full) = lines.split_last().unwrap(); + for line in full { + assert!( + (line.width as f32 - width).abs() < 0.2, + "line width {}", + line.width + ); + } + assert!((last.width - natural_lines.last().unwrap().width).abs() < 0.01); + + let chars: Vec = plan.texts[0].chars().collect(); + for (index, _) in &spacing[0] { + let pair = (chars[*index], chars.get(index + 1).copied().unwrap_or(' ')); + assert!( + !(pair.0.is_ascii_alphabetic() && pair.1.is_ascii_alphabetic()), + "no space inside ABC: {pair:?}" + ); + } +} + +#[test] +fn horizontal_squeeze_keeps_the_next_character_on_the_line() { + // JLREQ Fig 90: a line slightly too long squeezes its comma aki instead + // of moving its last character down. + init_state(); + let text = "あい、うえ、おかきくけこさしすせそ"; + let paragraph = make_paragraph(vec![make_span(text, 0.0)], 0.0); + let plan = HorizontalParagraphPlan::new(¶graph); + let mut style = skia::textlayout::TextStyle::default(); + style.set_font_size(16.0); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_default_font_manager(skia::FontMgr::new(), None); + let mut paragraph_style = ParagraphStyle::default(); + paragraph_style.set_apply_rounding_hack(false); + let advance = { + let mut builder = ParagraphBuilder::new(¶graph_style, &fonts); + builder.push_style(&style); + builder.add_text("あ"); + let mut probe = LayoutParagraph::build(&mut builder); + probe.layout(1000.0); + probe.max_intrinsic_width() + }; + // Seven characters fit; the eighth overflows by less than the 、 aki. + let width = advance * 7.6; + let lay_out = |extra: &[Vec<(usize, f32)>], window: std::ops::Range| { + let sheds: Vec<(usize, f32)> = plan.pair_spacing[0] + .iter() + .chain(extra.first().into_iter().flatten()) + .copied() + .collect(); + let (text, from) = + window_slice(&plan.texts[0], &plan.offsets.ranges[0], &window).unwrap_or(("", 0)); + let sheds = window_entries(&sheds, from, text.chars().count()); + let mut builder = ParagraphBuilder::new(¶graph_style, &fonts); + builder.push_style(&style); + add_horizontal_span( + &mut builder, + ¶graph.children()[0], + text, + &sheds, + &[], + &style, + &fonts, + ); + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(width); + laid_out + }; + + // Line ends count builder UTF-16 units, word joiners included. + let builder_text: Vec = plan.texts[0].chars().collect(); + let natural = lay_out(&[], FULL_WINDOW); + let natural_end = natural.get_line_metrics()[0].end_index; + assert_eq!( + builder_text[natural_end], 'か', + "the natural line ends before か" + ); + let (sheds, squeezed) = horizontal_squeeze_sheds( + ¶graph, + &plan, + width, + crate::shapes::LineAdjustment::PushInFirst, + &lay_out, + ); + + assert_eq!( + squeezed.get_line_metrics()[0].end_index, + natural_end + 1, + "か joins the line" + ); + let squeezed_chars: Vec = sheds[0].iter().map(|(index, _)| *index).collect(); + let commas: Vec = builder_text + .iter() + .enumerate() + .filter(|(_, ch)| **ch == '、') + .map(|(index, _)| index) + .collect(); + assert_eq!(squeezed_chars, commas, "both commas give up aki"); + assert!((sheds[0][0].1 - sheds[0][1].1).abs() < 0.01, "equally"); +} + +#[test] +fn horizontal_zhuyin_reserves_its_right_strip_without_changing_line_leading() { + init_state(); + for reading in ["ㄓ", "ㄋㄧˇ", "˙ㄉㄜ", "ㄓㄨㄤˋ"] { + let span = TextSpan { + ruby: reading.into(), + ruby_zhuyin: true, + annotation_clearance: AnnotationClearance::Auto, + ..make_span("中", 0.0) + }; + let paragraph = make_paragraph(vec![span.clone(), make_span("文", 0.0)], 0.0); + let spacing = test_ruby_spacing(¶graph); + assert_eq!(spacing.adjustments[0], vec![(0, span.font_size * 0.6)]); + assert_eq!(span.annotation_room_em(), 0.0); + let rect = skia::Rect::from_xywh(0.0, 0.0, span.font_size * 1.6, span.font_size); + let origin = horizontal_zhuyin_origin(&span, rect, 0.0); + assert!((origin.x - span.font_size).abs() < 0.001); + assert_eq!(origin.y, 0.0); + } +} + +#[test] +fn horizontal_annotations_attach_to_the_ideographic_em_box() { + use crate::shapes::text_vertical::test_support::{provider, EM, VMTX_TEST_FONT}; + init_state(); + // Noto metrics: hhea ascent/descent 1.16/0.288 em, em box 0.88/0.12 em. + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some(provider(VMTX_TEST_FONT).into())); + // The em box depends on the base alone; a reading would shape it through + // the global font resources. + let mut span = make_span("中", 0.0); + span.font_size = EM; + span.text_emphasis = TextEmphasis::FilledDot; + let paragraph = make_paragraph(vec![span], 0.0); + let (plan, layout) = cjk_layout_paragraph(¶graph, EM * 10.0, &fonts); + let range = &plan.offsets.ranges[0]; + let rect = layout.get_rects_for_range( + range.builder_start..range.builder_end, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + )[0] + .rect; + let lines = layout.get_line_metrics(); + let em_top = horizontal_base_em_top(&layout, &lines, range.builder_start, EM, rect); + let baseline = lines[0].baseline as f32; + assert!((em_top - (baseline - 0.88 * EM)).abs() < 0.01); + + let placements = horizontal_emphasis_placements(¶graph, &plan.offsets, &layout); + assert_eq!(placements.len(), 1); + assert!((placements[0].em_top - em_top).abs() < 0.01); +} + +#[test] +fn horizontal_indivisible_punctuation_unit_overflows_a_narrow_box() { + use crate::shapes::text_policy::KoreanWrap; + for language in ["zh-Hant-TW", "zh-Hans-CN", "zh-Hant-HK", "ko", "ja"] { + let (_, plan, layout) = + cjk_horizontal_layout(&["文", "。"], language, KoreanWrap::KeepWords, 20.0); + let starts: Vec<_> = layout + .get_line_metrics() + .iter() + .map(|line| plan.offsets.builder_to_source(line.start_index)) + .collect(); + assert_eq!( + starts, + vec![0], + "{language}: a closing mark must stay with its base" + ); + assert!( + layout.longest_line() > 20.0, + "the unit overflows rather than splitting" + ); + } +} + +#[test] +fn japanese_narrow_box_keeps_ruby_bases_whole_and_respects_kinsoku() { + use crate::shapes::jlreq::forbidden_at_line_start; + use crate::shapes::text_vertical::test_support::provider; + init_state(); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some( + provider(include_bytes!("../../fonts/cjk-locl-test.ttf")).into(), + )); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let mut base = make_span("文文", 0.0); + base.font_size = 20.0; + base.ruby = "ぶんぶん".to_string(); + let mut paragraph = make_paragraph( + vec![make_span("「", 0.0), base, make_span("」、文文。", 0.0)], + 0.0, + ); + paragraph.set_language("ja".to_string()); + let source: Vec = "「文文」、文文。".chars().collect(); + let (plan, layout) = cjk_layout_paragraph(¶graph, 20.0, &fonts); + let starts: Vec = layout + .get_line_metrics() + .iter() + .map(|line| plan.offsets.builder_to_source(line.start_index)) + .collect(); + for start in &starts { + assert!( + !forbidden_at_line_start(source[*start]), + "line {starts:?} starts with {}", + source[*start] + ); + } + let targets = horizontal_ruby_targets(¶graph, &plan.offsets); + assert_eq!(targets.len(), 1); + let rects = layout.get_rects_for_range( + targets[0].1.clone(), + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ); + assert!( + rects + .iter() + .all(|r| (r.rect.top() - rects[0].rect.top()).abs() < 0.01), + "the ruby base splits over lines {starts:?}" + ); +} + +#[test] +fn narrow_atomic_overflow_retains_normal_wrap_budget_and_explicit_newlines() { + use crate::shapes::text_policy::KoreanWrap; + for language in ["zh-Hant-TW", "zh-Hans-CN", "zh-Hant-HK", "ko"] { + for (texts, expected) in [ + (vec!["文文文", "。", "文文文"], vec![0, 1, 2, 4, 5, 6]), + (vec!["文\n", "文", "。", "\n文"], vec![0, 2, 5]), + (vec!["文(", "文)", "文文"], vec![0, 1, 4, 5]), + ] { + let (paragraph, plan, layout) = + cjk_horizontal_layout(&texts, language, KoreanWrap::KeepWords, 20.0); + let starts: Vec<_> = layout + .get_line_metrics() + .iter() + .map(|line| plan.offsets.builder_to_source(line.start_index)) + .collect(); + assert_eq!(starts, expected, "{language}: {texts:?}"); + for at in 0..=paragraph + .children() + .iter() + .map(|s| s.text.chars().count()) + .sum() + { + assert_eq!( + plan.offsets + .builder_to_source(plan.offsets.source_to_builder(at)), + at + ); + } + assert_eq!( + paragraph + .children() + .iter() + .map(|s| s.text.as_str()) + .collect::(), + texts.concat() + ); + } + } +} + +#[test] +fn narrow_atomic_units_preserve_controls_numeric_expressions_and_graphemes() { + use crate::shapes::text_policy::KoreanWrap; + for language in ["zh-Hant-TW", "zh-Hans-CN", "zh-Hant-HK", "ko"] { + for unit in [ + "文。", + "(文)", + "$1.5%", + "1℃", + "文\u{a0}文", + "文\u{2060}文", + "e\u{301}。", + "😀。", + "👩‍👩‍👧‍👦。", + ] { + for width in [1.0, 10.0, 20.0, 29.0, 39.0] { + let source = format!("文文{unit}文文"); + let (_, plan, layout) = + cjk_horizontal_layout(&[&source], language, KoreanWrap::KeepWords, width); + let end = 2 + unit.chars().count(); + for line in layout.get_line_metrics().iter().skip(1) { + let at = plan.offsets.builder_to_source(line.start_index); + assert!( + !(2 < at && at < end), + "{language} {width}px breaks {unit} at {at}" + ); + } + assert!( + layout + .get_line_metrics() + .iter() + .all(|line| line.width > 0.0), + "no synthetic empty lines in {source}" + ); + for at in 0..=source.chars().count() { + assert_eq!( + plan.offsets + .builder_to_source(plan.offsets.source_to_builder(at)), + at, + "{source} at {at}" + ); + } + } + } + } +} + +#[test] +fn narrow_atomic_overflow_keeps_ruby_and_warichu_with_their_closing_marks() { + use crate::shapes::text_vertical::test_support::provider; + init_state(); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some( + provider(include_bytes!("../../fonts/cjk-locl-test.ttf")).into(), + )); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for warichu in [false, true] { + let mut annotation = make_span("文文文文", 0.0); + annotation.font_size = 20.0; + annotation.warichu = warichu; + if !warichu { + annotation.ruby = "wén wén".to_string(); + } + let mut paragraph = make_paragraph( + vec![make_span("文文", 0.0), annotation, make_span("。文文", 0.0)], + 0.0, + ); + paragraph.set_language("zh-Hant-TW".to_string()); + let (plan, layout) = cjk_layout_paragraph(¶graph, 20.0, &fonts); + let starts: Vec<_> = layout + .get_line_metrics() + .iter() + .map(|line| plan.offsets.builder_to_source(line.start_index)) + .collect(); + assert_eq!(starts, vec![0, 1, 2, 7, 8], "warichu={warichu}"); + for at in [0, 1, 2, 6, 7, 8, 9] { + assert_eq!( + plan.offsets + .builder_to_source(plan.offsets.source_to_builder(at)), + at, + "warichu={warichu}, {at}" + ); + } + assert!( + horizontal_span_style(&layout, &plan.offsets.ranges[1]).is_some(), + "warichu={warichu}" + ); + if !warichu { + let targets = horizontal_ruby_targets(¶graph, &plan.offsets); + assert_eq!(targets.len(), 1); + let rects = layout.get_rects_for_range( + targets[0].1.clone(), + RectHeightStyle::Tight, + RectWidthStyle::Tight, + ); + assert!(rects + .iter() + .all(|r| (r.rect.top() - rects[0].rect.top()).abs() < 0.01)); + } + } +} + +#[test] +fn adjacent_horizontal_ruby_bases_retain_each_annotation_style() { + use crate::shapes::text_vertical::test_support::provider; + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let mut fonts = resources.fonts.font_collection().clone(); + fonts.set_asset_font_manager(Some( + provider(include_bytes!("../../fonts/cjk-locl-test.ttf")).into(), + )); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for width in [240.0, 48.0] { + let spans = ["文", "文", "文", "文"] + .iter() + .map(|text| TextSpan { + text: text.to_string(), + font_size: 24.0, + ruby: "wén".to_string(), + text_emphasis: TextEmphasis::FilledDot, + annotation_clearance: AnnotationClearance::Auto, + ..make_span(text, 0.0) + }) + .collect(); + let mut paragraph = make_paragraph(spans, 0.0); + paragraph.set_language("zh-Hant-TW".to_string()); + let (plan, layout) = cjk_layout_paragraph(¶graph, width, &fonts); + assert_eq!( + horizontal_emphasis_placements(¶graph, &plan.offsets, &layout).len(), + 4 + ); + for range in &plan.offsets.ranges { + let style = horizontal_span_style(&layout, range); + assert!( + style.is_some(), + "missing style for adjacent ruby base {} at {width}px", + range.span + ); + assert_eq!(style.unwrap().font_size(), 24.0); + } + } +} + +#[test] +fn adjacent_ruby_emphasis_paints_a_mark_for_every_base_on_either_side() { + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let fonts = resources.fonts.font_collection().clone(); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for position in [ + TextEmphasisPosition::OverRight, + TextEmphasisPosition::UnderRight, + ] { + let spans = ["A", "B", "C", "D"] + .iter() + .map(|text| TextSpan { + font_size: 48.0, + fills: vec![crate::shapes::Fill::Solid(crate::shapes::SolidColor( + skia::Color::BLACK, + ))], + ruby: "a".to_string(), + text_emphasis: TextEmphasis::FilledDot, + text_emphasis_position: position, + annotation_clearance: AnnotationClearance::Auto, + ..make_span(text, 0.0) + }) + .collect(); + let mut paragraph = make_paragraph(spans, 0.0); + paragraph.set_language("zh-Hant-TW".to_string()); + let (plan, layout) = cjk_layout_paragraph(¶graph, 400.0, &fonts); + let placements = horizontal_emphasis_placements(¶graph, &plan.offsets, &layout); + assert_eq!(placements.len(), 4); + let mut surface = skia::surfaces::raster_n32_premul((440, 200)).unwrap(); + surface.canvas().clear(skia::Color::TRANSPARENT); + paint_horizontal_emphasis(surface.canvas(), ¶graph, &plan, &layout, 20.0, 40.0); + let image = surface.image_snapshot(); + let pixels = image.peek_pixels().expect("raster pixels"); + for placement in placements { + let left = (20.0 + placement.rect.left()).ceil() as i32; + let right = (20.0 + placement.rect.right()).floor() as i32; + let ink = (0..200).any(|y| (left..right).any(|x| pixels.get_color((x, y)).a() > 0)); + assert!( + ink, + "missing {position:?} emphasis on adjacent ruby base {}", + placement.span + ); + } + } +} + +#[test] +fn annotation_style_keeps_utf16_lines_separate_from_utf8_style_keys() { + use crate::shapes::text_vertical::test_support::{provider, TEST_FONT}; + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let mut fonts = resources.fonts.font_collection().clone(); + fonts.set_asset_font_manager(Some(provider(TEST_FONT).into())); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let prefix = make_span("😀\n", 0.0); + let spans = vec![ + prefix, + TextSpan { + font_size: 24.0, + ruby: "a".to_string(), + ..make_span("A", 0.0) + }, + TextSpan { + font_size: 24.0, + ruby: "b".to_string(), + ..make_span("B", 0.0) + }, + ]; + let mut paragraph = make_paragraph(spans, 0.0); + paragraph.set_language("zh-Hant-TW".to_string()); + let (plan, layout) = cjk_layout_paragraph(¶graph, 240.0, &fonts); + for (range, expected) in plan.offsets.ranges.iter().zip([16.0, 24.0, 24.0]) { + let style = horizontal_span_style(&layout, range).expect("style on the base's line"); + assert_eq!(style.font_size(), expected); + } +} + +#[test] +fn horizontal_emphasis_export_bounds_include_every_adjacent_ruby_mark() { + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for grow in [ + crate::shapes::GrowType::Fixed, + crate::shapes::GrowType::AutoHeight, + crate::shapes::GrowType::AutoWidth, + ] { + let spans = ["A", "B", "C", "D"] + .iter() + .map(|text| TextSpan { + font_size: 48.0, + language: "zh-Hant-TW".to_string(), + layout_policy: TextLayoutPolicy::for_language("zh-Hant-TW", Default::default()), + ruby: "a".to_string(), + text_emphasis: TextEmphasis::FilledDot, + text_decoration: Some(skia::textlayout::TextDecoration::UNDERLINE), + annotation_clearance: AnnotationClearance::Auto, + ..make_span(text, 0.0) + }) + .collect(); + let mut paragraph = make_paragraph(spans, 0.0); + paragraph.set_language("zh-Hant-TW".to_string()); + let bounds = crate::math::Rect::from_xywh(0.0, 0.0, 360.0, 160.0); + let mut content = TextContent::new(bounds, grow); + content.add_paragraph(paragraph); + content.update_layout(bounds); + let mut shape = crate::shapes::Shape::new(Uuid::nil()); + shape.set_selrect(0.0, 0.0, 360.0, 160.0); + let export = content.calculate_bounds(&shape, false).to_rect(); + let plans = content.horizontal_plans(); + let paragraph = &content.paragraphs()[0]; + let marks = horizontal_emphasis_mark_boxes( + paragraph, + &plans[0].offsets, + &content.layout.paragraphs[0][0], + ); + assert_eq!(marks.len(), 4); + for (_, mark) in marks { + assert!( + export.bottom() >= mark.bottom(), + "{grow:?}: export {export:?} crops mark {mark:?}" + ); + } + } +} + +#[test] +fn horizontal_ruby_export_bounds_keep_every_reading_whole() { + // Readings sit above the line box with the Japanese default clearance. + // Auto-width text built its bounds from the content rect and the + // emphasis marks only, so an export cut the top of the reading. + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let grows = [ + crate::shapes::GrowType::Fixed, + crate::shapes::GrowType::AutoHeight, + crate::shapes::GrowType::AutoWidth, + ]; + let emphases = [TextEmphasis::None, TextEmphasis::FilledCircle]; + let texts = [("AB", "ABC", "CD"), ("漢字", "かんじ", "の圏点")]; + for (grow, emphasis, (base, ruby, rest)) in grows + .into_iter() + .flat_map(|grow| emphases.map(|emphasis| (grow, emphasis))) + .flat_map(|(grow, emphasis)| texts.map(|text| (grow, emphasis, text))) + { + let spans = vec![ + TextSpan { + font_size: 48.0, + fills: vec![crate::shapes::Fill::Solid(crate::shapes::SolidColor( + skia::Color::BLACK, + ))], + ruby: ruby.to_string(), + text_emphasis: emphasis, + ..make_span(base, 0.0) + }, + make_span(rest, 0.0), + ]; + let mut paragraph = make_paragraph(spans, 0.0); + paragraph.set_language("ja".to_string()); + let bounds = crate::math::Rect::from_xywh(0.0, 0.0, 360.0, 60.0); + let mut content = TextContent::new(bounds, grow); + content.add_paragraph(paragraph); + content.update_layout(bounds); + let mut shape = crate::shapes::Shape::new(Uuid::nil()); + shape.set_selrect(0.0, 0.0, 360.0, 60.0); + let export = content.calculate_bounds(&shape, false).to_rect(); + + let pad = 100.0; + let mut surface = skia::surfaces::raster_n32_premul((560, 260)).unwrap(); + surface.canvas().clear(skia::Color::TRANSPARENT); + let x = pad + content.horizontal_plans()[0].x_offset(&content.paragraphs()[0], 360.0); + paint_horizontal_ruby( + surface.canvas(), + &content, + 0, + &content.layout.paragraphs[0][0], + x, + pad, + ); + let image = surface.image_snapshot(); + let pixels = image.peek_pixels().expect("raster pixels"); + let ink_top = (0..260) + .find(|y| (0..560).any(|x| pixels.get_color((x, *y)).a() > 0)) + .expect("the reading is painted") as f32; + assert!( + ink_top - pad >= export.top() - 1.0, + "{grow:?} {emphasis:?} {ruby}: export top {} crops the reading inked from {}", + export.top(), + ink_top - pad + ); + } +} + +/// Fixed-width `TextContent` of one paragraph, laid out like the renderer. +fn aligned_content(text: &str, tracking: f32, align: TextAlign, width: f32) -> TextContent { + let mut paragraph = Paragraph::new( + align, + TextDirection::LTR, + None, + None, + 1.0, + tracking, + vec![TextSpan { + fills: vec![crate::shapes::Fill::Solid(crate::shapes::SolidColor( + skia::Color::BLACK, + ))], + ..make_span(text, tracking) + }], + ); + paragraph.set_language("zh-Hans".to_string()); + let bounds = crate::math::Rect::from_xywh(0.0, 0.0, width, 200.0); + let mut content = TextContent::new(bounds, crate::shapes::GrowType::Fixed); + content.add_paragraph(paragraph); + content.update_layout(bounds); + content +} + +#[test] +fn spread_lines_start_and_end_at_the_box_edges() { + use skia::textlayout::{RectHeightStyle, RectWidthStyle}; + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for (align, text, width) in [ + (TextAlign::Distribute, "国家", 200.0), + (TextAlign::Distribute, "手机号", 200.0), + (TextAlign::Justify, "国家国家国家国家国家国家国家国家", 50.0), + ] { + let content = aligned_content(text, 0.0, align, width); + let layout = &content.layout.paragraphs[0][0]; + let lines = layout.get_line_metrics(); + let spread = if align == TextAlign::Distribute { + lines.len() + } else { + lines.len() - 1 + }; + assert!(spread > 0, "{text}: no spread line"); + let rect = |at: usize| { + layout.get_rects_for_range(at..at + 1, RectHeightStyle::Tight, RectWidthStyle::Tight)[0] + .rect + }; + for line in lines.iter().take(spread) { + let first = rect(line.start_index); + let last = rect(line.end_excluding_whitespaces - 1); + assert!( + first.left.abs() < 0.1, + "{text}: line starts at {}", + first.left + ); + assert!(line.left.abs() < 0.1, "{text}: line left {}", line.left); + assert!( + (last.right - width).abs() < 0.1, + "{text}: line ends at {}, box {width}", + last.right + ); + let hit = layout.get_glyph_position_at_coordinate((first.left + 1.0, first.center_y())); + assert_eq!(hit.position as usize, line.start_index, "{text}: hit test"); + } + // The painted ink starts at the box edge too, not half a gap in. + let mut surface = skia::surfaces::raster_n32_premul((400, 200)).unwrap(); + layout.paint(surface.canvas(), (100.0, 0.0)); + let pixels = surface.peek_pixels().unwrap(); + let ink_left = (0..400) + .find(|x| (0..200).any(|y| pixels.get_color((*x, y)).a() > 0)) + .expect("visible ink"); + assert!( + (100..104).contains(&ink_left), + "{text}: ink starts at {}", + ink_left - 100 + ); + } + // Span tracking keeps SkParagraph's half spacing before the line. + let content = aligned_content("国家", 10.0, TextAlign::Left, 200.0); + let layout = &content.layout.paragraphs[0][0]; + let first = layout.get_rects_for_range(0..1, RectHeightStyle::Tight, RectWidthStyle::Tight)[0]; + assert!( + (first.rect.left - 5.0).abs() < 0.01, + "tracked line starts at {}", + first.rect.left + ); +} + +/// Japanese text distributes like the other languages (均等割り付け). +#[test] +fn japanese_distribution_reaches_the_box_edges() { + use skia::textlayout::{RectHeightStyle, RectWidthStyle}; + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for text in ["日本", "にほんご", "東京都"] { + let mut paragraph = Paragraph::new( + TextAlign::Distribute, + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + vec![TextSpan { + fills: vec![crate::shapes::Fill::Solid(crate::shapes::SolidColor( + skia::Color::BLACK, + ))], + ..make_span(text, 0.0) + }], + ); + paragraph.set_language("ja".to_string()); + let bounds = crate::math::Rect::from_xywh(0.0, 0.0, 200.0, 200.0); + let mut content = TextContent::new(bounds, crate::shapes::GrowType::Fixed); + content.add_paragraph(paragraph); + content.update_layout(bounds); + let layout = &content.layout.paragraphs[0][0]; + let line = &layout.get_line_metrics()[0]; + let last = layout.get_rects_for_range( + line.end_excluding_whitespaces - 1..line.end_excluding_whitespaces, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + )[0] + .rect; + assert!( + (last.right - 200.0).abs() < 0.1, + "{text}: ends at {}", + last.right + ); + } +} + +#[test] +fn horizontal_emphasis_mark_box_is_the_painted_mark_on_either_side() { + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let fonts = resources.fonts.font_collection().clone(); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for position in [ + TextEmphasisPosition::OverRight, + TextEmphasisPosition::UnderRight, + ] { + let span = TextSpan { + font_size: 48.0, + fills: vec![crate::shapes::Fill::Solid(crate::shapes::SolidColor( + skia::Color::BLACK, + ))], + text_emphasis: TextEmphasis::FilledCircle, + text_emphasis_position: position, + ..make_span("A", 0.0) + }; + let paragraph = make_paragraph(vec![span], 0.0); + let (plan, layout) = cjk_layout_paragraph(¶graph, 400.0, &fonts); + let marks = horizontal_emphasis_mark_boxes(¶graph, &plan.offsets, &layout); + assert_eq!(marks.len(), 1); + let (placement, mark_box) = &marks[0]; + let span = ¶graph.children()[0]; + let shaped = super::emphasis::HorizontalEmphasisMark::measure( + &layout, + &plan.offsets.ranges[0], + span, + placement.mark, + ) + .expect("shaped mark"); + assert_eq!(mark_box.width(), shaped.ink.width()); + assert_eq!(mark_box.height(), shaped.ink.height()); + assert_eq!(mark_box.center_x(), placement.rect.center_x()); + let offset = span.emphasis_outside_offset(false); + if position == TextEmphasisPosition::OverRight { + assert_eq!(mark_box.bottom(), placement.em_top - offset); + } else { + assert_eq!(mark_box.top(), placement.rect.bottom() + offset); + } + + let (x, y) = (20.0, 60.0); + let mut surface = skia::surfaces::raster_n32_premul((440, 220)).unwrap(); + surface.canvas().clear(skia::Color::TRANSPARENT); + paint_horizontal_emphasis(surface.canvas(), ¶graph, &plan, &layout, x, y); + let image = surface.image_snapshot(); + let pixels = image.peek_pixels().expect("raster pixels"); + let mut ink: Option = None; + for py in 0..220 { + for px in 0..440 { + if pixels.get_color((px, py)).a() > 0 { + let pixel = skia::IRect::from_xywh(px, py, 1, 1); + ink = Some(ink.map_or(pixel, |rect| skia::IRect::join(&rect, &pixel))); + } + } + } + let ink = ink.expect("the mark paints"); + let painted = mark_box.with_offset((x, y)); + assert!( + ink.left as f32 >= painted.left.floor() + && ink.right as f32 <= painted.right.ceil() + && ink.top as f32 >= painted.top.floor() + && ink.bottom as f32 <= painted.bottom.ceil(), + "{position:?}: mark paints {ink:?} outside its box {painted:?}" + ); + } +} + +/// Gaps, in em, of the first line of ああAああPenpot justified horizontally +/// with `extra_em` of slack on that line: (ああ, あA, Aあ, ああ). +fn horizontal_justified_gaps(extra_em: f32) -> Vec { + use crate::shapes::text_vertical::test_support::{ + provider_with_fallback, EM, TEST_FONT, VPAL_TEST_FONT, + }; + init_state(); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some( + provider_with_fallback(VPAL_TEST_FONT, TEST_FONT).into(), + )); + let span = TextSpan { + font_size: EM, + ..make_span("ああAああPenpot", 0.0) + }; + let paragraph = make_paragraph(vec![span], 0.0); + let (plan, wide) = cjk_layout_paragraph(¶graph, 1000.0, &fonts); + // Builder offset after the fifth real character, past both inserted + // inter-script spaces. + let texts: Vec = plan.texts[0].chars().collect(); + let fifth = texts + .iter() + .enumerate() + .filter(|(_, ch)| **ch != line_breaks::JAPANESE_WESTERN_SPACE) + .nth(4) + .map(|(index, _)| index) + .unwrap(); + let five = + wide.get_rects_for_range(0..fifth + 1, RectHeightStyle::Tight, RectWidthStyle::Tight); + let five_width = five.last().unwrap().rect.right() - five[0].rect.left(); + let width = five_width + extra_em * EM; + let (plan, natural) = cjk_layout_paragraph(¶graph, width, &fonts); + let lines = natural.get_line_metrics(); + assert_eq!(lines.len(), 2, "Penpot moves to the second line"); + let spacing = horizontal_jlreq_justify_spacing(¶graph, &natural, &plan, width); + let at = |index: usize| -> f32 { + spacing[0] + .iter() + .filter(|(at, _)| *at == index) + .map(|(_, amount)| amount) + .sum::() + / EM + }; + // あ0 あ1 U+2005 A3 U+2005 あ5 あ6: each gap takes the spacing of the + // character before it. + assert_eq!(texts[2], line_breaks::JAPANESE_WESTERN_SPACE); + vec![at(0), at(1), at(3), at(5)] +} + +/// The same gaps of ああAああPenpot justified as a vertical column. +fn vertical_justified_gaps(extra_em: f32) -> Vec { + use crate::shapes::text_vertical::test_support::{ + layout_with_fallback, provider_with_fallback, spans_content, vertical_paragraph, EM, + TEST_FONT, VPAL_TEST_FONT, + }; + let provider = provider_with_fallback(VPAL_TEST_FONT, TEST_FONT); + let fallback = ["fallback".to_string()]; + let content = |align: TextAlign, height: f32| { + let span = TextSpan { + text: "ああAああPenpot".to_string(), + font_size: EM, + line_height: 1.0, + ..TextSpan::default() + }; + let mut content = spans_content(vec![], height); + *content.paragraphs_mut() = vec![vertical_paragraph(vec![span], align, 1.0)]; + content + }; + let wide = layout_with_fallback( + &provider, + &content(TextAlign::Left, 1000.0), + 1000.0, + &fallback, + ); + let five: f32 = wide.cells[..5].iter().map(|cell| cell.extent).sum(); + let height = five + extra_em * EM; + let layout = layout_with_fallback( + &provider, + &content(TextAlign::Justify, height), + height, + &fallback, + ); + assert!(layout.cells[..5].iter().all(|cell| cell.column == 0)); + assert!(layout.cells[5..].iter().all(|cell| cell.column == 1)); + (0..4) + .map(|i| { + let (cell, next) = (&layout.cells[i], &layout.cells[i + 1]); + let natural = wide.cells[i + 1].top - wide.cells[i].top; + (next.top - cell.top - natural) / EM + }) + .collect() +} + +#[test] +fn horizontal_and_vertical_justify_widen_gaps_in_the_same_jlreq_order() { + for (axis, gaps) in [ + ( + "horizontal", + horizontal_justified_gaps as fn(f32) -> Vec, + ), + ("vertical", vertical_justified_gaps), + ] { + // Little slack: only the two Japanese/Western gaps widen. + let small = gaps(0.3); + assert!( + small[1] > 0.1 && (small[1] - small[2]).abs() < 0.01, + "{axis}: {small:?}" + ); + assert!( + small[0].abs() < 0.001 && small[3].abs() < 0.001, + "{axis}: {small:?}" + ); + // More slack: they stop at their half-em maximum (a quarter em past + // the preferred quarter), then the kana gaps share the rest. + let large = gaps(0.9); + assert!( + (large[1] - 0.25).abs() < 0.01 && (large[2] - 0.25).abs() < 0.01, + "{axis}: {large:?}" + ); + assert!( + large[0] > 0.1 && (large[0] - large[3]).abs() < 0.01, + "{axis}: {large:?}" + ); + } +} + +/// `paragraph` with the plan's sheds plus `extra`, restricted to the builder +/// `window`, laid out at `width` as the squeeze pass measures it. +fn squeeze_window_layout( + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, + fonts: &skia::textlayout::FontCollection, + width: f32, + extra: &[Vec<(usize, f32)>], + window: std::ops::Range, +) -> LayoutParagraph { + let mut paragraph_style = paragraph.paragraph_to_style(); + paragraph_style.set_apply_rounding_hack(false); + let mut builder = ParagraphBuilder::new(¶graph_style, fonts); + let span = ¶graph.children()[0]; + let style = span.to_style( + &crate::math::Rect::from_xywh(0.0, 0.0, width, 200.0), + &crate::render::FallbackFonts::default(), + false, + 1.0, + ); + let sheds: Vec<(usize, f32)> = plan.pair_spacing[0] + .iter() + .chain(extra.first().into_iter().flatten()) + .copied() + .collect(); + let (text, from) = + window_slice(&plan.texts[0], &plan.offsets.ranges[0], &window).unwrap_or(("", 0)); + let sheds = window_entries(&sheds, from, text.chars().count()); + builder.push_style(&style); + add_horizontal_span(&mut builder, span, text, &sheds, &[], &style, fonts); + let mut laid_out = LayoutParagraph::build(&mut builder); + laid_out.layout(width); + laid_out +} + +/// Squeeze `text` under each line adjustment, at a width that fits the +/// first line up to the `nth` (from zero) `fits` character plus `extra_em`. +/// Returns the line adjustment, whether squeezing kept the next line's first +/// unit and whether the push-out-first gate expects a squeeze. +fn squeeze_outcomes( + text: &str, + (fits, nth): (char, usize), + extra_em: f32, +) -> Vec<(LineAdjustment, bool, bool)> { + use crate::shapes::text_vertical::test_support::{provider, EM, VPAL_TEST_FONT}; + init_state(); + let mut fonts = skia::textlayout::FontCollection::new(); + fonts.set_asset_font_manager(Some(provider(VPAL_TEST_FONT).into())); + let span = TextSpan { + font_size: EM, + ..make_span(text, 0.0) + }; + let paragraph = make_paragraph(vec![span], 0.0); + let plan = HorizontalParagraphPlan::new(¶graph); + let chars: Vec = plan.texts[0].chars().collect(); + let fits_at = chars + .iter() + .enumerate() + .filter(|(_, ch)| **ch == fits) + .nth(nth) + .map(|(at, _)| at) + .unwrap(); + let wide = squeeze_window_layout(¶graph, &plan, &fonts, 1000.0, &[], FULL_WINDOW); + let right = wide.get_rects_for_range( + fits_at..fits_at + 1, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + )[0] + .rect + .right(); + let width = right + extra_em * EM; + let natural = squeeze_window_layout(¶graph, &plan, &fonts, width, &[], FULL_WINDOW); + let natural_end = natural.get_line_metrics()[0].end_index; + assert!( + natural_end <= fits_at, + "the unit at {fits:?} moves down whole" + ); + let may_fail = super::justify::horizontal_push_out_may_fail(¶graph, &plan); + [ + LineAdjustment::PushInFirst, + LineAdjustment::PushOutFirst, + LineAdjustment::PushOutOnly, + ] + .into_iter() + .map(|adjustment| { + let lay_out = |extra: &[Vec<(usize, f32)>], window: std::ops::Range| { + squeeze_window_layout(¶graph, &plan, &fonts, width, extra, window) + }; + let (sheds, squeezed) = + horizontal_squeeze_sheds(¶graph, &plan, width, adjustment, &lay_out); + let kept = squeezed.get_line_metrics()[0].end_index > fits_at; + assert_eq!(kept, !sheds[0].is_empty(), "{adjustment:?}"); + (adjustment, kept, may_fail) + }) + .collect() +} + +#[test] +fn push_out_first_squeezes_when_push_out_leaves_a_closing_bracket_at_the_line_start() { + // Without kinsoku the line would break before the fifth 」. Moving that + // break back to あ crosses five kinsoku pairs, more than oidashi moves + // over, so push-out first squeezes the commas as push-in first does. + let outcomes = squeeze_outcomes("あく、あく、あ」」」」」くあく", ('」', 3), 0.0); + for (adjustment, kept, may_fail) in outcomes { + assert!(may_fail); + assert_eq!( + kept, + adjustment != LineAdjustment::PushOutOnly, + "{adjustment:?}" + ); + } +} + +#[test] +fn push_out_first_keeps_push_out_when_it_resolves_the_line_start() { + // Without kinsoku the line would break before 」; moving あ」 down + // resolves it, so only push-in first squeezes. + let outcomes = squeeze_outcomes("あく、あく、あ」くあく", ('あ', 2), 0.3); + for (adjustment, kept, may_fail) in outcomes { + assert!(!may_fail); + assert_eq!( + kept, + adjustment == LineAdjustment::PushInFirst, + "{adjustment:?}" + ); + } +} + +fn reading_paragraph(spans: Vec) -> Paragraph { + let mut paragraph = make_paragraph( + [vec![make_span("a", 0.0)], spans, vec![make_span("b", 0.0)]].concat(), + 0.0, + ); + paragraph.set_language("ja".to_string()); + paragraph +} + +fn long_reading(text: &str, joins_previous: bool) -> TextSpan { + TextSpan { + ruby: "MMMMMMMMMMMM".to_string(), + ruby_overhang: RubyOverhang::None, + ruby_joins_previous: joins_previous, + ..make_span(text, 0.0) + } +} + +#[test] +fn a_joined_long_reading_spreads_its_whole_base_like_one_span() { + init_state(); + let one = test_ruby_spacing(&reading_paragraph(vec![long_reading("ii", false)])); + let paragraph = reading_paragraph(vec![ + long_reading("i", false), + TextSpan { + text_emphasis: TextEmphasis::FilledDot, + ..long_reading("i", true) + }, + ]); + let joined = test_ruby_spacing(¶graph); + // A joiner keeps the line from breaking between the two spans. + let (texts, _) = paragraph.layout_span_texts(); + let second = texts[2].chars().position(|ch| ch == 'i').unwrap(); + + let gap = one.base_gaps[1]; + assert!(gap > 0.0, "the base grows under a long reading"); + assert_eq!(joined.base_gaps[1], gap); + assert_eq!(joined.leading_gaps[1], one.leading_gaps[1]); + assert_eq!(joined.adjustments[1], vec![(0, gap)]); + assert_eq!(joined.adjustments[2], vec![(second, gap / 2.0)]); +} + +#[test] +fn a_joined_reading_never_breaks_between_its_spans() { + init_state(); + let paragraph = reading_paragraph(vec![long_reading("日", false), long_reading("本", true)]); + let (texts, map) = paragraph.layout_span_texts(); + let start = texts[..2] + .iter() + .map(|text| text.encode_utf16().count()) + .sum::(); + + assert!( + texts[2].starts_with('\u{2060}') && map.is_inserted(start), + "a word joiner starts the joined span: {texts:?}" + ); +} + +#[test] +fn a_joined_reading_has_one_target_over_its_spans() { + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let paragraph = reading_paragraph(vec![ + long_reading("日", false), + long_reading("本", true), + long_reading("語", false), + ]); + let plan = HorizontalParagraphPlan::new(¶graph); + let ranges = &plan.offsets.ranges; + + assert_eq!( + horizontal_ruby_targets(¶graph, &plan.offsets), + vec![ + ( + 1, + plan.offsets.transformed_to_builder(ranges[1].source_start)..ranges[2].builder_end + ), + ( + 3, + plan.offsets.transformed_to_builder(ranges[3].source_start)..ranges[3].builder_end + ), + ] + ); +} + +#[test] +fn a_joined_reading_paints_like_the_reading_of_one_span() { + init_state(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let ink = |spans: Vec| { + let bounds = crate::math::Rect::from_xywh(0.0, 0.0, 360.0, 60.0); + let mut content = TextContent::new(bounds, crate::shapes::GrowType::Fixed); + content.add_paragraph(reading_paragraph(spans)); + content.update_layout(bounds); + super::ruby::horizontal_ruby_ink_bounds( + &content, + 0, + &content.layout.paragraphs[0][0], + 0.0, + 0.0, + ) + .expect("the reading paints") + }; + let reading = |text: &str, joins_previous| TextSpan { + font_size: 48.0, + ruby: "にほんご".to_string(), + ruby_joins_previous: joins_previous, + ..make_span(text, 0.0) + }; + + let one = ink(vec![reading("日本", false)]); + let joined = ink(vec![ + reading("日", false), + TextSpan { + text_emphasis: TextEmphasis::FilledSesame, + ..reading("本", true) + }, + ]); + + assert!( + (joined.left() - one.left()).abs() < 0.5, + "{joined:?} vs {one:?}" + ); + assert!( + (joined.right() - one.right()).abs() < 0.5, + "{joined:?} vs {one:?}" + ); +} diff --git a/render-wasm/src/shapes/text_horizontal/warichu.rs b/render-wasm/src/shapes/text_horizontal/warichu.rs new file mode 100644 index 0000000000..94caded011 --- /dev/null +++ b/render-wasm/src/shapes/text_horizontal/warichu.rs @@ -0,0 +1,189 @@ +use super::*; +use crate::shapes::text_paragraph::LayoutParagraph; + +pub(super) const HORIZONTAL_WARICHU_BUILDER_LEN: usize = 3; +pub(super) const HORIZONTAL_WARICHU_STYLE_ANCHOR: char = '\u{00A0}'; +pub(super) const HORIZONTAL_WARICHU_BREAK_ANCHOR: char = '\u{200B}'; + +/// Placeholder rect of each horizontal warichu span, keyed by span index. +/// Tabs in normal spans are placeholders too, so they are skipped. +pub(crate) fn horizontal_warichu_placeholders( + paragraph: &Paragraph, + laid_out: &LayoutParagraph, +) -> Vec<(usize, skia::Rect)> { + let mut placeholders = laid_out.get_rects_for_placeholders().into_iter(); + let mut result = Vec::new(); + for (index, span) in paragraph.children().iter().enumerate() { + if span.is_warichu() { + if let Some(textbox) = placeholders.next() { + result.push((index, textbox.rect)); + } + } else { + for _ in span.text.matches('\t') { + placeholders.next(); + } + } + } + result +} + +fn horizontal_warichu_boxes<'a>( + paragraph: &'a Paragraph, + laid_out: &LayoutParagraph, +) -> Vec<(&'a TextSpan, usize, usize, skia::Rect)> { + let mut source_start = 0usize; + let mut placeholders = horizontal_warichu_placeholders(paragraph, laid_out) + .into_iter() + .peekable(); + let mut boxes = Vec::new(); + for (index, span) in paragraph.children().iter().enumerate() { + let length = span.text.chars().count(); + if let Some((_, rect)) = placeholders.next_if(|(span_index, _)| *span_index == index) { + boxes.push((span, source_start, source_start + length, rect)); + } + source_start += length; + } + boxes +} + +pub(crate) fn horizontal_warichu_hit_test( + paragraph: &Paragraph, + laid_out: &LayoutParagraph, + point: Point, +) -> Option { + for (span, source_start, _, rect) in horizontal_warichu_boxes(paragraph, laid_out) { + if !rect.contains(&point) { + continue; + } + let split = warichu_split_chars(&span.apply_text_transform()); + let total = span.text.chars().count(); + let second = point.y >= rect.top() + rect.height() / 2.0; + let (line_start, line_len) = if second { + (split, total - split) + } else { + (0, split) + }; + let fraction = ((point.x - rect.left()) / rect.width().max(0.01)).clamp(0.0, 1.0); + let within = (fraction * line_len as f32).round() as usize; + return Some(source_start + line_start + within.min(line_len)); + } + None +} + +pub(crate) fn horizontal_warichu_caret_rect( + paragraph: &Paragraph, + laid_out: &LayoutParagraph, + source_offset: usize, +) -> Option { + for (span, source_start, source_end, rect) in horizontal_warichu_boxes(paragraph, laid_out) { + if source_offset < source_start || source_offset > source_end { + continue; + } + let split = warichu_split_chars(&span.apply_text_transform()); + let local = source_offset - source_start; + let total = source_end - source_start; + let (line_start, line_len, top) = if local >= split { + (split, total - split, rect.top() + rect.height() / 2.0) + } else { + (0, split, rect.top()) + }; + let within = local.saturating_sub(line_start).min(line_len); + let char_width = rect.width() / line_len.max(1) as f32; + return Some(skia::Rect::from_xywh( + rect.left() + within as f32 * char_width, + top, + char_width, + rect.height() / 2.0, + )); + } + None +} + +pub(crate) fn horizontal_warichu_range_rects( + paragraph: &Paragraph, + laid_out: &LayoutParagraph, + source_start: usize, + source_end: usize, +) -> Vec { + let mut rects = Vec::new(); + for (span, span_start, span_end, rect) in horizontal_warichu_boxes(paragraph, laid_out) { + let selected_start = source_start.max(span_start); + let selected_end = source_end.min(span_end); + if selected_start >= selected_end { + continue; + } + let split = warichu_split_chars(&span.apply_text_transform()); + for (line_start, line_end, top) in [ + (span_start, span_start + split, rect.top()), + ( + span_start + split, + span_end, + rect.top() + rect.height() / 2.0, + ), + ] { + let start = selected_start.max(line_start); + let end = selected_end.min(line_end); + if start >= end { + continue; + } + let line_len = line_end - line_start; + let char_width = rect.width() / line_len.max(1) as f32; + rects.push(skia::Rect::from_xywh( + rect.left() + (start - line_start) as f32 * char_width, + top, + (end - start) as f32 * char_width, + rect.height() / 2.0, + )); + } + } + rects +} + +/// The two warichu sub-lines of `text`, split at `warichu_split_chars`. +pub(crate) fn warichu_text_lines(text: &str) -> (&str, &str) { + let split = warichu_split_chars(text); + let split_byte = text + .char_indices() + .nth(split) + .map(|(index, _)| index) + .unwrap_or(text.len()); + text.split_at(split_byte) +} + +/// Paint the two horizontal warichu sub-lines into SkParagraph's inline +/// placeholder boxes. Reading order is top line then bottom line. +pub(crate) fn paint_horizontal_warichu( + canvas: &skia::Canvas, + paragraph: &Paragraph, + plan: &HorizontalParagraphPlan, + laid_out: &LayoutParagraph, + x: f32, + y: f32, +) { + let ranges = &plan.offsets.ranges; + let placeholders = horizontal_warichu_placeholders(paragraph, laid_out); + let warichu_ranges: Vec<_> = ranges.iter().filter(|range| range.warichu).collect(); + if placeholders.len() != warichu_ranges.len() { + return; + } + + for (range, (_, rect)) in warichu_ranges.into_iter().zip(placeholders) { + let Some(span) = paragraph.children().get(range.span) else { + continue; + }; + let Some(mut style) = horizontal_span_style(laid_out, range) else { + continue; + }; + style.set_font_size(span.font_size * WARICHU_FONT_SCALE); + style.set_height(1.0); + style.set_height_override(true); + style.set_letter_spacing(span.letter_spacing * WARICHU_FONT_SCALE); + let transformed = span.apply_text_transform(); + let (first, second) = warichu_text_lines(&transformed); + let first_para = mini_paragraph(first, &style, get_font_collection()); + let second_para = mini_paragraph(second, &style, get_font_collection()); + let half_height = rect.height() / 2.0; + first_para.paint(canvas, (x + rect.left(), y + rect.top())); + second_para.paint(canvas, (x + rect.left(), y + rect.top() + half_height)); + } +} diff --git a/render-wasm/src/shapes/text_paragraph.rs b/render-wasm/src/shapes/text_paragraph.rs new file mode 100644 index 0000000000..4de36df72e --- /dev/null +++ b/render-wasm/src/shapes/text_paragraph.rs @@ -0,0 +1,447 @@ +//! SkParagraph rounds every line height and baseline, even with its rounding +//! hack disabled. Keep its shaping and horizontal layout, but use the original +//! ascent/descent for vertical geometry and painting. +//! +//! SkParagraph also moves each line by half the letter-spacing of its first +//! character. That centers span tracking, but letter-spacing added to single +//! characters (justification, distribution, squeezing, ruby room) must only +//! open the gap after them, so each line takes back half of that extra. + +use skia_safe::{ + textlayout::{ + FontCollection, LineMetrics, Paragraph, ParagraphBuilder, ParagraphStyle, + PositionWithAffinity, RectHeightStyle, RectWidthStyle, TextBox, + }, + Canvas, Paint, Point, TextBlob, TextBlobBuilder, +}; +use std::ops::{Deref, DerefMut, Range}; + +/// A builder `LayoutParagraph` can lay out, with the letter-spacing its +/// characters take beyond their span tracking. +pub trait SpacedParagraphBuilder { + fn paragraph_builder(&mut self) -> &mut ParagraphBuilder; + + /// Records `amount` of extra letter-spacing on the character at the + /// builder's UTF-16 offset `at`. + fn add_extra_spacing(&mut self, _at: usize, _amount: f32) {} + + /// Extra letter-spacing as (UTF-16 offset, amount), sorted by offset. + fn extra_spacing(&self) -> &[(usize, f32)] { + &[] + } +} + +impl SpacedParagraphBuilder for ParagraphBuilder { + fn paragraph_builder(&mut self) -> &mut ParagraphBuilder { + self + } +} + +/// Paragraph builder of horizontal text, which records the extra +/// letter-spacing of its characters. +pub struct HorizontalBuilder { + builder: ParagraphBuilder, + extra_spacing: Vec<(usize, f32)>, +} + +impl HorizontalBuilder { + pub fn new(style: &ParagraphStyle, fonts: impl Into) -> Self { + Self { + builder: ParagraphBuilder::new(style, fonts), + extra_spacing: Vec::new(), + } + } +} + +impl SpacedParagraphBuilder for HorizontalBuilder { + fn paragraph_builder(&mut self) -> &mut ParagraphBuilder { + &mut self.builder + } + + fn add_extra_spacing(&mut self, at: usize, amount: f32) { + self.extra_spacing.push((at, amount)); + } + + fn extra_spacing(&self) -> &[(usize, f32)] { + &self.extra_spacing + } +} + +impl std::fmt::Debug for HorizontalBuilder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("HorizontalBuilder") + .field("extra_spacing", &self.extra_spacing) + .finish_non_exhaustive() + } +} + +impl Deref for HorizontalBuilder { + type Target = ParagraphBuilder; + fn deref(&self) -> &ParagraphBuilder { + &self.builder + } +} + +impl DerefMut for HorizontalBuilder { + fn deref_mut(&mut self) -> &mut ParagraphBuilder { + &mut self.builder + } +} + +#[derive(Debug)] +pub struct LayoutParagraph { + inner: Paragraph, + text: String, + extra_spacing: Vec<(usize, f32)>, + shifts: Vec, + x_shifts: Vec, + height: f32, + runs: Vec<(TextBlob, Paint, Point)>, +} + +impl LayoutParagraph { + pub fn build(builder: &mut impl SpacedParagraphBuilder) -> Self { + let extra_spacing = builder.extra_spacing().to_vec(); + let builder = builder.paragraph_builder(); + let text = builder.get_text().to_owned(); + Self { + inner: builder.build(), + text, + extra_spacing, + shifts: Vec::new(), + x_shifts: Vec::new(), + height: 0.0, + runs: Vec::new(), + } + } + + pub fn layout(&mut self, width: f32) { + self.inner.layout(width); + let lines = self.inner.get_line_metrics(); + self.shifts.clear(); + self.x_shifts.clear(); + self.height = 0.0; + // Keep the first baseline anchored as before. Only remove drift + // between lines; this also preserves existing one-line layouts. + let anchor = lines + .first() + .map_or(0.0, |line| (line.baseline - line.ascent) as f32); + for line in &lines { + self.shifts + .push(self.height + line.ascent as f32 + anchor - line.baseline as f32); + self.height += (line.ascent + line.descent) as f32; + let first = self + .extra_spacing + .binary_search_by_key(&line.start_index, |(at, _)| *at) + .map_or(0.0, |found| self.extra_spacing[found].1); + self.x_shifts.push(-first / 2.0); + } + if lines.is_empty() { + self.height = self.inner.height(); + } + self.runs.clear(); + if self + .shifts + .iter() + .chain(&self.x_shifts) + .all(|shift| shift.abs() < 0.00001) + { + return; + } + // Copy the paints before visiting: Skia exposes style metrics as + // references into the paragraph that visit mutably borrows. + let paints: Vec> = lines + .iter() + .map(|line| { + line.get_style_metrics(0..self.text.len().saturating_add(1)) + .into_iter() + .map(|(at, metric)| (at, metric.text_style.foreground())) + .collect() + }) + .collect(); + drop(lines); + // Retain UTF-8 clusters in the blobs so PDF text stays selectable. + let mut boundaries = vec![self.text.len()]; + self.inner.visit(|_, info| { + if let Some(info) = info { + boundaries.extend(info.utf8_starts().iter().map(|at| *at as usize)); + } + }); + boundaries.sort_unstable(); + boundaries.dedup(); + let text = &self.text; + let shifts = &self.shifts; + let x_shifts = &self.x_shifts; + let runs = &mut self.runs; + self.inner.visit(|line, info| { + let Some(info) = info.filter(|info| info.count() > 0) else { + return; + }; + let starts = info.utf8_starts(); + let start = *starts.iter().min().unwrap() as usize; + let last = *starts.iter().max().unwrap() as usize; + let end = boundaries + .get(boundaries.partition_point(|at| *at <= last)) + .copied() + .unwrap_or(text.len()); + let paint = paints + .get(line) + .and_then(|paints| paints.iter().rev().find(|(at, _)| *at <= start)) + .map(|(_, paint)| paint.clone()) + .unwrap_or_default(); + let mut builder = TextBlobBuilder::new(); + if let Some(source) = text + .as_bytes() + .get(start..end) + .filter(|source| !source.is_empty()) + { + let (glyphs, points, utf8, clusters) = + builder.alloc_run_text_pos(info.font(), info.count(), source.len(), None); + glyphs.copy_from_slice(info.glyphs()); + points.copy_from_slice(info.positions()); + utf8.copy_from_slice(source); + for (cluster, at) in clusters.iter_mut().zip(starts) { + *cluster = *at - start as u32; + } + } else { + let (glyphs, points) = builder.alloc_run_pos(info.font(), info.count(), None); + glyphs.copy_from_slice(info.glyphs()); + points.copy_from_slice(info.positions()); + } + if let Some(blob) = builder.make() { + let mut origin = info.origin(); + origin.x += x_shifts.get(line).copied().unwrap_or(0.0); + origin.y += shifts.get(line).copied().unwrap_or(0.0); + runs.push((blob, paint, origin)); + } + }); + } + + pub fn line_shift(&self, line: usize) -> f32 { + self.shifts.get(line).copied().unwrap_or(0.0) + } + + pub fn line_x_shift(&self, line: usize) -> f32 { + self.x_shifts.get(line).copied().unwrap_or(0.0) + } + + pub fn height(&self) -> f32 { + self.height + } + + pub fn ideographic_baseline(&self) -> f32 { + self.inner.ideographic_baseline() + self.line_shift(0) + } + + /// Font that shaped the text at UTF-16 `offset`, fallbacks included. + pub fn font_at_utf16(&self, offset: usize) -> skia_safe::Font { + let mut utf16 = 0; + let byte = self + .text + .char_indices() + .find(|(_, ch)| { + let start = utf16; + utf16 += ch.len_utf16(); + start >= offset + }) + .map_or(self.text.len(), |(byte, _)| byte); + self.inner.get_font_at(byte) + } + + pub fn get_line_metrics(&self) -> Vec> { + let mut lines = self.inner.get_line_metrics(); + for (index, line) in lines.iter_mut().enumerate() { + line.left += self.line_x_shift(index) as f64; + line.baseline += self.line_shift(index) as f64; + line.height = line.ascent + line.descent; + } + lines + } + + pub fn get_rects_for_range( + &self, + range: Range, + height: RectHeightStyle, + width: RectWidthStyle, + ) -> Vec { + let mut result = Vec::new(); + for (index, line) in self.inner.get_line_metrics().iter().enumerate() { + let start = range.start.max(line.start_index); + let end = range.end.min(line.end_including_newline); + if start >= end { + continue; + } + let mut boxes = self.inner.get_rects_for_range(start..end, height, width); + for textbox in &mut boxes { + textbox + .rect + .offset((self.line_x_shift(index), self.line_shift(index))); + } + result.extend(boxes); + } + result + } + + pub fn get_rects_for_placeholders(&self) -> Vec { + let lines = self.inner.get_line_metrics(); + let mut boxes = self.inner.get_rects_for_placeholders(); + for textbox in &mut boxes { + let center = textbox.rect.center_y(); + let index = lines + .iter() + .position(|line| center < (line.baseline + line.descent) as f32) + .unwrap_or(lines.len().saturating_sub(1)); + textbox + .rect + .offset((self.line_x_shift(index), self.line_shift(index))); + } + boxes + } + + pub fn get_glyph_position_at_coordinate( + &self, + point: impl Into, + ) -> PositionWithAffinity { + let mut point = point.into(); + let lines = self.get_line_metrics(); + let index = lines + .iter() + .position(|line| point.y < (line.baseline + line.descent) as f32) + .unwrap_or(lines.len().saturating_sub(1)); + point.x -= self.line_x_shift(index); + point.y -= self.line_shift(index); + self.inner.get_glyph_position_at_coordinate(point) + } + + pub fn paint(&self, canvas: &Canvas, point: impl Into) { + let point = point.into(); + if self.runs.is_empty() { + self.inner.paint(canvas, point); + } else { + for (blob, paint, origin) in &self.runs { + canvas.draw_text_blob(blob, point + *origin, paint); + } + } + } +} + +impl Deref for LayoutParagraph { + type Target = Paragraph; + fn deref(&self) -> &Paragraph { + &self.inner + } +} + +impl DerefMut for LayoutParagraph { + fn deref_mut(&mut self) -> &mut Paragraph { + &mut self.inner + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shapes::text_vertical::test_support::provider; + use skia_safe::textlayout::{FontCollection, ParagraphStyle, TextStyle}; + + fn builder() -> ParagraphBuilder { + crate::globals::design_init(); + let mut fonts = FontCollection::new(); + fonts.set_asset_font_manager(Some( + provider(include_bytes!("../fonts/cjk-locl-test.ttf")).into(), + )); + ParagraphBuilder::new(&ParagraphStyle::default(), fonts) + } + + fn style(color: skia_safe::Color, stroke: bool) -> TextStyle { + let mut paint = Paint::default(); + paint.set_color(color); + if stroke { + paint.set_style(skia_safe::PaintStyle::Stroke); + paint.set_stroke_width(0.5); + } + let mut style = TextStyle::default(); + let family = format!( + "{}", + crate::shapes::FontFamily::new( + crate::Uuid::nil(), + 400, + crate::shapes::FontStyle::Normal + ) + ); + style.set_font_families(&[family]); + style.set_font_size(18.0); + style.set_height(1.6); + style.set_height_override(true); + style.set_half_leading(true); + style.set_foreground_paint(&paint); + style + } + + #[test] + fn fractional_wrapped_paint_matches_individual_lines_with_mixed_fills_and_strokes() { + for stroke in [false, true] { + let colors = [ + skia_safe::Color::RED, + skia_safe::Color::GREEN, + skia_safe::Color::BLUE, + ]; + let mut builder = builder(); + for (index, color) in colors.into_iter().enumerate() { + builder.push_style(&style(color, stroke)); + builder.add_text(if index < 2 { "한글\n" } else { "한글" }); + builder.pop(); + } + let mut paragraph = LayoutParagraph::build(&mut builder); + paragraph.layout(100.0); + let mut actual = skia_safe::surfaces::raster_n32_premul((100, 100)).unwrap(); + paragraph.paint(actual.canvas(), (0.0, 0.0)); + let mut expected = skia_safe::surfaces::raster_n32_premul((100, 100)).unwrap(); + for (index, color) in colors.into_iter().enumerate() { + let mut line_builder = self::builder(); + line_builder.push_style(&style(color, stroke)); + line_builder.add_text("한글"); + let mut line = line_builder.build(); + line.layout(100.0); + line.paint(expected.canvas(), (0.0, index as f32 * 28.8)); + } + let pixels = actual.peek_pixels().unwrap(); + assert!( + pixels.bytes().unwrap().iter().any(|byte| *byte > 0), + "the font paints visible ink" + ); + assert_eq!(pixels.bytes(), expected.peek_pixels().unwrap().bytes()); + } + } + + #[test] + fn fractional_geometry_hit_testing_and_relayout_share_the_same_lines() { + let mut builder = builder(); + builder.push_style(&style(skia_safe::Color::BLACK, false)); + builder.add_text("한글\n한글\n한글"); + let mut paragraph = LayoutParagraph::build(&mut builder); + for width in [100.0, 50.0, 100.0] { + paragraph.layout(width); + assert!((paragraph.height() - 86.4).abs() < 0.001); + let boxes: Vec<_> = [0, 3, 6] + .into_iter() + .map(|at| { + paragraph.get_rects_for_range( + at..at + 1, + RectHeightStyle::Tight, + RectWidthStyle::Tight, + )[0] + .rect + }) + .collect(); + for pair in boxes.windows(2) { + assert!((pair[1].top - pair[0].top - 28.8).abs() < 0.001); + } + for (index, rect) in boxes.iter().enumerate() { + let hit = + paragraph.get_glyph_position_at_coordinate((rect.left + 0.01, rect.center_y())); + assert_eq!(hit.position as usize, index * 3); + } + } + } +} diff --git a/render-wasm/src/shapes/text_paths.rs b/render-wasm/src/shapes/text_paths.rs index 344906971a..c9af4dbd7e 100644 --- a/render-wasm/src/shapes/text_paths.rs +++ b/render-wasm/src/shapes/text_paths.rs @@ -1,11 +1,8 @@ use crate::render::text::{decoration_segments, list_markers}; use crate::shapes::text::{vertical_align_offset, Paragraph, TextContent}; +use crate::shapes::text_paragraph::LayoutParagraph as SkiaParagraph; use crate::shapes::VerticalAlign; -use skia_safe::{ - self as skia, - textlayout::{paragraph::VisitorInfo, Paragraph as SkiaParagraph}, - Point, -}; +use skia_safe::{self as skia, textlayout::paragraph::VisitorInfo, Point}; use std::ops::Deref; pub struct TextPaths(TextContent); @@ -16,14 +13,27 @@ impl TextPaths { } pub fn get_paths(&self, vertical_align: VerticalAlign) -> Vec { + if self.0.is_vertical() { + return crate::shapes::text_vertical::vertical_text_paths( + &self.0, + vertical_align, + true, + ) + .into_iter() + .map(|(path, _)| path) + .collect(); + } + let layout_width = self.0.get_width(self.bounds.width()); + let plans = self.0.horizontal_plans(); let mut paragraph_builders = self.0.paragraph_builder_group_from_text(None); let mut paragraphs: Vec = paragraph_builders .iter_mut() .filter_map(|group| group.first_mut()) - .map(|paragraph_builder| { - let mut paragraph = paragraph_builder.build(); - paragraph.layout(layout_width); + .enumerate() + .map(|(index, paragraph_builder)| { + let mut paragraph = SkiaParagraph::build(paragraph_builder); + paragraph.layout(plans[index].layout_width(layout_width)); paragraph }) .collect(); @@ -48,9 +58,21 @@ impl TextPaths { .collect(); let mut offset_y = top; - for (paragraph, text_paragraph) in paragraphs.iter_mut().zip(self.0.paragraphs()) { - let origin = Point::new(self.bounds.x(), offset_y); - Self::collect_paragraph_paths(paragraph, text_paragraph, origin, &mut paths); + for (index, (paragraph, text_paragraph)) in + paragraphs.iter_mut().zip(self.0.paragraphs()).enumerate() + { + let plan = &plans[index]; + let origin = Point::new( + self.bounds.x() + plan.x_offset(text_paragraph, layout_width), + offset_y, + ); + Self::collect_paragraph_paths( + paragraph, + text_paragraph, + &plan.offsets, + origin, + &mut paths, + ); offset_y += paragraph.height(); } @@ -60,22 +82,26 @@ impl TextPaths { fn collect_paragraph_paths( paragraph: &mut SkiaParagraph, text_paragraph: &Paragraph, + offsets: &crate::shapes::HorizontalOffsets, origin: Point, paths: &mut Vec, ) { - for deco in decoration_segments(paragraph, text_paragraph, origin.x, origin.y) { + for deco in decoration_segments(paragraph, text_paragraph, offsets, origin.x, origin.y) { let mut builder = skia::PathBuilder::new(); builder.add_rect(deco.rect(), None, None); paths.push(builder.detach()); } - paragraph.visit(|_: usize, info: Option<&VisitorInfo>| { + let shifts: Vec<_> = (0..paragraph.get_line_metrics().len()) + .map(|line| Point::new(paragraph.line_x_shift(line), paragraph.line_shift(line))) + .collect(); + paragraph.visit(|line: usize, info: Option<&VisitorInfo>| { let Some(info) = info else { return; }; let font = info.font(); - let run_origin = origin + info.origin(); + let run_origin = origin + info.origin() + shifts[line]; let mut builder = skia::PathBuilder::new(); let mut has_glyphs = false; diff --git a/render-wasm/src/shapes/text_policy.rs b/render-wasm/src/shapes/text_policy.rs new file mode 100644 index 0000000000..ea4918250c --- /dev/null +++ b/render-wasm/src/shapes/text_policy.rs @@ -0,0 +1,531 @@ +//! Paragraph typography policies. Geometry and source offsets are shared. +//! Regional presets use CLREQ (2026-10-07); Korean restrictions use KLREQ +//! (2026-03-21). These are named presets, not universal regional mandates. + +use super::cjk_chars::{ + is_han, COMMAS_AND_FULL_STOPS, FULLWIDTH_CLOSING_BRACKETS, FULLWIDTH_OPENING_BRACKETS, +}; +use super::jlreq::{ + break_allowed_between, forbidden_at_line_end, forbidden_at_line_start, + keeps_together_classified, pair_rule, JlreqClass, +}; + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum KoreanWrap { + #[default] + Syllable = 0, + /// Preserve the original experiment when old content is reopened. + SpacesOnly = 1, + KeepWords = 2, +} + +/// Regional Chinese preset, chosen by the region subtag only. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub enum ChineseRegion { + /// Original basic preset, retained for tags without a known region. + #[default] + Basic, + /// CN and SG. + Mainland, + /// TW. + Taiwan, + /// HK and MO. + HongKong, +} + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub enum TextLayoutPolicy { + #[default] + Plain, + Japanese, + Chinese(ChineseRegion), + Korean(KoreanWrap), +} + +/// Spacing between two adjacent characters of a pairwise policy, in em. +#[derive(Debug, Default, Clone, Copy, PartialEq)] +pub struct PairSpacing { + /// Removable punctuation side bearings. + pub compression: f32, + /// Han/Latin gap to add. + pub script_gap: f32, +} + +/// The parts of a BCP 47 language tag the layout and font choices read. +/// Subtags may be separated by `-` or `_`; matching ignores case. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct LanguageTag<'a> { + pub primary: &'a str, + /// Four-letter script subtag (`Hans`, `Hant`). + pub script: Option<&'a str>, + /// Two-letter or three-digit region subtag (`CN`, `TW`, `419`). + pub region: Option<&'a str>, +} + +impl<'a> LanguageTag<'a> { + pub fn parse(tag: &'a str) -> Self { + let mut subtags = tag.split(['-', '_']); + let primary = subtags.next().unwrap_or_default(); + let mut script = None; + let mut region = None; + // Script and region precede extensions and private use, which start + // at a singleton subtag. + for subtag in subtags.take_while(|subtag| subtag.len() != 1) { + if script.is_none() + && subtag.len() == 4 + && subtag.bytes().all(|b| b.is_ascii_alphabetic()) + { + script = Some(subtag); + } else if region.is_none() + && (subtag.len() == 2 + || (subtag.len() == 3 && subtag.bytes().all(|b| b.is_ascii_digit()))) + { + region = Some(subtag); + } + } + Self { + primary, + script, + region, + } + } + + pub fn is(&self, primary: &str) -> bool { + self.primary.eq_ignore_ascii_case(primary) + } + + pub fn has_script(&self, script: &str) -> bool { + self.script.is_some_and(|s| s.eq_ignore_ascii_case(script)) + } + + pub fn has_region(&self, regions: &[&str]) -> bool { + self.region + .is_some_and(|r| regions.iter().any(|region| r.eq_ignore_ascii_case(region))) + } +} + +fn numeric(ch: char) -> bool { + ch.is_numeric() || "一二三四五六七八九十百千萬万億亿〇零".contains(ch) +} + +fn opening(ch: char) -> bool { + FULLWIDTH_OPENING_BRACKETS.contains(ch) || "‘“([{".contains(ch) +} + +fn closing(ch: char) -> bool { + COMMAS_AND_FULL_STOPS.contains(ch) + || FULLWIDTH_CLOSING_BRACKETS.contains(ch) + || "!?:;’”)]},.!?:;".contains(ch) +} + +fn protected(ch: char) -> bool { + matches!( + ch, + '\u{00a0}' | '\u{202f}' | '\u{2060}' | '\u{feff}' | '\u{2011}' + ) +} + +impl TextLayoutPolicy { + /// The policy a paragraph language (a BCP 47 tag) selects. Script never + /// implies region. `korean_wrap` applies only to Korean. + pub fn for_language(language: &str, korean_wrap: KoreanWrap) -> Self { + let tag = LanguageTag::parse(language); + if tag.is("ja") { + Self::Japanese + } else if tag.is("zh") { + Self::Chinese(if tag.has_region(&["CN", "SG"]) { + ChineseRegion::Mainland + } else if tag.has_region(&["TW"]) { + ChineseRegion::Taiwan + } else if tag.has_region(&["HK", "MO"]) { + ChineseRegion::HongKong + } else { + ChineseRegion::Basic + }) + } else if tag.is("ko") { + Self::Korean(korean_wrap) + } else { + Self::Plain + } + } + + pub fn is_chinese(self) -> bool { + matches!(self, Self::Chinese(_)) + } + + pub fn is_korean(self) -> bool { + matches!(self, Self::Korean(_)) + } + + /// Japanese layout: JLREQ classes, pair rules, kinsoku, aki and the + /// squeeze/justify passes built on them. + pub fn uses_jlreq(self) -> bool { + self == Self::Japanese + } + + /// Horizontal layout texts always carry this policy's break rules. + /// Japanese plans them only for text with ruby or CJK characters + /// (`uses_japanese_layout`); plain text keeps Skia's breaks. + pub fn plans_breaks(self) -> bool { + matches!(self, Self::Chinese(_) | Self::Korean(_)) + } + + /// An indivisible unit wider than the box overflows whole instead of + /// taking Skia's emergency split. The legacy Korean spaces-only mode + /// keeps its earlier behaviour. + pub fn keeps_overlong_units(self) -> bool { + self == Self::Japanese || self.reopens_overlong_words() + } + + /// An overlong word may wrap at safe grapheme boundaries the policy's + /// emergency rules allow. + pub fn reopens_overlong_words(self) -> bool { + matches!( + self, + Self::Chinese(_) | Self::Korean(KoreanWrap::Syllable | KoreanWrap::KeepWords) + ) + } + + /// Justification expands the policy's pairwise opportunities by bounded + /// amounts (`expansion_em`) instead of leaving it to Skia. + pub fn bounded_justify(self) -> bool { + self.reopens_overlong_words() + } + + /// The legacy Korean spaces-only mode never justified vertical columns. + pub fn justifies_columns(self) -> bool { + self != Self::Korean(KoreanWrap::SpacesOnly) + } + + /// Whether a vertical column break measures the whole unit that would + /// move to the next column, or (legacy spaces-only) only the part up to + /// the overflowing cell. + pub fn measures_whole_unit(self) -> bool { + self != Self::Korean(KoreanWrap::SpacesOnly) + } + + /// Western word spaces set to one third em (JLREQ §3.2.6). + pub fn normalizes_western_spaces(self) -> bool { + self == Self::Japanese + } + + /// Automatic emphasis and underline positions follow CLREQ: emphasis + /// below horizontal text, underlines below the ink. + pub fn uses_clreq_positions(self) -> bool { + self.is_chinese() + } + + /// Whether horizontal layout text needs a WORD JOINER between two + /// adjacent grapheme bases, given with their JLREQ classes, to block a + /// break Skia would otherwise take. + pub fn needs_joiner(self, before: (char, JlreqClass), after: (char, JlreqClass)) -> bool { + if self.uses_jlreq() { + pair_rule(before.1, after.1).suppress_break_with_joiner + || keeps_together_classified(before, after) + } else { + !self.break_allowed(before.0, after.0) + } + } + + /// Even emergency wrapping must retain punctuation, nonbreaking controls, + /// numeric expressions and paired marks. Grapheme/annotation atomicity is + /// enforced by the caller on the complete paragraph, across style runs. + pub fn emergency_break_allowed(self, before: char, after: char) -> bool { + if matches!(self, Self::Plain | Self::Korean(KoreanWrap::SpacesOnly)) { + return true; + } + !protected(before) + && !protected(after) + && !opening(before) + && !closing(after) + && !(self.is_korean() && "‐‑–-・·々〻ー".contains(after)) + && !(self.uses_jlreq() + && (forbidden_at_line_start(after) || forbidden_at_line_end(before))) + && !(before == after && matches!(after, '…' | '‥' | '—' | '―')) + && !("$$£€₩¥¥₽₺₫++--−".contains(before) && numeric(after)) + && !(numeric(before) && "%%‰℃℉°".contains(after)) + && !(numeric(before) && matches!(after, '.' | ',' | '.' | ',')) + && !(matches!(before, '.' | ',' | '.' | ',') && numeric(after)) + } + + /// Legal boundary between graphemes, before fitting a line or column. + pub fn break_allowed(self, before: char, after: char) -> bool { + match self { + Self::Japanese => break_allowed_between(before, after), + Self::Korean(KoreanWrap::SpacesOnly) => before.is_whitespace() || after.is_whitespace(), + Self::Plain => true, + Self::Chinese(_) | Self::Korean(_) => { + self.emergency_break_allowed(before, after) + && !(before.is_ascii_alphanumeric() && after.is_ascii_alphanumeric()) + && !(self == Self::Korean(KoreanWrap::KeepWords) + && before.is_alphanumeric() + && after.is_alphanumeric()) + } + } + } + + /// Removable leading/trailing side bearings of full-width punctuation. + /// TW uses a solid preset. CN/HK compress only adjacent punctuation; fixed + /// marks (notably vertical colon/semicolon/question/exclamation) keep 1em. + pub fn punctuation_space(self, ch: char, vertical: bool) -> (f32, f32) { + let Self::Chinese(region @ (ChineseRegion::Mainland | ChineseRegion::HongKong)) = self + else { + return (0.0, 0.0); + }; + if FULLWIDTH_OPENING_BRACKETS.contains(ch) { + return (0.5, 0.0); + } + if FULLWIDTH_CLOSING_BRACKETS.contains(ch) { + return (0.0, 0.5); + } + let adjustable = COMMAS_AND_FULL_STOPS.contains(ch) + || (!vertical && region == ChineseRegion::Mainland && ":;!?".contains(ch)) + || (!vertical && region == ChineseRegion::HongKong && ":;".contains(ch)); + if !adjustable { + return (0.0, 0.0); + } + if region == ChineseRegion::Mainland { + (0.0, 0.5) + } else { + (0.25, 0.25) + } + } + + pub fn compression_em(self, before: char, after: char, vertical: bool) -> f32 { + let a = self.punctuation_space(before, vertical); + let b = self.punctuation_space(after, vertical); + // Both characters must be adjustable punctuation. Never trim ordinary + // Han cells or assume proportional ASCII punctuation contains half an em. + if a == (0.0, 0.0) || b == (0.0, 0.0) { + 0.0 + } else { + (a.1 + b.0).min(0.5) + } + } + + /// Pairwise spacing between two adjacent characters, in em: punctuation + /// compression to remove and the script gap to add. Each axis applies + /// its own caps and skips. + pub fn pair_spacing_em(self, before: char, after: char, vertical: bool) -> PairSpacing { + PairSpacing { + compression: self.compression_em(before, after, vertical), + script_gap: self.script_spacing_em(before, after), + } + } + + /// This preset permits hanging only in vertical traditional composition. + /// Consecutive marks are excluded by the planner. Horizontal CN uses + /// compression and push-out; no preset silently enables horizontal hanging. + pub fn hanging_em(self, ch: char, vertical: bool) -> f32 { + if vertical + && matches!( + self, + Self::Chinese(ChineseRegion::Taiwan | ChineseRegion::HongKong) + ) + && COMMAS_AND_FULL_STOPS.contains(ch) + { + 0.5 + } else { + 0.0 + } + } + + pub fn script_spacing_em(self, before: char, after: char) -> f32 { + let regional = matches!(self, Self::Chinese(region) if region != ChineseRegion::Basic); + if regional + && ((is_han(before) && after.is_ascii_alphanumeric()) + || (before.is_ascii_alphanumeric() && is_han(after))) + { + 0.25 + } else { + 0.0 + } + } + + /// Bounded justification: spaces first for Korean, character gaps for + /// Chinese and unspaced Korean syllable layout. Never stretch word mode. + pub fn expansion_em(self, before: char, after: char) -> (u8, f32) { + if !self.break_allowed(before, after) { + return (0, 0.0); + } + if before == ' ' && !after.is_whitespace() && self.is_korean() { + return (1, 0.5); + } + if self.is_chinese() + && !before.is_whitespace() + && !after.is_whitespace() + && (is_han(before) || is_han(after)) + { + return (1, 0.5); + } + if self == Self::Korean(KoreanWrap::Syllable) + && before.is_alphanumeric() + && after.is_alphanumeric() + { + return (2, 0.125); + } + (0, 0.0) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn language_selects_one_policy_without_script_detection() { + for (language, expected) in [ + ("", TextLayoutPolicy::Plain), + ("en", TextLayoutPolicy::Plain), + ("ja-JP", TextLayoutPolicy::Japanese), + ( + "ZH-Hant-TW", + TextLayoutPolicy::Chinese(ChineseRegion::Taiwan), + ), + ("ko", TextLayoutPolicy::Korean(KoreanWrap::Syllable)), + ] { + assert_eq!( + TextLayoutPolicy::for_language(language, KoreanWrap::Syllable), + expected + ); + } + assert_eq!( + TextLayoutPolicy::for_language("ko-KR", KoreanWrap::SpacesOnly), + TextLayoutPolicy::Korean(KoreanWrap::SpacesOnly) + ); + assert_eq!( + TextLayoutPolicy::for_language("zh", KoreanWrap::SpacesOnly), + TextLayoutPolicy::Chinese(ChineseRegion::Basic) + ); + } + + #[test] + fn chinese_regions_are_explicit_and_independent_of_script() { + let tag = LanguageTag::parse("zh-Hant-TW-x-hk"); + assert!(tag.is("ZH")); + assert!(tag.has_script("hant")); + assert!(tag.has_region(&["tw"])); + assert!(!tag.has_region(&["HK"])); + assert_eq!(LanguageTag::parse("es-419").region, Some("419")); + for (language, expected) in [ + ("zh-Hant-HK", ChineseRegion::HongKong), + ("zh-MO", ChineseRegion::HongKong), + ("zh-SG", ChineseRegion::Mainland), + ] { + assert_eq!( + TextLayoutPolicy::for_language(language, KoreanWrap::Syllable), + TextLayoutPolicy::Chinese(expected), + "{language}" + ); + } + let mainland = TextLayoutPolicy::for_language("zh-Hans-CN", KoreanWrap::Syllable); + let mainland_traditional = + TextLayoutPolicy::for_language("zh-Hant-CN", KoreanWrap::Syllable); + let taiwan = TextLayoutPolicy::for_language("zh-Hant-TW", KoreanWrap::Syllable); + let hong_kong = TextLayoutPolicy::for_language("zh-Hant-HK", KoreanWrap::Syllable); + assert_eq!(mainland, mainland_traditional); + assert_ne!(mainland, taiwan); + assert_ne!(taiwan, hong_kong); + assert_eq!( + mainland, + TextLayoutPolicy::for_language("ZH-cn", KoreanWrap::Syllable) + ); + assert_eq!( + TextLayoutPolicy::Chinese(ChineseRegion::Basic), + TextLayoutPolicy::for_language("zh-Hant", KoreanWrap::Syllable) + ); + } + + #[test] + fn korean_punctuation_and_numeric_units_stay_attached() { + let policy = TextLayoutPolicy::Korean(KoreanWrap::Syllable); + for (before, after) in [ + ('한', '-'), + ('한', '·'), + ('₩', '1'), + ('1', '%'), + ('1', '.'), + ('.', '2'), + (',', '2'), + (',', '2'), + ('-', '2'), + ('-', '2'), + ('…', '…'), + ('―', '―'), + ('한', '\u{00a0}'), + ('\u{00a0}', '글'), + ('한', '\u{2060}'), + ] { + assert!(!policy.break_allowed(before, after), "{before}|{after}"); + } + assert!(policy.break_allowed('한', '글')); + assert!(policy.break_allowed('한', '漢')); + } + + #[test] + fn word_mode_retains_punctuation_opportunities_and_blocks_letter_pairs() { + let policy = TextLayoutPolicy::for_language("ko-KR", KoreanWrap::KeepWords); + for (before, after) in [ + ('한', '글'), + ('漢', '字'), + ('한', 'A'), + ('A', '漢'), + ('1', '글'), + ] { + assert!(!policy.break_allowed(before, after)); + assert!(policy.emergency_break_allowed(before, after)); + } + for (before, after) in [ + ('글', '('), + (')', '한'), + ('-', '글'), + ('글', ' '), + (' ', '한'), + ] { + assert!(policy.break_allowed(before, after), "{before}|{after}"); + } + for (before, after) in [('(', '한'), ('글', ')'), ('한', ','), ('글', '\u{00a0}')] { + assert!(!policy.emergency_break_allowed(before, after)); + } + assert!(!TextLayoutPolicy::Korean(KoreanWrap::SpacesOnly).break_allowed(')', '한')); + } + + #[test] + fn regional_fitting_uses_its_own_spacing_and_orientation_rules() { + let cn = TextLayoutPolicy::Chinese(ChineseRegion::Mainland); + let tw = TextLayoutPolicy::Chinese(ChineseRegion::Taiwan); + let hk = TextLayoutPolicy::Chinese(ChineseRegion::HongKong); + assert_eq!(cn.compression_em(',', '」', false), 0.5); + assert_eq!(tw.compression_em(',', '」', false), 0.0); + assert_eq!(hk.compression_em(',', '」', false), 0.25); + assert_eq!(cn.punctuation_space('!', true), (0.0, 0.0)); + assert_eq!(hk.punctuation_space('!', false), (0.0, 0.0)); + assert_eq!(tw.hanging_em(',', false), 0.0); + assert_eq!(tw.hanging_em(',', true), 0.5); + assert_eq!(cn.script_spacing_em('中', 'A'), 0.25); + assert_eq!(cn.script_spacing_em(',', 'A'), 0.0); + assert_eq!( + TextLayoutPolicy::Korean(KoreanWrap::KeepWords).script_spacing_em('漢', 'A'), + 0.0 + ); + assert_eq!( + TextLayoutPolicy::Korean(KoreanWrap::KeepWords).expansion_em('한', '글'), + (0, 0.0) + ); + assert_eq!( + TextLayoutPolicy::Korean(KoreanWrap::KeepWords).expansion_em(' ', '한'), + (1, 0.5) + ); + } + + #[test] + fn chinese_basic_restrictions_do_not_include_small_kana() { + let policy = TextLayoutPolicy::Chinese(ChineseRegion::Basic); + assert!(!policy.break_allowed('「', '中')); + assert!(!policy.break_allowed('文', '」')); + assert!(policy.break_allowed('中', '文')); + assert!(policy.break_allowed('あ', 'っ')); + assert!(!TextLayoutPolicy::Japanese.break_allowed('あ', 'っ')); + } +} diff --git a/render-wasm/src/shapes/text_ruby.rs b/render-wasm/src/shapes/text_ruby.rs new file mode 100644 index 0000000000..465448ebb9 --- /dev/null +++ b/render-wasm/src/shapes/text_ruby.rs @@ -0,0 +1,356 @@ +//! Ruby (furigana, Pinyin) piece, distribution and overhang rules shared by +//! the horizontal and vertical engines (JLREQ §3.3). + +use std::collections::HashMap; +use std::hash::Hash; + +use crate::shapes::jlreq::{classify, JlreqClass}; +use crate::shapes::{RubyAlign, RubyOverhang}; + +/// Room a long reading may overhang one neighbouring character (JLREQ +/// §3.3.8, Fig 135): up to one ruby character, and half the neighbour, over +/// kana or a comma, full stop or middle dot without ruby of their own. +/// Kanji, brackets, another ruby base and a line edge (`None`) get no +/// overhang, nor does any neighbour under `RubyOverhang::None`. +pub(crate) fn ruby_overhang_room( + policy: RubyOverhang, + neighbour: Option, + neighbour_has_ruby: bool, + neighbour_extent: f32, + ruby_font_size: f32, +) -> f32 { + let overhangable = neighbour.is_some_and(|ch| { + matches!( + classify(ch), + JlreqClass::Hiragana + | JlreqClass::Katakana + | JlreqClass::SmallKana + | JlreqClass::ProlongedSoundMark + | JlreqClass::Comma + | JlreqClass::FullStop + | JlreqClass::MiddleDot + ) + }); + if policy == RubyOverhang::Auto && overhangable && !neighbour_has_ruby { + ruby_font_size.min(neighbour_extent / 2.0).max(0.0) + } else { + 0.0 + } +} + +/// How far a reading `overflow` longer than its base hangs over the +/// neighbours before and after it, given their `room`: centred where the room +/// allows, as `distribute_ruby_pieces` places it. Overflow past the room +/// spreads the base instead. +pub(crate) fn ruby_overhangs(overflow: f32, room: (f32, f32)) -> (f32, f32) { + let (before, after) = room; + if overflow <= 0.0 { + return (0.0, 0.0); + } + if overflow >= before + after { + return (before, after); + } + let hang_before = (overflow / 2.0) + .min(before) + .max((overflow - after).min(before)) + .max(0.0); + (hang_before, (overflow - hang_before).clamp(0.0, after)) +} + +/// Overhang that earlier readings already take on the character after them. +/// A kana between two readings lends the later one only what the earlier one +/// leaves (JLREQ Fig 138), so the two readings never meet over it. `K` +/// identifies a neighbouring character. +pub(crate) struct OverhangClaims { + claimed: HashMap, +} + +impl OverhangClaims { + pub(crate) fn new() -> Self { + Self { + claimed: HashMap::new(), + } + } + + /// Room (before, after) left for a reading `overflow` longer than its + /// base, given the `room` its neighbours `before` and `after` offer, in + /// reading order. Records how far the reading hangs over `after`. + pub(crate) fn room( + &mut self, + before: Option, + after: Option, + room: (f32, f32), + overflow: f32, + ) -> (f32, f32) { + let taken = before + .and_then(|key| self.claimed.get(&key)) + .copied() + .unwrap_or(0.0); + let room = ((room.0 - taken).max(0.0), room.1); + if let Some(after) = after { + self.claimed.insert(after, ruby_overhangs(overflow, room).1); + } + room + } +} + +/// Gap that spreads a base under a reading longer than itself (JLREQ +/// §3.3.8). What the reading set solid (`ruby_length`) overflows the base +/// by, less the overhang `room`, becomes one gap per base character: a full +/// gap between characters and half a gap at each end. +pub(crate) fn long_ruby_gap( + ruby_length: f32, + base_length: f32, + room: (f32, f32), + base_chars: usize, +) -> f32 { + if base_chars == 0 { + return 0.0; + } + let overflow = ruby_length - base_length - room.0 - room.1; + (overflow / base_chars as f32).max(0.0) +} + +/// Pieces a reading is set in (JLREQ §3.3.4): an upright glyph is a piece +/// of its own in a `slot`; a run of Western glyphs is one word at its own +/// advances, which distribution never spreads. `glyphs` holds (western, +/// advance) per glyph. Returns each piece's extent and, per glyph, its piece +/// and its offset inside it. +pub(crate) fn ruby_pieces(glyphs: &[(bool, f32)], slot: f32) -> (Vec, Vec<(usize, f32)>) { + let mut extents: Vec = Vec::new(); + let mut placement = Vec::with_capacity(glyphs.len()); + let mut previous_western = false; + for &(western, advance) in glyphs { + if western && previous_western { + let piece = extents.len() - 1; + placement.push((piece, extents[piece])); + extents[piece] += advance; + } else { + placement.push((extents.len(), 0.0)); + extents.push(if western { advance } else { slot }); + } + previous_western = western; + } + (extents, placement) +} + +/// Flow-axis start of each reading piece of `extents` along the base segment +/// `[seg_top, seg_top + seg_extent)`. Per jlreq: +/// +/// - A reading that fits the base is placed by `align`; `SpaceAround` is even +/// distribution (均等割り付け): an equal gap around each piece. +/// - A longer reading packs solid and overhangs the base, centred where the +/// `room` (before, after) of its neighbours allows. Layout grows the base so +/// the overflow fits that room. +pub(crate) fn distribute_ruby_pieces( + seg_top: f32, + seg_extent: f32, + extents: &[f32], + align: RubyAlign, + room: (f32, f32), +) -> Vec { + let count = extents.len(); + if count == 0 { + return Vec::new(); + } + let line: f32 = extents.iter().sum(); + let packed = |start: f32, gap: f32| -> Vec { + let mut cursor = start; + extents + .iter() + .map(|extent| { + let top = cursor; + cursor += extent + gap; + top + }) + .collect() + }; + if line <= seg_extent { + let free = seg_extent - line; + match align { + RubyAlign::SpaceAround => { + let gap = free / count as f32; + packed(seg_top + gap / 2.0, gap) + } + RubyAlign::Center => packed(seg_top + free / 2.0, 0.0), + RubyAlign::Start => packed(seg_top, 0.0), + RubyAlign::SpaceBetween if count > 1 => packed(seg_top, free / (count - 1) as f32), + RubyAlign::SpaceBetween => packed(seg_top + free / 2.0, 0.0), + } + } else { + let overflow = line - seg_extent; + let (before, after) = room; + let overhang = (overflow / 2.0) + .min(before) + .max((overflow - after).min(before)); + packed(seg_top - overhang.max(0.0), 0.0) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ruby_shorter_than_base_distributes_evenly() { + // A ruby line of 2 x 50 equals the base extent (100): one glyph per + // slot, with no offset. + let tops = + distribute_ruby_pieces(0.0, 100.0, &[50.0; 2], RubyAlign::SpaceAround, (50.0, 50.0)); + assert_eq!(tops.len(), 2); + assert!( + (tops[0] - 0.0).abs() < 0.001, + "first ruby glyph at slot start" + ); + assert!( + (tops[1] - 50.0).abs() < 0.001, + "second ruby glyph one slot down" + ); + + // A wide base (extent 200) spreads 2 glyphs of advance 50 into slots + // of 100. + let spread = + distribute_ruby_pieces(0.0, 200.0, &[50.0; 2], RubyAlign::SpaceAround, (50.0, 50.0)); + assert!( + (spread[0] - 25.0).abs() < 0.001, + "ruby glyph centred in its slot" + ); + assert!( + (spread[1] - spread[0] - 100.0).abs() < 0.001, + "even distribution keeps a full slot between glyphs, not the advance" + ); + } + + #[test] + fn ruby_longer_than_base_overhangs_symmetrically() { + // A ruby line of 80 over a 40 base centres on the base, overhanging + // each end by 20 (the one-em cap). + let tops = + distribute_ruby_pieces(0.0, 40.0, &[20.0; 4], RubyAlign::SpaceAround, (50.0, 50.0)); + assert_eq!(tops.len(), 4); + assert!(tops[0] < 0.0, "long ruby overhangs above the base top"); + let block_center = (tops[0] + tops[3] + 20.0) / 2.0; + assert!( + (block_center - 20.0).abs() < 0.001, + "the ruby block stays centred on the base" + ); + } + + #[test] + fn ruby_alignment_modes_control_short_annotation_distribution() { + assert_eq!( + distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::SpaceAround, (4.0, 4.0),), + vec![3.0, 13.0] + ); + assert_eq!( + distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::Center, (4.0, 4.0)), + vec![6.0, 10.0] + ); + assert_eq!( + distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::Start, (4.0, 4.0)), + vec![0.0, 4.0] + ); + assert_eq!( + distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::SpaceBetween, (4.0, 4.0),), + vec![0.0, 16.0] + ); + } + + #[test] + fn ruby_overhang_none_keeps_long_annotation_at_base_start() { + let automatic = distribute_ruby_pieces(0.0, 10.0, &[4.0; 4], RubyAlign::Center, (4.0, 4.0)); + let constrained = + distribute_ruby_pieces(0.0, 10.0, &[4.0; 4], RubyAlign::Center, (0.0, 0.0)); + + assert_eq!(automatic, vec![-3.0, 1.0, 5.0, 9.0]); + assert_eq!(constrained, vec![0.0, 4.0, 8.0, 12.0]); + } + + #[test] + fn long_ruby_gap_spreads_the_overflow_left_after_the_room() { + // 60 of reading over 40 of base, 4 of room on each side: 12 left + // over two characters, one gap each, half of it at each end. + assert_eq!(long_ruby_gap(60.0, 40.0, (4.0, 4.0), 2), 6.0); + assert_eq!(long_ruby_gap(20.0, 40.0, (0.0, 0.0), 2), 0.0); + assert_eq!(long_ruby_gap(60.0, 40.0, (0.0, 0.0), 0), 0.0); + } + + #[test] + fn ruby_overhangs_follow_the_distribution() { + assert_eq!(ruby_overhangs(0.0, (5.0, 5.0)), (0.0, 0.0)); + assert_eq!(ruby_overhangs(6.0, (5.0, 5.0)), (3.0, 3.0)); + assert_eq!(ruby_overhangs(6.0, (5.0, 0.0)), (5.0, 0.0)); + assert_eq!(ruby_overhangs(6.0, (0.0, 5.0)), (0.0, 5.0)); + assert_eq!(ruby_overhangs(20.0, (5.0, 5.0)), (5.0, 5.0)); + } + + #[test] + fn ruby_pieces_keep_a_western_word_whole() { + // エディ + "tor": three upright slots, then one word at its advances. + let glyphs = [ + (false, 9.0), + (false, 9.0), + (true, 4.0), + (true, 5.0), + (true, 3.0), + ]; + let (extents, placement) = ruby_pieces(&glyphs, 10.0); + assert_eq!(extents, vec![10.0, 10.0, 12.0]); + assert_eq!( + placement, + vec![(0, 0.0), (1, 0.0), (2, 0.0), (2, 4.0), (2, 9.0)] + ); + } + + #[test] + fn distributing_a_word_never_spreads_its_letters() { + // JLREQ Fig 112: "editor" over 編集者 is one piece centred on the base. + let tops = distribute_ruby_pieces(0.0, 60.0, &[30.0], RubyAlign::SpaceAround, (0.0, 0.0)); + assert_eq!(tops, vec![15.0]); + let tops = + distribute_ruby_pieces(0.0, 60.0, &[10.0, 30.0], RubyAlign::SpaceAround, (0.0, 0.0)); + assert_eq!(tops, vec![5.0, 25.0]); + } + + #[test] + fn ruby_overhang_room_allows_kana_and_punctuation_without_ruby() { + assert_eq!( + ruby_overhang_room(RubyOverhang::Auto, Some('か'), false, 20.0, 10.0), + 10.0 + ); + // JLREQ Fig 135: 偏、冠、脚 hangs かんむり over the commas; Fig 136: + // never over a bracket. + for ch in ['、', '。', '・'] { + assert_eq!( + ruby_overhang_room(RubyOverhang::Auto, Some(ch), false, 20.0, 10.0), + 10.0 + ); + } + for ch in ['「', '」'] { + assert_eq!( + ruby_overhang_room(RubyOverhang::Auto, Some(ch), false, 20.0, 10.0), + 0.0 + ); + } + assert_eq!( + ruby_overhang_room(RubyOverhang::Auto, Some('か'), false, 12.0, 10.0), + 6.0 + ); + assert_eq!( + ruby_overhang_room(RubyOverhang::Auto, Some('漢'), false, 20.0, 10.0), + 0.0 + ); + assert_eq!( + ruby_overhang_room(RubyOverhang::Auto, Some('か'), true, 20.0, 10.0), + 0.0 + ); + assert_eq!( + ruby_overhang_room(RubyOverhang::Auto, None, false, 20.0, 10.0), + 0.0 + ); + assert_eq!( + ruby_overhang_room(RubyOverhang::None, Some('か'), false, 20.0, 10.0), + 0.0 + ); + } +} diff --git a/render-wasm/src/shapes/text_shaping/font_tables.rs b/render-wasm/src/shapes/text_shaping/font_tables.rs new file mode 100644 index 0000000000..065a07e875 --- /dev/null +++ b/render-wasm/src/shapes/text_shaping/font_tables.rs @@ -0,0 +1,226 @@ +use std::cell::RefCell; +use std::collections::HashMap; +use std::rc::Rc; +use std::thread::LocalKey; + +use skia_safe::{Font, GlyphId}; + +use crate::shapes::gpos_vpal::{parse_vpal, VpalDelta}; + +/// Parsed font tables keyed by `SkTypefaceID`. `None` is cached too: a face +/// without the table never gains it. +type TableCache = RefCell>>>; + +thread_local! { + // Layout runs on every paint; each face's tables are parsed once. + static VERTICAL_METRICS: TableCache = RefCell::default(); + static VPAL_TABLES: TableCache = RefCell::default(); + static TYPO_METRICS: TableCache = RefCell::default(); +} + +fn cached_table( + cache: &'static LocalKey>, + font: &Font, + parse: impl FnOnce(&Font) -> Option, +) -> Option> { + let id = font.typeface().unique_id(); + cache.with(|cache| { + cache + .borrow_mut() + .entry(id) + .or_insert_with(|| parse(font).map(Rc::new)) + .clone() + }) +} + +fn units_per_em(font: &Font) -> Option { + let units_per_em = font.typeface().units_per_em()? as f32; + (units_per_em > 0.0).then_some(units_per_em) +} + +fn table_data(font: &Font, tag: &[u8; 4]) -> Option { + font.typeface().copy_table_data(u32::from_be_bytes(*tag)) +} + +/// Vertical advances from a font's `vhea`/`vmtx` tables. Upright cells +/// advance by the glyph's vertical advance, not its shaped horizontal one; +/// the two differ for vertical alternates and proportional glyphs (e.g. the +/// vertical kana repeat marks). +pub(crate) struct VerticalMetrics { + units_per_em: f32, + /// Advance heights of the first `advances.len()` glyph ids. + advances: Vec, + /// Advance for glyph ids at or beyond the long-metric count. + last: u16, +} + +impl VerticalMetrics { + /// Parses `vhea`/`vmtx`. `None` when the font has no vertical metrics + /// (callers keep horizontal advances). + pub(crate) fn from_font(font: &Font) -> Option { + let units_per_em = units_per_em(font)?; + let vhea = table_data(font, b"vhea")?; + let vmtx = table_data(font, b"vmtx")?; + let vhea = vhea.as_bytes(); + let vmtx = vmtx.as_bytes(); + // `numberOfLongVerMetrics` is the trailing u16 of the 36-byte header. + let num_long = u16::from_be_bytes([*vhea.get(34)?, *vhea.get(35)?]) as usize; + if num_long == 0 { + return None; + } + // Each long metric is { advanceHeight: u16, topSideBearing: i16 }. + let mut advances = Vec::with_capacity(num_long); + for i in 0..num_long { + let o = i * 4; + let (Some(&hi), Some(&lo)) = (vmtx.get(o), vmtx.get(o + 1)) else { + break; + }; + advances.push(u16::from_be_bytes([hi, lo])); + } + let last = *advances.last()?; + Some(Self { + units_per_em, + advances, + last, + }) + } + + /// Vertical advance of `glyph` at `font_size`, in pixels (same units as + /// the horizontal advances). + pub(crate) fn advance(&self, glyph: GlyphId, font_size: f32) -> f32 { + let raw = self + .advances + .get(glyph as usize) + .copied() + .unwrap_or(self.last); + raw as f32 * font_size / self.units_per_em + } +} + +pub(crate) fn vertical_metrics(font: &Font) -> Option> { + cached_table(&VERTICAL_METRICS, font, VerticalMetrics::from_font) +} + +/// GPOS `vpal` deltas for one typeface, in font units. +pub(crate) struct VpalTable { + units_per_em: f32, + deltas: HashMap, +} + +impl VpalTable { + fn from_font(font: &Font) -> Option { + let units_per_em = units_per_em(font)?; + let gpos = table_data(font, b"GPOS")?; + let deltas = parse_vpal(gpos.as_bytes())?; + Some(Self { + units_per_em, + deltas, + }) + } + + /// Pixel deltas for a cluster at `font_size`: summed advance delta + /// (negative when the cell tightens) and flow-axis ink shift (positive + /// down the column; GPOS `yPlacement` is y-up, so its sign flips). + /// `None` when no glyph of the cluster is covered. + pub(crate) fn cluster_delta(&self, glyphs: &[GlyphId], font_size: f32) -> Option<(f32, f32)> { + let scale = font_size / self.units_per_em; + let mut advance = 0.0f32; + let mut flow_shift = None; + for glyph in glyphs { + if let Some(delta) = self.deltas.get(glyph) { + advance += delta.y_advance as f32 * scale; + flow_shift.get_or_insert(-(delta.y_placement as f32) * scale); + } + } + flow_shift.map(|shift| (advance, shift)) + } +} + +pub(crate) fn vpal_table(font: &Font) -> Option> { + cached_table(&VPAL_TABLES, font, VpalTable::from_font) +} + +/// OS/2 typographic ascender/descender over unitsPerEm. They bound the +/// ideographic em box, which the browser uses for the vertical central +/// baseline. `hhea`/`Font::metrics` ascent exceeds the em in CJK faces and +/// would push an upright glyph off the centre of its cell. +struct TypoMetrics { + /// sTypoAscender / unitsPerEm (positive, above the baseline). + ascender: f32, + /// sTypoDescender / unitsPerEm (negative, below the baseline). + descender: f32, +} + +impl TypoMetrics { + fn from_font(font: &Font) -> Option { + let units_per_em = units_per_em(font)?; + let os2 = table_data(font, b"OS/2")?; + let os2 = os2.as_bytes(); + // sTypoAscender @ 68, sTypoDescender @ 70 (i16), in every OS/2 version. + let ascender = i16::from_be_bytes([*os2.get(68)?, *os2.get(69)?]) as f32; + let descender = i16::from_be_bytes([*os2.get(70)?, *os2.get(71)?]) as f32; + Some(Self { + ascender: ascender / units_per_em, + descender: descender / units_per_em, + }) + } +} + +/// Ascent/descent for centring an upright cell, in Skia's sign convention +/// (ascent negative, descent positive) at the font's size. Uses the OS/2 +/// typographic metrics, which match the browser's vertical central baseline +/// in the SVG/foreignObject export, or `Font::metrics` without an OS/2 table. +pub(crate) fn upright_centre_metrics(font: &Font) -> (f32, f32) { + if let Some(typo) = cached_table(&TYPO_METRICS, font, TypoMetrics::from_font) { + let size = font.size(); + return (-typo.ascender * size, -typo.descender * size); + } + let (_, metrics) = font.metrics(); + (metrics.ascent, metrics.descent) +} + +/// Offset from an upright cell's top edge to the glyph baseline. Centres the +/// glyph's line box in the em cell, as Tate-chu-yoko does. CJK faces have an +/// ascent larger than the em, so hanging glyphs from the ascent would push +/// them below their cells and overflow the column. +pub(crate) fn upright_baseline_offset(ascent: f32, descent: f32, font_size: f32) -> f32 { + font_size / 2.0 - (ascent + descent) / 2.0 +} + +/// Baseline offset for an upright glyph of `font` in an em cell of `font_size`. +pub(crate) fn upright_baseline(font: &Font, font_size: f32) -> f32 { + let (ascent, descent) = upright_centre_metrics(font); + upright_baseline_offset(ascent, descent, font_size) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shapes::text_vertical::test_support::{TEST_FONT, VMTX_TEST_FONT}; + use skia_safe::FontMgr; + + #[test] + fn vertical_metrics_parse_vmtx() { + let font_mgr = FontMgr::new(); + let typeface = font_mgr + .new_from_data(skia_safe::Data::new_copy(VMTX_TEST_FONT), None) + .unwrap(); + let font = Font::new(typeface, 20.0); + let vm = VerticalMetrics::from_font(&font).expect("vmtx present"); + // gid 1 = 〱 (2000 units), gid 3+ fall back to the last long metric. + assert!((vm.advance(1, 20.0) - 40.0).abs() < 0.01); + assert!((vm.advance(3, 20.0) - 20.0).abs() < 0.01); + assert!((vm.advance(99, 20.0) - 20.0).abs() < 0.01); + } + + #[test] + fn vertical_metrics_absent_without_vmtx() { + // The bundled Source Sans face carries no `vmtx`. + let font_mgr = FontMgr::new(); + let typeface = font_mgr + .new_from_data(skia_safe::Data::new_copy(TEST_FONT), None) + .unwrap(); + let font = Font::new(typeface, 20.0); + assert!(VerticalMetrics::from_font(&font).is_none()); + } +} diff --git a/render-wasm/src/shapes/text_shaping/mod.rs b/render-wasm/src/shapes/text_shaping/mod.rs new file mode 100644 index 0000000000..13dc95813a --- /dev/null +++ b/render-wasm/src/shapes/text_shaping/mod.rs @@ -0,0 +1,12 @@ +//! Glyph shaping shared by the vertical engine and the horizontal annotation +//! painters (ruby, Zhuyin, emphasis): SkShaper runs with per-span font +//! fallback, upright/sideways orientation and the font tables (vmtx, vpal, +//! OS/2 typo metrics) the upright em box comes from. + +mod font_tables; +mod orientation; +mod shaping; + +pub(crate) use font_tables::*; +pub(crate) use orientation::*; +pub(crate) use shaping::*; diff --git a/render-wasm/src/shapes/text_shaping/orientation.rs b/render-wasm/src/shapes/text_shaping/orientation.rs new file mode 100644 index 0000000000..36dc56baf2 --- /dev/null +++ b/render-wasm/src/shapes/text_shaping/orientation.rs @@ -0,0 +1,188 @@ +use crate::shapes::TextOrientation; +use unicode_segmentation::UnicodeSegmentation; + +/// Emoji ranges recognized by the frontend font-loader. Emoji stay upright +/// in vertical flow; Latin text rotates under `text-orientation: mixed`. +pub(crate) fn is_emoji_char(c: char) -> bool { + matches!(u32::from(c), + 0x2300..=0x23FF + | 0x2600..=0x27BF + | 0x2B00..=0x2BFF + | 0x1F000..=0x1FAFF + ) +} + +/// Characters that stay upright in vertical flow: kana, kanji, hangul, +/// bopomofo, CJK punctuation, full-width forms and emoji. Everything else +/// rotates sideways under `text-orientation: mixed`. A table of its own: it +/// leaves half-width katakana sideways, unlike `cjk_chars::is_kana`. +pub(crate) fn is_upright_char(c: char) -> bool { + is_emoji_char(c) + || matches!(u32::from(c), + 0x2E80..=0x2FDF // CJK radicals, Kangxi radicals + | 0x1100..=0x11FF // conjoining Hangul Jamo + | 0x3100..=0x318F // Bopomofo and compatibility Jamo + | 0x31A0..=0x31BF // Bopomofo extended + | 0xA960..=0xA97F // Hangul Jamo extended A + | 0xD7B0..=0xD7FF // Hangul Jamo extended B + | 0x3000..=0x303F // CJK symbols and punctuation + | 0x3040..=0x30FF // hiragana, katakana + | 0x31C0..=0x31EF // CJK strokes + | 0x31F0..=0x31FF // katakana phonetic extensions + | 0x3200..=0x33FF // enclosed CJK, CJK compatibility + | 0x3400..=0x4DBF // CJK unified ideographs extension A + | 0x4E00..=0x9FFF // CJK unified ideographs + | 0xAC00..=0xD7AF // hangul syllables + | 0xF900..=0xFAFF // CJK compatibility ideographs + | 0xFE30..=0xFE4F // CJK compatibility forms + | 0xFF00..=0xFF60 // full-width forms + | 0xFFE0..=0xFFE6 // full-width signs + | 0x20000..=0x2FA1F // CJK extensions B..F + ) +} + +/// Characters whose horizontal glyph needs a vertical alternate, normally +/// from `vert` / `vrt2`. When the face has no such substitution, the +/// horizontal glyph rotates clockwise as a fallback. Covers UAX #50 `Tr`, +/// plus comma/full-stop punctuation whose plain glyph sits in the wrong +/// half of the vertical em box. +pub(crate) fn uses_rotated_vertical_fallback(c: char) -> bool { + matches!(u32::from(c), + 0x2018..=0x2019 // single quotation marks + | 0x201C..=0x201D // double quotation marks + | 0x2329..=0x232A // angle brackets + | 0x3001..=0x3002 // ideographic comma / full stop + | 0x3008..=0x301F // CJK brackets, wave dash and quotation marks + | 0x3030 // wavy dash + | 0x30A0 // katakana-hiragana double hyphen + | 0x30FC // prolonged sound mark + | 0xFE50..=0xFE52 // small comma / ideographic comma / full stop + | 0xFE59..=0xFE5E // small brackets + | 0xFF08..=0xFF09 // full-width parentheses + | 0xFF0C // full-width comma + | 0xFF0D // full-width hyphen-minus + | 0xFF0E // full-width full stop + | 0xFF1A..=0xFF1B // full-width colon / semicolon + | 0xFF1C..=0xFF1E // full-width comparison signs + | 0xFF3B // full-width left square bracket + | 0xFF3D // full-width right square bracket + | 0xFF3F // full-width low line + | 0xFF5B..=0xFF60 // full-width braces, bars, tilde and white parentheses + | 0xFFE3 // full-width macron + ) +} + +#[derive(Debug, PartialEq)] +pub(crate) struct Segment { + pub text: String, + /// UTF-16 offset of the segment start within its span text. + pub utf16_start: usize, + pub upright: bool, +} + +/// Split text into maximal runs of same orientation. Under +/// `TextOrientation::Upright` every character is upright. +pub(crate) fn segment_by_orientation(text: &str, orientation: TextOrientation) -> Vec { + let mut segments: Vec = Vec::new(); + let mut utf16_offset = 0; + for grapheme in text.graphemes(true) { + let c = grapheme.chars().next().unwrap(); + let upright = orientation == TextOrientation::Upright || is_upright_char(c); + let emoji = is_emoji_char(c); + // CJK and emoji are both upright, but split them so the segment + // probe can select the emoji family: the provider's generic fallback + // iterator does not reliably switch from a CJK face to a color-emoji + // face. + match segments.last_mut() { + Some(last) + if last.upright == upright + && last.text.chars().next_back().is_some_and(is_emoji_char) == emoji => + { + last.text.push_str(grapheme) + } + _ => segments.push(Segment { + text: grapheme.to_string(), + utf16_start: utf16_offset, + upright, + }), + } + utf16_offset += grapheme.encode_utf16().count(); + } + segments +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn upright_classification() { + assert!(is_upright_char('あ')); + assert!(is_upright_char('ア')); + assert!(is_upright_char('漢')); + assert!(is_upright_char('。')); + assert!(is_upright_char('「')); + assert!(is_upright_char('ー')); + assert!(is_upright_char('!')); + assert!(!is_upright_char('A')); + assert!(!is_upright_char('1')); + assert!(!is_upright_char(' ')); + assert!(!is_upright_char('.')); + } + + #[test] + fn emoji_stay_upright_in_vertical_text() { + for emoji in ['😆', '💦', '🙇'] { + assert!(is_upright_char(emoji)); + } + + let segments = segment_by_orientation("久😆元💦🙇", TextOrientation::Mixed); + assert_eq!(segments.len(), 4); + assert!(segments.iter().all(|segment| segment.upright)); + assert_eq!(segments[1].text, "😆"); + assert_eq!(segments[3].text, "💦🙇"); + } + + #[test] + fn transformed_japanese_punctuation_has_a_rotated_font_fallback() { + for character in [ + '「', '」', '『', '』', '(', ')', '[', ']', '【', '】', '、', '。', 'ー', '〜', + ':', ';', + ] { + assert!( + uses_rotated_vertical_fallback(character), + "{character} needs a transformed vertical glyph" + ); + } + assert!(!uses_rotated_vertical_fallback('あ')); + assert!(!uses_rotated_vertical_fallback('漢')); + assert!(!uses_rotated_vertical_fallback('!')); + } + + #[test] + fn segments_mixed_text() { + let segments = segment_by_orientation("縦書きABC123です", TextOrientation::Mixed); + assert_eq!(segments.len(), 3); + assert_eq!(segments[0].text, "縦書き"); + assert!(segments[0].upright); + assert_eq!(segments[0].utf16_start, 0); + assert_eq!(segments[1].text, "ABC123"); + assert!(!segments[1].upright); + assert_eq!(segments[1].utf16_start, 3); + assert_eq!(segments[2].text, "です"); + assert!(segments[2].upright); + assert_eq!(segments[2].utf16_start, 9); + } + + #[test] + fn segments_upright_orientation_keeps_latin_upright() { + let segments = segment_by_orientation("縦ABC", TextOrientation::Upright); + assert_eq!(segments.len(), 1); + assert!(segments[0].upright); + } + + #[test] + fn segments_empty_text() { + assert!(segment_by_orientation("", TextOrientation::Mixed).is_empty()); + } +} diff --git a/render-wasm/src/shapes/text_shaping/shaping.rs b/render-wasm/src/shapes/text_shaping/shaping.rs new file mode 100644 index 0000000000..fd83a19bd6 --- /dev/null +++ b/render-wasm/src/shapes/text_shaping/shaping.rs @@ -0,0 +1,648 @@ +use std::cell::RefCell; +use std::ops::Range; + +use skia_safe::{ + self as skia, + shaper::{ + run_handler::{Buffer, RunInfo}, + Feature, RunHandler, + }, + textlayout::TypefaceFontProvider, + Font, FontMgr, GlyphId, Point as SkPoint, TextBlob, TextBlobBuilder, Typeface, +}; + +use crate::render::{FallbackFonts, DEFAULT_EMOJI_FONT}; +use crate::shapes::{FontFeatures, TextSpan}; + +use super::font_tables::upright_centre_metrics; + +/// JLREQ preferred width of a word space inside sideways Western text. +pub(crate) const WESTERN_WORD_SPACING_EM: f32 = 1.0 / 3.0; + +/// Candidate families for a span: its own font first, then emoji and the +/// registered fallback fonts (Noto etc.) in the span language's order. +pub(crate) fn span_font_families(span: &TextSpan, fallback_fonts: &FallbackFonts) -> Vec { + let mut families = vec![ + format!("{}", span.font_family), + DEFAULT_EMOJI_FONT.to_string(), + ]; + families.extend(fallback_fonts.for_text(&span.language, &span.text)); + families +} + +/// One shaped run: glyphs from a single font, pen-relative positions, +/// per-glyph horizontal advances and per-glyph UTF-8 cluster starts +/// (relative to the shaped segment text). +#[derive(Clone)] +pub struct ShapedRun { + pub font: Font, + pub glyphs: Vec, + pub positions: Vec, + pub advances: Vec, + pub clusters: Vec, + pub advance: f32, + pub utf8_range: Range, + /// Source text of `utf8_range`, attached to the run's text blobs. + pub text: String, + /// Local-y offset that puts the font's central baseline on the column + /// axis after 90° rotation. + pub rotated_baseline_shift: f32, +} + +impl ShapedRun { + /// Glyph clusters as (first glyph, glyph count). A combining sequence + /// shapes to one cluster and must stay in one cell. + pub(crate) fn cluster_spans(&self) -> impl Iterator + '_ { + let mut glyph = 0usize; + std::iter::from_fn(move || { + let cluster = *self.clusters.get(glyph)?; + let count = self.clusters[glyph..] + .iter() + .take_while(|other| **other == cluster) + .count(); + let span = (glyph, count); + glyph += count; + Some(span) + }) + } + + /// Flow offset from the run start of each scalar boundary (0..=`chars`) + /// of a sideways run, from its glyph positions; the last boundary is + /// `extent`. A scalar inside a ligature takes the next glyph's position. + pub(crate) fn scalar_flow_offsets(&self, extent: f32, chars: usize) -> Option> { + let origin = self.positions.first()?.x; + let mut offsets = Vec::with_capacity(chars + 1); + let mut utf8 = self.utf8_range.start; + for ch in self.text.chars().take(chars) { + let flow = self + .clusters + .iter() + .position(|cluster| *cluster as usize >= utf8) + .map_or(extent, |glyph| self.positions[glyph].x - origin); + offsets.push(flow); + utf8 += ch.len_utf8(); + } + offsets.push(extent); + (offsets.len() == chars + 1).then_some(offsets) + } + + /// Summed horizontal advance of a cluster. + pub(crate) fn cluster_advance(&self, glyph: usize, count: usize) -> f32 { + self.advances[glyph..glyph + count].iter().sum() + } + + /// Positions of a cluster's glyphs relative to its first glyph's pen x. + fn cluster_positions(&self, glyph: usize, count: usize) -> Vec { + let base_x = self.positions[glyph].x; + self.positions[glyph..glyph + count] + .iter() + .map(|position| SkPoint::new(position.x - base_x, position.y)) + .collect() + } + + /// Ink bounds of a cluster, relative to its first glyph's pen position. + pub(crate) fn cluster_ink_bounds(&self, glyph: usize, count: usize) -> Option { + glyph_run_ink_bounds( + &self.font, + &self.glyphs[glyph..glyph + count], + &self.cluster_positions(glyph, count), + ) + } + + /// Ink bounds of the whole run at its shaped positions. + pub(crate) fn ink_bounds(&self) -> Option { + glyph_run_ink_bounds(&self.font, &self.glyphs, &self.positions) + } + + /// Byte range in `text` of the clusters covering `glyph..glyph + count`. + fn text_range(&self, glyph: usize, count: usize) -> Range { + let base = self.utf8_range.start as u32; + let clusters = &self.clusters[glyph..glyph + count]; + let first = clusters.iter().copied().min().unwrap_or(base); + let last = clusters.iter().copied().max().unwrap_or(base); + // Clusters ascend along the run: the next larger one ends the text. + let end = self.clusters[glyph + count..] + .iter() + .copied() + .find(|cluster| *cluster > last) + .map_or(self.text.len(), |cluster| { + cluster.saturating_sub(base) as usize + }); + (first.saturating_sub(base) as usize).min(end)..end + } + + /// Text blob of `glyph..glyph + count` at `positions`, carrying the + /// clusters' source text so PDF output maps vertical alternates back to + /// the original characters. + fn text_blob(&self, glyph: usize, count: usize, positions: &[SkPoint]) -> Option { + let range = self.text_range(glyph, count); + let Some(text) = self + .text + .as_bytes() + .get(range.clone()) + .filter(|t| !t.is_empty()) + else { + let mut builder = TextBlobBuilder::new(); + let (glyphs, points) = builder.alloc_run_pos(&self.font, count, None); + glyphs.copy_from_slice(&self.glyphs[glyph..glyph + count]); + points.copy_from_slice(positions); + return builder.make(); + }; + let text_start = self.utf8_range.start as u32 + range.start as u32; + let mut builder = TextBlobBuilder::new(); + let (glyphs, points, utf8_text, clusters) = + builder.alloc_run_text_pos(&self.font, count, text.len(), None); + glyphs.copy_from_slice(&self.glyphs[glyph..glyph + count]); + points.copy_from_slice(positions); + utf8_text.copy_from_slice(text); + for (cluster, source) in clusters + .iter_mut() + .zip(&self.clusters[glyph..glyph + count]) + { + *cluster = source.saturating_sub(text_start); + } + builder.make() + } + + /// Text blob of one cluster, positioned from its first glyph's pen x. + pub(crate) fn cluster_blob(&self, glyph: usize, count: usize) -> Option { + self.text_blob(glyph, count, &self.cluster_positions(glyph, count)) + } + + /// Text blob of the whole run at its shaped positions. + pub(crate) fn blob(&self) -> Option { + self.text_blob(0, self.glyphs.len(), &self.positions) + } + + /// Text blob of one glyph at its pen origin. The first glyph of a + /// cluster carries the cluster's source text; the rest are glyph-only. + pub(crate) fn glyph_blob(&self, glyph: usize) -> Option { + self.glyphs.get(glyph)?; + let starts_cluster = glyph == 0 || self.clusters.get(glyph) != self.clusters.get(glyph - 1); + if starts_cluster { + self.text_blob(glyph, 1, &[SkPoint::default()]) + } else { + single_glyph_blob(&self.font, self.glyphs[glyph]) + } + } + + /// Local-y shift that centres a cluster's ink after a 90° rotation. + pub(crate) fn cluster_rotated_baseline_shift(&self, glyph: usize, count: usize) -> f32 { + rotated_run_baseline_shift( + &self.font, + &self.glyphs[glyph..glyph + count], + &self.cluster_positions(glyph, count), + ) + } + + /// Spreads the glyphs by `letter_spacing` each, like horizontal + /// per-glyph spacing. Returns the spaced run advance. + pub(crate) fn spread_glyphs(&mut self, letter_spacing: f32) -> f32 { + if letter_spacing != 0.0 { + for (i, position) in self.positions.iter_mut().enumerate() { + position.x += letter_spacing * i as f32; + } + } + self.advance + letter_spacing * self.glyphs.len() as f32 + } + + /// Sets ASCII word spaces in a sideways Western run to JLREQ's one-third + /// em. The space keeps its scalar and break opportunity; only its advance + /// and the following glyph positions change. + pub(crate) fn normalize_word_spaces(&mut self, segment_text: &str, font_size: f32) { + let clusters: Vec<(usize, usize)> = self.cluster_spans().collect(); + let mut accumulated_shift = 0.0f32; + for (glyph, count) in clusters { + for position in &mut self.positions[glyph..glyph + count] { + position.x += accumulated_shift; + } + let is_word_space = segment_text + .get(self.clusters[glyph] as usize..) + .and_then(|text| text.chars().next()) + == Some(' '); + if is_word_space { + let delta = + font_size * WESTERN_WORD_SPACING_EM - self.cluster_advance(glyph, count); + self.advances[glyph + count - 1] += delta; + accumulated_shift += delta; + } + } + self.advance += accumulated_shift; + } +} + +/// Text blob holding a single glyph drawn at its pen origin. +pub(crate) fn single_glyph_blob(font: &Font, glyph: GlyphId) -> Option { + let mut builder = TextBlobBuilder::new(); + let (glyphs, points) = builder.alloc_run_pos(font, 1, None); + glyphs[0] = glyph; + points[0] = SkPoint::default(); + builder.make() +} + +#[derive(Default)] +struct RunCollector { + runs: Vec, + scratch_glyphs: Vec, + scratch_positions: Vec, + scratch_clusters: Vec, +} + +impl RunHandler for RunCollector { + fn begin_line(&mut self) {} + + fn run_info(&mut self, _info: &RunInfo) {} + + fn commit_run_info(&mut self) {} + + fn run_buffer(&mut self, info: &RunInfo) -> Buffer<'_> { + self.scratch_glyphs.resize(info.glyph_count, 0); + self.scratch_positions + .resize(info.glyph_count, SkPoint::default()); + self.scratch_clusters.resize(info.glyph_count, 0); + Buffer { + glyphs: &mut self.scratch_glyphs, + positions: &mut self.scratch_positions, + offsets: None, + clusters: Some(&mut self.scratch_clusters), + point: SkPoint::default(), + } + } + + fn commit_run_buffer(&mut self, info: &RunInfo) { + if info.glyph_count == 0 { + return; + } + let origin = self.scratch_positions[0]; + let positions: Vec = self + .scratch_positions + .iter() + .map(|p| SkPoint::new(p.x - origin.x, p.y)) + .collect(); + let end = positions[0].x + info.advance.x; + let advances: Vec = positions + .iter() + .enumerate() + .map(|(i, position)| positions.get(i + 1).map_or(end, |next| next.x) - position.x) + .collect(); + self.runs.push(ShapedRun { + rotated_baseline_shift: central_baseline_shift(info.font), + font: info.font.clone(), + glyphs: self.scratch_glyphs.clone(), + positions, + advances, + clusters: self.scratch_clusters.clone(), + advance: info.advance.x, + utf8_range: info.utf8_range.clone(), + text: String::new(), + }); + } + + fn commit_line(&mut self) {} +} + +fn feature(tag: &[u8; 4]) -> Feature { + Feature { + tag: u32::from_be_bytes(*tag), + value: 1, + start: 0, + end: usize::MAX, + } +} + +/// Omits `vpal`: SkShaper shapes on a horizontal line, where HarfBuzz would +/// apply its y-placement deltas as glyph offsets without the advance deltas. +/// Vertical layout applies the parsed GPOS `vpal` to upright cells itself. +fn font_feature(font_features: FontFeatures) -> Option { + match font_features { + FontFeatures::None => None, + FontFeatures::Palt => Some(feature(b"palt")), + FontFeatures::Vpal => None, + } +} + +/// Shape one orientation segment on a single unbounded line. Upright +/// segments get the OpenType vertical substitution features so CJK +/// punctuation and brackets take their vertical forms. +pub(crate) fn shape_segment( + text: &str, + font: &Font, + upright: bool, + font_features: FontFeatures, + fallback: FontMgr, + language: &str, +) -> Vec { + let mut font_iter = skia::Shaper::new_font_mgr_run_iterator(text, font, Some(fallback.clone())); + let mut bidi_iter = skia::shapers::primitive::trivial_bidi_run_iterator(0, text.len()); + let mut script_iter = skia::Shaper::new_hb_icu_script_run_iterator(text); + let mut lang_iter = skia::Shaper::new_trivial_language_run_iterator(language, text.len()); + + let mut features = if upright { + vec![feature(b"vert"), feature(b"vrt2")] + } else { + vec![] + }; + if let Some(font_feature) = font_feature(font_features) { + features.push(font_feature); + } + + let mut collector = RunCollector::default(); + with_shaper(&fallback, |shaper| { + shaper.shape_with_iterators_and_features( + text, + &mut font_iter, + &mut bidi_iter, + &mut script_iter, + &mut lang_iter, + &features, + f32::MAX, + &mut collector, + ) + }); + for run in &mut collector.runs { + run.text = text + .get(run.utf8_range.clone()) + .unwrap_or_default() + .to_string(); + } + collector.runs +} + +thread_local! { + static SHAPER: RefCell> = const { RefCell::new(None) }; +} + +/// Runs `f` with this thread's shaper, built on first use. One shaper serves +/// every font manager: shaping with explicit iterators takes the fallback +/// from the font run iterator, never from the shaper. +fn with_shaper(fallback: &FontMgr, f: impl FnOnce(&skia::Shaper) -> R) -> R { + SHAPER.with(|cache| { + let mut cache = cache.borrow_mut(); + f(cache.get_or_insert_with(|| skia::Shaper::new(fallback.clone()))) + }) +} + +/// Combining marks, variation selectors and ZWJ belong to the preceding +/// grapheme. They stay in that font run even without a standalone cmap +/// entry, so HarfBuzz gets the complete shaping sequence. +fn extends_previous_grapheme(ch: char) -> bool { + matches!(u32::from(ch), + 0x0300..=0x036F + | 0x1AB0..=0x1AFF + | 0x1DC0..=0x1DFF + | 0x200D + | 0x20D0..=0x20FF + | 0x3099..=0x309A + | 0xFE00..=0xFE0F + | 0xFE20..=0xFE2F + | 0xE0100..=0xE01EF + ) +} + +/// Split `text` into byte ranges covered by one typeface each: the first of +/// `typefaces` with a glyph for the character, or the primary face. +fn typeface_chunks(text: &str, typefaces: &[Typeface]) -> Vec<(Range, usize)> { + let mut chunks: Vec<(Range, usize)> = Vec::new(); + for (byte, ch) in text.char_indices() { + let current = chunks.last().map(|(_, typeface)| *typeface); + let selected = match current { + Some(typeface) if extends_previous_grapheme(ch) => typeface, + _ => typefaces + .iter() + .position(|typeface| typeface.unichar_to_glyph(ch as i32) != 0) + .unwrap_or(0), + }; + match chunks.last_mut() { + Some((range, typeface)) if *typeface == selected => range.end = byte + ch.len_utf8(), + _ => chunks.push((byte..byte + ch.len_utf8(), selected)), + } + } + chunks +} + +/// Shape a segment with explicit per-character font fallback. +/// `TypefaceFontProvider` resolves named families, but some Skia builds lack +/// its character fallback hook, so glyph coverage is resolved here. +#[allow(clippy::too_many_arguments)] +pub(crate) fn shape_segment_with_fallbacks( + text: &str, + font_size: f32, + families: &[String], + font_provider: &TypefaceFontProvider, + upright: bool, + font_features: FontFeatures, + fallback_mgr: &FontMgr, + language: &str, +) -> Vec { + shape_with_typefaces( + text, + font_size, + &resolve_typefaces(families, font_provider), + upright, + font_features, + fallback_mgr, + language, + ) +} + +/// The typefaces of `families` that `font_provider` holds, in order. +pub(crate) fn resolve_typefaces( + families: &[String], + font_provider: &TypefaceFontProvider, +) -> Vec { + families + .iter() + .filter_map(|family| font_provider.match_family_style(family, skia::FontStyle::default())) + .collect() +} + +/// `shape_segment_with_fallbacks` with the families already resolved. +pub(crate) fn shape_with_typefaces( + text: &str, + font_size: f32, + typefaces: &[Typeface], + upright: bool, + font_features: FontFeatures, + fallback_mgr: &FontMgr, + language: &str, +) -> Vec { + if typefaces.is_empty() { + return Vec::new(); + } + + let mut result = Vec::new(); + for (range, typeface_index) in typeface_chunks(text, typefaces) { + let font = Font::new(typefaces[typeface_index].clone(), font_size); + let mut shaped = shape_segment( + &text[range.clone()], + &font, + upright, + font_features, + fallback_mgr.clone(), + language, + ); + for run in &mut shaped { + run.utf8_range = + (run.utf8_range.start + range.start)..(run.utf8_range.end + range.start); + for cluster in &mut run.clusters { + *cluster += range.start as u32; + } + } + result.extend(shaped); + } + result +} + +fn glyph_run_ink_bounds( + font: &Font, + glyphs: &[GlyphId], + positions: &[SkPoint], +) -> Option { + let mut bounds = vec![skia::Rect::default(); glyphs.len()]; + font.get_bounds(glyphs, &mut bounds, None); + + let mut ink = skia::Rect::new(f32::MAX, f32::MAX, f32::MIN, f32::MIN); + for (bound, position) in bounds.iter().zip(positions) { + if bound.right > bound.left && bound.bottom > bound.top { + ink.left = ink.left.min(bound.left + position.x); + ink.top = ink.top.min(bound.top + position.y); + ink.right = ink.right.max(bound.right + position.x); + ink.bottom = ink.bottom.max(bound.bottom + position.y); + } + } + + (ink.right > ink.left && ink.bottom > ink.top).then_some(ink) +} + +/// Offset that centres a local-y band on the column axis after the run is +/// rotated 90°. +pub(crate) fn rotated_baseline_shift(top: f32, bottom: f32) -> f32 { + -(top + bottom) / 2.0 +} + +/// Shift that puts the font's central baseline on the column axis: the +/// middle of the em box upright glyphs centre on (`upright_centre_metrics`), +/// scaled to 1em. It depends only on the font, so every sideways run of a +/// face shares one baseline whatever its ink. +fn central_baseline_shift(font: &Font) -> f32 { + let (ascent, descent) = upright_centre_metrics(font); + let band = descent - ascent; + if band <= 0.0 { + return 0.0; + } + let em_over = -ascent * font.size() / band; + em_over - font.size() / 2.0 +} + +/// Centres the glyphs' actual ink; ascent/descent metrics are only the +/// fallback for glyphs without visible ink. +fn rotated_run_baseline_shift(font: &Font, glyphs: &[GlyphId], positions: &[SkPoint]) -> f32 { + if let Some(ink) = glyph_run_ink_bounds(font, glyphs, positions) { + rotated_baseline_shift(ink.top, ink.bottom) + } else { + let (_, metrics) = font.metrics(); + rotated_baseline_shift(metrics.ascent, metrics.descent) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shapes::text_vertical::test_support::*; + use crate::shapes::text_vertical::CellKind; + + #[test] + fn selected_language_reaches_horizontal_and_vertical_localized_glyphs() { + use crate::shapes::{GrowType, TextAlign}; + use skia_safe::textlayout::{FontCollection, ParagraphBuilder}; + + let font_provider = provider(include_bytes!("../../fonts/cjk-locl-test.ttf")); + let mut fonts = FontCollection::new(); + fonts.set_asset_font_manager(Some(font_provider.clone().into())); + for (language, glyph, width) in [("ja", 2, 12.0), ("zh-Hans", 3, 14.0), ("ko", 4, 16.0)] { + let mut paragraph = vertical_paragraph(vec![make_span("中")], TextAlign::Left, 1.0); + paragraph.set_language(language.to_string()); + let content = content_of(vec![paragraph.clone()], 100.0, GrowType::Fixed); + let layout = layout_with(&font_provider, &content); + assert_eq!(layout.runs[0].glyphs, vec![glyph], "vertical {language}"); + + let span = ¶graph.children()[0]; + let style = span.to_style(&content.bounds(), &FallbackFonts::default(), false, 1.0); + assert_eq!(style.locale(), language); + let mut builder = ParagraphBuilder::new(¶graph.paragraph_to_style(), &fonts); + builder.push_style(&style); + builder.add_text(&span.text); + let mut horizontal = builder.build(); + horizontal.layout(100.0); + assert!( + (horizontal.longest_line() - width).abs() < 0.01, + "horizontal {language}: {}", + horizontal.longest_line() + ); + } + } + + #[test] + fn span_families_try_emoji_after_the_span_font() { + let families = span_font_families( + &make_span("😆"), + &FallbackFonts::from_families(["fallback".to_string()]), + ); + assert_eq!(families[1], DEFAULT_EMOJI_FONT); + assert_eq!(families[2], "fallback"); + } + + fn rotated_shift(text: &str) -> f32 { + let layout = layout_content(&make_content(&[text], 1000.0), 1000.0); + let CellKind::Rotated { run } = layout.cells[0].kind else { + panic!("expected a rotated run"); + }; + layout.runs[run].rotated_baseline_shift + } + + #[test] + fn rotated_runs_share_a_baseline_whatever_their_ink() { + // Ascenders and descenders must not move the baseline across the + // column. + let plain = rotated_shift("aaa"); + for text in ["aaaf", "aaag", "aaagf", "AAA"] { + assert!( + (rotated_shift(text) - plain).abs() < 0.01, + "{text} shifted from {plain} to {}", + rotated_shift(text) + ); + } + } + + #[test] + fn rotated_runs_centre_on_the_upright_em_box() { + // Noto Sans JP's ascent overshoots the em; sideways runs must centre + // on the same em box as upright glyphs, or full-width marks such as + // `…` sit off the column axis. + let typeface = FontMgr::new() + .new_from_data(skia_safe::Data::new_copy(VMTX_TEST_FONT), None) + .unwrap(); + let font = Font::new(typeface, EM); + let (ascent, descent) = upright_centre_metrics(&font); + let em_box_middle = -(ascent + descent) / 2.0; + let shift = central_baseline_shift(&font); + assert!( + (shift - em_box_middle).abs() < 0.01, + "sideways baseline shift {shift}, upright em box middle {em_box_middle}" + ); + } + + #[test] + fn rotated_lowercase_sits_on_the_column_axis() { + // Source Sans: the em-box middle falls within 2% of an em of the + // x-height middle, so lowercase looks centred in the column. + let layout = layout_content(&make_content(&["a"], 1000.0), 1000.0); + let CellKind::Rotated { run } = layout.cells[0].kind else { + panic!("expected a rotated run"); + }; + let run = &layout.runs[run]; + let ink = run.ink_bounds().expect("visible ink"); + let ink_middle = (ink.top + ink.bottom) / 2.0 + run.rotated_baseline_shift; + assert!(ink_middle.abs() < 0.02 * EM, "ink middle at {ink_middle}"); + } +} diff --git a/render-wasm/src/shapes/text_warichu.rs b/render-wasm/src/shapes/text_warichu.rs new file mode 100644 index 0000000000..e164a5688f --- /dev/null +++ b/render-wasm/src/shapes/text_warichu.rs @@ -0,0 +1,53 @@ +//! Warichu (割注): inline notes set in two half-size sub-lines. + +use crate::shapes::jlreq::{forbidden_at_line_end, forbidden_at_line_start}; + +pub const WARICHU_FONT_SCALE: f32 = 0.5; + +/// Char index where a warichu run splits into its two sub-lines: the +/// midpoint (first line longer), moved to the nearest split that keeps +/// kinsoku. At equal distance a forward move wins, pulling the mark up into +/// the first sub-line (jlreq). Falls back to the midpoint. +pub(crate) fn warichu_split_chars(text: &str) -> usize { + let chars: Vec = text.chars().collect(); + let n = chars.len(); + let mid = n.div_ceil(2); + let valid = |split: usize| { + split >= 1 + && split < n + && !forbidden_at_line_start(chars[split]) + && !forbidden_at_line_end(chars[split - 1]) + }; + if valid(mid) { + return mid; + } + for distance in 1..n { + if valid(mid + distance) { + return mid + distance; + } + if mid > distance && valid(mid - distance) { + return mid - distance; + } + } + mid +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn warichu_split_balances_and_respects_kinsoku() { + // Balanced midpoint when nothing forbids it, first line longer. + assert_eq!(warichu_split_chars("あいうえおか"), 3); + assert_eq!(warichu_split_chars("あいうえお"), 3); + // A comma at the midpoint may end the first sub-line... + assert_eq!(warichu_split_chars("あい、うえ"), 3); + // ...but must not start the second one: the split moves forward. + assert_eq!(warichu_split_chars("あいう、えお"), 4); + // An opening bracket must not end the first sub-line. + assert_eq!(warichu_split_chars("あい「うえお"), 4); + // Pathological all-forbidden text keeps the midpoint. + assert_eq!(warichu_split_chars("、、、、"), 2); + } +} diff --git a/render-wasm/src/shapes/text_zhuyin.rs b/render-wasm/src/shapes/text_zhuyin.rs new file mode 100644 index 0000000000..da056b1758 --- /dev/null +++ b/render-wasm/src/shapes/text_zhuyin.rs @@ -0,0 +1,196 @@ +// Mandarin Zhuyin annotations: one Han base and one syllable per span. + +use std::ops::Range; + +use skia_safe::{self as skia, textlayout::TypefaceFontProvider, FontMgr}; + +use super::cjk_chars::is_zhuyin_base; +use super::text_shaping::{shape_segment_with_fallbacks, span_font_families, ShapedRun}; +use super::TextSpan; +use crate::render::FallbackFonts; + +pub(crate) const FONT_SCALE: f32 = 0.3; +pub(crate) const STRIP_WIDTH: f32 = 0.6; + +pub(crate) struct Slot { + pub ch: char, + pub range: Range, + /// Centre of the glyph's ink, in base em, relative to the strip top-left. + pub x: f32, + pub y: f32, +} + +pub(crate) struct Syllable { + chars: Vec, + symbols: Vec, + tone: Option, +} + +impl Syllable { + pub fn parse(reading: &str) -> Option { + let chars: Vec = reading.chars().take(5).collect(); + if chars.len() > 4 { + return None; + } + let mut symbols = Vec::new(); + let mut tone = None; + for (index, ch) in chars.iter().enumerate() { + match ch { + '\u{3105}'..='\u{3129}' => symbols.push(index), + 'ˉ' | 'ˊ' | 'ˇ' | 'ˋ' | '˙' if tone.is_none() => tone = Some(index), + _ => return None, + } + } + if !(1..=3).contains(&symbols.len()) + || tone.is_some_and(|i| i + 1 != chars.len() && !(i == 0 && chars[i] == '˙')) + { + return None; + } + Some(Self { + chars, + symbols, + tone, + }) + } + + pub fn slots(&self) -> Vec { + let neutral = self.tone.is_some_and(|i| self.chars[i] == '˙'); + let lead = if neutral { 1.0 / 15.0 } else { 0.0 }; + let top = (1.0 - self.symbols.len() as f32 * FONT_SCALE - lead) / 2.0; + self.chars + .iter() + .enumerate() + .map(|(i, ch)| { + let (x, y) = if let Some(symbol) = self.symbols.iter().position(|s| *s == i) { + ( + FONT_SCALE / 2.0, + top + lead + (symbol as f32 + 0.5) * FONT_SCALE, + ) + } else if neutral { + (FONT_SCALE / 2.0, top + lead / 2.0) + } else { + (0.45, top + (self.symbols.len() as f32 - 1.0) * FONT_SCALE) + }; + Slot { + ch: *ch, + range: i..i + 1, + x, + y, + } + }) + .collect() + } +} + +/// A Han grapheme, including an optional variation selector. Do not attach +/// a syllable to a group, Latin text, or an incomplete surrogate sequence. +pub(crate) fn single_han(text: &str) -> bool { + let mut chars = text.chars(); + let Some(first) = chars.next() else { + return false; + }; + is_zhuyin_base(first) + && chars.next().is_none_or(|ch| { + matches!(ch as u32, 0xfe00..=0xfe0f | 0xe0100..=0xe01ef) && chars.next().is_none() + }) +} + +pub(crate) fn syllable(span: &TextSpan) -> Option { + (span.ruby_zhuyin && span.has_ruby() && single_han(&span.text)) + .then(|| Syllable::parse(span.ruby_text())) + .flatten() +} + +pub(crate) struct Glyph { + pub run: ShapedRun, + pub glyph: usize, + pub range: Range, + /// Drawing baseline relative to the base em's top-right corner. + pub origin: skia::Point, +} + +/// Shape each symbol upright, retaining each fallback font and source range. +/// Ink-centred placement also works for fonts with narrow spacing tone marks. +pub(crate) fn shape( + span: &TextSpan, + syllable: &Syllable, + provider: &TypefaceFontProvider, + fallbacks: &FallbackFonts, +) -> Vec { + let manager = FontMgr::from(provider.clone()); + let leading = span.ruby.encode_utf16().count() - span.ruby.trim_start().encode_utf16().count(); + syllable + .slots() + .into_iter() + .flat_map(|slot| { + shape_segment_with_fallbacks( + &slot.ch.to_string(), + span.font_size * FONT_SCALE, + &span_font_families(span, fallbacks), + provider, + false, + span.font_features, + &manager, + &span.language, + ) + .into_iter() + .flat_map(move |run| { + (0..run.glyphs.len()).map(move |glyph| { + let mut bounds = [skia::Rect::default()]; + run.font.get_bounds(&[run.glyphs[glyph]], &mut bounds, None); + Glyph { + origin: skia::Point::new( + slot.x * span.font_size - bounds[0].center_x(), + slot.y * span.font_size - bounds[0].center_y(), + ), + run: run.clone(), + glyph, + range: leading + slot.range.start..leading + slot.range.end, + } + }) + }) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn zhuyin_keeps_neutral_tone_above_and_other_tones_beside_the_last_symbol() { + for reading in ["ㄓ", "ㄋㄧˇ", "ㄓㄨㄤˋ", "˙ㄉㄜ", "ㄉㄜ˙", "ㄏㄠˊ", "ㄇㄚˉ"] + { + let syllable = Syllable::parse(reading).expect(reading); + let slots = syllable.slots(); + assert_eq!(slots.len(), reading.chars().count()); + let symbols: Vec<_> = slots.iter().filter(|s| s.ch >= '\u{3105}').collect(); + assert!(symbols.windows(2).all(|s| s[0].y < s[1].y)); + for tone in slots.iter().filter(|s| s.ch < '\u{3105}') { + if tone.ch == '˙' { + assert!(tone.y < symbols[0].y); + assert_eq!(tone.x, symbols[0].x); + } else { + assert!(tone.x > symbols.last().unwrap().x); + assert!(tone.y < symbols.last().unwrap().y); + } + } + } + } + + #[test] + fn zhuyin_rejects_malformed_or_multiple_syllables_without_guessing() { + for reading in [ + "", + "nǐ", + "ˇ", + "ㄋˇㄧ", + "ㄋㄧˇˋ", + "ㄋ ㄧ", + "ㄅㄆㄇㄈ", + "ㄋ\nㄧ", + ] { + assert!(Syllable::parse(reading).is_none(), "{reading:?}"); + } + } +} diff --git a/render-wasm/src/state/shapes_pool.rs b/render-wasm/src/state/shapes_pool.rs index cd9542068a..25e1e3f77a 100644 --- a/render-wasm/src/state/shapes_pool.rs +++ b/render-wasm/src/state/shapes_pool.rs @@ -270,6 +270,17 @@ impl ShapesPoolImpl { }) } + /// Drop both the base bounds and any transformed snapshot after a direct edit. + pub fn invalidate_shape_extrect(&mut self, id: &Uuid) { + let Some(idx) = self.uuid_to_idx.get(id).copied() else { + return; + }; + self.shapes[idx].invalidate_extrect(); + if let Some(cell) = self.modified_shape_cache.get_mut(&idx) { + *cell = OnceCell::new(); + } + } + /// Drops the extrect cache of every ancestor whose extrect is affected by this shape /// stopping at the first ancestor that clips. pub fn invalidate_ancestors_extrect(&mut self, id: &Uuid) { diff --git a/render-wasm/src/utils.rs b/render-wasm/src/utils.rs index 12c9c51f90..371ab0d8da 100644 --- a/render-wasm/src/utils.rs +++ b/render-wasm/src/utils.rs @@ -1,9 +1,9 @@ use crate::get_resources; +use crate::render::FallbackFonts; use crate::skia::textlayout::FontCollection; use crate::skia::Image; use crate::uuid::Uuid; use crate::with_state; -use std::collections::HashSet; pub fn uuid_from_u32_quartet(a: u32, b: u32, c: u32, d: u32) -> Uuid { let hi: u64 = ((a as u64) << 32) | b as u64; @@ -29,7 +29,7 @@ pub fn get_image(image_id: &Uuid) -> Option<&Image> { } // FIXME: move to a different place ? -pub fn get_fallback_fonts() -> &'static HashSet { +pub fn get_fallback_fonts() -> &'static FallbackFonts { get_resources().fonts.get_fallback() } diff --git a/render-wasm/src/wasm/fonts.rs b/render-wasm/src/wasm/fonts.rs index f3ad1d4189..161dd7453d 100644 --- a/render-wasm/src/wasm/fonts.rs +++ b/render-wasm/src/wasm/fonts.rs @@ -124,3 +124,11 @@ pub extern "C" fn is_font_uploaded( let family = FontFamily::new(id, weight, font_style.into()); get_resources().fonts.has_family(&family, is_emoji) } + +#[no_mangle] +pub extern "C" fn mark_font_as_fallback(a: u32, b: u32, c: u32, d: u32, weight: u32, style: u8) { + let id = uuid_from_u32_quartet(a, b, c, d); + let font_style = RawFontStyle::from(style); + let family = FontFamily::new(id, weight, font_style.into()); + get_resources().fonts.mark_as_fallback(&family); +} diff --git a/render-wasm/src/wasm/text.rs b/render-wasm/src/wasm/text.rs index 54e47defc4..7334fd770e 100644 --- a/render-wasm/src/wasm/text.rs +++ b/render-wasm/src/wasm/text.rs @@ -13,6 +13,9 @@ use crate::{with_current_shape, with_current_shape_mut, with_state}; use crate::error::Error; pub mod helpers; +mod layout_attrs; + +pub use layout_attrs::*; const RAW_SPAN_DATA_SIZE: usize = std::mem::size_of::(); const RAW_PARAGRAPH_DATA_SIZE: usize = std::mem::size_of::(); @@ -26,6 +29,7 @@ pub enum RawTextAlign { Center = 1, Right = 2, Justify = 3, + Distribute = 4, } impl From for TextAlign { @@ -35,6 +39,7 @@ impl From for TextAlign { RawTextAlign::Center => TextAlign::Center, RawTextAlign::Right => TextAlign::Right, RawTextAlign::Justify => TextAlign::Justify, + RawTextAlign::Distribute => TextAlign::Distribute, } } } @@ -132,7 +137,10 @@ pub struct RawParagraphData { line_height: f32, letter_spacing: f32, list_style_type: RawListStyleType, - _padding: [u8; 3], + writing_mode: u8, + text_orientation: u8, + korean_wrap: u8, + language_length: u32, } impl From<[u8; RAW_PARAGRAPH_DATA_SIZE]> for RawParagraphData { @@ -159,6 +167,19 @@ pub struct RawTextSpan { text_decoration: RawTextDecoration, text_transform: RawTextTransform, text_direction: RawTextDirection, + text_orientation: u8, + text_combine_upright: u8, + text_emphasis: u8, + warichu: u8, + font_features: u8, + annotation_clearance: u8, + ruby_size: u8, + ruby_align: u8, + ruby_overhang: u8, + /// Bit 0: over/under side of normal ruby. Bit 1: Zhuyin. + ruby_flags: u8, + text_emphasis_position: u8, + text_underline_position: u8, font_size: f32, line_height: f32, letter_spacing: f32, @@ -167,6 +188,7 @@ pub struct RawTextSpan { font_family: [u8; 4], font_variant_id: [u32; 4], // TODO: maybe add RawUUID type text_length: u32, + ruby_length: u32, fill_count: u32, fills: [RawFillData; MAX_TEXT_FILLS], } @@ -205,7 +227,7 @@ impl From for shapes::TextSpan { .map(|fill| fill.into()) .collect(); - Self::new( + let mut span = Self::new( text, font_family, value.font_size, @@ -217,7 +239,32 @@ impl From for shapes::TextSpan { value.font_weight, uuid_from_u32(value.font_variant_id), fills, - ) + ); + span.text_orientation = RawTextOrientation::from(value.text_orientation).into(); + span.text_combine_upright = RawTextCombineUpright::from(value.text_combine_upright).into(); + span.text_emphasis = RawTextEmphasis::from(value.text_emphasis).into(); + span.text_emphasis_position = + RawTextEmphasisPosition::from(value.text_emphasis_position).into(); + span.text_underline_position = + RawTextUnderlinePosition::from(value.text_underline_position).into(); + span.warichu = RawWarichu::from(value.warichu).into(); + span.font_features = RawFontFeatures::from(value.font_features).into(); + span.annotation_clearance = RawAnnotationClearance::from(value.annotation_clearance).into(); + span.ruby_size = RawRubySize::from(value.ruby_size).into(); + span.ruby_align = RawRubyAlign::from(value.ruby_align).into(); + span.ruby_overhang = RawRubyOverhang::from(value.ruby_overhang).into(); + // Bit 2 joins the span to the reading of the span before it, bit 1 + // selects Zhuyin and bit 0 retains the existing over/under side. + // Reject unknown flags instead of treating corrupt bytes as Zhuyin. + let ruby_flags = if value.ruby_flags <= 7 { + value.ruby_flags + } else { + 0 + }; + span.ruby_side = RawRubySide::from(ruby_flags & 1).into(); + span.ruby_zhuyin = ruby_flags & 2 != 0; + span.ruby_joins_previous = ruby_flags & 4 != 0; + span } } @@ -258,21 +305,31 @@ impl From for shapes::Paragraph { fn from(value: RawParagraph) -> Self { let mut spans = vec![]; + // Layout: [ ]. Annotation blobs + // begin after all base text. let mut offset = 0; + let mut ruby_offset: usize = value.spans.iter().map(|s| s.text_length as usize).sum(); for raw_span in value.spans.into_iter() { let delta = raw_span.text_length as usize; - let text_buffer = &value.text_buffer[offset..offset + delta]; - + let text_buffer = value.text_buffer.get(offset..offset + delta).unwrap_or(&[]); + let ruby_delta = raw_span.ruby_length as usize; + let ruby_buffer = value + .text_buffer + .get(ruby_offset..ruby_offset + ruby_delta) + .unwrap_or(&[]); let mut span = shapes::TextSpan::from(raw_span); if !text_buffer.is_empty() { span.set_text(String::from_utf8_lossy(text_buffer).to_string()); } - + if !ruby_buffer.is_empty() { + span.ruby = String::from_utf8_lossy(ruby_buffer).to_string(); + } spans.push(span); offset += delta; + ruby_offset += ruby_delta; } - shapes::Paragraph::new( + let mut paragraph = shapes::Paragraph::new( value.attrs.text_align.into(), value.attrs.text_direction.into(), value.attrs.text_decoration.into(), @@ -281,7 +338,17 @@ impl From for shapes::Paragraph { value.attrs.letter_spacing, spans, ) - .with_list_style_type(value.attrs.list_style_type.into()) + .with_list_style_type(value.attrs.list_style_type.into()); + paragraph.set_writing_mode(RawWritingMode::from(value.attrs.writing_mode).into()); + paragraph + .set_text_orientation(RawTextOrientation::from(value.attrs.text_orientation).into()); + let language = ruby_offset + .checked_add(value.attrs.language_length as usize) + .and_then(|end| value.text_buffer.get(ruby_offset..end)) + .unwrap_or(&[]); + paragraph.set_language(String::from_utf8_lossy(language).into_owned()); + paragraph.set_korean_wrap(RawKoreanWrap::from(value.attrs.korean_wrap).into()); + paragraph } } @@ -349,6 +416,15 @@ pub extern "C" fn set_shape_grow_type(grow_type: u8) { }); } +#[no_mangle] +pub extern "C" fn set_shape_line_adjustment(line_adjustment: u8) { + with_current_shape_mut!(state, |shape: &mut Shape| { + if let Type::Text(text_content) = &mut shape.shape_type { + text_content.set_line_adjustment(RawLineAdjustment::from(line_adjustment).into()); + } + }); +} + #[no_mangle] pub extern "C" fn get_text_dimensions() -> *mut u8 { let mut ptr = std::ptr::null_mut(); @@ -474,3 +550,208 @@ pub extern "C" fn calculate_position_data() -> *mut u8 { }); mem::write_vec(result) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::shapes::{ + AnnotationClearance, AnnotationSide, FontFeatures, RubyAlign, RubyOverhang, RubySize, + }; + + /// The shared CLJS writer writes PARAGRAPH-ATTR-U8-SIZE (20) + /// attr bytes after the u32 span count, and SPAN-ATTR-U8-SIZE (80) + /// attr bytes before the fills block. These sizes must match the struct + /// layouts. + #[test] + fn zhuyin_flags_keep_legacy_side_and_header_size() { + for (flags, zhuyin, side, joins) in [ + (0, false, AnnotationSide::Over, false), + (1, false, AnnotationSide::Under, false), + (2, true, AnnotationSide::Over, false), + (3, true, AnnotationSide::Under, false), + (4, false, AnnotationSide::Over, true), + (5, false, AnnotationSide::Under, true), + (255, false, AnnotationSide::Over, false), + ] { + let mut bytes = [0u8; std::mem::size_of::()]; + bytes[13] = flags; + let span: shapes::TextSpan = RawTextSpan::from(bytes).into(); + assert_eq!(span.ruby_zhuyin, zhuyin); + assert_eq!(span.ruby_side, side); + assert_eq!(span.ruby_joins_previous, joins); + } + } + + #[test] + fn raw_struct_sizes_match_cljs_writer() { + const PARAGRAPH_ATTR_U8_SIZE: usize = 20; + const SPAN_ATTR_U8_SIZE: usize = 80; + assert_eq!(RAW_PARAGRAPH_DATA_SIZE, 4 + PARAGRAPH_ATTR_U8_SIZE); + assert_eq!( + RAW_SPAN_DATA_SIZE, + SPAN_ATTR_U8_SIZE + MAX_TEXT_FILLS * std::mem::size_of::() + ); + } + + #[test] + fn word_wrap_byte_is_distinct_from_the_experiment_and_unknown_defaults() { + use crate::shapes::text_policy::KoreanWrap; + for (byte, expected) in [ + (0, KoreanWrap::Syllable), + (1, KoreanWrap::SpacesOnly), + (2, KoreanWrap::KeepWords), + (255, KoreanWrap::Syllable), + ] { + let mut attrs = [0u8; RAW_PARAGRAPH_DATA_SIZE]; + attrs[19] = byte; + let paragraph = shapes::Paragraph::from(RawParagraph { + attrs: RawParagraphData::from(attrs), + spans: vec![], + text_buffer: vec![], + }); + assert_eq!(paragraph.korean_wrap(), expected); + } + } + + #[test] + fn language_decodes_after_base_and_ruby_text() { + let mut attrs = [0u8; RAW_PARAGRAPH_DATA_SIZE]; + attrs[19] = 1; // Korean spaces-only POC; byte 15 after span count. + attrs[20..24].copy_from_slice(&2u32.to_le_bytes()); + let mut span = [0u8; RAW_SPAN_DATA_SIZE]; + span[68..72].copy_from_slice(&3u32.to_le_bytes()); + span[72..76].copy_from_slice(&3u32.to_le_bytes()); + let paragraph = shapes::Paragraph::from(RawParagraph { + attrs: RawParagraphData::from(attrs), + spans: vec![RawTextSpan::from(span)], + text_buffer: "韓한ko".as_bytes().to_vec(), + }); + assert_eq!(paragraph.language(), "ko"); + assert_eq!( + paragraph.korean_wrap(), + crate::shapes::text_policy::KoreanWrap::SpacesOnly + ); + assert_eq!(paragraph.children()[0].text, "韓"); + assert_eq!(paragraph.children()[0].ruby, "한"); + assert_eq!(paragraph.children()[0].language, "ko"); + } + + #[test] + fn raw_text_combine_upright_counts_deserialize() { + // Byte 5 of the span attr block; the counted digits variants map + // onto their max run length. + let mut bytes = [0u8; RAW_SPAN_DATA_SIZE]; + bytes[5] = RawTextCombineUpright::Digits2 as u8; + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + assert_eq!(span.text_combine_upright.digits_max(), Some(2)); + + bytes[5] = RawTextCombineUpright::Digits3 as u8; + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + assert_eq!(span.text_combine_upright.digits_max(), Some(3)); + + bytes[5] = RawTextCombineUpright::Digits as u8; + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + assert_eq!(span.text_combine_upright.digits_max(), Some(4)); + } + + #[test] + fn annotation_positions_decode_from_former_padding_and_unknown_defaults() { + let mut bytes = [0u8; RAW_SPAN_DATA_SIZE]; + bytes[14] = 4; + bytes[15] = 5; + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + assert_eq!( + span.text_emphasis_position, + shapes::TextEmphasisPosition::UnderLeft + ); + assert_eq!( + span.text_underline_position, + shapes::TextUnderlinePosition::UnderRight + ); + bytes[14] = 255; + bytes[15] = 255; + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + assert_eq!( + span.text_emphasis_position, + shapes::TextEmphasisPosition::Auto + ); + assert_eq!( + span.text_underline_position, + shapes::TextUnderlinePosition::Auto + ); + } + + #[test] + fn raw_font_features_deserializes_from_reserved_span_byte() { + let mut bytes = [0u8; RAW_SPAN_DATA_SIZE]; + bytes[8] = RawFontFeatures::Vpal as u8; + + let raw = RawTextSpan::from(bytes); + let span = shapes::TextSpan::from(raw); + + assert_eq!(span.font_features, FontFeatures::Vpal); + } + + #[test] + fn raw_annotation_clearance_deserializes_from_reserved_span_byte() { + let mut bytes = [0u8; RAW_SPAN_DATA_SIZE]; + bytes[9] = RawAnnotationClearance::Auto as u8; + + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + + assert_eq!(span.annotation_clearance, AnnotationClearance::Auto); + } + + #[test] + fn raw_ruby_customization_deserializes_from_span_bytes() { + let mut bytes = [0u8; RAW_SPAN_DATA_SIZE]; + bytes[10] = RawRubySize::Quarter as u8; + bytes[11] = RawRubyAlign::SpaceBetween as u8; + bytes[12] = RawRubyOverhang::None as u8; + bytes[13] = RawRubySide::Under as u8; + + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + + assert_eq!(span.ruby_size, RubySize::Quarter); + assert_eq!(span.ruby_align, RubyAlign::SpaceBetween); + assert_eq!(span.ruby_overhang, RubyOverhang::None); + assert_eq!(span.ruby_side, AnnotationSide::Under); + } + + #[test] + fn out_of_range_japanese_span_bytes_fall_back_to_defaults() { + let mut bytes = [0u8; RAW_SPAN_DATA_SIZE]; + bytes[4..14].fill(0xFF); + + let span = shapes::TextSpan::from(RawTextSpan::from(bytes)); + + assert_eq!(span.text_orientation, shapes::TextOrientation::Mixed); + assert_eq!(span.text_combine_upright, shapes::TextCombineUpright::None); + assert_eq!(span.text_emphasis, shapes::TextEmphasis::None); + assert!(!span.warichu); + assert_eq!(span.font_features, FontFeatures::None); + assert_eq!(span.annotation_clearance, AnnotationClearance::None); + assert_eq!(span.ruby_size, RubySize::Half); + assert_eq!(span.ruby_align, RubyAlign::SpaceAround); + assert_eq!(span.ruby_overhang, RubyOverhang::Auto); + assert_eq!(span.ruby_side, AnnotationSide::Over); + } + + #[test] + fn out_of_range_paragraph_writing_bytes_fall_back_to_defaults() { + let mut bytes = [0u8; RAW_PARAGRAPH_DATA_SIZE]; + bytes[17] = 0xFF; + bytes[18] = 0xFF; + + let attrs = RawParagraphData::from(bytes); + + assert_eq!( + shapes::WritingMode::from(RawWritingMode::from(attrs.writing_mode)), + shapes::WritingMode::HorizontalTb + ); + assert_eq!( + shapes::TextOrientation::from(RawTextOrientation::from(attrs.text_orientation)), + shapes::TextOrientation::Mixed + ); + } +} diff --git a/render-wasm/src/wasm/text/helpers.rs b/render-wasm/src/wasm/text/helpers.rs index adab557a50..099074e8be 100644 --- a/render-wasm/src/wasm/text/helpers.rs +++ b/render-wasm/src/wasm/text/helpers.rs @@ -472,9 +472,7 @@ pub fn delete_selection_range(text_content: &mut TextContent, selection: &TextSe delete_range_in_paragraph(&mut paragraphs[end_paragraph], 0, end.offset); let end_para_children: Vec<_> = std::mem::take(paragraphs[end_paragraph].children_mut()); - paragraphs[start.paragraph] - .children_mut() - .extend(end_para_children); + paragraphs[start.paragraph].extend_children(end_para_children); paragraphs.drain((start.paragraph + 1)..=end_paragraph); @@ -587,9 +585,7 @@ pub fn delete_char_before( let prev_para_len = paragraph_char_count(¶graphs[prev_para_idx]); let current_children: Vec<_> = std::mem::take(paragraphs[cursor.paragraph].children_mut()); - paragraphs[prev_para_idx] - .children_mut() - .extend(current_children); + paragraphs[prev_para_idx].extend_children(current_children); paragraphs.remove(cursor.paragraph); @@ -761,9 +757,7 @@ pub fn delete_char_after(text_content: &mut TextContent, cursor: &TextPositionWi } else if cursor.paragraph < paragraphs.len() - 1 { let next_para_idx = cursor.paragraph + 1; let next_children: Vec<_> = std::mem::take(paragraphs[next_para_idx].children_mut()); - paragraphs[cursor.paragraph] - .children_mut() - .extend(next_children); + paragraphs[cursor.paragraph].extend_children(next_children); paragraphs.remove(next_para_idx); } @@ -813,6 +807,11 @@ pub fn split_paragraph_at_cursor( let text_direction = para.text_direction(); let text_decoration = para.text_decoration(); let text_transform = para.text_transform(); + let writing_mode = para.writing_mode(); + let text_orientation = para.text_orientation(); + let language = para.language().to_owned(); + let korean_wrap = para.korean_wrap(); + let source_position = para.source_position(); let line_height = para.line_height(); let letter_spacing = para.letter_spacing(); let list_style_type = para.list_style_type(); @@ -829,7 +828,7 @@ pub fn split_paragraph_at_cursor( span.set_text(new_text); } - let new_para = crate::shapes::Paragraph::new( + let mut new_para = crate::shapes::Paragraph::new( text_align, text_direction, text_decoration, @@ -839,6 +838,11 @@ pub fn split_paragraph_at_cursor( new_para_children, ) .with_list_style_type(list_style_type); + new_para.set_writing_mode(writing_mode); + new_para.set_text_orientation(text_orientation); + new_para.set_language(language); + new_para.set_korean_wrap(korean_wrap); + new_para.set_source_position(source_position); paragraphs.insert(cursor.paragraph + 1, new_para); @@ -852,6 +856,56 @@ mod tests { use crate::shapes::{FontFamily, FontStyle, GrowType, ListStyleType, TextAlign, TextSpan}; use crate::uuid::Uuid; + #[test] + fn paragraph_split_keeps_language_and_korean_wrap() { + for wrap in [ + crate::shapes::text_policy::KoreanWrap::SpacesOnly, + crate::shapes::text_policy::KoreanWrap::KeepWords, + ] { + let mut para = paragraph(&["한국어"]); + para.set_language("ko".to_string()); + para.set_korean_wrap(wrap); + let mut content = content(vec![para]); + let cursor = TextPositionWithAffinity { + paragraph: 0, + offset: 1, + ..Default::default() + }; + assert!(split_paragraph_at_cursor(&mut content, &cursor)); + assert_eq!(content.paragraphs().len(), 2); + for para in content.paragraphs() { + assert_eq!(para.language(), "ko"); + assert_eq!(para.korean_wrap(), wrap); + assert_eq!(para.source_position(), 0); + assert!(para.children().iter().all(|span| span.language == "ko")); + } + } + } + + #[test] + fn paragraph_merge_inherits_the_surviving_paragraph_language() { + let mut first = paragraph(&["中"]); + first.set_language("ja".to_string()); + let mut second = paragraph(&["中"]); + second.set_language("ko".to_string()); + let mut content = content(vec![first, second]); + delete_char_after( + &mut content, + &TextPositionWithAffinity { + paragraph: 0, + offset: 1, + ..Default::default() + }, + ); + assert_eq!(content.paragraphs().len(), 1); + let paragraph = &content.paragraphs()[0]; + assert_eq!(paragraph.language(), "ja"); + assert!(paragraph + .children() + .iter() + .all(|span| span.language == "ja")); + } + fn span(text: &str) -> TextSpan { TextSpan::new( text.to_string(), diff --git a/render-wasm/src/wasm/text/layout_attrs.rs b/render-wasm/src/wasm/text/layout_attrs.rs new file mode 100644 index 0000000000..8b6316a713 --- /dev/null +++ b/render-wasm/src/wasm/text/layout_attrs.rs @@ -0,0 +1,365 @@ +use crate::shapes::text_policy::KoreanWrap; +use crate::shapes::{ + self, AnnotationClearance, AnnotationSide, FontFeatures, LineAdjustment, RubyAlign, + RubyOverhang, RubySize, TextCombineUpright, TextEmphasis, +}; +use macros::ToJs; + +/// Reads a style byte as `$raw`, falling back to `$default` when the byte +/// names no variant, so a bad byte from the CLJS writer never becomes an +/// invalid enum value. +macro_rules! raw_enum_from_byte { + ($raw:ident, $default:ident, [$($variant:ident),+ $(,)?]) => { + impl From for $raw { + fn from(value: u8) -> Self { + [$($raw::$variant),+] + .into_iter() + .find(|variant| *variant as u8 == value) + .unwrap_or($raw::$default) + } + } + }; +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawWritingMode { + HorizontalTb = 0, + VerticalRl = 1, +} + +raw_enum_from_byte!(RawWritingMode, HorizontalTb, [HorizontalTb, VerticalRl]); + +impl From for shapes::WritingMode { + fn from(value: RawWritingMode) -> Self { + match value { + RawWritingMode::HorizontalTb => shapes::WritingMode::HorizontalTb, + RawWritingMode::VerticalRl => shapes::WritingMode::VerticalRl, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawTextOrientation { + Mixed = 0, + Upright = 1, +} + +raw_enum_from_byte!(RawTextOrientation, Mixed, [Mixed, Upright]); + +impl From for shapes::TextOrientation { + fn from(value: RawTextOrientation) -> Self { + match value { + RawTextOrientation::Mixed => shapes::TextOrientation::Mixed, + RawTextOrientation::Upright => shapes::TextOrientation::Upright, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawTextCombineUpright { + None = 0, + All = 1, + Digits = 2, + Digits2 = 3, + Digits3 = 4, +} + +raw_enum_from_byte!( + RawTextCombineUpright, + None, + [None, All, Digits, Digits2, Digits3] +); + +impl From for TextCombineUpright { + fn from(value: RawTextCombineUpright) -> Self { + match value { + RawTextCombineUpright::None => TextCombineUpright::None, + RawTextCombineUpright::All => TextCombineUpright::All, + RawTextCombineUpright::Digits => TextCombineUpright::Digits, + RawTextCombineUpright::Digits2 => TextCombineUpright::Digits2, + RawTextCombineUpright::Digits3 => TextCombineUpright::Digits3, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawTextEmphasis { + None = 0, + FilledDot = 1, + OpenDot = 2, + FilledCircle = 3, + OpenCircle = 4, + FilledSesame = 5, + OpenSesame = 6, +} + +raw_enum_from_byte!( + RawTextEmphasis, + None, + [ + None, + FilledDot, + OpenDot, + FilledCircle, + OpenCircle, + FilledSesame, + OpenSesame + ] +); + +impl From for TextEmphasis { + fn from(value: RawTextEmphasis) -> Self { + match value { + RawTextEmphasis::None => TextEmphasis::None, + RawTextEmphasis::FilledDot => TextEmphasis::FilledDot, + RawTextEmphasis::OpenDot => TextEmphasis::OpenDot, + RawTextEmphasis::FilledCircle => TextEmphasis::FilledCircle, + RawTextEmphasis::OpenCircle => TextEmphasis::OpenCircle, + RawTextEmphasis::FilledSesame => TextEmphasis::FilledSesame, + RawTextEmphasis::OpenSesame => TextEmphasis::OpenSesame, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawTextEmphasisPosition { + Auto = 0, + OverRight = 1, + OverLeft = 2, + UnderRight = 3, + UnderLeft = 4, +} + +raw_enum_from_byte!( + RawTextEmphasisPosition, + Auto, + [Auto, OverRight, OverLeft, UnderRight, UnderLeft] +); + +impl From for shapes::TextEmphasisPosition { + fn from(value: RawTextEmphasisPosition) -> Self { + match value { + RawTextEmphasisPosition::Auto => Self::Auto, + RawTextEmphasisPosition::OverRight => Self::OverRight, + RawTextEmphasisPosition::OverLeft => Self::OverLeft, + RawTextEmphasisPosition::UnderRight => Self::UnderRight, + RawTextEmphasisPosition::UnderLeft => Self::UnderLeft, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawTextUnderlinePosition { + Auto = 0, + Under = 1, + Left = 2, + Right = 3, + UnderLeft = 4, + UnderRight = 5, +} + +raw_enum_from_byte!( + RawTextUnderlinePosition, + Auto, + [Auto, Under, Left, Right, UnderLeft, UnderRight] +); + +impl From for shapes::TextUnderlinePosition { + fn from(value: RawTextUnderlinePosition) -> Self { + match value { + RawTextUnderlinePosition::Auto => Self::Auto, + RawTextUnderlinePosition::Under => Self::Under, + RawTextUnderlinePosition::Left => Self::Left, + RawTextUnderlinePosition::Right => Self::Right, + RawTextUnderlinePosition::UnderLeft => Self::UnderLeft, + RawTextUnderlinePosition::UnderRight => Self::UnderRight, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawWarichu { + None = 0, + Warichu = 1, +} + +raw_enum_from_byte!(RawWarichu, None, [None, Warichu]); + +impl From for bool { + fn from(value: RawWarichu) -> Self { + value == RawWarichu::Warichu + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawFontFeatures { + None = 0, + Palt = 1, + Vpal = 2, +} + +raw_enum_from_byte!(RawFontFeatures, None, [None, Palt, Vpal]); + +impl From for FontFeatures { + fn from(value: RawFontFeatures) -> Self { + match value { + RawFontFeatures::None => FontFeatures::None, + RawFontFeatures::Palt => FontFeatures::Palt, + RawFontFeatures::Vpal => FontFeatures::Vpal, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawAnnotationClearance { + None = 0, + Auto = 1, +} + +raw_enum_from_byte!(RawAnnotationClearance, None, [None, Auto]); + +impl From for AnnotationClearance { + fn from(value: RawAnnotationClearance) -> Self { + match value { + RawAnnotationClearance::None => AnnotationClearance::None, + RawAnnotationClearance::Auto => AnnotationClearance::Auto, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawRubySize { + Half = 0, + Third = 1, + Quarter = 2, +} + +raw_enum_from_byte!(RawRubySize, Half, [Half, Third, Quarter]); + +impl From for RubySize { + fn from(value: RawRubySize) -> Self { + match value { + RawRubySize::Half => RubySize::Half, + RawRubySize::Third => RubySize::Third, + RawRubySize::Quarter => RubySize::Quarter, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawRubyAlign { + SpaceAround = 0, + Center = 1, + Start = 2, + SpaceBetween = 3, +} + +raw_enum_from_byte!( + RawRubyAlign, + SpaceAround, + [SpaceAround, Center, Start, SpaceBetween] +); + +impl From for RubyAlign { + fn from(value: RawRubyAlign) -> Self { + match value { + RawRubyAlign::SpaceAround => RubyAlign::SpaceAround, + RawRubyAlign::Center => RubyAlign::Center, + RawRubyAlign::Start => RubyAlign::Start, + RawRubyAlign::SpaceBetween => RubyAlign::SpaceBetween, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawRubyOverhang { + Auto = 0, + None = 1, +} + +raw_enum_from_byte!(RawRubyOverhang, Auto, [Auto, None]); + +impl From for RubyOverhang { + fn from(value: RawRubyOverhang) -> Self { + match value { + RawRubyOverhang::Auto => RubyOverhang::Auto, + RawRubyOverhang::None => RubyOverhang::None, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawRubySide { + Over = 0, + Under = 1, +} + +raw_enum_from_byte!(RawRubySide, Over, [Over, Under]); + +impl From for AnnotationSide { + fn from(value: RawRubySide) -> Self { + match value { + RawRubySide::Over => AnnotationSide::Over, + RawRubySide::Under => AnnotationSide::Under, + } + } +} + +// The variants are Adobe's Kinsoku Type values and the JS keys. +#[allow(clippy::enum_variant_names)] +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawLineAdjustment { + PushInFirst = 0, + PushOutFirst = 1, + PushOutOnly = 2, +} + +raw_enum_from_byte!( + RawLineAdjustment, + PushInFirst, + [PushInFirst, PushOutFirst, PushOutOnly] +); + +impl From for LineAdjustment { + fn from(value: RawLineAdjustment) -> Self { + match value { + RawLineAdjustment::PushInFirst => LineAdjustment::PushInFirst, + RawLineAdjustment::PushOutFirst => LineAdjustment::PushOutFirst, + RawLineAdjustment::PushOutOnly => LineAdjustment::PushOutOnly, + } + } +} + +#[derive(Debug, PartialEq, Clone, Copy, ToJs)] +#[repr(u8)] +pub enum RawKoreanWrap { + Syllable = 0, + SpacesOnly = 1, + KeepWords = 2, +} + +raw_enum_from_byte!(RawKoreanWrap, Syllable, [Syllable, SpacesOnly, KeepWords]); + +impl From for KoreanWrap { + fn from(value: RawKoreanWrap) -> Self { + match value { + RawKoreanWrap::Syllable => KoreanWrap::Syllable, + RawKoreanWrap::SpacesOnly => KoreanWrap::SpacesOnly, + RawKoreanWrap::KeepWords => KoreanWrap::KeepWords, + } + } +}