Add overset and underset math atoms
This commit is contained in:
1 parent
4e892005cd
commit
15741c0b9c
2 files changed
+39
No files matched your search
@@ -27,6 +27,11 @@ pub enum MathAtom {
|
|||||||
Underline {
|
Underline {
|
||||||
base: Box<MathAtom>,
|
base: Box<MathAtom>,
|
||||||
},
|
},
|
||||||
|
Stack {
|
||||||
|
top: Box<MathAtom>,
|
||||||
|
base: Box<MathAtom>,
|
||||||
|
above: bool,
|
||||||
|
},
|
||||||
Brace {
|
Brace {
|
||||||
base: Box<MathAtom>,
|
base: Box<MathAtom>,
|
||||||
above: bool,
|
above: bool,
|
||||||
@@ -209,6 +214,15 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|||||||
depth: base_box.depth,
|
depth: base_box.depth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MathAtom::Stack { top, base, above } => {
|
||||||
|
let top = layout_math(top, font);
|
||||||
|
let base = layout_math(base, font);
|
||||||
|
MathBox {
|
||||||
|
width: top.width.max(base.width),
|
||||||
|
height: base.height + if *above { top.height + 65_536 } else { 0 },
|
||||||
|
depth: base.depth + if *above { 0 } else { top.height + 65_536 },
|
||||||
|
}
|
||||||
|
}
|
||||||
MathAtom::Brace { base, above } => {
|
MathAtom::Brace { base, above } => {
|
||||||
let base_box = layout_math(base, font);
|
let base_box = layout_math(base, font);
|
||||||
MathBox {
|
MathBox {
|
||||||
@@ -416,6 +430,18 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|||||||
after,
|
after,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
"overset" | "underset" => {
|
||||||
|
let (top, after) = parse_atom(chars, next);
|
||||||
|
let (base, after) = parse_atom(chars, after);
|
||||||
|
(
|
||||||
|
MathAtom::Stack {
|
||||||
|
top: Box::new(top),
|
||||||
|
base: Box::new(base),
|
||||||
|
above: command == "overset",
|
||||||
|
},
|
||||||
|
after,
|
||||||
|
)
|
||||||
|
}
|
||||||
"overbrace" | "underbrace" => {
|
"overbrace" | "underbrace" => {
|
||||||
let (base, after) = parse_atom(chars, next);
|
let (base, after) = parse_atom(chars, next);
|
||||||
(
|
(
|
||||||
@@ -580,6 +606,17 @@ mod tests {
|
|||||||
assert!(indexed.height > plain.height);
|
assert!(indexed.height > plain.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn overset_and_underset_stack_their_arguments() {
|
||||||
|
let font = Cmr10Metrics::default();
|
||||||
|
let plain = layout_math(&parse_math("x"), &font);
|
||||||
|
let over = layout_math(&parse_math("\\overset{a}{x}"), &font);
|
||||||
|
let under = layout_math(&parse_math("\\underset{a}{x}"), &font);
|
||||||
|
assert!(over.height > plain.height);
|
||||||
|
assert_eq!(over.depth, plain.depth);
|
||||||
|
assert!(under.depth > plain.depth);
|
||||||
|
assert_eq!(under.height, plain.height);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn braces_add_height_or_depth() {
|
fn braces_add_height_or_depth() {
|
||||||
let font = Cmr10Metrics::default();
|
let font = Cmr10Metrics::default();
|
||||||
|
|||||||
@@ -110,6 +110,8 @@ 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::Stack { top, base, above }` parses `\overset`/`\underset`;
|
||||||
|
the decoration contributes 1pt of extra separation.
|
||||||
- `MathAtom::Brace { base, above }` parses `\overbrace`/`\underbrace` and
|
- `MathAtom::Brace { base, above }` parses `\overbrace`/`\underbrace` and
|
||||||
adds 2pt of height or depth.
|
adds 2pt of height or depth.
|
||||||
- `MathAtom::Binomial { numerator, denominator }` parses
|
- `MathAtom::Binomial { numerator, denominator }` parses
|
||||||
|
|||||||
Reference in new issue
Block a user