laminapdf-model 0.9.0

Typed views of a PDF document in Rust: pages, outline, document info, page labels, name trees and the structure tree, read tolerantly.
Documentation
# laminapdf-model

Typed views of a PDF document in Rust: its pages, outline, document info, page labels, name trees and structure tree, each read tolerantly, with stable IDs that edits can refer to. Most programs use it through [`laminapdf`](../laminapdf/README.md), which wraps the core crates in one API.

Typed, tolerant views over a PDF's COS graph and the stable `LaminaPdfId`s that ops, undo and sync address ([ADR 0005](../../docs/decisions/0005-document-model.md) §4–§5). Part of Lamina PDF's pure core: bytes in, bytes out.

- `Document`: a `laminapdf_cos::Document` (it dereferences to one) plus the document-level `IdAllocator`, a view of the COS overlay's number allocator (new objects' IDs come from the numbers it reserves). Views read through the overlay, so they see edits made by [`laminapdf-ops`](../laminapdf-ops/README.md); `cos_mut()` is the write access `laminapdf-ops` keeps private.
- `Pages` / `Page`: boundary boxes as normalised `UserSpaceRect`s (crop, bleed, trim and art boxes clipped and defaulted as §14.11.2 says), `Rotation`, user unit, the `PageFrame`, resources, contents and annotation references, page labels. The boxes, rotation, user unit and frame are read once per page and cached in `Pages`, so their warnings are raised once. This crate is the only reader of the page boxes and `/Rotate`: `laminapdf-cos`'s page tree carries them as stored. `Rotation` comes from `laminapdf-syntax`, and `/Rotate 90` and `/Rotate 90.0` read alike. The `RotatePage` op replaces the value, so it reads `/Rotate` without the `invalid-rotate` warning: through `Page::rotate_quietly` after a walk, or through `Document::page_by_parents`, which finds one page by following `/Parent` instead of walking the tree, and `Document::read_rotate` for its own entry.
- Coordinates and units ([ADR 0027](../../docs/decisions/0027-coordinates-units-and-page-indexing.md), B5a): `Pt` (`from_mm`, `from_in`), `PointPt` and `RectPt` in page space (top-left of the displayed CropBox, y down, `/UserUnit` applied); `UserSpacePoint` and `UserSpaceRect` (formerly `Rect`) in raw user space. `PageFrame { crop box, rotation, user unit }` converts with `to_page` / `to_user_space` (and `to_user_space_matrix`); `Page::frame()` falls back to US Letter (`LETTER`, `[0 0 612 792]`) with `invalid-box` (`MediaBox`) when no box has an area.
- Graphics values that building and reading share: `Matrix` (`then` applies the receiver first, `after` its argument first; `invert`, `apply`), `FillRule` and `Dash`, in whatever space they are used in (page space when building, user space when reading content). `laminapdf-build`, `laminapdf-content` and `laminapdf-render` use these and define none of their own.
- `Outline` / `OutlineItem`: titles (PDFDocEncoding, UTF-16BE, UTF-8), nesting, open state, destinations resolved to `PageId` + view (explicit, `/Dests`, `/Names /Dests`, `GoTo` actions), action kinds.
- `Info` with dates as `PdfDate` (raw string kept when invalid); `NameTree`, `NumberTree`; `PageLabels`.
- `StructTree` / `StructElem` / `StructKid` (`Document::structure()`, B8a): a minimal read-only view of the logical structure: each element's `/S`, `/Pg`, `/Alt`, `/ActualText`, `/E`, `/Lang` and kids (child elements, MCIDs with their page, `OBJR`s), the `RoleMap` (`standard_kind`), the raw `ParentTree` and `/ParentTreeNextKey`; elements reached twice or deeper than `max_tree_depth` are skipped with `invalid-entry`.
- Writing (the editor's foundation, ISO 32000-2 §7.7.3, §7.9.6, §7.9.7; the default feature `edit`, which crates that only read leave off):
  - `Document::edit_page_tree()` gives a `PageTreeEditor`, which inserts, removes and moves pages by `PageId` and `PagePosition` (`insert`, `insert_new`, `remove`, `move_pages`). `finish` writes the edits through the overlay; dropping the editor writes nothing.
  - A page that moves keeps the `/Resources`, `/MediaBox`, `/CropBox` and `/Rotate` it showed. Every touched `/Count` and `/Parent` is written, and nodes left empty are removed.
  - The tree is rebuilt balanced only past `PageTreeEditor::MAX_DEPTH` (32 levels) or `MAX_KIDS` (1,024 kids on a touched node). A page dictionary listed twice is copied, with an `invalid-entry` warning on `Kids`.
  - `TreeWriter` writes balanced name and number trees from sorted entries (`with_max_kids`, default 64).
  - `PageLabels::remap(before, after)` and `PageLabels::write` keep every page's label across a change to the page list.
  - `Document::page_references(pages)` gives a `PageReferences`: everything that points at those pages. That covers outline items, annotations with a destination, named destinations (name tree and `/Dests`), `/OpenAction`, structure elements and their `MCR`/`OBJR` kids (by `/Pg`), and field widgets (by `/P`, else `/Annots`).
- `ObjectCopier` (E2p): copies pages, and every object they reach, from a source document into a target's overlay with fresh numbers. A page's `/Parent` is left out and what it inherited is set on the copy; an object reached from several pages is copied once, and so is a stream stored twice with the same content (matched by a hash of its dictionary, references included, and data); references to pages, page-tree nodes or the catalog that aren't copied, and to annotations of such pages, become `null` and are listed in `outside()`; `copy_of(source)` says what a source object became (`Copied::{Object, Null}`). The source is walked before anything is written, so a copy past `Limits::max_decoded_total` stored stream bytes or `max_nesting_depth` fails with `LimitExceeded` and leaves the target and the copier unchanged. An encrypted source's streams are copied decrypted, without their `/Crypt` filter.
- `LaminaPdfId` (`p12.0`, path IDs such as `p5.0/Kids/3` for direct dictionaries) and the typed `PageId`, `OutlineItemId`, `AnnotId`, `FieldId`.

Per-field tolerance follows [ADR 0003](../../docs/decisions/0003-reader-tolerance-and-limits.md): a malformed field is absent (or its default) plus a warning on the document (`invalid-box`, `invalid-rotate`, `name-tree-node-skipped`, `outline-cycle`, `outline-item-skipped`, `invalid-date`, `invalid-page-label`, `invalid-entry`); strict mode turns each into `Error::Strict`. Tree walks are iterative with visited sets and skip subtrees deeper than `Limits::max_tree_depth`.

Tests: `tests/page_tree.rs` (random edits on `fixtures/pages/` against a flat model, qpdf `--check` on the saved results; `PageLabels::remap`), `tests/trees.rs` (both writers, qpdf's read-back of a `/PageLabels` tree), `tests/references.rs`, `tests/copy.rs` (what a copy carries, shares and leaves out; its limits), `tests/views.rs` (with the frame: every `/Rotate` value against ADR 0027 §3's table is in `src/geometry.rs`), `ids.rs`, `limits.rs`, `corpus.rs` (pypdf's facts in `fixtures/corpus/expected.json`) and `differential.rs` (pypdf over the corpus and fixed-seed mutants, `fixtures/differential/`).