diff --git a/crates/render/src/text/layout.rs b/crates/render/src/text/layout.rs index bd49718163..7e805420a4 100644 --- a/crates/render/src/text/layout.rs +++ b/crates/render/src/text/layout.rs @@ -209,6 +209,28 @@ pub fn layout(glyphs: Vec) -> PageText { PageText { glyphs, line_of, space_before, truncated: false } } +/// The word-gap threshold between neighbouring glyphs `p` and `c` on a line whose threshold is `threshold`: it follows +/// the smaller of the two, not the tallest glyph of the line (#3172). A sub- or superscript keeps the line's threshold, +/// since its smaller height says nothing about the gap next to its host. +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)); + // 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); + (typical + h * WORD_GAP).max(h * WORD_GAP).min(threshold) +} + /// How many grid cells around a glyph the far redraw check looks at in each direction, however large the glyph is next to /// the page's median glyph. const MAX_REDRAW_REACH: i32 = 8; @@ -603,6 +625,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 }; + // 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 @@ -632,7 +658,7 @@ fn arrange(glyphs: &[TextGlyph]) -> (Vec, Vec, Vec) { let tight_cluster = (loose_sign(pt) || loose_sign(ct)) && 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) + *space = (gap > pair_threshold || run_start) && !cjk && !tight_cluster && !pt.trim().is_empty() diff --git a/crates/render/src/text/order.rs b/crates/render/src/text/order.rs index 052abe4193..bbc4d6dea6 100644 --- a/crates/render/src/text/order.rs +++ b/crates/render/src/text/order.rs @@ -232,6 +232,10 @@ 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`. + pub(super) ink: Vec<[f32; 3]>, + /// The ink glyphs of the current segment as (left, right, run), by left edge, for `drop_covered_blanks`. + pub(super) ink_runs: Vec<(f32, f32, usize)>, /// For each glyph of the page, the glyph a combining mark drawn right after it rides on (the start of its cluster), /// or the glyph itself. Placement sorts clusters, so a mark keeps its content-stream place after its base (#6517). /// A glyph past its end is its own anchor. @@ -395,6 +399,8 @@ pub(super) fn place_glyphs(glyphs: &[&TextGlyph], height: &dyn Fn(usize) -> f32, sc.placed.sort_unstable_by(|a, b| by_cluster(glyphs, &sc.anchor, *a, *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, or its letter's when it is a // combining mark riding on one. Marks are visited in that order, and that position never moves back, so one pass @@ -532,6 +538,99 @@ fn keep_taken_back_blanks(glyphs: &[&TextGlyph], placed: &mut [usize]) { } } +/// 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). Only other runs' ink counts: a blank its own run's +/// letters overlap (kerned into a tight word gap) is that run's word break. 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 over the line's ink, and a blank that ink mostly covers then a scan of the glyphs that can reach it. +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) + }; + // The ink glyphs with their runs, by left edge, and the widest of them: only a glyph starting less than that left + // of a blank can reach it. + let by_x = &mut sc.ink_runs; + by_x.clear(); + by_x.extend(sc.owner.iter().filter(|&&(i, _)| !blank(i)).map(|&(i, r)| (glyphs[i].rect[0], glyphs[i].rect[2], r))); + by_x.sort_by(|a, b| a.0.total_cmp(&b.0)); + let widest = by_x.iter().map(|g| g.1 - g.0).fold(0.0f32, f32::max); + let by_x = &sc.ink_runs; + // How much of [l, r] the ink of runs other than `run` covers. The intervals come in order of their left edges. + let covered_by_others = |l: f32, r: f32, run: usize| { + let (from, to) = (by_x.partition_point(|g| g.0 < l - widest), by_x.partition_point(|g| g.0 < r)); + let (mut sum, mut reach) = (0.0, l); + for &(a, b, _) in by_x.get(from..to).unwrap_or_default().iter().filter(|g| g.2 != run) { + let (a, b) = (a.max(reach), b.min(r)); + if b > a { + sum += b - a; + reach = b; + } + } + sum + }; + sc.merged.clear(); + for &(i, run) in &sc.owner { + 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) && covered_by_others(l, r, run) > 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 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 9c0adf1ca7..789749fb17 100644 --- a/crates/render/src/text/tests.rs +++ b/crates/render/src/text/tests.rs @@ -1327,6 +1327,85 @@ 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 v = vec![ + g("1", 0.0, 10.0, 6.0), + // " X" drawn over the "1": the blank is covered, and "X" sits right behind it. + g(" ", 0.0, 10.0, 6.0), + g("X", 6.0, 10.0, 12.0), + ]; + assert_eq!(layout(v).plain_text(), "1 X"); +} + +/// #3172: a superscript keeps the line's word-gap threshold, not its own smaller height's, so a small gap before it is +/// no word break. +#[test] +fn a_superscript_close_to_its_host_takes_no_space() { + let v = vec![g("a", 0.0, 10.0, 6.0), g("x", 6.0, 10.0, 12.0), tall("2", 13.2, 7.0, 16.8, 6.0)]; + 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: 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] +fn a_blank_its_own_letters_overlap_stays_a_word_break() { + let v = vec![ + // "A B" with the space pulled back under the "A", and the "B" kerned close: the space is the only word break. + g("A", 0.0, 10.0, 6.0), + g(" ", 2.0, 10.0, 8.0), + g("B", 6.5, 10.0, 12.5), + // A word far right, then a run drawn back over the "B", so the line's runs collide. + g("Q", 200.0, 10.0, 206.0), + g("1", 7.5, 10.0, 13.5), + ]; + let text = layout(v).plain_text(); + assert!(text.starts_with("A B"), "{text:?}"); +} + 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;