Embed math symbols as Computer Modern Type1 outlines

This commit is contained in:
gloamxun committed 2026-10-24 03:42:45 +08:00
1 parent a07d93510f
commit 6446de6b6b
7 files changed
+89 -8

No files matched your search

+1
View File
@@ -129,6 +129,7 @@ impl Cmr10Metrics {
4 => Self::Mono,
5 => Self::Sans,
6 => Self::Math,
7 => Self::Math,
_ => Self::Roman,
}
}
+26
View File
@@ -4139,6 +4139,32 @@ mod tests {
)));
}
#[test]
fn math_symbol_paints_with_the_cmsy_face() {
use crate::render::{DisplayItem, FontFace};
let document = Document::new(
1_000_000,
vec![Unit::math(
1,
crate::math::parse_math("\\times"),
&Cmr10Metrics::Math,
)],
);
let engine = Engine::layout(&document);
let list = engine.pages()[0].display_list();
let symbols: Vec<(String, FontFace)> = list
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Text { text, font, .. } if text == "\u{00D7}" => {
Some((text.clone(), *font))
}
_ => None,
})
.collect();
assert_eq!(symbols, vec![("\u{00D7}".to_owned(), FontFace::MathSymbol)]);
}
#[test]
fn math_alphabet_paints_with_its_type1_face() {
use crate::render::{DisplayItem, FontFace};
+48 -5
View File
@@ -16,7 +16,7 @@ use crate::cmr10::Cmr10Metrics;
use crate::render::{DisplayItem, DisplayList, FontFace};
use crate::type1::Type1Font;
const FACE_COUNT: usize = 7;
const FACE_COUNT: usize = 8;
fn deflate(bytes: &[u8]) -> Vec<u8> {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
@@ -74,6 +74,12 @@ static FULL_FACES: [PdfFace; FACE_COUNT] = [
italic_angle: -12,
font: OnceLock::new(),
},
PdfFace {
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Regular.ttf"),
base: "LiberationSerif-MathSymbol",
italic_angle: 0,
font: OnceLock::new(),
},
];
static MIN_FACES: [PdfFace; FACE_COUNT] = [
@@ -119,6 +125,12 @@ static MIN_FACES: [PdfFace; FACE_COUNT] = [
italic_angle: -12,
font: OnceLock::new(),
},
PdfFace {
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Regular.ttf"),
base: "LiberationSerif-MathSymbol",
italic_angle: 0,
font: OnceLock::new(),
},
];
fn minimal_text_char(ch: char) -> bool {
@@ -248,6 +260,7 @@ fn font_resource(font: FontFace) -> &'static str {
FontFace::Mono => "F5",
FontFace::Sans => "F6",
FontFace::MathItalic => "F7",
FontFace::MathSymbol => "F8",
}
}
@@ -270,6 +283,7 @@ fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
Some(include_bytes!("../../../assets/fonts/cmtt10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmss10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmmi10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmsy10.pfb")),
];
PFBS[slot]
}
@@ -483,6 +497,15 @@ fn write_pdf(
return None;
}
let font = type1_fonts()[slot].as_ref()?;
if slot == FontFace::MathSymbol as usize {
let codes = used_chars[slot]
.iter()
.filter_map(|character| {
crate::cmr10::math_symbol_slot(*character).map(|code| (*character, code))
})
.collect();
return Some((font, codes));
}
if !font.covers(used_chars[slot].iter().copied()) {
return None;
}
@@ -529,10 +552,14 @@ fn write_pdf(
(metrics.width(*character) * 100 / 65_536) as i64
});
widths.push(width.to_string());
if let Some((code, name)) = by_code
.get(&code)
.and_then(|character| font.code_and_name_for(*character))
{
let glyph = if slot == FontFace::MathSymbol as usize {
font.name_for_code(code).map(|name| (code, name))
} else {
by_code
.get(&code)
.and_then(|character| font.code_and_name_for(*character))
};
if let Some((code, name)) = glyph {
if code != next_code {
differences.push(code.to_string());
}
@@ -859,6 +886,22 @@ mod type1_pdf_tests {
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
}
#[test]
fn math_symbols_embed_cmsy10() {
let mut list = hello_list();
list.items[0] = DisplayItem::Text {
x: 0,
y: 800_000,
size: 655_360,
text: "\u{00D7}".to_string(),
font: FontFace::MathSymbol,
};
let pdf = write(&[list]);
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("/BaseFont /CMSY10"), "{text}");
assert!(text.contains("/multiply"), "{text}");
}
#[test]
fn type1_backend_falls_back_for_uncovered_characters() {
let mut list = hello_list();
+4
View File
@@ -21,6 +21,8 @@ pub enum FontFace {
Sans,
/// Computer Modern math italic (`cmmi10`).
MathItalic,
/// Computer Modern math symbols (`cmsy10`), addressed by TeX slot.
MathSymbol,
}
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -657,6 +659,8 @@ impl Page {
if !character.is_whitespace() {
let face = if character.is_ascii_alphabetic() || is_math_letter(*character) {
FontFace::MathItalic
} else if crate::cmr10::math_symbol_slot(*character).is_some() {
FontFace::MathSymbol
} else {
FontFace::Roman
};
+7
View File
@@ -116,6 +116,13 @@ impl Type1Font {
.find_map(|name| self.codes.get(*name).map(|code| (*code, *name)))
}
/// PostScript glyph name stored at `code` in the font's built-in encoding.
pub fn name_for_code(&self, code: u8) -> Option<&str> {
self.codes
.iter()
.find_map(|(name, slot)| (*slot == code).then_some(name.as_str()))
}
/// Whether the program has a glyph for every character in `chars`.
pub fn covers(&self, chars: impl IntoIterator<Item = char>) -> bool {
chars.into_iter().all(|ch| self.code_for(ch).is_some())