From 72f7cfaf2ac77bcf27cea3be9fd7c8e72d594d55 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 19:28:33 +0800 Subject: [PATCH] Grow left-right delimiters with tall bodies --- crates/texrra-tex-core/src/math.rs | 27 ++++++++++++++++++++++----- crates/texrra-tex-core/src/render.rs | 12 ++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 2a3605b..c2af31f 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -189,6 +189,13 @@ fn delimiter_width(delimiter: char, font: &Cmr10Metrics) -> i64 { } } +/// TeX grows delimiters with the enclosed formula; this is a simple 1x-3x +/// approximation of that rule. +pub fn delimiter_scale(body: &MathBox) -> f64 { + let extent = (body.height + body.depth) as f64 / 65_536.0; + (extent / 9.0).clamp(1.0, 3.0) +} + /// Lay out a math atom at the current style size. pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { match atom { @@ -252,12 +259,13 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { }, MathAtom::Delimited { left, right, body } => { let body_box = layout_math(body, font); + let scale = delimiter_scale(&body_box); MathBox { - width: delimiter_width(*left, font) - + body_box.width - + delimiter_width(*right, font), - height: body_box.height + 2 * 65_536, - depth: body_box.depth + 2 * 65_536, + width: ((delimiter_width(*left, font) + delimiter_width(*right, font)) as f64 + * scale) as i64 + + body_box.width, + height: body_box.height + (2 * 65_536) * scale as i64, + depth: body_box.depth + (2 * 65_536) * scale as i64, } } MathAtom::Accent { base, .. } => { @@ -1421,6 +1429,15 @@ mod tests { let middle = layout_math(&parse_math(r"\left(a\middle\{b\right)"), &font); assert!(middle.width > plain.width); } + + #[test] + fn left_right_delimiters_scale_with_the_body() { + let font = Cmr10Metrics::default(); + let short = layout_math(&parse_math(r"\left(a\right)"), &font); + let tall = layout_math(&parse_math(r"\left(\frac{a}{b}\right)"), &font); + assert!(tall.height > short.height); + assert!(tall.width > short.width); + } #[test] fn left_right_accept_control_sequence_delimiters() { let font = Cmr10Metrics::default(); diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index f4b9f4a..cbb74d4 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -639,22 +639,26 @@ impl Page { } MathAtom::Delimited { left, right, body } => { let body_box = math::layout_math(body, font); + let scale = math::delimiter_scale(&body_box); + let delimiter_size = (size as f64 * scale) as i64; + let left_width = (font.width(*left) as f64 * scale) as i64; + let right_width = (font.width(*right) as f64 * scale) as i64; if *left != '.' { list.push(DisplayItem::Text { font: FontFace::Roman, x, y: baseline, - size, + size: delimiter_size, text: left.to_string(), }); } - Self::paint_math(body, font, x + font.width(*left), baseline, size, list); + Self::paint_math(body, font, x + left_width, baseline, size, list); if *right != '.' { list.push(DisplayItem::Text { font: FontFace::Roman, - x: x + font.width(*left) + body_box.width, + x: x + left_width + body_box.width, y: baseline, - size, + size: delimiter_size, text: right.to_string(), }); }