Paint binomial parentheses around stacked arguments

This commit is contained in:
gloamxun committed 2026-10-20 18:55:16 +08:00
1 parent 0446a35b8c
commit c00edaf357
2 files changed
+60 -11

No files matched your search

+25
View File
@@ -1838,6 +1838,31 @@ mod tests {
assert!(svg.contains("font-family=\"monospace\">face3")); 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] #[test]
fn sqrt_degree_is_painted_before_the_radical() { fn sqrt_degree_is_painted_before_the_radical() {
let document = Document::new( let document = Document::new(
+35 -11
View File
@@ -551,22 +551,31 @@ impl Page {
let num = math::layout_math(numerator, font); let num = math::layout_math(numerator, font);
let den = math::layout_math(denominator, font); let den = math::layout_math(denominator, font);
let width = num.width.max(den.width); let width = num.width.max(den.width);
Self::paint_math( let bracket = (size as f64 * 1.2) as i64;
numerator, let inner_x = x + (width - num.width) / 2 + 2 * 65_536;
font, Self::paint_math(numerator, font, inner_x, baseline - 8 * 65_536, size, list);
x + (width - num.width) / 2,
baseline - 8 * 65_536,
size,
list,
);
Self::paint_math( Self::paint_math(
denominator, denominator,
font, font,
x + (width - den.width) / 2, x + (width - den.width) / 2 + 2 * 65_536,
baseline + 12 * 65_536, baseline + 12 * 65_536,
size, size,
list, 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 { list.push(DisplayItem::Rule {
x, x,
y: baseline - 2 * 65_536, y: baseline - 2 * 65_536,
@@ -751,10 +760,11 @@ impl Page {
let num = math::layout_math(numerator, font); let num = math::layout_math(numerator, font);
let den = math::layout_math(denominator, font); let den = math::layout_math(denominator, font);
let width = num.width.max(den.width); let width = num.width.max(den.width);
let bracket = (size as f64 * 1.2) as i64;
Self::paint_math( Self::paint_math(
numerator, numerator,
font, font,
x + (width - num.width) / 2, x + (width - num.width) / 2 + 2 * 65_536,
baseline - 8 * 65_536, baseline - 8 * 65_536,
size, size,
list, list,
@@ -762,11 +772,25 @@ impl Page {
Self::paint_math( Self::paint_math(
denominator, denominator,
font, font,
x + (width - den.width) / 2, x + (width - den.width) / 2 + 2 * 65_536,
baseline + 12 * 65_536, baseline + 12 * 65_536,
size, size,
list, 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 } => { MathAtom::BigOperator { name } => {
for (index, character) in name.chars().enumerate() { for (index, character) in name.chars().enumerate() {