Collect running marks per page

This commit is contained in:
gloamxun committed 2026-10-20 12:39:28 +08:00
1 parent 112a700c9b
commit bef442684e
2 files changed
+65 -2

No files matched your search

+61
View File
@@ -24,6 +24,7 @@ pub enum VerticalItem {
Glue(i64),
Penalty(i64),
Insertion(Insertion),
Mark(Mark),
}
/// Vertical material reserved at the bottom of a page (footnotes etc.).
@@ -34,6 +35,13 @@ pub struct Insertion {
pub items: Vec<VerticalItem>,
}
/// Running mark material for headers and footers.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Mark {
pub class: u8,
pub text: String,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Unit {
pub id: UnitId,
@@ -95,6 +103,8 @@ pub struct Page {
pub items: Vec<VerticalItem>,
#[serde(default)]
pub insertions: Vec<Insertion>,
#[serde(default)]
pub marks: Vec<Mark>,
pub sources: Vec<(UnitId, u64)>,
pub hash: u64,
}
@@ -104,6 +114,7 @@ impl Page {
Self {
items: Vec::new(),
insertions: Vec::new(),
marks: Vec::new(),
sources: Vec::new(),
hash: 0,
}
@@ -113,6 +124,7 @@ impl Page {
let mut hasher = DefaultHasher::new();
self.items.hash(&mut hasher);
self.insertions.hash(&mut hasher);
self.marks.hash(&mut hasher);
self.sources.hash(&mut hasher);
self.hash = hasher.finish();
}
@@ -140,6 +152,9 @@ pub struct Checkpoint {
/// Total insertion height already reserved on the current page.
#[serde(default)]
pub insertion_height: i64,
/// Running marks already collected on the current page.
#[serde(default)]
pub page_marks: Vec<Mark>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -176,6 +191,7 @@ impl Engine {
page_items: current.items.clone(),
page_insertions: current.insertions.clone(),
insertion_height,
page_marks: current.marks.clone(),
});
for item in &unit.items {
match item {
@@ -215,6 +231,9 @@ impl Engine {
insertion_height += insertion.height;
current.insertions.push(insertion.clone());
}
VerticalItem::Mark(mark) => {
current.marks.push(mark.clone());
}
}
}
}
@@ -274,6 +293,7 @@ impl Engine {
page_items: Vec::new(),
page_insertions: Vec::new(),
insertion_height: 0,
page_marks: Vec::new(),
});
let edited_height: i64 = next.units[unit_index]
.items
@@ -283,6 +303,7 @@ impl Engine {
VerticalItem::Glue(height) => *height,
VerticalItem::Penalty(_) => 0,
VerticalItem::Insertion(insertion) => insertion.height,
VerticalItem::Mark(_) => 0,
})
.sum();
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() {
@@ -295,6 +316,7 @@ impl Engine {
let mut current = Page {
items: checkpoint.page_items,
insertions: checkpoint.page_insertions,
marks: checkpoint.page_marks,
sources: Vec::new(),
hash: 0,
};
@@ -310,6 +332,7 @@ impl Engine {
page_items: current.items.clone(),
page_insertions: current.insertions.clone(),
insertion_height,
page_marks: current.marks.clone(),
});
for item in &unit.items {
match item {
@@ -348,6 +371,9 @@ impl Engine {
insertion_height += insertion.height;
current.insertions.push(insertion.clone());
}
VerticalItem::Mark(mark) => {
current.marks.push(mark.clone());
}
}
}
}
@@ -506,6 +532,41 @@ mod tests {
assert_eq!(engine.layout.pages[1].items.len(), 1);
}
#[test]
fn marks_are_attached_to_pages_and_affect_the_hash() {
let document = |text: &str| {
Document::new(
100,
vec![Unit {
id: 0,
source: text.to_owned(),
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
}),
VerticalItem::Mark(Mark {
class: 0,
text: text.to_owned(),
}),
VerticalItem::Penalty(-10_000),
VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
}),
],
}],
)
};
let first = Engine::layout(&document("even page"));
let second = Engine::layout(&document("odd page"));
assert_eq!(first.layout.pages[0].marks[0].text, "even page");
assert_ne!(
first.layout.pages[0].hash, second.layout.pages[0].hash,
"running marks must participate in the page hash"
);
}
#[test]
fn checkpoint_round_trips_through_serde() {
let engine = Engine::layout(&document());