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

+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}