Support explicit math atom classes
This commit is contained in:
1 parent
3fb2b57f21
commit
4702bf6ceb
2 files changed
+37
-1
No files matched your search
@@ -27,6 +27,10 @@ pub enum MathAtom {
|
|||||||
Underline {
|
Underline {
|
||||||
base: Box<MathAtom>,
|
base: Box<MathAtom>,
|
||||||
},
|
},
|
||||||
|
Classed {
|
||||||
|
class: MathClass,
|
||||||
|
body: Box<MathAtom>,
|
||||||
|
},
|
||||||
SizedDelimiter {
|
SizedDelimiter {
|
||||||
delimiter: char,
|
delimiter: char,
|
||||||
size: u8,
|
size: u8,
|
||||||
@@ -85,7 +89,7 @@ pub struct MathBox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// TeX math atom classes used for inter-atom spacing.
|
/// 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 {
|
pub enum MathClass {
|
||||||
Ordinary,
|
Ordinary,
|
||||||
Binary,
|
Binary,
|
||||||
@@ -131,6 +135,7 @@ fn scaled(value: i64, factor: f64) -> i64 {
|
|||||||
fn atom_class(atom: &MathAtom) -> MathClass {
|
fn atom_class(atom: &MathAtom) -> MathClass {
|
||||||
match atom {
|
match atom {
|
||||||
MathAtom::Symbol(character) => math_class(*character),
|
MathAtom::Symbol(character) => math_class(*character),
|
||||||
|
MathAtom::Classed { class, .. } => *class,
|
||||||
MathAtom::Row(atoms) => atoms.last().map(atom_class).unwrap_or(MathClass::Ordinary),
|
MathAtom::Row(atoms) => atoms.last().map(atom_class).unwrap_or(MathClass::Ordinary),
|
||||||
_ => MathClass::Ordinary,
|
_ => MathClass::Ordinary,
|
||||||
}
|
}
|
||||||
@@ -218,6 +223,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|||||||
depth: base_box.depth,
|
depth: base_box.depth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MathAtom::Classed { body, .. } => layout_math(body, font),
|
||||||
MathAtom::SizedDelimiter { delimiter, size } => {
|
MathAtom::SizedDelimiter { delimiter, size } => {
|
||||||
let extent = match *size {
|
let extent = match *size {
|
||||||
1 => 8 * 65_536,
|
1 => 8 * 65_536,
|
||||||
@@ -451,6 +457,26 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|||||||
after,
|
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"
|
"big" | "bigl" | "bigr" | "Big" | "Bigl" | "Bigr" | "bigg" | "biggl" | "biggr"
|
||||||
| "Bigg" | "Biggl" | "Biggr" => {
|
| "Bigg" | "Biggl" | "Biggr" => {
|
||||||
let size = match command.as_str() {
|
let size = match command.as_str() {
|
||||||
@@ -645,6 +671,13 @@ mod tests {
|
|||||||
assert!(indexed.height > plain.height);
|
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]
|
#[test]
|
||||||
fn big_delimiters_get_progressively_taller() {
|
fn big_delimiters_get_progressively_taller() {
|
||||||
let font = Cmr10Metrics::default();
|
let font = Cmr10Metrics::default();
|
||||||
|
|||||||
@@ -110,6 +110,9 @@ a single cost function.
|
|||||||
- `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`,
|
- `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`,
|
||||||
`\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds
|
`\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds
|
||||||
accent height without changing the base width.
|
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
|
- `MathAtom::SizedDelimiter { delimiter, size }` parses
|
||||||
`\big`/`\Big`/`\bigg`/`\Bigg` (and `l`/`r` variants); extents are 8/11/14/17pt.
|
`\big`/`\Big`/`\bigg`/`\Bigg` (and `l`/`r` variants); extents are 8/11/14/17pt.
|
||||||
- `MathAtom::Stack { top, base, above }` parses `\overset`/`\underset`;
|
- `MathAtom::Stack { top, base, above }` parses `\overset`/`\underset`;
|
||||||
|
|||||||
Reference in new issue
Block a user