Skip to main content

Array

Struct Array 

Source
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

Source

pub fn new() -> Self

An empty array.

Source

pub fn of(values: impl IntoIterator<Item = Object>) -> Self

An array of these values, in order. The counterpart of Dict::from_pairs.

Source

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.

Source

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().

Source

pub fn remove(&mut self, index: usize) -> Option<Object>

Removes and returns the item at index; None when out of range.

Source

pub fn len(&self) -> usize

Number of elements.

Source

pub fn is_empty(&self) -> bool

Whether the array is empty.

Source

pub fn iter(&self) -> impl Iterator<Item = &Object>

The elements, in order.

Source

pub fn as_slice(&self) -> &[Object]

The elements as a slice.

Source

pub fn raw_at(&self, index: usize) -> Option<&Object>

The element at index, whatever its type, without resolving.

Source

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.

Source

pub fn number_at(&self, index: usize) -> Option<f32>

The numeric value at index, coercing integers to f32.

Source

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.

Source

pub fn bool_at(&self, index: usize) -> Option<bool>

The value of a Boolean-typed element. An Int(1) reads as absent.

Source

pub fn name_at(&self, index: usize) -> Option<&Name>

The name at index, only for an actual name.

Source

pub fn string_at(&self, index: usize) -> Option<&PdfString>

The string at index, only for an actual string.

Source

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.

Source

pub fn byte_string_at(&self, index: usize) -> Option<Vec<u8>>

The byte-string spelling at index — see Object::to_byte_string.

Source

pub fn text_at(&self, index: usize) -> Option<String>

The element at index read as text — see Object::to_text.

Source

pub fn reference_at(&self, index: usize) -> Option<ObjRef>

The reference at index, without resolving it.

Source

pub fn get<'a>(&'a self, index: usize, r: &impl Resolve) -> Option<Resolved<'a>>

The element at index, following one level of indirection.

Source

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.

Source

pub fn array_at(&self, index: usize, r: &impl Resolve) -> Option<Array>

The array at index, following one level of indirection.

Source

pub fn stream_at(&self, index: usize, r: &impl Resolve) -> Option<Stream>

The stream at index, following one level of indirection.

Source

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));
Source

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.

Source

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”.

Trait Implementations§

Source§

impl Clone for Array

Source§

fn clone(&self) -> Array

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Array

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Array

Source§

fn default() -> Array

Returns the “default value” for a type. Read more
Source§

impl From<Array> for Object

Source§

fn from(v: Array) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Object> for Array

Source§

fn from_iter<I: IntoIterator<Item = Object>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a> IntoIterator for &'a Array

Source§

type Item = &'a Object

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Object>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Array

Source§

fn eq(&self, other: &Array) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Array

Auto Trait Implementations§

§

impl Freeze for Array

§

impl RefUnwindSafe for Array

§

impl Send for Array

§

impl Sync for Array

§

impl Unpin for Array

§

impl UnsafeUnpin for Array

§

impl UnwindSafe for Array

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.