Expand vertical units in the paged builder

This commit is contained in:
gloamxun committed 2026-10-23 02:40:36 +08:00
1 parent 4be0ff5787
commit bcd3b6fa66
3 files changed
+110 -42

No files matched your search

+108 -40
View File
@@ -766,43 +766,50 @@ fn source_hash(source: &str) -> u64 {
/// Convert a document into the vertical list consumed by [`PageBuilder`]. /// Convert a document into the vertical list consumed by [`PageBuilder`].
/// ///
/// Only single-item units can be expressed as page-builder nodes today, so /// Units made of box/glue/penalty/mark material can be expressed as
/// floats and insertions make this return `None` and keep the incremental /// page-builder nodes today (a unit may contribute several nodes and be
/// builder in charge. /// split across pages); floats and insertions return `None`, leaving the
fn document_nodes(document: &Document) -> Option<Vec<VerticalNode>> { /// scrolling builder in charge.
let mut nodes = Vec::with_capacity(document.units.len()); fn document_nodes(
for unit in &document.units { document: &Document,
let [item] = unit.items.as_slice() else { ) -> Option<(Vec<VerticalNode>, Vec<usize>, Vec<VerticalItem>)> {
return None; let mut nodes = Vec::new();
}; let mut owners = Vec::new();
nodes.push(match item { let mut items = Vec::new();
VerticalItem::Box(vertical_box) => VerticalNode::Box { for (index, unit) in document.units.iter().enumerate() {
height: vertical_box.height, for item in &unit.items {
depth: 0, let node = match item {
}, VerticalItem::Box(vertical_box) => VerticalNode::Box {
VerticalItem::Rule { height, .. } => VerticalNode::Box { height: vertical_box.height,
height: *height, depth: 0,
depth: 0, },
}, VerticalItem::Rule { height, .. } => VerticalNode::Box {
VerticalItem::Math { height, depth, .. } => VerticalNode::Box { height: *height,
height: *height, depth: 0,
depth: *depth, },
}, VerticalItem::Math { height, depth, .. } => VerticalNode::Box {
VerticalItem::Paragraph(paragraph) => VerticalNode::Box { height: *height,
height: paragraph.height, depth: *depth,
depth: 0, },
}, VerticalItem::Paragraph(paragraph) => VerticalNode::Box {
VerticalItem::Halign(halign) => VerticalNode::Box { height: paragraph.height,
height: halign.height, depth: 0,
depth: 0, },
}, VerticalItem::Halign(halign) => VerticalNode::Box {
VerticalItem::Glue(height) => VerticalNode::Glue(VerticalGlue::new(*height, 0, 0)), height: halign.height,
VerticalItem::Penalty(value) => VerticalNode::Penalty(*value), depth: 0,
VerticalItem::Mark(mark) => VerticalNode::Mark(mark.clone()), },
VerticalItem::Insertion(_) | VerticalItem::Float(_) => return None, 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 { fn item_height(item: &VerticalItem) -> i64 {
@@ -1560,7 +1567,7 @@ impl Engine {
/// because their page interaction belongs to the incremental builder for /// because their page interaction belongs to the incremental builder for
/// now. /// now.
pub fn layout_paged(document: &Document) -> Option<Self> { pub fn layout_paged(document: &Document) -> Option<Self> {
let nodes = document_nodes(document)?; let (nodes, owners, node_items) = document_nodes(document)?;
let builder = PageBuilder { let builder = PageBuilder {
page_goal: document.page_goal, page_goal: document.page_goal,
top_skip: document.top_skip, top_skip: document.top_skip,
@@ -1571,14 +1578,17 @@ impl Engine {
let mut items = Vec::new(); let mut items = Vec::new();
let mut marks = Vec::new(); let mut marks = Vec::new();
let mut sources = Vec::new(); let mut sources = Vec::new();
for unit in &document.units[fragment.start..fragment.end] { for index in fragment.start..fragment.end {
let item = unit.items.first()?.clone(); let item = node_items[index].clone();
if let VerticalItem::Mark(mark) = &item { if let VerticalItem::Mark(mark) = &item {
marks.push(mark.clone()); marks.push(mark.clone());
} else { } else {
items.push(item); 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(); let mut page = Page::empty();
page.items = items; page.items = items;
@@ -4705,6 +4715,64 @@ mod tests {
assert!(engine.pages().iter().all(|page| page.hash != 0)); 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] #[test]
fn insertion_count_scales_reserved_page_space() { fn insertion_count_scales_reserved_page_space() {
let insertions = |document: &mut Document| { let insertions = |document: &mut Document| {
+1 -1
View File
@@ -66,7 +66,7 @@ yet generic `fil`/`fill` glue
policies, now with line-level paragraph splits; `page::PageBuilder` policies, now with line-level paragraph splits; `page::PageBuilder`
implements the candidate-breakpoint cost model (badness + penalty, glue implements the candidate-breakpoint cost model (badness + penalty, glue
setting, `\topskip`, `\maxdepth`) and `Engine::layout_paged` runs it for 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 checkpoint replay still use the scrolling builder, and an interpreted
`\output` is absent; `\output` is absent;
3. `\insert` classes model `\count`/`\dimen`/`\skip` but are not TeX 3. `\insert` classes model `\count`/`\dimen`/`\skip` but are not TeX
+1 -1
View File
@@ -290,7 +290,7 @@ for a later overlaid slash.
| --- | --- | --- | | --- | --- | --- |
| M0 checkpoint engine | done | page list, page hashes, snapshot/restore, edit replay | | 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 | | 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 | | 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 | | 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 | | 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 |