Honour forced vertical penalties in the M0 page builder

This commit is contained in:
gloamxun committed 2026-10-20 12:34:07 +08:00
1 parent 19ecb7fdc6
commit 252963f0fa
2 files changed
+54 -5

No files matched your search

+51 -3
View File
@@ -164,7 +164,19 @@ impl Engine {
current.items.push(item.clone());
}
}
VerticalItem::Penalty(_) => {}
VerticalItem::Penalty(penalty) => {
if *penalty <= -10_000 && !current.items.is_empty() {
pages.push(std::mem::replace(
&mut current,
Page {
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
used = 0;
}
}
}
}
}
@@ -228,7 +240,8 @@ impl Engine {
.iter()
.map(|item| match item {
VerticalItem::Box(vertical_box) => vertical_box.height,
VerticalItem::Glue(height) | VerticalItem::Penalty(height) => *height,
VerticalItem::Glue(height) => *height,
VerticalItem::Penalty(_) => 0,
})
.sum();
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() {
@@ -277,7 +290,19 @@ impl Engine {
current.items.push(item.clone());
}
}
VerticalItem::Penalty(_) => {}
VerticalItem::Penalty(penalty) => {
if *penalty <= -10_000 && !current.items.is_empty() {
pages.push(std::mem::replace(
&mut current,
Page {
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
used = 0;
}
}
}
}
}
@@ -351,6 +376,29 @@ mod tests {
assert!(engine.recomputed_pages() >= 1);
}
#[test]
fn vertical_penalty_can_force_a_page_break() {
let unit = Unit {
id: 0,
source: "penalty".to_owned(),
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
}),
VerticalItem::Penalty(-10_000),
VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
}),
],
};
let engine = Engine::layout(&Document::new(100, vec![unit]));
assert_eq!(engine.layout.pages.len(), 2);
assert_eq!(engine.layout.pages[0].items.len(), 1);
assert_eq!(engine.layout.pages[1].items.len(), 1);
}
#[test]
fn checkpoint_round_trips_through_serde() {
let engine = Engine::layout(&document());