Embed only the font faces used by a route-D PDF
This commit is contained in:
1 parent
472f778e75
commit
295093ae5a
3 files changed
+29
-5
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user