Reserve top_skip at the start of each page

This commit is contained in:
gloamxun committed 2026-10-20 12:35:11 +08:00
1 parent 252963f0fa
commit 6af137b17e
2 files changed
+43 -8

No files matched your search

+39 -4
View File
@@ -61,11 +61,23 @@ impl Unit {
pub struct Document {
pub units: Vec<Unit>,
pub page_goal: i64,
/// TeX-like `\topskip`: distance from the page top to the first baseline.
#[serde(default)]
pub top_skip: i64,
}
impl Document {
pub fn new(page_goal: i64, units: Vec<Unit>) -> Self {
Self { units, page_goal }
Self {
units,
page_goal,
top_skip: 0,
}
}
pub fn with_top_skip(mut self, top_skip: i64) -> Self {
self.top_skip = top_skip;
self
}
}
@@ -131,7 +143,7 @@ impl Engine {
sources: Vec::new(),
hash: 0,
};
let mut used = 0_i64;
let mut used = document.top_skip;
for (unit_index, unit) in document.units.iter().enumerate() {
checkpoints.push(Checkpoint {
unit_index,
@@ -153,7 +165,7 @@ impl Engine {
hash: 0,
},
));
used = 0;
used = document.top_skip;
}
used += vertical_box.height;
current.items.push(item.clone());
@@ -279,7 +291,7 @@ impl Engine {
hash: 0,
},
));
used = 0;
used = next.top_skip;
}
used += vertical_box.height;
current.items.push(item.clone());
@@ -376,6 +388,29 @@ mod tests {
assert!(engine.recomputed_pages() >= 1);
}
#[test]
fn top_skip_consumes_space_on_each_page() {
let unit = |id| Unit {
id,
source: "boxes".to_owned(),
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
height: 50,
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 50,
}),
],
};
let plain = Engine::layout(&Document::new(100, vec![unit(0)]));
assert_eq!(plain.layout.pages.len(), 1);
let with_top_skip = Engine::layout(&Document::new(100, vec![unit(1)]).with_top_skip(10));
assert_eq!(with_top_skip.layout.pages.len(), 2);
}
#[test]
fn vertical_penalty_can_force_a_page_break() {
let unit = Unit {