A table row split across pages no longer runs off the bottom of the page (#6827)

* Tables: split the paragraph that starts a cell's part of a page where its lines fall

A row split across pages whose cell continued with a paragraph of two or three
lines, of which only the first (or two) fit, kept nothing on the page: the
orphan/widow rule moved the paragraph on whole, but it already started the page.
The row was then placed whole and ran off the bottom. Word splits such a paragraph
where the lines fall (a lone first line stays; three lines with room for two split
two and one; a continuation's lone line stays). A paragraph below other content of
its cell, and a row starting below other content of a page, keep the rule.

Part of #6823 (phase 1: not built or run yet).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* Tables: a can't split row taller than a page splits at the page's end; more Word-measured cases

A can't split row (not an exact one) taller than a page now moves to a new page when it starts
below text and splits where the page ends, as Word does, instead of running off the page. The
tests now cover nested tables, a repeated header row, text lines, rows with several paragraphs and
the exact configurations measured in Word. The earlier note that Word keeps two and one below
other content was a misreading of a run without compatibility mode 15 and is removed.

Part of #6823 (phase 1: not built or run yet).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* Tables: review fixes for the split-row work

The new test module defines its own lay helper; only text in the flow decides which paragraph
starts a cell's part of a page, so a floating picture above it no longer keeps the old orphan
rule; a can't split row only splits when it is taller than the room; a stale test comment is
updated; tests for a floating picture and a row that fits a page.

Part of #6823.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* Tables: a test for a floating picture above the first paragraph; rustfmt

Floats are placed with the first piece of their paragraph, so the test puts the picture on the
table's first page. Without the in-flow-only rule it fails.

Part of #6823.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* 6823: Document split-row limits

* 6823: Clarify widow control in split rows

---------

Co-authored-by: vangel76 <[email protected]>
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Co-authored-by: Martin Ball <[email protected]>
This commit is contained in:
Valentin Klein
2026-11-21 15:32:16 +00:00
committed by GitHub
co-authored by Claude Sonnet 5.5 vangel76 Martin Ball
parent 45f49e9990
commit f44b2e4165
7 changed files with 349 additions and 11 deletions
+3 -1
View File
@@ -1,6 +1,6 @@
# WordCraft roadmap
> **Last reviewed:** 2026-10-31 · **Last updated:** 2026-11-18 · **Change:** trivial (progress log: TIFF picture chooser, drop and pasted-path routing, #6704); previously minor (added layout.fitOnePage and its Layout › Page Setup ribbon button to condense a multi-page document by one page while preserving typography hierarchy); previously minor (#3398: persistent screen paper colors; parity estimates unchanged); previously minor (#3693: additional Delete Previous Sentence command and shortcut; Word-parity estimates unchanged)
> **Last reviewed:** 2026-10-31 · **Last updated:** 2026-11-21 · **Change:** trivial (table rows split across pages: a cell's first paragraph on the page can split even when only one line fits, if the row starts at the top, with or without its own headers, or continues from the previous page (#6823)); previously trivial (progress log: TIFF picture chooser, drop and pasted-path routing, #6704); previously minor (added layout.fitOnePage and its Layout › Page Setup ribbon button to condense a multi-page document by one page while preserving typography hierarchy); previously minor (#3398: persistent screen paper colors; parity estimates unchanged); previously minor (#3693: additional Delete Previous Sentence command and shortcut; Word-parity estimates unchanged)
> **CLI path update:** 2026-11-04 · Options before a non-Unicode input filename no longer make the CLI reject that filename; text option values still require Unicode; `run --file` and `run --script` retain native paths. No ranked gap or readiness estimate changes.
> **Floating anchor row segments:** 2026-11-07 · A floating anchor after a manual page/column break counts visible content on the far side of the same wrapped row. Marker-only cover anchors retain their previous-page rule. Synthetic picture spacing, placement and history controls; no new native Word measurement. Readiness estimates unchanged.
@@ -920,6 +920,8 @@ Quoted field escapes are interpreted like Word, and body `PAGE` fields use their
Section page-numbering settings preserve `w:chapStyle` values across the full unsigned-byte range.
MATH glyphs that reference the same construction now share its immutable variants and assembly data (#3630). Glyph mappings, values and the existing partial-record fallback stay unchanged. Repeated glyph IDs keep their last valid reference without retaining superseded constructions. Distinct overlapping constructions and assembly-only sharing remain separate.
- 2026-11-15: Layout: the first paragraph of a table cell on a page can split even when only one line fits, if the row starts at the top (possibly below its own headers) or continues from the previous page, and a non-exact row marked Can't Split that is taller than a page splits at the page's end, as in Word, instead of the row running off the bottom of the page (#6823). Readiness estimates unchanged.
## Revision history
> 2026-11-08 · Progress log: `convert --strict` refuses a PDF or PNG that would silently lack a character or a picture (#6005).
+35 -5
View File
@@ -926,6 +926,18 @@ fn split_row_with(row: &RowLayout, cut: f32, whole_cells: bool) -> Option<(RowLa
let own_depth = row.items.iter().filter_map(|it| if let Placed::Cell { table, .. } = it { Some(table.0.len()) } else { None }).min();
// The first line of each paragraph that moves on (`l1` when all of them stay), by cell and
// item.
// The top of a cell's text on this page decides which paragraph starts the cell's part of the
// page. Only text in the flow counts: a floating picture or shape anchored in the cell can sit
// above it without the text being any less the first.
let content_top = |it: &Placed| -> Option<f32> {
if background(it) {
return None;
}
if let Some((_, _, _, y)) = text_lines(it) {
return Some(y);
}
it.turned_bounds().map(|r| r.y)
};
let splits: Vec<Vec<usize>> = cells
.iter()
.zip(&ends)
@@ -933,13 +945,18 @@ fn split_row_with(row: &RowLayout, cut: f32, whole_cells: bool) -> Option<(RowLa
// Where the cell's text ends: the paragraph ending lowest is the cell's last.
let lowest =
items.iter().filter_map(|it| text_lines(it).and_then(|(p, l0, l1, y)| crate::item_bottom(y, p, l0, l1))).fold(f32::MIN, f32::max);
// The top of what the cell holds on this page: a paragraph there starts the cell's
// part of the page, so a row that doesn't start below other content has nowhere to
// move it to (see `line_split`).
let cell_top = items.iter().filter_map(&content_top).fold(f32::MAX, f32::min);
items
.iter()
.map(|it| {
let Some((para, l0, l1, y)) = text_lines(it) else { return 0 };
// The cell's last paragraph ends with the cell's content (its space after).
let last = l1 == para.lines.len() && crate::item_bottom(y, para, l0, l1).is_some_and(|b| b >= lowest - 0.01);
line_split(para, (l0, l1), y, limit, end.filter(|_| last))
let first = !whole_cells && y <= cell_top + 0.01;
line_split(para, (l0, l1), y, limit, end.filter(|_| last), first)
})
.collect()
})
@@ -1262,7 +1279,16 @@ fn text_lines(it: &Placed) -> Option<(&ParaLayout, usize, usize, f32)> {
/// above `limit`: the last line of a cell's last paragraph needs room down to the cell's content
/// `end`, its space after. Widow control keeps two lines on either side of the break, or moves
/// the paragraph on whole.
fn line_split(para: &ParaLayout, (l0, l1): (usize, usize), y: f32, limit: f32, end: Option<f32>) -> usize {
///
/// `first`: the paragraph is the topmost content of its cell on a page the row doesn't start
/// below other content on (the row continues from the page before, or starts the page). Moving
/// it whole would gain nothing, and Word then splits it where the lines fall (measured with
/// synthetic documents, Word for Windows 16.0, compatibility mode 15): a lone first line stays,
/// and a three-line paragraph with room for two splits two and one, not leaving the first line
/// alone (a continuation's lone line stays too). A paragraph of four lines or more still takes
/// one more line along with its widow.
/// Without this, nothing was kept and the row ran off the bottom of the page (#6823).
fn line_split(para: &ParaLayout, (l0, l1): (usize, usize), y: f32, limit: f32, end: Option<f32>, first: bool) -> usize {
let n = para.lines.len();
let mut k = l0;
while k < l1 {
@@ -1276,12 +1302,16 @@ fn line_split(para: &ParaLayout, (l0, l1): (usize, usize), y: f32, limit: f32, e
// The body's rule differs: there a widow takes a line along only when three or more lines are
// on the page (`place_para_in_flow`).
if para.rp.widow_control && k > l0 && k < l1 {
// A widow: take one more line along.
// A widow: take one more line along, unless that leaves nothing of a paragraph that
// starts the page on it, or its first line alone.
if n - k == 1 {
k -= 1;
let taken = k - 1;
if !(first && (taken <= l0 || (l0 == 0 && taken == 1))) {
k = taken;
}
}
// An orphan (the first line alone): the paragraph moves on whole.
if l0 == 0 && k == 1 {
if l0 == 0 && k == 1 && !first {
k = 0;
}
}
+10 -1
View File
@@ -255,6 +255,13 @@ pub(crate) fn place_table(ctx: &mut Ctx, pb: &mut PageBuilder, t: &wordcraft_doc
.rows
.get(ri)
.is_some_and(|r| r.props.cant_split || (r.props.height_rule == HeightRule::Exact && r.props.height.is_some_and(|h| h > 0.0)));
// A Can't Split row (not an exact one) that is taller than a page splits where the page
// ends, as in Word (measured with synthetic documents): it moves to a new page first
// when it fits there, and only on a page it still doesn't fit does it break.
let splits_when_too_tall = ri >= header_rows.len()
&& t.rows
.get(ri)
.is_some_and(|r| r.props.cant_split && !(r.props.height_rule == HeightRule::Exact && r.props.height.is_some_and(|h| h > 0.0)));
let mut rest: Option<table::RowLayout> = None;
// On a new page holding only the repeated header rows.
let mut fresh = false;
@@ -290,7 +297,9 @@ pub(crate) fn place_table(ctx: &mut Ctx, pb: &mut PageBuilder, t: &wordcraft_doc
// (#4272): moving on can give the cell room there.
let first_part = rest.is_none() && !pb.at_top() && !fresh && !under_top_headers;
let moves_on = first_part && short_of_min(ri, room);
let split = match (splittable && !moves_on && !defer_kept, first_part) {
// A row that fits the room but for the border below it is still placed whole.
let too_tall_to_keep = splits_when_too_tall && !first_part && cur.height > room + 0.01;
let split = match ((splittable || too_tall_to_keep) && !moves_on && !defer_kept, first_part) {
(false, _) => None,
(true, true) => split_keeping_nested(ctx.doc, cur, room),
// On a fresh page, a nested row taller than the room splits rather than overflowing.
+3 -1
View File
@@ -21,6 +21,7 @@ mod numeral;
mod pages;
mod styleref;
mod table_contact;
mod table_first_line;
mod textbox;
mod vertical;
@@ -4665,7 +4666,8 @@ fn tall_rows_split_across_pages() {
.find_map(|i| if let Placed::Lines { path, para, .. } = i { (path.0 == vec![1, 0, 0, 0]).then_some(para.lines.len()) } else { None })
.unwrap_or(0);
assert_eq!(total, n);
// "Can't split" rows move whole instead.
// A "can't split" row taller than a page splits too, from the top of a page, as in Word (#6823);
// a shorter one moves on whole (`table_first_line`).
let mut d2 = Document::from_text("before\nafter");
t.rows[0].props.cant_split = true;
d2.insert_block(StoryRef::Body, &Path::top(1), wordcraft_doc::Block::Table(t)).unwrap();
+294
View File
@@ -0,0 +1,294 @@
//! A paragraph that starts a cell's part of a page, in a row split across pages, is split where
//! its lines fall, as in Word (#6823): moving it on whole would gain nothing, and keeping
//! widow/orphan control left the cell with nothing on the page, so the row was placed whole and
//! ran off the bottom. A paragraph below other content of its cell keeps the rule, and so does a
//! row that starts below other content of the page. A "can't split" row taller than a page
//! splits too.
//!
//! The expectations were measured in Word for Windows 16.0 (compatibility mode 15, widow control
//! on, Letter with 1 inch margins) on synthetic documents laid out like these.
use crate::*;
use wordcraft_doc::para::{Anchor, Float, InlineObject, Wrap};
use wordcraft_doc::props::{LineSpacing, ParaProps};
use wordcraft_doc::{Table, para_block};
fn lay(doc: &Document) -> DocLayout {
let mut c = LayoutCache::new();
layout(doc, &mut c, &LayoutOptions::default())
}
fn exact(height: f32, widow: bool) -> ParaProps {
ParaProps {
line_spacing: Some(LineSpacing::Exactly(height)),
space_before: Some(0.0),
space_after: Some(0.0),
widow_control: Some(widow),
..Default::default()
}
}
fn lines_para(lines: usize, height: f32, widow: bool, name: &str) -> Arc<Block> {
para_block(lines_paragraph(lines, height, widow, name))
}
fn lines_paragraph(lines: usize, height: f32, widow: bool, name: &str) -> Paragraph {
let text: Vec<String> = (1..=lines).map(|i| format!("{name}{i}")).collect();
let mut p = Paragraph::with_text(&text.join(&wordcraft_doc::para::LINE_BREAK.to_string()), Default::default());
p.props = exact(height, widow);
p
}
/// What the table is made of: `pre` one-line paragraphs of 16 pt before it, an optional repeated
/// header row, a row (can't split or not) whose second cell holds `fill` lines of `fill_height`,
/// the paragraphs `before` (lines and line height) and last a paragraph `last` of lines of a
/// height. The last two are inside a nested table when `nested` is set.
struct Case {
pre: usize,
header: bool,
cant_split: bool,
nested: bool,
fill: usize,
fill_height: f32,
before: Vec<(usize, f32)>,
last: (usize, f32),
widow: bool,
/// A picture floating through the text, anchored in the last paragraph and placed above it.
float_above: bool,
}
impl Case {
fn new(last: (usize, f32)) -> Case {
Case {
pre: 0,
header: false,
cant_split: false,
nested: false,
fill: 45,
fill_height: 14.0,
before: Vec::new(),
last,
widow: true,
float_above: false,
}
}
/// The lines of the last paragraph on each page.
fn per_page(&self) -> Vec<usize> {
crate::test_fonts();
let w = self.widow;
let mut d = Document::from_text("After");
for i in 0..self.pre {
let mut p = Paragraph::with_text(&format!("F{i}"), Default::default());
p.props = exact(16.0, w);
d.insert_block(StoryRef::Body, &Path::top(i), Block::Para(p)).unwrap();
}
let mut t = Table::new(1 + usize::from(self.header), 2, 400.0);
t.props.cell_margins = Some([0.0, 5.4, 0.0, 5.4]);
let row = usize::from(self.header);
if self.header {
t.rows[0].props.header = true;
for c in &mut t.rows[0].cells {
c.blocks = vec![lines_para(1, 14.0, w, "H")];
}
}
t.rows[row].props.cant_split = self.cant_split;
let mut inner: Vec<Arc<Block>> = self.before.iter().map(|&(n, h)| lines_para(n, h, w, "P")).collect();
let mut last = lines_paragraph(self.last.0, self.last.1, w, "B");
if self.float_above {
let float = Float { wrap: Wrap::Through, h_rel: Anchor::Column, v_rel: Anchor::Paragraph, x: 250.0, y: -6.0, ..Default::default() };
let picture = InlineObject::Image {
media: "float".into(),
w: 20.0,
h: 20.0,
alt: "float".into(),
float,
crop: [0.0; 4],
fx: Default::default(),
ole: None,
};
last.insert_object(0, picture, &Default::default()).unwrap();
}
inner.push(para_block(last));
// Without a fill paragraph the cell starts with `inner`.
let mut blocks = if self.fill > 0 { vec![lines_para(self.fill, self.fill_height, w, "A")] } else { Vec::new() };
let mut at = vec![self.pre as u32, row as u32, 1, (blocks.len() + inner.len()) as u32 - 1];
if self.nested {
let mut nested = Table::new(1, 1, 200.0);
nested.rows[0].cells[0].blocks = inner.clone();
at = vec![self.pre as u32, row as u32, 1, blocks.len() as u32, 0, 0, inner.len() as u32 - 1];
blocks.push(Arc::new(Block::Table(nested)));
blocks.push(lines_para(1, 1.0, w, "Z"));
} else {
blocks.extend(inner);
}
t.rows[row].cells[1].blocks = blocks;
d.insert_block(StoryRef::Body, &Path::top(self.pre), Block::Table(t)).unwrap();
lay(&d)
.pages
.iter()
.map(|p| {
p.items
.iter()
.filter_map(|it| match it {
Placed::Lines { path, l0, l1, .. } if path.0 == at => Some(l1 - l0),
_ => None,
})
.sum()
})
.collect()
}
}
/// A three-line paragraph at the top of a page with room for its first line only keeps that line
/// there (the next two go on, one to a page here).
#[test]
fn a_first_line_alone_at_the_top_of_a_page_stays_in_a_split_row() {
assert_eq!(Case::new((3, 400.0)).per_page(), vec![0, 1, 1, 1]);
}
/// Two lines, room for one: one and one.
#[test]
fn a_two_line_paragraph_at_the_top_of_a_page_splits_one_and_one() {
assert_eq!(Case::new((2, 400.0)).per_page(), vec![0, 1, 1]);
}
/// Three lines, room for two: two and one (not the whole paragraph on, which is no better at the
/// top of a page, and not one and two).
#[test]
fn a_three_line_paragraph_at_the_top_of_a_page_with_room_for_two_splits_two_and_one() {
assert_eq!(Case::new((3, 300.0)).per_page(), vec![0, 2, 1]);
}
/// Guard (passes without the fix): four lines with room for three still keep two for the widow.
#[test]
fn four_lines_at_the_top_of_a_page_with_room_for_three_still_keep_two_for_the_widow() {
assert_eq!(Case::new((4, 200.0)).per_page(), vec![0, 2, 2]);
}
/// Guard (passes without the fix): a paragraph below another one of its cell on the page keeps
/// the orphan rule, so its single line there moves on with the rest.
#[test]
fn a_paragraph_below_other_content_of_its_cell_still_moves_on_whole_instead_of_an_orphan() {
let mut c = Case::new((3, 200.0));
c.before = vec![(1, 300.0)];
assert_eq!(c.per_page(), vec![0, 0, 3]);
}
/// Where only the later paragraphs of a cell continue, the one that moved to the top of a page
/// splits at its lines (two and one), the paragraph before it having stayed.
#[test]
fn a_later_paragraph_moved_to_the_top_of_a_page_splits_where_its_lines_fall() {
let mut c = Case::new((3, 300.0));
c.before = vec![(1, 100.0)];
assert_eq!(c.per_page(), vec![0, 0, 2, 1]);
}
/// Guard (passes without the fix): four lines below a paragraph, room for three, keep two.
#[test]
fn four_lines_below_other_content_with_room_for_three_still_keep_two_for_the_widow() {
let mut c = Case::new((4, 200.0));
c.before = vec![(1, 40.0)];
assert_eq!(c.per_page(), vec![0, 2, 2]);
}
/// Guard (passes without the fix): without widow control the paragraph always split where it fell.
#[test]
fn without_widow_control_the_lines_split_where_they_fall() {
let mut c = Case::new((3, 400.0));
c.widow = false;
assert_eq!(c.per_page(), vec![0, 1, 1, 1]);
}
/// A paragraph of a nested table splits the same way.
#[test]
fn a_nested_paragraph_at_the_top_of_a_page_splits_where_its_lines_fall() {
let mut c = Case::new((3, 300.0));
c.nested = true;
assert_eq!(c.per_page(), vec![0, 2, 1]);
let mut c = Case::new((2, 400.0));
c.nested = true;
assert_eq!(c.per_page(), vec![0, 1, 1]);
}
/// A nested paragraph below another one of the nested cell moves on whole, then splits at the top
/// of the page.
#[test]
fn a_nested_paragraph_below_other_content_moves_on_then_splits() {
let mut c = Case::new((3, 300.0));
c.nested = true;
c.before = vec![(1, 100.0)];
assert_eq!(c.per_page(), vec![0, 0, 2, 1]);
}
/// Under a repeated header row the paragraph that starts the page splits at its lines too. The
/// lines per page are Word's; Word leaves the header off some of the pages, which is not covered.
#[test]
fn under_a_repeated_header_row_the_paragraph_starting_the_page_splits_where_its_lines_fall() {
let mut c = Case::new((3, 300.0));
c.header = true;
c.fill = 44;
assert_eq!(c.per_page(), vec![0, 2, 1]);
let mut c = Case::new((3, 300.0));
c.header = true;
c.fill = 44;
c.before = vec![(1, 100.0)];
assert_eq!(c.per_page(), vec![0, 0, 2, 1]);
}
/// Guard (passes without the fix): under a header row, four lines with room for three keep two.
#[test]
fn under_a_repeated_header_row_four_lines_still_keep_two_for_the_widow() {
let mut c = Case::new((4, 200.0));
c.header = true;
c.fill = 44;
assert_eq!(c.per_page(), vec![0, 2, 2]);
}
/// A can't split row taller than a page splits where the pages end.
#[test]
fn a_cant_split_row_taller_than_a_page_splits_where_the_page_ends() {
let mut c = Case::new((3, 400.0));
c.cant_split = true;
assert_eq!(c.per_page(), vec![0, 1, 1, 1]);
}
/// A can't split row taller than a page that starts below text goes to the next page first, then
/// splits there.
#[test]
fn a_tall_cant_split_row_below_text_starts_the_next_page_and_splits_there() {
let mut c = Case::new((3, 400.0));
c.cant_split = true;
c.pre = 20;
assert_eq!(c.per_page(), vec![0, 0, 1, 1, 1]);
}
/// Guard (passes without the fix): a can't split row that fits the next page moves there whole.
#[test]
fn a_cant_split_row_that_fits_the_next_page_moves_there_whole() {
let mut c = Case::new((3, 16.0));
c.cant_split = true;
c.pre = 38;
c.fill = 5;
c.fill_height = 16.0;
assert_eq!(c.per_page(), vec![0, 3]);
}
/// A picture floating above the paragraph that starts the cell doesn't make it any less the
/// first: only the text in the flow decides. (Floats are placed with the first piece of their
/// paragraph, so this is the table's first page.)
#[test]
fn a_floating_picture_above_the_first_paragraph_doesnt_keep_it_from_splitting() {
let mut c = Case::new((3, 400.0));
c.fill = 0;
c.float_above = true;
assert_eq!(c.per_page(), vec![1, 1, 1]);
}
/// Guard (passes without the fix): a can't split row that fits a page with room to spare stays
/// whole on it, whatever the border below it needs.
#[test]
fn a_cant_split_row_that_fits_a_page_is_left_whole() {
let mut c = Case::new((1, 10.0));
c.cant_split = true;
assert_eq!(c.per_page(), vec![1, 0], "the row stays on page 1 (the paragraph after it is on page 2)");
}
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long