diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 748306b..14c3ed7 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -27,6 +27,11 @@ pub enum MathAtom { Underline { base: Box, }, + Stack { + top: Box, + base: Box, + above: bool, + }, Brace { base: Box, 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(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 551a182..466275a 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::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