Lay out natural-width halign tables

This commit is contained in:
gloamxun committed 2026-10-20 12:44:24 +08:00
1 parent 336ca510c8
commit 9744e14809
2 files changed
+175 -1

No files matched your search

+172
View File
@@ -18,6 +18,36 @@ pub struct VerticalBox {
pub height: i64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum CellAlignment {
Left,
Center,
Right,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct HalignCell {
pub text: String,
pub span: usize,
pub alignment: CellAlignment,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct HalignRow {
pub cells: Vec<HalignCell>,
pub rule: bool,
}
/// A laid-out `\halign` table: natural column widths plus row rules.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Halign {
pub rows: Vec<HalignRow>,
pub column_widths: Vec<i64>,
pub width: i64,
pub height: i64,
pub line_height: i64,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum VerticalItem {
Box(VerticalBox),
@@ -26,6 +56,7 @@ pub enum VerticalItem {
Insertion(Insertion),
Mark(Mark),
Float(Float),
Halign(Halign),
}
/// Vertical material reserved at the bottom of a page (footnotes etc.).
@@ -65,6 +96,67 @@ pub struct Unit {
}
impl Unit {
/// Build a simple `\halign` table with natural column widths.
pub fn halign(id: UnitId, rows: Vec<HalignRow>, column_count: usize, intercolumn: i64) -> Self {
let mut column_widths = vec![0_i64; column_count];
for row in &rows {
let mut column = 0_usize;
for cell in &row.cells {
let span = cell.span.max(1).min(column_count.saturating_sub(column));
if span == 1 {
let width: i64 = cell
.text
.chars()
.map(|character| if character.is_whitespace() { 4 } else { 10 })
.sum();
if let Some(slot) = column_widths.get_mut(column) {
*slot = (*slot).max(width);
}
}
column += span;
}
}
for row in &rows {
let mut column = 0_usize;
for cell in &row.cells {
let span = cell.span.max(1).min(column_count.saturating_sub(column));
if span > 1 {
let gaps = intercolumn * (span.saturating_sub(1) as i64);
let available: i64 = column_widths[column..column + span]
.iter()
.copied()
.sum::<i64>()
+ gaps;
let needed: i64 = cell.text.chars().count() as i64 * 10;
let extra = (needed - available).max(0);
for offset in 0..span {
column_widths[column + offset] += extra / span as i64;
}
}
column += span;
}
}
let gaps = intercolumn * column_count.saturating_sub(1) as i64;
let width = column_widths.iter().sum::<i64>() + gaps;
let line_height = 12;
let rule_height = 26_214; // 0.4pt
let height = rows
.iter()
.map(|row| if row.rule { rule_height } else { line_height })
.sum();
Self {
id,
source: "halign".to_owned(),
items: vec![VerticalItem::Halign(Halign {
rows,
column_widths,
width,
height,
line_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 {
@@ -267,6 +359,20 @@ impl Engine {
}
current.items.push(item.clone());
}
VerticalItem::Halign(halign) => {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(halign.height);
} else if used + halign.height
> document.page_goal - insertion_height - bottom_float_height(&current)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip.max(halign.height);
insertion_height = 0;
} else {
used += halign.height;
}
current.items.push(item.clone());
}
VerticalItem::Glue(amount) => {
if !current.items.is_empty() {
used += amount;
@@ -390,6 +496,7 @@ impl Engine {
VerticalItem::Insertion(insertion) => insertion.height,
VerticalItem::Mark(_) => 0,
VerticalItem::Float(float) => float.height,
VerticalItem::Halign(halign) => halign.height,
})
.sum();
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() {
@@ -438,6 +545,20 @@ impl Engine {
}
current.items.push(item.clone());
}
VerticalItem::Halign(halign) => {
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(halign.height);
} else if used + halign.height
> next.page_goal - insertion_height - bottom_float_height(&current)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip.max(halign.height);
insertion_height = 0;
} else {
used += halign.height;
}
current.items.push(item.clone());
}
VerticalItem::Glue(amount) => {
if !current.items.is_empty() {
used += amount;
@@ -760,6 +881,57 @@ mod tests {
assert_eq!(engine.layout.pages[1].items.len(), 1);
}
#[test]
fn halign_computes_natural_column_widths() {
let rows = vec![
HalignRow {
cells: vec![
HalignCell {
text: "A".to_owned(),
span: 1,
alignment: CellAlignment::Left,
},
HalignCell {
text: "B".to_owned(),
span: 1,
alignment: CellAlignment::Left,
},
],
rule: false,
},
HalignRow {
cells: vec![
HalignCell {
text: "longword".to_owned(),
span: 1,
alignment: CellAlignment::Left,
},
HalignCell {
text: "C".to_owned(),
span: 1,
alignment: CellAlignment::Right,
},
],
rule: true,
},
HalignRow {
cells: vec![HalignCell {
text: "TOTAL".to_owned(),
span: 2,
alignment: CellAlignment::Center,
}],
rule: false,
},
];
let unit = Unit::halign(0, rows, 2, 20);
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected a halign item");
};
assert_eq!(halign.column_widths, vec![80, 10]);
assert_eq!(halign.width, 110);
assert_eq!(halign.height, 12 + 26_214 + 12);
}
#[test]
fn checkpoint_round_trips_through_serde() {
let engine = Engine::layout(&document());