pdf2 1.6.0

A PDF 2.0 (ISO 32000-2) reader, writer and text extractor.
Documentation
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! The document model: the page tree (§7.7.3) and XMP metadata (§14.3.2).
//!
//! The page tree is walked from the catalog's `/Pages` node. Attributes that
//! the standard marks as inheritable — `/MediaBox`, `/CropBox`, `/Rotate` and
//! `/Resources` — are carried down the walk and overridden where a node or page
//! states its own value, which is what makes "per-page boxes" correct for files
//! that state them once at the root.
//!
//! `/Count` is deliberately not trusted: it is a hint, and a wrong hint would
//! silently hide pages. Pages are counted by walking the tree (§7.7.3.2 says a
//! conforming reader shall not rely on `/Count`).

use alloc::vec;
use alloc::vec::Vec;

use alloc::collections::BTreeSet;

use crate::document::{Document, ObjectSource};
use crate::error::{Error, Result};
use crate::object::{Dictionary, Object, ObjectId};

/// Deepest page tree accepted. Big documents nest a handful of levels; a
/// hostile file must produce an error rather than a stack overflow.
pub const MAX_PAGE_TREE_DEPTH: usize = 64;

/// A rectangle in PDF user space: `[x0 y0 x1 y1]`.
///
/// Coordinates are normalised so that `x0 <= x1` and `y0 <= y1`, because
/// writers disagree about the order of the corners.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
    /// Left edge.
    pub x0: f64,
    /// Bottom edge.
    pub y0: f64,
    /// Right edge.
    pub x1: f64,
    /// Top edge.
    pub y1: f64,
}

impl Rect {
    /// A rectangle from its four coordinates.
    pub fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Self {
        Self {
            x0: if x0 <= x1 { x0 } else { x1 },
            y0: if y0 <= y1 { y0 } else { y1 },
            x1: if x1 >= x0 { x1 } else { x0 },
            y1: if y1 >= y0 { y1 } else { y0 },
        }
    }

    /// Width in user-space units.
    pub fn width(&self) -> f64 {
        self.x1 - self.x0
    }

    /// Height in user-space units.
    pub fn height(&self) -> f64 {
        self.y1 - self.y0
    }

    /// Read a box from a four-element array of numbers.
    pub fn from_array(items: &[Object]) -> Result<Self> {
        if items.len() != 4 {
            return Err(Error::Malformed {
                what: "a page box must be an array of four numbers",
                offset: None,
            });
        }
        let mut values = [0.0_f64; 4];
        for (slot, item) in values.iter_mut().zip(items) {
            *slot = match item {
                Object::Integer(value) => *value as f64,
                Object::Real(value) => *value,
                _ => {
                    return Err(Error::Malformed {
                        what: "a page box must be an array of four numbers",
                        offset: None,
                    });
                }
            };
        }
        Ok(Self::new(values[0], values[1], values[2], values[3]))
    }
}

/// One page, with every inheritable attribute already resolved.
#[derive(Debug, Clone, PartialEq)]
pub struct Page {
    /// The page object's id, absent when the page dictionary sits directly in
    /// its parent's `/Kids` array.
    pub id: Option<ObjectId>,
    /// Effective `/MediaBox`, required by the standard for every page.
    pub media_box: Option<Rect>,
    /// Effective `/CropBox`, which defaults to the media box.
    pub crop_box: Option<Rect>,
    /// Effective `/Rotate`, normalised to 0, 90, 180 or 270.
    pub rotate: i64,
    /// Effective `/Resources`.
    pub resources: Option<Dictionary>,
    /// Content stream ids, in the order they are painted.
    pub contents: Vec<ObjectId>,
}

impl Page {
    /// Whether this page's media box contains the point `(x, y)`.
    pub fn contains(&self, x: f64, y: f64) -> bool {
        match self.media_box {
            Some(rect) => x >= rect.x0 && x <= rect.x1 && y >= rect.y0 && y <= rect.y1,
            None => false,
        }
    }
}

