Layout: a paragraph of only a page break moves when it doesn't fit (#3198)

* Layout: a paragraph of only a page break moves when it doesn't fit

* Apply cargo fmt

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs: name the deferred double-spacing case in the break-only paragraph entry

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Dominik Strebel <dominik.strebel@gmail.com>
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Martin Ball <98639962+benandez@users.noreply.github.com>
Co-authored-by: vangel76 <271991477+vangel76@users.noreply.github.com>
Co-authored-by: Martin Ball <martin.ball@ucscaregroup.co.uk>
This commit is contained in:
Valentin Klein
2026-11-21 15:52:13 +00:00
committed by GitHub
co-authored by Claude Sonnet 5.5 Dominik Strebel Martin Ball vangel76 Martin Ball
parent f44b2e4165
commit 4db39e40eb
4 changed files with 58 additions and 10 deletions
+10 -5
View File
@@ -455,14 +455,19 @@ impl<'a> Flow<'a> {
fit
}
/// Whether the line may overflow the page: one holding nothing but a page or column break stays
/// at the bottom of the page even when it doesn't fit (it only ends the page, and moving it
/// would leave a blank page behind it, as in Word), and so does an empty paragraph closing a
/// section before a new page (measured in Word: it stays on a full page instead of starting
/// one of its own).
/// Whether the line (the paragraph's line `l0`) may overflow the page: one holding nothing but
/// a page or column break, with text after it in the paragraph, stays at the bottom of the
/// page even when it doesn't fit (it only ends the page, and moving it would leave a blank
/// page behind it, as in Word), and so does an empty paragraph closing a section before a new
/// page (measured in Word: it stays on a full page instead of starting one of its own).
/// Measured in Word: a paragraph of only a page break is an ordinary line, and when its mark's
/// text doesn't fit it goes to the next page, blank page and all.
fn stays_at_bottom(&self, pb: &PageBuilder, l: &para::Line) -> bool {
let p = self.p;
let mark_only_after =
l.end == LineEnd::PageBreak && self.pl.lines.get(self.l0 + 1..).is_some_and(|rest| rest.iter().all(|m| only_markers(p, m.start..m.stop)));
let only_break = matches!(l.end, LineEnd::PageBreak | LineEnd::ColumnBreak)
&& !mark_only_after
&& p.text
.get(l.start..l.stop)
.is_some_and(|t| t.chars().all(|c| matches!(c, wordcraft_doc::para::PAGE_BREAK | wordcraft_doc::para::COLUMN_BREAK)));
+40
View File
@@ -8026,6 +8026,46 @@ fn a_page_break_at_the_bottom_of_a_full_page_leaves_no_blank_page() {
assert!(last_on_2, "the text after the break is on page 2");
}
/// Measured in Word for Mac: a paragraph holding only a page break is an ordinary line. When it
/// doesn't fit at the bottom of a full page it goes to the next one, and its break leaves that
/// page blank. Only a break with text after it in its paragraph stays on the full page (#534).
/// WordCraft kept both on the page, so a paper's references started a page early.
#[test]
fn a_paragraph_of_only_a_page_break_moves_when_it_does_not_fit() {
use wordcraft_doc::props::LineSpacing;
let chr = wordcraft_doc::CharProps { font: Some("Source Sans 3".into()), size: Some(12.0), ..Default::default() };
let doc = |spacing: LineSpacing, room: f32, tail: &[&str]| {
let para = |text: &str| {
let mut p = wordcraft_doc::Paragraph::with_text(text, chr.clone());
p.mark = chr.clone();
p.props = ParaProps { line_spacing: Some(spacing), space_before: Some(0.0), space_after: Some(0.0), ..Default::default() };
wordcraft_doc::para_block(p)
};
let mut d = Document::new();
d.body = (0..20).map(|i| para(&format!("Line {i}"))).chain(tail.iter().map(|t| para(t))).collect();
let l = lay(&d);
let pitch = l.caret(&Pos::body(1, 0)).unwrap().top - l.caret(&Pos::body(0, 0)).unwrap().top;
d.last_section.margin_bottom = d.last_section.page_h - d.last_section.margin_top - 20.0 * pitch - room;
d
};
let pages = |d: &Document| lay(d).pages.len();
let single = {
let l = lay(&doc(LineSpacing::Multiple(1.0), 600.0, &[]));
l.caret(&Pos::body(1, 0)).unwrap().top - l.caret(&Pos::body(0, 0)).unwrap().top
};
let only = ["\u{000C}", "After"];
let start = ["\u{000C}After"];
// Exactly 24 pt lines, 10 pt left: the break's own paragraph moves, a blank page follows.
assert_eq!(pages(&doc(LineSpacing::Exactly(24.0), 10.0, &only)), 3, "exact, break only");
assert_eq!(pages(&doc(LineSpacing::Exactly(24.0), 10.0, &start)), 2, "exact, break before text");
// Double spacing: the paragraph moves when its text cannot fit. Its leading can hang into
// the bottom margin once the paragraph mark fits.
assert_eq!(pages(&doc(LineSpacing::Multiple(2.0), single - 0.2, &only)), 3, "double, text doesn't fit");
assert_eq!(pages(&doc(LineSpacing::Multiple(2.0), single + 0.2, &only)), 2, "double, leading hangs");
assert_eq!(pages(&doc(LineSpacing::Multiple(2.0), 2.0 * single + 0.2, &only)), 2, "double, line fits");
assert_eq!(pages(&doc(LineSpacing::Multiple(2.0), single - 0.2, &start)), 2, "double, break before text");
}
/// #534: indents in characters (`w:firstLineChars`) are measured in the paragraph mark's font
/// size and win over the indent in points.
#[test]
+4 -2
View File
@@ -302,8 +302,10 @@ fn inline_box_that_resizes_to_fit_takes_its_texts_height() {
}
/// Filler paragraphs before the box in [`inline_box_that_resizes_to_fit_takes_its_texts_height`]:
/// enough that a 10 cm box no longer leaves room for the page break on the first page.
const FILLER: usize = 22;
/// enough that a 10 cm box no longer leaves room for the page break on the first page, while the
/// box fitted to its three lines does (a paragraph of only a page break that doesn't fit goes to
/// the next page, as in Word).
const FILLER: usize = 20;
/// A floating box that resizes to fit its text takes its text's height too, both where it is
/// drawn and the room the text beside it wraps around (an OpenDocument frame without a height).
File diff suppressed because one or more lines are too long