Add overset and underset math atoms

This commit is contained in:
gloamxun committed 2026-10-20 13:02:42 +08:00
1 parent 4e892005cd
commit 15741c0b9c
2 files changed
+39

No files matched your search

+37
View File
@@ -27,6 +27,11 @@ pub enum MathAtom {
Underline {
base: Box<MathAtom>,
},
Stack {
top: Box<MathAtom>,
base: Box<MathAtom>,
above: bool,
},
Brace {
base: Box<MathAtom>,
above: bool,
@@ -209,6 +214,15 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth,
}
}
MathAtom::Stack { top, base, above } => {
let top = layout_math(top, font);
let base = layout_math(base, font);
MathBox {
width: top.width.max(base.width),
height: base.height + if *above { top.height + 65_536 } else { 0 },
depth: base.depth + if *above { 0 } else { top.height + 65_536 },
}
}
MathAtom::Brace { base, above } => {
let base_box = layout_math(base, font);
MathBox {
@@ -416,6 +430,18 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"overset" | "underset" => {
let (top, after) = parse_atom(chars, next);
let (base, after) = parse_atom(chars, after);
(
MathAtom::Stack {
top: Box::new(top),
base: Box::new(base),
above: command == "overset",
},
after,
)
}
"overbrace" | "underbrace" => {
let (base, after) = parse_atom(chars, next);
(
@@ -580,6 +606,17 @@ mod tests {
assert!(indexed.height > plain.height);
}
#[test]
fn overset_and_underset_stack_their_arguments() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
let over = layout_math(&parse_math("\\overset{a}{x}"), &font);
let under = layout_math(&parse_math("\\underset{a}{x}"), &font);
assert!(over.height > plain.height);
assert_eq!(over.depth, plain.depth);
assert!(under.depth > plain.depth);
assert_eq!(under.height, plain.height);
}
#[test]
fn braces_add_height_or_depth() {
let font = Cmr10Metrics::default();
+2
View File
@@ -110,6 +110,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::Stack { top, base, above }` parses `\overset`/`\underset`;
the decoration contributes 1pt of extra separation.
- `MathAtom::Brace { base, above }` parses `\overbrace`/`\underbrace` and
adds 2pt of height or depth.
- `MathAtom::Binomial { numerator, denominator }` parses