/// Inheritable attributes carried down the page tree.
#[derive(Debug, Clone, Default)]
struct Inherited {
    media_box: Option<Rect>,
    crop_box: Option<Rect>,
    rotate: i64,
    resources: Option<Dictionary>,
}

impl Document {
    /// The catalog dictionary.
    pub fn catalog_dict(&self) -> Result<Dictionary> {
        match self.catalog()? {
            Object::Dictionary(dict) => Ok(dict),
            _ => Err(Error::Malformed {
                what: "catalog /Root is not a dictionary",
                offset: None,
            }),
        }
    }

    /// Walk the page tree and return every page in document order.
    pub fn pages(&self) -> Result<Vec<Page>> {
        pages_of(self)
    }

    /// Number of pages, counted by walking the tree rather than trusting
    /// `/Count`.
    pub fn page_count(&self) -> Result<usize> {
        Ok(self.pages()?.len())
    }

    /// The page at `index`, counting from zero.
    pub fn page(&self, index: usize) -> Result<Page> {
        self.pages()?
            .into_iter()
            .nth(index)
            .ok_or(Error::Malformed {
                what: "page index is past the last page",
                offset: None,
            })
    }

    /// The page's effective `/UserUnit`, defaulting to `1.0` (7.7.3.4).
    ///
    /// The attribute is inheritable, so the page dictionary's `/Parent` chain is
    /// walked; a missing entry, a cycle (bounded at 32 levels) or a
    /// non-positive value stops the walk at `1.0`.
    pub fn page_user_unit(&self, page: &Page) -> Result<f64> {
        let mut current = page.id;
        for _ in 0..32 {
            let Some(id) = current else {
                break;
            };
            let Object::Dictionary(dictionary) = self.get_object(id)? else {
                break;
            };
            if let Some(value) = dictionary.get(b"UserUnit") {
                let value = self.resolve(Some(value))?.and_then(|value| value.as_real());
                return Ok(match value {
                    Some(value) if value.is_finite() && value > 0.0 => value,
                    _ => 1.0,
                });
            }
            current = match dictionary.get(b"Parent") {
                Some(Object::Reference(parent)) => Some(*parent),
                _ => None,
            };
        }
        Ok(1.0)
    }

    /// The decoded content of a page, with its streams joined by a newline.
    ///
    /// This is the input the content-stream interpreter takes.
    pub fn page_content(&self, page: &Page) -> Result<Vec<u8>> {
        let mut out = Vec::new();
        for id in &page.contents {
            let stream = self.stream_data(*id)?;
            if !out.is_empty() {
                out.push(b'\n');
            }
            out.extend_from_slice(&stream);
        }
        Ok(out)
    }

    /// Whether the catalog carries an XMP `/Metadata` stream.
    pub fn has_metadata(&self) -> Result<bool> {
        Ok(self.catalog_dict()?.contains_key(b"Metadata"))
    }

    /// The decoded XMP metadata packet, if the document has one.
    pub fn metadata(&self) -> Result<Option<Vec<u8>>> {
        let catalog = self.catalog_dict()?;
        let Some(metadata) = catalog.get(b"Metadata") else {
            return Ok(None);
        };
        match self.resolve(Some(metadata))? {
            Some(Object::Stream(stream)) => Ok(Some(self.decode_stream(&stream)?)),
            _ => Err(Error::Malformed {
                what: "/Metadata must be a stream",
                offset: None,
            }),
        }
    }
}
/// Walk the page tree of any object source and return every page in order.
pub(crate) fn pages_of<S: ObjectSource + ?Sized>(source: &S) -> Result<Vec<Page>> {
    let catalog = match source.get_object(source.root_id()?)? {
        Object::Dictionary(dict) => dict,
        _ => {
            return Err(Error::Malformed {
                what: "catalog /Root is not a dictionary",
                offset: None,
            });
        }
    };
    let root = catalog.get(b"Pages").ok_or(Error::Malformed {
        what: "catalog without /Pages",
        offset: None,
    })?;
    let mut pages = Vec::new();
    let mut visited = BTreeSet::new();
    collect_pages(
        source,
        root,
        &Inherited::default(),
        0,
        &mut visited,
        &mut pages,
    )?;
    Ok(pages)
}

