Add phantom math boxes

This commit is contained in:
gloamxun committed 2026-10-20 13:09:18 +08:00
1 parent 247cc8bd37
commit 4e45a354fe
2 files changed
+41

No files matched your search

+39
View File
@@ -30,6 +30,11 @@ pub enum MathAtom {
BigOperator {
name: String,
},
Phantom {
body: Box<MathAtom>,
horizontal: bool,
vertical: bool,
},
Limits {
base: Box<MathAtom>,
sub: Option<Box<MathAtom>>,
@@ -231,6 +236,18 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth,
}
}
MathAtom::Phantom {
body,
horizontal,
vertical,
} => {
let body = layout_math(body, font);
MathBox {
width: if *horizontal { body.width } else { 0 },
height: if *vertical { body.height } else { 0 },
depth: if *vertical { body.depth } else { 0 },
}
}
MathAtom::BigOperator { name } => {
let (width, height, depth) = match name.as_str() {
"int" => (8, 10, 4),
@@ -527,6 +544,17 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"phantom" | "hphantom" | "vphantom" => {
let (body, after) = parse_atom(chars, next);
(
MathAtom::Phantom {
body: Box::new(body),
horizontal: command != "vphantom",
vertical: command != "hphantom",
},
after,
)
}
"sum" | "prod" | "int" => {
let base = MathAtom::BigOperator {
name: command.clone(),
@@ -776,6 +804,17 @@ mod tests {
assert_eq!(calligraphic.height, upright.height);
}
#[test]
fn phantoms_keep_only_selected_dimensions() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
let horizontal = layout_math(&parse_math("\\hphantom{x}"), &font);
let vertical = layout_math(&parse_math("\\vphantom{x}"), &font);
assert_eq!(horizontal.width, plain.width);
assert_eq!(horizontal.height, 0);
assert_eq!(vertical.width, 0);
assert_eq!(vertical.height, plain.height);
}
#[test]
fn big_operators_are_wider_than_letters() {
let font = Cmr10Metrics::default();
+2
View File
@@ -113,6 +113,8 @@ a single cost function.
- `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`,
`\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds
accent height without changing the base width.
- `MathAtom::Phantom { body, horizontal, vertical }` implements
`\phantom`, `\hphantom`, `\vphantom` dimension-only boxes.
- `MathAtom::BigOperator { name }` gives `\sum`/`\prod` 12pt width and
`\int` 8pt width with larger height/depth than text.
- `MathAtom::Limits { base, sub, sup }` gives `\sum`, `\prod`, `\int`,