diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index d4d689a..88f67e1 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -27,6 +27,11 @@ pub enum MathAtom { Underline { base: Box, }, + Limits { + base: Box, + sub: Option>, + sup: Option>, + }, Classed { class: MathClass, body: Box, @@ -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(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 17f4118..14f69b8 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::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.