Route route-D page breaking through the paged builder

This commit is contained in:
gloamxun committed 2026-10-23 15:02:52 +08:00
1 parent 08a312be82
commit 6b3145fd09
6 files changed
+113 -26

No files matched your search

+78 -3
View File
@@ -867,8 +867,11 @@ fn source_hash(source: &str) -> u64 {
/// 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); insertion classes become [`VerticalNode::Insertion`]
/// reservations. Floats return `None`, leaving the scrolling builder in
/// charge.
/// reservations and floats become top, bottom or in-text nodes. A paragraph
/// expands to one node per line with a zero-width interline glue between
/// lines, which is the legal breakpoint that lets a page break inside it.
/// `None` means the document holds material the paged builder cannot express
/// yet.
fn document_nodes(
document: &Document,
) -> Option<(Vec<VerticalNode>, Vec<usize>, Vec<VerticalItem>)> {
@@ -905,6 +908,41 @@ fn document_nodes(
document.par_skip_shrink,
)));
}
if let VerticalItem::Paragraph(paragraph) = item {
if paragraph.lines.len() > 1 {
// TeX breaks pages between lines. Expand a paragraph into
// one node per line so the page builder is free to break
// inside it; each slice renders as its own single-line
// paragraph and keeps the baselines of its own line.
let mut remaining = paragraph.clone();
loop {
let tail = if remaining.lines.len() > 1 {
Some(remaining.split_at(1))
} else {
None
};
nodes.push(VerticalNode::Box {
height: remaining.height,
depth: 0,
});
owners.push(index);
items.push(VerticalItem::Paragraph(remaining.clone()));
match tail {
Some(tail) => {
// TeX puts interline glue between lines; a
// zero-width glue node is the legal breakpoint
// the page builder needs to split here.
nodes.push(VerticalNode::Glue(VerticalGlue::new(0, 0, 0)));
owners.push(index);
items.push(VerticalItem::Glue(0));
remaining = tail;
}
None => break,
}
}
continue;
}
}
let node = match item {
VerticalItem::Box(vertical_box) => VerticalNode::Box {
height: vertical_box.height,
@@ -2427,7 +2465,9 @@ impl Engine {
/// long as the prefix is unchanged.
pub fn relayout_from(&mut self, document: &Document, start_index: usize) {
if self.layout.checkpoints.is_empty() {
self.replace_with_paged(document);
if !self.relayout_paged_incremental(document, start_index) {
self.replace_with_paged(document);
}
return;
}
if start_index == 0 || start_index >= self.layout.checkpoints.len() {
@@ -5692,6 +5732,41 @@ mod tests {
assert!(engine.pages().iter().all(|page| page.hash != 0));
}
#[test]
fn paged_layout_breaks_inside_a_paragraph() {
let text = "word ".repeat(60);
let unit = Unit::paragraph_with_style_and_face(
1,
text,
ParagraphStyle::default(),
ParagraphFont::Roman,
);
let VerticalItem::Paragraph(paragraph) = &unit.items[0] else {
panic!("paragraph unit")
};
let line_count = paragraph.lines.len();
assert!(line_count >= 4, "fixture should wrap: {line_count}");
let mut document = Document::new(paragraph.height / 2, vec![unit]);
document.top_skip = 0;
let engine = Engine::layout_paged(&document).expect("paragraphs are page-buildable");
assert!(engine.pages().len() >= 2, "half a paragraph per page");
let lines: usize = engine
.pages()
.iter()
.flat_map(|page| page.items.iter())
.map(|item| match item {
VerticalItem::Paragraph(slice) => slice.lines.len(),
_ => 0,
})
.sum();
assert_eq!(lines, line_count, "line slices keep every line");
assert!(engine.pages().iter().all(|page| {
page.items
.iter()
.any(|item| matches!(item, VerticalItem::Paragraph(_)))
}));
}
#[test]
fn fill_glue_absorbs_the_page_set() {
let mut document = Document::new(