PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(xfdf): fall back to the /Rect when an imported redaction mark has a zero-area quad
aiongg63f7932d06e1
diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rsindex 1eac1f96c..9582298b3 100644--- a/crates/xfdf/src/lib.rs+++ b/crates/xfdf/src/lib.rs@@ -246,6 +246,14 @@ fn nums_of(doc: &Document, o: Option<&Object>) -> Vec<f64> {o.map(|o| doc.resolve(o)).and_then(|a| a.as_array().map(|a| a.iter().filter_map(|x| doc.resolve(x).as_f64()).collect())).unwrap_or_default()}+/// Whether a quad's four corners enclose any area: some three of them are not collinear.+fn quad_has_area(q: &[f64; 8]) -> bool {+ let [ax, ay, bx, by, cx, cy, dx, dy] = *q;+ let cross = |(px, py): (f64, f64), (qx, qy): (f64, f64), (rx, ry): (f64, f64)| (qx - px) * (ry - py) - (qy - py) * (rx - px);+ let (a, b, c, d) = ((ax, ay), (bx, by), (cx, cy), (dx, dy));+ [cross(a, b, c), cross(a, b, d), cross(a, c, d), cross(b, c, d)].iter().any(|v| v.abs() > 0.0)+}+fn text_of(doc: &Document, d: &Dict, k: &[u8]) -> Option<String> {d.get(k).and_then(|o| match &*doc.resolve(o) {Object::String(s) => Some(s.to_text()),@@ -1036,13 +1044,15 @@ fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result<ObjRef, Da// 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)));+ let quads_drawable = d.get(b"QuadPoints").and_then(Object::as_array).is_some_and(|q| {+ let v: Vec<f64> = q.iter().filter_map(Object::as_f64).filter(|v| v.is_finite()).collect();+ let (quads, rest) = v.as_chunks::<8>();+ v.len() == q.len() && rest.is_empty() && !quads.is_empty() && quads.iter().all(quad_has_area)+ });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.+ // Quads the outline can't draw, or any quad that encloses no area, 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));diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rsindex 3e6dc58a5..de880fe66 100644--- a/crates/xfdf/src/tests.rs+++ b/crates/xfdf/src/tests.rs@@ -1717,3 +1717,37 @@ fn merged_spreadsheet_cells_never_start_a_formula() {assert_eq!(lines[0], "\"'=HYPERLINK(\"\"http://x\"\")\",Total,Plus,Balance,\"\tName\"");assert_eq!(lines[1], "'@SUM(A1),'-2+3,\"'\t+1+cmd\",-12.5,Ada");}++fn imported_redact_mark(data: &[u8]) -> Dict {+ let mut doc = blank();+ assert_eq!(import(&mut doc, data).unwrap().comments, 1);+ let page = pdfcraft_model::pages(&doc)[0].clone();+ 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()+}++#[test]+fn an_imported_redaction_mark_with_zero_area_quads_falls_back_to_its_rect() {+ let rect_quads = vec![60.0, 620.0, 560.0, 620.0, 60.0, 600.0, 560.0, 600.0];+ let fdf = |quads: &str| {+ format!(+ "%FDF-1.2\n1 0 obj\n<< /FDF << /Annots [<< /Subtype /Redact /NM (r1) /Page 0 /Rect [60 600 560 620] /QuadPoints [{quads}] >>] >> >>\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF\n"+ )+ };+ let cases = [+ fdf("100 100 100 100 100 100 100 100").into_bytes(),+ // A good quad beside one that is a line (all four corners collinear).+ fdf("60 620 560 620 60 600 560 600 10 10 20 20 30 30 40 40").into_bytes(),+ br#"<xfdf><annots><redact page="0" name="r1" rect="60,600,560,620" coords="5,5,5,5,5,5,5,5"/></annots></xfdf>"#.to_vec(),+ ];+ for data in cases {+ let mark = imported_redact_mark(&data);+ let quads: Vec<f64> = mark.get(b"QuadPoints").and_then(Object::as_array).unwrap().iter().filter_map(Object::as_f64).collect();+ assert_eq!(quads, rect_quads, "zero-area quads were kept: {mark:?}");+ }+}