laminapdf-model 0.8.0

Typed views of a PDF document in Rust: pages, outline, document info, page labels, name trees and the structure tree, read tolerantly.
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! [`LaminaPdfId`]s (ADR 0005 ยง5): opaque, typed identifiers for pages, outline items,
//! annotations and fields, and the document-level [`IdAllocator`].

use std::fmt::{self, Write as _};
use std::str::FromStr;

use laminapdf_cos::{DirectPath, Error, ObjRef, PathSegment};

/// What a [`LaminaPdfId`] identifies. Its letter starts the string form.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Kind {
    /// A page (`p`).
    Page,
    /// An outline item (`o`).
    OutlineItem,
    /// An annotation (`a`; views arrive with the editor rounds).
    Annot,
    /// An interactive form field (`f`; views arrive with the editor rounds).
    Field,
    /// An attached file's file specification (`e`, for an embedded file).
    ///
    /// Added in 0.6.0.
    Attachment,
}

impl Kind {
    const fn letter(self) -> char {
        match self {
            Self::Page => 'p',
            Self::OutlineItem => 'o',
            Self::Annot => 'a',
            Self::Field => 'f',
            Self::Attachment => 'e',
        }
    }

    const fn from_letter(c: char) -> Option<Self> {
        match c {
            'p' => Some(Self::Page),
            'o' => Some(Self::OutlineItem),
            'a' => Some(Self::Annot),
            'f' => Some(Self::Field),
            'e' => Some(Self::Attachment),
            _ => None,
        }
    }
}

/// Where a [`LaminaPdfId`]'s path starts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Anchor {
    /// An indirect object.
    Object(ObjRef),
    /// The trailer (only when `/Root` itself is direct).
    Trailer,
}

/// One step of a path ID.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Segment {
    Key(Box<str>),
    Index(usize),
}

/// An opaque, typed identifier for an item of the document (ADR 0005 ยง5).
///
/// An item that is an indirect object is identified by its kind plus that object's number
/// and generation; the string form is `p12.0`. A direct (inline) dictionary has no object
/// identity and gets a **path ID** from its nearest indirect ancestor, e.g. `p5.0/Kids/3`,
/// which is stable for the same bytes. The string form is documented as opaque: compare IDs,
/// store them, send them, but don't derive meaning from them.
///
/// ```
/// use laminapdf_model::{Kind, LaminaPdfId};
/// use laminapdf_cos::ObjRef;
///
/// let id = LaminaPdfId::object(Kind::Page, ObjRef::new(12, 0));
/// assert_eq!(id.to_string(), "p12.0");
/// assert_eq!("p12.0".parse::<LaminaPdfId>()?, id);
/// assert_eq!("p5.0/Kids/3".parse::<LaminaPdfId>()?.to_string(), "p5.0/Kids/3");
/// # Ok::<(), laminapdf_model::ParseIdError>(())
/// ```
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LaminaPdfId {
    kind: Kind,
    anchor: Anchor,
    path: Box<[Segment]>,
}

impl LaminaPdfId {
    /// The ID of the indirect object `r` as an item of `kind`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn object(kind: Kind, r: ObjRef) -> Self {
        Self {
            kind,
            anchor: Anchor::Object(r),
            path: Box::new([]),
        }
    }

    /// The path ID of a direct dictionary at `path`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn path(kind: Kind, path: &DirectPath) -> Self {
        let segments = path
            .segments
            .iter()
            .map(|s| match *s {
                PathSegment::Key(k) => Segment::Key(k.into()),
                PathSegment::Index(i) => Segment::Index(i),
            })
            .collect();
        Self {
            kind,
            anchor: path.anchor.map_or(Anchor::Trailer, Anchor::Object),
            path: segments,
        }
    }

    /// What this ID identifies.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn kind(&self) -> Kind {
        self.kind
    }

    /// The indirect object this ID names, when it is an object ID (not a path ID).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn obj_ref(&self) -> Option<ObjRef> {
        match self.anchor {
            Anchor::Object(r) if self.path.is_empty() => Some(r),
            _ => None,
        }
    }

    /// True for a path ID (a direct dictionary).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn is_path(&self) -> bool {
        !self.path.is_empty()
    }
}

