From 1d9b152bebcbbb9c6f7523d2464b844e12571e8a Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:23:29 +0800 Subject: [PATCH] Normalize halign geometry to scaled points --- crates/texrra-tex-core/src/lib.rs | 46 ++++++++++++++++++++++------ crates/texrra-tex-core/src/render.rs | 2 +- docs/TEX_CORE.md | 2 +- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 3e029f3..840446a 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -108,7 +108,13 @@ impl Halign { let text_width = cell .text .chars() - .map(|character| if character.is_whitespace() { 4 } else { 10 }) + .map(|character| { + if character.is_whitespace() { + 4 * 65_536 + } else { + 10 * 65_536 + } + }) .sum::(); let offset = match cell.alignment { CellAlignment::Left => 0, @@ -163,6 +169,7 @@ pub struct Unit { impl Unit { /// Build a simple `\halign` table with natural column widths. pub fn halign(id: UnitId, rows: Vec, column_count: usize, intercolumn: i64) -> Self { + let intercolumn = intercolumn.saturating_mul(65_536); let mut column_widths = vec![0_i64; column_count]; for row in &rows { let mut column = 0_usize; @@ -172,7 +179,13 @@ impl Unit { let width: i64 = cell .text .chars() - .map(|character| if character.is_whitespace() { 4 } else { 10 }) + .map(|character| { + if character.is_whitespace() { + 4 * 65_536 + } else { + 10 * 65_536 + } + }) .sum(); if let Some(slot) = column_widths.get_mut(column) { *slot = (*slot).max(width); @@ -192,7 +205,7 @@ impl Unit { .copied() .sum::() + gaps; - let needed: i64 = cell.text.chars().count() as i64 * 10; + let needed: i64 = cell.text.chars().count() as i64 * 10 * 65_536; let extra = (needed - available).max(0); for offset in 0..span { column_widths[column + offset] += extra / span as i64; @@ -203,7 +216,7 @@ impl Unit { } let gaps = intercolumn * column_count.saturating_sub(1) as i64; let width = column_widths.iter().sum::() + gaps; - let line_height = 12; + let line_height = 12 * 65_536; let rule_height = 26_214; // 0.4pt let height = rows .iter() @@ -1317,9 +1330,9 @@ mod tests { let VerticalItem::Halign(halign) = &unit.items[0] else { panic!("expected a halign item"); }; - assert_eq!(halign.cell_box(0, 0), Some((0, 80))); - assert_eq!(halign.cell_box(1, 1), Some((100, 10))); - assert_eq!(halign.cell_box(2, 0), Some((30, 110))); + assert_eq!(halign.cell_box(0, 0), Some((0, 5242880))); + assert_eq!(halign.cell_box(1, 1), Some((6553600, 655360))); + assert_eq!(halign.cell_box(2, 0), Some((1966080, 7208960))); } #[test] @@ -1571,6 +1584,19 @@ mod tests { assert!(svg.contains(">h")); assert!(svg.contains(">w")); } + + #[test] + fn halign_text_is_painted_at_ten_points() { + let unit = Unit::halign_from_source(1, r"A & B", 2, 10); + let document = Document::new(1000, vec![unit]); + let engine = Engine::layout(&document); + let list = engine.pages()[0].display_list(); + let size = list.items.iter().find_map(|item| match item { + DisplayItem::Text { size, .. } => Some(*size), + _ => None, + }); + assert_eq!(size, Some(10 * 65_536)); + } #[test] fn halign_display_list_paints_cells_and_rules() { let unit = Unit::halign_from_source(1, r"A & B \\ \hline C & D", 2, 10); @@ -1742,9 +1768,9 @@ mod tests { 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); + assert_eq!(halign.column_widths, vec![5242880, 655360]); + assert_eq!(halign.width, 7208960); + assert_eq!(halign.height, 1_599_078); } #[test] diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 499835b..7258e42 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -215,7 +215,7 @@ impl Halign { list.push(DisplayItem::Text { x, y: baseline, - size: self.line_height, + size: self.line_height * 5 / 6, text: cell.text.clone(), }); } diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 536b9a0..9683a65 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -266,4 +266,4 @@ M2 (partial): a 0.4pt rule at 40% of the body width is painted above insertion m M2 (partial): top and bottom float placeholders are painted at the page edges in display lists; real float material and float-page fractions remain. -Known route-D gap (M3): Halign row heights are stored in raw points while DisplayList/page geometry uses scaled points. The halign painter therefore cannot use its own line_height as a glyph size until Unit::halign normalizes all dimensions to scaled points; a 10pt experiment was reverted rather than shipped with a mixed-unit layout. +M3 (partial): Unit::halign normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the display-list painter uses a 10pt glyph size inside 12pt rows. Rules and spans are painted; page-level table splitting remains.