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, which wraps the core crates in one API.
Typed, tolerant views over a PDF's COS graph and the stable LaminaPdfIds that ops, undo and sync address (ADR 0005 §4–§5). Part of Lamina PDF's pure core: bytes in, bytes out.
Document: alaminapdf_cos::Document(it dereferences to one) plus the document-levelIdAllocator, 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 bylaminapdf-ops;cos_mut()is the write accesslaminapdf-opskeeps private.Pages/Page: boundary boxes as normalisedUserSpaceRects (crop, bleed, trim and art boxes clipped and defaulted as §14.11.2 says),Rotation, user unit, thePageFrame, resources, contents and annotation references, page labels. The boxes, rotation, user unit and frame are read once per page and cached inPages, 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.Rotationcomes fromlaminapdf-syntax, and/Rotate 90and/Rotate 90.0read alike. TheRotatePageop replaces the value, so it reads/Rotatewithout theinvalid-rotatewarning: throughPage::rotate_quietlyafter a walk, or throughDocument::page_by_parents, which finds one page by following/Parentinstead of walking the tree, andDocument::read_rotatefor its own entry.- Coordinates and units (ADR 0027, B5a):
Pt(from_mm,from_in),PointPtandRectPtin page space (top-left of the displayed CropBox, y down,/UserUnitapplied);UserSpacePointandUserSpaceRect(formerlyRect) in raw user space.PageFrame { crop box, rotation, user unit }converts withto_page/to_user_space(andto_user_space_matrix);Page::frame()falls back to US Letter (LETTER,[0 0 612 792]) withinvalid-box(MediaBox) when no box has an area. - Graphics values that building and reading share:
Matrix(thenapplies the receiver first,afterits argument first;invert,apply),FillRuleandDash, in whatever space they are used in (page space when building, user space when reading content).laminapdf-build,laminapdf-contentandlaminapdf-renderuse these and define none of their own. Outline/OutlineItem: titles (PDFDocEncoding, UTF-16BE, UTF-8), nesting, open state, destinations resolved toPageId+ view (explicit,/Dests,/Names /Dests,GoToactions), action kinds.Infowith dates asPdfDate(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,/Langand kids (child elements, MCIDs with their page,OBJRs), theRoleMap(standard_kind), the rawParentTreeand/ParentTreeNextKey; elements reached twice or deeper thanmax_tree_depthare skipped withinvalid-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 aPageTreeEditor, which inserts, removes and moves pages byPageIdandPagePosition(insert,insert_new,remove,move_pages).finishwrites the edits through the overlay; dropping the editor writes nothing.- A page that moves keeps the
/Resources,/MediaBox,/CropBoxand/Rotateit showed. Every touched/Countand/Parentis written, and nodes left empty are removed. - The tree is rebuilt balanced only past
PageTreeEditor::MAX_DEPTH(32 levels) orMAX_KIDS(1,024 kids on a touched node). A page dictionary listed twice is copied, with aninvalid-entrywarning onKids. TreeWriterwrites balanced name and number trees from sorted entries (with_max_kids, default 64).PageLabels::remap(before, after)andPageLabels::writekeep every page's label across a change to the page list.Document::page_references(pages)gives aPageReferences: everything that points at those pages. That covers outline items, annotations with a destination, named destinations (name tree and/Dests),/OpenAction, structure elements and theirMCR/OBJRkids (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/Parentis 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, becomenulland are listed inoutside();copy_of(source)says what a source object became (Copied::{Object, Null}). The source is walked before anything is written, so a copy pastLimits::max_decoded_totalstored stream bytes ormax_nesting_depthfails withLimitExceededand leaves the target and the copier unchanged. An encrypted source's streams are copied decrypted, without their/Cryptfilter.LaminaPdfId(p12.0, path IDs such asp5.0/Kids/3for direct dictionaries) and the typedPageId,OutlineItemId,AnnotId,FieldId.
Per-field tolerance follows ADR 0003: 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/).