diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 033ba83..db4bc74 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, }, + Phantom { + body: Box, + horizontal: bool, + vertical: bool, + }, Limits { base: Box, sub: Option>, @@ -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(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index dd15f7f..f133caf 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -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`,