impl fmt::Display for LaminaPdfId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.kind.letter())?;
        match self.anchor {
            Anchor::Object(r) => write!(f, "{}.{}", r.num, r.gen_nr)?,
            Anchor::Trailer => f.write_str("t")?,
        }
        for segment in &self.path {
            match segment {
                Segment::Key(k) => write!(f, "/{}", escape_key(k))?,
                Segment::Index(i) => write!(f, "/{i}")?,
            }
        }
        Ok(())
    }
}

/// Why a string is not a [`LaminaPdfId`] (or not one of the expected kind).
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseIdError {
    /// The string does not have the ID syntax. The string is kept but not formatted: it comes
    /// from the caller or an op log (ADR 0026 ยง4).
    #[error("not a lamina ID")]
    Syntax(String),
    /// The ID is valid but of another kind than the typed ID asked for.
    #[error("expected a {expected:?} ID, found a {found:?} ID")]
    WrongKind {
        /// The kind the typed ID requires.
        expected: Kind,
        /// The kind the string has.
        found: Kind,
    },
}

/// A canonical unsigned decimal: digits only, no sign, no leading zero (except `0`).
fn canonical_number<T: FromStr>(s: &str) -> Option<T> {
    let canonical = !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()) && (s == "0" || !s.starts_with('0'));
    canonical.then(|| s.parse().ok()).flatten()
}

/// Writes a dictionary key so it reads back as the same key and never as an index: every
/// byte outside `[A-Za-z0-9]` and a leading digit become `#XX` (as in PDF names), and the
/// empty key is a lone `#`.
fn escape_key(key: &str) -> String {
    if key.is_empty() {
        return "#".to_owned();
    }
    let mut out = String::with_capacity(key.len());
    for (i, b) in key.bytes().enumerate() {
        if b.is_ascii_alphabetic() || (b.is_ascii_digit() && i > 0) {
            out.push(char::from(b));
        } else {
            // Writing to a `String` cannot fail.
            let _ = write!(out, "#{b:02X}");
        }
    }
    out
}

/// The inverse of [`escape_key`]; `None` for anything [`escape_key`] can't have written.
fn unescape_key(part: &str) -> Option<String> {
    if part == "#" {
        return Some(String::new());
    }
    let bytes = part.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while let Some(&b) = bytes.get(i) {
        if b == b'#' {
            let hex = part.get(i + 1..i + 3)?;
            if !hex.bytes().all(|h| h.is_ascii_digit() || (b'A'..=b'F').contains(&h)) {
                return None;
            }
            out.push(u8::from_str_radix(hex, 16).ok()?);
            i += 3;
        } else if b.is_ascii_alphanumeric() && !(i == 0 && b.is_ascii_digit()) {
            out.push(b);
            i += 1;
        } else {
            return None;
        }
    }
    let key = String::from_utf8(out).ok()?;
    // Only the canonical spelling parses, so a string form maps to exactly one ID.
    (escape_key(&key) == part).then_some(key)
}

impl FromStr for LaminaPdfId {
    type Err = ParseIdError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let err = || ParseIdError::Syntax(s.to_owned());
        let mut chars = s.chars();
        let kind = chars.next().and_then(Kind::from_letter).ok_or_else(err)?;
        let mut parts = chars.as_str().split('/');
        let anchor = match parts.next().ok_or_else(err)? {
            "t" => Anchor::Trailer,
            head => {
                let (num, gen_nr) = head.split_once('.').ok_or_else(err)?;
                let num = canonical_number(num).ok_or_else(err)?;
                let gen_nr = canonical_number(gen_nr).ok_or_else(err)?;
                Anchor::Object(ObjRef::new(num, gen_nr))
            }
        };
        let path = parts
            .map(|part| {
                if let Some(i) = canonical_number(part) {
                    Some(Segment::Index(i))
                } else {
                    unescape_key(part).map(|k| Segment::Key(k.into()))
                }
            })
            .collect::<Option<Box<[Segment]>>>()
            .ok_or_else(err)?;
        Ok(Self { kind, anchor, path })
    }
}

