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] 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]