Add explicit math spacing commands
This commit is contained in:
1 parent
5a9571f792
commit
e05caa351b
2 files changed
+38
No files matched your search
@@ -9,6 +9,7 @@ use crate::cmr10::Cmr10Metrics;
|
|||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum MathAtom {
|
pub enum MathAtom {
|
||||||
Symbol(char),
|
Symbol(char),
|
||||||
|
Space(i64),
|
||||||
Row(Vec<MathAtom>),
|
Row(Vec<MathAtom>),
|
||||||
Sqrt {
|
Sqrt {
|
||||||
radicand: Box<MathAtom>,
|
radicand: Box<MathAtom>,
|
||||||
@@ -190,6 +191,11 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|||||||
height: 7 * 65_536,
|
height: 7 * 65_536,
|
||||||
depth: 2 * 65_536,
|
depth: 2 * 65_536,
|
||||||
},
|
},
|
||||||
|
MathAtom::Space(width) => MathBox {
|
||||||
|
width: *width,
|
||||||
|
height: 0,
|
||||||
|
depth: 0,
|
||||||
|
},
|
||||||
MathAtom::Row(atoms) => {
|
MathAtom::Row(atoms) => {
|
||||||
let mut width = 0;
|
let mut width = 0;
|
||||||
let mut height = 0;
|
let mut height = 0;
|
||||||
@@ -556,7 +562,25 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|||||||
break;
|
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() {
|
return match command.as_str() {
|
||||||
|
"quad" => (MathAtom::Space(655_360), next),
|
||||||
|
"qquad" => (MathAtom::Space(1_310_720), next),
|
||||||
"displaystyle" | "textstyle" | "scriptstyle" | "scriptscriptstyle" => {
|
"displaystyle" | "textstyle" | "scriptstyle" | "scriptscriptstyle" => {
|
||||||
let mut after = next;
|
let mut after = next;
|
||||||
while chars.get(after).is_some_and(|ch| ch.is_whitespace()) {
|
while chars.get(after).is_some_and(|ch| ch.is_whitespace()) {
|
||||||
@@ -1223,6 +1247,19 @@ mod tests {
|
|||||||
assert_eq!(under.height, plain.height);
|
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]
|
#[test]
|
||||||
fn math_text_preserves_internal_spaces() {
|
fn math_text_preserves_internal_spaces() {
|
||||||
let font = Cmr10Metrics::default();
|
let font = Cmr10Metrics::default();
|
||||||
|
|||||||
@@ -487,6 +487,7 @@ impl Page {
|
|||||||
list: &mut Vec<DisplayItem>,
|
list: &mut Vec<DisplayItem>,
|
||||||
) {
|
) {
|
||||||
match atom {
|
match atom {
|
||||||
|
MathAtom::Space(_) => {}
|
||||||
MathAtom::Symbol(character) => {
|
MathAtom::Symbol(character) => {
|
||||||
if !character.is_whitespace() {
|
if !character.is_whitespace() {
|
||||||
list.push(DisplayItem::Text {
|
list.push(DisplayItem::Text {
|
||||||
|
|||||||
Reference in new issue
Block a user