Map math alphabets onto Computer Modern Type1 faces

This commit is contained in:
gloamxun committed 2026-10-24 03:38:53 +08:00
1 parent a95cf8092e
commit a07d93510f
5 files changed
+142 -4

No files matched your search

+24
View File
@@ -4139,6 +4139,30 @@ mod tests {
)));
}
#[test]
fn math_alphabet_paints_with_its_type1_face() {
use crate::render::{DisplayItem, FontFace};
let document = Document::new(
1_000_000,
vec![Unit::math(
1,
crate::math::parse_math("\\mathbf{x}"),
&Cmr10Metrics::Math,
)],
);
let engine = Engine::layout(&document);
let list = engine.pages()[0].display_list();
let faces: Vec<FontFace> = list
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Text { text, font, .. } if text == "x" => Some(*font),
_ => None,
})
.collect();
assert_eq!(faces, vec![FontFace::Bold]);
}
#[test]
fn edit_on_second_page_only_rebuilds_second_page() {
let mut document = document();
+90 -1
View File
@@ -6,6 +6,15 @@
use crate::cmr10::{Cmr10Metrics, cmex_delimiter};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum MathAlphabet {
Roman,
Bold,
Italic,
Sans,
Mono,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum MathAtom {
Symbol(char),
@@ -16,6 +25,10 @@ pub enum MathAtom {
degree: Option<Box<MathAtom>>,
},
Text(String),
Alphabet {
text: String,
alphabet: MathAlphabet,
},
Delimited {
left: char,
right: char,
@@ -274,6 +287,27 @@ pub fn split_math_lines(atom: &MathAtom, font: &Cmr10Metrics, max_width: i64) ->
lines.into_iter().map(MathAtom::Row).collect()
}
/// Metrics for the built-in math alphabets.
///
/// Mathematical italic letters are the default math face (`cmmi10`); the
/// upright faces reuse the paragraph TFMs so `\mathbf`, `\mathsf` and
/// `\mathtt` get the same advances as their text counterparts.
pub(crate) fn alphabet_metrics(font: Cmr10Metrics, alphabet: MathAlphabet) -> Cmr10Metrics {
match alphabet {
MathAlphabet::Roman => font,
MathAlphabet::Italic => Cmr10Metrics::Math,
MathAlphabet::Bold => {
Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Bold)
}
MathAlphabet::Sans => {
Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Sans)
}
MathAlphabet::Mono => {
Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Mono)
}
}
}
fn atom_class(atom: &MathAtom) -> MathClass {
match atom {
MathAtom::Symbol(character) => math_class(*character),
@@ -417,6 +451,23 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
height: 6 * 65_536,
depth: 2 * 65_536,
},
MathAtom::Alphabet { text, alphabet } => {
let metrics = alphabet_metrics(*font, *alphabet);
MathBox {
width: text
.chars()
.map(|character| {
if character == ' ' {
metrics.space()
} else {
metrics.width(character)
}
})
.sum(),
height: 6 * 65_536,
depth: 2 * 65_536,
}
}
MathAtom::Delimited { left, right, body } => {
let body_box = layout_math(body, font);
let delta = body_box.height + body_box.depth;
@@ -1267,7 +1318,17 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
if after < chars.len() {
after += 1;
}
(MathAtom::Text(text), after)
let alphabet = match command.as_str() {
"mathbf" => Some(MathAlphabet::Bold),
"mathit" => Some(MathAlphabet::Italic),
"mathsf" => Some(MathAlphabet::Sans),
"mathtt" => Some(MathAlphabet::Mono),
_ => None,
};
match alphabet {
Some(alphabet) => (MathAtom::Alphabet { text, alphabet }, after),
None => (MathAtom::Text(text), after),
}
}
"not" => {
let (base, after) = parse_atom(chars, next);
@@ -1446,6 +1507,34 @@ mod tests {
assert!(indexed.height > plain.height);
}
#[test]
fn math_bold_letters_use_bold_metrics() {
let font = Cmr10Metrics::Math;
let plain = layout_math(&parse_math("\\mathrm{x}"), &font);
let bold = layout_math(&parse_math("\\mathbf{x}"), &font);
let bold_metrics = Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Bold);
assert_eq!(bold.width, bold_metrics.width('x'));
assert_ne!(bold.width, plain.width);
}
#[test]
fn math_italic_letters_use_the_math_face() {
let font = Cmr10Metrics::Math;
let italic = layout_math(&parse_math("\\mathit{x}"), &font);
assert_eq!(italic.width, Cmr10Metrics::Math.width('x'));
}
#[test]
fn math_sans_and_typewriter_use_their_metrics() {
let font = Cmr10Metrics::Math;
let sans = layout_math(&parse_math("\\mathsf{x}"), &font);
let mono = layout_math(&parse_math("\\mathtt{x}"), &font);
let sans_metrics = Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Sans);
let mono_metrics = Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Mono);
assert_eq!(sans.width, sans_metrics.width('x'));
assert_eq!(mono.width, mono_metrics.width('x'));
}
#[test]
fn mathcal_keeps_character_widths() {
let font = Cmr10Metrics::default();
+25
View File
@@ -682,6 +682,31 @@ impl Page {
cursor += font.width(character);
}
}
MathAtom::Alphabet { text, alphabet } => {
let metrics = math::alphabet_metrics(*font, *alphabet);
let face = match alphabet {
math::MathAlphabet::Roman => FontFace::Roman,
math::MathAlphabet::Bold => FontFace::Bold,
math::MathAlphabet::Italic => FontFace::MathItalic,
math::MathAlphabet::Sans => FontFace::Sans,
math::MathAlphabet::Mono => FontFace::Mono,
};
let mut cursor = x;
for character in text.chars() {
list.push(DisplayItem::Text {
font: face,
x: cursor,
y: baseline,
size,
text: character.to_string(),
});
cursor += if character == ' ' {
metrics.space()
} else {
metrics.width(character)
};
}
}
MathAtom::Row(atoms) => {
let mut cursor = x;
for child in atoms {