diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 05c48cf..a7ceaa7 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -51,10 +51,58 @@ pub struct MathBox { pub depth: i64, } +/// TeX math atom classes used for inter-atom spacing. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum MathClass { + Ordinary, + Binary, + Relation, + Open, + Close, + Punctuation, +} + +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, + (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::Row(atoms) => atoms.last().map(atom_class).unwrap_or(MathClass::Ordinary), + _ => MathClass::Ordinary, + } +} + /// Lay out a math atom at the current style size. pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { match atom { @@ -67,8 +115,13 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { let mut width = 0; let mut height = 0; let mut depth = 0; - for atom in 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 += boxed.width; height = height.max(boxed.height); depth = depth.max(boxed.depth); @@ -268,6 +321,15 @@ mod tests { assert!(fraction.depth > 0); } + #[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"); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 917de85..3dbaefc 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -104,6 +104,8 @@ overfull ratio 4.11, badness 6942. TeX rejects the underfull candidate under its tolerance but still emits the moderately overfull 5-word line. Reproducing this needs a two-pass tolerance algorithm with an emergency overfull pass, not a single cost function. +- `MathClass` and TeX's 0/3/4/5mu inter-atom table add spacing for + binary/relation/open/close/punctuation atoms in `MathAtom::Row`. - `parse_math` converts a small TeX math subset (symbols, `{}` groups, `_`/`^` scripts, `\frac`, `\sqrt`) into `MathAtom::Row`. - `MathAtom::{Symbol, Script, Fraction}` and `layout_math` produce