Advance native LaTeX execution and preview

This commit is contained in:
gloamxun committed 2026-11-18 22:17:21 +08:00
1 parent 91f9d76b16
commit 2741546ca8
414 files changed
+99872 -933

No files matched your search

+255 -98
View File
@@ -32,6 +32,20 @@ fn default_matrix_row_lead() -> i64 {
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum MathAtom {
/// A list-local style declaration, scoped by Row/group boundaries.
Style(crate::math_engine_tex::MathStyle),
Styled {
style: crate::math_engine_tex::MathStyle,
body: Box<MathAtom>,
},
/// Explicit mu glue/kern, in 16.16 math units, resolved at the active style.
MuSpace(i64),
/// Explicit `\limits` remains stacked even in text/script styles.
ForcedLimits {
base: Box<MathAtom>,
sub: Option<Box<MathAtom>>,
sup: Option<Box<MathAtom>>,
},
Symbol(char),
Space(i64),
Row(Vec<MathAtom>),
@@ -205,7 +219,10 @@ pub(crate) fn stack_layout(rows: &[MathAtom], font: &Cmr10Metrics) -> StackLayou
depth: 0,
};
}
let boxes: Vec<MathBox> = rows.iter().map(|row| layout_math(row, font)).collect();
let boxes: Vec<MathBox> = rows
.iter()
.map(|row| legacy_layout_math(row, font))
.collect();
let width = boxes.iter().map(|box_| box_.width).max().unwrap_or(0);
let mut offsets = vec![0_i64; boxes.len()];
let mut offset = 0_i64;
@@ -248,7 +265,7 @@ pub(crate) fn layout_aligned(
.map(|row| {
row.cells
.iter()
.map(|cell| layout_math(cell, font))
.map(|cell| legacy_layout_math(cell, font))
.collect()
})
.collect();
@@ -348,7 +365,9 @@ impl MathAtom {
}
/// Geometry of a math atom in scaled points, measured from its baseline.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(
Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct MathBox {
pub width: i64,
pub height: i64,
@@ -644,7 +663,10 @@ pub(crate) fn multline_layout(
tag: Option<&str>,
font: &Cmr10Metrics,
) -> MultlineLayout {
let boxes: Vec<MathBox> = rows.iter().map(|row| layout_math(row, font)).collect();
let boxes: Vec<MathBox> = rows
.iter()
.map(|row| legacy_layout_math(row, font))
.collect();
let body_width = boxes.iter().map(|row| row.width).max().unwrap_or(0);
let tag_width = tag
.map(|text| text.chars().map(|ch| Cmr10Metrics::Roman.width(ch)).sum())
@@ -694,7 +716,11 @@ pub(crate) fn matrix_layout(
) -> MatrixLayout {
let boxes: Vec<Vec<MathBox>> = rows
.iter()
.map(|row| row.iter().map(|cell| layout_math(cell, font)).collect())
.map(|row| {
row.iter()
.map(|cell| legacy_layout_math(cell, font))
.collect()
})
.collect();
let columns = boxes.iter().map(|row| row.len()).max().unwrap_or(0);
let mut column_widths = vec![0_i64; columns];
@@ -968,12 +994,77 @@ pub(crate) fn intrinsic_depth(atom: &MathAtom, font: &Cmr10Metrics) -> i64 {
MathAtom::Classed { body, .. } => intrinsic_depth(body, font),
MathAtom::Script { base, .. } => intrinsic_depth(base, font),
MathAtom::Limits { base, .. } => intrinsic_depth(base, font),
_ => layout_math(atom, font).depth,
_ => legacy_layout_math(atom, font).depth,
}
}
/// Prepared once at measurement time. Native geometry is consumed directly by
/// both display and inline painters. Unported atom kinds retain Route-D's
/// existing renderer as a whole formula; this never dispatches to v1.
#[derive(Clone, Debug)]
pub struct PreparedMath {
pub metrics: MathBox,
pub geometry: Option<crate::math_engine_tex::MathGeometry>,
}
/// Route-D page coordinates are PDF big points; native geometry is TeX sp.
/// Convert only at the layout/export boundary, preserving the original font
/// master and the original geometry for both painters.
pub(crate) fn math_output_dimension(value: i64) -> i64 {
value * 800 / 803
}
pub fn prepare_math(
atom: &MathAtom,
style: crate::math_engine_tex::MathStyle,
font: &Cmr10Metrics,
) -> PreparedMath {
let mut native_atom = atom.clone();
if let MathAtom::TaggedWidth { width, .. } = &mut native_atom {
// A page measure is already in backend units. Present its TeX-point
// equivalent to the native box builder before centering and tagging.
*width = *width * 803 / 800;
}
match crate::math_engine_tex::layout(&native_atom, style) {
Ok(geometry) => PreparedMath {
metrics: MathBox {
width: math_output_dimension(geometry.metrics.width),
height: math_output_dimension(geometry.metrics.height),
depth: math_output_dimension(geometry.metrics.depth),
},
geometry: Some(geometry),
},
Err(_) => PreparedMath {
metrics: legacy_layout_math(atom, font),
geometry: None,
},
}
}
/// Display-style dimensions, sharing the native geometry builder.
pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
crate::math_engine_tex::layout(atom, crate::math_engine_tex::MathStyle::Display)
.map(|geometry| geometry.metrics)
.unwrap_or_else(|_| legacy_layout_math(atom, font))
}
pub(crate) fn legacy_layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
match atom {
MathAtom::Style(_) => MathBox::default(),
MathAtom::Styled { body, .. } => legacy_layout_math(body, font),
MathAtom::MuSpace(mu) => MathBox {
width: mu * 10 / 18,
height: 0,
depth: 0,
},
MathAtom::ForcedLimits { base, sub, sup } => legacy_layout_math(
&MathAtom::Limits {
base: base.clone(),
sub: sub.clone(),
sup: sup.clone(),
},
font,
),
MathAtom::Symbol(character) => {
let height = font.height(*character);
let depth = font.depth(*character);
@@ -994,7 +1085,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
let mut depth = 0;
let classes = row_classes(atoms);
for (index, atom) in atoms.iter().enumerate() {
let boxed = layout_math(atom, font);
let boxed = legacy_layout_math(atom, font);
if index > 0 {
width += spacing_width(classes[index - 1], classes[index]);
}
@@ -1009,8 +1100,8 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Sqrt { radicand, degree } => {
let boxed = layout_math(radicand, font);
let degree_box = degree.as_deref().map(|atom| layout_math(atom, font));
let boxed = legacy_layout_math(radicand, font);
let degree_box = degree.as_deref().map(|atom| legacy_layout_math(atom, font));
MathBox {
width: boxed.width
+ 4 * 65_536
@@ -1053,7 +1144,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Delimited { left, right, body } => {
let body_box = layout_math(body, font);
let body_box = legacy_layout_math(body, font);
let delta = body_box.height + body_box.depth;
let left_box = select_delimiter(*font, *left, delta);
let right_box = select_delimiter(*font, *right, delta);
@@ -1086,7 +1177,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Accent { base, .. } => {
let base_box = layout_math(base, font);
let base_box = legacy_layout_math(base, font);
MathBox {
width: base_box.width,
height: base_box.height + 2 * 65_536,
@@ -1094,7 +1185,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Smash { body, top, bottom } => {
let body = layout_math(body, font);
let body = legacy_layout_math(body, font);
MathBox {
width: body.width,
height: if *top { 0 } else { body.height },
@@ -1111,7 +1202,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
horizontal,
vertical,
} => {
let body = layout_math(body, font);
let body = legacy_layout_math(body, font);
MathBox {
width: if *horizontal { body.width } else { 0 },
height: if *vertical { body.height } else { 0 },
@@ -1128,13 +1219,13 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Limits { base, sub, sup } => {
let base = layout_math(base, font);
let base = legacy_layout_math(base, font);
let sub = sub
.as_ref()
.map(|atom| script_metrics(layout_math(atom, font)));
.map(|atom| script_metrics(legacy_layout_math(atom, font)));
let sup = sup
.as_ref()
.map(|atom| script_metrics(layout_math(atom, font)));
.map(|atom| script_metrics(legacy_layout_math(atom, font)));
MathBox {
width: base
.width
@@ -1150,7 +1241,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
.map_or(0, |item| item.height + item.depth + 65_536),
}
}
MathAtom::Classed { body, .. } => layout_math(body, font),
MathAtom::Classed { body, .. } => legacy_layout_math(body, font),
MathAtom::SizedDelimiter { delimiter, size } => {
if *delimiter == '.' {
MathBox {
@@ -1178,7 +1269,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
}
MathAtom::Middle(delimiter) => layout_math(&MathAtom::Symbol(*delimiter), font),
MathAtom::Middle(delimiter) => legacy_layout_math(&MathAtom::Symbol(*delimiter), font),
MathAtom::Aligned { rows, centered } => {
let layout = layout_aligned(rows, *centered, font);
MathBox {
@@ -1216,8 +1307,8 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Stack { top, base, above } => {
let top = layout_math(top, font);
let base = layout_math(base, font);
let top = legacy_layout_math(top, font);
let base = legacy_layout_math(base, font);
MathBox {
width: top.width.max(base.width),
height: base.height + if *above { top.height + 65_536 } else { 0 },
@@ -1225,7 +1316,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Brace { base, above } => {
let base_box = layout_math(base, font);
let base_box = legacy_layout_math(base, font);
MathBox {
width: base_box.width.max(4 * 65_536),
height: base_box.height + if *above { 2 * 65_536 } else { 0 },
@@ -1236,8 +1327,8 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
numerator,
denominator,
} => {
let numerator = layout_math(numerator, font);
let denominator = layout_math(denominator, font);
let numerator = legacy_layout_math(numerator, font);
let denominator = legacy_layout_math(denominator, font);
MathBox {
width: numerator.width.max(denominator.width) + 4 * 65_536,
height: numerator.height + denominator.height + 2 * 65_536,
@@ -1245,7 +1336,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Boxed { body } => {
let inner = layout_math(body, font);
let inner = legacy_layout_math(body, font);
MathBox {
width: inner.width + 6 * 65_536,
height: inner.height + 3 * 65_536,
@@ -1253,7 +1344,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Negated { base } => {
let inner = layout_math(base, font);
let inner = legacy_layout_math(base, font);
MathBox {
width: inner.width,
height: inner.height,
@@ -1261,7 +1352,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Cancel { base, .. } => {
let inner = layout_math(base, font);
let inner = legacy_layout_math(base, font);
MathBox {
width: inner.width,
height: inner.height,
@@ -1269,7 +1360,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Underline { base } => {
let base_box = layout_math(base, font);
let base_box = legacy_layout_math(base, font);
MathBox {
width: base_box.width,
height: base_box.height,
@@ -1277,7 +1368,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Tagged { body, tag } => {
let body_box = layout_math(body, font);
let body_box = legacy_layout_math(body, font);
let tag_width: i64 = tag
.chars()
.map(|character| Cmr10Metrics::Roman.width(character))
@@ -1289,7 +1380,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::TaggedWidth { body, tag, width } => {
let body_box = layout_math(body, font);
let body_box = legacy_layout_math(body, font);
let tag_width: i64 = tag
.chars()
.map(|character| Cmr10Metrics::Roman.width(character))
@@ -1301,9 +1392,9 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
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 base_box = legacy_layout_math(base, font);
let sub_box = sub.as_deref().map(|atom| legacy_layout_math(atom, font));
let sup_box = sup.as_deref().map(|atom| legacy_layout_math(atom, font));
let script_width = sub_box
.as_ref()
.map(|boxed| boxed.width)
@@ -1327,8 +1418,8 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
denominator,
..
} => {
let numerator_box = layout_math(numerator, font);
let denominator_box = layout_math(denominator, font);
let numerator_box = legacy_layout_math(numerator, font);
let denominator_box = legacy_layout_math(denominator, font);
let width = numerator_box
.width
.max(denominator_box.width)
@@ -1456,13 +1547,13 @@ fn is_right_command(chars: &[char], index: usize) -> bool {
&& chars.get(index + 5) == Some(&'t')
}
fn limits_override(chars: &[char], index: usize) -> Option<(bool, usize)> {
fn limits_override(chars: &[char], index: usize) -> Option<(i8, usize)> {
let mut cursor = index;
while cursor < chars.len() && (chars[cursor] == '\u{0}' || chars[cursor].is_whitespace()) {
cursor += 1;
}
let rest = chars.get(cursor..)?;
for (word, forced) in [("\\limits", true), ("\\nolimits", false)] {
for (word, forced) in [("\\limits", 1), ("\\nolimits", -1), ("\\displaylimits", 0)] {
let letters: Vec<char> = word.chars().collect();
if rest.starts_with(&letters)
&& !rest
@@ -1527,10 +1618,6 @@ fn delimiter_from_text(text: &str) -> Option<char> {
fn parse_limits(chars: &[char], next: usize, base: MathAtom) -> (MathAtom, usize) {
let mut after = next;
let mut forced = None;
while let Some((value, end)) = limits_override(chars, after) {
forced = Some(value);
after = end;
}
let mut sub = None;
let mut sup = None;
loop {
@@ -1538,6 +1625,11 @@ fn parse_limits(chars: &[char], next: usize, base: MathAtom) -> (MathAtom, usize
while cursor < chars.len() && (chars[cursor].is_whitespace() || chars[cursor] == '\u{0}') {
cursor += 1;
}
if let Some((value, end)) = limits_override(chars, cursor) {
forced = Some(value);
after = end;
continue;
}
match chars.get(cursor) {
Some('_') => {
let (atom, end) = parse_atom(chars, cursor + 1);
@@ -1552,9 +1644,18 @@ fn parse_limits(chars: &[char], next: usize, base: MathAtom) -> (MathAtom, usize
_ => break,
}
}
let stacked = forced.unwrap_or_else(|| operator_default_limits(&base));
let stacked = forced.map_or_else(|| operator_default_limits(&base), |policy| policy >= 0);
if sub.is_none() && sup.is_none() {
(base, after)
} else if forced == Some(1) {
(
MathAtom::ForcedLimits {
base: Box::new(base),
sub,
sup,
},
after,
)
} else if !stacked {
(
MathAtom::Script {
@@ -1868,13 +1969,17 @@ fn scan_math_dimension(chars: &[char], index: usize, muskip: bool) -> Option<(i6
let mut fraction = 0i64;
if chars.get(at) == Some(&'.') {
at += 1;
let mut scale = 65_536i64;
let mut numerator = 0i128;
let mut denominator = 1i128;
while let Some(digit) = chars.get(at).and_then(|ch| ch.to_digit(10)) {
scale /= 10;
fraction += digit as i64 * scale;
if denominator < 1_000_000_000_000_000_000 {
numerator = numerator * 10 + i128::from(digit);
denominator *= 10;
}
at += 1;
digits += 1;
}
fraction = ((numerator * 65_536 + denominator / 2) / denominator) as i64;
}
if digits == 0 {
return None;
@@ -1889,6 +1994,7 @@ fn scan_math_dimension(chars: &[char], index: usize, muskip: bool) -> Option<(i6
}
}
let (numerator, denominator) = match unit.as_str() {
"mu" if muskip => (1, 1),
"mu" => (10, 18),
"pt" => (1, 1),
"bp" => (7227, 7200),
@@ -1901,7 +2007,7 @@ fn scan_math_dimension(chars: &[char], index: usize, muskip: bool) -> Option<(i6
"em" => (10, 1),
"ex" => (5, 1),
"sp" => (1, 65_536),
"" if muskip => (10, 18),
"" if muskip => (1, 1),
"" => (1, 1),
_ => return None,
};
@@ -2039,10 +2145,10 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
}
if command.is_empty() {
let width = match chars.get(next) {
Some(',') => 109_227,
Some(':') | Some('>') => 218_454,
Some(';') => 327_680,
Some('!') => -109_227,
Some(',') => 3 * 65_536,
Some(':') | Some('>') => 4 * 65_536,
Some(';') => 5 * 65_536,
Some('!') => -3 * 65_536,
Some(' ') => 0,
_ => 0,
};
@@ -2050,7 +2156,23 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
chars.get(next),
Some(',') | Some(':') | Some('>') | Some(';') | Some('!')
) {
return (MathAtom::Space(width), next + 1);
return (MathAtom::MuSpace(width), next + 1);
}
}
if command.starts_with("var") {
if let Some(character) = math_symbol_command(&command) {
if matches!(
character,
'Γ' | 'Δ' | 'Θ' | 'Λ' | 'Ξ' | 'Π' | 'Σ' | 'Υ' | 'Φ' | 'Ψ' | 'Ω'
) {
return (
MathAtom::Alphabet {
text: character.to_string(),
alphabet: MathAlphabet::Italic,
},
next,
);
}
}
}
return match command.as_str() {
@@ -2069,11 +2191,11 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
next,
),
"mskip" | "mkern" => match math_space_argument(chars, next, true, false) {
Some((width, after)) => (MathAtom::Space(width), after),
Some((width, after)) => (MathAtom::MuSpace(width), after),
None => (MathAtom::Symbol('?'), next),
},
"mspace" => match math_space_argument(chars, next, true, true) {
Some((width, after)) => (MathAtom::Space(width), after),
Some((width, after)) => (MathAtom::MuSpace(width), after),
None => (MathAtom::Symbol('?'), next),
},
"hskip" | "kern" => match math_space_argument(chars, next, false, false) {
@@ -2096,8 +2218,17 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
"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" => {
"displaystyle" | "textstyle" | "scriptstyle" | "scriptscriptstyle" => {
use crate::math_engine_tex::MathStyle;
let style = match command.as_str() {
"displaystyle" => MathStyle::Display,
"textstyle" => MathStyle::Text,
"scriptstyle" => MathStyle::Script,
_ => MathStyle::ScriptScript,
};
(MathAtom::Style(style), next)
}
"nonumber" | "notag" | "limits" | "nolimits" | "displaylimits" => {
let mut after = next;
while chars.get(after).is_some_and(|ch| ch.is_whitespace()) {
after += 1;
@@ -2142,7 +2273,21 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
"frac" | "tfrac" | "dfrac" | "cfrac" => {
let (numerator, after) = parse_atom(chars, next);
let (denominator, after) = parse_atom(chars, after);
(MathAtom::fraction(numerator, denominator), after)
{
let fraction = MathAtom::fraction(numerator, denominator);
let atom = match command.as_str() {
"tfrac" => MathAtom::Styled {
style: crate::math_engine_tex::MathStyle::Text,
body: Box::new(fraction),
},
"dfrac" | "cfrac" => MathAtom::Styled {
style: crate::math_engine_tex::MathStyle::Display,
body: Box::new(fraction),
},
_ => fraction,
};
(atom, after)
}
}
"sqrt" => {
let mut after = next;
@@ -2611,7 +2756,7 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
let body_start = skip_math_space(chars, next);
let (left, body_start) = parse_delimiter(chars, body_start);
let (body, after) = parse_sequence(chars, body_start, false, true);
let body_box = layout_math(&body, &Cmr10Metrics::default());
let body_box = legacy_layout_math(&body, &Cmr10Metrics::default());
let body = size_middle_delimiters(body, body_box.height + body_box.depth);
let mut after = after;
// Consume `\right` and its delimiter.
@@ -2682,9 +2827,14 @@ mod tests {
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_eq!(
boxed.width,
crate::math_font::MathFontId::new(crate::math_engine_tex::MathFont::Italic, 655_360)
.metrics()
.width('x')
);
assert!(boxed.height > 0);
assert!(boxed.depth > 0);
assert_eq!(boxed.depth, 0);
}
#[test]
@@ -2810,8 +2960,8 @@ mod tests {
#[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);
let plain = legacy_layout_math(&parse_math("x"), &font);
let smashed = legacy_layout_math(&parse_math("\\smash{x}"), &font);
assert_eq!(smashed.width, plain.width);
assert_eq!(smashed.height, 0);
assert_eq!(smashed.depth, 0);
@@ -2829,9 +2979,9 @@ mod tests {
#[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);
let plain = legacy_layout_math(&parse_math("x"), &font);
let horizontal = legacy_layout_math(&parse_math("\\hphantom{x}"), &font);
let vertical = legacy_layout_math(&parse_math("\\vphantom{x}"), &font);
assert_eq!(horizontal.width, plain.width);
assert_eq!(horizontal.height, 0);
assert_eq!(vertical.width, 0);
@@ -2858,8 +3008,8 @@ mod tests {
#[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);
let plain = legacy_layout_math(&parse_math("x"), &font);
let boxed = legacy_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);
@@ -3052,7 +3202,7 @@ mod tests {
assert_eq!(layout.offsets[0], 0);
assert_eq!(
layout.offsets[2],
layout.body_width - layout_math(&rows[2], &font).width
layout.body_width - legacy_layout_math(&rows[2], &font).width
);
assert_eq!(layout.offsets[2], 0);
assert!(layout.offsets[1] > 0);
@@ -3229,9 +3379,9 @@ mod tests {
#[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);
let plain = legacy_layout_math(&parse_math("x"), &font);
let over = legacy_layout_math(&parse_math("\\overset{a}{x}"), &font);
let under = legacy_layout_math(&parse_math("\\underset{a}{x}"), &font);
assert!(over.height > plain.height);
assert_eq!(over.depth, plain.depth);
assert!(under.depth > plain.depth);
@@ -3240,9 +3390,9 @@ mod tests {
#[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);
let over = legacy_layout_math(&parse_math("\\overbrace{x+y}"), &font);
let under = legacy_layout_math(&parse_math("\\underbrace{x+y}"), &font);
let plain = legacy_layout_math(&parse_math("x+y"), &font);
assert!(over.height > plain.height);
assert_eq!(over.depth, plain.depth);
assert!(under.depth > plain.depth);
@@ -3261,9 +3411,9 @@ mod tests {
#[test]
fn cancel_commands_keep_the_base_box() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
let plain = legacy_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);
let cancelled = legacy_layout_math(&parse_math(command), &font);
assert_eq!(cancelled.width, plain.width);
assert_eq!(cancelled.height, plain.height);
}
@@ -3304,10 +3454,10 @@ mod tests {
#[test]
fn left_right_accept_control_sequence_delimiters() {
let font = Cmr10Metrics::default();
let escaped = layout_math(&parse_math(r"\left\{x\right\}"), &font);
let plain = layout_math(&parse_math("x"), &font);
let escaped = legacy_layout_math(&parse_math(r"\left\{x\right\}"), &font);
let plain = legacy_layout_math(&parse_math("x"), &font);
assert!(escaped.width > plain.width);
let empty = layout_math(&parse_math(r"\left.x\right."), &font);
let empty = legacy_layout_math(&parse_math(r"\left.x\right."), &font);
assert_eq!(empty.width, plain.width);
}
#[test]
@@ -3426,7 +3576,7 @@ mod tests {
let MathAtom::Row(atoms) = parse_math(r"\int\limits_{0}^{1}") else {
panic!("expected a row");
};
assert!(matches!(&atoms[0], MathAtom::Limits { .. }));
assert!(matches!(&atoms[0], MathAtom::ForcedLimits { .. }));
let MathAtom::Row(nolimits) = parse_math(r"\sum\nolimits_{i}") else {
panic!("expected a row");
};
@@ -3459,12 +3609,15 @@ mod tests {
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"] {
for command in ["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);
}
let text = layout_math(&parse_math(r"\tfrac{a}{b}"), &font);
assert!(text.width < plain.width);
assert!(text.height < plain.height);
}
#[test]
fn math_spacing_commands_have_expected_widths() {
@@ -3486,8 +3639,8 @@ mod tests {
let thin = layout_math(&parse_math(r"a\mskip 3mu b"), &font).width;
let mspace = layout_math(&parse_math(r"a\mspace{18mu}b"), &font).width;
let em = layout_math(&parse_math(r"a\hspace{1em}b"), &font).width;
assert_eq!(thin - plain, THIN_MU);
assert_eq!(mspace - plain, 655_360);
assert_eq!(thin - plain, 3 * 36_408);
assert_eq!(mspace - plain, 18 * 36_408);
assert_eq!(em - plain, 655_360);
}
@@ -3497,8 +3650,8 @@ mod tests {
let plain = layout_math(&parse_math("ab"), &font).width;
let stretchy = layout_math(&parse_math(r"a\mskip 3mu plus 1mu minus 1mu b"), &font).width;
let tight = layout_math(&parse_math(r"a\mkern-2mu b"), &font).width;
assert_eq!(stretchy - plain, THIN_MU);
assert_eq!(plain - tight, 2 * 655_360 / 18);
assert_eq!(stretchy - plain, 3 * 36_408);
assert_eq!(plain - tight, 2 * 36_408);
}
#[test]
@@ -3506,7 +3659,7 @@ mod tests {
let font = Cmr10Metrics::default();
let mid = layout_math(&parse_math(r"a\mid b"), &font).width;
let bar = layout_math(&parse_math("a|b"), &font).width;
assert_eq!(mid - bar, 2 * THICK_MU);
assert_eq!(mid - bar, 10 * 36_408);
let colon = layout_math(&parse_math(r"a\colon b"), &font).width;
let relation = layout_math(&parse_math("a:b"), &font).width;
assert!(colon < relation, "\\colon is punctuation, not a relation");
@@ -3597,8 +3750,8 @@ mod tests {
#[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);
let plain = legacy_layout_math(&parse_math("x"), &font);
let underlined = legacy_layout_math(&parse_math("\\underline{x}"), &font);
assert_eq!(underlined.width, plain.width);
assert!(underlined.depth > plain.depth);
assert_eq!(underlined.height, plain.height);
@@ -3639,15 +3792,15 @@ mod tests {
#[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);
let dotted = legacy_layout_math(&parse_math("\\left.x\\right|"), &font);
let bare = legacy_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);
let plain = legacy_layout_math(&parse_math("x"), &font);
let accented = legacy_layout_math(&parse_math("\\hat{x}"), &font);
assert_eq!(accented.width, plain.width);
assert!(accented.height > plain.height);
}
@@ -3856,7 +4009,7 @@ mod spacing_class_tests {
fn punctuation_gets_a_thin_space() {
let font = Cmr10Metrics::Math;
let packed = font.width('a') + font.width(',') + font.width('b');
assert_eq!(width("a,b") - packed, THIN_MU);
assert_eq!(width("a,b") - packed, 3 * 36_408);
}
#[test]
@@ -3871,17 +4024,16 @@ mod spacing_class_tests {
#[test]
fn relation_and_binary_spacing_stay_distinct() {
let font = Cmr10Metrics::Math;
let base = font.width('x') + font.width('+') + font.width('y');
assert_eq!(width("x+y") - base, 2 * MED_MU);
let relation = font.width('x') + font.width('=') + font.width('y');
assert_eq!(width("x=y") - relation, 2 * THICK_MU);
let base = width("x") + font.width('+') + width("y");
assert_eq!(width("x+y") - base, 8 * 36_408);
let relation = width("x") + font.width('=') + width("y");
assert_eq!(width("x=y") - relation, 10 * 36_408);
}
#[test]
fn big_operators_are_operator_atoms() {
let font = Cmr10Metrics::Math;
let sum = width(r"\sum");
assert_eq!(width(r"\sum x") - sum - font.width('x'), THIN_MU);
assert_eq!(width(r"\sum x") - sum - width("x"), 3 * 36_408);
}
}
@@ -3943,6 +4095,11 @@ mod tagged_equation_tests {
crate::render::DisplayItem::Text { text: value, y, .. } if value == text => {
Some(*y)
}
crate::render::DisplayItem::MathGlyph { character, y, .. }
if character.to_string() == text =>
{
Some(*y)
}
_ => None,
})
.unwrap_or_else(|| panic!("no {text} glyph"))
@@ -3952,7 +4109,7 @@ mod tagged_equation_tests {
let sup = find("N");
assert!(sub > base, "subscript {sub} above base {base}");
assert!(sup < base, "superscript {sup} below base {base}");
let gap = (sub - base) as f64 / 65_536.0;
let gap = (sub - base) as f64 / 65_536.0 - 9.50006;
assert!(gap < 12.0, "sum limit gap {gap} pt");
}