Parse sqrt degrees and empty delimiters
This commit is contained in:
1 parent
63927110d9
commit
6769a6e80b
2 files changed
+67
-9
No files matched your search
@@ -10,7 +10,10 @@ use crate::cmr10::Cmr10Metrics;
|
|||||||
pub enum MathAtom {
|
pub enum MathAtom {
|
||||||
Symbol(char),
|
Symbol(char),
|
||||||
Row(Vec<MathAtom>),
|
Row(Vec<MathAtom>),
|
||||||
Sqrt(Box<MathAtom>),
|
Sqrt {
|
||||||
|
radicand: Box<MathAtom>,
|
||||||
|
degree: Option<Box<MathAtom>>,
|
||||||
|
},
|
||||||
Delimited {
|
Delimited {
|
||||||
left: char,
|
left: char,
|
||||||
right: char,
|
right: char,
|
||||||
@@ -112,6 +115,14 @@ fn atom_class(atom: &MathAtom) -> MathClass {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn delimiter_width(delimiter: char, font: &Cmr10Metrics) -> i64 {
|
||||||
|
if delimiter == '.' {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
font.width(delimiter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Lay out a math atom at the current style size.
|
/// Lay out a math atom at the current style size.
|
||||||
pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
||||||
match atom {
|
match atom {
|
||||||
@@ -141,18 +152,25 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|||||||
depth,
|
depth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MathAtom::Sqrt(atom) => {
|
MathAtom::Sqrt { radicand, degree } => {
|
||||||
let boxed = layout_math(atom, font);
|
let boxed = layout_math(radicand, font);
|
||||||
|
let degree_box = degree.as_deref().map(|atom| layout_math(atom, font));
|
||||||
MathBox {
|
MathBox {
|
||||||
width: boxed.width + 4 * 65_536,
|
width: boxed.width
|
||||||
height: boxed.height + 2 * 65_536,
|
+ 4 * 65_536
|
||||||
|
+ degree_box.as_ref().map_or(0, |boxed| boxed.width),
|
||||||
|
height: boxed.height
|
||||||
|
+ 2 * 65_536
|
||||||
|
+ degree_box.as_ref().map_or(0, |boxed| boxed.height),
|
||||||
depth: boxed.depth,
|
depth: boxed.depth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MathAtom::Delimited { left, right, body } => {
|
MathAtom::Delimited { left, right, body } => {
|
||||||
let body_box = layout_math(body, font);
|
let body_box = layout_math(body, font);
|
||||||
MathBox {
|
MathBox {
|
||||||
width: font.width(*left) + body_box.width + font.width(*right),
|
width: delimiter_width(*left, font)
|
||||||
|
+ body_box.width
|
||||||
|
+ delimiter_width(*right, font),
|
||||||
height: body_box.height + 2 * 65_536,
|
height: body_box.height + 2 * 65_536,
|
||||||
depth: body_box.depth + 2 * 65_536,
|
depth: body_box.depth + 2 * 65_536,
|
||||||
}
|
}
|
||||||
@@ -299,8 +317,30 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|||||||
(MathAtom::fraction(numerator, denominator), after)
|
(MathAtom::fraction(numerator, denominator), after)
|
||||||
}
|
}
|
||||||
"sqrt" => {
|
"sqrt" => {
|
||||||
let (radicand, after) = parse_atom(chars, next);
|
let mut after = next;
|
||||||
(MathAtom::Sqrt(Box::new(radicand)), after)
|
let mut degree = None;
|
||||||
|
if chars.get(after) == Some(&'[') {
|
||||||
|
let mut close = after + 1;
|
||||||
|
while close < chars.len() && chars[close] != ']' {
|
||||||
|
close += 1;
|
||||||
|
}
|
||||||
|
let degree_chars = &chars[after + 1..close.min(chars.len())];
|
||||||
|
let (atom, _) = parse_sequence(degree_chars, 0, false, false);
|
||||||
|
degree = Some(Box::new(atom));
|
||||||
|
after = if close < chars.len() {
|
||||||
|
close + 1
|
||||||
|
} else {
|
||||||
|
close
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let (radicand, after) = parse_atom(chars, after);
|
||||||
|
(
|
||||||
|
MathAtom::Sqrt {
|
||||||
|
radicand: Box::new(radicand),
|
||||||
|
degree,
|
||||||
|
},
|
||||||
|
after,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
"hat" | "widehat" => {
|
"hat" | "widehat" => {
|
||||||
let (base, after) = parse_atom(chars, next);
|
let (base, after) = parse_atom(chars, next);
|
||||||
@@ -424,6 +464,22 @@ mod tests {
|
|||||||
assert!(fraction.depth > 0);
|
assert!(fraction.depth > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sqrt_degree_is_wider_than_a_plain_root() {
|
||||||
|
let font = Cmr10Metrics::default();
|
||||||
|
let plain = layout_math(&parse_math("\\sqrt{x}"), &font);
|
||||||
|
let indexed = layout_math(&parse_math("\\sqrt[3]{x}"), &font);
|
||||||
|
assert!(indexed.width > plain.width);
|
||||||
|
assert!(indexed.height > plain.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_left_delimiter_has_no_width() {
|
||||||
|
let font = Cmr10Metrics::default();
|
||||||
|
let dotted = layout_math(&parse_math("\\left.x\\right|"), &font);
|
||||||
|
let bare = layout_math(&parse_math("x|"), &font);
|
||||||
|
assert_eq!(dotted.width, bare.width);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn accents_add_height_without_changing_width() {
|
fn accents_add_height_without_changing_width() {
|
||||||
let font = Cmr10Metrics::default();
|
let font = Cmr10Metrics::default();
|
||||||
@@ -466,6 +522,6 @@ mod tests {
|
|||||||
panic!("expected a row");
|
panic!("expected a row");
|
||||||
};
|
};
|
||||||
assert!(matches!(&atoms[0], MathAtom::Fraction { .. }));
|
assert!(matches!(&atoms[0], MathAtom::Fraction { .. }));
|
||||||
assert!(matches!(&atoms[2], MathAtom::Sqrt(_)));
|
assert!(matches!(&atoms[2], MathAtom::Sqrt { .. }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,6 +104,8 @@ overfull ratio 4.11, badness 6942. TeX rejects the underfull candidate under
|
|||||||
its tolerance but still emits the moderately overfull 5-word line. Reproducing
|
its tolerance but still emits the moderately overfull 5-word line. Reproducing
|
||||||
this needs a two-pass tolerance algorithm with an emergency overfull pass, not
|
this needs a two-pass tolerance algorithm with an emergency overfull pass, not
|
||||||
a single cost function.
|
a single cost function.
|
||||||
|
- `\sqrt[n]{x}` carries an optional degree atom; `\left.` is an empty
|
||||||
|
delimiter with zero width.
|
||||||
- `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.
|
||||||
|
|||||||
Reference in new issue
Block a user