diff --git a/crates/text/src/breaking.rs b/crates/text/src/breaking.rs index 9a6028876..7f76ab23c 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)) + tab_advance(x0 + x, tab_stops, default_tab, tab_segment(&p.cells, i), avail) } else { p.cells.get(i).map(|c| c.adv).unwrap_or(0.0) }; @@ -305,11 +305,15 @@ pub(crate) fn tab_segment(cells: &[Cell], i: usize) -> (f64, f64) { /// Advance of a tab at `x` (from the start edge) to the next stop, placing the text after it /// (`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. -pub(crate) fn tab_advance(x: f64, stops: &[Tab], default_tab: f64, seg: (f64, f64)) -> f64 { +/// +/// `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 { // `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 to = s.pos - x; + let pos = if avail.is_finite() && avail > 0.0 { s.pos.min(avail) } else { s.pos }; + let to = (pos - x).max(0.0); return match s.align { TabAlign::Left => to, TabAlign::Right => (to - seg.0).max(0.0), @@ -329,7 +333,16 @@ mod tab_tests { #[test] fn a_nan_position_takes_the_finite_default_stop() { let stops = [Tab { pos: 100.0, align: TabAlign::Left }]; - assert!(tab_advance(f64::NAN, &stops, 72.0, (0.0, 0.0)).is_finite()); - assert_eq!(tab_advance(10.0, &stops, 72.0, (0.0, 0.0)), 90.0); + assert!(tab_advance(f64::NAN, &stops, 72.0, (0.0, 0.0), 600.0).is_finite()); + assert_eq!(tab_advance(10.0, &stops, 72.0, (0.0, 0.0), 600.0), 90.0); + } + + #[test] + fn a_stop_past_the_line_ends_at_its_edge() { + let stops = [Tab { pos: 700.0, align: TabAlign::Right }]; + // Right-aligned text after the tab ends at the line's right edge, not 700. + assert_eq!(tab_advance(0.0, &stops, 72.0, (100.0, 100.0), 600.0), 500.0); + // 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); } } diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rs index c52b840a2..f62e27599 100644 --- a/crates/text/src/layout.rs +++ b/crates/text/src/layout.rs @@ -376,7 +376,7 @@ fn line_extent(p: &ParaShaped, rl: &RawLine) -> (f64, f64, f64) { } /// Logical advance of each cell on the line (tabs measure from the start edge) and their sum. -fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64) -> (Vec, f64) { +fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64, avail: f64) -> (Vec, f64) { let p = env.p; let line_cells = p.cells.get(rl.start..rl.end).unwrap_or(&[]); let n = rl.end.saturating_sub(rl.start); @@ -388,7 +388,7 @@ fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64) -> (Vec 0.0; diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index a747af045..5d7ce8dce 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -1416,6 +1416,17 @@ fn tab_stops_align_the_text_after_them() { assert!((l.lines[0].edges[4].0 - x0(&l) - 72.0).abs() < 0.01); } +#[test] +fn a_right_tab_stop_past_the_margin_ends_the_text_at_the_margin() { + // #1663: a layout may set a right tab stop a little past the text area's right edge. + // PowerPoint treats it as one at the margin: the text after it stays on one line and ends + // at the right edge. DeckCraft started it too far right and wrapped the rest. + let l = tabbed("x\tCorrie Ten Boom", &[(700.0, "r")], 600.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.