Lay out natural-width halign tables
This commit is contained in:
1 parent
336ca510c8
commit
9744e14809
2 files changed
+175
-1
No files matched your search
@@ -18,6 +18,36 @@ pub struct VerticalBox {
|
|||||||
pub height: i64,
|
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)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum VerticalItem {
|
pub enum VerticalItem {
|
||||||
Box(VerticalBox),
|
Box(VerticalBox),
|
||||||
@@ -26,6 +56,7 @@ pub enum VerticalItem {
|
|||||||
Insertion(Insertion),
|
Insertion(Insertion),
|
||||||
Mark(Mark),
|
Mark(Mark),
|
||||||
Float(Float),
|
Float(Float),
|
||||||
|
Halign(Halign),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Vertical material reserved at the bottom of a page (footnotes etc.).
|
/// Vertical material reserved at the bottom of a page (footnotes etc.).
|
||||||
@@ -65,6 +96,67 @@ pub struct Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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
|
/// M1 paragraph builder: box/glue/penalty item stream with a
|
||||||
/// Knuth-Plass-style word-level line breaker.
|
/// Knuth-Plass-style word-level line breaker.
|
||||||
pub fn paragraph(id: UnitId, source: impl Into<String>) -> Self {
|
pub fn paragraph(id: UnitId, source: impl Into<String>) -> Self {
|
||||||
@@ -267,6 +359,20 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
current.items.push(item.clone());
|
current.items.push(item.clone());
|
||||||
}
|
}
|
||||||
|
VerticalItem::Halign(halign) => {
|
||||||
|
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||||
|
used = document.top_skip.max(halign.height);
|
||||||
|
} else if used + halign.height
|
||||||
|
> document.page_goal - insertion_height - bottom_float_height(¤t)
|
||||||
|
{
|
||||||
|
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) => {
|
VerticalItem::Glue(amount) => {
|
||||||
if !current.items.is_empty() {
|
if !current.items.is_empty() {
|
||||||
used += amount;
|
used += amount;
|
||||||
@@ -390,6 +496,7 @@ impl Engine {
|
|||||||
VerticalItem::Insertion(insertion) => insertion.height,
|
VerticalItem::Insertion(insertion) => insertion.height,
|
||||||
VerticalItem::Mark(_) => 0,
|
VerticalItem::Mark(_) => 0,
|
||||||
VerticalItem::Float(float) => float.height,
|
VerticalItem::Float(float) => float.height,
|
||||||
|
VerticalItem::Halign(halign) => halign.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() {
|
||||||
@@ -438,6 +545,20 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
current.items.push(item.clone());
|
current.items.push(item.clone());
|
||||||
}
|
}
|
||||||
|
VerticalItem::Halign(halign) => {
|
||||||
|
if current.items.is_empty() && top_float_height(¤t) == 0 {
|
||||||
|
used = next.top_skip.max(halign.height);
|
||||||
|
} else if used + halign.height
|
||||||
|
> next.page_goal - insertion_height - bottom_float_height(¤t)
|
||||||
|
{
|
||||||
|
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) => {
|
VerticalItem::Glue(amount) => {
|
||||||
if !current.items.is_empty() {
|
if !current.items.is_empty() {
|
||||||
used += amount;
|
used += amount;
|
||||||
@@ -760,6 +881,57 @@ mod tests {
|
|||||||
assert_eq!(engine.layout.pages[1].items.len(), 1);
|
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]
|
#[test]
|
||||||
fn checkpoint_round_trips_through_serde() {
|
fn checkpoint_round_trips_through_serde() {
|
||||||
let engine = Engine::layout(&document());
|
let engine = Engine::layout(&document());
|
||||||
|
|||||||
+3
-1
@@ -72,7 +72,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, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; interpreted `\output`, float pages next |
|
| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; interpreted `\output`, float pages next |
|
||||||
| M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | table width/rules match reference |
|
| M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | **partial**: `Halign` model with natural column widths, spans and rule rows; no `\omit`/`\span` token parsing or PDF painting yet |
|
||||||
| 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 |
|
||||||
| M6 | class/package execution (`article`, `sn-jnl`) | Overleaf page-count parity |
|
| M6 | class/package execution (`article`, `sn-jnl`) | Overleaf page-count parity |
|
||||||
@@ -101,6 +101,8 @@ overfull ratio 4.11, badness 6942. TeX rejects the underfull candidate under
|
|||||||
its tolerance but still emits the moderately overfull 5-word line. Reproducing
|
its tolerance but still emits the moderately overfull 5-word line. Reproducing
|
||||||
this needs a two-pass tolerance algorithm with an emergency overfull pass, not
|
this needs a two-pass tolerance algorithm with an emergency overfull pass, not
|
||||||
a single cost function.
|
a single cost function.
|
||||||
|
- `Unit::halign` lays out natural column widths, multi-column spans and rule
|
||||||
|
rows as a `VerticalItem::Halign`; it is not yet painted by the v2 PDF path.
|
||||||
- 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. `VerticalItem::Insertion` material is
|
but not yet part of page badness. `VerticalItem::Insertion` material is
|
||||||
|
|||||||
Reference in new issue
Block a user