Subset embedded fonts to the glyphs a document uses

This commit is contained in:
gloamxun committed 2026-10-21 09:14:46 +08:00
1 parent 06779fb8dd
commit 8731ec2b9e
5 files changed
+335 -13

No files matched your search

+22
View File
@@ -3,6 +3,7 @@ pub mod math;
pub mod paragraph;
pub mod pdf;
pub mod render;
mod subset;
pub mod tfm;
pub use cmr10::Cmr10Metrics;
@@ -2852,6 +2853,27 @@ mod tests {
assert!(text.trim_end().ends_with("%%EOF"));
}
#[test]
fn pdf_subset_keeps_only_used_glyphs() {
let document = Document::new(400, vec![Unit::paragraph(1, "caf\u{e9}")]);
let engine = Engine::layout(&document);
let list = engine.pages()[0].display_list();
let pdf = crate::pdf::display_list_to_pdf(&list, 400 * 65_536);
let text = String::from_utf8_lossy(&pdf);
let length1 = text
.split("/Length1 ")
.nth(1)
.and_then(|rest| rest.split_whitespace().next())
.and_then(|value| value.parse::<usize>().ok())
.expect("FontFile2 /Length1");
assert!(
length1 < crate::pdf::full_face_size(crate::render::FontFace::Roman),
"subset {length1} should be smaller than the full face {}",
crate::pdf::full_face_size(crate::render::FontFace::Roman)
);
assert!(text.contains("/Filter /FlateDecode"));
}
#[test]
fn pdf_embeds_only_the_used_faces() {
let document = Document::new(400, vec![Unit::paragraph(1, "roman only")]);