From 12288b51eef718f562bfe15d3076044642723558 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 21:07:26 +0800 Subject: [PATCH] Split oversized route-D insertions across pages --- crates/texrra-tex-core/src/lib.rs | 93 ++++++++++++++++++++++++++++--- docs/TEX_CORE.md | 3 +- 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index b3568a4..575e289 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -589,6 +589,20 @@ fn source_hash(source: &str) -> u64 { hasher.finish() } +fn item_height(item: &VerticalItem) -> i64 { + match item { + VerticalItem::Box(vertical_box) => vertical_box.height, + VerticalItem::Rule { height, .. } => *height, + VerticalItem::Glue(amount) => (*amount).max(0), + VerticalItem::Insertion(insertion) => insertion.height, + VerticalItem::Float(float) => float.height, + VerticalItem::Halign(halign) => halign.height, + VerticalItem::Paragraph(paragraph) => paragraph.height, + VerticalItem::Math { height, depth, .. } => height + depth, + VerticalItem::Mark(_) | VerticalItem::Penalty(_) => 0, + } +} + fn top_float_height(page: &Page) -> i64 { page.floats .iter() @@ -805,14 +819,49 @@ impl Engine { } } VerticalItem::Insertion(insertion) => { - let goal = document.page_goal - insertion_height; - if !current.items.is_empty() && used + insertion.height > goal { - pages.push(std::mem::replace(&mut current, Page::empty())); - used = document.top_skip; - insertion_height = 0; + let page_capacity = document.page_goal.saturating_sub(document.top_skip); + if insertion.height > page_capacity && !insertion.items.is_empty() { + if !current.items.is_empty() { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip; + insertion_height = 0; + } + let mut chunk = Vec::new(); + let mut chunk_height = 0; + for sub_item in &insertion.items { + let sub_height = item_height(sub_item); + if chunk_height + sub_height > page_capacity && !chunk.is_empty() { + current.insertions.push(Insertion { + class: insertion.class, + height: chunk_height, + items: std::mem::take(&mut chunk), + }); + insertion_height = chunk_height; + chunk_height = 0; + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip; + } + chunk.push(sub_item.clone()); + chunk_height += sub_height; + } + if !chunk.is_empty() { + current.insertions.push(Insertion { + class: insertion.class, + height: chunk_height, + items: chunk, + }); + insertion_height = chunk_height; + } + } else { + let goal = document.page_goal - insertion_height; + if !current.items.is_empty() && used + insertion.height > goal { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip; + insertion_height = 0; + } + insertion_height += insertion.height; + current.insertions.push(insertion.clone()); } - insertion_height += insertion.height; - current.insertions.push(insertion.clone()); } VerticalItem::Mark(mark) => { current.marks.push(mark.clone()); @@ -1483,6 +1532,36 @@ mod tests { assert_eq!(before, after); } + #[test] + fn tall_insertions_split_across_pages() { + let document = Document::new( + 100, + vec![Unit::insertion( + 1, + 0, + 120, + vec![ + VerticalItem::Box(VerticalBox { + width: 10, + height: 40, + }), + VerticalItem::Box(VerticalBox { + width: 10, + height: 40, + }), + VerticalItem::Box(VerticalBox { + width: 10, + height: 40, + }), + ], + )], + ); + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 2); + assert_eq!(engine.pages()[0].insertions[0].height, 80); + assert_eq!(engine.pages()[1].insertions[0].height, 40); + } + #[test] fn insertion_reserves_page_height() { let boxed = |height: i64| VerticalItem::Box(VerticalBox { width: 100, height }); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 75134c4..003d699 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -165,6 +165,7 @@ a single cost function. - `Unit::vertical` and `Unit::insertion` build arbitrary vertical material and reserved-height page insertions; insertion height is deducted from the usable page goal and the material is attached to `Page::insertions`. + Insertions taller than a page are split at item boundaries across pages. - Page breaking handles vertical boxes/glue, `Document::top_skip` and forced vertical penalties (`penalty <= -10000`); positive penalties are recorded but not yet part of page badness. `VerticalItem::Insertion` material is @@ -255,7 +256,7 @@ M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments. -M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, and can take a dedicated float page. +M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, and can take a dedicated float page. M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the painter uses 10pt glyphs in 12pt rows. Rules and spans are painted; tall tables split at row boundaries (`Halign::split_for_height`) with column widths preserved.