From c31193812900e3ee1fa08b0ad1ed3b4ce3232b9f Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:10:02 +0800 Subject: [PATCH] Add smash math boxes --- crates/texrra-tex-core/src/math.rs | 33 ++++++++++++++++++++++++++++++ docs/TEX_CORE.md | 1 + 2 files changed, 34 insertions(+) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index db4bc74..576d88d 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -30,6 +30,11 @@ pub enum MathAtom { BigOperator { name: String, }, + Smash { + body: Box, + top: bool, + bottom: bool, + }, Phantom { body: Box, 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(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index f133caf..cb96226 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -113,6 +113,7 @@ 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::Smash { body, top, bottom }` implements `\smash` (zero height/depth). - `MathAtom::Phantom { body, horizontal, vertical }` implements `\phantom`, `\hphantom`, `\vphantom` dimension-only boxes. - `MathAtom::BigOperator { name }` gives `\sum`/`\prod` 12pt width and