Add vertical rules to the page builder

This commit is contained in:
gloamxun committed 2026-10-20 13:14:34 +08:00
1 parent a00a2d5148
commit 792621d39b
2 files changed
+50 -1

No files matched your search

+47
View File
@@ -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<String>) -> 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(&current)
{
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(&current) == 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(&current)
{
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(&current) == 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);