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

+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();