From 1add4c138b209bb7a03ab0f2e156e0999ae2d58f Mon Sep 17 00:00:00 2001 From: Madhav Agarwal Date: Mon, 16 Nov 2026 18:09:48 +0530 Subject: [PATCH] review: clamp a tab stop to the line's absolute right edge (coderabbit/relative-width clamp) --- crates/text/src/breaking.rs | 21 ++++++++++++++++----- crates/text/src/layout.rs | 2 +- crates/text/src/tests.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/crates/text/src/breaking.rs b/crates/text/src/breaking.rs index 7f76ab23c..c573d8481 100644 --- a/crates/text/src/breaking.rs +++ b/crates/text/src/breaking.rs @@ -179,7 +179,7 @@ pub(crate) fn break_lines(p: &ParaShaped, params: &BreakParams) -> Vec break; } let adv = if c == '\t' { - tab_advance(x0 + x, tab_stops, default_tab, tab_segment(&p.cells, i), avail) + tab_advance(x0 + x, tab_stops, default_tab, tab_segment(&p.cells, i), x0 + avail) } else { p.cells.get(i).map(|c| c.adv).unwrap_or(0.0) }; @@ -306,13 +306,14 @@ pub(crate) fn tab_segment(cells: &[Cell], i: usize) -> (f64, f64) { /// (`seg`: its width, and its width before the decimal point) as the stop aligns it but never /// moving back, as PowerPoint does; past the last stop, to the next default stop. /// -/// `avail` is the line's width from the start edge: a stop set past the right edge acts as one at -/// the edge, as PowerPoint treats it (a right-aligned stop there ends the text at the margin). -pub(crate) fn tab_advance(x: f64, stops: &[Tab], default_tab: f64, seg: (f64, f64), avail: f64) -> f64 { +/// `right` is the line's right edge in the same coordinate space as `x` (the text area's left +/// edge plus the line's start offset and width): a stop set past that edge acts as one at the +/// edge, as PowerPoint treats it (a right-aligned stop there ends the text at the margin). +pub(crate) fn tab_advance(x: f64, stops: &[Tab], default_tab: f64, seg: (f64, f64), right: f64) -> f64 { // `stops` is in position order (`tabs_of`): the next stop is the first past x. // A NaN `x` (a NaN spacing upstream) skips the lookup and takes the finite default-stop path. if let Some(s) = stops.get(stops.partition_point(|s| s.pos <= x + 0.5)).filter(|_| !x.is_nan()) { - let pos = if avail.is_finite() && avail > 0.0 { s.pos.min(avail) } else { s.pos }; + let pos = if right.is_finite() && right > 0.0 { s.pos.min(right) } else { s.pos }; let to = (pos - x).max(0.0); return match s.align { TabAlign::Left => to, @@ -345,4 +346,14 @@ mod tab_tests { // A stop inside the line keeps its own position. assert_eq!(tab_advance(0.0, &stops, 72.0, (100.0, 100.0), 800.0), 600.0); } + + #[test] + fn an_offset_line_clamps_to_its_absolute_right_edge() { + // The stop and the edge are absolute coordinates: a 100 pt start offset does not move the + // edge in to 500 pt, so an inside stop at 550 stays there and one past 600 ends at 600. + let inside = [Tab { pos: 550.0, align: TabAlign::Right }]; + assert_eq!(tab_advance(100.0, &inside, 72.0, (100.0, 100.0), 600.0), 350.0); + let past = [Tab { pos: 700.0, align: TabAlign::Right }]; + assert_eq!(tab_advance(100.0, &past, 72.0, (100.0, 100.0), 600.0), 400.0); + } } diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rs index f62e27599..6bbbe0e2b 100644 --- a/crates/text/src/layout.rs +++ b/crates/text/src/layout.rs @@ -388,7 +388,7 @@ fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64, avail: f64 let a = if shy_end && k + 1 == n { p.faces.get(c.face).zip(p.styles.get(c.style)).map(|(f, st)| measure_str(f, st.size, "-").0).unwrap_or(0.0) } else if c.ch == '\t' { - tab_advance(x0 + x, env.tabs, p.props.default_tab.unwrap_or(72.0), tab_segment(&p.cells, rl.start + k), avail) + tab_advance(x0 + x, env.tabs, p.props.default_tab.unwrap_or(72.0), tab_segment(&p.cells, rl.start + k), x0 + avail) } else { c.adv }; diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index 5d7ce8dce..f624fcb98 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -1427,6 +1427,43 @@ fn a_right_tab_stop_past_the_margin_ends_the_text_at_the_margin() { assert!((l.lines[0].extent.1 - right).abs() < 0.01, "ends at {:?}, margin {right}", l.lines[0].extent); } +#[test] +fn an_indented_paragraph_clamps_a_tab_stop_to_its_absolute_right_edge() { + // #1663: the stop and the edge are measured from the text area's left edge, so an indented + // first line does not pull the edge in by its margin. A stop inside it keeps its position; + // one past it ends the text at the margin. + let (p, mut sh) = setup("x\tCorrie Ten Boom", 600.0, 300.0); + let para = &mut sh.text.as_mut().unwrap().paragraphs[0]; + for r in &mut para.runs { + r.props.size = Some(18.0); + } + let pp = &mut para.props; + pp.bullet = Some(Bullet::None); + pp.margin_left = Some(100.0); + pp.indent = Some(0.0); + pp.default_tab = Some(72.0); + pp.tabs = Some(vec![deckcraft_model::text::TabStop { pos: 550.0, align: "r".to_string() }]); + let l = lay(&p, &sh, 600.0, 300.0); + assert_eq!(l.lines.len(), 1, "{:?}", l.lines.iter().map(|x| (x.start, x.end, x.extent)).collect::>()); + assert!((l.lines[0].extent.1 - l.inner.x0 - 550.0).abs() < 0.01, "inside stop at {:?}", l.lines[0].extent); + + let (p, mut sh) = setup("x\tCorrie Ten Boom", 600.0, 300.0); + let para = &mut sh.text.as_mut().unwrap().paragraphs[0]; + for r in &mut para.runs { + r.props.size = Some(18.0); + } + let pp = &mut para.props; + pp.bullet = Some(Bullet::None); + pp.margin_left = Some(100.0); + pp.indent = Some(0.0); + pp.default_tab = Some(72.0); + pp.tabs = Some(vec![deckcraft_model::text::TabStop { pos: 700.0, align: "r".to_string() }]); + let l = lay(&p, &sh, 600.0, 300.0); + assert_eq!(l.lines.len(), 1, "{:?}", l.lines.iter().map(|x| (x.start, x.end, x.extent)).collect::>()); + let right = l.inner.x0 + l.inner.width(); + assert!((l.lines[0].extent.1 - right).abs() < 0.01, "ends at {:?}, margin {right}", l.lines[0].extent); +} + #[test] fn many_tabs_over_many_stops_lay_out_by_position() { // 20,000 stops given out of order, and 2,000 tabs: each tab finds the next stop by position.