From 792621d39b77ac094d942db908deb6d38765451f Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:14:34 +0800 Subject: [PATCH] Add vertical rules to the page builder --- crates/texrra-tex-core/src/lib.rs | 47 +++++++++++++++++++++++++++++++ docs/TEX_CORE.md | 4 ++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index b842b35..8c742e8 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -52,6 +52,7 @@ pub struct Halign { #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum VerticalItem { Box(VerticalBox), + Rule { width: i64, height: i64 }, Glue(i64), Penalty(i64), Insertion(Insertion), @@ -291,6 +292,11 @@ impl Unit { Self::halign(id, rows, column_count, intercolumn) } + /// A filled rule in the vertical list. + pub fn rule(id: UnitId, width: i64, height: i64) -> Self { + Self::vertical(id, vec![VerticalItem::Rule { width, height }]) + } + /// M1 paragraph builder: box/glue/penalty item stream with a /// Knuth-Plass-style word-level line breaker. pub fn paragraph(id: UnitId, source: impl Into) -> Self { @@ -520,6 +526,20 @@ impl Engine { }); for item in &unit.items { match item { + VerticalItem::Rule { height, .. } => { + if current.items.is_empty() { + used = document.top_skip.max(*height); + } else if used + *height + > document.page_goal - insertion_height - bottom_float_height(¤t) + { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip.max(*height); + insertion_height = 0; + } else { + used += *height; + } + current.items.push(item.clone()); + } VerticalItem::Box(vertical_box) => { if current.items.is_empty() && top_float_height(¤t) == 0 { used = document.top_skip.max(vertical_box.height); @@ -681,6 +701,7 @@ impl Engine { .iter() .map(|item| match item { VerticalItem::Box(vertical_box) => vertical_box.height, + VerticalItem::Rule { height, .. } => *height, VerticalItem::Glue(height) => *height, VerticalItem::Penalty(_) => 0, VerticalItem::Insertion(insertion) => insertion.height, @@ -721,6 +742,20 @@ impl Engine { }); for item in &unit.items { match item { + VerticalItem::Rule { height, .. } => { + if current.items.is_empty() { + used = next.top_skip.max(*height); + } else if used + *height + > next.page_goal - insertion_height - bottom_float_height(¤t) + { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = next.top_skip.max(*height); + insertion_height = 0; + } else { + used += *height; + } + current.items.push(item.clone()); + } VerticalItem::Box(vertical_box) => { if current.items.is_empty() && top_float_height(¤t) == 0 { used = next.top_skip.max(vertical_box.height); @@ -1174,6 +1209,18 @@ mod tests { assert_eq!(halign.cell_box(2, 0), Some((30, 110))); } + #[test] + fn vertical_rules_consume_page_height() { + let unit = Unit::rule(1, 100, 60); + let document = Document::new(100, vec![unit]); + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 1); + + let unit = Unit::rule(2, 100, 60); + let document = Document::new(100, vec![unit.clone(), unit]); + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 2); + } #[test] fn halign_template_centers_and_right_aligns_cells() { let unit = Unit::halign_from_source_with_template(1, "lcr", r"A & B & C", 10); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index a7ef6fe..8b275fb 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -192,4 +192,6 @@ empty paragraphs. `Unit::halign_from_source` parses `&` rows, `\\` separators, `\omit`, `\span`, `\multispan{n}` and `\hline` rule rows. `Unit::halign_from_source_with_template` additionally accepts an `l`/`c`/`r` -column template. \ No newline at end of file +column template. +`VerticalItem::Rule { width, height }` and `Unit::rule(id, width, height)` +add filled rules to the vertical list; rules consume page height like boxes.