1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//! The page tree: [`Page`]s with their inherited attributes (§7.7.3), the walk and page content.
use std::collections::BTreeSet;
use std::sync::Arc;
use laminapdf_syntax::{Dict, ObjRef, Object};
use super::{Document, Warning};
use crate::error::{Error, Result, absent_if_malformed};
/// A page with its inheritable attributes (§7.7.3.4): `/Resources` resolved, the others as
/// stored. `laminapdf-model`'s page view reads the boxes and `/Rotate` from them.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Page {
/// The page object's reference (`None` for a non-standard direct page dictionary).
pub obj_ref: Option<ObjRef>,
/// The page dictionary itself.
pub dict: Dict,
/// `/Resources`, own or inherited, resolved. Always an [`Object::Dict`]; shared (not
/// copied) between pages that inherit the same resources. See [`Page::resources_dict`].
pub resources: Option<Arc<Object>>,
/// The inheritable attributes as stored (unresolved, own or inherited).
pub inherited: Arc<Inherited>,
/// Where a direct page dictionary sits (`None` when the page is an indirect object).
pub direct_path: Option<DirectPath>,
}
/// The location of a direct (inline) object: its nearest indirect ancestor and the keys and
/// array indices that lead from there to it, e.g. `5 0 R` → `/Kids` → `3` (ADR 0005 §5).
/// The same bytes always give the same path.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub struct DirectPath {
/// The nearest indirect ancestor (`None` when the chain starts at the trailer, i.e. the
/// trailer's `/Root` is itself a direct dictionary).
pub anchor: Option<ObjRef>,
/// The steps from the anchor (from the trailer when `anchor` is `None`).
pub segments: Vec<PathSegment>,
}
/// One step of a [`DirectPath`].
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PathSegment {
/// A dictionary key, e.g. `Kids`.
Key(&'static str),
/// An array index.
Index(usize),
}
impl DirectPath {
/// The path `segments` from `anchor` (`None`: from the trailer).
///
/// Added in 0.2.0.
#[must_use]
pub const fn new(anchor: Option<ObjRef>, segments: Vec<PathSegment>) -> Self {
Self { anchor, segments }
}
fn child(&self, step: PathSegment) -> Self {
let mut segments = Vec::with_capacity(self.segments.len() + 1);
segments.extend_from_slice(&self.segments);
segments.push(step);
Self {
anchor: self.anchor,
segments,
}
}
}
impl Page {
/// The resolved `/Resources` dictionary, if any.
///
/// Added in 0.2.0.
#[must_use]
pub fn resources_dict(&self) -> Option<&Dict> {
match self.resources.as_deref() {
Some(Object::Dict(d)) => Some(d),
_ => None,
}
}
}
/// Inheritable page attributes (§7.7.3.4) as stored, own or inherited and unresolved (a
/// value may be a reference). Carried down the page tree and shared via [`Arc`], so passing
/// them to every kid (and every page) never copies a dictionary.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct Inherited {
/// `/MediaBox`.
pub media_box: Option<Arc<Object>>,
/// `/CropBox`.
pub crop_box: Option<Arc<Object>>,
/// `/Rotate`.
pub rotate: Option<Arc<Object>>,
/// `/Resources`.
pub resources: Option<Arc<Object>>,
}
const INHERITABLE: [&str; 4] = ["MediaBox", "CropBox", "Rotate", "Resources"];
impl Inherited {
/// Each inheritable attribute's key with its value: `/MediaBox`, `/CropBox`, `/Rotate`,
/// `/Resources`.
///
/// Added in 0.2.0.
#[must_use]
pub fn entries(&self) -> [(&'static str, Option<&Arc<Object>>); 4] {
let values = [&self.media_box, &self.crop_box, &self.rotate, &self.resources];
let mut out = [("", None); 4];
for ((slot, key), value) in out.iter_mut().zip(INHERITABLE).zip(values) {
*slot = (key, value.as_ref());
}
out
}
/// `parent` with any attributes `dict` sets itself replacing the inherited ones.
/// Returns `parent` unchanged (a pointer copy) when `dict` sets none of them.
///
/// A value that is `null`, or a reference that `is_null` says resolves to `null` (a missing
/// or free object), counts as absent (§7.3.7, §7.3.10): the inherited value is kept.
///
/// # Errors
/// Whatever `is_null` fails with (it resolves references).
fn overlay(parent: &Arc<Self>, dict: &Dict, is_null: impl Fn(&Object) -> Result<bool>) -> Result<Arc<Self>> {
let mut own: [Option<&Object>; 4] = [None; 4];
for (slot, key) in own.iter_mut().zip(INHERITABLE) {
if let Some(value) = dict.get(key)
&& !is_null(value)?
{
*slot = Some(value);
}
}
if own.iter().all(Option::is_none) {
return Ok(Arc::clone(parent));
}
let [media_box, crop_box, rotate, resources] = own;
let pick = |own: Option<&Object>, inherited: &Option<Arc<Object>>| {
own.map(|o| Arc::new(o.clone())).or_else(|| inherited.clone())
};
Ok(Arc::new(Self {
media_box: pick(media_box, &parent.media_box),
crop_box: pick(crop_box, &parent.crop_box),
rotate: pick(rotate, &parent.rotate),
resources: pick(resources, &parent.resources),
}))
}
}
impl Document {
/// Walks the page tree in document order, applying inherited attributes.
///
/// Tolerant: a node that is missing, unparsable, not a dictionary, deeper than
/// [`Limits::max_page_tree_depth`] or otherwise over a limit (including a missing `/Pages`
/// root) is skipped, as is a `/Kids` that is not an array — each with
/// [`Warning::PageTreeNodeSkipped`]. `/Count` is not trusted: the walk is authoritative,
/// and a root `/Count` that differs raises [`Warning::PageCountMismatch`].
///
/// Page attributes are inherited as stored (the page's own reference is resolved only to
/// tell an explicit `null` from a value); `laminapdf-model` reads the boxes and `/Rotate`. A
/// malformed `/Resources` or `/Count` reads as absent.
///
/// # Errors
/// [`Error::ReferenceCycle`] when a page-tree node is reachable twice (e.g. a
/// self-referencing `/Kids`) or a reference chain loops; [`Error::Strict`] in strict mode
/// for any skip or any warning raised while reading attributes; [`Error::LimitExceeded`]
/// when reading a page attribute exceeds a hard limit; errors from [`Document::catalog`].
///
/// [`Limits::max_page_tree_depth`]: laminapdf_syntax::Limits::max_page_tree_depth
///
/// Added in 0.2.0.
pub fn pages(&self) -> Result<Vec<Page>> {
let _call = self.call();
let catalog = self.catalog()?;
let root = catalog.get("Pages").cloned().unwrap_or(Object::Null);
// A direct `/Pages` sits in the catalog: its path starts there (ADR 0005 §5).
let root_path = (!matches!(root, Object::Ref(_))).then(|| {
let catalog_ref = self.trailer().get("Root").and_then(Object::as_ref);
let segments = match catalog_ref {
Some(_) => vec![PathSegment::Key("Pages")],
None => vec![PathSegment::Key("Root"), PathSegment::Key("Pages")],
};
DirectPath::new(catalog_ref, segments)
});
let mut visited = BTreeSet::new();
let mut stack = vec![(root.clone(), Arc::new(Inherited::default()), 0usize, root_path)];
let mut pages = Vec::new();
while let Some((node_obj, inherited, depth, path)) = stack.pop() {
let obj_ref = node_obj.as_ref();
let skip = || {
self.warn(Warning::PageTreeNodeSkipped {
object: obj_ref.map(|r| r.num),
})
};
// ADR 0003 §3: `max_page_tree_depth` is a hard limit hit inside the walk, one of
// the tolerant paths, so the subtree is skipped with `page-tree-node-skipped`
// (strict mode: `Error::Strict`) instead of failing the call.
if depth > self.limits.max_page_tree_depth() {
skip()?;
continue;
}
if let Some(r) = obj_ref
&& !visited.insert(r.num)
{
return Err(Error::ReferenceCycle(r.num));
}
let resolved = match self.deref(&node_obj) {
Ok(obj) => obj,
Err(e) if e.ends_page_walk() => return Err(e),
Err(_) => {
skip()?;
continue;
}
};
let Object::Dict(node) = &*resolved else {
skip()?;
continue;
};
let attrs = Inherited::overlay(&inherited, node, |o| match o {
Object::Null => Ok(true),
// A malformed object counts as present (it was before): the page keeps the
// attribute and reading it later yields `None`.
Object::Ref(_) => Ok(absent_if_malformed(self.deref(o))?.is_some_and(|r| matches!(*r, Object::Null))),
_ => Ok(false),
})?;
let is_tree_node = node.get_name("Type") == Some(b"Pages")
|| (node.get_name("Type") != Some(b"Page") && node.contains_key("Kids"));
if is_tree_node {
let kids_obj = node.get("Kids").unwrap_or(&Object::Null);
let kids = match self.deref(kids_obj) {
Ok(obj) => obj,
Err(e) if e.ends_page_walk() => return Err(e),
Err(_) => {
skip()?;
continue;
}
};
if let Object::Array(kids) = &*kids {
// A direct kid is named from its nearest indirect ancestor: the `/Kids`
// array when that is an indirect object, else the node, else the node's
// own path. Indirect kids need no path.
let base = match (kids_obj.as_ref(), obj_ref) {
(Some(array), _) => DirectPath::new(Some(array), Vec::new()),
(None, Some(r)) => DirectPath::new(Some(r), vec![PathSegment::Key("Kids")]),
(None, None) => {
let mut base = path.unwrap_or_else(|| DirectPath::new(None, Vec::new()));
base.segments.push(PathSegment::Key("Kids"));
base
}
};
stack.extend(kids.iter().enumerate().rev().map(|(i, kid)| {
let kid_path = (!matches!(kid, Object::Ref(_))).then(|| base.child(PathSegment::Index(i)));
(kid.clone(), Arc::clone(&attrs), depth + 1, kid_path)
}));
} else {
skip()?;
}
} else {
let direct_path = if obj_ref.is_none() { path } else { None };
pages.push(self.make_page(obj_ref, node.clone(), attrs, direct_path)?);
}
}
self.check_page_count(&root, pages.len())?;
Ok(pages)
}
/// Raises [`Warning::PageCountMismatch`] when the root node's `/Count` differs from `walked`.
fn check_page_count(&self, root: &Object, walked: usize) -> Result<()> {
let declared = match absent_if_malformed(self.deref(root))? {
Some(node) => match node.as_dict().and_then(|d| d.get("Count")) {
Some(count) => absent_if_malformed(self.deref(count))?.and_then(|c| c.as_int()),
None => None,
},
None => None,
};
let walked = u64::try_from(walked).unwrap_or(u64::MAX);
match declared {
Some(declared) if u64::try_from(declared).ok() != Some(walked) => self.warn(Warning::PageCountMismatch {
object: root.as_ref().map(|r| r.num),
declared,
walked,
}),
_ => Ok(()),
}
}
/// A page from its dictionary and inherited attributes. Malformed `/Resources` is `None`;
/// cycles, hard limits and strict-mode refusals fail the call.
fn make_page(
&self,
obj_ref: Option<ObjRef>,
dict: Dict,
attrs: Arc<Inherited>,
direct_path: Option<DirectPath>,
) -> Result<Page> {
// A direct dictionary is already shared through `attrs`; a reference resolves to
// the cached object. Either way no dictionary is copied per page.
let resources = match attrs.resources.as_ref() {
Some(o) => match **o {
Object::Ref(r) => absent_if_malformed(self.resolve(r))?,
_ => Some(Arc::clone(o)),
},
None => None,
}
.filter(|shared| matches!(**shared, Object::Dict(_)));
Ok(Page {
obj_ref,
resources,
dict,
inherited: attrs,
direct_path,
})
}
/// Number of leaf pages in the page tree.
///
/// # Errors
/// As [`Document::pages`].
///
/// Added in 0.2.0.
pub fn page_count(&self) -> Result<usize> {
self.pages().map(|p| p.len())
}
/// The decoded content of a page: all `/Contents` streams joined with `\n`. `/Contents`
/// (or an array element) that is `null`, missing or free contributes nothing. All the
/// page's streams share one [`Limits::max_decoded_total`] budget.
///
/// # Errors
/// Decode errors, [`Error::LimitExceeded`], or [`Error::Type`] if `/Contents` is not a
/// stream or array of streams.
///
/// [`Limits::max_decoded_total`]: laminapdf_syntax::Limits::max_decoded_total
///
/// Added in 0.2.0.
pub fn page_content(&self, page: &Page) -> Result<Vec<u8>> {
let _call = self.call();
let Some(contents) = page.dict.get("Contents") else {
return Ok(Vec::new());
};
let resolved = self.deref(contents)?;
let parts = match &*resolved {
Object::Array(items) => items.as_slice(),
single => std::slice::from_ref(single),
};
let mut out = Vec::new();
let mut first = true;
for part in parts {
// The object number names the stream in a `flate-truncated` warning.
let object = match part {
Object::Ref(r) => Some(r.num),
_ => contents.as_ref().map(|r| r.num),
};
let part = self.deref(part)?;
let s = match &*part {
Object::Stream(s) => s,
// A missing or free object is null (§7.3.10): it contributes no content.
Object::Null => continue,
_ => {
return Err(Error::Type {
expected: "stream",
context: "/Contents",
});
}
};
if !first {
out.push(b'\n');
}
first = false;
out.extend_from_slice(&self.decode_stream_of(s, object)?);
}
Ok(out)
}
}