macro_rules! typed_id {
    ($(#[$doc:meta])* $name:ident, $kind:expr) => {
        typed_id!($(#[$doc])* $name, $kind, "0.2.0");
    };
    ($(#[$doc:meta])* $name:ident, $kind:expr, $since:literal) => {
        $(#[$doc])*
        #[doc = ""]
        #[doc = concat!("Added in ", $since, ".")]
        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $name(LaminaPdfId);

        impl $name {
            /// The kind every ID of this type has.
            ///
            /// Added in 0.2.0.
            pub const KIND: Kind = $kind;

            /// The ID of the indirect object `r`.
            ///
            /// Added in 0.2.0.
            #[must_use]
            pub fn object(r: ObjRef) -> Self {
                Self(LaminaPdfId::object(Self::KIND, r))
            }

            /// The path ID of a direct dictionary at `path`.
            ///
            /// Added in 0.2.0.
            #[must_use]
            pub fn path(path: &DirectPath) -> Self {
                Self(LaminaPdfId::path(Self::KIND, path))
            }

            /// The untyped ID.
            ///
            /// Added in 0.2.0.
            #[must_use]
            pub const fn as_lamina_id(&self) -> &LaminaPdfId {
                &self.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                self.0.fmt(f)
            }
        }

        impl FromStr for $name {
            type Err = ParseIdError;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Self::try_from(s.parse::<LaminaPdfId>()?)
            }
        }

        impl TryFrom<LaminaPdfId> for $name {
            type Error = ParseIdError;

            fn try_from(id: LaminaPdfId) -> Result<Self, Self::Error> {
                if id.kind == Self::KIND {
                    Ok(Self(id))
                } else {
                    Err(ParseIdError::WrongKind {
                        expected: Self::KIND,
                        found: id.kind,
                    })
                }
            }
        }

        impl From<$name> for LaminaPdfId {
            fn from(id: $name) -> Self {
                id.0
            }
        }
    };
}

typed_id!(
    /// The [`LaminaPdfId`] of a page. Kept when the page moves (page ops never replace the page
    /// object, ADR 0005 ยง5).
    PageId,
    Kind::Page
);
typed_id!(
    /// The [`LaminaPdfId`] of an outline item.
    OutlineItemId,
    Kind::OutlineItem
);
typed_id!(
    /// The [`LaminaPdfId`] of an annotation (views arrive with the editor rounds).
    AnnotId,
    Kind::Annot
);
typed_id!(
    /// The [`LaminaPdfId`] of an interactive form field (views arrive with the editor rounds).
    FieldId,
    Kind::Field
);
typed_id!(
    /// The [`LaminaPdfId`] of an attached file: its file specification, which one entry of the
    /// `/EmbeddedFiles` tree, several `/AF` arrays and a `FileAttachment` annotation may share.
    AttachmentId,
    Kind::Attachment,
    "0.6.0"
);

/// The document-level ID allocator (ADR 0005 ยง5): a borrowed view of the COS overlay's
/// number allocator, returned by [`Document::ids`](crate::Document::ids).
///
/// Existing items need no allocation: their ID is derived from their object (or path) when
/// a view first sees them, so the same bytes always give the same IDs and nothing is
/// recorded per walk. New objects get theirs when the overlay allocates a number for them
/// ([`laminapdf_cos::Document::reserve_number`], also used by [`laminapdf_cos::CosMut::create`]):
/// numbers start above both the trailer's `/Size` and the highest object number in any
/// xref section (as the incremental writer does, so a dangling reference below `/Size`
/// never resolves to a new object), stay within
/// [`Limits::max_object_number`](laminapdf_cos::Limits::max_object_number), and are never
/// reused in a session, so neither are IDs.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy)]
pub struct IdAllocator<'d> {
    cos: &'d laminapdf_cos::Document,
}

impl<'d> IdAllocator<'d> {
    /// The allocator of `cos`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn new(cos: &'d laminapdf_cos::Document) -> Self {
        Self { cos }
    }

    /// Whether this document handed out `id`'s object number (existing items' IDs are
    /// derived, not allocated, so this never depends on which views were read).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn is_allocated(&self, id: &LaminaPdfId) -> bool {
        id.obj_ref()
            .is_some_and(|r| r.gen_nr == 0 && self.cos.is_reserved_number(r.num))
    }

    /// Allocates a fresh object number (generation 0) and its ID as an item of `kind`. The
    /// object itself is created by a later [`laminapdf_cos::CosMut::set`].
    ///
    /// # Errors
    /// [`Error::InvalidStructure`] once the next number would exceed the document's
    /// [`Limits::max_object_number`](laminapdf_cos::Limits::max_object_number).
    ///
    /// Added in 0.2.0.
    pub fn allocate(&self, kind: Kind) -> Result<(ObjRef, LaminaPdfId), Error> {
        let r = self.cos.reserve_number()?;
        Ok((r, LaminaPdfId::object(kind, r)))
    }
}

#[cfg(test)]
mod tests {
    use super::{escape_key, unescape_key};

    #[test]
    fn keys_with_any_characters_round_trip_and_never_read_as_indexes() {
        for key in ["Kids", "My-Key", "A_B", "3D", "0", "", "รœnรฏ", "a/b#c"] {
            let escaped = escape_key(key);
            assert_eq!(unescape_key(&escaped).as_deref(), Some(key), "{key:?} -> {escaped:?}");
            assert!(escaped.parse::<usize>().is_err(), "{key:?} must not look like an index");
        }
    }

    #[test]
    fn non_canonical_key_spellings_are_rejected() {
        for part in ["#4B", "my-key", "#zz", "#4"] {
            assert_eq!(unescape_key(part), None, "{part:?}");
        }
    }

    use super::*;

    fn round_trip(s: &str) -> String {
        s.parse::<LaminaPdfId>().unwrap().to_string()
    }

    #[test]
    fn object_ids_round_trip_through_their_string_form() {
        for s in ["p12.0", "o1.3", "a0.0", "f4294967295.65535"] {
            assert_eq!(round_trip(s), s);
        }
    }

    #[test]
    fn path_ids_round_trip_through_their_string_form() {
        for s in [
            "p5.0/Kids/3",
            "p1.0/Pages/Kids/0/Kids/12",
            "pt/Root/Pages/Kids/0",
            "o9.0/First/Next",
        ] {
            assert_eq!(round_trip(s), s);
        }
    }

    #[test]
    fn parse_rejects_malformed_strings() {
        for s in [
            "",
            "p",
            "x1.0",
            "p1",
            "p1.",
            "p.0",
            "p01.0",
            "p1.00",
            "p-1.0",
            "p1.0/",
            "p1.0//Kids",
            "p1.0/Ki ds",
            "p1.0/3x",
            "p4294967296.0",
            "p1.65536",
            "p1.0/07",
        ] {
            assert!(s.parse::<LaminaPdfId>().is_err(), "{s:?}");
        }
    }

    #[test]
    fn path_id_from_a_direct_path_names_anchor_and_steps() {
        let path = DirectPath::new(
            Some(ObjRef::new(5, 0)),
            vec![PathSegment::Key("Kids"), PathSegment::Index(3)],
        );
        assert_eq!(PageId::path(&path).to_string(), "p5.0/Kids/3");
    }

    #[test]
    fn obj_ref_is_only_set_for_object_ids() {
        let object: LaminaPdfId = "p3.1".parse().unwrap();
        let path: LaminaPdfId = "p3.1/Kids/0".parse().unwrap();
        assert_eq!((object.obj_ref(), path.obj_ref()), (Some(ObjRef::new(3, 1)), None));
    }

    #[test]
    fn typed_ids_reject_another_kind() {
        assert_eq!(
            "o3.0".parse::<PageId>(),
            Err(ParseIdError::WrongKind {
                expected: Kind::Page,
                found: Kind::OutlineItem
            })
        );
    }

    #[test]
    fn typed_ids_parse_their_own_kind() {
        let id: OutlineItemId = "o3.0".parse().unwrap();
        assert_eq!(id, OutlineItemId::object(ObjRef::new(3, 0)));
    }

    #[test]
    fn ids_of_different_kinds_for_one_object_differ() {
        let r = ObjRef::new(7, 0);
        assert_ne!(LaminaPdfId::object(Kind::Page, r), LaminaPdfId::object(Kind::Annot, r));
    }

    #[test]
    fn a_trailer_rooted_empty_path_round_trips() {
        let id = LaminaPdfId::path(Kind::Page, &DirectPath::new(None, Vec::new()));
        assert_eq!(id.to_string().parse::<LaminaPdfId>().unwrap(), id);
    }
}