Skip to main content

Crate laminapdf_syntax

Crate laminapdf_syntax 

Source
Expand description

The PDF syntax layer: the object model, its exact serialization, the lexer, the object parser and the stream filters (ISO 32000-2 §7.2–7.4).

use laminapdf_syntax::{Object, parse_object};

let obj = parse_object(b"<< /Kids [3 0 R] /Count 1 >>")?;
assert_eq!(obj.as_dict().and_then(|d| d.get_int("Count")), Some(1));
assert_eq!(obj.to_bytes(), b"<< /Kids [3 0 R] /Count 1 >>");

This crate is part of Lamina PDF’s pure core: it takes bytes and returns values, and never touches the file system, network, clocks or threads, so it builds for native, WebAssembly and mobile targets alike. Every parse path returns Result; no input panics. Recursion only happens in the object parser, which is depth-limited (Limits::max_nesting_depth).

  • Object and friends: the object model and its byte-exact serialization.
  • parse_object / Parser: the tolerant object parser (indirect objects and streams, with an endstream scan when /Length is wrong).
  • Lexer / Token: the tokenizer the parser and the cross-reference reader share.
  • filters: Flate encode/decode within Limits, and PNG predictors.
  • crypt: standard security handler decryption (read only): password authentication for R2–R6 and RC4 / AES-128 / AES-256 string and stream decryption.
  • ErrorKind, Position and Location: the shared parts of the error contract (ADR 0026): kinds and tagged positions; ErrorCode and WarningCode, every code’s kind, hint and docsUrl (generated from fixtures/codes.json); and, with the json feature, Diagnostic, the §1 object every boundary serialises.
  • Limits: the resource limits of ADR 0003, shared by every layer that reads input.
  • Rotation: a rotation by a multiple of 90°, read from /Rotate values and shared by the page model and the renderers.
  • text: text strings (PDFDocEncoding, UTF-16BE, UTF-8) and the PDFDocEncoding table.

The file structure (cross-reference tables, object streams, repair, the page tree) and the writer live one layer up, in laminapdf-cos.

Modules§

crypt
Standard security handler decryption (ISO 32000-2 §7.6; ISO 32000-1 §7.6.3 for R2–R4): password authentication for revisions 2–6 and string/stream decryption with RC4, AES-128-CBC and AES-256-CBC.
filters
Stream filters (ISO 32000-1 §7.4): FlateDecode (via miniz_oxide), LZWDecode, ASCIIHexDecode, ASCII85Decode and RunLengthDecode, with the PNG and TIFF predictors. The image codecs (DCT, JPX, CCITT, JBIG2) are left to their readers: decode reports them as unsupported.
png
The PNG container’s pieces (PNG §5), shared by the crates that read and write PNGs: the signature, the CRC-32 every chunk carries, and writing a chunk. The row filters are filters::unfilter_row and filters::paeth.
text
Text strings (ISO 32000-2 §7.9.2.2): PDFDocEncoding, UTF-16BE with a byte-order mark, and (PDF 2.0) UTF-8 with a byte-order mark.

Structs§

Budget
How much work one step of a job may do, in work units, not time (ADR 0028 §4): the core has no clock, so the same budget gives the same step boundaries everywhere. One unit is one cheap, indivisible piece of work; each job documents its weights. Edges measure time between steps and adapt the next budget.
CancelToken
A cancel flag shared by the jobs and calls it is passed to (ADR 0028 §5). Cloning shares the flag: cancel one clone and every holder sees it.
DecodeBudget
The decoding budget of one top-level call: Limits::max_decoded_total bytes, spent by every filter stage of every stream the call decodes.
Diagnostic
One error or warning as the ADR 0026 §1 object: what the façade’s Error and Warning serialise to, what the WASM bindings throw, as a record of strings and numbers an edge maps one-to-one (the UniFFI Diagnostic record, the TS LaminaPdfError fields; ADR 0028 F9). Serialises with camelCase keys, optional keys omitted (never null).
Dict
An insertion-ordered dictionary. Lookups are linear, which is faster than hashing for the handful of keys real PDF dictionaries carry.
IndirectObject
The result of Parser::indirect_object.
Lexer
A cursor over PDF bytes that yields Tokens.
Limits
Resource limits for reading untrusted PDF input (ADR 0003 §3).
Location
The location of a serialized diagnostic (ADR 0026 §1.1): each field names one kind of position, and all of them are lamina-made values, never document text.
Name
A PDF name, stored as its decoded bytes without the leading /.
ObjRef
A reference to an indirect object: num gen R.
Parser
A parser over PDF bytes, positioned at a byte offset.
PdfString
A PDF string: raw bytes plus the syntax it was written in.
Stream
A stream: its dictionary plus the raw (still encoded) bytes between stream and endstream.
StreamHead
A stream’s header and dictionary, before its data is read (Parser::indirect_head).

Enums§

Error
Everything that can go wrong while lexing, parsing or decoding PDF syntax.
ErrorCode
Every error code lamina’s Rust crates and its WASM binding raise (the codes only @laminapdf/sdk raises are not here), in codes.json order.
ErrorKind
What kind of failure a code is: what the caller can do about it (ADR 0026 §2).
IndirectHead
What Parser::indirect_head read.
Limit
Which hard limit was exceeded (the limit of Error::LimitExceeded).
Object
A PDF object.
Phase
What a job is doing (ADR 0028 §3).
Position
Where an error was found, tagged so that a number has one meaning (ADR 0026 §1.1).
Rotation
A rotation by a multiple of 90°, clockwise when displayed: a page’s /Rotate (§7.7.3.3), or a view rotation added to it.
Step
What a step returns: progress, or the job’s result (Rust-only; edges expose a concrete record per job, ADR 0028 F3).
StringFormat
How a string was (or should be) written: (literal) or <hex>.
Token
One lexical token. Keywords borrow from the input; decoded values are owned.
WarningCode
Every warning code, in codes.json order.

Constants§

DOCS_URL_BASE
The base of every docsUrl (ADR 0026 §6): DOCS_URL_BASE + code + DOCS_URL_SUFFIX.
DOCS_URL_SUFFIX
The suffix of every docsUrl (ADR 0026 §6).

Functions§

did_you_mean
The known names within two edits of given, closest first; names at the same distance in code-point order. Edits count chars, after lowercasing ASCII letters only (fixtures/did-you-mean.json pins the rule for both languages).
endstream_at
Whether endstream follows end after optional white-space: the check that a stream’s declared /Length is right. Sets reached_end when the answer depends on bytes past the end of data.
find
Finds the first occurrence of needle in hay at or after byte offset from, returning its offset. Used to recover stream extents (endstream) and, in laminapdf-cos, to locate file-structure keywords. None when from is past the end or needle is absent.
format_real
Formats a real: never an exponent, at most 6 fractional digits, trailing zeros and a trailing . trimmed, -0 becomes 0. Non-finite values (which PDF cannot express) are written as 0.
is_delimiter
PDF delimiter characters (Table 2).
is_regular
Neither white-space nor delimiter.
is_whitespace
PDF white-space characters (Table 1).
parse_object
Parses a single direct object from input with the default Limits. Trailing white-space and comments are allowed; anything else after the object is an error.
parse_object_with
parse_object with explicit limits.
stream_data_end
The end of stream data that starts at from when its endstream keyword is at idx: the EOL before the keyword (CRLF, LF or CR) is not data. Reads only data[idx - 2..idx].

Type Aliases§

Result
Shorthand for Result<T, laminapdf_syntax::Error>.