1602 lines
55 KiB
Rust
Executable File
1602 lines
55 KiB
Rust
Executable File
//! M4 math list model: symbols, scripts and fractions expressed as boxes.
|
|
//!
|
|
//! This is deliberately independent of v1's math renderer. It provides the
|
|
//! geometry the route-D page builder needs; token-to-atom conversion and the
|
|
//! final painter arrive in later M4 slices.
|
|
|
|
use crate::cmr10::Cmr10Metrics;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub enum MathAtom {
|
|
Symbol(char),
|
|
Space(i64),
|
|
Row(Vec<MathAtom>),
|
|
Sqrt {
|
|
radicand: Box<MathAtom>,
|
|
degree: Option<Box<MathAtom>>,
|
|
},
|
|
Text(String),
|
|
Delimited {
|
|
left: char,
|
|
right: char,
|
|
body: Box<MathAtom>,
|
|
},
|
|
Accent {
|
|
accent: char,
|
|
base: Box<MathAtom>,
|
|
},
|
|
Underline {
|
|
base: Box<MathAtom>,
|
|
},
|
|
Negated {
|
|
base: Box<MathAtom>,
|
|
},
|
|
Cancel {
|
|
base: Box<MathAtom>,
|
|
forward: bool,
|
|
backward: bool,
|
|
},
|
|
Boxed {
|
|
body: Box<MathAtom>,
|
|
},
|
|
BigOperator {
|
|
name: String,
|
|
},
|
|
Smash {
|
|
body: Box<MathAtom>,
|
|
top: bool,
|
|
bottom: bool,
|
|
},
|
|
Phantom {
|
|
body: Box<MathAtom>,
|
|
horizontal: bool,
|
|
vertical: bool,
|
|
},
|
|
Strut,
|
|
Limits {
|
|
base: Box<MathAtom>,
|
|
sub: Option<Box<MathAtom>>,
|
|
sup: Option<Box<MathAtom>>,
|
|
},
|
|
Classed {
|
|
class: MathClass,
|
|
body: Box<MathAtom>,
|
|
},
|
|
SizedDelimiter {
|
|
delimiter: char,
|
|
size: u8,
|
|
},
|
|
StackLines(Vec<MathAtom>),
|
|
Stack {
|
|
top: Box<MathAtom>,
|
|
base: Box<MathAtom>,
|
|
above: bool,
|
|
},
|
|
Brace {
|
|
base: Box<MathAtom>,
|
|
above: bool,
|
|
},
|
|
Binomial {
|
|
numerator: Box<MathAtom>,
|
|
denominator: Box<MathAtom>,
|
|
},
|
|
Script {
|
|
base: Box<MathAtom>,
|
|
sub: Option<Box<MathAtom>>,
|
|
sup: Option<Box<MathAtom>>,
|
|
},
|
|
Fraction {
|
|
numerator: Box<MathAtom>,
|
|
denominator: Box<MathAtom>,
|
|
},
|
|
}
|
|
|
|
impl MathAtom {
|
|
pub fn symbol(character: char) -> Self {
|
|
Self::Symbol(character)
|
|
}
|
|
|
|
pub fn script(base: MathAtom, sub: Option<MathAtom>, sup: Option<MathAtom>) -> Self {
|
|
Self::Script {
|
|
base: Box::new(base),
|
|
sub: sub.map(Box::new),
|
|
sup: sup.map(Box::new),
|
|
}
|
|
}
|
|
|
|
pub fn fraction(numerator: MathAtom, denominator: MathAtom) -> Self {
|
|
Self::Fraction {
|
|
numerator: Box::new(numerator),
|
|
denominator: Box::new(denominator),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Geometry of a math atom in scaled points, measured from its baseline.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct MathBox {
|
|
pub width: i64,
|
|
pub height: i64,
|
|
pub depth: i64,
|
|
}
|
|
|
|
/// TeX math atom classes used for inter-atom spacing.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub enum MathClass {
|
|
Ordinary,
|
|
Binary,
|
|
Relation,
|
|
Open,
|
|
Close,
|
|
Punctuation,
|
|
Inner,
|
|
Op,
|
|
}
|
|
|
|
pub fn math_class(character: char) -> MathClass {
|
|
match character {
|
|
'+' | '-' | '*' | '/' => MathClass::Binary,
|
|
'=' | '<' | '>' | ':' => MathClass::Relation,
|
|
'(' | '[' | '{' => MathClass::Open,
|
|
')' | ']' | '}' => MathClass::Close,
|
|
',' | ';' => MathClass::Punctuation,
|
|
_ => MathClass::Ordinary,
|
|
}
|
|
}
|
|
|
|
/// TeX's spacing table in math units (1mu = 1/18em).
|
|
fn spacing_mu(left: MathClass, right: MathClass) -> i64 {
|
|
use MathClass::*;
|
|
match (left, right) {
|
|
(Binary, Binary)
|
|
| (Relation, Relation)
|
|
| (Open, _)
|
|
| (_, Close)
|
|
| (_, Punctuation)
|
|
| (Punctuation, _) => 0,
|
|
(Ordinary, Binary) | (Binary, Ordinary) => 4,
|
|
(Ordinary, Relation) | (Relation, Ordinary) => 5,
|
|
(Binary, Relation) | (Relation, Binary) => 0,
|
|
(Inner, Inner) => 0,
|
|
(Inner, _) | (_, Inner) => 3,
|
|
(Op, Op) => 0,
|
|
(Op, _) | (_, Op) => 3,
|
|
(Ordinary, Ordinary) => 0,
|
|
_ => 0,
|
|
}
|
|
}
|
|
|
|
fn scaled(value: i64, factor: f64) -> i64 {
|
|
(value as f64 * factor).round() as i64
|
|
}
|
|
|
|
fn atom_class(atom: &MathAtom) -> MathClass {
|
|
match atom {
|
|
MathAtom::Symbol(character) => math_class(*character),
|
|
MathAtom::Classed { class, .. } => *class,
|
|
MathAtom::Row(atoms) => atoms.last().map(atom_class).unwrap_or(MathClass::Ordinary),
|
|
MathAtom::Negated { base } => atom_class(base),
|
|
MathAtom::Cancel { base, .. } => atom_class(base),
|
|
_ => MathClass::Ordinary,
|
|
}
|
|
}
|
|
|
|
fn delimiter_width(delimiter: char, font: &Cmr10Metrics) -> i64 {
|
|
if delimiter == '.' {
|
|
0
|
|
} else {
|
|
font.width(delimiter)
|
|
}
|
|
}
|
|
|
|
/// Lay out a math atom at the current style size.
|
|
pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|
match atom {
|
|
MathAtom::Symbol(character) => MathBox {
|
|
width: font.width(*character),
|
|
height: 7 * 65_536,
|
|
depth: 2 * 65_536,
|
|
},
|
|
MathAtom::Space(width) => MathBox {
|
|
width: *width,
|
|
height: 0,
|
|
depth: 0,
|
|
},
|
|
MathAtom::Row(atoms) => {
|
|
let mut width = 0;
|
|
let mut height = 0;
|
|
let mut depth = 0;
|
|
for (index, atom) in atoms.iter().enumerate() {
|
|
let boxed = layout_math(atom, font);
|
|
if index > 0 {
|
|
let left = atom_class(&atoms[index - 1]);
|
|
let right = atom_class(atom);
|
|
width += spacing_mu(left, right) * 65_536 / 18;
|
|
}
|
|
width += boxed.width;
|
|
height = height.max(boxed.height);
|
|
depth = depth.max(boxed.depth);
|
|
}
|
|
MathBox {
|
|
width,
|
|
height,
|
|
depth,
|
|
}
|
|
}
|
|
MathAtom::Sqrt { radicand, degree } => {
|
|
let boxed = layout_math(radicand, font);
|
|
let degree_box = degree.as_deref().map(|atom| layout_math(atom, font));
|
|
MathBox {
|
|
width: boxed.width
|
|
+ 4 * 65_536
|
|
+ degree_box.as_ref().map_or(0, |boxed| boxed.width),
|
|
height: boxed.height
|
|
+ 2 * 65_536
|
|
+ degree_box.as_ref().map_or(0, |boxed| boxed.height),
|
|
depth: boxed.depth,
|
|
}
|
|
}
|
|
MathAtom::Text(text) => MathBox {
|
|
width: text
|
|
.chars()
|
|
.map(|character| {
|
|
if character == ' ' {
|
|
font.space()
|
|
} else {
|
|
font.width(character)
|
|
}
|
|
})
|
|
.sum(),
|
|
height: 6 * 65_536,
|
|
depth: 2 * 65_536,
|
|
},
|
|
MathAtom::Delimited { left, right, body } => {
|
|
let body_box = layout_math(body, font);
|
|
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,
|
|
}
|
|
}
|
|
MathAtom::Accent { base, .. } => {
|
|
let base_box = layout_math(base, font);
|
|
MathBox {
|
|
width: base_box.width,
|
|
height: base_box.height + 2 * 65_536,
|
|
depth: base_box.depth,
|
|
}
|
|
}
|
|
MathAtom::Smash { body, top, bottom } => {
|
|
let body = layout_math(body, font);
|
|
MathBox {
|
|
width: body.width,
|
|
height: if *top { 0 } else { body.height },
|
|
depth: if *bottom { 0 } else { body.depth },
|
|
}
|
|
}
|
|
MathAtom::Strut => MathBox {
|
|
width: 0,
|
|
height: 7 * 65_536,
|
|
depth: 2 * 65_536,
|
|
},
|
|
MathAtom::Phantom {
|
|
body,
|
|
horizontal,
|
|
vertical,
|
|
} => {
|
|
let body = layout_math(body, font);
|
|
MathBox {
|
|
width: if *horizontal { body.width } else { 0 },
|
|
height: if *vertical { body.height } else { 0 },
|
|
depth: if *vertical { body.depth } else { 0 },
|
|
}
|
|
}
|
|
MathAtom::BigOperator { name } => {
|
|
let (width, height, depth) = match name.as_str() {
|
|
"int" => (8, 10, 4),
|
|
"prod" => (12, 8, 3),
|
|
_ => (12, 8, 3),
|
|
};
|
|
MathBox {
|
|
width: width * 65_536,
|
|
height: height * 65_536,
|
|
depth: depth * 65_536,
|
|
}
|
|
}
|
|
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 {
|
|
1 => 8 * 65_536,
|
|
2 => 11 * 65_536,
|
|
3 => 14 * 65_536,
|
|
_ => 17 * 65_536,
|
|
};
|
|
MathBox {
|
|
width: if *delimiter == '.' {
|
|
0
|
|
} else {
|
|
(extent as f64 * 0.35) as i64
|
|
},
|
|
height: extent,
|
|
depth: 2 * 65_536,
|
|
}
|
|
}
|
|
MathAtom::StackLines(rows) => {
|
|
let boxes: Vec<MathBox> = rows.iter().map(|atom| layout_math(atom, font)).collect();
|
|
MathBox {
|
|
width: boxes.iter().map(|b| b.width).max().unwrap_or(0),
|
|
height: boxes.iter().map(|b| b.height).sum(),
|
|
depth: boxes.last().map_or(0, |b| b.depth),
|
|
}
|
|
}
|
|
MathAtom::Stack { top, base, above } => {
|
|
let top = layout_math(top, font);
|
|
let base = layout_math(base, font);
|
|
MathBox {
|
|
width: top.width.max(base.width),
|
|
height: base.height + if *above { top.height + 65_536 } else { 0 },
|
|
depth: base.depth + if *above { 0 } else { top.height + 65_536 },
|
|
}
|
|
}
|
|
MathAtom::Brace { base, above } => {
|
|
let base_box = layout_math(base, font);
|
|
MathBox {
|
|
width: base_box.width.max(4 * 65_536),
|
|
height: base_box.height + if *above { 2 * 65_536 } else { 0 },
|
|
depth: base_box.depth + if *above { 0 } else { 2 * 65_536 },
|
|
}
|
|
}
|
|
MathAtom::Binomial {
|
|
numerator,
|
|
denominator,
|
|
} => {
|
|
let numerator = layout_math(numerator, font);
|
|
let denominator = layout_math(denominator, font);
|
|
MathBox {
|
|
width: numerator.width.max(denominator.width) + 4 * 65_536,
|
|
height: numerator.height + denominator.height + 2 * 65_536,
|
|
depth: denominator.depth,
|
|
}
|
|
}
|
|
MathAtom::Boxed { body } => {
|
|
let inner = layout_math(body, font);
|
|
MathBox {
|
|
width: inner.width + 6 * 65_536,
|
|
height: inner.height + 3 * 65_536,
|
|
depth: inner.depth + 3 * 65_536,
|
|
}
|
|
}
|
|
MathAtom::Negated { base } => {
|
|
let inner = layout_math(base, font);
|
|
MathBox {
|
|
width: inner.width,
|
|
height: inner.height,
|
|
depth: inner.depth,
|
|
}
|
|
}
|
|
MathAtom::Cancel { base, .. } => {
|
|
let inner = layout_math(base, font);
|
|
MathBox {
|
|
width: inner.width,
|
|
height: inner.height,
|
|
depth: inner.depth,
|
|
}
|
|
}
|
|
MathAtom::Underline { base } => {
|
|
let base_box = layout_math(base, font);
|
|
MathBox {
|
|
width: base_box.width,
|
|
height: base_box.height,
|
|
depth: base_box.depth + 2 * 65_536,
|
|
}
|
|
}
|
|
MathAtom::Script { base, sub, sup } => {
|
|
let base_box = layout_math(base, font);
|
|
let sub_box = sub.as_deref().map(|atom| layout_math(atom, font));
|
|
let sup_box = sup.as_deref().map(|atom| layout_math(atom, font));
|
|
let script_width = sub_box
|
|
.as_ref()
|
|
.map(|boxed| boxed.width)
|
|
.unwrap_or(0)
|
|
.max(sup_box.as_ref().map(|boxed| boxed.width).unwrap_or(0));
|
|
let sup_height = sup_box.as_ref().map_or(0, |boxed| boxed.height);
|
|
let sub_depth = sub_box.as_ref().map_or(0, |boxed| boxed.depth);
|
|
MathBox {
|
|
width: base_box.width + scaled(script_width, 0.7),
|
|
height: base_box
|
|
.height
|
|
.max(scaled(base_box.height, 0.6) + scaled(sup_height, 0.7)),
|
|
depth: base_box
|
|
.depth
|
|
.max(scaled(base_box.depth, 0.5) + scaled(sub_depth, 0.7)),
|
|
}
|
|
}
|
|
MathAtom::Fraction {
|
|
numerator,
|
|
denominator,
|
|
} => {
|
|
let numerator_box = layout_math(numerator, font);
|
|
let denominator_box = layout_math(denominator, font);
|
|
let width = numerator_box
|
|
.width
|
|
.max(denominator_box.width)
|
|
.saturating_add(2 * 65_536);
|
|
MathBox {
|
|
width,
|
|
height: scaled(numerator_box.height + numerator_box.depth, 0.7) + 2 * 65_536,
|
|
depth: scaled(denominator_box.height + denominator_box.depth, 0.7) + 2 * 65_536,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Parse a small TeX math subset into a [`MathAtom`] row.
|
|
///
|
|
/// Supported: plain symbols, `{}` groups, `_`/`^` scripts, `\frac{a}{b}` and
|
|
/// `\sqrt{x}`. Whitespace is ignored. Unknown control words become `?` so a
|
|
/// later M4 parser can replace them.
|
|
pub fn parse_math(input: &str) -> MathAtom {
|
|
let chars: Vec<char> = input
|
|
.chars()
|
|
.map(|ch| if ch.is_whitespace() { '\u{0}' } else { ch })
|
|
.collect();
|
|
let (atom, _) = parse_sequence(&chars, 0, false, false);
|
|
atom
|
|
}
|
|
|
|
fn is_right_command(chars: &[char], index: usize) -> bool {
|
|
chars.get(index) == Some(&'\\')
|
|
&& chars.get(index + 1) == Some(&'r')
|
|
&& chars.get(index + 2) == Some(&'i')
|
|
&& chars.get(index + 3) == Some(&'g')
|
|
&& chars.get(index + 4) == Some(&'h')
|
|
&& 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,
|
|
stop_at_brace: bool,
|
|
stop_at_right: bool,
|
|
) -> (MathAtom, usize) {
|
|
let mut atoms = Vec::new();
|
|
while index < chars.len() {
|
|
if stop_at_brace && chars[index] == '}' {
|
|
break;
|
|
}
|
|
if stop_at_right && is_right_command(chars, index) {
|
|
break;
|
|
}
|
|
let (mut atom, mut next) = parse_atom(chars, index);
|
|
let mut sub = None;
|
|
let mut sup = None;
|
|
loop {
|
|
if next < chars.len() && chars[next] == '_' && sub.is_none() {
|
|
let (script, after) = parse_script(chars, next + 1);
|
|
sub = Some(Box::new(script));
|
|
next = after;
|
|
} else if next < chars.len() && chars[next] == '^' && sup.is_none() {
|
|
let (script, after) = parse_script(chars, next + 1);
|
|
sup = Some(Box::new(script));
|
|
next = after;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if sub.is_some() || sup.is_some() {
|
|
atom = MathAtom::Script {
|
|
base: Box::new(atom),
|
|
sub,
|
|
sup,
|
|
};
|
|
}
|
|
atoms.push(atom);
|
|
index = next;
|
|
}
|
|
(MathAtom::Row(atoms), index)
|
|
}
|
|
|
|
fn accent_atom(chars: &[char], next: usize, accent: char) -> (MathAtom, usize) {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent,
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
|
|
fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|
let mut index = index;
|
|
while chars.get(index) == Some(&'\u{0}') {
|
|
index += 1;
|
|
}
|
|
let Some(&character) = chars.get(index) else {
|
|
return (MathAtom::Row(Vec::new()), index);
|
|
};
|
|
if character == '{' {
|
|
let (atom, next) = parse_sequence(chars, index + 1, true, false);
|
|
let next = if chars.get(next) == Some(&'}') {
|
|
next + 1
|
|
} else {
|
|
next
|
|
};
|
|
return (atom, next);
|
|
}
|
|
if character == '\\' {
|
|
let mut next = index + 1;
|
|
let mut command = String::new();
|
|
while let Some(&ch) = chars.get(next) {
|
|
if ch.is_ascii_alphabetic() {
|
|
command.push(ch);
|
|
next += 1;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if command.is_empty() {
|
|
let width = match chars.get(next) {
|
|
Some(',') => 109_227,
|
|
Some(':') | Some('>') => 218_454,
|
|
Some(';') => 327_680,
|
|
Some('!') => -109_227,
|
|
Some(' ') => 0,
|
|
_ => 0,
|
|
};
|
|
if matches!(
|
|
chars.get(next),
|
|
Some(',') | Some(':') | Some('>') | Some(';') | Some('!')
|
|
) {
|
|
return (MathAtom::Space(width), next + 1);
|
|
}
|
|
}
|
|
return match command.as_str() {
|
|
"quad" => (MathAtom::Space(655_360), next),
|
|
"qquad" => (MathAtom::Space(1_310_720), next),
|
|
"lvert" | "rvert" | "vert" => (MathAtom::Symbol('|'), next),
|
|
"lVert" | "rVert" | "Vert" => (MathAtom::Symbol('\u{2016}'), next),
|
|
"lbrace" => (MathAtom::Symbol('{'), next),
|
|
"rbrace" => (MathAtom::Symbol('}'), next),
|
|
"langle" => (MathAtom::Symbol('\u{27E8}'), next),
|
|
"rangle" => (MathAtom::Symbol('\u{27E9}'), next),
|
|
"lfloor" => (MathAtom::Symbol('\u{230A}'), next),
|
|
"rfloor" => (MathAtom::Symbol('\u{230B}'), next),
|
|
"lceil" => (MathAtom::Symbol('\u{2308}'), next),
|
|
"rceil" => (MathAtom::Symbol('\u{2309}'), next),
|
|
"displaystyle" | "textstyle" | "scriptstyle" | "scriptscriptstyle" | "nonumber"
|
|
| "notag" | "limits" | "nolimits" | "displaylimits" => {
|
|
let mut after = next;
|
|
while chars.get(after).is_some_and(|ch| ch.is_whitespace()) {
|
|
after += 1;
|
|
}
|
|
parse_atom(chars, after)
|
|
}
|
|
"frac" | "tfrac" | "dfrac" | "cfrac" => {
|
|
let (numerator, after) = parse_atom(chars, next);
|
|
let (denominator, after) = parse_atom(chars, after);
|
|
(MathAtom::fraction(numerator, denominator), after)
|
|
}
|
|
"sqrt" => {
|
|
let mut after = next;
|
|
let mut degree = None;
|
|
if chars.get(after) == Some(&'[') {
|
|
let mut close = after + 1;
|
|
while close < chars.len() && chars[close] != ']' {
|
|
close += 1;
|
|
}
|
|
let degree_chars = &chars[after + 1..close.min(chars.len())];
|
|
let (atom, _) = parse_sequence(degree_chars, 0, false, false);
|
|
degree = Some(Box::new(atom));
|
|
after = if close < chars.len() {
|
|
close + 1
|
|
} else {
|
|
close
|
|
};
|
|
}
|
|
let (radicand, after) = parse_atom(chars, after);
|
|
(
|
|
MathAtom::Sqrt {
|
|
radicand: Box::new(radicand),
|
|
degree,
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"hat" | "widehat" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent: '\u{0302}',
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"tilde" | "widetilde" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent: '\u{0303}',
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"smash" => {
|
|
let mut cursor = next;
|
|
while cursor < chars.len() && chars[cursor].is_whitespace() {
|
|
cursor += 1;
|
|
}
|
|
let mut top = true;
|
|
let mut bottom = true;
|
|
if chars.get(cursor) == Some(&'[') {
|
|
let mut end = cursor + 1;
|
|
while end < chars.len() && chars[end] != ']' {
|
|
match chars[end] {
|
|
't' => {
|
|
top = true;
|
|
bottom = false;
|
|
}
|
|
'b' => {
|
|
top = false;
|
|
bottom = true;
|
|
}
|
|
_ => {}
|
|
}
|
|
end += 1;
|
|
}
|
|
cursor = (end + 1).min(chars.len());
|
|
}
|
|
let (body, after) = parse_atom(chars, cursor);
|
|
(
|
|
MathAtom::Smash {
|
|
body: Box::new(body),
|
|
top,
|
|
bottom,
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"mathstrut" => (MathAtom::Strut, next),
|
|
"phantom" | "hphantom" | "vphantom" => {
|
|
let (body, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Phantom {
|
|
body: Box::new(body),
|
|
horizontal: command != "vphantom",
|
|
vertical: command != "hphantom",
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"sum" | "prod" | "int" => {
|
|
let base = MathAtom::BigOperator {
|
|
name: command.clone(),
|
|
};
|
|
parse_limits(chars, next, base)
|
|
}
|
|
"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);
|
|
let class = match command.as_str() {
|
|
"mathop" => MathClass::Op,
|
|
"mathbin" => MathClass::Binary,
|
|
"mathrel" => MathClass::Relation,
|
|
"mathopen" => MathClass::Open,
|
|
"mathclose" => MathClass::Close,
|
|
"mathpunct" => MathClass::Punctuation,
|
|
"mathinner" => MathClass::Inner,
|
|
_ => MathClass::Ordinary,
|
|
};
|
|
let atom = MathAtom::Classed {
|
|
class,
|
|
body: Box::new(body),
|
|
};
|
|
if command == "mathop" {
|
|
parse_limits(chars, after, atom)
|
|
} else {
|
|
(atom, after)
|
|
}
|
|
}
|
|
"big" | "bigl" | "bigr" | "Big" | "Bigl" | "Bigr" | "bigg" | "biggl" | "biggr"
|
|
| "Bigg" | "Biggl" | "Biggr" => {
|
|
let size = match command.as_str() {
|
|
"big" | "bigl" | "bigr" => 1,
|
|
"Big" | "Bigl" | "Bigr" => 2,
|
|
"bigg" | "biggl" | "biggr" => 3,
|
|
_ => 4,
|
|
};
|
|
let mut after = next;
|
|
while after < chars.len() && chars[after].is_whitespace() {
|
|
after += 1;
|
|
}
|
|
let delimiter = chars.get(after).copied().unwrap_or('.');
|
|
(
|
|
MathAtom::SizedDelimiter { delimiter, size },
|
|
(after + 1).min(chars.len()),
|
|
)
|
|
}
|
|
"substack" => {
|
|
let mut after = next;
|
|
if chars.get(after) == Some(&'{') {
|
|
after += 1;
|
|
}
|
|
let start = after;
|
|
while after < chars.len() && chars[after] != '}' {
|
|
after += 1;
|
|
}
|
|
let content: String = chars[start..after].iter().collect();
|
|
let rows = content.split("\\\\").map(parse_math).collect();
|
|
(MathAtom::StackLines(rows), (after + 1).min(chars.len()))
|
|
}
|
|
"overset" | "underset" | "stackrel" => {
|
|
let (top, after) = parse_atom(chars, next);
|
|
let (base, after) = parse_atom(chars, after);
|
|
(
|
|
MathAtom::Stack {
|
|
top: Box::new(top),
|
|
base: Box::new(base),
|
|
above: command != "underset",
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"overbrace" | "underbrace" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
parse_limits(
|
|
chars,
|
|
after,
|
|
MathAtom::Brace {
|
|
base: Box::new(base),
|
|
above: command == "overbrace",
|
|
},
|
|
)
|
|
}
|
|
"binom" | "dbinom" | "tbinom" => {
|
|
let (numerator, after) = parse_atom(chars, next);
|
|
let (denominator, after) = parse_atom(chars, after);
|
|
(
|
|
MathAtom::Binomial {
|
|
numerator: Box::new(numerator),
|
|
denominator: Box::new(denominator),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"underline" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Underline {
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"cancel" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Cancel {
|
|
base: Box::new(base),
|
|
forward: true,
|
|
backward: false,
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"bcancel" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Cancel {
|
|
base: Box::new(base),
|
|
forward: false,
|
|
backward: true,
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"xcancel" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Cancel {
|
|
base: Box::new(base),
|
|
forward: true,
|
|
backward: true,
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"dot" => accent_atom(chars, next, '\u{0307}'),
|
|
"ddot" => accent_atom(chars, next, '\u{0308}'),
|
|
"check" => accent_atom(chars, next, '\u{030C}'),
|
|
"breve" => accent_atom(chars, next, '\u{0306}'),
|
|
"acute" => accent_atom(chars, next, '\u{0301}'),
|
|
"grave" => accent_atom(chars, next, '\u{0300}'),
|
|
"bar" | "overline" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent: '\u{0305}',
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"overrightarrow" | "overleftarrow" | "overleftrightarrow" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
let accent = match command.as_str() {
|
|
"overrightarrow" => '\u{20D7}',
|
|
"overleftarrow" => '\u{20D6}',
|
|
_ => '\u{20E1}',
|
|
};
|
|
(
|
|
MathAtom::Accent {
|
|
accent,
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"mathring" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent: '\u{030A}',
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"widecheck" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent: '\u{030C}',
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"dddot" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent: '\u{20DB}',
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"vec" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Accent {
|
|
accent: '\u{20D7}',
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"middle" => {
|
|
let delimiter = chars.get(next).copied().unwrap_or('|');
|
|
(MathAtom::Symbol(delimiter), next + 1)
|
|
}
|
|
"max" | "min" | "log" | "ln" | "sin" | "cos" | "tan" | "cot" | "sec" | "csc"
|
|
| "exp" | "det" | "dim" | "ker" | "hom" | "arg" | "deg" | "gcd" => {
|
|
(MathAtom::Text(command.clone()), next)
|
|
}
|
|
"mathcal" | "mathbb" | "mathbf" | "mathit" | "mathsf" | "mathtt" | "mathfrak"
|
|
| "mathnormal" => {
|
|
let mut after = next;
|
|
if chars.get(after) == Some(&'{') {
|
|
after += 1;
|
|
}
|
|
let start = after;
|
|
while after < chars.len() && chars[after] != '}' {
|
|
after += 1;
|
|
}
|
|
let text: String = chars[start..after]
|
|
.iter()
|
|
.map(|ch| if *ch == '\u{0}' { ' ' } else { *ch })
|
|
.collect();
|
|
if after < chars.len() {
|
|
after += 1;
|
|
}
|
|
(MathAtom::Text(text), after)
|
|
}
|
|
"not" => {
|
|
let (base, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Negated {
|
|
base: Box::new(base),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"bmod" => (MathAtom::Text("mod".to_owned()), next),
|
|
"pmod" => {
|
|
let (inner, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Classed {
|
|
class: MathClass::Relation,
|
|
body: Box::new(MathAtom::Row(vec![
|
|
MathAtom::Text("(mod ".to_owned()),
|
|
inner,
|
|
MathAtom::Text(")".to_owned()),
|
|
])),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"boxed" => {
|
|
let (body, after) = parse_atom(chars, next);
|
|
(
|
|
MathAtom::Boxed {
|
|
body: Box::new(body),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
"operatorname" => {
|
|
let mut after = next;
|
|
let starred = chars.get(after) == Some(&'*');
|
|
if starred {
|
|
after += 1;
|
|
}
|
|
if chars.get(after) == Some(&'{') {
|
|
after += 1;
|
|
}
|
|
let start = after;
|
|
while after < chars.len() && chars[after] != '}' {
|
|
after += 1;
|
|
}
|
|
let text: String = chars[start..after]
|
|
.iter()
|
|
.map(|ch| if *ch == '\u{0}' { ' ' } else { *ch })
|
|
.collect();
|
|
if after < chars.len() {
|
|
after += 1;
|
|
}
|
|
let atom = MathAtom::Text(text);
|
|
if starred {
|
|
parse_limits(chars, after, atom)
|
|
} else {
|
|
(atom, after)
|
|
}
|
|
}
|
|
"text" | "mathrm" => {
|
|
let mut after = next;
|
|
if chars.get(after) == Some(&'{') {
|
|
after += 1;
|
|
}
|
|
let start = after;
|
|
while after < chars.len() && chars[after] != '}' {
|
|
after += 1;
|
|
}
|
|
let text: String = chars[start..after].iter().collect();
|
|
if after < chars.len() {
|
|
after += 1;
|
|
}
|
|
(MathAtom::Text(text), after)
|
|
}
|
|
"left" => {
|
|
let left = chars.get(next).copied().unwrap_or('(');
|
|
let (body, after) = parse_sequence(chars, next + 1, false, true);
|
|
let mut after = after;
|
|
// Consume `\right` and its delimiter.
|
|
if is_right_command(chars, after) {
|
|
after += 6;
|
|
}
|
|
let right = chars.get(after).copied().unwrap_or(')');
|
|
if chars.get(after).is_some() {
|
|
after += 1;
|
|
}
|
|
(
|
|
MathAtom::Delimited {
|
|
left,
|
|
right,
|
|
body: Box::new(body),
|
|
},
|
|
after,
|
|
)
|
|
}
|
|
_ => (MathAtom::Symbol('?'), next),
|
|
};
|
|
}
|
|
(MathAtom::Symbol(character), index + 1)
|
|
}
|
|
|
|
fn parse_script(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|
if chars.get(index) == Some(&'{') {
|
|
parse_atom(chars, index)
|
|
} else if chars.get(index) == Some(&'\\') {
|
|
parse_atom(chars, index)
|
|
} else if let Some(&character) = chars.get(index) {
|
|
(MathAtom::Symbol(character), index + 1)
|
|
} else {
|
|
(MathAtom::Row(Vec::new()), index)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn math_symbol_uses_font_width() {
|
|
let font = Cmr10Metrics::default();
|
|
let boxed = layout_math(&MathAtom::symbol('x'), &font);
|
|
assert_eq!(boxed.width, font.width('x'));
|
|
assert!(boxed.height > 0);
|
|
assert!(boxed.depth > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn script_stacks_above_and_below_the_base() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&MathAtom::symbol('x'), &font);
|
|
let scripted = layout_math(
|
|
&MathAtom::script(
|
|
MathAtom::symbol('x'),
|
|
Some(MathAtom::symbol('i')),
|
|
Some(MathAtom::symbol('2')),
|
|
),
|
|
&font,
|
|
);
|
|
assert!(scripted.width > plain.width);
|
|
assert!(scripted.height > plain.height);
|
|
assert!(scripted.depth >= plain.depth);
|
|
}
|
|
|
|
#[test]
|
|
fn fraction_is_wider_than_its_parts() {
|
|
let font = Cmr10Metrics::default();
|
|
let numerator = layout_math(&MathAtom::symbol('a'), &font);
|
|
let fraction = layout_math(
|
|
&MathAtom::fraction(MathAtom::symbol('a'), MathAtom::symbol('b')),
|
|
&font,
|
|
);
|
|
assert!(fraction.width > numerator.width);
|
|
assert!(fraction.height > numerator.height);
|
|
assert!(fraction.depth > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn sqrt_degree_is_wider_than_a_plain_root() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("\\sqrt{x}"), &font);
|
|
let indexed = layout_math(&parse_math("\\sqrt[3]{x}"), &font);
|
|
assert!(indexed.width > plain.width);
|
|
assert!(indexed.height > plain.height);
|
|
}
|
|
|
|
#[test]
|
|
fn mathcal_keeps_character_widths() {
|
|
let font = Cmr10Metrics::default();
|
|
let calligraphic = layout_math(&parse_math("\\mathcal{N}"), &font);
|
|
let upright = layout_math(&parse_math("\\mathrm{N}"), &font);
|
|
assert_eq!(calligraphic.width, upright.width);
|
|
assert_eq!(calligraphic.height, upright.height);
|
|
}
|
|
|
|
#[test]
|
|
fn smash_can_target_only_the_top_or_bottom() {
|
|
let font = Cmr10Metrics::default();
|
|
let top = layout_math(&parse_math("\\smash[t]{x}"), &font);
|
|
let bottom = layout_math(&parse_math("\\smash[b]{x}"), &font);
|
|
assert_eq!(top.height, 0);
|
|
assert!(top.depth > 0);
|
|
assert!(bottom.height > 0);
|
|
assert_eq!(bottom.depth, 0);
|
|
}
|
|
#[test]
|
|
fn smash_removes_height_but_keeps_width() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
let smashed = layout_math(&parse_math("\\smash{x}"), &font);
|
|
assert_eq!(smashed.width, plain.width);
|
|
assert_eq!(smashed.height, 0);
|
|
assert_eq!(smashed.depth, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn mathstrut_has_zero_width_and_math_height() {
|
|
let font = Cmr10Metrics::default();
|
|
let box_ = layout_math(&parse_math(r"\mathstrut"), &font);
|
|
assert_eq!(box_.width, 0);
|
|
assert_eq!(box_.height, 7 * 65_536);
|
|
assert_eq!(box_.depth, 2 * 65_536);
|
|
}
|
|
|
|
#[test]
|
|
fn phantoms_keep_only_selected_dimensions() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
let horizontal = layout_math(&parse_math("\\hphantom{x}"), &font);
|
|
let vertical = layout_math(&parse_math("\\vphantom{x}"), &font);
|
|
assert_eq!(horizontal.width, plain.width);
|
|
assert_eq!(horizontal.height, 0);
|
|
assert_eq!(vertical.width, 0);
|
|
assert_eq!(vertical.height, plain.height);
|
|
}
|
|
#[test]
|
|
fn big_operators_are_wider_than_letters() {
|
|
let font = Cmr10Metrics::default();
|
|
let sum = layout_math(&parse_math("\\sum"), &font);
|
|
let letter = layout_math(&parse_math("s"), &font);
|
|
assert!(sum.width > letter.width);
|
|
assert!(sum.height > letter.height);
|
|
assert!(sum.depth > letter.depth);
|
|
}
|
|
#[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 boxed_pads_its_body_on_all_sides() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
let boxed = layout_math(&parse_math(r"\boxed{x}"), &font);
|
|
assert_eq!(boxed.width, plain.width + 6 * 65_536);
|
|
assert_eq!(boxed.height, plain.height + 3 * 65_536);
|
|
assert_eq!(boxed.depth, plain.depth + 3 * 65_536);
|
|
}
|
|
|
|
#[test]
|
|
fn mathop_uses_the_operator_spacing_class() {
|
|
let MathAtom::Row(atoms) = parse_math(r"\mathop{x}") else {
|
|
panic!("expected row")
|
|
};
|
|
let MathAtom::Classed { class, .. } = &atoms[0] else {
|
|
panic!("expected classed")
|
|
};
|
|
assert_eq!(*class, MathClass::Op);
|
|
}
|
|
#[test]
|
|
fn mathinner_adds_three_mu_on_both_sides() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("axb"), &font);
|
|
let inner = layout_math(&parse_math(r"a\mathinner{x}b"), &font);
|
|
assert!(inner.width > plain.width + 10_000);
|
|
}
|
|
#[test]
|
|
fn mathrel_wrapper_forces_relation_spacing() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("a+b"), &font);
|
|
let relation = layout_math(&parse_math("a \\mathrel{+} b"), &font);
|
|
assert!(relation.width > plain.width);
|
|
}
|
|
#[test]
|
|
fn big_delimiters_get_progressively_taller() {
|
|
let font = Cmr10Metrics::default();
|
|
let big = layout_math(&parse_math("\\big("), &font);
|
|
let bigg = layout_math(&parse_math("\\Bigg("), &font);
|
|
assert!(bigg.height > big.height);
|
|
assert!(bigg.width > big.width);
|
|
}
|
|
#[test]
|
|
fn tall_delimiters_grow_with_their_body() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("\\left(a\\right)"), &font);
|
|
let tall = layout_math(&parse_math("\\left(\\frac{a}{b}\\right)"), &font);
|
|
assert!(tall.height > plain.height);
|
|
assert!(tall.width > plain.width);
|
|
}
|
|
|
|
#[test]
|
|
fn overleftrightarrow_uses_the_double_arrow_accent() {
|
|
let MathAtom::Row(atoms) = parse_math(r"\overleftrightarrow{AB}") else {
|
|
panic!("expected row")
|
|
};
|
|
assert!(matches!(
|
|
&atoms[0],
|
|
MathAtom::Accent {
|
|
accent: '\u{20E1}',
|
|
..
|
|
}
|
|
));
|
|
}
|
|
#[test]
|
|
fn mathring_uses_the_ring_accent() {
|
|
let MathAtom::Row(atoms) = parse_math(r"\mathring{x}") else {
|
|
panic!("expected row")
|
|
};
|
|
assert!(matches!(
|
|
&atoms[0],
|
|
MathAtom::Accent {
|
|
accent: '\u{030A}',
|
|
..
|
|
}
|
|
));
|
|
}
|
|
#[test]
|
|
fn substack_stacks_multiple_rows() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("a"), &font);
|
|
let stacked = layout_math(&parse_math(r"\substack{a\\b}"), &font);
|
|
assert!(stacked.height > plain.height);
|
|
}
|
|
#[test]
|
|
fn overbrace_accepts_a_superscript_label() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math(r"\overbrace{x}"), &font);
|
|
let labeled = layout_math(&parse_math(r"\overbrace{x}^{n}"), &font);
|
|
assert!(labeled.height > plain.height);
|
|
}
|
|
#[test]
|
|
fn widecheck_and_dddot_use_their_accent_codepoints() {
|
|
let MathAtom::Row(check) = parse_math(r"\widecheck{x}") else {
|
|
panic!("expected row")
|
|
};
|
|
assert!(matches!(
|
|
&check[0],
|
|
MathAtom::Accent {
|
|
accent: '\u{030C}',
|
|
..
|
|
}
|
|
));
|
|
let MathAtom::Row(dots) = parse_math(r"\dddot{x}") else {
|
|
panic!("expected row")
|
|
};
|
|
assert!(matches!(
|
|
&dots[0],
|
|
MathAtom::Accent {
|
|
accent: '\u{20DB}',
|
|
..
|
|
}
|
|
));
|
|
}
|
|
#[test]
|
|
fn overrightarrow_uses_the_arrow_accent() {
|
|
let parsed = parse_math(r"\overrightarrow{AB}");
|
|
let MathAtom::Row(atoms) = parsed else {
|
|
panic!("expected row")
|
|
};
|
|
assert!(matches!(
|
|
&atoms[0],
|
|
MathAtom::Accent {
|
|
accent: '\u{20D7}',
|
|
..
|
|
}
|
|
));
|
|
}
|
|
#[test]
|
|
fn stackrel_places_its_first_argument_above_the_relation() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("="), &font);
|
|
let stacked = layout_math(&parse_math(r"\stackrel{def}{=}"), &font);
|
|
assert!(stacked.height > plain.height);
|
|
}
|
|
#[test]
|
|
fn overset_and_underset_stack_their_arguments() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
let over = layout_math(&parse_math("\\overset{a}{x}"), &font);
|
|
let under = layout_math(&parse_math("\\underset{a}{x}"), &font);
|
|
assert!(over.height > plain.height);
|
|
assert_eq!(over.depth, plain.depth);
|
|
assert!(under.depth > plain.depth);
|
|
assert_eq!(under.height, plain.height);
|
|
}
|
|
#[test]
|
|
fn braces_add_height_or_depth() {
|
|
let font = Cmr10Metrics::default();
|
|
let over = layout_math(&parse_math("\\overbrace{x+y}"), &font);
|
|
let under = layout_math(&parse_math("\\underbrace{x+y}"), &font);
|
|
let plain = layout_math(&parse_math("x+y"), &font);
|
|
assert!(over.height > plain.height);
|
|
assert_eq!(over.depth, plain.depth);
|
|
assert!(under.depth > plain.depth);
|
|
assert_eq!(under.height, plain.height);
|
|
}
|
|
|
|
#[test]
|
|
fn numbering_control_words_are_transparent() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x+y"), &font);
|
|
let numbered = layout_math(&parse_math(r"x\nonumber+y"), &font);
|
|
assert_eq!(plain.width, numbered.width);
|
|
assert_eq!(plain.height, numbered.height);
|
|
}
|
|
|
|
#[test]
|
|
fn cancel_commands_keep_the_base_box() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
for command in [r"\cancel{x}", r"\bcancel{x}", r"\xcancel{x}"] {
|
|
let cancelled = layout_math(&parse_math(command), &font);
|
|
assert_eq!(cancelled.width, plain.width);
|
|
assert_eq!(cancelled.height, plain.height);
|
|
}
|
|
}
|
|
#[test]
|
|
fn dot_and_check_accents_raise_the_math_box() {
|
|
let font = Cmr10Metrics::default();
|
|
for command in [r"\dot{x}", r"\ddot{x}", r"\check{x}", r"\breve{x}"] {
|
|
let accented = layout_math(&parse_math(command), &font);
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
assert!(accented.height > plain.height);
|
|
}
|
|
}
|
|
#[test]
|
|
fn fence_delimiters_have_widths() {
|
|
let font = Cmr10Metrics::default();
|
|
for source in [r"\lvert x \rvert", r"\lfloor x \rfloor", r"\lceil x \rceil"] {
|
|
let boxed = layout_math(&parse_math(source), &font);
|
|
assert!(boxed.width > layout_math(&parse_math(r"x"), &font).width);
|
|
}
|
|
}
|
|
#[test]
|
|
fn pmod_wraps_the_argument_and_bmod_stays_inline() {
|
|
let font = Cmr10Metrics::default();
|
|
let pmod = layout_math(&parse_math(r"a\pmod{m}"), &font);
|
|
assert!(pmod.width > layout_math(&parse_math(r"a"), &font).width);
|
|
assert!(matches!(
|
|
&parse_math(r"\bmod"),
|
|
MathAtom::Row(atoms) if matches!(&atoms[0], MathAtom::Text(text) if text == "mod")
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn mathop_takes_limits_below() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math(r"\mathop{argmax}"), &font);
|
|
let limited = layout_math(&parse_math(r"\mathop{argmax}_{x}"), &font);
|
|
assert!(limited.depth > plain.depth);
|
|
}
|
|
#[test]
|
|
fn starred_operatorname_takes_limits_below() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math(r"\operatorname*{argmax}"), &font);
|
|
let limited = layout_math(&parse_math(r"\operatorname*{argmax}_{x}"), &font);
|
|
assert!(limited.depth > plain.depth);
|
|
assert!(limited.width >= plain.width);
|
|
}
|
|
#[test]
|
|
fn operatorname_star_does_not_emit_a_symbol() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math(r"\operatorname{argmax}"), &font);
|
|
let starred = layout_math(&parse_math(r"\operatorname*{argmax}"), &font);
|
|
assert_eq!(plain.width, starred.width);
|
|
assert_eq!(plain.height, starred.height);
|
|
}
|
|
#[test]
|
|
fn operatorname_becomes_upright_text() {
|
|
let font = Cmr10Metrics::default();
|
|
let long = layout_math(&parse_math(r"\operatorname{argmax}"), &font);
|
|
let short = layout_math(&parse_math(r"\operatorname{arg}"), &font);
|
|
assert!(long.width > short.width);
|
|
}
|
|
#[test]
|
|
fn fraction_style_aliases_match_frac() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math(r"\frac{a}{b}"), &font);
|
|
for command in ["tfrac", "dfrac", "cfrac"] {
|
|
let source = format!(r"\{command}{{a}}{{b}}");
|
|
let boxed = layout_math(&parse_math(&source), &font);
|
|
assert_eq!(plain.width, boxed.width);
|
|
assert_eq!(plain.height, boxed.height);
|
|
}
|
|
}
|
|
#[test]
|
|
fn math_spacing_commands_have_expected_widths() {
|
|
let font = Cmr10Metrics::default();
|
|
let thin = layout_math(&parse_math(r"a\,b"), &font);
|
|
let medium = layout_math(&parse_math(r"a\;b"), &font);
|
|
let quad = layout_math(&parse_math(r"a\quad b"), &font);
|
|
let double = layout_math(&parse_math(r"a\qquad b"), &font);
|
|
let plain = layout_math(&parse_math("ab"), &font);
|
|
assert!(thin.width > plain.width);
|
|
assert!(medium.width > thin.width);
|
|
assert!(quad.width > medium.width);
|
|
assert!(double.width > quad.width);
|
|
}
|
|
#[test]
|
|
fn math_text_preserves_internal_spaces() {
|
|
let font = Cmr10Metrics::default();
|
|
let spaced = layout_math(&parse_math(r"\text{a b}"), &font);
|
|
let joined = layout_math(&parse_math(r"\text{ab}"), &font);
|
|
assert!(spaced.width > joined.width);
|
|
}
|
|
#[test]
|
|
fn math_style_commands_are_transparent() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
let styled = layout_math(&parse_math(r"\displaystyle x"), &font);
|
|
assert_eq!(plain.width, styled.width);
|
|
assert_eq!(plain.height, styled.height);
|
|
}
|
|
#[test]
|
|
fn binom_stacks_two_arguments() {
|
|
let font = Cmr10Metrics::default();
|
|
let binom = layout_math(&parse_math("\\binom{n}{k}"), &font);
|
|
let single = layout_math(&parse_math("n"), &font);
|
|
assert!(binom.width >= single.width);
|
|
assert!(binom.height > single.height);
|
|
assert!(binom.depth > 0);
|
|
}
|
|
#[test]
|
|
fn named_operators_use_upright_text_widths() {
|
|
let font = Cmr10Metrics::default();
|
|
let max = layout_math(&parse_math("\\max"), &font);
|
|
let single = layout_math(&parse_math("m"), &font);
|
|
assert!(max.width > single.width);
|
|
assert!(matches!(
|
|
&parse_math("\\max"),
|
|
MathAtom::Row(atoms) if matches!(&atoms[0], MathAtom::Text(text) if text == "max")
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn pmod_wraps_its_argument_in_a_mod_row() {
|
|
let parsed = parse_math(r"\pmod{n}");
|
|
let MathAtom::Row(atoms) = parsed else {
|
|
panic!("expected row")
|
|
};
|
|
assert!(matches!(
|
|
&atoms[0],
|
|
MathAtom::Classed {
|
|
class: MathClass::Relation,
|
|
..
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn negated_atom_keeps_its_base_geometry() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("="), &font);
|
|
let negated = layout_math(&parse_math("\\not="), &font);
|
|
assert_eq!(negated.width, plain.width);
|
|
assert!(matches!(
|
|
&parse_math("\\not="),
|
|
MathAtom::Row(atoms) if matches!(&atoms[0], MathAtom::Negated { .. })
|
|
));
|
|
}
|
|
#[test]
|
|
fn underline_adds_depth_without_changing_width() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
let underlined = layout_math(&parse_math("\\underline{x}"), &font);
|
|
assert_eq!(underlined.width, plain.width);
|
|
assert!(underlined.depth > plain.depth);
|
|
assert_eq!(underlined.height, plain.height);
|
|
}
|
|
#[test]
|
|
fn math_text_uses_upright_character_widths() {
|
|
let font = Cmr10Metrics::default();
|
|
let atom = parse_math("\\text{PSNR}");
|
|
let MathAtom::Row(atoms) = &atom else {
|
|
panic!("expected a row");
|
|
};
|
|
let MathAtom::Text(text) = &atoms[0] else {
|
|
panic!("expected a text atom");
|
|
};
|
|
assert_eq!(text, "PSNR");
|
|
let text_box = layout_math(&atom, &font);
|
|
let symbol = layout_math(&parse_math("P"), &font);
|
|
assert!(text_box.width > symbol.width);
|
|
assert!(text_box.height > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn middle_delimiter_adds_a_symbol() {
|
|
let font = Cmr10Metrics::default();
|
|
let without = layout_math(&parse_math("\\left(ab\\right)"), &font);
|
|
let with = layout_math(&parse_math("\\left(a\\middle|b\\right)"), &font);
|
|
assert!(with.width > without.width);
|
|
}
|
|
#[test]
|
|
fn empty_left_delimiter_has_no_width() {
|
|
let font = Cmr10Metrics::default();
|
|
let dotted = layout_math(&parse_math("\\left.x\\right|"), &font);
|
|
let bare = layout_math(&parse_math("x|"), &font);
|
|
assert_eq!(dotted.width, bare.width);
|
|
}
|
|
#[test]
|
|
fn accents_add_height_without_changing_width() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("x"), &font);
|
|
let accented = layout_math(&parse_math("\\hat{x}"), &font);
|
|
assert_eq!(accented.width, plain.width);
|
|
assert!(accented.height > plain.height);
|
|
}
|
|
#[test]
|
|
fn left_right_delimiters_grow_with_their_body() {
|
|
let font = Cmr10Metrics::default();
|
|
let atom = parse_math("\\left(\\frac{a}{b}\\right)");
|
|
let boxed = layout_math(&atom, &font);
|
|
let plain = layout_math(&parse_math("(a)"), &font);
|
|
assert!(matches!(atom, MathAtom::Row(_)));
|
|
assert!(boxed.height > plain.height);
|
|
}
|
|
#[test]
|
|
fn operator_spacing_separates_binary_and_relation_atoms() {
|
|
let font = Cmr10Metrics::default();
|
|
let plain = layout_math(&parse_math("xy"), &font);
|
|
let binary = layout_math(&parse_math("x+y"), &font);
|
|
let relation = layout_math(&parse_math("x=y"), &font);
|
|
assert!(binary.width > plain.width);
|
|
assert!(relation.width > binary.width);
|
|
}
|
|
#[test]
|
|
fn parse_script_attaches_to_the_previous_atom() {
|
|
let atom = parse_math("x^2");
|
|
let MathAtom::Row(atoms) = atom else {
|
|
panic!("expected a row");
|
|
};
|
|
assert!(matches!(&atoms[0], MathAtom::Script { sup: Some(_), .. }));
|
|
}
|
|
|
|
#[test]
|
|
fn parse_fraction_and_sqrt() {
|
|
let atom = parse_math("\\frac{a}{b} + \\sqrt{x}");
|
|
let MathAtom::Row(atoms) = atom else {
|
|
panic!("expected a row");
|
|
};
|
|
assert!(matches!(&atoms[0], MathAtom::Fraction { .. }));
|
|
assert!(matches!(&atoms[2], MathAtom::Sqrt { .. }));
|
|
}
|
|
}
|