Normalize halign geometry to scaled points

This commit is contained in:
gloamxun committed 2026-10-20 18:23:29 +08:00
1 parent 4c64726e61
commit 1d9b152beb
3 files changed
+38 -12

No files matched your search

+36 -10
View File
@@ -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::<i64>();
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<HalignRow>, 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::<i64>()
+ 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::<i64>() + 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</text>"));
assert!(svg.contains(">w</text>"));
}
#[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]
+1 -1
View File
@@ -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(),
});
}
+1 -1
View File
@@ -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.