Place big operator limits above and below

This commit is contained in:
gloamxun committed 2026-10-20 13:05:36 +08:00
1 parent 4702bf6ceb
commit 6465915dc1
2 files changed
+69

No files matched your search

+67
View File
@@ -27,6 +27,11 @@ pub enum MathAtom {
Underline {
base: Box<MathAtom>,
},
Limits {
base: Box<MathAtom>,
sub: Option<Box<MathAtom>>,
sup: Option<Box<MathAtom>>,
},
Classed {
class: MathClass,
body: Box<MathAtom>,
@@ -223,6 +228,19 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth,
}
}
MathAtom::Limits { base, sub, sup } => {
let base = layout_math(base, font);
let sub = sub.as_ref().map(|atom| layout_math(atom, font));
let sup = sup.as_ref().map(|atom| layout_math(atom, font));
MathBox {
width: base
.width
.max(sub.as_ref().map_or(0, |item| item.width))
.max(sup.as_ref().map_or(0, |item| item.width)),
height: base.height + sup.as_ref().map_or(0, |item| item.height + 65_536),
depth: base.depth + sub.as_ref().map_or(0, |item| item.height + 65_536),
}
}
MathAtom::Classed { body, .. } => layout_math(body, font),
MathAtom::SizedDelimiter { delimiter, size } => {
let extent = match *size {
@@ -338,6 +356,43 @@ fn is_right_command(chars: &[char], index: usize) -> bool {
&& chars.get(index + 5) == Some(&'t')
}
fn parse_limits(chars: &[char], next: usize, base: MathAtom) -> (MathAtom, usize) {
let mut after = next;
let mut sub = None;
let mut sup = None;
loop {
let mut cursor = after;
while cursor < chars.len() && chars[cursor].is_whitespace() {
cursor += 1;
}
match chars.get(cursor) {
Some('_') => {
let (atom, end) = parse_atom(chars, cursor + 1);
sub = Some(Box::new(atom));
after = end;
}
Some('^') => {
let (atom, end) = parse_atom(chars, cursor + 1);
sup = Some(Box::new(atom));
after = end;
}
_ => break,
}
}
if sub.is_none() && sup.is_none() {
(base, after)
} else {
(
MathAtom::Limits {
base: Box::new(base),
sub,
sup,
},
after,
)
}
}
fn parse_sequence(
chars: &[char],
mut index: usize,
@@ -457,6 +512,10 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"sum" | "prod" | "int" | "lim" | "sup" | "inf" => {
let base = MathAtom::Text(command.clone());
parse_limits(chars, next, base)
}
"mathord" | "mathop" | "mathbin" | "mathrel" | "mathopen" | "mathclose"
| "mathpunct" | "mathinner" => {
let (body, after) = parse_atom(chars, next);
@@ -671,6 +730,14 @@ mod tests {
assert!(indexed.height > plain.height);
}
#[test]
fn sum_places_scripts_above_and_below() {
let font = Cmr10Metrics::default();
let sum = layout_math(&parse_math("\\sum_{i=1}^{n}"), &font);
let plain = layout_math(&parse_math("sum"), &font);
assert!(sum.height > plain.height);
assert!(sum.depth > plain.depth);
}
#[test]
fn mathrel_wrapper_forces_relation_spacing() {
let font = Cmr10Metrics::default();
+2
View File
@@ -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::Limits { base, sub, sup }` gives `\sum`, `\prod`, `\int`,
`\lim`, `\sup`, `\inf` above/below scripts.
- `MathAtom::Classed { class, body }` implements `\mathord`, `\mathop`,
`\mathbin`, `\mathrel`, `\mathopen`, `\mathclose`, `\mathpunct` and
`\mathinner` spacing overrides.