Add a math atom box model

This commit is contained in:
gloamxun committed 2026-10-20 12:52:33 +08:00
1 parent 9a7404c717
commit cd6baba53f
3 files changed
+167 -1

No files matched your search

+146
View File
@@ -0,0 +1,146 @@
//! 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, serde::Serialize, serde::Deserialize)]
pub enum MathAtom {
Symbol(char),
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,
}
fn scaled(value: i64, factor: f64) -> i64 {
(value as f64 * factor).round() as i64
}
/// 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::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,
}
}
}
}
#[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);
}
}