Check stacked limits against pdfLaTeX

This commit is contained in:
gloamxun committed 2026-10-26 00:59:19 +08:00
1 parent 52a74755f7
commit 569160f805
6 files changed
+107 -13

No files matched your search

+41 -4
View File
@@ -896,6 +896,33 @@ fn math_environment(chars: &[char], index: usize) -> Option<(MathAtom, usize)> {
))
}
/// Scale a math box to TeX's 70% script size.
pub(crate) fn script_metrics(box_: MathBox) -> MathBox {
MathBox {
width: box_.width * 7 / 10,
height: box_.height * 7 / 10,
depth: box_.depth * 7 / 10,
}
}
/// The glyph depth TeX would use for a base, without the 2pt floor that
/// ordinary math boxes apply for visual spacing.
pub(crate) fn intrinsic_depth(atom: &MathAtom, font: &Cmr10Metrics) -> i64 {
match atom {
MathAtom::Symbol(character) => font.depth(*character).max(0),
MathAtom::Row(atoms) => atoms
.iter()
.rev()
.map(|atom| intrinsic_depth(atom, font))
.find(|depth| *depth > 0)
.unwrap_or(0),
MathAtom::Classed { body, .. } => intrinsic_depth(body, font),
MathAtom::Script { base, .. } => intrinsic_depth(base, font),
MathAtom::Limits { base, .. } => intrinsic_depth(base, font),
_ => layout_math(atom, font).depth,
}
}
pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
match atom {
MathAtom::Symbol(character) => {
@@ -1053,15 +1080,25 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
MathAtom::Limits { base, sub, sup } => {
let base = layout_math(base, font);
let sub = sub.as_ref().map(|atom| layout_math(atom, font));
let sup = sup.as_ref().map(|atom| layout_math(atom, font));
let sub = sub
.as_ref()
.map(|atom| script_metrics(layout_math(atom, font)));
let sup = sup
.as_ref()
.map(|atom| script_metrics(layout_math(atom, font)));
MathBox {
width: base
.width
.max(sub.as_ref().map_or(0, |item| item.width))
.max(sup.as_ref().map_or(0, |item| item.width)),
height: base.height + sup.as_ref().map_or(0, |item| item.height + 65_536),
depth: base.depth + sub.as_ref().map_or(0, |item| item.height + 65_536),
height: base.height
+ sup
.as_ref()
.map_or(0, |item| item.height + item.depth + 65_536),
depth: base.depth
+ sub
.as_ref()
.map_or(0, |item| item.height + item.depth + 65_536),
}
}
MathAtom::Classed { body, .. } => layout_math(body, font),
+9 -4
View File
@@ -1232,23 +1232,28 @@ impl Page {
Self::paint_math(base, font, x, baseline, size, list);
let small = (size as f64 * 0.7) as i64;
if let Some(sup) = sup {
let sup_box = math::layout_math(sup, font);
let sup_box = math::script_metrics(math::layout_math(sup, font));
Self::paint_math(
sup,
font,
x + (base_box.width - sup_box.width).max(0) / 2,
baseline - base_box.height - small,
baseline - base_box.height - sup_box.depth + 96_993,
small,
list,
);
}
if let Some(sub) = sub {
let sub_box = math::layout_math(sub, font);
let sub_box = math::script_metrics(math::layout_math(sub, font));
Self::paint_math(
sub,
font,
x + (base_box.width - sub_box.width).max(0) / 2,
baseline + base_box.depth + small,
baseline
+ math::intrinsic_depth(base, font)
+ 131_072
+ sub_box.height
+ 65_536
+ 46_531,
small,
list,
);
+52
View File
@@ -1373,3 +1373,55 @@ fn cases_row_spacing_matches_pdftex() {
);
}
}
#[test]
fn display_limits_sit_above_and_below_their_base() {
for base in ["M", "A", "g", "W"] {
let source = format!(
"\\documentclass[10pt]{{article}}\n\\pagestyle{{empty}}\n\\begin{{document}}\n\\[ \\mathop{{{base}}}_{{i}}^{{n}} \\]\n\\end{{document}}\n"
);
let Some(tex) = tex_word_positions(&source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let tex = |letter: &str| {
tex.iter()
.find(|(text, _, _)| text == letter)
.map(|(_, x, y)| (*x, *y))
.unwrap_or_else(|| panic!("pdfLaTeX produced no `{letter}`"))
};
let atom = texrra_tex_core::math::parse_math(&format!("\\mathop{{{base}}}_{{i}}^{{n}}"));
let font = texrra_tex_core::cmr10::Cmr10Metrics::default();
let unit = Unit::math(1, atom, &font);
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 = |letter: &str| {
display
.items
.iter()
.find_map(|item| match item {
texrra_tex_core::render::DisplayItem::Text { text, x, y, .. }
if text == letter =>
{
Some((*x as f64 / PT as f64, *y as f64 / PT as f64))
}
_ => None,
})
.unwrap_or_else(|| panic!("route-D produced no `{letter}`"))
};
let (origin_x, origin_y) = ours(base);
let (tex_x, tex_y) = tex(base);
for letter in ["i", "n"] {
let (x, y) = ours(letter);
let (reference_x, reference_y) = tex(letter);
let dx = (x - origin_x) - (reference_x - tex_x);
let dy = (y - origin_y) - (reference_y - tex_y);
assert!(
dx.abs() <= 1.2 && dy.abs() <= 0.2,
"limits over `{base}`: `{letter}` is off by ({dx:.2}, {dy:.2}) pt"
);
}
}
}