Select CMEX delimiter sizes for left and right
This commit is contained in:
1 parent
4c2aefd490
commit
f7e89c78a2
4 files changed
+115
-17
No files matched your search
@@ -221,6 +221,46 @@ 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.
|
||||
/// Pick the delimiter glyph TeX would use for a body of `delta` extent.
|
||||
///
|
||||
/// The ordinary glyph is used while it is tall enough; otherwise the smallest
|
||||
/// CMEX size variant whose extent covers the requirement wins, and oversized
|
||||
/// bodies scale the largest variant further.
|
||||
pub(crate) fn select_delimiter(font: Cmr10Metrics, delimiter: char, delta: i64) -> (i64, i64, i64) {
|
||||
if delimiter == '.' {
|
||||
return (0, 0, 0);
|
||||
}
|
||||
let base = (
|
||||
font.width(delimiter),
|
||||
font.height(delimiter),
|
||||
font.depth(delimiter),
|
||||
);
|
||||
let need = ((delta * 901) / 1000).max(delta - 5 * 65_536).max(0);
|
||||
if base.1 + base.2 >= need && base.1 + base.2 > 0 {
|
||||
return base;
|
||||
}
|
||||
for size in 1..=4 {
|
||||
if let Some(metrics) = cmex_delimiter(delimiter, size) {
|
||||
if metrics.1 + metrics.2 >= need {
|
||||
return metrics;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(metrics) = cmex_delimiter(delimiter, 4) {
|
||||
let extent = metrics.1 + metrics.2;
|
||||
if extent > 0 && need > extent {
|
||||
let factor = need as f64 / extent as f64;
|
||||
return (
|
||||
(metrics.0 as f64 * factor) as i64,
|
||||
(metrics.1 as f64 * factor) as i64,
|
||||
(metrics.2 as f64 * factor) as i64,
|
||||
);
|
||||
}
|
||||
return metrics;
|
||||
}
|
||||
base
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -309,13 +349,13 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
||||
},
|
||||
MathAtom::Delimited { left, right, body } => {
|
||||
let body_box = layout_math(body, font);
|
||||
let scale = delimiter_scale(&body_box);
|
||||
let delta = body_box.height + body_box.depth;
|
||||
let left_box = select_delimiter(*font, *left, delta);
|
||||
let right_box = select_delimiter(*font, *right, delta);
|
||||
MathBox {
|
||||
width: ((delimiter_width(*left, font) + delimiter_width(*right, font)) as f64
|
||||
* scale) as i64
|
||||
+ body_box.width,
|
||||
height: body_box.height + (2 * 65_536) * scale as i64,
|
||||
depth: body_box.depth + (2 * 65_536) * scale as i64,
|
||||
width: left_box.0 + body_box.width + right_box.0,
|
||||
height: body_box.height.max(left_box.1).max(right_box.1),
|
||||
depth: body_box.depth.max(left_box.2).max(right_box.2),
|
||||
}
|
||||
}
|
||||
MathAtom::Accent { base, .. } => {
|
||||
@@ -1892,3 +1932,47 @@ mod cmex_size_tests {
|
||||
assert!(bigg.height > big.height);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod delimiter_selection_tests {
|
||||
use super::*;
|
||||
use crate::cmr10::{Cmr10Metrics, cmex_delimiter};
|
||||
|
||||
#[test]
|
||||
fn selector_keeps_the_ordinary_glyph_for_small_bodies() {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let small = select_delimiter(font, '(', 7 * 65_536);
|
||||
assert_eq!(small.0, font.width('('));
|
||||
assert_eq!(small.1, font.height('('));
|
||||
assert_eq!(small.2, font.depth('('));
|
||||
let empty = select_delimiter(font, '.', 30 * 65_536);
|
||||
assert_eq!(empty, (0, 0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selector_grows_to_cmex_variants_for_tall_bodies() {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let tall = select_delimiter(font, '(', 30 * 65_536);
|
||||
let bigg_bigg = cmex_delimiter('(', 4).expect("cmex Bigg paren");
|
||||
assert_eq!(tall, bigg_bigg);
|
||||
let medium = select_delimiter(font, '(', 15 * 65_536);
|
||||
let mut matched = false;
|
||||
for size in 1..=4 {
|
||||
if let Some(metrics) = cmex_delimiter('(', size) {
|
||||
if metrics == medium {
|
||||
matched = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(matched, "medium selection {medium:?} is not a cmex variant");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_layout_uses_selected_delimiters() {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let plain = 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 + tall.depth > plain.height + plain.depth);
|
||||
assert!(tall.width > plain.width);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user