From 112a700c9be2ef8303aa5a3e3e72d473f094c821 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 12:38:10 +0800 Subject: [PATCH] Place the first box baseline at topskip --- crates/texrra-tex-core/src/lib.rs | 41 ++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index cd1e5b0..3e5bd5e 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -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); }