Support explicit math atom classes

This commit is contained in:
gloamxun committed 2026-10-20 13:05:00 +08:00
1 parent 3fb2b57f21
commit 4702bf6ceb
2 files changed
+37 -1

No files matched your search

+34 -1
View File
@@ -27,6 +27,10 @@ pub enum MathAtom {
Underline {
base: Box<MathAtom>,
},
Classed {
class: MathClass,
body: Box<MathAtom>,
},
SizedDelimiter {
delimiter: char,
size: u8,
@@ -85,7 +89,7 @@ pub struct MathBox {
}
/// TeX math atom classes used for inter-atom spacing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum MathClass {
Ordinary,
Binary,
@@ -131,6 +135,7 @@ fn scaled(value: i64, factor: f64) -> 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),
_ => MathClass::Ordinary,
}
@@ -218,6 +223,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth,
}
}
MathAtom::Classed { body, .. } => layout_math(body, font),
MathAtom::SizedDelimiter { delimiter, size } => {
let extent = match *size {
1 => 8 * 65_536,
@@ -451,6 +457,26 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"mathord" | "mathop" | "mathbin" | "mathrel" | "mathopen" | "mathclose"
| "mathpunct" | "mathinner" => {
let (body, after) = parse_atom(chars, next);
let class = match command.as_str() {
"mathop" => MathClass::Ordinary,
"mathbin" => MathClass::Binary,
"mathrel" => MathClass::Relation,
"mathopen" => MathClass::Open,
"mathclose" => MathClass::Close,
"mathpunct" => MathClass::Punctuation,
_ => MathClass::Ordinary,
};
(
MathAtom::Classed {
class,
body: Box::new(body),
},
after,
)
}
"big" | "bigl" | "bigr" | "Big" | "Bigl" | "Bigr" | "bigg" | "biggl" | "biggr"
| "Bigg" | "Biggl" | "Biggr" => {
let size = match command.as_str() {
@@ -645,6 +671,13 @@ mod tests {
assert!(indexed.height > plain.height);
}
#[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();
+3
View File
@@ -110,6 +110,9 @@ a single cost function.
- `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`,
`\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds
accent height without changing the base width.
- `MathAtom::Classed { class, body }` implements `\mathord`, `\mathop`,
`\mathbin`, `\mathrel`, `\mathopen`, `\mathclose`, `\mathpunct` and
`\mathinner` spacing overrides.
- `MathAtom::SizedDelimiter { delimiter, size }` parses
`\big`/`\Big`/`\bigg`/`\Bigg` (and `l`/`r` variants); extents are 8/11/14/17pt.
- `MathAtom::Stack { top, base, above }` parses `\overset`/`\underset`;