From b11fc864f240c66a8778b7eb6317cb6c99f558aa Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:00:20 +0800 Subject: [PATCH] Add underline math atoms --- crates/texrra-tex-core/src/math.rs | 29 +++++++++++++++++++++++++++++ docs/TEX_CORE.md | 2 ++ 2 files changed, 31 insertions(+) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index bd4d58e..05dcf53 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -24,6 +24,9 @@ pub enum MathAtom { accent: char, base: Box, }, + Underline { + base: Box, + }, Script { base: Box, sub: Option>, @@ -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(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 8958747..a4ff8d0 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -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`