diff --git a/crates/xfa/src/fixtures.rs b/crates/xfa/src/fixtures.rs index f486d749f5..b45145c1b6 100644 --- a/crates/xfa/src/fixtures.rs +++ b/crates/xfa/src/fixtures.rs @@ -686,14 +686,28 @@ pub fn saved_with_pages_shell(render: &str, signed: bool) -> Vec { /// fields of its own (none of them a signature, none with a value). pub fn deep_saved_with_pages_shell(depth: usize) -> Vec { let mut objs = saved_with_pages_objects("required", false); - // Object 6, the box, becomes the outermost of the nested fields; the box goes last. + // Object 6, the box, becomes the outermost of the nested fields, each the parent of the next; + // the box goes last, under the innermost, and the page lists it under its new number. let first = objs.len() + 1; - let field = |name: usize, kid: usize| format!("<< /T (n{name}) /Kids [{kid} 0 R] >>").into_bytes(); - let Some(sig) = objs.get_mut(5).map(|o| std::mem::replace(o, field(0, first))) else { return assemble(objs) }; + let depth = depth.max(1); + let widget = first + depth - 1; + let parent_of = |n: usize| if n == first { 6 } else { n - 1 }; + let field = |name: usize, kid: usize, parent: Option| { + let parent = parent.map(|p| format!(" /Parent {p} 0 R")).unwrap_or_default(); + format!("<< /T (n{name}) /Kids [{kid} 0 R]{parent} >>").into_bytes() + }; + let kid_of_six = if depth == 1 { widget } else { first }; + let Some(sig) = objs.get_mut(5).map(|o| std::mem::replace(o, field(0, kid_of_six, None))) else { return assemble(objs) }; for d in 1..depth { - objs.push(field(d, first + d)); + let n = first + d - 1; + objs.push(field(d, n + 1, Some(parent_of(n)))); + } + let innermost = if depth == 1 { 6 } else { widget - 1 }; + let sig = String::from_utf8_lossy(&sig).replacen("/T (form[0].sign[0])", &format!("/T (sign[0]) /Parent {innermost} 0 R"), 1); + objs.push(sig.into_bytes()); + if let Some(page) = objs.get_mut(2) { + *page = String::from_utf8_lossy(page).replacen("/Annots [6 0 R]", &format!("/Annots [{widget} 0 R]"), 1).into_bytes(); } - objs.push(sig); assemble(objs) } diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs index a0772584bf..fe849e3824 100644 --- a/crates/xfa/src/tests.rs +++ b/crates/xfa/src/tests.rs @@ -319,6 +319,13 @@ fn a_form_saved_with_its_pages_is_laid_out_when_it_requires_dynamic_rendering() let deep = |depth: usize| Document::open(Arc::new(crate::fixtures::deep_saved_with_pages_shell(depth))).unwrap(); assert!(is_dynamic(&deep(5)), "nested, unsigned"); assert!(!is_dynamic(&deep(40)), "too deep to tell: as if signed"); + // The page lists the box under its new number, and the box knows its parent. + let doc = deep(5); + let page = doc.get(pdfcraft_cos::ObjRef::new(3, 0)); + let annot = page.as_dict().and_then(|d| d.get(b"Annots")).and_then(|a| a.as_array().and_then(|a| a.first().cloned())).and_then(|a| a.as_ref()); + let widget = annot.map(|r| doc.get(r)); + assert_eq!(widget.as_ref().and_then(|w| w.as_dict().and_then(|d| d.name(b"FT")).map(<[u8]>::to_vec)), Some(b"Sig".to_vec())); + assert!(widget.as_ref().and_then(|w| w.as_dict().map(|d| d.contains(b"Parent"))).unwrap_or(false)); } /// Only the configuration packet's `acrobat/acrobat7/dynamicRender` asks for dynamic rendering: