mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-02 15:01:42 -05:00
Merge pull request #4538 from EricKaden/fix/xfa-dynamic-render-required
XFA: a form saved with its pages is laid out when it requires dynamic rendering, unless signed
This commit is contained in:
@@ -8590,6 +8590,8 @@ fn an_xfa_form_without_a_template_opens_with_its_placeholder_and_a_warning() {
|
||||
assert!(doc.info.warnings.iter().any(|w| w.contains("could not be laid out") && w.contains("no template")), "{:?}", doc.info.warnings);
|
||||
}
|
||||
|
||||
/// A dynamic form opens laid out with the values its datasets hold, and with the rows its data
|
||||
/// adds to a repeating table row.
|
||||
#[test]
|
||||
fn dynamic_xfa_forms_open_with_the_values_and_rows_of_their_data() {
|
||||
let data = "<form><head><familyName>Kaur</familyName><born>2001-02-03</born><answer>Y</answer></head><table><row><what0>a</what0></row><row><what0>b</what0></row><row><what0>c</what0></row></table></form>";
|
||||
@@ -8605,6 +8607,29 @@ fn dynamic_xfa_forms_open_with_the_values_and_rows_of_their_data() {
|
||||
assert!(!doc.dirty);
|
||||
}
|
||||
|
||||
/// A dynamic form a viewer saved with its pages drawn holds only its signature box, so it opened as
|
||||
/// a picture with nothing to fill in. When its configuration says it must be rendered dynamically
|
||||
/// it is laid out from its template on opening, and its text field can be filled; once signed, it
|
||||
/// opens as saved, its pages and signature untouched.
|
||||
#[test]
|
||||
fn a_form_saved_with_its_pages_that_requires_dynamic_rendering_is_laid_out_unless_signed() {
|
||||
let mut s = Session::new().with_clock(|| 1_700_000_000);
|
||||
let unsigned = Arc::new(pdfcraft_xfa::fixtures::saved_with_pages_shell("required", false));
|
||||
let id = s.open("saved.pdf", None, unsigned, None).expect("opens");
|
||||
let doc = s.get(id).unwrap();
|
||||
assert_eq!(doc.info.xfa, Some(pdfcraft_render::Xfa::Dynamic));
|
||||
assert!(doc.xfa.is_some(), "laid out from the template");
|
||||
let name = doc.form.iter().find(|f| f.name.contains("name")).map(|f| f.name.clone()).expect("the template's text field");
|
||||
s.apply(id, Edit::SetFieldValue { name: name.clone(), value: FieldValue::Text("Ada".into()) }).expect("fills");
|
||||
assert_eq!(s.get(id).unwrap().form.iter().find(|f| f.name == name).map(|f| f.value.clone()), Some(vec!["Ada".to_string()]));
|
||||
let signed = Arc::new(pdfcraft_xfa::fixtures::saved_with_pages_shell("required", true));
|
||||
let id = s.open("signed.pdf", None, signed.clone(), None).expect("opens");
|
||||
let doc = s.get(id).unwrap();
|
||||
assert_eq!(doc.info.xfa, Some(pdfcraft_render::Xfa::Static), "signed: as saved");
|
||||
assert!(doc.xfa.is_none(), "nothing was laid out");
|
||||
assert_eq!(doc.bytes[..], signed[..], "no revision was appended on open");
|
||||
}
|
||||
|
||||
/// #3180: an XFA form with no fields whose configuration says `dynamicRender` is `forbidden` is a
|
||||
/// static form: its pages are the form. Opening it used to lay the template out into new pages
|
||||
/// and write them as a revision, so even a save with no change replaced the pages.
|
||||
@@ -14128,6 +14153,44 @@ fn repeated_backdrop_artwork_survives_background_undo_redo_and_both_save_modes()
|
||||
black(&s, rewritten);
|
||||
}
|
||||
|
||||
/// A form saved with its pages whose signature box sits deeper than the 32 nested fields looked
|
||||
/// through counts as signed, as the xfa crate counts it, so it is left as saved rather than laid
|
||||
/// out again over a signature that may be there; nested less deep, unsigned, it is laid out.
|
||||
#[test]
|
||||
fn a_saved_form_too_deep_to_read_through_is_left_as_saved() {
|
||||
let xfa = |depth: usize| {
|
||||
let mut s = Session::new();
|
||||
let id = s.open("deep.pdf", None, Arc::new(pdfcraft_xfa::fixtures::deep_saved_with_pages_shell(depth)), None).expect("opens");
|
||||
s.get(id).unwrap().info.xfa
|
||||
};
|
||||
assert_eq!(xfa(5), Some(pdfcraft_render::Xfa::Dynamic), "nested, unsigned: laid out");
|
||||
assert_eq!(xfa(40), Some(pdfcraft_render::Xfa::Static), "too deep to tell: left as saved");
|
||||
}
|
||||
|
||||
/// The renderer's inspection and the xfa crate each decide whether a form saved with its pages
|
||||
/// is laid out again from its template, each walking the field tree for a signature in its own
|
||||
/// way (the renderer reads the file through lopdf, the xfa crate through cos). They agree on every
|
||||
/// such form: required or not, signed or not, and a signature box nested from shallow to past
|
||||
/// the depth either looks through.
|
||||
#[test]
|
||||
fn the_renderer_and_the_xfa_crate_agree_on_which_saved_forms_are_laid_out() {
|
||||
let mut forms: Vec<(String, Vec<u8>)> = Vec::new();
|
||||
for render in ["required", "forbidden", ""] {
|
||||
for signed in [false, true] {
|
||||
forms.push((format!("{render:?}, signed {signed}"), pdfcraft_xfa::fixtures::saved_with_pages_shell(render, signed)));
|
||||
}
|
||||
}
|
||||
for depth in [1, 5, 30, 31, 32, 33, 34, 40] {
|
||||
forms.push((format!("nested {depth} deep"), pdfcraft_xfa::fixtures::deep_saved_with_pages_shell(depth)));
|
||||
}
|
||||
for (what, bytes) in forms {
|
||||
let bytes = Arc::new(bytes);
|
||||
let info = pdfcraft_render::inspect(bytes.clone(), None).expect("inspects");
|
||||
let cos = pdfcraft_cos::Document::open(bytes).expect("opens");
|
||||
assert_eq!(info.xfa == Some(pdfcraft_render::Xfa::Dynamic), pdfcraft_xfa::is_dynamic(&cos), "{what}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A page still exists after its only master widget is hidden: its master nodes stay in the
|
||||
/// script model, so a later script can address them again (test from wireless89's review of
|
||||
/// #5368).
|
||||
|
||||
@@ -171,8 +171,9 @@ impl<'a> Inspector<'a> {
|
||||
}
|
||||
|
||||
/// Reads what `info` holds beyond the page list: encryption, document information, tagging,
|
||||
/// page labels, bookmarks, annotations, transitions, form fields, layers, fonts, attachments
|
||||
/// (and, for a PDF portfolio, how it presents them), JavaScript and the opening destination.
|
||||
/// page labels, bookmarks, annotations, transitions, form fields, XFA kind (dynamic when it
|
||||
/// must be laid out from its template, else static), layers, fonts, attachments (and, for a
|
||||
/// PDF portfolio, how it presents them), JavaScript and the opening destination.
|
||||
pub(super) fn fill(&self, info: &mut DocInfo) {
|
||||
// lopdf drops /Encrypt from the trailer once it has decrypted the file.
|
||||
info.encrypted = self.doc.encrypted();
|
||||
@@ -260,8 +261,24 @@ impl<'a> Inspector<'a> {
|
||||
// A form with no fields is taken to be laid out from its template, unless its
|
||||
// configuration says it is a static form (#3180): Designer writes
|
||||
// `<dynamicRender>forbidden</dynamicRender>` for one, whose pages are the form.
|
||||
let static_form = self.xfa_dynamic_render(xfa).as_deref() == Some("forbidden");
|
||||
info.xfa = Some(if needs_rendering || (info.fields.is_empty() && !static_form) { Xfa::Dynamic } else { Xfa::Static });
|
||||
let render = self.xfa_dynamic_render(xfa);
|
||||
let static_form = render.as_deref() == Some("forbidden");
|
||||
// A form a viewer saved with its pages drawn, whose configuration says it must be
|
||||
// rendered dynamically, is laid out from its template too: its fields live there.
|
||||
// Not once it is signed: the signature covers the pages as saved (the xfa crate's
|
||||
// `is_dynamic` keeps the same rule).
|
||||
let signed = self.signed_form(catalog);
|
||||
let required = render.as_deref() == Some("required") && !signed;
|
||||
// Whether the AcroForm lists any field, as the xfa crate's `has_fields` reads it, so
|
||||
// both call the same forms dynamic: fields nested too deep to read through count.
|
||||
let listed = catalog
|
||||
.get(b"AcroForm")
|
||||
.ok()
|
||||
.and_then(|o| self.dict(o))
|
||||
.and_then(|f| f.get(b"Fields").ok())
|
||||
.and_then(|f| self.resolve(f).as_array().ok())
|
||||
.is_some_and(|a| !a.is_empty());
|
||||
info.xfa = Some(if needs_rendering || required || (!listed && !static_form) { Xfa::Dynamic } else { Xfa::Static });
|
||||
}
|
||||
self.layers(catalog, &mut info.layers);
|
||||
info.layer_groups = self.layer_groups(catalog);
|
||||
@@ -288,6 +305,40 @@ impl<'a> Inspector<'a> {
|
||||
info.open_dest = catalog.get(b"OpenAction").ok().and_then(|o| self.open_dest(o));
|
||||
}
|
||||
|
||||
/// Whether the form holds a signature field with a value, read as the xfa crate's
|
||||
/// `has_signature` reads it so that both say the same of a form: down the field tree from the
|
||||
/// AcroForm's `/Fields`, the field type inherited from the parent, and a tree deeper than 32
|
||||
/// fields or larger than 10,000 taken as signed. A form too big to read through is then left
|
||||
/// as saved rather than laid out again over a signature.
|
||||
pub(super) fn signed_form(&self, catalog: &'a Dictionary) -> bool {
|
||||
let Some(acro) = catalog.get(b"AcroForm").ok().and_then(|o| self.dict(o)) else { return false };
|
||||
let Some(fields) = acro.get(b"Fields").ok().and_then(|f| self.resolve(f).as_array().ok()) else { return false };
|
||||
// (field, signature type inherited, depth)
|
||||
let mut todo: Vec<(&Object, bool, usize)> = fields.iter().map(|f| (f, false, 0)).collect();
|
||||
let mut seen = HashSet::new();
|
||||
let mut budget = 10_000usize;
|
||||
while let Some((f, sig_above, depth)) = todo.pop() {
|
||||
if budget == 0 || depth > 32 {
|
||||
return true;
|
||||
}
|
||||
if let Some(id) = self.resolved_id(f)
|
||||
&& !seen.insert(id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
budget -= 1;
|
||||
let Some(d) = self.dict(f) else { continue };
|
||||
let sig = d.get(b"FT").ok().and_then(|t| self.resolve(t).as_name().ok()).map_or(sig_above, |t| t == b"Sig");
|
||||
if sig && d.get(b"V").ok().is_some_and(|v| !matches!(self.resolve(v), Object::Null)) {
|
||||
return true;
|
||||
}
|
||||
if let Some(kids) = d.get(b"Kids").ok().and_then(|k| self.resolve(k).as_array().ok()) {
|
||||
todo.extend(kids.iter().map(|k| (k, sig, depth + 1)));
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// A portfolio's collection dictionary (Table 153). `/D` names the initial document's key in
|
||||
/// the `/EmbeddedFiles` name tree, decoded as the keys are.
|
||||
fn portfolio(&self, collection: &Dictionary, attachments: &[Attachment]) -> Portfolio {
|
||||
|
||||
@@ -184,6 +184,10 @@ let form = pdfcraft_xfa::layout_xml(template_xml)?; // pages of items, for
|
||||
only their data is read and written. A form with no fields is also static when its `config`
|
||||
packet says `<dynamicRender>forbidden</dynamicRender>` (Designer's static PDF form): its pages
|
||||
are the form and are not laid out again (#3180). Without that, no fields means dynamic.
|
||||
The other way round, a form with fields is dynamic when its `config` packet says
|
||||
`<dynamicRender>required</dynamicRender>`: a viewer saved it with its pages drawn and only its
|
||||
signature boxes as fields, so its other fields live in the template. Once one of those boxes
|
||||
is signed it stays as saved, since laying it out would change the pages the signature covers.
|
||||
- **Flattening and converting.** `strip` takes the XFA out of a document: the packets,
|
||||
`/NeedsRendering`, the layout mark, the overrides and the private keys on the fields. The
|
||||
engine calls it after flattening an XFA form's fields (so Adobe's viewers show the baked
|
||||
|
||||
@@ -1041,6 +1041,79 @@ pub fn signed_shell() -> Vec<u8> {
|
||||
assemble(objs)
|
||||
}
|
||||
|
||||
/// A dynamic form as a viewer saves it with its pages drawn: no `/NeedsRendering`, a page with
|
||||
/// the form's picture, and only its signature box `sign` among the AcroForm's fields (the text
|
||||
/// field `name` lives in the template), signed when `signed`. Its configuration packet says
|
||||
/// `<dynamicRender>{render}</dynamicRender>` (none when `render` is empty).
|
||||
pub fn saved_with_pages_shell(render: &str, signed: bool) -> Vec<u8> {
|
||||
assemble(saved_with_pages_objects(render, signed))
|
||||
}
|
||||
|
||||
/// [`saved_with_pages_shell`]`("required", false)` whose signature box sits under `depth` nested
|
||||
/// fields of its own (none of them a signature, none with a value).
|
||||
pub fn deep_saved_with_pages_shell(depth: usize) -> Vec<u8> {
|
||||
let mut objs = saved_with_pages_objects("required", false);
|
||||
// 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 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<usize>| {
|
||||
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 {
|
||||
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();
|
||||
}
|
||||
assemble(objs)
|
||||
}
|
||||
|
||||
/// The objects of [`saved_with_pages_shell`], in order (the signature box is object 6).
|
||||
fn saved_with_pages_objects(render: &str, signed: bool) -> Vec<Vec<u8>> {
|
||||
let config = if render.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!(
|
||||
r#"<config xmlns="http://www.xfa.org/schema/xci/3.1/"><present><pdf><version>1.7</version></pdf></present><acrobat><acrobat7><dynamicRender>{render}</dynamicRender></acrobat7></acrobat></config>"#
|
||||
)
|
||||
};
|
||||
let xdp = format!(
|
||||
r##"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
|
||||
{config}
|
||||
<template xmlns="http://www.xfa.org/schema/xfa-template/3.3/">
|
||||
<subform name="form" layout="tb">
|
||||
<pageSet><pageArea name="page"><contentArea x="0.5in" y="0.5in" w="7.5in" h="10in"/><medium short="8.5in" long="11in"/></pageArea></pageSet>
|
||||
<field name="name" w="3in" h="0.3in"><ui><textEdit/></ui></field>
|
||||
<field name="sign" w="3in" h="1in"><ui><signature/></ui></field>
|
||||
</subform>
|
||||
</template>
|
||||
</xdp:xdp>
|
||||
"##
|
||||
);
|
||||
let value = if signed { " /V 7 0 R" } else { "" };
|
||||
let picture = "BT /F1 12 Tf 72 700 Td (Name: ______) Tj ET";
|
||||
vec![
|
||||
b"<< /Type /Catalog /Pages 2 0 R /AcroForm << /XFA 5 0 R /Fields [6 0 R] /SigFlags 3 >> >>".to_vec(),
|
||||
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
|
||||
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Annots [6 0 R] >>".to_vec(),
|
||||
format!("<< /Length {} >>\nstream\n{picture}\nendstream", picture.len()).into_bytes(),
|
||||
format!("<< /Length {} >>\nstream\n{xdp}\nendstream", xdp.len()).into_bytes(),
|
||||
format!("<< /T (form[0].sign[0]) /FT /Sig{value} /Type /Annot /Subtype /Widget /Rect [72 500 288 572] /P 3 0 R >>").into_bytes(),
|
||||
b"<< /Type /Sig /Name (A Signer) >>".to_vec(),
|
||||
]
|
||||
}
|
||||
|
||||
/// A check box `kind` whose change script, once it is checked, opens the protected check box
|
||||
/// `leave` and shows the invisible field `details`, then fails on an object the form doesn't
|
||||
/// have (as scripts written for an earlier version of a form do).
|
||||
|
||||
@@ -136,9 +136,16 @@ pub struct Report {
|
||||
}
|
||||
|
||||
/// Is this a dynamic XFA form still showing its placeholder pages (one PdfCraft has not laid
|
||||
/// out yet)?
|
||||
/// out yet)? That is a form that asks to be laid out (`/NeedsRendering`), one with no fields of
|
||||
/// its own, or one a viewer saved with its pages drawn whose configuration says it must be
|
||||
/// rendered dynamically (`<dynamicRender>required</dynamicRender>`): its fields live in the
|
||||
/// template. A signature on such a form covers the pages as saved, so a signed one is left as it
|
||||
/// is: laying it out would change what was signed.
|
||||
pub fn is_dynamic(doc: &Document) -> bool {
|
||||
existing_layout(doc).is_none() && matches!(read_packets(doc), Ok(Some(p)) if p.needs_rendering || !p.has_fields)
|
||||
existing_layout(doc).is_none()
|
||||
&& matches!(read_packets(doc), Ok(Some(p)) if p.needs_rendering
|
||||
|| !p.has_fields
|
||||
|| (packets::requires_dynamic_render(&p.xdp) && !packets::has_signature(doc)))
|
||||
}
|
||||
|
||||
/// Lay a template packet (or whole XDP, whose datasets then fill the fields) out.
|
||||
|
||||
@@ -28,6 +28,10 @@ fn check_text(len: usize) -> Result<(), XfaError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Most XML nodes read when the XDP is parsed for its configuration, as many as the template
|
||||
/// parser allows.
|
||||
const MAX_XDP_NODES: u32 = 2_000_000;
|
||||
|
||||
/// What was found in the XFA entry.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Packets {
|
||||
@@ -117,6 +121,70 @@ fn to_text(bytes: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the XDP's configuration packet asks for dynamic rendering: `required` at
|
||||
/// `config/acrobat/acrobat7/dynamicRender` (Designer writes it for a dynamic form). The packet is
|
||||
/// the `config` child of the `xdp` root (a whole-XDP stream, or an array's packets read in order
|
||||
/// between its preamble and postamble), or the document itself when it is a bare `config` packet.
|
||||
/// An element of that name anywhere else (a data node, inside the template) is not it. XML that
|
||||
/// doesn't parse, or has a DTD, asks for nothing.
|
||||
pub(crate) fn requires_dynamic_render(xdp: &str) -> bool {
|
||||
let opts = crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XDP_NODES };
|
||||
let Ok(doc) = crate::xml::Document::parse_with_options(xdp, opts) else { return false };
|
||||
let root = doc.root_element();
|
||||
/// `n`'s first child element named `name` (any namespace).
|
||||
fn child<'a>(n: crate::xml::Node<'a>, name: &str) -> Option<crate::xml::Node<'a>> {
|
||||
n.children().find(|c| c.is_element() && c.tag_name().name() == name)
|
||||
}
|
||||
let config = match root.tag_name().name() {
|
||||
"config" => Some(root),
|
||||
"xdp" => child(root, "config"),
|
||||
_ => None,
|
||||
};
|
||||
config
|
||||
.and_then(|c| child(c, "acrobat"))
|
||||
.and_then(|a| child(a, "acrobat7"))
|
||||
.and_then(|a| child(a, "dynamicRender"))
|
||||
.and_then(|d| d.text())
|
||||
.is_some_and(|t| t.trim() == "required")
|
||||
}
|
||||
|
||||
/// Whether the AcroForm holds a signed signature field (`/FT /Sig` with a value), looked for at
|
||||
/// most 32 levels deep and among at most 10 000 fields. A form whose fields go past either limit
|
||||
/// counts as signed: what wasn't looked at may be, and laying a signed form out would change
|
||||
/// what was signed.
|
||||
pub(crate) fn has_signature(doc: &Document) -> bool {
|
||||
let Some(root) = doc.root() else { return false };
|
||||
let catalog = doc.get(root);
|
||||
let Some(acro) = catalog.as_dict().and_then(|c| c.get(b"AcroForm")).map(|a| doc.resolve(a)) else { return false };
|
||||
let Some(fields) = acro.as_dict().and_then(|a| a.get(b"Fields")).map(|f| doc.resolve(f)) else { return false };
|
||||
let Some(fields) = fields.as_array() else { return false };
|
||||
// (field, inherited field type, depth)
|
||||
let mut todo: Vec<(Object, bool, usize)> = fields.iter().map(|f| (f.clone(), false, 0)).collect();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut budget = 10_000usize;
|
||||
while let Some((f, sig_above, depth)) = todo.pop() {
|
||||
if budget == 0 || depth > 32 {
|
||||
return true;
|
||||
}
|
||||
if f.as_ref().is_some_and(|r| !seen.insert(r)) {
|
||||
continue;
|
||||
}
|
||||
budget -= 1;
|
||||
let f = doc.resolve(&f);
|
||||
let Some(d) = f.as_dict() else { continue };
|
||||
let sig = d.name(b"FT").map_or(sig_above, |t| t == b"Sig");
|
||||
if sig && d.get(b"V").is_some_and(|v| !matches!(&*doc.resolve(v), Object::Null)) {
|
||||
return true;
|
||||
}
|
||||
if let Some(kids) = d.get(b"Kids").map(|k| doc.resolve(k))
|
||||
&& let Some(kids) = kids.as_array()
|
||||
{
|
||||
todo.extend(kids.iter().map(|k| (k.clone(), sig, depth + 1)));
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Read the XFA packets, if the document has any.
|
||||
pub fn read_packets(doc: &Document) -> Result<Option<Packets>, XfaError> {
|
||||
let Some(root) = doc.root() else { return Ok(None) };
|
||||
|
||||
@@ -597,6 +597,58 @@ fn tables_break_across_pages_and_repeat_their_header_row() {
|
||||
assert_eq!(all.len(), all.iter().collect::<std::collections::HashSet<_>>().len(), "field names are unique");
|
||||
}
|
||||
|
||||
/// A form a viewer saved with its pages drawn, holding only its signature box, is laid out from
|
||||
/// its template when its configuration says it must be rendered dynamically: its other fields
|
||||
/// live there. Signed, it is left as saved (laying it out would change what was signed), as it is
|
||||
/// without that configuration or with `forbidden`.
|
||||
#[test]
|
||||
fn a_form_saved_with_its_pages_is_laid_out_when_it_requires_dynamic_rendering() {
|
||||
let open = |render: &str, signed: bool| Document::open(Arc::new(crate::fixtures::saved_with_pages_shell(render, signed))).unwrap();
|
||||
let mut doc = open("required", false);
|
||||
assert!(is_dynamic(&doc));
|
||||
let report = render_into(&mut doc).unwrap();
|
||||
assert_eq!(report.fields, 2, "the text field from the template, and the signature box");
|
||||
assert!(!is_dynamic(&open("required", true)), "signed: left as saved");
|
||||
assert!(!is_dynamic(&open("", false)), "no configuration: the fields it has are the form's");
|
||||
assert!(!is_dynamic(&open("forbidden", false)));
|
||||
// A box under nested fields is found; past the 32 levels looked at, the form counts as signed.
|
||||
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:
|
||||
/// not the same element elsewhere, another element that starts with `config`, or another value.
|
||||
#[test]
|
||||
fn dynamic_rendering_is_read_from_the_configuration_only() {
|
||||
let required = crate::packets::requires_dynamic_render;
|
||||
let config = |inner: &str| {
|
||||
format!(
|
||||
r#"<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/"><config xmlns="http://www.xfa.org/schema/xci/3.1/">{inner}</config><template/></xdp:xdp>"#
|
||||
)
|
||||
};
|
||||
assert!(required(&config("<acrobat><acrobat7><dynamicRender> required </dynamicRender></acrobat7></acrobat>")));
|
||||
assert!(!required(&config("<acrobat><acrobat7><dynamicRender>forbidden</dynamicRender></acrobat7></acrobat>")));
|
||||
assert!(!required(&config("<acrobat><dynamicRender>required</dynamicRender></acrobat>")), "not under acrobat7");
|
||||
assert!(!required(&config("")));
|
||||
assert!(!required("<configuration><acrobat><acrobat7><dynamicRender>required</dynamicRender></acrobat7></acrobat></configuration>"));
|
||||
assert!(!required("<template><acrobat><acrobat7><dynamicRender>required</dynamicRender></acrobat7></acrobat></template>"));
|
||||
assert!(!required("<config><acrobat><acrobat7><dynamicRender>required"), "no end tag");
|
||||
// A data node named `config` before the configuration packet is not the configuration.
|
||||
let data_first = r#"<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/"><xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"><xfa:data><form><config>x</config></form></xfa:data></xfa:datasets><config xmlns="http://www.xfa.org/schema/xci/3.1/"><acrobat><acrobat7><dynamicRender>required</dynamicRender></acrobat7></acrobat></config><template/></xdp:xdp>"#;
|
||||
assert!(required(data_first), "the configuration packet after a data node named config");
|
||||
let only_data = r#"<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/"><xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"><xfa:data><config><acrobat><acrobat7><dynamicRender>required</dynamicRender></acrobat7></acrobat></config></xfa:data></xfa:datasets><template/></xdp:xdp>"#;
|
||||
assert!(!required(only_data), "a data node shaped like a configuration");
|
||||
assert!(required("<config><acrobat><acrobat7><dynamicRender>required</dynamicRender></acrobat7></acrobat></config>"), "a bare config packet");
|
||||
}
|
||||
|
||||
/// Laying a dynamic form out into its PDF replaces the placeholder page with the template's pages
|
||||
/// and adds its fields (a radio group once), keeps the XFA packets and `/NeedsRendering` for Adobe's
|
||||
/// viewers, writes each `/DR` font as an object of its own, and marks the form so it isn't laid out
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user