diff --git a/crates/text/src/breaking.rs b/crates/text/src/breaking.rs index 8db60f830..c2ce447fd 100644 --- a/crates/text/src/breaking.rs +++ b/crates/text/src/breaking.rs @@ -182,7 +182,8 @@ 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), hang_stop) + // The implicit stop is for the first line only: later lines start at the margin. + tab_advance(x0 + x, tab_stops, default_tab, tab_segment(&p.cells, i), if lines.is_empty() { hang_stop } else { None }) } else { p.cells.get(i).map(|c| c.adv).unwrap_or(0.0) }; diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rs index 42a7cfc70..cf9c67ea2 100644 --- a/crates/text/src/layout.rs +++ b/crates/text/src/layout.rs @@ -316,7 +316,9 @@ fn place_paragraph(p: &ParaShaped, pi: usize, g: &BoxGeom, spacing: (f64, bool), // A hanging first line (marL > 0, indent < 0) treats the indent position (marL) as an // implicit tab stop for its first tab; later lines already start there, so the stop // only ever applies ahead of the first line's start. - let hang = (margin - first_x > 0.5 && margin.is_finite()).then_some(margin); + // LTR only: the implicit stop is measured from the left margin, and RTL tab handling is + // out of this change's scope (its stops are not mirrored here yet). + let hang = (!p.rtl && margin - first_x > 0.5 && margin.is_finite()).then_some(margin); let first_w = (g.col_w - first_x - mr).max(1.0); let rest_w = (g.col_w - rest_x - mr).max(1.0); let raw = break_lines( @@ -382,7 +384,10 @@ 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) { +/// `hang` is the implicit first-line stop, or `None` on later lines: they already start at the +/// margin, and negative character spacing can dip `x` below it, which would wrongly re-trigger +/// the stop. +fn line_advances(env: &ParaEnv, rl: &RawLine, is_last: bool, x0: f64, hang: Option) -> (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); @@ -394,7 +399,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 1bd3bbd11..7e0e13b53 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -1477,6 +1477,29 @@ fn a_tab_on_a_wrapped_line_keeps_the_default_stop() { assert!((x - 54.0).abs() > 5.0, "and not marL + 27"); } +#[test] +fn a_tab_after_negative_spacing_on_a_later_line_keeps_the_default_stop() { + // #1683 follow-up: the implicit stop is a first-line stop only. Negative character spacing + // can put a later line's cursor left of the margin; its tab must still take the default stop. + let (p, mut sh) = setup("A\u{b}X\tY", 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); + r.props.spacing = Some(-20.0); + } + let pp = &mut para.props; + pp.bullet = Some(Bullet::None); + pp.margin_left = Some(27.0); + pp.indent = Some(-27.0); + pp.default_tab = Some(72.0); + pp.tabs = None; + let l = lay(&p, &sh, 600.0, 300.0); + assert_eq!(l.lines.len(), 2, "{:?}", l.lines.iter().map(|x| (x.start, x.end)).collect::>()); + let x = l.lines[1].edges[2].0 - l.inner.x0; + assert!((x - 72.0).abs() < 0.5, "the later line's tab uses the default stop, got {x}"); + assert!((x - 27.0).abs() > 5.0, "and not the first line's margin stop"); +} + /// Underline decorations of "ab cd" underlined `u` at 40 pt. fn underline_decos(u: &str) -> (TextLayout, Vec) { let (p, mut sh) = setup("ab cd", 2000.0, 300.0);