Expand description
§pdfrum-object
The eight object types of ISO 32000-1 §7.3 — null, boolean, integer, real,
string, name, array, dictionary — plus streams (a dictionary with bytes) and
indirect references (N G R). Construction and typed access only: nothing in
this crate reads a byte of a file, which is what lets a page, a font or an
annotation be built from synthesized objects in a test with no document
behind it.
use pdfrum_object::{Array, Dict, NoResolve, Object, names};
let page = Dict::from_pairs([
(names::TYPE.clone(), Object::Name(names::PAGE.clone())),
(names::RECT.clone(), Object::Array(Array::of([0, 0, 612, 792].map(Object::from)))),
]);
assert_eq!(page.rect(names::RECT, &NoResolve).width(), 612.0);Resolution is one hop, deliberately. A reference whose target is itself a
reference reads as absent rather than being chased, because a file can point
an object at itself and a resolver that loops is a hang on untrusted input.
Accessors come in resolving and non-resolving pairs so a caller says which it
wants at every site; NoResolve is the resolver for values that cannot
contain references, and it is a type rather than an Option so the choice is
visible in the signature.
Numbers have two readings and both are needed. 4294967295 is a valid
integer object and a valid real, and a /Length reading it as one while an
xref offset reads it as the other is a real file’s real behaviour — so
integer and number access are separate methods rather than one lossy
conversion.
names is the workspace’s one declaration site for the specification’s
dictionary keys. A key spelled there is never spelled again at a use site, so
a typo cannot silently produce a lookup that never matches — the failure mode
of string keys is a missing feature, not a compile error. Name is a
Cow<'static, [u8]>, so a constant costs no allocation, and #xx escapes
are resolved on construction so two spellings of one key compare equal.
Part of pdfrum. #![forbid(unsafe_code)].
MIT OR Apache-2.0
Modules§
- names
- The dictionary-key names the specification defines, as constants.
Macros§
- names
- Declare PDF name constants: one table, no desyncing spellings.
Structs§
- Array
- A PDF array: an ordered sequence of objects.
- Byte
Span - A window into a shared byte buffer.
- Dict
- A PDF dictionary: key-value pairs in document order.
- Name
- A PDF name, holding its decoded bytes without the leading
/. - NoResolve
- A resolver that knows nothing, for reading dictionaries whose references are irrelevant (or before a store exists).
- ObjRef
- An indirect object’s identity: the pair a
N G objheader carries and a cross-reference entry keys. - PdfString
- A PDF string object: raw bytes plus the syntax they were written in.
- Stream
- A stream object: a dictionary describing bytes, plus the bytes.
Enums§
- Error
- What can go wrong constructing or resolving PDF objects.
- Object
- A PDF object (ISO 32000-1 §7.3).
- Resolved
- The result of resolving: a direct object stays borrowed, an indirect one arrives shared from the store.
- String
Syntax - How the string was spelled in the file.
Constants§
- INT_
RANGE - The range of integer values a conforming lexer may store in
Object::Int. - PDF_
DOC_ ENCODING PDFDocEncoding(ISO 32000-1 Annex D.2) as a byte-to-code-point table.
Traits§
- Resolve
- A store that can produce the object behind a reference.
Functions§
- decode_
text - Read a text string’s bytes as text (ISO 32000-1 §7.9.2.2).
- encode_
string_ hex - Spell bytes as a hexadecimal string:
<…>with uppercase digit pairs. - encode_
string_ literal - Spell bytes as a literal string:
(…)with\n,\r,\(,\)and\\escaped and every other byte verbatim. - encode_
text - Write text back out as a text string’s bytes (ISO 32000-1 §7.9.2.2).
- fmt_int
- Spell an integer the way the writers do: through the signed 32-bit view,
so a stored
4294967295writes as-1. - fmt_
number - Spell a real the way the content-stream and object writers do.
- name_
decode - Resolve
#xxescapes in a raw name token. - name_
encode - Spell a name’s bytes as file syntax (without the leading
/). - narrow_
to_ signed32 - The integer view of a stored integer: keep the low 32 bits and read them as signed.
- truncate_
to_ signed32 - The integer view of a real: truncate toward zero, saturating at the signed 32-bit bounds, with NaN mapping to 0.
- widen_
to_ f32 - The numeric view of a stored integer: widen it to
f32.