PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(xfdf): drop the replaced note's popup when re-importing a comment (#807) (#860)
MarekSpodBiedry7608632db192
diff --git a/crates/xfdf/README.md b/crates/xfdf/README.mdindex f30155b63..fc79172fd 100644--- a/crates/xfdf/README.md+++ b/crates/xfdf/README.md@@ -17,6 +17,7 @@ let report = import(&mut doc, &bytes)?; // XFDF, FDF, XML, CSV o- **Form data**: Acrobat's XML (`xfdf:original` keeps names that aren't XML names), CSV andtab-delimited text (a row of names, a row of values).-Import replaces comments with the same `/NM`, draws appearances PdfCraft can draw, and sets-values through the form's own checks (formats, validation, recalculation); read-only fields take-imported values, as in Acrobat. XML is read with `roxmltree`.+Import replaces comments with the same `/NM` (and drops the replaced comment's pop-up), draws+appearances PdfCraft can draw, and sets values through the form's own checks (formats,+validation, recalculation); read-only fields take imported values, as in Acrobat. XML is read+with `roxmltree`.diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rsindex 190080611..d839a0525 100644--- a/crates/xfdf/src/lib.rs+++ b/crates/xfdf/src/lib.rs@@ -635,13 +635,21 @@ fn place(doc: &mut Document, page_ref: ObjRef, d: Dict) -> Result<ObjRef, DataEr.and_then(|a| a.as_array().cloned()).unwrap_or_default();let mut kept = Vec::new();+ let mut removed: Vec<ObjRef> = Vec::new();for a in annots {let same = nm.is_some()&& a.as_ref().and_then(|r| doc.get(r).as_dict().and_then(|x| x.get(b"NM").and_then(|n| n.as_string().map(|s| s.to_text())))) == nm;- if !same {+ if same {+ removed.extend(a.as_ref());+ } else {kept.push(a);}}+ // The replaced note's pop-up goes with it; otherwise it stays on the page pointing at a note that is no longer there.+ kept.retain(|a| {+ let Some(x) = a.as_ref().map(|r| doc.get(r)).and_then(|o| o.as_dict().cloned()) else { return true };+ x.name(b"Subtype") != Some(b"Popup".as_slice()) || x.reference(b"Parent").is_none_or(|p| !removed.contains(&p))+ });let r = doc.add(Object::Dict(d));// Our own appearance where we can draw one; others keep none (viewers draw a default).let _ = pdfcraft_annot::set_appearance(doc, r);diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rsindex ca1debe49..371884ded 100644--- a/crates/xfdf/src/tests.rs+++ b/crates/xfdf/src/tests.rs@@ -331,3 +331,24 @@ fn unicode_annotation_colours_are_ignored_without_panicking() {assert_eq!(report.comments, 1);}}++/// Importing the same note again must not leave the earlier pop-up on the page.+#[test]+fn reimporting_a_note_replaces_its_popup() {+ let xfdf = concat!(+ r#"<xfdf xmlns="http://ns.adobe.com/xfdf/"><annots><text page="0" name="n1" rect="10,10,30,30"><contents>body</contents>"#,+ r#"<popup page="0" rect="40,10,140,60" open="no"/></text></annots></xfdf>"#,+ );+ let mut doc = blank();+ for _ in 0..3 {+ import(&mut doc, xfdf.as_bytes()).unwrap();+ }+ let page = pdfcraft_model::pages(&doc)[0].obj;+ let annots = doc.get(page).as_dict().and_then(|p| p.get(b"Annots").cloned()).and_then(|a| doc.resolve(&a).as_array().cloned()).unwrap();+ let refs: Vec<ObjRef> = annots.iter().filter_map(|a| a.as_ref()).collect();+ let is_popup = |r: &ObjRef| doc.get(*r).as_dict().and_then(|d| d.name(b"Subtype")) == Some(b"Popup".as_slice());+ let popups: Vec<ObjRef> = refs.iter().copied().filter(is_popup).collect();+ assert_eq!(popups.len(), 1, "one current pop-up, not one per import");+ let parent = doc.get(popups[0]).as_dict().and_then(|d| d.reference(b"Parent")).unwrap();+ assert!(refs.contains(&parent), "the pop-up belongs to a note that is still on the page");+}