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), Box(VerticalBox),
Glue(i64), Glue(i64),
Penalty(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)] #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -84,14 +93,26 @@ impl Document {
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Page { pub struct Page {
pub items: Vec<VerticalItem>, pub items: Vec<VerticalItem>,
#[serde(default)]
pub insertions: Vec<Insertion>,
pub sources: Vec<(UnitId, u64)>, pub sources: Vec<(UnitId, u64)>,
pub hash: u64, pub hash: u64,
} }
impl Page { impl Page {
fn empty() -> Self {
Self {
items: Vec::new(),
insertions: Vec::new(),
sources: Vec::new(),
hash: 0,
}
}
fn seal_in_place(&mut self) { fn seal_in_place(&mut self) {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
self.items.hash(&mut hasher); self.items.hash(&mut hasher);
self.insertions.hash(&mut hasher);
self.sources.hash(&mut hasher); self.sources.hash(&mut hasher);
self.hash = hasher.finish(); self.hash = hasher.finish();
} }
@@ -113,6 +134,12 @@ pub struct Checkpoint {
pub used: i64, pub used: i64,
/// Page items already emitted on the current page. /// Page items already emitted on the current page.
pub page_items: Vec<VerticalItem>, 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)] #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -138,34 +165,27 @@ impl Engine {
pub fn layout(document: &Document) -> Self { pub fn layout(document: &Document) -> Self {
let mut pages = Vec::new(); let mut pages = Vec::new();
let mut checkpoints = Vec::with_capacity(document.units.len()); let mut checkpoints = Vec::with_capacity(document.units.len());
let mut current = Page { let mut current = Page::empty();
items: Vec::new(),
sources: Vec::new(),
hash: 0,
};
let mut used = document.top_skip; let mut used = document.top_skip;
let mut insertion_height = 0_i64;
for (unit_index, unit) in document.units.iter().enumerate() { for (unit_index, unit) in document.units.iter().enumerate() {
checkpoints.push(Checkpoint { checkpoints.push(Checkpoint {
unit_index, unit_index,
page_index: pages.len(), page_index: pages.len(),
used, used,
page_items: current.items.clone(), page_items: current.items.clone(),
page_insertions: current.insertions.clone(),
insertion_height,
}); });
for item in &unit.items { for item in &unit.items {
match item { match item {
VerticalItem::Box(vertical_box) => { 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() && !current.items.is_empty()
{ {
pages.push(std::mem::replace( pages.push(std::mem::replace(&mut current, Page::empty()));
&mut current,
Page {
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
used = document.top_skip; used = document.top_skip;
insertion_height = 0;
} }
used += vertical_box.height; used += vertical_box.height;
current.items.push(item.clone()); current.items.push(item.clone());
@@ -178,17 +198,21 @@ impl Engine {
} }
VerticalItem::Penalty(penalty) => { VerticalItem::Penalty(penalty) => {
if *penalty <= -10_000 && !current.items.is_empty() { if *penalty <= -10_000 && !current.items.is_empty() {
pages.push(std::mem::replace( pages.push(std::mem::replace(&mut current, Page::empty()));
&mut current, used = document.top_skip;
Page { insertion_height = 0;
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
used = 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, page_index: 0,
used: 0, used: 0,
page_items: Vec::new(), page_items: Vec::new(),
page_insertions: Vec::new(),
insertion_height: 0,
}); });
let edited_height: i64 = next.units[unit_index] let edited_height: i64 = next.units[unit_index]
.items .items
@@ -254,6 +280,7 @@ impl Engine {
VerticalItem::Box(vertical_box) => vertical_box.height, VerticalItem::Box(vertical_box) => vertical_box.height,
VerticalItem::Glue(height) => *height, VerticalItem::Glue(height) => *height,
VerticalItem::Penalty(_) => 0, VerticalItem::Penalty(_) => 0,
VerticalItem::Insertion(insertion) => insertion.height,
}) })
.sum(); .sum();
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() { 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 pages = old_pages[..checkpoint.page_index].to_vec();
let mut current = Page { let mut current = Page {
items: checkpoint.page_items, items: checkpoint.page_items,
insertions: checkpoint.page_insertions,
sources: Vec::new(), sources: Vec::new(),
hash: 0, hash: 0,
}; };
let mut used = checkpoint.used; let mut used = checkpoint.used;
let mut insertion_height = checkpoint.insertion_height;
let mut checkpoints = self.layout.checkpoints[..unit_index].to_vec(); let mut checkpoints = self.layout.checkpoints[..unit_index].to_vec();
for (index, unit) in next.units.iter().enumerate().skip(unit_index) { for (index, unit) in next.units.iter().enumerate().skip(unit_index) {
@@ -277,21 +306,18 @@ impl Engine {
page_index: pages.len(), page_index: pages.len(),
used, used,
page_items: current.items.clone(), page_items: current.items.clone(),
page_insertions: current.insertions.clone(),
insertion_height,
}); });
for item in &unit.items { for item in &unit.items {
match item { match item {
VerticalItem::Box(vertical_box) => { 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( pages.push(std::mem::replace(&mut current, Page::empty()));
&mut current,
Page {
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
used = next.top_skip; used = next.top_skip;
insertion_height = 0;
} }
used += vertical_box.height; used += vertical_box.height;
current.items.push(item.clone()); current.items.push(item.clone());
@@ -304,17 +330,21 @@ impl Engine {
} }
VerticalItem::Penalty(penalty) => { VerticalItem::Penalty(penalty) => {
if *penalty <= -10_000 && !current.items.is_empty() { if *penalty <= -10_000 && !current.items.is_empty() {
pages.push(std::mem::replace( pages.push(std::mem::replace(&mut current, Page::empty()));
&mut current, used = next.top_skip;
Page { insertion_height = 0;
items: Vec::new(),
sources: Vec::new(),
hash: 0,
},
));
used = 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); 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] #[test]
fn top_skip_consumes_space_on_each_page() { fn top_skip_consumes_space_on_each_page() {
let unit = |id| Unit { let unit = |id| Unit {
+4 -2
View File
@@ -71,7 +71,7 @@ next paragraph milestones.
| --- | --- | --- | | --- | --- | --- |
| M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 | | M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 |
| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, pdfTeX fixture and minimum line fill; hyphenation next | | M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, pdfTeX fixture and minimum line fill; hyphenation next |
| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties; `\output`/insertions next | | M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, page-bottom `Insertion` reserve; `\output`/float placements next |
| M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | table width/rules match reference | | M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | table width/rules match reference |
| M4 | math lists, scripts, fractions, radicals, accents, delimiters | arXiv formulas render without overlap | | M4 | math lists, scripts, fractions, radicals, accents, delimiters | arXiv formulas render without overlap |
| M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference | | M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference |
@@ -103,7 +103,9 @@ this needs a two-pass tolerance algorithm with an emergency overfull pass, not
a single cost function. a single cost function.
- Page breaking handles vertical boxes/glue, `Document::top_skip` and forced - Page breaking handles vertical boxes/glue, `Document::top_skip` and forced
vertical penalties (`penalty <= -10000`); positive penalties are recorded vertical penalties (`penalty <= -10000`); positive penalties are recorded
but not yet part of page badness. There is no `\output` routine yet. but not yet part of page badness. `VerticalItem::Insertion` material is
attached to `Page::insertions` and reserves height from the page goal.
There is no `\output` routine and no `\insert`/float wiring yet.
- No macro state, output routine, math, fonts or PDF output in the new core yet. - No macro state, output routine, math, fonts or PDF output in the new core yet.
- The v1 server and project API still use `src/rust_typeset` and are unchanged. - The v1 server and project API still use `src/rust_typeset` and are unchanged.