pub struct Array(/* private fields */);Expand description
A PDF array: an ordered sequence of objects.
§Streams as elements
ISO 32000-1 §7.3.8.1 forbids a file from writing a stream as a direct
array element, and the reader drops one found inline while parsing. That
is a file-format constraint, not an in-memory invariant, and this
type does not police it:
Object::clone_direct flattens
references, so an array of indirect streams clones into one holding those
streams directly, and Array::stream_at reads such an element back.
Enforcing §7.3.8.1 is the writer’s job: pdfrum-edit hoists a direct
stream to an indirect object at serialization time.
use pdfrum_object::{Array, NoResolve, Object};
let a = Array::of([Object::Int(8902), Object::Name("address".into())]);
assert_eq!(a.int_at(0), Some(8902));
assert_eq!(a.name_at(1).and_then(|n| n.as_str()), Some("address"));
// Out of range is absence, never a panic.
assert_eq!(a.int_at(99), None);Implementations§
Source§impl Array
impl Array
Sourcepub fn of(values: impl IntoIterator<Item = Object>) -> Self
pub fn of(values: impl IntoIterator<Item = Object>) -> Self
An array of these values, in order. The counterpart of
Dict::from_pairs.
Sourcepub fn push(&mut self, value: Object)
pub fn push(&mut self, value: Object)
Append an element.
Any object, a stream included — see the type-level note on §7.3.8.1.
Sourcepub fn insert(&mut self, index: usize, value: Object)
pub fn insert(&mut self, index: usize, value: Object)
Inserts value at index, shifting later items; index == len()
appends.
This is the same contract as Vec::insert: a panic here is a
caller bug, not a response to untrusted PDF bytes. Array::remove
returns None out of range because absence is a normal outcome
there.
§Panics
When index > len().
Sourcepub fn remove(&mut self, index: usize) -> Option<Object>
pub fn remove(&mut self, index: usize) -> Option<Object>
Removes and returns the item at index; None when out of range.
Sourcepub fn raw_at(&self, index: usize) -> Option<&Object>
pub fn raw_at(&self, index: usize) -> Option<&Object>
The element at index, whatever its type, without resolving.
Sourcepub fn int_at(&self, index: usize) -> Option<i64>
pub fn int_at(&self, index: usize) -> Option<i64>
The integer value at index in the C-integer view, coercing any type
that has one.
Sourcepub fn number_at(&self, index: usize) -> Option<f32>
pub fn number_at(&self, index: usize) -> Option<f32>
The numeric value at index, coercing integers to f32.
Sourcepub fn number_at_or_zero(&self, index: usize) -> f32
pub fn number_at_or_zero(&self, index: usize) -> f32
The numeric value at index, or 0.0 when it is missing or not a
number. The fallback Array::as_rect and Array::as_matrix use.
Sourcepub fn bool_at(&self, index: usize) -> Option<bool>
pub fn bool_at(&self, index: usize) -> Option<bool>
The value of a Boolean-typed element. An Int(1) reads as absent.
Sourcepub fn name_at(&self, index: usize) -> Option<&Name>
pub fn name_at(&self, index: usize) -> Option<&Name>
The name at index, only for an actual name.
Sourcepub fn string_at(&self, index: usize) -> Option<&PdfString>
pub fn string_at(&self, index: usize) -> Option<&PdfString>
The string at index, only for an actual string.
Sourcepub fn number_obj_at(&self, index: usize) -> Option<&Object>
pub fn number_obj_at(&self, index: usize) -> Option<&Object>
A Number-typed element as an object, without resolving.
This is how a cross-reference stream’s /Index is validated: an
indirect number there is skipped, not chased.
Sourcepub fn byte_string_at(&self, index: usize) -> Option<Vec<u8>>
pub fn byte_string_at(&self, index: usize) -> Option<Vec<u8>>
The byte-string spelling at index — see Object::to_byte_string.
Sourcepub fn text_at(&self, index: usize) -> Option<String>
pub fn text_at(&self, index: usize) -> Option<String>
The element at index read as text — see Object::to_text.
Sourcepub fn reference_at(&self, index: usize) -> Option<ObjRef>
pub fn reference_at(&self, index: usize) -> Option<ObjRef>
The reference at index, without resolving it.
Sourcepub fn get<'a>(&'a self, index: usize, r: &impl Resolve) -> Option<Resolved<'a>>
pub fn get<'a>(&'a self, index: usize, r: &impl Resolve) -> Option<Resolved<'a>>
The element at index, following one level of indirection.
Sourcepub fn dict_at(&self, index: usize, r: &impl Resolve) -> Option<Dict>
pub fn dict_at(&self, index: usize, r: &impl Resolve) -> Option<Dict>
The dictionary at index, following one level of indirection. A
stream answers with its own dictionary.
Sourcepub fn array_at(&self, index: usize, r: &impl Resolve) -> Option<Array>
pub fn array_at(&self, index: usize, r: &impl Resolve) -> Option<Array>
The array at index, following one level of indirection.
Sourcepub fn stream_at(&self, index: usize, r: &impl Resolve) -> Option<Stream>
pub fn stream_at(&self, index: usize, r: &impl Resolve) -> Option<Stream>
The stream at index, following one level of indirection.
Sourcepub fn as_rect(&self) -> Rect
pub fn as_rect(&self) -> Rect
The array read as a rectangle: exactly four elements, or the zero rectangle.
The PDF order is left, bottom, right, top, mapping onto kurbo’s
(x0, y0, x1, y1) in that order. The result is not normalized:
files write inverted boxes and consumers that care normalize
themselves.
use pdfrum_object::{Array, Object};
use pdfrum_common::kurbo::Rect;
let media_box = Array::of([0, 0, 612, 792].map(Object::from));
assert_eq!(media_box.as_rect(), Rect::new(0.0, 0.0, 612.0, 792.0));Sourcepub fn as_matrix(&self) -> Affine
pub fn as_matrix(&self) -> Affine
The array read as a transformation matrix: exactly six elements
[a b c d e f], or the identity.
Sourcepub fn to_numbers(&self) -> Vec<f32>
pub fn to_numbers(&self) -> Vec<f32>
The numbers in the array, missing or non-numeric elements reading as
0.0. Used wherever the specification says “an array of n numbers”.