diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index f20bf7f..748306b 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -27,6 +27,10 @@ pub enum MathAtom { Underline { base: Box, }, + Brace { + base: Box, + above: bool, + }, Binomial { numerator: Box, denominator: Box, @@ -205,6 +209,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { depth: base_box.depth, } } + MathAtom::Brace { base, above } => { + let base_box = layout_math(base, font); + MathBox { + width: base_box.width.max(4 * 65_536), + height: base_box.height + if *above { 2 * 65_536 } else { 0 }, + depth: base_box.depth + if *above { 0 } else { 2 * 65_536 }, + } + } MathAtom::Binomial { numerator, denominator, @@ -404,6 +416,16 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { after, ) } + "overbrace" | "underbrace" => { + let (base, after) = parse_atom(chars, next); + ( + MathAtom::Brace { + base: Box::new(base), + above: command == "overbrace", + }, + after, + ) + } "binom" | "dbinom" | "tbinom" => { let (numerator, after) = parse_atom(chars, next); let (denominator, after) = parse_atom(chars, after); @@ -558,6 +580,17 @@ mod tests { assert!(indexed.height > plain.height); } + #[test] + fn braces_add_height_or_depth() { + let font = Cmr10Metrics::default(); + let over = layout_math(&parse_math("\\overbrace{x+y}"), &font); + let under = layout_math(&parse_math("\\underbrace{x+y}"), &font); + let plain = layout_math(&parse_math("x+y"), &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 binom_stacks_two_arguments() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 50f01ef..551a182 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::Brace { base, above }` parses `\overbrace`/`\underbrace` and + adds 2pt of height or depth. - `MathAtom::Binomial { numerator, denominator }` parses `\binom{n}{k}` / `\dbinom` / `\tbinom`; layout stacks the arguments with a 4pt delimiter allowance.