Draw big operators with cmex10 outlines

This commit is contained in:
gloamxun committed 2026-10-24 03:46:50 +08:00
1 parent 6446de6b6b
commit 66ceabad0e
7 files changed
+167 -28

No files matched your search

+42 -4
View File
@@ -16,7 +16,7 @@ use crate::cmr10::Cmr10Metrics;
use crate::render::{DisplayItem, DisplayList, FontFace};
use crate::type1::Type1Font;
const FACE_COUNT: usize = 8;
const FACE_COUNT: usize = 9;
fn deflate(bytes: &[u8]) -> Vec<u8> {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
@@ -80,6 +80,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-MathExtension",
italic_angle: 0,
font: OnceLock::new(),
},
];
static MIN_FACES: [PdfFace; FACE_COUNT] = [
@@ -131,6 +137,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-MathExtension",
italic_angle: 0,
font: OnceLock::new(),
},
];
fn minimal_text_char(ch: char) -> bool {
@@ -261,6 +273,7 @@ fn font_resource(font: FontFace) -> &'static str {
FontFace::Sans => "F6",
FontFace::MathItalic => "F7",
FontFace::MathSymbol => "F8",
FontFace::MathExtension => "F9",
}
}
@@ -284,6 +297,7 @@ fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
Some(include_bytes!("../../../assets/fonts/cmss10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmmi10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmsy10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmex10.pfb")),
];
PFBS[slot]
}
@@ -497,11 +511,16 @@ fn write_pdf(
return None;
}
let font = type1_fonts()[slot].as_ref()?;
if slot == FontFace::MathSymbol as usize {
if slot == FontFace::MathSymbol as usize || slot == FontFace::MathExtension as usize {
let codes = used_chars[slot]
.iter()
.filter_map(|character| {
crate::cmr10::math_symbol_slot(*character).map(|code| (*character, code))
let code = if slot == FontFace::MathSymbol as usize {
crate::cmr10::math_symbol_slot(*character)
} else {
crate::cmr10::math_extension_slot(*character)
};
code.map(|code| (*character, code))
})
.collect();
return Some((font, codes));
@@ -552,7 +571,9 @@ fn write_pdf(
(metrics.width(*character) * 100 / 65_536) as i64
});
widths.push(width.to_string());
let glyph = if slot == FontFace::MathSymbol as usize {
let glyph = if slot == FontFace::MathSymbol as usize
|| slot == FontFace::MathExtension as usize
{
font.name_for_code(code).map(|name| (code, name))
} else {
by_code
@@ -886,6 +907,23 @@ mod type1_pdf_tests {
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
}
#[test]
fn math_operators_embed_cmex10() {
let mut list = hello_list();
list.items[0] = DisplayItem::Text {
x: 0,
y: 800_000,
size: 655_360,
text: "\u{2211}".to_string(),
font: FontFace::MathExtension,
};
let pdf = write(&[list]);
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("/BaseFont /CMEX10"), "{text}");
assert!(text.contains("/summationtext"), "{text}");
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
}
#[test]
fn math_symbols_embed_cmsy10() {
let mut list = hello_list();