Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(xfdf): draw every imported redaction mark's review outline, opaque

aionggd266222339ad
diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rs
index 1e037b679..80c37b5e8 100644
--- a/crates/xfdf/src/lib.rs
+++ b/crates/xfdf/src/lib.rs
@@ -1032,6 +1032,27 @@ fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result<ObjRef, Da
if f & REDACT_UNSEEN != 0 {
d.set(b"F".to_vec(), Object::Int(f & !REDACT_UNSEEN));
}
+ // Its review outline is drawn opaque, and from the area it removes: applying a mark
+ // without usable /QuadPoints removes its /Rect, which the outline is then drawn from.
+ d.remove(b"CA");
+ d.remove(b"ca");
+ let quads_drawable = d
+ .get(b"QuadPoints")
+ .and_then(Object::as_array)
+ .is_some_and(|q| !q.is_empty() && q.len() % 8 == 0 && q.iter().all(|v| v.as_f64().is_some_and(f64::is_finite)));
+ if !quads_drawable {
+ // Quads the outline can't draw are replaced by the /Rect's, or dropped when there is
+ // no usable /Rect either; such a mark then removes nothing.
+ match nums_of(doc, d.get(b"Rect"))[..] {
+ [x0, y0, x1, y1] if [x0, y0, x1, y1].iter().all(|v| v.is_finite()) => {
+ let (l, r, b, t) = (x0.min(x1), x0.max(x1), y0.min(y1), y0.max(y1));
+ d.set(b"QuadPoints".to_vec(), arr(&[l, t, r, t, l, b, r, b]));
+ }
+ _ => {
+ d.remove(b"QuadPoints");
+ }
+ }
+ }
}
let nm = d.get(b"NM").and_then(|n| n.as_string().map(|s| s.to_text()));
let annots: Vec<Object> = doc
diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rs
index 57155e4b7..7951bef4d 100644
--- a/crates/xfdf/src/tests.rs
+++ b/crates/xfdf/src/tests.rs
@@ -1679,3 +1679,26 @@ fn a_same_named_comment_import_never_replaces_a_form_widget() {
assert_eq!(import(&mut doc, fdf).unwrap().comments, 1);
assert_eq!(doc.get(widget), before, "the widget was overwritten by an imported comment");
}
+
+#[test]
+fn an_imported_rect_only_or_transparent_redaction_mark_is_drawn_for_review() {
+ let xfdf = br#"<xfdf><annots><redact page="0" name="r1" rect="60,600,560,620" opacity="0"/></annots></xfdf>"#;
+ let fdf = b"%FDF-1.2\n1 0 obj\n<< /FDF << /Annots [<< /Subtype /Redact /NM (r1) /Page 0 /Rect [60 600 560 620] /CA 0 /ca 0 >>] >> >>\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF\n";
+ for data in [xfdf.as_slice(), fdf.as_slice()] {
+ let mut doc = blank();
+ assert_eq!(import(&mut doc, data).unwrap().comments, 1);
+ let page = pdfcraft_model::pages(&doc)[0].clone();
+ let mark = doc
+ .resolve(page.dict.get(b"Annots").unwrap())
+ .as_array()
+ .unwrap()
+ .iter()
+ .filter_map(|a| a.as_ref().and_then(|r| doc.get(r).as_dict().cloned()))
+ .find(|d| d.name(b"Subtype") == Some(b"Redact".as_slice()))
+ .unwrap();
+ assert_eq!(nums_of(&doc, mark.get(b"QuadPoints")), vec![60.0, 620.0, 560.0, 620.0, 60.0, 600.0, 560.0, 600.0]);
+ let ap = mark.get(b"AP").map(|a| doc.resolve(a)).and_then(|a| a.as_dict().cloned());
+ assert!(ap.is_some_and(|ap| ap.get(b"N").is_some()), "the mark has no review appearance: {mark:?}");
+ assert!(mark.get(b"CA").is_none() && mark.get(b"ca").is_none(), "the mark can be drawn transparent: {mark:?}");
+ }
+}