Space math atoms with the full TeX class table

This commit is contained in:
gloamxun committed 2026-10-24 01:51:10 +08:00
1 parent f7e89c78a2
commit 3cbfdabe03
3 files changed
+138 -22

No files matched your search

+135 -19
View File
@@ -144,28 +144,98 @@ pub fn math_class(character: char) -> MathClass {
}
}
/// TeX's spacing table in math units (1mu = 1/18em).
fn spacing_mu(left: MathClass, right: MathClass) -> i64 {
/// Width of one math unit at 10pt (TeX's `\thinmuskip` is 3mu).
const THIN_MU: i64 = 3 * 65_536 / 18;
/// Width of `\medmuskip` (4mu plus 2mu minus 4mu at 10pt).
const MED_MU: i64 = 4 * 65_536 / 18;
/// Width of `\thickmuskip` (5mu plus 5mu at 10pt).
const THICK_MU: i64 = 5 * 65_536 / 18;
/// TeX's inter-atom spacing table.
///
/// The result is the number of math units (0, thin, medium or thick) inserted
/// between two atoms of the given classes. Entries that TeX marks as
/// impossible cannot occur after `row_classes` normalises binary operators.
fn spacing_level(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,
(Ordinary, Ordinary) | (Ordinary, Open) | (Ordinary, Close) | (Ordinary, Punctuation) => 0,
(Ordinary, Op) | (Ordinary, Inner) => 1,
(Ordinary, Binary) => 2,
(Ordinary, Relation) => 3,
(Op, Ordinary) | (Op, Op) | (Op, Inner) => 1,
(Op, Binary) => 2,
(Op, Relation) => 3,
(Op, Open) | (Op, Close) | (Op, Punctuation) => 0,
(Binary, Ordinary) | (Binary, Op) | (Binary, Open) | (Binary, Inner) => 2,
(Binary, Binary) | (Binary, Relation) | (Binary, Close) | (Binary, Punctuation) => 0,
(Relation, Ordinary) | (Relation, Op) | (Relation, Open) | (Relation, Inner) => 3,
(Relation, Binary) | (Relation, Relation) | (Relation, Close) | (Relation, Punctuation) => {
0
}
(Open, Ordinary)
| (Open, Op)
| (Open, Binary)
| (Open, Relation)
| (Open, Open)
| (Open, Close)
| (Open, Punctuation)
| (Open, Inner) => 0,
(Close, Ordinary) | (Close, Open) | (Close, Close) | (Close, Punctuation) => 0,
(Close, Op) | (Close, Inner) => 1,
(Close, Binary) => 2,
(Close, Relation) => 3,
(Punctuation, Ordinary)
| (Punctuation, Op)
| (Punctuation, Binary)
| (Punctuation, Relation)
| (Punctuation, Open)
| (Punctuation, Close)
| (Punctuation, Punctuation)
| (Punctuation, Inner) => 1,
(Inner, Ordinary) | (Inner, Op) | (Inner, Open) | (Inner, Punctuation) => 1,
(Inner, Binary) => 2,
(Inner, Relation) | (Inner, Inner) => 3,
(Inner, Close) => 0,
}
}
fn spacing_width(left: MathClass, right: MathClass) -> i64 {
match spacing_level(left, right) {
1 => THIN_MU,
2 => MED_MU,
3 => THICK_MU,
_ => 0,
}
}
/// Classify the atoms of one math list and apply TeX's binary-operator
/// normalisation: a `\mathbin` that starts the list, follows an operator-like
/// atom, or precedes a relation/close/punctuation becomes ordinary.
fn row_classes(atoms: &[MathAtom]) -> Vec<MathClass> {
use MathClass::*;
let original: Vec<MathClass> = atoms.iter().map(atom_class).collect();
let mut classes = original.clone();
for (index, class) in original.iter().enumerate() {
if *class != Binary {
continue;
}
let previous_is_operand = index > 0;
let previous_forbids = index == 0
|| matches!(
original[index - 1],
Binary | Op | Relation | Open | Punctuation
);
let next_forbids = index + 1 == original.len()
|| matches!(original[index + 1], Relation | Close | Punctuation);
if previous_forbids || next_forbids {
classes[index] = Ordinary;
}
let _ = previous_is_operand;
}
classes
}
fn scaled(value: i64, factor: f64) -> i64 {
(value as f64 * factor).round() as i64
}
@@ -207,6 +277,11 @@ fn atom_class(atom: &MathAtom) -> MathClass {
MathAtom::Row(atoms) => atoms.last().map(atom_class).unwrap_or(MathClass::Ordinary),
MathAtom::Negated { base } => atom_class(base),
MathAtom::Cancel { base, .. } => atom_class(base),
MathAtom::BigOperator { .. } => MathClass::Op,
MathAtom::Limits { base, .. } => atom_class(base),
MathAtom::Fraction { .. } | MathAtom::Binomial { .. } | MathAtom::Delimited { .. } => {
MathClass::Inner
}
_ => MathClass::Ordinary,
}
}
@@ -303,12 +378,11 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
let mut width = 0;
let mut height = 0;
let mut depth = 0;
let classes = row_classes(atoms);
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 += spacing_width(classes[index - 1], classes[index]);
}
width += boxed.width;
height = height.max(boxed.height);
@@ -1976,3 +2050,45 @@ mod delimiter_selection_tests {
assert!(tall.width > plain.width);
}
}
#[cfg(test)]
mod spacing_class_tests {
use super::*;
use crate::cmr10::Cmr10Metrics;
fn width(source: &str) -> i64 {
layout_math(&parse_math(source), &Cmr10Metrics::Math).width
}
#[test]
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, 3 * 65_536 / 18);
}
#[test]
fn binary_operators_at_list_edges_become_ordinary() {
let font = Cmr10Metrics::Math;
let plus = font.width('+');
let a = font.width('a');
assert_eq!(width("+a"), plus + a);
assert_eq!(width("a+"), a + plus);
}
#[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 * (4 * 65_536 / 18));
let relation = font.width('x') + font.width('=') + font.width('y');
assert_eq!(width("x=y") - relation, 2 * (5 * 65_536 / 18));
}
#[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'), 3 * 65_536 / 18);
}
}