Embed only the font faces used by a route-D PDF

This commit is contained in:
gloamxun committed 2026-10-20 22:15:53 +08:00
1 parent 472f778e75
commit 295093ae5a
3 files changed
+29 -5

No files matched your search

+12
View File
@@ -1957,6 +1957,18 @@ mod tests {
assert!(text.trim_end().ends_with("%%EOF"));
}
#[test]
fn pdf_embeds_only_the_used_faces() {
let document = Document::new(400, vec![Unit::paragraph(1, "roman only")]);
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);
assert_eq!(text.matches("/Subtype /Type0").count(), 1);
assert!(!text.contains("/BaseFont /LiberationSerif-Bold"));
assert!(!text.contains("/BaseFont /LiberationMono"));
}
#[test]
fn sans_paragraph_paints_with_the_sans_face() {
let unit = Unit::paragraph_with_face(1, "sans face", ParagraphFont::Sans);
+16 -4
View File
@@ -237,8 +237,19 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
let mut objects: Vec<Vec<u8>> = Vec::new();
objects.push(b"<< /Type /Catalog /Pages 2 0 R >>".to_vec());
objects.push(Vec::new()); // pages object filled below
let mut used_faces = [false; 6];
for page in pages {
for item in &page.items {
if let DisplayItem::Text { font, .. } = item {
used_faces[*font as usize] = true;
}
}
}
let mut font_ids = [0usize; 6];
for (slot, face) in FACES.iter().enumerate() {
if !used_faces[slot] {
continue;
}
let type0_id = objects.len() + 1;
let cid_id = type0_id + 1;
let descriptor_id = type0_id + 2;
@@ -300,10 +311,11 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
.map(|(_, slot)| format!("/Im{slot} {} 0 R", image_base + *slot))
.collect::<Vec<_>>()
.join(" ");
let fonts = format!(
"/F1 {} 0 R /F2 {} 0 R /F3 {} 0 R /F4 {} 0 R /F5 {} 0 R /F6 {} 0 R",
font_ids[0], font_ids[1], font_ids[2], font_ids[3], font_ids[4], font_ids[5]
);
let fonts = (0..6)
.filter(|slot| used_faces[*slot])
.map(|slot| format!("/F{} {} 0 R", slot + 1, font_ids[slot]))
.collect::<Vec<_>>()
.join(" ");
let resources = if used.is_empty() {
format!("<< /Font << {fonts} >> >>")
} else {
+1 -1
View File
@@ -264,7 +264,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
The crate currently has 138 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 232 tests (138 route-D unit, 1 route-D integration, 71 v1 integration, 17 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
The crate currently has 138 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 233 tests (139 route-D unit, 1 route-D integration, 71 v1 integration, 17 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo