Place the first box baseline at topskip

This commit is contained in:
gloamxun committed 2026-10-20 12:38:10 +08:00
1 parent 3724ab1a00
commit 112a700c9b
1 file changed
+26 -15
+26 -15
View File
@@ -166,7 +166,7 @@ impl Engine {
let mut pages = Vec::new();
let mut checkpoints = Vec::with_capacity(document.units.len());
let mut current = Page::empty();
let mut used = document.top_skip;
let mut used = 0_i64;
let mut insertion_height = 0_i64;
for (unit_index, unit) in document.units.iter().enumerate() {
checkpoints.push(Checkpoint {
@@ -180,14 +180,16 @@ impl Engine {
for item in &unit.items {
match item {
VerticalItem::Box(vertical_box) => {
if used + vertical_box.height > document.page_goal - insertion_height
&& !current.items.is_empty()
if current.items.is_empty() {
used = document.top_skip.max(vertical_box.height);
} else if used + vertical_box.height > document.page_goal - insertion_height
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
used = document.top_skip.max(vertical_box.height);
insertion_height = 0;
} else {
used += vertical_box.height;
}
used += vertical_box.height;
current.items.push(item.clone());
}
VerticalItem::Glue(amount) => {
@@ -312,14 +314,15 @@ impl Engine {
for item in &unit.items {
match item {
VerticalItem::Box(vertical_box) => {
if used + vertical_box.height > next.page_goal - insertion_height
&& !current.items.is_empty()
{
if current.items.is_empty() {
used = next.top_skip.max(vertical_box.height);
} else if used + vertical_box.height > next.page_goal - insertion_height {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
used = next.top_skip.max(vertical_box.height);
insertion_height = 0;
} else {
used += vertical_box.height;
}
used += vertical_box.height;
current.items.push(item.clone());
}
VerticalItem::Glue(amount) => {
@@ -450,25 +453,33 @@ mod tests {
}
#[test]
fn top_skip_consumes_space_on_each_page() {
fn top_skip_sets_the_first_baseline_distance() {
let unit = |id| Unit {
id,
source: "boxes".to_owned(),
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
height: 50,
height: 2,
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 50,
height: 30,
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 30,
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 30,
}),
],
};
let plain = Engine::layout(&Document::new(100, vec![unit(0)]));
let plain = Engine::layout(&Document::new(95, 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));
let with_top_skip = Engine::layout(&Document::new(95, vec![unit(1)]).with_top_skip(10));
assert_eq!(with_top_skip.layout.pages.len(), 2);
}