Add smash math boxes

This commit is contained in:
gloamxun committed 2026-10-20 13:10:02 +08:00
1 parent 4e45a354fe
commit c311938129
2 files changed
+34

No files matched your search

+33
View File
@@ -30,6 +30,11 @@ pub enum MathAtom {
BigOperator {
name: String,
},
Smash {
body: Box<MathAtom>,
top: bool,
bottom: bool,
},
Phantom {
body: Box<MathAtom>,
horizontal: bool,
@@ -236,6 +241,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth,
}
}
MathAtom::Smash { body, top, bottom } => {
let body = layout_math(body, font);
MathBox {
width: body.width,
height: if *top { 0 } else { body.height },
depth: if *bottom { 0 } else { body.depth },
}
}
MathAtom::Phantom {
body,
horizontal,
@@ -544,6 +557,17 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"smash" => {
let (body, after) = parse_atom(chars, next);
(
MathAtom::Smash {
body: Box::new(body),
top: true,
bottom: true,
},
after,
)
}
"phantom" | "hphantom" | "vphantom" => {
let (body, after) = parse_atom(chars, next);
(
@@ -804,6 +828,15 @@ mod tests {
assert_eq!(calligraphic.height, upright.height);
}
#[test]
fn smash_removes_height_but_keeps_width() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
let smashed = layout_math(&parse_math("\\smash{x}"), &font);
assert_eq!(smashed.width, plain.width);
assert_eq!(smashed.height, 0);
assert_eq!(smashed.depth, 0);
}
#[test]
fn phantoms_keep_only_selected_dimensions() {
let font = Cmr10Metrics::default();