Measure table cells with Computer Modern metrics

This commit is contained in:
gloamxun committed 2026-10-26 04:07:29 +08:00
1 parent 39f02b83c0
commit d89b71bfbc
5 files changed
+91 -18

No files matched your search

+21 -14
View File
@@ -548,14 +548,15 @@ impl Halign {
}
}
/// Natural width of a run of text at the illustrative 10pt metrics.
/// Natural width of a run of text in the default 10pt Roman face.
pub(crate) fn text_width(text: &str) -> i64 {
let metrics = crate::cmr10::Cmr10Metrics::Roman;
text.chars()
.map(|character| {
if character.is_whitespace() {
4 * 65_536
metrics.space()
} else {
10 * 65_536
metrics.width(character)
}
})
.sum()
@@ -6190,9 +6191,16 @@ mod tests {
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected a halign item");
};
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)));
let first = crate::text_width("longword").max(crate::text_width("A"));
let second = crate::text_width("C").max(crate::text_width("B"));
let gap = 20 * 65_536;
assert_eq!(halign.cell_box(0, 0), Some((0, first)));
assert_eq!(halign.cell_box(1, 1), Some((first + gap, second)));
let total = crate::text_width("TOTAL");
assert_eq!(
halign.cell_box(2, 0),
Some(((first + gap + second - total) / 2, first + gap + second))
);
}
#[test]
@@ -7714,8 +7722,10 @@ mod tests {
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected a halign item");
};
assert_eq!(halign.column_widths, vec![5242880, 655360]);
assert_eq!(halign.width, 7208960);
let first = crate::text_width("longword").max(crate::text_width("A"));
let second = crate::text_width("C").max(crate::text_width("B"));
assert_eq!(halign.column_widths, vec![first, second]);
assert_eq!(halign.width, first + second + 20 * 65_536);
assert_eq!(halign.height, 1_599_078);
}
@@ -7851,23 +7861,20 @@ second row & right\n",
);
assert_eq!(halign.column_widths[0], wrap);
let lines = halign.cell_lines(0, 0);
assert!(lines.len() >= 3, "lines: {lines:?}");
assert!(lines.len() >= 2, "lines: {lines:?}");
assert!(lines.iter().all(|line| crate::text_width(line) <= wrap));
assert_eq!(
halign.row_height(0),
halign.line_height * lines.len() as i64
);
assert_eq!(
halign.cell_lines(1, 0),
vec!["second".to_owned(), "row".to_owned()]
);
assert_eq!(halign.cell_lines(1, 0).join(" "), "second row");
}
#[test]
fn paragraph_columns_paint_wrapped_lines_and_split_rows() {
let unit = Unit::halign_from_source_with_template(
1,
"p{20mm}l",
"p{10mm}l",
"alpha beta gamma delta & x \\\\
second row text & y\n",
0,
+66
View File
@@ -1261,6 +1261,72 @@ fn hfill_and_hfil_match_pdftex() {
compare_fixture_with("alpha \\hfil beta \\hfill gamma", 150, "", |_| {});
}
#[test]
fn tabular_word_positions_match_pdftex() {
let source = r"\documentclass[10pt]{article}
\pagestyle{empty}
\setlength{\tabcolsep}{0pt}
\begin{document}
\noindent\begin{tabular}{lcr}
Alpha & Beta & Gamma \\
Delta & Epsilon & Zeta
\end{tabular}
\end{document}
";
let Some(tex) = tex_word_positions(source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let tex_word = |word: &str| {
tex.iter()
.find(|(text, _, _)| text == word)
.map(|(_, x, y)| (*x, *y))
.unwrap_or_else(|| panic!("pdfLaTeX produced no `{word}`"))
};
let unit = Unit::halign_from_source_with_template(
1,
"lcr",
"Alpha & Beta & Gamma \\\\ Delta & Epsilon & Zeta",
0,
);
let document = Document::new(700 * PT, vec![unit]);
let engine = Engine::layout(&document);
let page = engine.pages().first().expect("one page");
let display = page.display_list();
let ours = |word: &str| {
display
.items
.iter()
.find_map(|item| match item {
texrra_tex_core::render::DisplayItem::Text { text, x, y, .. } if text == word => {
Some((*x as f64 / PT as f64, *y as f64 / PT as f64))
}
_ => None,
})
.unwrap_or_else(|| panic!("route-D produced no `{word}`"))
};
let words = ["Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta"];
let (origin_x, origin_y) = ours("Alpha");
let (tex_x, tex_y) = tex_word("Alpha");
let mut max_dx = 0.0_f64;
let mut max_dy = 0.0_f64;
for word in words {
let (x, y) = ours(word);
let (reference_x, reference_y) = tex_word(word);
let dx = (x - origin_x) - (reference_x - tex_x);
let dy = (y - origin_y) - (reference_y - tex_y);
max_dx = max_dx.max(dx.abs());
max_dy = max_dy.max(dy.abs());
}
eprintln!("tabular maximum offsets: ({max_dx:.2}, {max_dy:.2}) pt");
assert!(
max_dx <= 2.0 && max_dy <= 2.0,
"tabular words are off by up to ({max_dx:.2}, {max_dy:.2}) pt"
);
}
#[test]
fn pmatrix_word_positions_match_pdftex() {
let source = r"\documentclass[10pt]{article}