Add underline math atoms

This commit is contained in:
gloamxun committed 2026-10-20 13:00:20 +08:00
1 parent 053468875e
commit b11fc864f2
2 files changed
+31

No files matched your search

+29
View File
@@ -24,6 +24,9 @@ pub enum MathAtom {
accent: char,
base: Box<MathAtom>,
},
Underline {
base: Box<MathAtom>,
},
Script {
base: Box<MathAtom>,
sub: Option<Box<MathAtom>>,
@@ -198,6 +201,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth,
}
}
MathAtom::Underline { base } => {
let base_box = layout_math(base, font);
MathBox {
width: base_box.width,
height: base_box.height,
depth: base_box.depth + 2 * 65_536,
}
}
MathAtom::Script { base, sub, sup } => {
let base_box = layout_math(base, font);
let sub_box = sub.as_deref().map(|atom| layout_math(atom, font));
@@ -377,6 +388,15 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"underline" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Underline {
base: Box::new(base),
},
after,
)
}
"bar" | "overline" => {
let (base, after) = parse_atom(chars, next);
(
@@ -507,6 +527,15 @@ mod tests {
assert!(indexed.height > plain.height);
}
#[test]
fn underline_adds_depth_without_changing_width() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
let underlined = layout_math(&parse_math("\\underline{x}"), &font);
assert_eq!(underlined.width, plain.width);
assert!(underlined.depth > plain.depth);
assert_eq!(underlined.height, plain.height);
}
#[test]
fn math_text_uses_upright_character_widths() {
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::Underline { base }` parses `\underline{...}` and adds 2pt depth
without changing width or height.
- `MathAtom::Text(String)` parses `\text{...}` and `\mathrm{...}`;
widths use upright CMR glyphs and spaces use the font space width.
- `MathAtom::Delimited { left, right, body }` and `\left...\right`