Add explicit math spacing commands

This commit is contained in:
gloamxun committed 2026-10-20 19:03:07 +08:00
1 parent 5a9571f792
commit e05caa351b
2 files changed
+38

No files matched your search

+37
View File
@@ -9,6 +9,7 @@ use crate::cmr10::Cmr10Metrics;
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum MathAtom {
Symbol(char),
Space(i64),
Row(Vec<MathAtom>),
Sqrt {
radicand: Box<MathAtom>,
@@ -190,6 +191,11 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
height: 7 * 65_536,
depth: 2 * 65_536,
},
MathAtom::Space(width) => MathBox {
width: *width,
height: 0,
depth: 0,
},
MathAtom::Row(atoms) => {
let mut width = 0;
let mut height = 0;
@@ -556,7 +562,25 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
break;
}
}
if command.is_empty() {
let width = match chars.get(next) {
Some(',') => 109_227,
Some(':') | Some('>') => 218_454,
Some(';') => 327_680,
Some('!') => -109_227,
Some(' ') => 0,
_ => 0,
};
if matches!(
chars.get(next),
Some(',') | Some(':') | Some('>') | Some(';') | Some('!')
) {
return (MathAtom::Space(width), next + 1);
}
}
return match command.as_str() {
"quad" => (MathAtom::Space(655_360), next),
"qquad" => (MathAtom::Space(1_310_720), next),
"displaystyle" | "textstyle" | "scriptstyle" | "scriptscriptstyle" => {
let mut after = next;
while chars.get(after).is_some_and(|ch| ch.is_whitespace()) {
@@ -1223,6 +1247,19 @@ mod tests {
assert_eq!(under.height, plain.height);
}
#[test]
fn math_spacing_commands_have_expected_widths() {
let font = Cmr10Metrics::default();
let thin = layout_math(&parse_math(r"a\,b"), &font);
let medium = layout_math(&parse_math(r"a\;b"), &font);
let quad = layout_math(&parse_math(r"a\quad b"), &font);
let double = layout_math(&parse_math(r"a\qquad b"), &font);
let plain = layout_math(&parse_math("ab"), &font);
assert!(thin.width > plain.width);
assert!(medium.width > thin.width);
assert!(quad.width > medium.width);
assert!(double.width > quad.width);
}
#[test]
fn math_text_preserves_internal_spaces() {
let font = Cmr10Metrics::default();
+1
View File
@@ -487,6 +487,7 @@ impl Page {
list: &mut Vec<DisplayItem>,
) {
match atom {
MathAtom::Space(_) => {}
MathAtom::Symbol(character) => {
if !character.is_whitespace() {
list.push(DisplayItem::Text {