From c120c09423187c1c690bff7c822420512c399fb9 Mon Sep 17 00:00:00 2001 From: dfgodvan <180286157+dfgodvan@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:30:32 +0300 Subject: [PATCH] Layout: a right tab past the right indent takes its text up to the stop, as in Word A TOC entry in Word's TOC styles has a right indent and a right tab with a dot leader at the margin, past that indent. Word keeps the page number on the entry's line whenever it fits between the text and the stop, so the number ends past the indent. WordCraft checked the first character after the tab against the right indent (the tab has no room yet), and moved the tab and its number to the next line when the text ended less than a digit short of the indent. Measured in Word for Mac 16.93 (Times New Roman 12pt, right indent 1cm, stop at the margin): entries whose text ends 0.4, 1, 2, 3, 4, 5 and 5.8pt short of the indent all keep their dot leader and number on the line, the number ending at the stop. With a 0.2cm indent the number stays on the line when 18.7pt are left before the stop and goes to the next line when 16.7pt are left. Text after a right or decimal tab now fits while it ends before the stop. The running shift of the tab also no longer counts the tab's own room as text: it dropped back to nothing after every second character, so those characters were checked against the indent as well. A tab whose text is pending starts with no room on the line it goes to. --- crates/layout/src/para/lines.rs | 21 +++++++++++---- crates/layout/src/tests.rs | 48 +++++++++++++++++++++++++++++++++ docs/layout-parity.md | 1 + 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/crates/layout/src/para/lines.rs b/crates/layout/src/para/lines.rs index 821132f6aa..d04dba2619 100644 --- a/crates/layout/src/para/lines.rs +++ b/crates/layout/src/para/lines.rs @@ -856,19 +856,29 @@ impl Scan { } self.x += w; } - _ => self.pending_tab = Some((j, stop, self.x)), + _ => { + // Its room grows from nothing with its text (a tab moved on from the line before + // may still hold the width it had there). + if let Some(c) = pl.clusters.get_mut(j) { + c.adv = 0.0; + } + self.pending_tab = Some((j, stop, self.x)); + } } if stop.leader != TabLeader::None { self.leaders.push((j, stop.leader)); } } - /// Whether text after a right/centre tab grows leftwards into the tab's space first. + /// Whether text after a right/centre tab grows leftwards into the tab's space first. Up to a + /// right or decimal stop it fits even past the right indent (Word: a TOC entry whose text ends + /// less than a digit short of the indent keeps its page number on its line when the stop is + /// at the margin); unshifted, the text after the tab ends at `x - room`. fn absorbs(&self, pl: &ParaLayout, advance: f32) -> bool { self.pending_tab.is_some_and(|(tj, stop, _)| { let room = pl.clusters.get(tj).map(|t| t.adv).unwrap_or(0.0); match stop.align { - TabAlign::Right | TabAlign::Decimal => room >= advance, + TabAlign::Right | TabAlign::Decimal => room >= advance || self.x - room + advance <= stop.pos + 0.01, TabAlign::Center => room >= advance / 2.0 && self.x + advance / 2.0 <= self.right_edge + 0.01, _ => false, } @@ -944,7 +954,9 @@ impl Scan { /// Text after a decimal/centre/right tab shifts as it grows. fn shift_pending_tab(&mut self, pl: &mut ParaLayout) { let Some((tj, stop, tx)) = self.pending_tab else { return }; - let seg_w = self.x - tx; + // `x` includes the room the tab has so far. + let old = pl.clusters.get(tj).map(|c| c.adv).unwrap_or(0.0); + let seg_w = self.x - tx - old; let shift = match stop.align { TabAlign::Right => stop.pos - tx - seg_w, TabAlign::Center => stop.pos - tx - seg_w / 2.0, @@ -953,7 +965,6 @@ impl Scan { _ => 0.0, }; let shift = shift.max(0.0); - let old = pl.clusters.get(tj).map(|c| c.adv).unwrap_or(0.0); if (shift - old).abs() > 0.001 { let delta = shift - old; if let Some(c) = pl.clusters.get_mut(tj) { diff --git a/crates/layout/src/tests.rs b/crates/layout/src/tests.rs index a15f2f672b..0280e4fe62 100644 --- a/crates/layout/src/tests.rs +++ b/crates/layout/src/tests.rs @@ -1847,6 +1847,54 @@ fn leader_dots_are_spaced_and_placed_as_in_word() { } } +/// A TOC entry with a right indent and its right tab at the margin, past the indent (Word's TOC +/// styles): the page number may end past the indent, up to the stop. Like Word, the number stays +/// on the text's line whenever it fits between the text and the stop, also when the text ends +/// less than a digit short of the indent; when it doesn't fit there, the tab and the number go +/// to the next line as before. +#[test] +fn right_tab_past_the_right_indent_takes_its_number_up_to_the_stop() { + use wordcraft_doc::props::{TabAlign, TabLeader, TabStop}; + let stop = 72.0 + 468.0; + let mut close = 0; + for indent in [28.35f32, 6.0] { + let edge = stop - indent; + for m in 20..40 { + for r in 2..7 { + let text = format!("{}{}\t104", "abc ".repeat(m), "y".repeat(r)); + let n = text.chars().count(); + let tab = n - 4; + let mut d = Document::from_text(&text); + d.format_paragraphs(&Pos::body(0, 0), &Pos::body(0, 0), &|p| { + p.indent_right = Some(indent); + p.tabs = Some(vec![TabStop { pos: 468.0, align: TabAlign::Right, leader: TabLeader::Dot }]); + }) + .unwrap(); + let l = lay(&d); + let Some(para) = l.pages[0].items.iter().find_map(|i| if let Placed::Lines { para, .. } = i { Some(para.clone()) } else { None }) + else { + panic!("no lines") + }; + let line_of = |k: usize| para.lines.iter().position(|l| l.c0 <= k && k < l.c1).unwrap(); + // Where the text ends and how wide the number is (the text's last letter and the number + // are each on one line; the last two letters are both a "y"). + let last = l.caret(&Pos::body(0, tab - 1)).unwrap().x; + let text_end = last + (last - l.caret(&Pos::body(0, tab - 2)).unwrap().x); + let end = l.caret(&Pos::body(0, n)).unwrap().x; + let number = end - l.caret(&Pos::body(0, n - 3)).unwrap().x; + assert!((end - stop).abs() < 0.5, "indent {indent}, {m} words + {r}: number ends at {end}"); + assert_eq!(line_of(tab), line_of(n - 1), "indent {indent}, {m} words + {r}: the tab and its number are on different lines"); + let same = line_of(tab) == line_of(tab - 1); + assert_eq!(same, text_end + number <= stop + 0.01, "indent {indent}, {m} words + {r}: text ends at {text_end}, number {number} wide"); + if same && text_end > edge - number / 3.0 { + close += 1; + } + } + } + } + assert!(close > 0, "no entry whose text ends within a digit of the indent"); +} + /// A left tab whose only stop is past the right indent wraps; on the next line it still can't /// reach that stop, so it takes no room there and its text starts at the indent instead of /// going to a line of its own. diff --git a/docs/layout-parity.md b/docs/layout-parity.md index 0a39f59cca..2b02835e69 100644 --- a/docs/layout-parity.md +++ b/docs/layout-parity.md @@ -383,6 +383,7 @@ The front-of-text opt-out is retained from the earlier implementation; this patc | 2026-10-27 | trivial | A kept-together paragraph in a continuous section is measured against the next page's body, so a tall different first-page header no longer splits one that fits on page 2 (#3907 CodeRabbit) | | 2026-10-27 | trivial | 2026-10-27 · #4533 review: numeric REF updates lazily index body bookmarks once per update, retaining the first body bookmark for duplicate names. Unsupported switches and ordinary REF/PAGEREF behavior are unchanged; a synthetic 64-reference regression covers repeated updates and duplicate names. Estimates unchanged. | | 2026-10-27 | trivial | 2026-10-27 · #4533: numeric REF fields use paragraph list numbers for no/full/relative context, reusing bounded numbering. Shared context already embedded in a level label is removed; merged-away cells do not count. Unsupported separator/text/position switches and ambiguous contexts or delimiter-free joins retain cached results. Synthetic OOXML regressions; no native Word or real-document validation, estimates unchanged. | +| 2026-10-27 | trivial | A right or decimal tab past the right indent (a TOC entry's page number at the margin) keeps its text on the line while it ends before the stop, also when the text before the tab ends less than a digit short of the indent; measured in Word for Mac 16.93. `layout::tests::right_tab_past_the_right_indent_takes_its_number_up_to_the_stop` | | 2026-10-20 | trivial | A click below a floating table, outside its cells, goes to the paragraph beside or after it instead of a cell of its last row, as checked in Word (#2038) | | 2026-10-19 | trivial | A right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, starts the next line with its text instead of ending the line (a TOC entry's page number keeps its leader); a left tab still past the right indent there takes no room (#1840) | | 2026-10-19 | trivial | Justified lines (compatibility mode 15) shrink their spaces to fit one more word by at most a quarter of the spaces' width (was a fifth) and by at most 0.355 of that word's own width with the space before it. Measured in Word for Mac with synthetic justified paragraphs: words of 2.7 to 80 pt, spaces of 3.3 and 6.7 pt, only justified text, last lines included, never in modes 12 and 14. A real paper's justified abstract now breaks like Word on all 23 lines (3 before); on 180 real-world papers, page counts matching Word went from 139 to 142, none further |