Grow left-right delimiters with tall bodies
This commit is contained in:
1 parent
b281738084
commit
72f7cfaf2a
2 files changed
+30
-9
No files matched your search
@@ -189,6 +189,13 @@ fn delimiter_width(delimiter: char, font: &Cmr10Metrics) -> i64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// TeX grows delimiters with the enclosed formula; this is a simple 1x-3x
|
||||||
|
/// approximation of that rule.
|
||||||
|
pub fn delimiter_scale(body: &MathBox) -> f64 {
|
||||||
|
let extent = (body.height + body.depth) as f64 / 65_536.0;
|
||||||
|
(extent / 9.0).clamp(1.0, 3.0)
|
||||||
|
}
|
||||||
|
|
||||||
/// 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 {
|
||||||
@@ -252,12 +259,13 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|||||||
},
|
},
|
||||||
MathAtom::Delimited { left, right, body } => {
|
MathAtom::Delimited { left, right, body } => {
|
||||||
let body_box = layout_math(body, font);
|
let body_box = layout_math(body, font);
|
||||||
|
let scale = delimiter_scale(&body_box);
|
||||||
MathBox {
|
MathBox {
|
||||||
width: delimiter_width(*left, font)
|
width: ((delimiter_width(*left, font) + delimiter_width(*right, font)) as f64
|
||||||
+ body_box.width
|
* scale) as i64
|
||||||
+ delimiter_width(*right, font),
|
+ body_box.width,
|
||||||
height: body_box.height + 2 * 65_536,
|
height: body_box.height + (2 * 65_536) * scale as i64,
|
||||||
depth: body_box.depth + 2 * 65_536,
|
depth: body_box.depth + (2 * 65_536) * scale as i64,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MathAtom::Accent { base, .. } => {
|
MathAtom::Accent { base, .. } => {
|
||||||
@@ -1421,6 +1429,15 @@ mod tests {
|
|||||||
let middle = layout_math(&parse_math(r"\left(a\middle\{b\right)"), &font);
|
let middle = layout_math(&parse_math(r"\left(a\middle\{b\right)"), &font);
|
||||||
assert!(middle.width > plain.width);
|
assert!(middle.width > plain.width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn left_right_delimiters_scale_with_the_body() {
|
||||||
|
let font = Cmr10Metrics::default();
|
||||||
|
let short = layout_math(&parse_math(r"\left(a\right)"), &font);
|
||||||
|
let tall = layout_math(&parse_math(r"\left(\frac{a}{b}\right)"), &font);
|
||||||
|
assert!(tall.height > short.height);
|
||||||
|
assert!(tall.width > short.width);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn left_right_accept_control_sequence_delimiters() {
|
fn left_right_accept_control_sequence_delimiters() {
|
||||||
let font = Cmr10Metrics::default();
|
let font = Cmr10Metrics::default();
|
||||||
|
|||||||
@@ -639,22 +639,26 @@ impl Page {
|
|||||||
}
|
}
|
||||||
MathAtom::Delimited { left, right, body } => {
|
MathAtom::Delimited { left, right, body } => {
|
||||||
let body_box = math::layout_math(body, font);
|
let body_box = math::layout_math(body, font);
|
||||||
|
let scale = math::delimiter_scale(&body_box);
|
||||||
|
let delimiter_size = (size as f64 * scale) as i64;
|
||||||
|
let left_width = (font.width(*left) as f64 * scale) as i64;
|
||||||
|
let right_width = (font.width(*right) as f64 * scale) as i64;
|
||||||
if *left != '.' {
|
if *left != '.' {
|
||||||
list.push(DisplayItem::Text {
|
list.push(DisplayItem::Text {
|
||||||
font: FontFace::Roman,
|
font: FontFace::Roman,
|
||||||
x,
|
x,
|
||||||
y: baseline,
|
y: baseline,
|
||||||
size,
|
size: delimiter_size,
|
||||||
text: left.to_string(),
|
text: left.to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Self::paint_math(body, font, x + font.width(*left), baseline, size, list);
|
Self::paint_math(body, font, x + left_width, baseline, size, list);
|
||||||
if *right != '.' {
|
if *right != '.' {
|
||||||
list.push(DisplayItem::Text {
|
list.push(DisplayItem::Text {
|
||||||
font: FontFace::Roman,
|
font: FontFace::Roman,
|
||||||
x: x + font.width(*left) + body_box.width,
|
x: x + left_width + body_box.width,
|
||||||
y: baseline,
|
y: baseline,
|
||||||
size,
|
size: delimiter_size,
|
||||||
text: right.to_string(),
|
text: right.to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user