From e55b44beacfd829b2e5cea320e217d707e7fdd7e Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:07:15 +0800 Subject: [PATCH] Give big operators display-sized metrics --- crates/texrra-tex-core/src/math.rs | 33 +++++++++++++++++++++++++++++- docs/TEX_CORE.md | 2 ++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 26eee9b..033ba83 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -27,6 +27,9 @@ pub enum MathAtom { Underline { base: Box, }, + BigOperator { + name: String, + }, Limits { base: Box, sub: Option>, @@ -228,6 +231,18 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { depth: base_box.depth, } } + MathAtom::BigOperator { name } => { + let (width, height, depth) = match name.as_str() { + "int" => (8, 10, 4), + "prod" => (12, 8, 3), + _ => (12, 8, 3), + }; + MathBox { + width: width * 65_536, + height: height * 65_536, + depth: depth * 65_536, + } + } MathAtom::Limits { base, sub, sup } => { let base = layout_math(base, font); let sub = sub.as_ref().map(|atom| layout_math(atom, font)); @@ -512,7 +527,13 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { after, ) } - "sum" | "prod" | "int" | "lim" | "sup" | "inf" => { + "sum" | "prod" | "int" => { + let base = MathAtom::BigOperator { + name: command.clone(), + }; + parse_limits(chars, next, base) + } + "lim" | "sup" | "inf" => { let base = MathAtom::Text(command.clone()); parse_limits(chars, next, base) } @@ -754,6 +775,16 @@ mod tests { assert_eq!(calligraphic.width, upright.width); assert_eq!(calligraphic.height, upright.height); } + + #[test] + fn big_operators_are_wider_than_letters() { + let font = Cmr10Metrics::default(); + let sum = layout_math(&parse_math("\\sum"), &font); + let letter = layout_math(&parse_math("s"), &font); + assert!(sum.width > letter.width); + assert!(sum.height > letter.height); + assert!(sum.depth > letter.depth); + } #[test] fn sum_places_scripts_above_and_below() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 8f19393..7b744e3 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::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`, `\lim`, `\sup`, `\inf` above/below scripts. - `MathAtom::Classed { class, body }` implements `\mathord`, `\mathop`,