fn collect_pages<S: ObjectSource + ?Sized>(
    source: &S,
    node: &Object,
    inherited: &Inherited,
    depth: usize,
    visited: &mut BTreeSet<ObjectId>,
    pages: &mut Vec<Page>,
) -> Result<()> {
    if depth > MAX_PAGE_TREE_DEPTH {
        return Err(Error::Malformed {
            what: "page tree is nested too deeply",
            offset: None,
        });
    }
    if pages.len() >= source.page_limit() {
        return Err(Error::TooMany {
            what: "the page tree",
            limit: source.page_limit(),
        });
    }
    let (dict, id) = match node {
        Object::Reference(id) => {
            if !visited.insert(*id) {
                return Err(Error::Malformed {
                    what: "page tree contains a cycle",
                    offset: None,
                });
            }
            match source.get_object(*id)? {
                Object::Dictionary(dict) => (dict, Some(*id)),
                _ => {
                    return Err(Error::Malformed {
                        what: "page tree node is not a dictionary",
                        offset: None,
                    });
                }
            }
        }
        Object::Dictionary(dict) => (dict.clone(), None),
        _ => {
            return Err(Error::Malformed {
                what: "page tree node is not a dictionary or reference",
                offset: None,
            });
        }
    };

    let inherited = Inherited {
        media_box: box_of(source, &dict, b"MediaBox")?.or(inherited.media_box),
        crop_box: box_of(source, &dict, b"CropBox")?.or(inherited.crop_box),
        rotate: match dict.get(b"Rotate") {
            Some(rotate) => normalise_rotate(rotate)?,
            None => inherited.rotate,
        },
        resources: match dict.get(b"Resources") {
            Some(resources) => Some(resources_of(source, resources)?),
            None => inherited.resources.clone(),
        },
    };

    if let Some(kids) = dict.get(b"Kids") {
        // Only the array itself may be indirect: the entries stay
        // references so that each page keeps its object id.
        let kids = match kids {
            Object::Array(items) => items.clone(),
            Object::Reference(id) => match source.get_object(*id)? {
                Object::Array(items) => items,
                _ => {
                    return Err(Error::Malformed {
                        what: "/Kids must be an array",
                        offset: None,
                    });
                }
            },
            _ => {
                return Err(Error::Malformed {
                    what: "/Kids must be an array",
                    offset: None,
                });
            }
        };
        for kid in &kids {
            collect_pages(source, kid, &inherited, depth + 1, visited, pages)?;
        }
        return Ok(());
    }

    let is_page = matches!(
        dict.get(b"Type").and_then(Object::as_name),
        Some(name) if name == b"Page"
    ) || dict.contains_key(b"MediaBox")
        || inherited.media_box.is_some();
    if !is_page {
        return Err(Error::Malformed {
            what: "page tree node has neither /Kids nor /Type /Page",
            offset: None,
        });
    }
    let crop_box = inherited.crop_box.or(inherited.media_box);
    pages.push(Page {
        id,
        media_box: inherited.media_box,
        crop_box,
        rotate: inherited.rotate,
        resources: inherited.resources,
        contents: page_contents(&dict)?,
    });
    Ok(())
}

fn box_of<S: ObjectSource + ?Sized>(
    source: &S,
    dict: &Dictionary,
    key: &[u8],
) -> Result<Option<Rect>> {
    let Some(value) = dict.get(key) else {
        return Ok(None);
    };
    let value = source.resolve(Some(value))?.unwrap_or(Object::Null);
    match value {
        Object::Array(items) => Rect::from_array(&items).map(Some),
        _ => Err(Error::Malformed {
            what: "a page box must be an array of four numbers",
            offset: None,
        }),
    }
}

fn resources_of<S: ObjectSource + ?Sized>(source: &S, resources: &Object) -> Result<Dictionary> {
    match source.resolve(Some(resources))?.unwrap_or(Object::Null) {
        Object::Dictionary(dict) => Ok(dict),
        _ => Err(Error::Malformed {
            what: "/Resources must be a dictionary",
            offset: None,
        }),
    }
}

