From be10efd762e67a82a5f0dc0b5fbd1c601f26c119 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 17 Nov 2026 19:15:25 +0500 Subject: [PATCH 1/2] Tracking in right-to-left text: the space follows the character in reading order Tracking (and manual kerning) on a character is space after it in reading order. The shaper adds it to the glyph's advance, at its right, which is after the character left to right but before it right to left: tracking on bet opened a gap between alef and bet instead of between bet and gimel. The bidi reorder now moves a right-to-left cluster's tracking to its left: out of its own advance and into the advance of whatever is drawn left of it (at the start of a line, nothing), so advances stay end to end for carets, selections and rules. Glyphs record the tracking they carry, scaled with glyph scaling and drop caps; a synthesized hyphen carries none. Fixes #2884 --- crates/compose/src/lib.rs | 23 +++++++++++++++-- crates/compose/src/shape.rs | 10 +++++++- crates/compose/src/tests.rs | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 3c468b0bf..30af4491c 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -4069,6 +4069,7 @@ fn apply_desired_spacing(glyphs: &mut [Glyph], pp: &ParaProps) { g.adv += g.space * (ws - 1.0); } else if !g.is_space() && g.adv > 0.0 { g.adv = g.adv * gs + ls * g.space; + g.track *= gs; g.sx *= gs; g.dx *= gs; } @@ -5442,13 +5443,30 @@ fn reorder_visual(line: &[Glyph], out: &mut [PlacedGlyph], pens: &[f64], start: let width = |u: &std::ops::Range| pens[u.end] - pens[u.start]; let mut cur = start; let mut shifts = vec![0.0; line.len()]; + // The glyph whose advance ends at `cur`: the last one of the cluster drawn there. + let mut left: Option = None; for &k in &order { let u = &units[k]; + let rtl = levels[k].is_rtl(); + // Tracking is space after the character in reading order (#2884): right-to-left, that + // is at its left. The space leaves the cluster's advance for the advance of whatever is + // drawn left of it (at the start of the line, nothing), so advances stay end to end. + let track: f64 = if rtl { line[u.clone()].iter().map(|g| g.track).sum() } else { 0.0 }; + if track != 0.0 { + if let Some(o) = left.and_then(|l| out.get_mut(l)) { + o.adv += track; + } + for i in u.clone() { + out[i].adv -= line[i].track; + } + cur += track; + } for i in u.clone() { shifts[i] = cur - pens[u.start]; - out[i].rtl = levels[k].is_rtl(); + out[i].rtl = rtl; } - cur += width(u); + cur += width(u) - track; + left = u.end.checked_sub(1); } for (p, d) in out.iter_mut().zip(shifts) { p.x += d; @@ -5733,6 +5751,7 @@ fn split_drop_cap( let k = if k.is_finite() && k > 0.0 { k.min(1000.0) } else { 1.0 }; for g in dc.iter_mut().chain(after.iter_mut()) { g.adv *= k; + g.track *= k; g.dx *= k; g.dy *= k; g.sx *= k; diff --git a/crates/compose/src/shape.rs b/crates/compose/src/shape.rs index 1de4b823c..dbc1a9f7e 100644 --- a/crates/compose/src/shape.rs +++ b/crates/compose/src/shape.rs @@ -71,6 +71,9 @@ pub struct Glyph { pub allow_kashidas: bool, pub safe_tatweel_before: bool, pub shaping_rtl: bool, + /// Tracking and manual kerning included in `adv`, the space after the character in reading + /// order. Right-to-left, that is at the glyph's left (see `reorder_visual`). + pub track: f64, /// The character's language as a BCP 47 tag ([`designcraft_doc::language_tag`]). pub lang: Option<&'static str>, /// Upright in a vertical frame: shaped top to bottom (`adv` is its vertical advance) and hung @@ -1145,6 +1148,7 @@ fn control_glyph(face: &Arc, p: &CharProps, auto_leading: TypeEnv, sty allow_kashidas: p.allow_kashidas, safe_tatweel_before: false, shaping_rtl: false, + track: 0.0, lang: designcraft_doc::language_tag(&p.language), upright: false, list_label: false, @@ -1262,10 +1266,12 @@ fn shape_segment( }; let (ascent, descent) = if up { (em_top * k_v, -em_bottom * k_v) } else { (ascent, descent) }; let body = adv; + let mut track = 0.0; if ch == SOFT_HYPHEN { adv = 0.0; } else if last_in_cluster { - adv += tracking + manual; + track = tracking + manual; + adv += track; } let mark = face.glyph_is_mark(sg.gid); let mark_x = if mark { p.diacritic_x_offset.clamp(-1000.0, 1000.0) / 1000.0 * p.size * hs } else { 0.0 }; @@ -1319,6 +1325,7 @@ fn shape_segment( allow_kashidas: p.allow_kashidas, safe_tatweel_before: sg.safe_tatweel_before, shaping_rtl: sg.rtl, + track, lang, upright: up, list_label: false, @@ -1433,6 +1440,7 @@ pub(crate) fn hyphen_after(g: &Glyph) -> Glyph { let mut h = g.clone(); h.gid = gid; h.adv = g.face.advance(gid) * g.sx; + h.track = 0.0; h.dx = 0.0; h.dy = 0.0; h.byte = g.byte + g.len; diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 8e03b35d0..a23aeb051 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -3571,6 +3571,56 @@ fn right_to_left_runs_are_ordered_visually() { assert!(g.windows(2).all(|w| w[0].cluster <= w[1].cluster), "{:?}", g.iter().map(|g| g.cluster).collect::>()); } +/// #2884: tracking is space after the character in reading order. Right to left that is at its +/// left, between it and the character read after it, mirroring left to right. +#[test] +fn tracking_in_right_to_left_text_follows_the_character_in_reading_order() { + use designcraft_doc::TextDirection; + let lines = |text: &str, direction: TextDirection, align: Align, tracked: std::ops::Range| { + let para = ParaAttrs { direction: Some(direction), align: Some(align), ..Default::default() }; + let (mut d, sid, _) = doc_with(text, Rect::new(36.0, 36.0, 500.0, 200.0), para); + d.story_mut(sid).unwrap().format_chars(tracked, |f| f.over.tracking = Some(1000.0)); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + all_lines(&cs)[0].clone() + }; + let x_of = |l: &Line, byte: usize| l.glyphs.iter().find(|g| g.byte == byte && g.len > 0).map(|g| g.x).unwrap(); + let width = |l: &Line| l.glyphs.iter().map(|g| g.adv).sum::(); + // Alef, bet, gimel, dalet, he (two bytes each); tracking on bet. + let text = "\u{5D0}\u{5D1}\u{5D2}\u{5D3}\u{5D4}"; + let bytes = [0, 2, 4, 6, 8]; + for align in [Align::Right, Align::Left] { + let plain = lines(text, TextDirection::RightToLeft, align, 0..0); + let tracked = lines(text, TextDirection::RightToLeft, align, 2..4); + // Tracking 1000 is one em: what it adds to the line. + let em = width(&tracked) - width(&plain); + assert!(em > 1.0, "{align:?}: the tracking widens the line by {em}"); + // The space opens between bet and gimel (read after it); every other pair keeps its gap. + for w in bytes.windows(2) { + let gap = |l: &Line| x_of(l, w[0]) - x_of(l, w[1]); + let want = if w[0] == 2 { gap(&plain) + em } else { gap(&plain) }; + assert!((gap(&tracked) - want).abs() < 0.01, "{align:?}: bytes {w:?} are {} apart, not {want}", gap(&tracked)); + } + // Advances stay end to end on the line, so carets, selections and rules have no gap. + let mut drawn: Vec<&PlacedGlyph> = tracked.glyphs.iter().filter(|g| g.len > 0).collect(); + drawn.sort_by(|a, b| a.x.total_cmp(&b.x)); + for w in drawn.windows(2) { + assert!((w[0].x + w[0].adv - w[1].x).abs() < 0.01, "{align:?}: advances of bytes {} and {} don't meet", w[0].byte, w[1].byte); + } + // The caret after bet is at bet's left edge, the one before it at its right edge. + assert!((crate::caret_x(&tracked, 4) - x_of(&tracked, 2)).abs() < 0.01, "{align:?}: caret after bet"); + assert!((crate::caret_x(&tracked, 2) - x_of(&tracked, 0)).abs() < 0.01, "{align:?}: caret before bet"); + } + // Left to right, the same: the space opens between b and c. + let plain = lines("abcde", TextDirection::LeftToRight, Align::Left, 0..0); + let tracked = lines("abcde", TextDirection::LeftToRight, Align::Left, 1..2); + let em = width(&tracked) - width(&plain); + for (a, b) in [(0, 1), (1, 2), (2, 3), (3, 4)] { + let gap = |l: &Line| x_of(l, b) - x_of(l, a); + let want = if a == 1 { gap(&plain) + em } else { gap(&plain) }; + assert!((gap(&tracked) - want).abs() < 0.01, "left to right: bytes {a}, {b}"); + } +} + #[test] fn bidi_matches_the_reference_order() { use unicode_bidi::{BidiInfo, Level}; From b4ab777f747bc7b44264992d6eed4deb19965678 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 17 Nov 2026 19:46:30 +0500 Subject: [PATCH 2/2] Right-to-left tracking: move it as scaled, and at a drop cap's letters too Review fixes. A justified line that scales its glyphs scales each advance, tracking included, so the space moved to a cluster's left is the scaled tracking, not the shaped one. A right-to-left drop cap places its letters from the right edge; a letter's tracking now goes to its left there too, into the advance of the letter drawn left of it, as in a line. --- crates/compose/src/lib.rs | 29 +++++++++++++++----- crates/compose/src/tests.rs | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 30af4491c..3171dba0f 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -5337,9 +5337,12 @@ fn layout_line( let mut x = x0 + offset; let bidi = bidi_info.has_rtl(); let mut pens = Vec::with_capacity(if bidi { line.len() } else { 0 }); + // The tracking each placed advance carries, scaled with it. + let mut tracks = Vec::with_capacity(if bidi { line.len() } else { 0 }); for (i, g) in line.iter().enumerate() { if bidi { pens.push(x); + tracks.push(g.track * scale[i]); } let mut p = place(g, x); if scale[i] != 1.0 { @@ -5362,7 +5365,7 @@ fn layout_line( } if bidi { pens.push(x); - reorder_visual(&line, &mut out, &pens, x0 + offset, bidi_info); + reorder_visual(&line, &mut out, &pens, &tracks, x0 + offset, bidi_info); } for (i, leader) in &leaders { if let Some((g, p)) = line.get(*i).zip(out.get(*i)) { @@ -5422,9 +5425,9 @@ fn kashida_points(line: &[Glyph]) -> Vec { } /// Bidi: put a laid-out line (glyphs in text order, `pens` their pen positions and then the end -/// position) into visual order from `start`, cluster by cluster (Unicode Bidirectional -/// Algorithm, rule L2). -fn reorder_visual(line: &[Glyph], out: &mut [PlacedGlyph], pens: &[f64], start: f64, info: &unicode_bidi::BidiInfo<'_>) { +/// position, `tracks` the tracking in their placed advances) into visual order from `start`, +/// cluster by cluster (Unicode Bidirectional Algorithm, rule L2). +fn reorder_visual(line: &[Glyph], out: &mut [PlacedGlyph], pens: &[f64], tracks: &[f64], start: f64, info: &unicode_bidi::BidiInfo<'_>) { use unicode_bidi::{BidiInfo, Level}; // Clusters: runs of glyphs from the same source character. let mut units: Vec> = Vec::new(); @@ -5451,13 +5454,13 @@ fn reorder_visual(line: &[Glyph], out: &mut [PlacedGlyph], pens: &[f64], start: // Tracking is space after the character in reading order (#2884): right-to-left, that // is at its left. The space leaves the cluster's advance for the advance of whatever is // drawn left of it (at the start of the line, nothing), so advances stay end to end. - let track: f64 = if rtl { line[u.clone()].iter().map(|g| g.track).sum() } else { 0.0 }; + let track: f64 = if rtl { u.clone().filter_map(|i| tracks.get(i)).sum() } else { 0.0 }; if track != 0.0 { if let Some(o) = left.and_then(|l| out.get_mut(l)) { o.adv += track; } for i in u.clone() { - out[i].adv -= line[i].track; + out[i].adv -= tracks.get(i).copied().unwrap_or(0.0); } cur += track; } @@ -5802,10 +5805,22 @@ impl DropCap { /// The drop cap's glyphs set from `at` (right to left: mirrored, its first character on the right). fn place_drop_cap(dc: &DropCap, at: f64, rtl: bool) -> Vec { let mut x = at; + // Right to left, the edge the next letter ends at and the tracking of the letter before it in + // reading order: that space is at its left (#2884), so in the advance of the letter drawn there. + let mut right = at + dc.width; + let mut carried = 0.0; let mut out = Vec::with_capacity(dc.glyphs.len()); for g in &dc.glyphs { - let pen = if rtl { at + at + dc.width - x - g.adv } else { x }; + let (pen, adv) = if rtl { + let adv = g.adv - g.track + carried; + carried = g.track; + right -= adv; + (right, adv) + } else { + (x, g.adv) + }; let mut p = place(g, pen); + p.adv = adv; p.y += dc.drop; p.rtl = rtl; out.push(p); diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index a23aeb051..07967ffa6 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -3621,6 +3621,60 @@ fn tracking_in_right_to_left_text_follows_the_character_in_reading_order() { } } +/// #2884 review: a justified line whose glyphs are scaled moves the tracking as scaled, so every +/// advance keeps the glyph's own share; and a right-to-left drop cap puts a letter's tracking at +/// its left, as the line does. +#[test] +fn right_to_left_tracking_follows_glyph_scaling_and_drop_caps() { + use designcraft_doc::TextDirection; + let rtl = |align: Align| ParaAttrs { direction: Some(TextDirection::RightToLeft), align: Some(align), ..Default::default() }; + let text = "\u{5D0}\u{5D1}\u{5D2}\u{5D3}\u{5D4}"; + // Two words, then a third the measure leaves for the next line. + let words = format!("{text} \u{5D5}\u{5D6} {text}"); + let line = |para: ParaAttrs, width: f64, tracked: std::ops::Range| { + let (mut d, sid, _) = doc_with(&words, Rect::new(0.0, 0.0, width, 200.0), para); + d.story_mut(sid).unwrap().format_chars(tracked, |f| f.over.tracking = Some(1000.0)); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + all_lines(&cs)[0].clone() + }; + let glyph = |l: &Line, byte: usize| l.glyphs.iter().find(|g| g.byte == byte && g.len > 0).cloned().unwrap(); + // The first two words on a line of their own, justified by scaling their glyphs about a third + // (the word space is fixed). + let plain = line(rtl(Align::Right), 500.0, 2..4); + let two: f64 = plain.glyphs.iter().filter(|g| g.byte < 15).map(|g| g.adv).sum(); + let scaled = ParaAttrs { + glyph_scale_min: Some(1.0), + glyph_scale_max: Some(2.0), + word_space_max: Some(1.0), + word_space_min: Some(1.0), + ..rtl(Align::RightJustified) + }; + let just = line(scaled, two * 1.3, 2..4); + assert!(just.range.end <= 16, "the first two words are a line of their own: {:?}", just.range); + for byte in [0, 2, 4, 6, 8, 11, 13] { + let (p, j) = (glyph(&plain, byte), glyph(&just, byte)); + let k = j.sx / p.sx; + assert!(k > 1.05, "byte {byte}: the glyph is scaled by {k}"); + assert!((j.adv - p.adv * k).abs() < 0.01, "byte {byte}: advance {} is not {} scaled by {k}", j.adv, p.adv); + } + // A two-letter drop cap with tracking on its first letter (alef): the space opens between alef + // and bet, read after it, and their advances still meet. + let cap = |tracked: std::ops::Range| { + let para = ParaAttrs { drop_cap_lines: Some(2), drop_cap_chars: Some(2), ..rtl(Align::Right) }; + let (mut d, sid, _) = doc_with(&format!("{text} {}", "\u{5D5}".repeat(200)), Rect::new(0.0, 0.0, 300.0, 400.0), para); + d.story_mut(sid).unwrap().format_chars(tracked, |f| f.over.tracking = Some(1000.0)); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + all_lines(&cs)[0].clone() + }; + let (untracked, tracked) = (cap(0..0), cap(0..2)); + let (a0, b0, a1, b1) = (glyph(&untracked, 0), glyph(&untracked, 2), glyph(&tracked, 0), glyph(&tracked, 2)); + assert!(a1.x > b1.x, "alef is right of bet"); + let em = (a1.x - b1.x) - (a0.x - b0.x); + assert!(em > 1.0, "the tracking opens a space between alef and bet: {em}"); + assert!((b1.x + b1.adv - a1.x).abs() < 0.01, "bet's advance meets alef: {} vs {}", b1.x + b1.adv, a1.x); + assert!((a1.adv - a0.adv).abs() < 0.01, "alef's advance is its own: {} vs {}", a1.adv, a0.adv); +} + #[test] fn bidi_matches_the_reference_order() { use unicode_bidi::{BidiInfo, Level};