From 8c6ce999d9453ce89f34c5a0fa27ee9d8862755c Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 24 Nov 2026 13:58:18 +0100 Subject: [PATCH] :bug: Fix Mainland punctuation spacing in a Traditional Chinese font MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mainland spacing trims the right half of a stop's em, where its font leaves it blank. A Traditional Chinese face draws ,。:; centred, so the trimmed stop drifted from the text before it and crowded the next mark. The layout now measures where the font draws each stop and trims a centred one by a quarter em on each side, as Hong Kong spacing does. Add a Noto Serif TC subset as a test font. AI-assisted-by: claude-opus-5-5 --- .../src/fonts/notoseriftc-stops-test.ttf | Bin 0 -> 3764 bytes render-wasm/src/shapes/text_horizontal/mod.rs | 67 +++++++++++++- .../src/shapes/text_horizontal/tests.rs | 67 ++++++++++++++ render-wasm/src/shapes/text_policy.rs | 83 ++++++++++++++++-- 4 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 render-wasm/src/fonts/notoseriftc-stops-test.ttf diff --git a/render-wasm/src/fonts/notoseriftc-stops-test.ttf b/render-wasm/src/fonts/notoseriftc-stops-test.ttf new file mode 100644 index 0000000000000000000000000000000000000000..b2caa67bce2ed48fa0c6d7b4f520d045a1c500de GIT binary patch literal 3764 zcmbVPZ%kX)6+ibqV{B}LO$=uI+XE9rBI7tV7#2Db{sck-0b*zf+mbn8umjl0I3#3| zl4zZ_Rcn(?os_0p+ez!DY16c|+Z1X2&_!*NR8z8jXt!$9R#o|cDotsocG}c}_dEAJ zmym8+)o1Q|=bn4+Ilpu6Irm;j5RnTn6FIxPqI-{@RL>JR?BL$%9~qAJE)SkW|6TO8 z{^)r3Ld%f=QLz*K?&0W;(05K8`vWGop#SCQSl8IfvnMWKd<^~VQwy=h>-W~nG5!Sl zgR`-vMSL;WVZzrkJHIk>@8Yj_5{YUe?k*1!t5WqJJL!ViL+ldR2Wz9ijrFuDY^1mshzcBJ1z^)S-s8 z?~x#H?kD1i65{c_b**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eDVeai493$V@3cgR|enw|;C)^v(h-c=#qIAAP(Nx1jsI4@&QY()*zF zJ}A8pJ}^74L*Smqn%weTP0Fd+L{>0IksK0E+#vDDTdEf~*hbtx_kn>6jCgMy|9{%9 S=0a; Vec Vec> { + horizontal_pairwise_spacing_with(paragraph, texts, draws_stop_centred) +} + +/// Whether the font of `span` draws the regional stop `ch` in the centre of +/// its em, as Traditional Chinese faces do, rather than in the left half, +/// as mainland faces do. False when there are no fonts to measure. +fn draws_stop_centred(span: &TextSpan, ch: char) -> bool { + if !crate::globals::has_render_resources() { + return false; + } + let provider = get_resources().fonts.font_provider(); + draws_stop_centred_with(span, ch, provider, get_fallback_fonts()) +} + +fn draws_stop_centred_with( + span: &TextSpan, + ch: char, + provider: &skia::textlayout::TypefaceFontProvider, + fallbacks: &crate::render::FallbackFonts, +) -> bool { + let runs = shape_segment_with_fallbacks( + &ch.to_string(), + span.font_size.max(1.0), + &span_font_families(span, fallbacks), + provider, + false, + span.font_features, + &FontMgr::from(provider.clone()), + &span.language, + ); + let [run] = runs.as_slice() else { + return false; + }; + let count = run.glyphs.len(); + let advance = run.cluster_advance(0, count); + // A left-half mark centres its ink near a quarter of the em. + advance > 0.0 + && run + .cluster_ink_bounds(0, count) + .is_some_and(|ink| ink.center_x() > advance * 0.375) +} + +/// `horizontal_pairwise_spacing` with `centred` telling whether a span's +/// font draws a regional stop in the centre of its em. Only mainland +/// spacing asks, once per span and mark. +fn horizontal_pairwise_spacing_with( + paragraph: &Paragraph, + texts: &[String], + centred: impl Fn(&TextSpan, char) -> bool, +) -> Vec> { let mut result = vec![Vec::new(); texts.len()]; let policy = paragraph.layout_policy(); + let asks = policy == TextLayoutPolicy::Chinese(ChineseRegion::Mainland); + let mut measured: Vec<(usize, char, bool)> = Vec::new(); + let mut is_centred = |span: usize, ch: char| -> bool { + if !asks || !TextLayoutPolicy::is_regional_stop(ch) { + return false; + } + if let Some(&(_, _, known)) = measured.iter().find(|m| m.0 == span && m.1 == ch) { + return known; + } + let known = centred(¶graph.children()[span], ch); + measured.push((span, ch, known)); + known + }; let mut previous: Option<(usize, usize, char)> = None; for (span, text) in texts.iter().enumerate() { for (index, ch) in text.chars().enumerate() { @@ -161,7 +225,8 @@ fn horizontal_pairwise_spacing(paragraph: &Paragraph, texts: &[String]) -> Vec f32 { - let a = self.punctuation_space(before, vertical); - let b = self.punctuation_space(after, vertical); + /// True for the marks whose place in the em CLREQ sets by region in + /// horizontal text: the left half in mainland China, the centre in + /// Taiwan and Hong Kong. A font draws them in the way of its own region. + pub fn is_regional_stop(ch: char) -> bool { + COMMAS_AND_FULL_STOPS.contains(ch) || ":;!?".contains(ch) + } + + /// `punctuation_space` of `ch` as its font draws it. Mainland spacing + /// puts the blank half of a regional stop on its right; a stop its font + /// draws `centred` (a Traditional Chinese face) has a quarter em blank + /// on each side, and sheds those as Hong Kong does. + fn drawn_punctuation_space(self, ch: char, vertical: bool, centred: bool) -> (f32, f32) { + let (start, end) = self.punctuation_space(ch, vertical); + if centred + && !vertical + && self == Self::Chinese(ChineseRegion::Mainland) + && Self::is_regional_stop(ch) + { + let half = (start + end) / 2.0; + (half, half) + } else { + (start, end) + } + } + + /// Punctuation space to remove between two characters, in em, from + /// their removable side bearings `a` and `b`. + fn compression_between(a: (f32, f32), b: (f32, f32)) -> f32 { // Both characters must be adjustable punctuation. Never trim ordinary // Han cells or assume proportional ASCII punctuation contains half an em. if a == (0.0, 0.0) || b == (0.0, 0.0) { @@ -308,8 +333,24 @@ impl TextLayoutPolicy { /// compression to remove and the script gap to add. Each axis applies /// its own caps and skips. pub fn pair_spacing_em(self, before: char, after: char, vertical: bool) -> PairSpacing { + self.drawn_pair_spacing_em(before, after, vertical, (false, false)) + } + + /// `pair_spacing_em` for marks as their fonts draw them: `centred` tells, + /// for `before` and `after`, whether the font draws that character in + /// the centre of its em (see `drawn_punctuation_space`). + pub fn drawn_pair_spacing_em( + self, + before: char, + after: char, + vertical: bool, + centred: (bool, bool), + ) -> PairSpacing { PairSpacing { - compression: self.compression_em(before, after, vertical), + compression: Self::compression_between( + self.drawn_punctuation_space(before, vertical, centred.0), + self.drawn_punctuation_space(after, vertical, centred.1), + ), script_gap: self.script_spacing_em(before, after), } } @@ -496,9 +537,9 @@ mod tests { let cn = TextLayoutPolicy::Chinese(ChineseRegion::Mainland); let tw = TextLayoutPolicy::Chinese(ChineseRegion::Taiwan); let hk = TextLayoutPolicy::Chinese(ChineseRegion::HongKong); - assert_eq!(cn.compression_em(',', '」', false), 0.5); - assert_eq!(tw.compression_em(',', '」', false), 0.0); - assert_eq!(hk.compression_em(',', '」', false), 0.25); + assert_eq!(cn.pair_spacing_em(',', '」', false).compression, 0.5); + assert_eq!(tw.pair_spacing_em(',', '」', false).compression, 0.0); + assert_eq!(hk.pair_spacing_em(',', '」', false).compression, 0.25); assert_eq!(cn.punctuation_space('!', true), (0.0, 0.0)); assert_eq!(hk.punctuation_space('!', false), (0.0, 0.0)); assert_eq!(tw.hanging_em(',', false), 0.0); @@ -519,6 +560,34 @@ mod tests { ); } + #[test] + fn a_centred_mainland_stop_sheds_on_both_sides_as_in_hong_kong() { + let cn = TextLayoutPolicy::Chinese(ChineseRegion::Mainland); + let hk = TextLayoutPolicy::Chinese(ChineseRegion::HongKong); + let compression = |policy: TextLayoutPolicy, before, after, centred| { + policy + .drawn_pair_spacing_em(before, after, false, centred) + .compression + }; + // A left-half 。 gives its blank right half to 」; a centred one only + // has a quarter em there. + assert_eq!(compression(cn, '。', '」', (false, false)), 0.5); + assert_eq!(compression(cn, '。', '」', (true, false)), 0.25); + assert_eq!( + compression(cn, '。', '」', (true, false)), + compression(hk, '。', '」', (true, false)) + ); + // A centred : still meets the blank half of 「. + assert_eq!(compression(cn, ':', '「', (true, false)), 0.5); + // Brackets keep their own sides; vertical text places stops itself. + assert_eq!(compression(cn, '」', '「', (true, true)), 0.5); + assert_eq!( + cn.drawn_pair_spacing_em('。', '」', true, (true, false)) + .compression, + cn.pair_spacing_em('。', '」', true).compression + ); + } + #[test] fn chinese_basic_restrictions_do_not_include_small_kana() { let policy = TextLayoutPolicy::Chinese(ChineseRegion::Basic);