mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-05 05:23:36 -05:00
571 lines
27 KiB
Rust
571 lines
27 KiB
Rust
use super::{Dict, Document, Field, FormError, ObjRef, Object, PdfString, invalid};
|
|
|
|
/// The AcroForm dictionary's reference, creating the form (with `/Helv` in `/DR`) if needed.
|
|
pub(crate) fn ensure_form(doc: &mut Document) -> Result<ObjRef, FormError> {
|
|
let root = doc.root().ok_or(FormError::NoForm)?;
|
|
let existing = doc.get(root).as_dict().and_then(|d| d.get(b"AcroForm").cloned());
|
|
let r = match existing {
|
|
Some(Object::Ref(r)) if doc.get(r).as_dict().is_some() => r,
|
|
other => {
|
|
let d = other.and_then(|o| o.as_dict().cloned()).unwrap_or_default();
|
|
let r = doc.add(Object::Dict(d));
|
|
doc.update_dict(root, |c| c.set(b"AcroForm".to_vec(), Object::Ref(r)))?;
|
|
r
|
|
}
|
|
};
|
|
let mut af = doc.get(r).as_dict().cloned().unwrap_or_default();
|
|
if !af.contains(b"Fields") {
|
|
af.set(b"Fields".to_vec(), Object::Array(Vec::new()));
|
|
}
|
|
if !af.contains(b"DA") {
|
|
af.set(b"DA".to_vec(), PdfString::literal(b"/Helv 0 Tf 0 g".to_vec()));
|
|
}
|
|
let (dr, mut fonts) = form_fonts(doc, &af);
|
|
let mut added = inline_fonts_as_objects(doc, &mut fonts);
|
|
// Acrobat's resource names for the standard fonts its Appearance tab offers.
|
|
for (name, base) in [("Helv", "Helvetica"), ("TiRo", "Times-Roman"), ("Cour", "Courier")] {
|
|
if fonts.contains(name.as_bytes()) {
|
|
continue;
|
|
}
|
|
let mut f = Dict::new();
|
|
f.set(b"Type".to_vec(), Object::name("Font"));
|
|
f.set(b"Subtype".to_vec(), Object::name("Type1"));
|
|
f.set(b"BaseFont".to_vec(), Object::name(base));
|
|
f.set(b"Encoding".to_vec(), Object::name("WinAnsiEncoding"));
|
|
let r = doc.add(Object::Dict(f));
|
|
fonts.set(name.as_bytes().to_vec(), Object::Ref(r));
|
|
added = true;
|
|
}
|
|
if added {
|
|
store_form_fonts(doc, &mut af, dr, fonts);
|
|
}
|
|
doc.set(r, Object::Dict(af));
|
|
Ok(r)
|
|
}
|
|
|
|
/// The form dictionary's `/DR` and its `/Font` dictionary, resolved (empty where missing).
|
|
fn form_fonts(doc: &Document, af: &Dict) -> (Dict, Dict) {
|
|
let dr = af.get(b"DR").map(|o| doc.resolve(o)).and_then(|o| o.as_dict().cloned()).unwrap_or_default();
|
|
let fonts = dr.get(b"Font").map(|o| doc.resolve(o)).and_then(|o| o.as_dict().cloned()).unwrap_or_default();
|
|
(dr, fonts)
|
|
}
|
|
|
|
/// Each /DR font its own object: Acrobat, redrawing a text field after it is typed in, shows
|
|
/// nothing when the font is a dictionary written inline in /DR (#3441). Fonts written inline
|
|
/// (by an earlier save, or another writer) become objects saying the same; a font that is an
|
|
/// object already, or an entry that isn't a dictionary, is left alone. Whether any changed.
|
|
fn inline_fonts_as_objects(doc: &mut Document, fonts: &mut Dict) -> bool {
|
|
let inline: Vec<(Vec<u8>, Dict)> = fonts.iter().filter_map(|(k, v)| Some((k.clone(), v.as_dict()?.clone()))).collect();
|
|
let changed = !inline.is_empty();
|
|
for (name, font) in inline {
|
|
let r = doc.add(Object::Dict(font));
|
|
fonts.set(name, Object::Ref(r));
|
|
}
|
|
changed
|
|
}
|
|
|
|
/// Write `fonts` back as the `/DR /Font` of the form dictionary `af`. An indirect `/DR` or
|
|
/// `/Font` stays the object it is.
|
|
fn store_form_fonts(doc: &mut Document, af: &mut Dict, mut dr: Dict, fonts: Dict) {
|
|
match dr.get(b"Font").and_then(Object::as_ref) {
|
|
Some(fr) => doc.set(fr, Object::Dict(fonts)),
|
|
None => dr.set(b"Font".to_vec(), Object::Dict(fonts)),
|
|
}
|
|
match af.get(b"DR").and_then(Object::as_ref) {
|
|
Some(dr_ref) => doc.set(dr_ref, Object::Dict(dr)),
|
|
None => af.set(b"DR".to_vec(), Object::Dict(dr)),
|
|
}
|
|
}
|
|
|
|
/// Filling a field: the form's inline `/DR` fonts become objects (see
|
|
/// [`inline_fonts_as_objects`]), so a form an earlier version of PdfCraft made works in Acrobat
|
|
/// once it has been filled here too (#3441). Only the font storage changes; nothing is added.
|
|
/// Not in a form a signature covers: there the file stays as the signer saw it, apart from the
|
|
/// value itself.
|
|
pub(crate) fn form_fonts_as_objects(doc: &mut Document, all: &[crate::Field]) -> Result<(), FormError> {
|
|
let signed = all.iter().any(|f| f.kind == crate::FieldKind::Signature && holds_value(doc, f.obj));
|
|
if signed {
|
|
return Ok(());
|
|
}
|
|
let Some(root) = doc.root() else { return Ok(()) };
|
|
let Some(entry) = doc.get(root).as_dict().and_then(|c| c.get(b"AcroForm").cloned()) else { return Ok(()) };
|
|
match entry {
|
|
Object::Ref(r) => {
|
|
let Some(mut af) = doc.get(r).as_dict().cloned() else { return Ok(()) };
|
|
let (dr, mut fonts) = form_fonts(doc, &af);
|
|
if inline_fonts_as_objects(doc, &mut fonts) {
|
|
store_form_fonts(doc, &mut af, dr, fonts);
|
|
doc.set(r, Object::Dict(af));
|
|
}
|
|
}
|
|
// A form dictionary written directly in the catalog (the catalog's /AcroForm need not be
|
|
// an indirect reference) is written back there.
|
|
Object::Dict(mut af) => {
|
|
let (dr, mut fonts) = form_fonts(doc, &af);
|
|
if inline_fonts_as_objects(doc, &mut fonts) {
|
|
store_form_fonts(doc, &mut af, dr, fonts);
|
|
doc.update_dict(root, |c| c.set(b"AcroForm".to_vec(), Object::Dict(af)))?;
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Whether `d` has a non-null entry `key` (a `null` entry is absent per ISO 32000-2 §7.3.9).
|
|
fn dict_has(doc: &Document, d: &Dict, key: &[u8]) -> bool {
|
|
d.get(key).is_some_and(|v| !matches!(*doc.resolve(v), Object::Null))
|
|
}
|
|
|
|
/// Whether the field `r`, or the nearest ancestor that has one, holds a `/V` (the entry is
|
|
/// inheritable, ISO 32000-2 §12.7.4). The `/Parent` chain is followed at most 64 deep, as deep
|
|
/// as the field walk goes, so a cyclic chain ends.
|
|
fn holds_value(doc: &Document, r: ObjRef) -> bool {
|
|
let mut at = r;
|
|
for _ in 0..=64 {
|
|
let obj = doc.get(at);
|
|
let Some(d) = obj.as_dict() else { return false };
|
|
if dict_has(doc, d, b"V") {
|
|
return true;
|
|
}
|
|
match d.reference(b"Parent") {
|
|
Some(p) => at = p,
|
|
None => return false,
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
pub(super) fn add_to_page(doc: &mut Document, page: ObjRef, widget: ObjRef) -> Result<(), FormError> {
|
|
let existing = doc.get(page).as_dict().and_then(|d| d.get(b"Annots").cloned());
|
|
match existing.as_ref().and_then(|o| o.as_ref()).filter(|r| doc.get(*r).as_array().is_some()) {
|
|
Some(r) => {
|
|
let mut a = doc.get(r).as_array().cloned().unwrap_or_default();
|
|
a.push(Object::Ref(widget));
|
|
doc.set(r, Object::Array(a));
|
|
}
|
|
None => {
|
|
let mut a = existing.and_then(|o| o.as_array().cloned()).unwrap_or_default();
|
|
a.push(Object::Ref(widget));
|
|
doc.update_dict(page, |d| d.set(b"Annots".to_vec(), Object::Array(a)))?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn add_to_fields(doc: &mut Document, af: ObjRef, field: ObjRef) -> Result<(), FormError> {
|
|
let mut d = doc.get(af).as_dict().cloned().unwrap_or_default();
|
|
let mut list = d.get(b"Fields").map(|o| doc.resolve(o)).and_then(|o| o.as_array().cloned()).unwrap_or_default();
|
|
list.push(Object::Ref(field));
|
|
d.set(b"Fields".to_vec(), Object::Array(list));
|
|
doc.set(af, Object::Dict(d));
|
|
Ok(())
|
|
}
|
|
|
|
/// The most parts a dotted field name may have ("a.b.c" has three).
|
|
const MAX_NAME_PARTS: usize = 32;
|
|
|
|
/// The field attributes a field inherits from its parents (ISO 32000-2 §12.7.4.1, §12.7.5.2).
|
|
const INHERITED_KEYS: [&[u8]; 8] = [b"FT", b"Ff", b"V", b"DV", b"DA", b"Q", b"MaxLen", b"Opt"];
|
|
|
|
/// The parts of a full field name: `a.b` is the field `b` under the parent field `a`, since a
|
|
/// field's own (partial) name can't contain a period (ISO 32000-2 §12.7.4.2) (#2696).
|
|
pub(super) fn name_parts(name: &str) -> Result<Vec<&str>, FormError> {
|
|
let parts: Vec<&str> = name.split('.').take(MAX_NAME_PARTS.saturating_add(1)).collect();
|
|
if parts.len() > MAX_NAME_PARTS {
|
|
return invalid(format!("field names can have at most {MAX_NAME_PARTS} parts"));
|
|
}
|
|
if parts.iter().any(|p| p.trim().is_empty()) {
|
|
return invalid("field names can't be empty, or have an empty part between periods");
|
|
}
|
|
Ok(parts)
|
|
}
|
|
|
|
/// Why the full name `name` can't be used beside the field named `other`, if it can't: the
|
|
/// same name, or one inside the other (a field that has a value can't also hold fields).
|
|
pub(super) fn clash(name: &str, other: &str) -> Option<String> {
|
|
if name == other {
|
|
return Some(format!("a field named {name:?} already exists"));
|
|
}
|
|
let inside = |a: &str, b: &str| a.strip_prefix(b).is_some_and(|rest| rest.starts_with('.'));
|
|
(inside(name, other) || inside(other, name)).then(|| format!("{name:?} can't be used beside the field {other:?}"))
|
|
}
|
|
|
|
/// The array `key` of `holder` (`/Fields` of the form, `/Kids` of a field).
|
|
pub(super) fn list_of(doc: &Document, holder: ObjRef, key: &[u8]) -> Vec<Object> {
|
|
let d = doc.get(holder).as_dict().cloned().unwrap_or_default();
|
|
d.get(key).map(|o| doc.resolve(o)).and_then(|o| o.as_array().cloned()).unwrap_or_default()
|
|
}
|
|
|
|
/// Add `field` to `parent`'s `/Kids`, or to the form's `/Fields` at the top.
|
|
fn link(doc: &mut Document, af: ObjRef, parent: Option<ObjRef>, field: ObjRef) -> Result<(), FormError> {
|
|
let Some(p) = parent else { return add_to_fields(doc, af, field) };
|
|
let mut kids = list_of(doc, p, b"Kids");
|
|
kids.push(Object::Ref(field));
|
|
doc.update_dict(p, |d| d.set(b"Kids".to_vec(), Object::Array(kids)))?;
|
|
Ok(())
|
|
}
|
|
|
|
/// The node named `name` (its own `/T`) directly under `parent`, or at the top of the form.
|
|
pub(super) fn child_named(doc: &Document, af: ObjRef, parent: Option<ObjRef>, name: &str) -> Option<ObjRef> {
|
|
let list = match parent {
|
|
Some(p) => list_of(doc, p, b"Kids"),
|
|
None => list_of(doc, af, b"Fields"),
|
|
};
|
|
list.iter()
|
|
.filter_map(|o| o.as_ref())
|
|
.find(|r| doc.get(*r).as_dict().and_then(|d| d.get(b"T").cloned()).and_then(|t| crate::text_of(&doc.resolve(&t))).as_deref() == Some(name))
|
|
}
|
|
|
|
/// The values a parent field passes to the fields under it that can't be turned off by giving
|
|
/// the field a neutral one of its own, as `/Ff` and `/Q` can.
|
|
const PASSED_VALUES: [&[u8]; 3] = [b"V", b"DV", b"MaxLen"];
|
|
|
|
/// Whether `field` may go in the field tree under the full name `parts`, changing nothing. The
|
|
/// field list holds only terminal fields, so a parent left empty by a move isn't in it: a node
|
|
/// of the same name there is found here.
|
|
/// `inherited` is what the field will hold as its own before it moves (see [`inherited`]).
|
|
pub(super) fn check_place(doc: &Document, af: ObjRef, field: ObjRef, parts: &[&str], inherited: &Dict) -> Result<(), FormError> {
|
|
let Some((own, parents)) = parts.split_last() else { return invalid("field names can't be empty") };
|
|
let mut owned = doc.get(field).as_dict().cloned().unwrap_or_default();
|
|
for (k, v) in inherited.iter() {
|
|
owned.set(k.to_vec(), v.clone());
|
|
}
|
|
let mut at: Option<ObjRef> = None;
|
|
let mut whole_path = true;
|
|
for part in parents {
|
|
let Some(r) = child_named(doc, af, at, part) else {
|
|
whole_path = false;
|
|
break;
|
|
};
|
|
// A value, reset value or length limit held by a parent would pass to this field.
|
|
let held = doc.get(r).as_dict().is_some_and(|d| PASSED_VALUES.iter().any(|k| dict_has(doc, d, k) && !dict_has(doc, &owned, k)));
|
|
if held {
|
|
return invalid(format!("{part:?} holds a value for the fields under it, so {:?} can't be put there", parts.join(".")));
|
|
}
|
|
at = Some(r);
|
|
}
|
|
// A field being moved is still in the tree here, but under another full name (a move to its
|
|
// own name changes nothing and doesn't get here), so a node found is another.
|
|
if whole_path && child_named(doc, af, at, own).is_some() {
|
|
return invalid(format!("a field named {:?} already exists", parts.join(".")));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Put `field` in the field tree under the full name `parts`: its own `/T` is the last part,
|
|
/// and the parts before it name parent fields, reused where the tree has them and made where
|
|
/// not (Acrobat makes `a.b` a field `b` under a field `a`).
|
|
pub(super) fn attach(doc: &mut Document, af: ObjRef, field: ObjRef, parts: &[&str]) -> Result<(), FormError> {
|
|
let Some((own, parents)) = parts.split_last() else { return invalid("field names can't be empty") };
|
|
check_place(doc, af, field, parts, &Dict::new())?;
|
|
let mut parent: Option<ObjRef> = None;
|
|
let mut inherits = false;
|
|
for part in parents {
|
|
let next = match child_named(doc, af, parent, part) {
|
|
Some(r) => {
|
|
inherits |= doc.get(r).as_dict().is_some_and(|d| dict_has(doc, d, b"Ff") || dict_has(doc, d, b"Q"));
|
|
r
|
|
}
|
|
None => {
|
|
let mut d = Dict::new();
|
|
d.set(b"T".to_vec(), PdfString::text(part));
|
|
d.set(b"Kids".to_vec(), Object::Array(Vec::new()));
|
|
if let Some(p) = parent {
|
|
d.set(b"Parent".to_vec(), Object::Ref(p));
|
|
}
|
|
let r = doc.add(Object::Dict(d));
|
|
link(doc, af, parent, r)?;
|
|
r
|
|
}
|
|
};
|
|
parent = Some(next);
|
|
}
|
|
doc.update_dict(field, |d| {
|
|
d.set(b"T".to_vec(), PdfString::text(own));
|
|
match parent {
|
|
Some(p) => d.set(b"Parent".to_vec(), Object::Ref(p)),
|
|
None => {
|
|
d.remove(b"Parent");
|
|
}
|
|
}
|
|
// An existing parent's flags and alignment would otherwise pass to this field.
|
|
if inherits {
|
|
for k in [&b"Ff"[..], b"Q"] {
|
|
if !d.contains(k) {
|
|
d.set(k.to_vec(), Object::Int(0));
|
|
}
|
|
}
|
|
}
|
|
})?;
|
|
link(doc, af, parent, field)
|
|
}
|
|
|
|
/// What `field` inherits from its parents and doesn't hold itself, changing nothing.
|
|
pub(super) fn inherited(doc: &Document, field: ObjRef) -> Dict {
|
|
let own = doc.get(field).as_dict().cloned().unwrap_or_default();
|
|
let mut add = Dict::new();
|
|
let mut seen = std::collections::HashSet::new();
|
|
let mut parent = own.reference(b"Parent");
|
|
while let Some(p) = parent {
|
|
if seen.len() > 64 || !seen.insert(p) {
|
|
break;
|
|
}
|
|
let Some(d) = doc.get(p).as_dict().cloned() else { break };
|
|
for k in INHERITED_KEYS {
|
|
if let Some(v) = d.get(k).filter(|_| dict_has(doc, &d, k) && !dict_has(doc, &own, k) && !add.contains(k)) {
|
|
add.set(k.to_vec(), v.clone());
|
|
}
|
|
}
|
|
parent = d.reference(b"Parent");
|
|
}
|
|
// /Opt is inheritable for check boxes and radio buttons (Table 230), not choice
|
|
// fields (Table 234). Classify by effective /FT and /Ff before materializing it.
|
|
let effective = |key: &[u8]| if dict_has(doc, &own, key) { own.get(key) } else { add.get(key) };
|
|
let button = effective(b"FT").is_some_and(|v| doc.resolve(v).as_name() == Some(b"Btn"));
|
|
let flags = effective(b"Ff").and_then(|v| doc.resolve(v).as_int()).and_then(|v| u32::try_from(v).ok()).unwrap_or(0);
|
|
if !button || flags & crate::flags::PUSH_BUTTON != 0 {
|
|
add.remove(b"Opt");
|
|
}
|
|
add
|
|
}
|
|
|
|
/// Whether an ancestor of `field` sets the inheritable `key`, which `field` would take on without
|
|
/// its own entry.
|
|
pub(crate) fn ancestor_sets(doc: &Document, field: ObjRef, key: &[u8]) -> bool {
|
|
let mut seen = std::collections::HashSet::new();
|
|
let mut parent = doc.get(field).as_dict().and_then(|d| d.reference(b"Parent"));
|
|
while let Some(p) = parent {
|
|
if seen.len() > 64 || !seen.insert(p) {
|
|
return false;
|
|
}
|
|
let Some(d) = doc.get(p).as_dict().cloned() else { return false };
|
|
if dict_has(doc, &d, key) {
|
|
return true;
|
|
}
|
|
parent = d.reference(b"Parent");
|
|
}
|
|
false
|
|
}
|
|
|
|
/// Copy onto `field` what it inherits (`add`, from [`inherited`]), so it keeps it when it moves.
|
|
pub(super) fn own_inherited(doc: &mut Document, field: ObjRef, add: &Dict) -> Result<(), FormError> {
|
|
if add.iter().next().is_some() {
|
|
doc.update_dict(field, |d| {
|
|
for (k, v) in add.iter() {
|
|
d.set(k.to_vec(), v.clone());
|
|
}
|
|
})?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Take `field` out of the field tree: from its parent's `/Kids`, or from `/Fields`.
|
|
pub(super) fn detach(doc: &mut Document, field: ObjRef) -> Result<(), FormError> {
|
|
let parent = doc.get(field).as_dict().and_then(|d| d.reference(b"Parent"));
|
|
let remove_from = |doc: &mut Document, holder: ObjRef, key: &[u8]| -> Result<(), FormError> {
|
|
let mut d = doc.get(holder).as_dict().cloned().unwrap_or_default();
|
|
let list = d.get(key).map(|o| doc.resolve(o)).and_then(|o| o.as_array().cloned()).unwrap_or_default();
|
|
let kept: Vec<Object> = list.into_iter().filter(|o| o.as_ref() != Some(field)).collect();
|
|
d.set(key.to_vec(), Object::Array(kept));
|
|
doc.set(holder, Object::Dict(d));
|
|
Ok(())
|
|
};
|
|
match parent {
|
|
Some(p) => remove_from(doc, p, b"Kids")?,
|
|
None => {
|
|
let root = doc.root().ok_or(FormError::NoForm)?;
|
|
match doc.get(root).as_dict().and_then(|d| d.get(b"AcroForm").cloned()) {
|
|
Some(Object::Ref(af)) => remove_from(doc, af, b"Fields")?,
|
|
// A form dictionary held directly in the catalog.
|
|
Some(Object::Dict(mut af)) => {
|
|
let list = af.get(b"Fields").map(|o| doc.resolve(o)).and_then(|o| o.as_array().cloned()).unwrap_or_default();
|
|
let kept: Vec<Object> = list.into_iter().filter(|o| o.as_ref() != Some(field)).collect();
|
|
af.set(b"Fields".to_vec(), Object::Array(kept));
|
|
doc.update_dict(root, |d| d.set(b"AcroForm".to_vec(), Object::Dict(af)))?;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// The first free "<base>N" name: not a field's, and not held by a node at the top of the field
|
|
/// tree (`held`), such as a parent field left empty by a move, which isn't in `existing`.
|
|
pub(super) fn free_name(existing: &[Field], held: &[String], base: &str) -> String {
|
|
(1..=u64::MAX)
|
|
.map(|n| format!("{base}{n}"))
|
|
.find(|c| !held.contains(c) && !existing.iter().any(|f| f.name == *c || f.name.starts_with(&format!("{c}."))))
|
|
.unwrap_or_else(|| base.to_string())
|
|
}
|
|
|
|
/// The own names (`/T`) of the nodes in the form's `/Fields`, changing nothing.
|
|
pub(super) fn top_names(doc: &Document) -> Vec<String> {
|
|
let Some(root) = doc.root() else { return Vec::new() };
|
|
let Some(af) = doc.get(root).as_dict().and_then(|d| d.get(b"AcroForm").map(|o| doc.resolve(o))).and_then(|o| o.as_dict().cloned()) else {
|
|
return Vec::new();
|
|
};
|
|
let list = af.get(b"Fields").map(|o| doc.resolve(o)).and_then(|o| o.as_array().cloned()).unwrap_or_default();
|
|
list.iter().filter_map(|o| doc.resolve(o).as_dict().and_then(|d| d.get(b"T").cloned())).filter_map(|t| crate::text_of(&doc.resolve(&t))).collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// /Opt is inheritable only for check boxes and radio buttons, using effective FT/Ff.
|
|
#[test]
|
|
fn inherited_options_are_limited_to_checkboxes_and_radios() {
|
|
for (ft, flags, expected) in
|
|
[("Btn", 0, true), ("Btn", crate::flags::RADIO, true), ("Btn", crate::flags::PUSH_BUTTON, false), ("Ch", 0, false), ("Tx", 0, false)]
|
|
{
|
|
for own_type in [false, true] {
|
|
let mut doc = Document::new_empty();
|
|
let mut parent = Dict::new();
|
|
parent.set(b"FT".to_vec(), Object::name(ft));
|
|
parent.set(b"Ff".to_vec(), Object::Int(i64::from(flags)));
|
|
parent.set(b"Opt".to_vec(), Object::Array(vec![Object::String(PdfString::text("export"))]));
|
|
parent.set(b"DA".to_vec(), PdfString::text("/Helv 10 Tf 0 g"));
|
|
let p = doc.add(parent);
|
|
let mut child = Dict::new();
|
|
child.set(b"Parent".to_vec(), Object::Ref(p));
|
|
if own_type {
|
|
child.set(b"FT".to_vec(), Object::name(ft));
|
|
child.set(b"Ff".to_vec(), Object::Int(i64::from(flags)));
|
|
}
|
|
let c = doc.add(child);
|
|
let inherited = inherited(&doc, c);
|
|
assert_eq!(inherited.contains(b"Opt"), expected, "{ft}, flags {flags}, own {own_type}");
|
|
assert!(inherited.contains(b"DA"));
|
|
}
|
|
}
|
|
}
|
|
|
|
fn one_page() -> Document {
|
|
let mut doc = Document::new_empty();
|
|
let pages = doc.get(doc.root().unwrap()).as_dict().unwrap().reference(b"Pages").unwrap();
|
|
let mut page = Dict::new();
|
|
page.set(b"Type".to_vec(), Object::name("Page"));
|
|
page.set(b"Parent".to_vec(), Object::Ref(pages));
|
|
page.set(b"MediaBox".to_vec(), Object::Array(vec![0.into(), 0.into(), 600.into(), 800.into()]));
|
|
let page = doc.add(page);
|
|
doc.update_dict(pages, |d| {
|
|
d.set(b"Kids".to_vec(), Object::Array(vec![Object::Ref(page)]));
|
|
d.set(b"Count".to_vec(), Object::Int(1));
|
|
})
|
|
.unwrap();
|
|
doc
|
|
}
|
|
|
|
fn reopen(doc: &Document) -> Document {
|
|
let bytes = pdfcraft_cos::write_incremental(doc, &pdfcraft_cos::SaveOptions::default()).unwrap();
|
|
hayro_syntax::Pdf::new(bytes.clone()).unwrap();
|
|
Document::open(std::sync::Arc::new(bytes)).unwrap()
|
|
}
|
|
|
|
/// Moving a choice field must not turn its ancestor's unrelated /Opt into local choices.
|
|
#[test]
|
|
fn moving_choice_fields_does_not_copy_ancestor_options() {
|
|
for combo in [false, true] {
|
|
for own_options in [false, true] {
|
|
let mut doc = one_page();
|
|
let options = if own_options { vec!["local".into()] } else { Vec::new() };
|
|
let kind = if combo { crate::NewField::Combo { options, editable: false } } else { crate::NewField::List { options, multi: false } };
|
|
crate::add_field(&mut doc, 0, [50.0, 700.0, 250.0, 720.0], &kind, Some("old.pick")).unwrap();
|
|
let f = crate::fields(&doc).into_iter().find(|f| f.name == "old.pick").unwrap();
|
|
let parent = doc.get(f.obj).as_dict().unwrap().reference(b"Parent").unwrap();
|
|
if !own_options {
|
|
doc.update_dict(f.obj, |d| {
|
|
d.remove(b"Opt");
|
|
})
|
|
.unwrap();
|
|
}
|
|
doc.update_dict(parent, |d| d.set(b"Opt".to_vec(), Object::Array(vec![Object::String(PdfString::text("borrowed"))]))).unwrap();
|
|
let before = crate::fields(&doc).into_iter().find(|f| f.name == "old.pick").unwrap().options;
|
|
crate::set_props(&mut doc, "old.pick", &crate::FieldProps { name: Some("new.pick".into()), ..Default::default() }).unwrap();
|
|
let after = crate::fields(&doc).into_iter().find(|f| f.name == "new.pick").unwrap();
|
|
assert_eq!(after.options, before);
|
|
assert_eq!(doc.get(after.obj).as_dict().unwrap().contains(b"Opt"), own_options);
|
|
let reopened = reopen(&doc);
|
|
let after = crate::fields(&reopened).into_iter().find(|f| f.name == "new.pick").unwrap();
|
|
assert_eq!(after.options, before);
|
|
assert_eq!(reopened.get(after.obj).as_dict().unwrap().contains(b"Opt"), own_options);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn moving_buttons_preserves_inherited_export_mapping() {
|
|
for kind in [crate::NewField::CheckBox, crate::NewField::Radio { group: None, export: "0".into() }] {
|
|
let mut doc = one_page();
|
|
crate::add_field(&mut doc, 0, [50.0, 700.0, 70.0, 720.0], &kind, Some("old.button")).unwrap();
|
|
let field = crate::fields(&doc).into_iter().find(|f| f.name == "old.button").unwrap();
|
|
let state = field.widgets.first().unwrap().on_state.clone().unwrap();
|
|
let parent = doc.get(field.obj).as_dict().unwrap().reference(b"Parent").unwrap();
|
|
let opt = Object::Array(vec![Object::String(PdfString::text("exported"))]);
|
|
doc.update_dict(parent, |d| d.set(b"Opt".to_vec(), opt.clone())).unwrap();
|
|
doc.update_dict(field.obj, |d| {
|
|
d.remove(b"Opt");
|
|
})
|
|
.unwrap();
|
|
crate::set_props(&mut doc, "old.button", &crate::FieldProps { name: Some("new.button".into()), ..Default::default() }).unwrap();
|
|
for doc in [&doc, &reopen(&doc)] {
|
|
let after = crate::fields(doc).into_iter().find(|f| f.name == "new.button").unwrap();
|
|
assert_eq!(doc.get(after.obj).as_dict().unwrap().get(b"Opt"), Some(&opt));
|
|
assert_eq!(after.export_of(0), Some("exported"));
|
|
assert_eq!(after.state_for("exported"), Some(state.as_str()));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn null_entries_on_parents_and_unsigned_signatures_are_treated_as_absent() {
|
|
let mut doc = Document::new_empty();
|
|
let mut sig = Dict::new();
|
|
sig.set(b"FT".to_vec(), Object::name("Sig"));
|
|
sig.set(b"T".to_vec(), PdfString::text("sig"));
|
|
sig.set(b"V".to_vec(), Object::Null);
|
|
let sig_ref = doc.add(Object::Dict(sig));
|
|
assert!(!holds_value(&doc, sig_ref));
|
|
|
|
let mut grand = Dict::new();
|
|
grand.set(b"FT".to_vec(), Object::name("Tx"));
|
|
grand.set(b"MaxLen".to_vec(), Object::Int(10));
|
|
let grand_ref = doc.add(Object::Dict(grand));
|
|
|
|
let mut parent = Dict::new();
|
|
parent.set(b"Parent".to_vec(), Object::Ref(grand_ref));
|
|
parent.set(b"T".to_vec(), PdfString::text("group"));
|
|
parent.set(b"V".to_vec(), Object::Null);
|
|
parent.set(b"MaxLen".to_vec(), Object::Null);
|
|
let parent_ref = doc.add(Object::Dict(parent));
|
|
|
|
let mut child = Dict::new();
|
|
child.set(b"Parent".to_vec(), Object::Ref(parent_ref));
|
|
child.set(b"T".to_vec(), PdfString::text("item"));
|
|
let child_ref = doc.add(Object::Dict(child));
|
|
|
|
let inh = inherited(&doc, child_ref);
|
|
assert_eq!(inh.get(b"MaxLen"), Some(&Object::Int(10)));
|
|
assert!(!inh.contains(b"V"));
|
|
assert!(!ancestor_sets(&doc, child_ref, b"V"));
|
|
assert!(ancestor_sets(&doc, child_ref, b"MaxLen"));
|
|
|
|
// Button /Opt is inherited, text /Opt is not; null kids and /Parent cycles are handled.
|
|
let opt_val = Object::Array(vec![Object::String(PdfString::text("Choice1"))]);
|
|
doc.update_dict(grand_ref, |d| {
|
|
d.set(b"Opt".to_vec(), opt_val.clone());
|
|
d.set(b"Parent".to_vec(), Object::Ref(parent_ref));
|
|
d.set(b"Kids".to_vec(), Object::Array(vec![Object::Null, Object::Ref(parent_ref)]));
|
|
})
|
|
.unwrap();
|
|
assert!(!inherited(&doc, child_ref).contains(b"Opt"));
|
|
doc.update_dict(grand_ref, |d| d.set(b"FT".to_vec(), Object::name("Btn"))).unwrap();
|
|
let inh_cycle = inherited(&doc, child_ref);
|
|
assert_eq!(inh_cycle.get(b"Opt"), Some(&opt_val));
|
|
assert!(!holds_value(&doc, child_ref));
|
|
assert_eq!(child_named(&doc, grand_ref, Some(grand_ref), "group"), Some(parent_ref));
|
|
}
|
|
}
|