Reserve insertion material at the page bottom

This commit is contained in:
gloamxun committed 2026-10-20 12:37:00 +08:00
1 parent 6af137b17e
commit 3724ab1a00
2 files changed
+106 -43

No files matched your search

+102 -41
View File
@@ -23,6 +23,15 @@ pub enum VerticalItem {
Box(VerticalBox),
Glue(i64),
Penalty(i64),
Insertion(Insertion),
}
/// Vertical material reserved at the bottom of a page (footnotes etc.).
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Insertion {
pub class: u8,
pub height: i64,
pub items: Vec<VerticalItem>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -84,14 +93,26 @@ impl Document {
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Page {
pub items: Vec<VerticalItem>,
#[serde(default)]
pub insertions: Vec<Insertion>,
pub sources: Vec<(UnitId, u64)>,
pub hash: u64,
}
impl Page {
fn empty() -> Self {
Self {
items: Vec::new(),
insertions: Vec::new(),
sources: Vec::new(),
hash: 0,
}
}
fn seal_in_place(&mut self) {
let mut hasher = DefaultHasher::new();
self.items.hash(&mut hasher);
self.insertions.hash(&mut hasher);
self.sources.hash(&mut hasher);
self.hash = hasher.finish();
}
@@ -113,6 +134,12 @@ pub struct Checkpoint {
pub used: i64,
/// Page items already emitted on the current page.
pub page_items: Vec<VerticalItem>,
/// Insertions already attached to the current page.
#[serde(default)]
pub page_insertions: Vec<Insertion>,
/// Total insertion height already reserved on the current page.
#[serde(default)]
pub insertion_height: i64,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -138,34 +165,27 @@ impl Engine {
pub fn layout(document: &Document) -> Self {
let mut pages = Vec::new();
let mut checkpoints = Vec::with_capacity(document.units.len());
let mut current = Page {
items: Vec::new(),
sources: Vec::new(),
hash: 0,
};
let mut current = Page::empty();
let mut used = document.top_skip;
let mut insertion_height = 0_i64;
for (unit_index, unit) in document.units.iter().enumerate() {
checkpoints.push(Checkpoint {
unit_index,
page_index: pages.len(),
used,
page_items: current.items.clone(),
page_insertions: current.insertions.clone(),
insertion_height,
});
for item in &unit.items {
match item {
VerticalItem::Box(vertical_box) => {
if used + vertical_box.height > document.page_goal
if used + vertical_box.height > document.page_goal - insertion_height
&& !current.items.is_empty()
{
pages.push(std::mem::replace(
&mut current,
Page {
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
used += vertical_box.height;
current.items.push(item.clone());
@@ -178,17 +198,21 @@ impl Engine {
}
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;
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
}
VerticalItem::Insertion(insertion) => {
let goal = document.page_goal - insertion_height;
if !current.items.is_empty() && used + insertion.height > goal {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
insertion_height += insertion.height;
current.insertions.push(insertion.clone());
}
}
}
}
@@ -246,6 +270,8 @@ impl Engine {
page_index: 0,
used: 0,
page_items: Vec::new(),
page_insertions: Vec::new(),
insertion_height: 0,
});
let edited_height: i64 = next.units[unit_index]
.items
@@ -254,6 +280,7 @@ impl Engine {
VerticalItem::Box(vertical_box) => vertical_box.height,
VerticalItem::Glue(height) => *height,
VerticalItem::Penalty(_) => 0,
VerticalItem::Insertion(insertion) => insertion.height,
})
.sum();
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() {
@@ -265,10 +292,12 @@ impl Engine {
let mut pages = old_pages[..checkpoint.page_index].to_vec();
let mut current = Page {
items: checkpoint.page_items,
insertions: checkpoint.page_insertions,
sources: Vec::new(),
hash: 0,
};
let mut used = checkpoint.used;
let mut insertion_height = checkpoint.insertion_height;
let mut checkpoints = self.layout.checkpoints[..unit_index].to_vec();
for (index, unit) in next.units.iter().enumerate().skip(unit_index) {
@@ -277,21 +306,18 @@ impl Engine {
page_index: pages.len(),
used,
page_items: current.items.clone(),
page_insertions: current.insertions.clone(),
insertion_height,
});
for item in &unit.items {
match item {
VerticalItem::Box(vertical_box) => {
if used + vertical_box.height > next.page_goal && !current.items.is_empty()
if used + vertical_box.height > next.page_goal - insertion_height
&& !current.items.is_empty()
{
pages.push(std::mem::replace(
&mut current,
Page {
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
used += vertical_box.height;
current.items.push(item.clone());
@@ -304,17 +330,21 @@ impl Engine {
}
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;
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
}
VerticalItem::Insertion(insertion) => {
let goal = next.page_goal - insertion_height;
if !current.items.is_empty() && used + insertion.height > goal {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
insertion_height += insertion.height;
current.insertions.push(insertion.clone());
}
}
}
}
@@ -388,6 +418,37 @@ mod tests {
assert!(engine.recomputed_pages() >= 1);
}
#[test]
fn insertions_reserve_space_at_the_page_bottom() {
let unit = Unit {
id: 7,
source: "footnote layout".to_owned(),
items: vec![
VerticalItem::Insertion(Insertion {
class: 0,
height: 30,
items: vec![VerticalItem::Box(VerticalBox {
width: 20,
height: 30,
})],
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 40,
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 40,
}),
],
};
let result = Engine::layout(&Document::new(100, vec![unit]));
assert_eq!(result.layout.pages.len(), 2);
assert_eq!(result.layout.pages[0].insertions.len(), 1);
assert_eq!(result.layout.pages[0].items.len(), 1);
assert_eq!(result.layout.pages[1].insertions.len(), 0);
}
#[test]
fn top_skip_consumes_space_on_each_page() {
let unit = |id| Unit {