From 03025de783d44d01ed001cda61287feb7d4c5957 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:25:07 +0800 Subject: [PATCH] Split tall halign tables across pages --- crates/texrra-tex-core/src/lib.rs | 151 +++++++++++++++++++++++++++--- docs/TEX_CORE.md | 2 +- 2 files changed, 140 insertions(+), 13 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 840446a..cb1b687 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -75,6 +75,43 @@ pub enum VerticalItem { } impl Halign { + /// Split a tall table at row boundaries so each chunk fits `max_height`. + /// + /// Column widths are preserved so the fragments still line up when the + /// page builder places them on consecutive pages. + pub fn split_for_height(&self, max_height: i64) -> Vec { + if self.rows.is_empty() { + return vec![self.clone()]; + } + let max_height = max_height.max(self.line_height); + let mut chunks = Vec::new(); + let mut start = 0_usize; + let mut height = 0_i64; + for (index, row) in self.rows.iter().enumerate() { + let row_height = if row.rule { 26_214 } else { self.line_height }; + if index > start && height + row_height > max_height { + chunks.push(Self { + rows: self.rows[start..index].to_vec(), + column_widths: self.column_widths.clone(), + width: self.width, + height, + line_height: self.line_height, + }); + start = index; + height = 0; + } + height += row_height; + } + chunks.push(Self { + rows: self.rows[start..].to_vec(), + column_widths: self.column_widths.clone(), + width: self.width, + height, + line_height: self.line_height, + }); + chunks + } + /// The horizontal rectangle occupied by a cell's text: `(x, width)`. /// /// Spanned cells include the intercolumn gaps they cover. The x offset is @@ -621,18 +658,34 @@ impl Engine { current.items.push(item.clone()); } VerticalItem::Halign(halign) => { - if current.items.is_empty() && top_float_height(¤t) == 0 { - used = document.top_skip.max(halign.height); - } else if used + halign.height - > document.page_goal - insertion_height - bottom_float_height(¤t) - { - pages.push(std::mem::replace(&mut current, Page::empty())); - used = document.top_skip.max(halign.height); - insertion_height = 0; + let mut available = if current.items.is_empty() { + document.page_goal - document.top_skip } else { - used += halign.height; + document.page_goal + - used + - insertion_height + - bottom_float_height(¤t) + }; + if !current.items.is_empty() && available < halign.line_height { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip; + insertion_height = 0; + available = document.page_goal - document.top_skip; + } + let chunks = halign.split_for_height(available.max(halign.line_height)); + for (index, chunk) in chunks.iter().enumerate() { + if index > 0 { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip; + insertion_height = 0; + } + if current.items.is_empty() && top_float_height(¤t) == 0 { + used = document.top_skip.max(chunk.height); + } else { + used += chunk.height; + } + current.items.push(VerticalItem::Halign(chunk.clone())); } - current.items.push(item.clone()); } VerticalItem::Paragraph(paragraph) => { if current.items.is_empty() && top_float_height(¤t) == 0 { @@ -1588,7 +1641,7 @@ mod tests { #[test] fn halign_text_is_painted_at_ten_points() { let unit = Unit::halign_from_source(1, r"A & B", 2, 10); - let document = Document::new(1000, vec![unit]); + let document = Document::new(2_000_000, vec![unit]); let engine = Engine::layout(&document); let list = engine.pages()[0].display_list(); let size = list.items.iter().find_map(|item| match item { @@ -1600,7 +1653,7 @@ mod tests { #[test] fn halign_display_list_paints_cells_and_rules() { let unit = Unit::halign_from_source(1, r"A & B \\ \hline C & D", 2, 10); - let document = Document::new(1000, vec![unit]); + let document = Document::new(2_000_000, vec![unit]); let engine = Engine::layout(&document); let list = engine.pages()[0].display_list(); let texts = list @@ -1773,6 +1826,80 @@ mod tests { assert_eq!(halign.height, 1_599_078); } + #[test] + fn halign_splits_at_row_boundaries() { + let rows = vec![ + HalignRow { + cells: vec![HalignCell { + text: "A".to_owned(), + span: 1, + alignment: CellAlignment::Left, + }], + rule: false, + }, + HalignRow { + cells: vec![HalignCell { + text: "B".to_owned(), + span: 1, + alignment: CellAlignment::Left, + }], + rule: false, + }, + HalignRow { + cells: vec![HalignCell { + text: "C".to_owned(), + span: 1, + alignment: CellAlignment::Left, + }], + rule: false, + }, + ]; + let unit = Unit::halign(0, rows, 1, 0); + let VerticalItem::Halign(halign) = &unit.items[0] else { + panic!("expected halign"); + }; + let chunks = halign.split_for_height(1_700_000); + assert_eq!(chunks.len(), 2); + assert_eq!(chunks[0].rows.len(), 2); + assert_eq!(chunks[1].rows.len(), 1); + assert_eq!(chunks[0].height, 2 * halign.line_height); + assert_eq!(chunks[1].height, halign.line_height); + } + + #[test] + fn tall_halign_splits_across_pages() { + let rows = vec![ + HalignRow { + cells: vec![HalignCell { + text: "A".to_owned(), + span: 1, + alignment: CellAlignment::Left, + }], + rule: false, + }, + HalignRow { + cells: vec![HalignCell { + text: "B".to_owned(), + span: 1, + alignment: CellAlignment::Left, + }], + rule: false, + }, + HalignRow { + cells: vec![HalignCell { + text: "C".to_owned(), + span: 1, + alignment: CellAlignment::Left, + }], + rule: false, + }, + ]; + let document = Document::new(2_000_000, vec![Unit::halign(0, rows, 1, 0)]); + let layout = Engine::layout(&document); + assert_eq!(layout.pages().len(), 2); + assert_eq!(layout.pages()[0].items.len(), 1); + assert_eq!(layout.pages()[1].items.len(), 1); + } #[test] fn checkpoint_round_trips_through_serde() { let engine = Engine::layout(&document()); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 9683a65..788ec1f 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -266,4 +266,4 @@ M2 (partial): a 0.4pt rule at 40% of the body width is painted above insertion m M2 (partial): top and bottom float placeholders are painted at the page edges in display lists; real float material and float-page fractions remain. -M3 (partial): Unit::halign normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the display-list painter uses a 10pt glyph size inside 12pt rows. Rules and spans are painted; page-level table splitting remains. +M3 (partial): Unit::halign normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the display-list painter uses a 10pt glyph size inside 12pt rows. Rules and spans are painted; tall tables split at row boundaries across pages (split_for_height) with column widths preserved.