Add Fraktur letters, infix fractions and middle delimiters
- bundle eufm10 (TFM + Type1), add the MathFraktur face and map \mathfrak
onto it, including PDF embedding and width lookup;
- parse TeX's infix fractions (\over, \atop, \choose, \brace,
\brack, \overwithdelims, \atopwithdelims): the rest of the current
row becomes the denominator, ar records whether a rule is drawn, and the
painter no longer emits parentheses (a \frac painter bug) and only draws
the bar for \over forms;
- add MathAtom::Middle, size top-level middle delimiters with the enclosing
\left/\right group through the CMEX size classes, and paint
SizedDelimiter at its selected size instead of the base glyph;
- skip the parser's '\u{0}' math spaces before \big, \middle,
\left and the withdelims delimiters, so \big ( parses like \big(.
560 workspace tests (381 route-D unit).
This commit is contained in:
1 parent
cb40cc4634
commit
8c42f2de2f
10 files changed
+499
-72
No files matched your search
@@ -19,6 +19,8 @@ pub enum Cmr10Metrics {
|
||||
MathCalligraphic,
|
||||
/// Blackboard-bold capitals from msbm10 (`\mathbb`).
|
||||
MathBlackboard,
|
||||
/// Euler Fraktur letters from eufm10 (`\mathfrak`).
|
||||
MathFraktur,
|
||||
/// Math face: italic letters from cmmi10, big operators from cmex10.
|
||||
Math,
|
||||
}
|
||||
@@ -33,6 +35,7 @@ static CMMI10_TFM: &[u8] = include_bytes!("../assets/cmmi10.tfm");
|
||||
static CMEX10_TFM: &[u8] = include_bytes!("../assets/cmex10.tfm");
|
||||
static CMSY10_TFM: &[u8] = include_bytes!("../assets/cmsy10.tfm");
|
||||
static MSBM10_TFM: &[u8] = include_bytes!("../assets/msbm10.tfm");
|
||||
static EUFM10_TFM: &[u8] = include_bytes!("../assets/eufm10.tfm");
|
||||
|
||||
static CMR10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
@@ -44,6 +47,7 @@ static CMMI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMEX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSY10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static MSBM10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static EUFM10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
|
||||
fn parse(bytes: &[u8]) -> TfmMetrics {
|
||||
TfmMetrics::parse(bytes).expect("embedded TFM must be valid")
|
||||
@@ -105,6 +109,14 @@ fn math_blackboard_metrics() -> &'static TfmMetrics {
|
||||
MSBM10_METRICS.get_or_init(parse_blackboard)
|
||||
}
|
||||
|
||||
fn parse_fraktur() -> TfmMetrics {
|
||||
parse(EUFM10_TFM)
|
||||
}
|
||||
|
||||
fn math_fraktur_metrics() -> &'static TfmMetrics {
|
||||
EUFM10_METRICS.get_or_init(parse_fraktur)
|
||||
}
|
||||
|
||||
fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
match font {
|
||||
Cmr10Metrics::Roman => CMR10_METRICS.get_or_init(parse_roman),
|
||||
@@ -116,6 +128,7 @@ fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
Cmr10Metrics::Math => CMR10_METRICS.get_or_init(parse_roman),
|
||||
Cmr10Metrics::MathCalligraphic => CMSY10_METRICS.get_or_init(parse_math_symbol),
|
||||
Cmr10Metrics::MathBlackboard => MSBM10_METRICS.get_or_init(parse_blackboard),
|
||||
Cmr10Metrics::MathFraktur => EUFM10_METRICS.get_or_init(parse_fraktur),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,6 +161,7 @@ impl Cmr10Metrics {
|
||||
7 => Self::Math,
|
||||
8 => Self::Math,
|
||||
9 => Self::MathBlackboard,
|
||||
10 => Self::MathFraktur,
|
||||
_ => Self::Roman,
|
||||
}
|
||||
}
|
||||
@@ -157,14 +171,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn width(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().width(character);
|
||||
}
|
||||
return Self::Math.width(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
if character.is_ascii_uppercase() {
|
||||
if matches!(
|
||||
*self,
|
||||
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
|
||||
) {
|
||||
let capital_only = *self != Self::MathFraktur;
|
||||
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
|
||||
return self.metrics().width(character);
|
||||
}
|
||||
return Self::Math.width(character);
|
||||
@@ -194,14 +206,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn height(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().height(character);
|
||||
}
|
||||
return Self::Math.height(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
if character.is_ascii_uppercase() {
|
||||
if matches!(
|
||||
*self,
|
||||
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
|
||||
) {
|
||||
let capital_only = *self != Self::MathFraktur;
|
||||
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
|
||||
return self.metrics().height(character);
|
||||
}
|
||||
return Self::Math.height(character);
|
||||
@@ -224,14 +234,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn depth(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().depth(character);
|
||||
}
|
||||
return Self::Math.depth(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
if character.is_ascii_uppercase() {
|
||||
if matches!(
|
||||
*self,
|
||||
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
|
||||
) {
|
||||
let capital_only = *self != Self::MathFraktur;
|
||||
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
|
||||
return self.metrics().depth(character);
|
||||
}
|
||||
return Self::Math.depth(character);
|
||||
@@ -254,14 +262,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn italic(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().italic(character);
|
||||
}
|
||||
return Self::Math.italic(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
if character.is_ascii_uppercase() {
|
||||
if matches!(
|
||||
*self,
|
||||
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
|
||||
) {
|
||||
let capital_only = *self != Self::MathFraktur;
|
||||
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
|
||||
return self.metrics().italic(character);
|
||||
}
|
||||
return Self::Math.italic(character);
|
||||
@@ -388,6 +394,17 @@ pub fn math_blackboard_width(character: char) -> Option<i64> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Advance width of an `eufm10` Fraktur letter.
|
||||
pub fn math_fraktur_width(character: char) -> Option<i64> {
|
||||
if character.is_ascii_alphabetic() {
|
||||
let width = math_fraktur_metrics().width(character);
|
||||
if width > 0 {
|
||||
return Some(width);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Advance width of a `cmex10` glyph addressed by a Unicode n-ary operator.
|
||||
pub fn math_extension_width(character: char) -> Option<i64> {
|
||||
math_extension_slot(character)
|
||||
|
||||
@@ -4235,6 +4235,29 @@ mod tests {
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathfrak_paints_with_the_eufm_face() {
|
||||
use crate::render::{DisplayItem, FontFace};
|
||||
let document = Document::new(
|
||||
1_000_000,
|
||||
vec![Unit::math(
|
||||
1,
|
||||
crate::math::parse_math("\\mathfrak{A}"),
|
||||
&Cmr10Metrics::Math,
|
||||
)],
|
||||
);
|
||||
let engine = Engine::layout(&document);
|
||||
let list = engine.pages()[0].display_list();
|
||||
assert!(list.items.iter().any(|item| matches!(
|
||||
item,
|
||||
DisplayItem::Text {
|
||||
text,
|
||||
font: FontFace::MathFraktur,
|
||||
..
|
||||
} if text == "A"
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_on_second_page_only_rebuilds_second_page() {
|
||||
let mut document = document();
|
||||
@@ -6328,6 +6351,48 @@ mod tests {
|
||||
assert!(svg.contains("font-family=\"monospace\">face3"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn big_delimiters_paint_at_their_selected_size() {
|
||||
let atom = crate::math::parse_math("\\big(");
|
||||
let document = Document::new(
|
||||
400,
|
||||
vec![Unit::math(1, atom, &crate::cmr10::Cmr10Metrics::new())],
|
||||
);
|
||||
let engine = Engine::layout(&document);
|
||||
let list = engine.pages()[0].display_list();
|
||||
let size = list
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
DisplayItem::Text { text, size, .. } if text == "(" => Some(*size),
|
||||
_ => None,
|
||||
})
|
||||
.expect("big paren should be painted");
|
||||
assert!(size > 655_360, "expected a larger glyph, got {size}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn middle_delimiters_paint_larger_than_the_base_glyph() {
|
||||
let atom = crate::math::parse_math(
|
||||
"\\left( \\frac{\\frac{a}{b}}{\\frac{c}{d}} \\middle\\{ c \\right)",
|
||||
);
|
||||
let document = Document::new(
|
||||
400,
|
||||
vec![Unit::math(1, atom, &crate::cmr10::Cmr10Metrics::new())],
|
||||
);
|
||||
let engine = Engine::layout(&document);
|
||||
let list = engine.pages()[0].display_list();
|
||||
let size = list
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
DisplayItem::Text { text, size, .. } if text == "{" => Some(*size),
|
||||
_ => None,
|
||||
})
|
||||
.expect("middle brace should be painted");
|
||||
assert!(size > 655_360, "expected a larger glyph, got {size}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binomial_is_painted_with_parentheses() {
|
||||
let document = Document::new(
|
||||
@@ -6459,6 +6524,47 @@ mod tests {
|
||||
assert!(svg.contains(">x</text>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractions_paint_a_rule_without_parentheses() {
|
||||
let atom = crate::math::parse_math("\\frac{a}{b}");
|
||||
let document = Document::new(
|
||||
400,
|
||||
vec![Unit::math(1, atom, &crate::cmr10::Cmr10Metrics::new())],
|
||||
);
|
||||
let engine = Engine::layout(&document);
|
||||
let list = engine.pages()[0].display_list();
|
||||
assert!(
|
||||
!list.items.iter().any(|item| matches!(
|
||||
item,
|
||||
DisplayItem::Text { text, .. } if text == "(" || text == ")"
|
||||
)),
|
||||
"fractions must not paint parentheses: {:?}",
|
||||
list.items
|
||||
);
|
||||
assert!(
|
||||
list.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, DisplayItem::Rule { .. }))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atop_paints_without_a_fraction_rule() {
|
||||
let atom = crate::math::parse_math("a \\atop b");
|
||||
let document = Document::new(
|
||||
400,
|
||||
vec![Unit::math(1, atom, &crate::cmr10::Cmr10Metrics::new())],
|
||||
);
|
||||
let engine = Engine::layout(&document);
|
||||
let list = engine.pages()[0].display_list();
|
||||
assert!(
|
||||
!list
|
||||
.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, DisplayItem::Rule { .. }))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn big_operators_paint_with_the_cmex_face() {
|
||||
let atom = crate::math::parse_math("\\sum_{i=1}^{n}");
|
||||
|
||||
@@ -15,6 +15,7 @@ pub enum MathAlphabet {
|
||||
Mono,
|
||||
Calligraphic,
|
||||
Blackboard,
|
||||
Fraktur,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
@@ -85,6 +86,8 @@ pub enum MathAtom {
|
||||
delimiter: char,
|
||||
size: u8,
|
||||
},
|
||||
/// A `\middle` delimiter; TeX sizes it with the enclosing `\left`/`\right`.
|
||||
Middle(char),
|
||||
StackLines(Vec<MathAtom>),
|
||||
Stack {
|
||||
top: Box<MathAtom>,
|
||||
@@ -107,9 +110,16 @@ pub enum MathAtom {
|
||||
Fraction {
|
||||
numerator: Box<MathAtom>,
|
||||
denominator: Box<MathAtom>,
|
||||
/// Whether TeX draws the fraction bar (`\over` vs `\atop`).
|
||||
#[serde(default = "fraction_bar_default")]
|
||||
bar: bool,
|
||||
},
|
||||
}
|
||||
|
||||
fn fraction_bar_default() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl MathAtom {
|
||||
pub fn symbol(character: char) -> Self {
|
||||
Self::Symbol(character)
|
||||
@@ -127,6 +137,7 @@ impl MathAtom {
|
||||
Self::Fraction {
|
||||
numerator: Box::new(numerator),
|
||||
denominator: Box::new(denominator),
|
||||
bar: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -309,6 +320,7 @@ pub(crate) fn alphabet_metrics(font: Cmr10Metrics, alphabet: MathAlphabet) -> Cm
|
||||
}
|
||||
MathAlphabet::Calligraphic => Cmr10Metrics::MathCalligraphic,
|
||||
MathAlphabet::Blackboard => Cmr10Metrics::MathBlackboard,
|
||||
MathAlphabet::Fraktur => Cmr10Metrics::MathFraktur,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -566,6 +578,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
||||
}
|
||||
}
|
||||
}
|
||||
MathAtom::Middle(delimiter) => layout_math(&MathAtom::Symbol(*delimiter), font),
|
||||
MathAtom::StackLines(rows) => {
|
||||
let boxes: Vec<MathBox> = rows.iter().map(|atom| layout_math(atom, font)).collect();
|
||||
MathBox {
|
||||
@@ -672,6 +685,7 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
||||
MathAtom::Fraction {
|
||||
numerator,
|
||||
denominator,
|
||||
..
|
||||
} => {
|
||||
let numerator_box = layout_math(numerator, font);
|
||||
let denominator_box = layout_math(denominator, font);
|
||||
@@ -702,6 +716,97 @@ pub fn parse_math(input: &str) -> MathAtom {
|
||||
atom
|
||||
}
|
||||
|
||||
/// An infix fraction command such as `\over`, `\atop` or `\choose`.
|
||||
///
|
||||
/// Returns the fraction bar flag, optional delimiters and the index after the
|
||||
/// command (and its delimiters).
|
||||
fn infix_fraction(chars: &[char], index: usize) -> Option<(bool, Option<(char, char)>, usize)> {
|
||||
if chars.get(index) != Some(&'\\') {
|
||||
return None;
|
||||
}
|
||||
let mut cursor = index + 1;
|
||||
let mut command = String::new();
|
||||
while let Some(&ch) = chars.get(cursor) {
|
||||
if ch.is_ascii_alphabetic() {
|
||||
command.push(ch);
|
||||
cursor += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
match command.as_str() {
|
||||
"over" => Some((true, None, cursor)),
|
||||
"atop" => Some((false, None, cursor)),
|
||||
"choose" => Some((false, Some(('(', ')')), cursor)),
|
||||
"brace" => Some((false, Some(('{', '}')), cursor)),
|
||||
"brack" => Some((false, Some(('[', ']')), cursor)),
|
||||
"overwithdelims" | "atopwithdelims" => {
|
||||
while chars.get(cursor) == Some(&'\u{0}') {
|
||||
cursor += 1;
|
||||
}
|
||||
let (left, after_left) = parse_delimiter(chars, cursor);
|
||||
let mut cursor = after_left;
|
||||
while chars.get(cursor) == Some(&'\u{0}') {
|
||||
cursor += 1;
|
||||
}
|
||||
let (right, after_right) = parse_delimiter(chars, cursor);
|
||||
Some((
|
||||
command == "overwithdelims",
|
||||
Some((left, right)),
|
||||
after_right,
|
||||
))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn skip_math_space(chars: &[char], mut index: usize) -> usize {
|
||||
while chars.get(index) == Some(&'\u{0}') {
|
||||
index += 1;
|
||||
}
|
||||
index
|
||||
}
|
||||
|
||||
/// The `cmex10` size class (0 for the base glyph) that covers `delta`.
|
||||
pub(crate) fn delimiter_size(delimiter: char, delta: i64) -> u8 {
|
||||
if delimiter == '.' {
|
||||
return 0;
|
||||
}
|
||||
let font = Cmr10Metrics::default();
|
||||
let base = font.height(delimiter) + font.depth(delimiter);
|
||||
let need = ((delta * 901) / 1000).max(delta - 5 * 65_536).max(0);
|
||||
if base >= need && base > 0 {
|
||||
return 0;
|
||||
}
|
||||
for size in 1..=4 {
|
||||
if let Some(metrics) = cmex_delimiter(delimiter, size) {
|
||||
if metrics.1 + metrics.2 >= need {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
4
|
||||
}
|
||||
|
||||
/// Replace top-level `\middle` atoms with sized delimiters.
|
||||
fn size_middle_delimiters(body: MathAtom, delta: i64) -> MathAtom {
|
||||
match body {
|
||||
MathAtom::Row(atoms) => MathAtom::Row(
|
||||
atoms
|
||||
.into_iter()
|
||||
.map(|atom| match atom {
|
||||
MathAtom::Middle(delimiter) => MathAtom::SizedDelimiter {
|
||||
delimiter,
|
||||
size: delimiter_size(delimiter, delta),
|
||||
},
|
||||
other => other,
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_right_command(chars: &[char], index: usize) -> bool {
|
||||
chars.get(index) == Some(&'\\')
|
||||
&& chars.get(index + 1) == Some(&'r')
|
||||
@@ -762,6 +867,30 @@ fn parse_sequence(
|
||||
if stop_at_right && is_right_command(chars, index) {
|
||||
break;
|
||||
}
|
||||
let mut probe = index;
|
||||
while chars.get(probe) == Some(&'\u{0}') {
|
||||
probe += 1;
|
||||
}
|
||||
if let Some((bar, delimiters, after)) = infix_fraction(chars, probe) {
|
||||
// TeX's infix fractions consume everything collected so far in the
|
||||
// current group; the rest of the group becomes the denominator.
|
||||
let numerator = MathAtom::Row(std::mem::take(&mut atoms));
|
||||
let (denominator, end) = parse_sequence(chars, after, stop_at_brace, stop_at_right);
|
||||
let fraction = MathAtom::Fraction {
|
||||
numerator: Box::new(numerator),
|
||||
denominator: Box::new(denominator),
|
||||
bar,
|
||||
};
|
||||
let atom = match delimiters {
|
||||
Some((left, right)) => MathAtom::Delimited {
|
||||
left,
|
||||
right,
|
||||
body: Box::new(fraction),
|
||||
},
|
||||
None => fraction,
|
||||
};
|
||||
return (MathAtom::Row(vec![atom]), end);
|
||||
}
|
||||
let (mut atom, mut next) = parse_atom(chars, index);
|
||||
let mut sub = None;
|
||||
let mut sup = None;
|
||||
@@ -1151,10 +1280,7 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
"bigg" | "biggl" | "biggr" => 3,
|
||||
_ => 4,
|
||||
};
|
||||
let mut after = next;
|
||||
while after < chars.len() && chars[after].is_whitespace() {
|
||||
after += 1;
|
||||
}
|
||||
let after = skip_math_space(chars, next);
|
||||
let (delimiter, after) = parse_delimiter(chars, after);
|
||||
(MathAtom::SizedDelimiter { delimiter, size }, after)
|
||||
}
|
||||
@@ -1319,8 +1445,9 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
)
|
||||
}
|
||||
"middle" => {
|
||||
let (delimiter, after) = parse_delimiter(chars, next);
|
||||
(MathAtom::Symbol(delimiter), after)
|
||||
let after = skip_math_space(chars, next);
|
||||
let (delimiter, after) = parse_delimiter(chars, after);
|
||||
(MathAtom::Middle(delimiter), after)
|
||||
}
|
||||
"max" | "min" | "log" | "ln" | "sin" | "cos" | "tan" | "cot" | "sec" | "csc"
|
||||
| "exp" | "det" | "dim" | "ker" | "hom" | "arg" | "deg" | "gcd" => {
|
||||
@@ -1350,6 +1477,7 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
"mathtt" => Some(MathAlphabet::Mono),
|
||||
"mathcal" => Some(MathAlphabet::Calligraphic),
|
||||
"mathbb" => Some(MathAlphabet::Blackboard),
|
||||
"mathfrak" => Some(MathAlphabet::Fraktur),
|
||||
_ => None,
|
||||
};
|
||||
match alphabet {
|
||||
@@ -1433,8 +1561,11 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
(MathAtom::Text(text), after)
|
||||
}
|
||||
"left" => {
|
||||
let (left, body_start) = parse_delimiter(chars, next);
|
||||
let body_start = skip_math_space(chars, next);
|
||||
let (left, body_start) = parse_delimiter(chars, body_start);
|
||||
let (body, after) = parse_sequence(chars, body_start, false, true);
|
||||
let body_box = layout_math(&body, &Cmr10Metrics::default());
|
||||
let body = size_middle_delimiters(body, body_box.height + body_box.depth);
|
||||
let mut after = after;
|
||||
// Consume `\right` and its delimiter.
|
||||
if is_right_command(chars, after) {
|
||||
@@ -1586,6 +1717,25 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathfrak_uses_fraktur_widths() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let fraktur = layout_math(&parse_math("\\mathfrak{A}"), &font);
|
||||
let upright = layout_math(&parse_math("\\mathrm{A}"), &font);
|
||||
let blackboard = layout_math(&parse_math("\\mathbb{A}"), &font);
|
||||
assert_ne!(fraktur.width, upright.width);
|
||||
assert_eq!(
|
||||
fraktur.width,
|
||||
crate::cmr10::math_fraktur_width('A').expect("eufm has Fraktur capitals")
|
||||
);
|
||||
let lower = layout_math(&parse_math("\\mathfrak{a}"), &font);
|
||||
assert_eq!(
|
||||
lower.width,
|
||||
crate::cmr10::math_fraktur_width('a').expect("eufm has Fraktur lowercase")
|
||||
);
|
||||
assert_ne!(fraktur.width, blackboard.width);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smash_can_target_only_the_top_or_bottom() {
|
||||
let font = Cmr10Metrics::default();
|
||||
@@ -1686,6 +1836,49 @@ mod tests {
|
||||
assert!(bigg.height > big.height);
|
||||
assert!(bigg.width > big.width);
|
||||
}
|
||||
#[test]
|
||||
fn middle_delimiters_follow_the_group_extent() {
|
||||
let atom = parse_math("\\left( \\frac{\\frac{a}{b}}{\\frac{c}{d}} \\middle\\{ c \\right)");
|
||||
let MathAtom::Row(atoms) = &atom else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
let MathAtom::Delimited { body, .. } = &atoms[0] else {
|
||||
panic!("expected a delimited group, got {:?}", atoms[0]);
|
||||
};
|
||||
let MathAtom::Row(parts) = &**body else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
let middle = parts
|
||||
.iter()
|
||||
.find_map(|atom| match atom {
|
||||
MathAtom::SizedDelimiter { delimiter, size } if *delimiter == '{' => Some(*size),
|
||||
_ => None,
|
||||
})
|
||||
.expect("middle delimiter should have been sized");
|
||||
assert!(middle >= 1, "expected a CMEX variant, got size {middle}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn middle_outside_left_right_stays_a_plain_delimiter() {
|
||||
let atom = parse_math("a \\middle| b");
|
||||
let MathAtom::Row(atoms) = &atom else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
assert!(
|
||||
atoms
|
||||
.iter()
|
||||
.any(|atom| matches!(atom, MathAtom::Middle('|')))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn big_delimiters_allow_math_spaces() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let spaced = layout_math(&parse_math("\\big ("), &font);
|
||||
let compact = layout_math(&parse_math("\\big("), &font);
|
||||
assert_eq!(spaced, compact);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tall_delimiters_grow_with_their_body() {
|
||||
let font = Cmr10Metrics::default();
|
||||
@@ -2082,6 +2275,63 @@ mod tests {
|
||||
assert!(matches!(&atoms[0], MathAtom::Script { sup: Some(_), .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infix_fractions_consume_the_current_row() {
|
||||
let atom = parse_math("a + b \\over c + d");
|
||||
let MathAtom::Row(atoms) = atom else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
assert_eq!(atoms.len(), 1);
|
||||
let MathAtom::Fraction {
|
||||
numerator,
|
||||
denominator,
|
||||
bar,
|
||||
} = &atoms[0]
|
||||
else {
|
||||
panic!("expected a fraction, got {:?}", atoms[0]);
|
||||
};
|
||||
assert!(*bar);
|
||||
assert!(matches!(**numerator, MathAtom::Row(ref row) if row.len() == 3));
|
||||
assert!(matches!(**denominator, MathAtom::Row(ref row) if row.len() == 3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atop_and_choose_use_their_own_forms() {
|
||||
let atom = parse_math("a \\atop b");
|
||||
let MathAtom::Row(atoms) = &atom else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
assert!(matches!(&atoms[0], MathAtom::Fraction { bar: false, .. }));
|
||||
let choose = parse_math("n \\choose k");
|
||||
let MathAtom::Row(atoms) = &choose else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
assert!(matches!(
|
||||
&atoms[0],
|
||||
MathAtom::Delimited {
|
||||
left: '(',
|
||||
right: ')',
|
||||
body,
|
||||
} if matches!(**body, MathAtom::Fraction { bar: false, .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn over_with_delims_keeps_the_requested_delimiters() {
|
||||
let atom = parse_math("a \\overwithdelims [ ] b");
|
||||
let MathAtom::Row(atoms) = &atom else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
assert!(matches!(
|
||||
&atoms[0],
|
||||
MathAtom::Delimited {
|
||||
left: '[',
|
||||
right: ']',
|
||||
body,
|
||||
} if matches!(**body, MathAtom::Fraction { bar: true, .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_fraction_and_sqrt() {
|
||||
let atom = parse_math("\\frac{a}{b} + \\sqrt{x}");
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::cmr10::Cmr10Metrics;
|
||||
use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
use crate::type1::Type1Font;
|
||||
|
||||
const FACE_COUNT: usize = 10;
|
||||
const FACE_COUNT: usize = 11;
|
||||
|
||||
fn deflate(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
@@ -92,6 +92,12 @@ static FULL_FACES: [PdfFace; FACE_COUNT] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Regular.ttf"),
|
||||
base: "LiberationSerif-MathFraktur",
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
@@ -155,6 +161,12 @@ static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Regular.ttf"),
|
||||
base: "LiberationSerif-MathFraktur",
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
fn minimal_text_char(ch: char) -> bool {
|
||||
@@ -287,6 +299,7 @@ fn font_resource(font: FontFace) -> &'static str {
|
||||
FontFace::MathSymbol => "F8",
|
||||
FontFace::MathExtension => "F9",
|
||||
FontFace::MathBlackboard => "F10",
|
||||
FontFace::MathFraktur => "F11",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,6 +325,7 @@ fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
|
||||
Some(include_bytes!("../../../assets/fonts/cmsy10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmex10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/msbm10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/eufm10.pfb")),
|
||||
];
|
||||
PFBS[slot]
|
||||
}
|
||||
@@ -967,6 +981,22 @@ mod type1_pdf_tests {
|
||||
assert!(text.contains("(\\101)"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathfrak_letters_embed_eufm10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "A".to_string(),
|
||||
font: FontFace::MathFraktur,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /EUFM10"), "{text}");
|
||||
assert!(text.contains("(\\101)"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathcal_letters_embed_cmsy10() {
|
||||
let mut list = hello_list();
|
||||
|
||||
@@ -27,6 +27,8 @@ pub enum FontFace {
|
||||
MathExtension,
|
||||
/// Computer Modern blackboard bold (`msbm10`).
|
||||
MathBlackboard,
|
||||
/// Euler Fraktur (`eufm10`).
|
||||
MathFraktur,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
@@ -700,6 +702,7 @@ impl Page {
|
||||
math::MathAlphabet::Mono => FontFace::Mono,
|
||||
math::MathAlphabet::Calligraphic => FontFace::MathSymbol,
|
||||
math::MathAlphabet::Blackboard => FontFace::MathBlackboard,
|
||||
math::MathAlphabet::Fraktur => FontFace::MathFraktur,
|
||||
};
|
||||
let mut cursor = x;
|
||||
for character in text.chars() {
|
||||
@@ -774,41 +777,37 @@ impl Page {
|
||||
MathAtom::Fraction {
|
||||
numerator,
|
||||
denominator,
|
||||
bar,
|
||||
} => {
|
||||
let num = math::layout_math(numerator, font);
|
||||
let den = math::layout_math(denominator, font);
|
||||
let width = num.width.max(den.width);
|
||||
let bracket = (size as f64 * 1.2) as i64;
|
||||
let inner_x = x + (width - num.width) / 2 + 2 * 65_536;
|
||||
Self::paint_math(numerator, font, inner_x, baseline - 8 * 65_536, size, list);
|
||||
let clearance = 2 * 65_536;
|
||||
let rule_width = num.width.max(den.width) + clearance;
|
||||
let bar_y = baseline - clearance;
|
||||
if *bar {
|
||||
list.push(DisplayItem::Rule {
|
||||
x,
|
||||
y: bar_y,
|
||||
width: rule_width,
|
||||
height: 26_214,
|
||||
});
|
||||
}
|
||||
Self::paint_math(
|
||||
denominator,
|
||||
numerator,
|
||||
font,
|
||||
x + (width - den.width) / 2 + 2 * 65_536,
|
||||
baseline + 12 * 65_536,
|
||||
x + (rule_width - num.width) / 2,
|
||||
bar_y - clearance - num.depth,
|
||||
size,
|
||||
list,
|
||||
);
|
||||
Self::paint_math(
|
||||
denominator,
|
||||
font,
|
||||
x + (rule_width - den.width) / 2,
|
||||
bar_y + clearance + den.height,
|
||||
size,
|
||||
list,
|
||||
);
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline,
|
||||
size: bracket,
|
||||
text: "(".to_string(),
|
||||
});
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x: x + width + 4 * 65_536,
|
||||
y: baseline,
|
||||
size: bracket,
|
||||
text: ")".to_string(),
|
||||
});
|
||||
list.push(DisplayItem::Rule {
|
||||
x,
|
||||
y: baseline - 2 * 65_536,
|
||||
width,
|
||||
height: 26_214,
|
||||
});
|
||||
}
|
||||
MathAtom::Sqrt { radicand, degree } => {
|
||||
let box_ = math::layout_math(radicand, font);
|
||||
@@ -1095,7 +1094,30 @@ impl Page {
|
||||
}
|
||||
}
|
||||
}
|
||||
MathAtom::SizedDelimiter { delimiter, .. } => {
|
||||
MathAtom::SizedDelimiter {
|
||||
delimiter,
|
||||
size: step,
|
||||
} => {
|
||||
let base_extent = font.height(*delimiter) + font.depth(*delimiter);
|
||||
let extent = crate::cmr10::cmex_delimiter(*delimiter, *step)
|
||||
.map(|(_, height, depth)| height + depth);
|
||||
let (glyph_size, shift) = match extent {
|
||||
Some(extent) if base_extent > 0 && extent > base_extent => {
|
||||
let scale = extent as f64 / base_extent as f64;
|
||||
let glyph_size = (size as f64 * scale) as i64;
|
||||
(glyph_size, ((glyph_size - size) as f64 * 0.35) as i64)
|
||||
}
|
||||
_ => (size, 0),
|
||||
};
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline + shift,
|
||||
size: glyph_size,
|
||||
text: delimiter.to_string(),
|
||||
});
|
||||
}
|
||||
MathAtom::Middle(delimiter) => {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
|
||||
Reference in new issue
Block a user