diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index fd2ba02..cd1e5b0 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -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, } #[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, + #[serde(default)] + pub insertions: Vec, 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, + /// Insertions already attached to the current page. + #[serde(default)] + pub page_insertions: Vec, + /// 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 { diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 3a97736..13bb010 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -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 | | 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 | | 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 | @@ -103,7 +103,9 @@ this needs a two-pass tolerance algorithm with an emergency overfull pass, not a single cost function. - Page breaking handles vertical boxes/glue, `Document::top_skip` and forced 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. - The v1 server and project API still use `src/rust_typeset` and are unchanged.