From 27299b213eba98c38c2c05a60552e6ec8396cab8 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:50:09 +0200 Subject: [PATCH 1/5] Fix blank glyphs inside overprinted numbers and word gaps next to taller glyphs in text extraction (#3172) --- crates/render/src/text.rs | 65 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/crates/render/src/text.rs b/crates/render/src/text.rs index 5054859961..dec2a77453 100644 --- a/crates/render/src/text.rs +++ b/crates/render/src/text.rs @@ -1257,6 +1257,43 @@ fn collide(glyphs: &[&TextGlyph], height: &dyn Fn(usize) -> f32, h: f32, sc: &mu dense } +/// Removes blank glyphs that are mostly covered by the ink of other glyphs on the line. A run of blanks drawn over +/// the digits of an earlier run (a cover headline that overprints a number with spaces) would otherwise sort between +/// the digits and show as spaces inside the number (#3172). A blank that stands in a gap is kept. +fn drop_covered_blanks(glyphs: &[&TextGlyph], placed: &mut Vec, ink: &mut Vec<[f32; 2]>) { + let blank = |i: usize| glyphs[i].text.trim().is_empty(); + if !placed.iter().any(|&i| blank(i)) { + return; + } + ink.clear(); + ink.extend(placed.iter().filter(|&&i| !blank(i)).map(|&i| [glyphs[i].rect[0], glyphs[i].rect[2]])); + ink.sort_by(|a, b| a[0].total_cmp(&b[0])); + // Merge into disjoint intervals. + ink.dedup_by(|cur, prev| { + let overlaps = cur[0] <= prev[1]; + if overlaps { + prev[1] = prev[1].max(cur[1]); + } + overlaps + }); + placed.retain(|&i| { + if !blank(i) { + return true; + } + let (l, r) = (glyphs[i].rect[0], glyphs[i].rect[2]); + if r - l <= 0.0 { + return true; + } + let mut covered = 0.0; + let mut k = ink.partition_point(|iv| iv[1] <= l); + while let Some(iv) = ink.get(k).filter(|iv| iv[0] < r) { + covered += iv[1].min(r) - iv[0].max(l); + k += 1; + } + covered <= 0.5 * (r - l) + }); +} + /// Simplified bidi class of a glyph or word gap (UAX #9: strong L/R, numbers, neutrals). #[derive(Clone, Copy, PartialEq, Eq)] enum Bidi { @@ -1590,9 +1627,14 @@ fn arrange(glyphs: &[TextGlyph]) -> (Vec, Vec, Vec) { // `spaces[w]`: a word gap left of the w-th glyph of the segment, by position on the line, not by drawing order // (#1062). Reused by every segment. let mut spaces: Vec = Vec::new(); + let mut ink: Vec<[f32; 2]> = Vec::new(); for b in block_order { for &si in &blocks[b].0 { place_glyphs(&glyphs, &height, &segs[si], &mut sc); + // Only another run can draw over a run's blanks: a single run is left as it is. + if sc.text.len() > 1 { + drop_covered_blanks(&glyphs, &mut sc.placed, &mut ink); + } let h = segs[si].h; // Word gaps relative to this line's typical letter gap (handles tracking). sc.gaps.clear(); @@ -1605,12 +1647,15 @@ fn arrange(glyphs: &[TextGlyph]) -> (Vec, Vec, Vec) { for (w, pair) in sc.placed.windows(2).enumerate() { let &[p, c] = pair else { continue }; let gap = glyphs[c].rect[0] - glyphs[p].rect[2]; + // The word gap follows the two glyphs it lies between, not the tallest glyph of the line (#3172). + let hp = height(p).min(height(c)); + let pair_threshold = (typical + hp * WORD_GAP).max(hp * WORD_GAP).min(threshold); let (pt, ct) = (&glyphs[p].text, &glyphs[c].text); let cjk = pt.chars().any(is_cjk) && ct.chars().any(is_cjk) && gap < h * 0.5; let tight_cluster = (pt.chars().any(is_complex) || ct.chars().any(is_complex)) && gap < h * 0.6; let run_start = sc.breaks.binary_search(&c).is_ok(); if let Some(space) = spaces.get_mut(w + 1) { - *space = (gap > threshold || run_start) && !cjk && !tight_cluster && !pt.trim().is_empty() && !ct.trim().is_empty(); + *space = (gap > pair_threshold || run_start) && !cjk && !tight_cluster && !pt.trim().is_empty() && !ct.trim().is_empty(); } } // Right-to-left spans are reversed (visual → logical) with their gaps, and only then is a second copy made. @@ -2113,6 +2158,24 @@ mod tests { assert_eq!(text.find_opts_limited("o", false, false, 10), vec![0..1, 1..2, 2..3, 3..4]); } + fn tall(t: &str, x0: f32, y0: f32, x1: f32, h: f32) -> TextGlyph { + let rect = [x0, y0, x1, y0 + h]; + TextGlyph { text: t.into(), rect, quad: [[x0, y0], [x1, y0], [x1, rect[3]], [x0, rect[3]]], direction: [1.0, 0.0] } + } + + #[test] + fn blanks_under_a_number_and_a_word_gap_next_to_big_digits() { + // Big dark digits, then a smaller sentence drawn over them with blanks where the digits sit. + let mut v = Vec::new(); + for (t, x) in [("5", 0.0), ("0", 6.0), ("1", 100.0), ("4", 106.0)] { + v.push(tall(t, x, 3.0, x + 6.0, 34.0)); + } + for (t, x) in [("F", 16.5), ("R", 22.5), (" ", 100.0), (" ", 106.0), ("C", 130.0), ("D", 136.0)] { + v.push(tall(t, x, 6.0, x + 6.0, 28.0)); + } + assert_eq!(layout(v).plain_text(), "50 FR 14 CD"); + } + fn word(v: &mut Vec, s: &str, x: f32, y: f32, advance: f32) { for (i, c) in s.chars().enumerate() { let x0 = x + i as f32 * advance; From acc733266371ee7027c1be704925f044a5c71c0a Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Wed, 4 Nov 2026 08:52:22 +0100 Subject: [PATCH 2/5] Keep word breaks of removed blanks and bound the blank coverage work (#3172) --- crates/render/src/text/layout.rs | 5 ++- crates/render/src/text/order.rs | 74 ++++++++++++++++++++++++++++++++ crates/render/src/text/tests.rs | 31 +++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/crates/render/src/text/layout.rs b/crates/render/src/text/layout.rs index 99afc0f8a3..81b08a3e17 100644 --- a/crates/render/src/text/layout.rs +++ b/crates/render/src/text/layout.rs @@ -428,12 +428,15 @@ fn arrange(glyphs: &[TextGlyph]) -> (Vec, Vec, Vec) { for (w, pair) in sc.placed.windows(2).enumerate() { let &[p, c] = pair else { continue }; let gap = glyphs[c].rect[0] - glyphs[p].rect[2]; + // The word gap follows the two glyphs it lies between, not the tallest glyph of the line (#3172). + let hp = height(p).min(height(c)); + let pair_threshold = (typical + hp * WORD_GAP).max(hp * WORD_GAP).min(threshold); let (pt, ct) = (&glyphs[p].text, &glyphs[c].text); let cjk = pt.chars().any(is_cjk) && ct.chars().any(is_cjk) && gap < h * 0.5; let tight_cluster = (pt.chars().any(is_complex) || ct.chars().any(is_complex)) && gap < h * 0.6; let run_start = sc.breaks.binary_search(&c).is_ok(); if let Some(space) = spaces.get_mut(w + 1) { - *space = (gap > threshold || run_start) && !cjk && !tight_cluster && !pt.trim().is_empty() && !ct.trim().is_empty(); + *space = (gap > pair_threshold || run_start) && !cjk && !tight_cluster && !pt.trim().is_empty() && !ct.trim().is_empty(); } } // Right-to-left spans are reversed (visual → logical) with their gaps, and only then is a second copy made. diff --git a/crates/render/src/text/order.rs b/crates/render/src/text/order.rs index efcabb5f1a..ed5ea72c79 100644 --- a/crates/render/src/text/order.rs +++ b/crates/render/src/text/order.rs @@ -152,6 +152,8 @@ pub(super) struct Scratch { pub(super) logical: Vec, /// `placed` with the marks merged in, built before it is swapped in. pub(super) merged: Vec, + /// The merged ink intervals of the current segment, each with the ink length before it, for `drop_covered_blanks`. + ink: Vec<[f32; 3]>, } /// One run of a colliding component. @@ -264,6 +266,8 @@ pub(super) fn place_glyphs(glyphs: &[&TextGlyph], height: &dyn Fn(usize) -> f32, sc.placed.sort_unstable_by(|a, b| x0(*a).total_cmp(&x0(*b)).then(a.cmp(b))); } else { collide(glyphs, height, s.h, sc); + // Only another run can draw over a run's blanks: a single run is left as it is. + drop_covered_blanks(glyphs, sc); } // Each mark goes before the first placed glyph whose x is greater than its own. Marks are visited in x order, and // that position never moves back, so one pass over `placed` puts all of them in. @@ -283,6 +287,76 @@ pub(super) fn place_glyphs(glyphs: &[&TextGlyph], height: &dyn Fn(usize) -> f32, } } +/// Removes blank glyphs from `sc.placed` that are mostly covered by the ink of other glyphs on the line. A run of blanks +/// drawn over the digits of an earlier run (a cover headline that overprints a number with spaces) would otherwise sort +/// between the digits and show as spaces inside the number (#3172). A blank that stands in a gap is kept. A forced word +/// break on a removed blank moves to the next glyph of its run that is kept. Each blank costs two binary searches. +fn drop_covered_blanks(glyphs: &[&TextGlyph], sc: &mut Scratch) { + let blank = |i: usize| glyphs[i].text.trim().is_empty(); + if !sc.placed.iter().any(|&i| blank(i)) { + return; + } + let ink = &mut sc.ink; + ink.clear(); + ink.extend(sc.placed.iter().filter(|&&i| !blank(i)).map(|&i| [glyphs[i].rect[0], glyphs[i].rect[2], 0.0])); + ink.sort_by(|a, b| a[0].total_cmp(&b[0])); + // Merge into disjoint intervals. + ink.dedup_by(|cur, prev| { + let overlaps = cur[0] <= prev[1]; + if overlaps { + prev[1] = prev[1].max(cur[1]); + } + overlaps + }); + // `iv[2]` is the ink length of all intervals before this one. + let mut total = 0.0; + for iv in ink.iter_mut() { + iv[2] = total; + total += iv[1] - iv[0]; + } + let ink = &sc.ink; + let covered = |l: f32, r: f32| { + let first = ink.partition_point(|iv| iv[1] <= l); + let end = ink.partition_point(|iv| iv[0] < r); + let (Some(head), Some(last)) = (ink.get(first), end.checked_sub(1).and_then(|k| ink.get(k))) else { return 0.0 }; + if end <= first { + return 0.0; + } + let after = ink.get(end).map_or(total, |iv| iv[2]); + after - head[2] - (l - head[0]).max(0.0) - (last[1] - r).max(0.0) + }; + sc.merged.clear(); + for &i in &sc.placed { + let (l, r) = (glyphs[i].rect[0], glyphs[i].rect[2]); + if blank(i) && r - l > 0.0 && covered(l, r) > 0.5 * (r - l) { + sc.merged.push(i); + } + } + if sc.merged.is_empty() { + return; + } + // `placed` is not sorted by index, so the dropped glyphs are sorted for the lookups. + sc.merged.sort_unstable(); + let dropped = &sc.merged; + sc.placed.retain(|i| dropped.binary_search(i).is_err()); + for b in sc.breaks.iter_mut() { + if dropped.binary_search(b).is_err() { + continue; + } + let x0 = |i: usize| glyphs[i].rect[0]; + let run = sc.text.iter().find(|run| run.range().contains(b)); + let kept = run.and_then(|run| { + let mut left = run.range().filter(|j| dropped.binary_search(j).is_err()); + left.min_by(|a, c| x0(*a).total_cmp(&x0(*c)).then(a.cmp(c))) + }); + *b = kept.unwrap_or(NONE); + } + sc.breaks.retain(|&b| b != NONE); + sc.breaks.sort_unstable(); + sc.breaks.dedup(); + sc.merged.clear(); +} + /// `place_glyphs` for a segment of two or more text runs. `h` is the segment's line height. Returns whether the segment /// was too dense to sweep ([`COLLIDE_PAIRS_PER_GLYPH`]), in which case all its text runs form one component. /// diff --git a/crates/render/src/text/tests.rs b/crates/render/src/text/tests.rs index a1af42a8d0..6bf5d13f31 100644 --- a/crates/render/src/text/tests.rs +++ b/crates/render/src/text/tests.rs @@ -785,6 +785,37 @@ fn limited_search_stops_after_the_requested_matches() { assert_eq!(text.find_opts_limited("o", false, false, 10), vec![0..1, 1..2, 2..3, 3..4]); } +fn tall(t: &str, x0: f32, y0: f32, x1: f32, h: f32) -> TextGlyph { + let rect = [x0, y0, x1, y0 + h]; + TextGlyph { text: t.into(), rect, quad: [[x0, y0], [x1, y0], [x1, rect[3]], [x0, rect[3]]], direction: [1.0, 0.0] } +} + +/// #3172: blanks drawn over big digits must not show inside the number, and the word gap next to big digits follows +/// the glyphs it lies between. +#[test] +fn blanks_under_a_number_and_a_word_gap_next_to_big_digits() { + // Big dark digits, then a smaller sentence drawn over them with blanks where the digits sit. + let mut v = Vec::new(); + for (t, x) in [("5", 0.0), ("0", 6.0), ("1", 100.0), ("4", 106.0)] { + v.push(tall(t, x, 3.0, x + 6.0, 34.0)); + } + for (t, x) in [("F", 16.5), ("R", 22.5), (" ", 100.0), (" ", 106.0), ("C", 130.0), ("D", 136.0)] { + v.push(tall(t, x, 6.0, x + 6.0, 28.0)); + } + assert_eq!(layout(v).plain_text(), "50 FR 14 CD"); +} + +/// #3172: a word break on the covered blank that opens an overprinted run moves to the run's next glyph. +#[test] +fn a_covered_leading_blank_keeps_the_word_break_of_its_run() { + let mut v = Vec::new(); + v.push(g("1", 0.0, 10.0, 6.0)); + // " X" drawn over the "1": the blank is covered, and "X" sits right behind it. + v.push(g(" ", 0.0, 10.0, 6.0)); + v.push(g("X", 6.0, 10.0, 12.0)); + assert_eq!(layout(v).plain_text(), "1 X"); +} + fn word(v: &mut Vec, s: &str, x: f32, y: f32, advance: f32) { for (i, c) in s.chars().enumerate() { let x0 = x + i as f32 * advance; From 0de6903b2fd8bd2aa17de53fa823590a039cd549 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Wed, 4 Nov 2026 08:53:50 +0100 Subject: [PATCH 3/5] Make the scratch ink buffer visible to the tests and drop an unneeded mut (#3172) --- crates/render/src/text/order.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/render/src/text/order.rs b/crates/render/src/text/order.rs index ed5ea72c79..717ed6f0df 100644 --- a/crates/render/src/text/order.rs +++ b/crates/render/src/text/order.rs @@ -153,7 +153,7 @@ pub(super) struct Scratch { /// `placed` with the marks merged in, built before it is swapped in. pub(super) merged: Vec, /// The merged ink intervals of the current segment, each with the ink length before it, for `drop_covered_blanks`. - ink: Vec<[f32; 3]>, + pub(super) ink: Vec<[f32; 3]>, } /// One run of a colliding component. @@ -346,7 +346,7 @@ fn drop_covered_blanks(glyphs: &[&TextGlyph], sc: &mut Scratch) { let x0 = |i: usize| glyphs[i].rect[0]; let run = sc.text.iter().find(|run| run.range().contains(b)); let kept = run.and_then(|run| { - let mut left = run.range().filter(|j| dropped.binary_search(j).is_err()); + let left = run.range().filter(|j| dropped.binary_search(j).is_err()); left.min_by(|a, c| x0(*a).total_cmp(&x0(*c)).then(a.cmp(c))) }); *b = kept.unwrap_or(NONE); From 6f9c05d8a1949a9d48729c1135bb366d7cc49c25 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:09:32 +0700 Subject: [PATCH 4/5] Measure a combining mark's word-gap threshold by its cluster base (#3172) --- crates/render/src/text/layout.rs | 5 ++++- crates/render/src/text/tests.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/render/src/text/layout.rs b/crates/render/src/text/layout.rs index 15ea5ef311..eff534f888 100644 --- a/crates/render/src/text/layout.rs +++ b/crates/render/src/text/layout.rs @@ -616,7 +616,10 @@ fn arrange(glyphs: &[TextGlyph]) -> (Vec, Vec, Vec) { for (w, pair) in sc.placed.windows(2).enumerate() { let &[p, c] = pair else { continue }; let Some(&Some(gap)) = sc.cluster_gap.get(w + 1) else { continue }; - let pair_threshold = pair_threshold(glyphs[p], glyphs[c], typical, threshold); + // A combining mark is measured by its cluster's base, as its text is: its own small box would lower the + // threshold next to it. + let base = |i: usize| glyphs.get(sc.anchor.get(i).copied().unwrap_or(i)).copied().unwrap_or(glyphs[i]); + let pair_threshold = pair_threshold(base(p), base(c), typical, threshold); let (pt, ct) = (cluster_text(p), cluster_text(c)); // Character spacing is added after every glyph of a show (`Tc`, ISO 32000-2 §9.3.2), so text spread to a // wider column -- a Chinese label on a form, 分散對齊 -- repeats one gap, while the gap between two cells diff --git a/crates/render/src/text/tests.rs b/crates/render/src/text/tests.rs index 79b20fa5dc..966b0063f5 100644 --- a/crates/render/src/text/tests.rs +++ b/crates/render/src/text/tests.rs @@ -1367,6 +1367,20 @@ fn a_superscript_close_to_its_host_takes_no_space() { assert_eq!(layout(v).plain_text(), "ax2"); } +/// #3172: a combining mark is measured by its cluster's base. Its own small box would shrink the word-gap threshold +/// next to it, so a close letter after "á" read as a new word (noted in review). +#[test] +fn a_letter_close_after_a_combining_mark_takes_no_space() { + let v = vec![ + g("x", 0.0, 10.0, 6.0), + g("a", 6.0, 10.0, 12.0), + tall("\u{301}", 10.0, 19.0, 12.0, 1.0), + g("b", 13.0, 10.0, 19.0), + g("y", 19.0, 10.0, 25.0), + ]; + assert_eq!(layout(v).plain_text(), "xa\u{301}by"); +} + /// #3172: only another run's ink drops a blank. A space its own run's letter is kerned over is still that run's word /// break, even on a line where another run overprints elsewhere. #[test] From 2d45b068fc3deec58c8d6d63fff180d91b92e874 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:26:45 +0100 Subject: [PATCH 5/5] Keep the line's word-gap threshold beside a superscript under the script size floor (#3172) --- crates/render/src/text/layout.rs | 11 ++++++++++- crates/render/src/text/tests.rs | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/render/src/text/layout.rs b/crates/render/src/text/layout.rs index eff534f888..7e805420a4 100644 --- a/crates/render/src/text/layout.rs +++ b/crates/render/src/text/layout.rs @@ -215,7 +215,16 @@ pub fn layout(glyphs: Vec) -> PageText { fn pair_threshold(p: &TextGlyph, c: &TextGlyph, typical: f32, threshold: f32) -> f32 { let height = |g: &TextGlyph| (g.rect[3] - g.rect[1]).max(0.1); let (hp, hc) = (height(p), height(c)); - if is_script_on(&p.rect, hp, &c.rect, hc) || is_script_on(&c.rect, hc, &p.rect, hp) { + // A glyph under the script test's size floor (a small `²`) is still a script when it sits off its neighbour's + // baseline inside its line, unlike body text beside a big digit, which shares the digit's baseline. + let off_baseline = |host: &[f32; 4], host_h: f32, s: &[f32; 4], s_h: f32| { + s_h < host_h * 0.45 && (host[3] - s[3]).abs() >= host_h * 0.15 && s[3] > host[1] && s[1] < host[3] + }; + if is_script_on(&p.rect, hp, &c.rect, hc) + || is_script_on(&c.rect, hc, &p.rect, hp) + || off_baseline(&p.rect, hp, &c.rect, hc) + || off_baseline(&c.rect, hc, &p.rect, hp) + { return threshold; } let h = hp.min(hc); diff --git a/crates/render/src/text/tests.rs b/crates/render/src/text/tests.rs index 966b0063f5..789749fb17 100644 --- a/crates/render/src/text/tests.rs +++ b/crates/render/src/text/tests.rs @@ -1381,6 +1381,14 @@ fn a_letter_close_after_a_combining_mark_takes_no_space() { assert_eq!(layout(v).plain_text(), "xa\u{301}by"); } +/// #3172: a superscript too small for the sub- and superscript test (under half its host's height) still keeps the +/// line's word-gap threshold next to its host: its height says no more about that gap (noted in review). +#[test] +fn a_tiny_superscript_close_to_its_host_takes_no_space() { + let v = vec![g("a", 0.0, 10.0, 6.0), g("b", 6.0, 10.0, 12.0), tall("2", 13.0, 9.5, 15.4, 4.0)]; + assert_eq!(layout(v).plain_text(), "ab2"); +} + /// #3172: only another run's ink drops a blank. A space its own run's letter is kerned over is still that run's word /// break, even on a line where another run overprints elsewhere. #[test]