PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(xfdf): combine imported annotation flags with bitwise OR (#931)
Nigel Ihza Firdausy77241397d6d8
diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rsindex 91c3d2454..e0dfdf9c1 100644--- a/crates/xfdf/src/lib.rs+++ b/crates/xfdf/src/lib.rs@@ -590,7 +590,7 @@ fn annot_from_xml(node: roxmltree::Node, subtype: &str, page_ref: ObjRef) -> Opt.unwrap_or("print").split(',').filter_map(|f| FLAGS.iter().find(|(_, s)| s.eq_ignore_ascii_case(f.trim())).map(|x| x.0))- .sum();+ .fold(0, |bits, flag| bits | flag);d.set(b"F".to_vec(), Object::Int(flags));if let Some(c) = node.attribute("color").and_then(parse_hex) {d.set(b"C".to_vec(), arr(&c));diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rsindex 33b3a5a88..3e5635a9a 100644--- a/crates/xfdf/src/tests.rs+++ b/crates/xfdf/src/tests.rs@@ -445,3 +445,20 @@ fn xfdf_keeps_callout_leader_inset_and_ending() {assert_eq!(want.1.len(), 6, "{want:?}");assert_eq!(callout_keys(&dst), want, "{xfdf}");}++#[test]+fn duplicate_xfdf_annotation_flags_do_not_turn_on_unrelated_bits() {+ // Flags are a set of bit masks, not additive integers. A repeated "print" flag+ // must not carry into "nozoom", nor repeated "readonly" into "locked".+ for (flags, expected) in [("print, PRINT, nozoom", 12), ("readonly,readonly", 64), ("print,print,print", 4)] {+ let xfdf = format!(+ r#"<xfdf xmlns="http://ns.adobe.com/xfdf/"><annots><square page="0" rect="10,10,50,50" name="flags" flags="{flags}" /></annots></xfdf>"#+ );+ let mut doc = blank();+ assert_eq!(import(&mut doc, xfdf.as_bytes()).unwrap().comments, 1);+ let page = pdfcraft_model::pages(&doc)[0].obj;+ let annotations = doc.get(page).as_dict().and_then(|p| p.get(b"Annots")).map(|a| doc.resolve(a)).and_then(|a| a.as_array().cloned()).unwrap();+ let square = annotations.iter().filter_map(|a| doc.resolve(a).as_dict().cloned()).find(|d| d.name(b"Subtype") == Some(b"Square")).unwrap();+ assert_eq!(square.get(b"F").and_then(Object::as_int), Some(expected), "flags={flags}");+ }+}