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