From bcd3b6fa661a481f253273d0805982d2226dbe9d Mon Sep 17 00:00:00 2001 From: GloamXun Date: Fri, 23 Oct 2026 02:40:36 +0800 Subject: [PATCH] Expand vertical units in the paged builder --- crates/texrra-tex-core/src/lib.rs | 148 ++++++++++++++++++++++-------- docs/ROUTE_D_PLAN.md | 2 +- docs/TEX_CORE.md | 2 +- 3 files changed, 110 insertions(+), 42 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index fc41bdc..7e7c6d8 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -766,43 +766,50 @@ fn source_hash(source: &str) -> u64 { /// Convert a document into the vertical list consumed by [`PageBuilder`]. /// -/// Only single-item units can be expressed as page-builder nodes today, so -/// floats and insertions make this return `None` and keep the incremental -/// builder in charge. -fn document_nodes(document: &Document) -> Option> { - let mut nodes = Vec::with_capacity(document.units.len()); - for unit in &document.units { - let [item] = unit.items.as_slice() else { - return None; - }; - nodes.push(match item { - VerticalItem::Box(vertical_box) => VerticalNode::Box { - height: vertical_box.height, - depth: 0, - }, - VerticalItem::Rule { height, .. } => VerticalNode::Box { - height: *height, - depth: 0, - }, - VerticalItem::Math { height, depth, .. } => VerticalNode::Box { - height: *height, - depth: *depth, - }, - VerticalItem::Paragraph(paragraph) => VerticalNode::Box { - height: paragraph.height, - depth: 0, - }, - VerticalItem::Halign(halign) => VerticalNode::Box { - height: halign.height, - depth: 0, - }, - VerticalItem::Glue(height) => VerticalNode::Glue(VerticalGlue::new(*height, 0, 0)), - VerticalItem::Penalty(value) => VerticalNode::Penalty(*value), - VerticalItem::Mark(mark) => VerticalNode::Mark(mark.clone()), - VerticalItem::Insertion(_) | VerticalItem::Float(_) => return None, - }); +/// Units made of box/glue/penalty/mark material can be expressed as +/// page-builder nodes today (a unit may contribute several nodes and be +/// split across pages); floats and insertions return `None`, leaving the +/// scrolling builder in charge. +fn document_nodes( + document: &Document, +) -> Option<(Vec, Vec, Vec)> { + let mut nodes = Vec::new(); + let mut owners = Vec::new(); + let mut items = Vec::new(); + for (index, unit) in document.units.iter().enumerate() { + for item in &unit.items { + let node = match item { + VerticalItem::Box(vertical_box) => VerticalNode::Box { + height: vertical_box.height, + depth: 0, + }, + VerticalItem::Rule { height, .. } => VerticalNode::Box { + height: *height, + depth: 0, + }, + VerticalItem::Math { height, depth, .. } => VerticalNode::Box { + height: *height, + depth: *depth, + }, + VerticalItem::Paragraph(paragraph) => VerticalNode::Box { + height: paragraph.height, + depth: 0, + }, + VerticalItem::Halign(halign) => VerticalNode::Box { + height: halign.height, + depth: 0, + }, + VerticalItem::Glue(height) => VerticalNode::Glue(VerticalGlue::new(*height, 0, 0)), + VerticalItem::Penalty(value) => VerticalNode::Penalty(*value), + VerticalItem::Mark(mark) => VerticalNode::Mark(mark.clone()), + VerticalItem::Insertion(_) | VerticalItem::Float(_) => return None, + }; + nodes.push(node); + owners.push(index); + items.push(item.clone()); + } } - Some(nodes) + Some((nodes, owners, items)) } fn item_height(item: &VerticalItem) -> i64 { @@ -1560,7 +1567,7 @@ impl Engine { /// because their page interaction belongs to the incremental builder for /// now. pub fn layout_paged(document: &Document) -> Option { - let nodes = document_nodes(document)?; + let (nodes, owners, node_items) = document_nodes(document)?; let builder = PageBuilder { page_goal: document.page_goal, top_skip: document.top_skip, @@ -1571,14 +1578,17 @@ impl Engine { let mut items = Vec::new(); let mut marks = Vec::new(); let mut sources = Vec::new(); - for unit in &document.units[fragment.start..fragment.end] { - let item = unit.items.first()?.clone(); + for index in fragment.start..fragment.end { + let item = node_items[index].clone(); if let VerticalItem::Mark(mark) = &item { marks.push(mark.clone()); } else { items.push(item); } - sources.push((unit.id, source_hash(&unit.source))); + let unit = &document.units[owners[index]]; + if sources.last().map(|(id, _)| *id) != Some(unit.id) { + sources.push((unit.id, source_hash(&unit.source))); + } } let mut page = Page::empty(); page.items = items; @@ -4705,6 +4715,64 @@ mod tests { assert!(engine.pages().iter().all(|page| page.hash != 0)); } + #[test] + fn paged_layout_expands_vertical_units() { + let document = Document::new( + 100, + vec![Unit::vertical( + 1, + vec![ + VerticalItem::Rule { + width: 100, + height: 60, + }, + VerticalItem::Glue(10), + VerticalItem::Rule { + width: 100, + height: 60, + }, + ], + )], + ); + let engine = + Engine::layout_paged(&document).expect("vertical materials are page-buildable"); + assert_eq!(engine.pages().len(), 2); + assert_eq!(engine.pages()[0].sources, vec![(1, source_hash(""))]); + } + + #[test] + fn paged_layout_can_split_one_unit_across_pages() { + let document = Document::new( + 70, + vec![Unit::vertical( + 1, + vec![ + VerticalItem::Rule { + width: 100, + height: 60, + }, + VerticalItem::Penalty(0), + VerticalItem::Rule { + width: 100, + height: 60, + }, + ], + )], + ); + let engine = + Engine::layout_paged(&document).expect("vertical materials are page-buildable"); + assert_eq!(engine.pages().len(), 2); + let rules = |page: &Page| { + page.items + .iter() + .filter(|item| matches!(item, VerticalItem::Rule { .. })) + .count() + }; + assert_eq!(rules(&engine.pages()[0]), 1); + assert_eq!(rules(&engine.pages()[1]), 1); + assert_eq!(engine.pages()[1].sources, vec![(1, source_hash(""))]); + } + #[test] fn insertion_count_scales_reserved_page_space() { let insertions = |document: &mut Document| { diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index 3425fc1..0c7bc40 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -66,7 +66,7 @@ yet generic `fil`/`fill` glue policies, now with line-level paragraph splits; `page::PageBuilder` implements the candidate-breakpoint cost model (badness + penalty, glue setting, `\topskip`, `\maxdepth`) and `Engine::layout_paged` runs it for - documents made of single-item units, but floats, insertions and incremental + documents made of box/glue/penalty/mark units, but floats, insertions and incremental checkpoint replay still use the scrolling builder, and an interpreted `\output` is absent; 3. `\insert` classes model `\count`/`\dimen`/`\skip` but are not TeX diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 5466c14..fb521b6 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -290,7 +290,7 @@ for a later overlaid slash. | --- | --- | --- | | M0 checkpoint engine | done | page list, page hashes, snapshot/restore, edit replay | | M1 horizontal/paragraphs | partial | boxes/glue/penalties, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing with default and custom `\sfcode`/`\frenchspacing`, explicit `\spaceskip`/`\xspaceskip`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfTeX oracle matrix 100-240pt plus register, geometry and vertical-glue fixtures | -| M2 vertical/page builder | partial | vsize, topskip, insertions, marks, Rust output hook, top/bottom floats, float-page/text fractions, rules, vskip, penalties; `page::PageBuilder` scores breakpoints with page badness and penalties and models `\topskip`/`\maxdepth`; `Engine::layout_paged` builds pages through it for single-item documents, while floats, insertions and incremental replay still use the scrolling path; interpreted `\output` absent | +| M2 vertical/page builder | partial | vsize, topskip, insertions, marks, Rust output hook, top/bottom floats, float-page/text fractions, rules, vskip, penalties; `page::PageBuilder` scores breakpoints with page badness and penalties and models `\topskip`/`\maxdepth`; `Engine::layout_paged` builds pages through it for documents made of box/glue/penalty/mark vertical material (a unit may span pages), while floats, insertions and incremental replay still use the scrolling path; interpreted `\output` absent | | M3 halign | partial | natural columns, spans, omit, hline rows, templates, noalign skip, rules; no PDF painter | | M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping | | M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and width arrays; used faces embed per-document TrueType glyf subsets that keep the original glyph IDs, include composite components, and carry a matching ToUnicode CMap; ASCII documents use the compact tier; no Base-14 dependency; SVG carries family/weight/style |