From c00edaf35705683247ece9b8cad790976d7bdd69 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:55:16 +0800 Subject: [PATCH] Paint binomial parentheses around stacked arguments --- crates/texrra-tex-core/src/lib.rs | 25 +++++++++++++++ crates/texrra-tex-core/src/render.rs | 46 +++++++++++++++++++++------- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index c1461c4..ba22b61 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1838,6 +1838,31 @@ mod tests { assert!(svg.contains("font-family=\"monospace\">face3")); } + #[test] + fn binomial_is_painted_with_parentheses() { + let document = Document::new( + 1000, + vec![Unit::math( + 1, + crate::math::parse_math("\\binom{n}{k}"), + &Cmr10Metrics::default(), + )], + ); + let engine = Engine::layout(&document); + let list = engine.pages()[0].display_list(); + let texts: Vec<_> = list + .items + .iter() + .filter_map(|item| match item { + DisplayItem::Text { text, .. } => Some(text.as_str()), + _ => None, + }) + .collect(); + assert!(texts.contains(&"(")); + assert!(texts.contains(&")")); + assert!(texts.contains(&"n")); + assert!(texts.contains(&"k")); + } #[test] fn sqrt_degree_is_painted_before_the_radical() { let document = Document::new( diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index e3bfca7..3296fa0 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -551,22 +551,31 @@ impl Page { let num = math::layout_math(numerator, font); let den = math::layout_math(denominator, font); let width = num.width.max(den.width); - Self::paint_math( - numerator, - font, - x + (width - num.width) / 2, - baseline - 8 * 65_536, - size, - list, - ); + let bracket = (size as f64 * 1.2) as i64; + let inner_x = x + (width - num.width) / 2 + 2 * 65_536; + Self::paint_math(numerator, font, inner_x, baseline - 8 * 65_536, size, list); Self::paint_math( denominator, font, - x + (width - den.width) / 2, + x + (width - den.width) / 2 + 2 * 65_536, baseline + 12 * 65_536, size, list, ); + list.push(DisplayItem::Text { + font: FontFace::Roman, + x, + y: baseline, + size: bracket, + text: "(".to_string(), + }); + list.push(DisplayItem::Text { + font: FontFace::Roman, + x: x + width + 4 * 65_536, + y: baseline, + size: bracket, + text: ")".to_string(), + }); list.push(DisplayItem::Rule { x, y: baseline - 2 * 65_536, @@ -751,10 +760,11 @@ impl Page { let num = math::layout_math(numerator, font); let den = math::layout_math(denominator, font); let width = num.width.max(den.width); + let bracket = (size as f64 * 1.2) as i64; Self::paint_math( numerator, font, - x + (width - num.width) / 2, + x + (width - num.width) / 2 + 2 * 65_536, baseline - 8 * 65_536, size, list, @@ -762,11 +772,25 @@ impl Page { Self::paint_math( denominator, font, - x + (width - den.width) / 2, + x + (width - den.width) / 2 + 2 * 65_536, baseline + 12 * 65_536, size, list, ); + list.push(DisplayItem::Text { + font: FontFace::Roman, + x, + y: baseline, + size: bracket, + text: "(".to_string(), + }); + list.push(DisplayItem::Text { + font: FontFace::Roman, + x: x + width + 4 * 65_536, + y: baseline, + size: bracket, + text: ")".to_string(), + }); } MathAtom::BigOperator { name } => { for (index, character) in name.chars().enumerate() {