/// `/Rotate` is a multiple of 90; normalise it into 0, 90, 180 or 270.
fn normalise_rotate(value: &Object) -> Result<i64> {
    let value = match value {
        Object::Integer(value) => *value,
        _ => {
            return Err(Error::Malformed {
                what: "/Rotate must be an integer",
                offset: None,
            });
        }
    };
    if value % 90 != 0 {
        return Err(Error::Malformed {
            what: "/Rotate must be a multiple of 90",
            offset: None,
        });
    }
    Ok(((value % 360) + 360) % 360)
}

/// Content stream ids of a page: one reference or an array of references.
fn page_contents(dict: &Dictionary) -> Result<Vec<ObjectId>> {
    match dict.get(b"Contents") {
        None | Some(Object::Null) => Ok(Vec::new()),
        Some(Object::Reference(id)) => Ok(vec![*id]),
        Some(Object::Array(items)) => {
            let mut contents = Vec::with_capacity(items.len());
            for item in items {
                match item {
                    Object::Reference(id) => contents.push(*id),
                    _ => {
                        return Err(Error::Malformed {
                            what: "/Contents array entries must be stream references",
                            offset: None,
                        });
                    }
                }
            }
            Ok(contents)
        }
        Some(_) => Err(Error::Malformed {
            what: "/Contents must be a stream reference or an array",
            offset: None,
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::{Rect, normalise_rotate, page_contents};
    use crate::object::{Dictionary, Object, ObjectId};

    #[test]
    fn rectangles_normalise_their_corners() {
        let rect = Rect::new(10.0, 20.0, 0.0, 0.0);
        assert_eq!(
            rect,
            Rect {
                x0: 0.0,
                y0: 0.0,
                x1: 10.0,
                y1: 20.0
            }
        );
        assert_eq!(rect.width(), 10.0);
        assert_eq!(rect.height(), 20.0);
    }

    #[test]
    fn rectangles_read_integer_and_real_arrays() {
        let rect = Rect::from_array(&[
            Object::Integer(0),
            Object::Integer(0),
            Object::Real(612.5),
            Object::Integer(792),
        ])
        .expect("rectangle");
        assert_eq!(rect.x1, 612.5);
        assert_eq!(rect.y1, 792.0);
    }

    #[test]
    fn malformed_rectangles_are_errors() {
        assert!(Rect::from_array(&[Object::Integer(0), Object::Integer(0)]).is_err());
        assert!(
            Rect::from_array(&[
                Object::Integer(0),
                Object::Integer(0),
                Object::Integer(1),
                Object::name("top"),
            ])
            .is_err()
        );
    }

    #[test]
    fn rotations_are_normalised() {
        assert_eq!(normalise_rotate(&Object::Integer(0)).expect("0"), 0);
        assert_eq!(normalise_rotate(&Object::Integer(90)).expect("90"), 90);
        assert_eq!(normalise_rotate(&Object::Integer(450)).expect("450"), 90);
        assert_eq!(normalise_rotate(&Object::Integer(-90)).expect("-90"), 270);
        assert_eq!(normalise_rotate(&Object::Integer(360)).expect("360"), 0);
        assert!(normalise_rotate(&Object::Integer(45)).is_err());
        assert!(normalise_rotate(&Object::Real(90.0)).is_err());
    }

    #[test]
    fn contents_accept_one_reference_or_an_array() {
        let mut dict = Dictionary::new();
        assert!(page_contents(&dict).expect("absent").is_empty());

        dict.insert("Contents", Object::reference(ObjectId::new(4, 0)));
        assert_eq!(
            page_contents(&dict).expect("single"),
            vec![ObjectId::new(4, 0)]
        );

        dict.insert(
            "Contents",
            Object::array([
                Object::reference(ObjectId::new(4, 0)),
                Object::reference(ObjectId::new(5, 0)),
            ]),
        );
        assert_eq!(
            page_contents(&dict).expect("array"),
            vec![ObjectId::new(4, 0), ObjectId::new(5, 0)]
        );

        dict.insert("Contents", Object::Integer(1));
        assert!(page_contents(&dict).is_err());
    }
}