mod actions;
mod forms;
mod objects;
use std::collections::{BTreeMap, BTreeSet};
use laminapdf_cos::{Object, Result};
use crate::document::Document;
use crate::outline::ActionKind;
use crate::values::text_of;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct XmpPacket {
pub object: Option<u32>,
pub bytes: u64,
pub namespaces: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum EmbeddedFileHolder {
EmbeddedFiles,
AssociatedFiles,
Annotation,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct EmbeddedFile {
pub name: Option<String>,
pub size: Option<u64>,
pub mime: Option<String>,
pub object: Option<u32>,
pub holders: Vec<EmbeddedFileHolder>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Trigger {
OpenAction,
Additional(String),
Activate,
NamedScript(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Owner {
Document,
Page(usize),
Annotation {
page: usize,
object: Option<u32>,
},
FormField(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Action {
pub kind: ActionKind,
pub trigger: Trigger,
pub owner: Owner,
pub object: Option<u32>,
pub script_bytes: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Annotation {
pub subtype: String,
pub page: usize,
pub object: Option<u32>,
pub author: Option<String>,
pub contents: Option<String>,
}
impl Annotation {
#[must_use]
pub fn is_plain(&self) -> bool {
matches!(self.subtype.as_str(), "Link" | "Widget" | "Popup")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FormField {
pub name: String,
pub kind: Option<String>,
pub value: Option<String>,
pub hidden: bool,
pub object: Option<u32>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Disclosure {
pub xmp: Option<XmpPacket>,
pub metadata_streams: Vec<u32>,
pub piece_info: Vec<u32>,
pub thumbnails: Vec<u32>,
pub attachments: Vec<EmbeddedFile>,
pub actions: Vec<Action>,
pub xfa: bool,
pub annotations: Vec<Annotation>,
pub fields: Vec<FormField>,
pub layers_off: Vec<String>,
pub unreferenced: Vec<u32>,
pub linearization: Option<u32>,
}
impl Disclosure {
#[must_use]
pub fn redactions(&self) -> Vec<(usize, u32)> {
let mut out: BTreeMap<usize, u32> = BTreeMap::new();
for annot in self.annotations.iter().filter(|a| a.subtype == "Redact") {
let n = out.entry(annot.page).or_default();
*n = n.saturating_add(1);
}
out.into_iter().collect()
}
}
impl Document {
pub fn disclosure(&self) -> Result<Disclosure> {
let mut scan = Scan {
doc: self,
out: Disclosure::default(),
attachments: BTreeMap::new(),
direct_attachments: Vec::new(),
seen_actions: BTreeSet::new(),
};
let catalog = self.catalog()?;
scan.objects(&catalog)?;
scan.document_actions(&catalog)?;
scan.pages()?;
scan.fields(&catalog)?;
scan.layers(&catalog)?;
let mut attachments: Vec<EmbeddedFile> = std::mem::take(&mut scan.attachments).into_values().collect();
attachments.append(&mut scan.direct_attachments);
for a in &mut attachments {
a.holders.sort_unstable();
a.holders.dedup();
}
scan.out.attachments = attachments;
Ok(scan.out)
}
}
struct Scan<'d> {
doc: &'d Document,
out: Disclosure,
attachments: BTreeMap<u32, EmbeddedFile>,
direct_attachments: Vec<EmbeddedFile>,
seen_actions: BTreeSet<u32>,
}
impl Scan<'_> {
fn read(&self, obj: &Object) -> Result<Option<std::sync::Arc<Object>>> {
self.doc.read(obj)
}
fn hold(&mut self, obj: &Object, holder: EmbeddedFileHolder) -> Result<()> {
if let Some(r) = obj.as_ref() {
if let Some(a) = self.attachments.get_mut(&r.num) {
a.holders.push(holder);
}
return Ok(());
}
if let Some(dict) = obj.as_dict()
&& let Some(mut a) = objects::attachment(self.doc, dict, None)?
{
a.holders.push(holder);
self.direct_attachments.push(a);
}
Ok(())
}
}
fn name_of(obj: &Object) -> Option<String> {
match obj {
Object::Name(n) => Some(String::from_utf8_lossy(n.as_bytes()).into_owned()),
_ => None,
}
}
fn len(n: usize) -> u64 {
u64::try_from(n).unwrap_or(u64::MAX)
}