PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(edit): a header reuses a font name only for its very own dictionary
Lombardi200314b397834400
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rsindex a37ed4d1e..7b6410619 100644--- a/crates/edit/src/lib.rs+++ b/crates/edit/src/lib.rs@@ -326,13 +326,19 @@ fn page_fonts(doc: &Document, page: &pdfcraft_model::Page) -> Dict {}/// A name in `fonts` for the standard face `base` that can't take over a font the page already-/// draws with: `preferred` when it is free or already names that face, otherwise the first such-/// `preferred_1`, `preferred_2`… (one of the first `fonts.len() + 1` always is).+/// draws with: `preferred` when it is free or already names exactly the dictionary+/// [`add_page_resources`] writes for that face, otherwise the first such `preferred_1`,+/// `preferred_2`… (one of the first `fonts.len() + 1` always is). A font with anything more+/// (`/ToUnicode`, `/Widths`…) is someone else's, even in the same face.fn free_font_name(doc: &Document, fonts: &Dict, preferred: &str, base: &str) -> String {let usable = |name: &str| match fonts.get(name.as_bytes()) {None => true,Some(font) => doc.resolve(font).as_dict().is_some_and(|d| {- d.name(b"Subtype") == Some(b"Type1") && d.name(b"BaseFont") == Some(base.as_bytes()) && d.name(b"Encoding") == Some(b"WinAnsiEncoding")+ d.len() == 4+ && d.name(b"Type") == Some(b"Font")+ && d.name(b"Subtype") == Some(b"Type1")+ && d.name(b"BaseFont") == Some(base.as_bytes())+ && d.name(b"Encoding") == Some(b"WinAnsiEncoding")}),};if usable(preferred) {@@ -729,7 +735,10 @@ pub fn add_header_footer(doc: &mut Document, pages: &[usize], hf: &HeaderFooter,let mut content = begin(MarkKind::HeaderFooter, "Header", page.view_matrix(doc)).into_bytes();content.extend(body);content.extend_from_slice(END.as_bytes());- add_page_resources(doc, page, None, &[(resource.into_bytes(), base)])?;+ // A name already there is that very font (see `free_font_name`): left as it is.+ if !page_fonts(doc, page).contains(resource.as_bytes()) {+ add_page_resources(doc, page, None, &[(resource.into_bytes(), base)])?;+ }let page = &page_list(doc)[i];place(doc, page, MarkKind::HeaderFooter, content, false)?;}diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rsindex a0f4272ec..bc6b84e19 100644--- a/crates/edit/src/tests.rs+++ b/crates/edit/src/tests.rs@@ -168,6 +168,26 @@ fn a_header_never_takes_over_a_font_name_the_page_already_draws_with() {assert!(streams(&doc, 0).last().unwrap().contains("/PCTimes_1 8 Tf"));assert_eq!(base_of(&doc, b"PCTimes_2"), None, "no new name on update");assert_eq!(base_of(&doc, b"PCTimes").as_deref(), Some(&b"Helvetica-Bold"[..]));+ // The same face with more to it (here a /ToUnicode map) is the page's own too: kept whole.+ let mut doc = fixture();+ let page = pdfcraft_model::pages(&doc)[0].obj;+ let map = doc.add(Object::Stream(pdfcraft_cos::Stream::flate(Dict::new(), b"/CIDInit /ProcSet findresource begin")));+ let mut theirs = Dict::new();+ for (k, v) in [("Type", "Font"), ("Subtype", "Type1"), ("BaseFont", "Times-Roman"), ("Encoding", "WinAnsiEncoding")] {+ theirs.set(k.as_bytes().to_vec(), Object::name(v));+ }+ theirs.set(b"ToUnicode".to_vec(), Object::Ref(map));+ let mut fonts = Dict::new();+ fonts.set(b"PCTimes".to_vec(), Object::Dict(theirs));+ let mut res = Dict::new();+ res.set(b"Font".to_vec(), Object::Dict(fonts));+ doc.update_dict(page, |d| d.set(b"Resources".to_vec(), Object::Dict(res))).unwrap();+ add_header_footer(&mut doc, &[0], &hf, false, &cx()).unwrap();+ let doc = reopen(&doc);+ let fonts = page_fonts(&doc, &pdfcraft_model::pages(&doc)[0]);+ let kept = doc.resolve(fonts.get(b"PCTimes").unwrap()).as_dict().cloned().unwrap();+ assert!(kept.contains(b"ToUnicode"), "the page's map is kept");+ assert!(streams(&doc, 0).last().unwrap().contains("/PCTimes_1 8 Tf"));}#[test]