Class: HexaPDF::PDFArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hexapdf/pdf_array.rb

Overview

Implementation of the PDF array type.

This is mainly done to provide automatic resolution of indirect object references when using the #[] method. Therefore not all Array methods are implemented - use the #value directly if other methods are needed.

See: PDF2.0 s7.3.6

Direct Known Subclasses

Rectangle

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, field, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, make_direct, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#<<(data) ⇒ Object

Append a value to the array.



105
106
107
# File 'lib/hexapdf/pdf_array.rb', line 105

def <<(data)
  value << data
end

#[](arg1, arg2 = nil) ⇒ Object

:call-seq:

array[index]             -> obj or nil
array[start, length]     -> new_array or nil
array[range]             -> new_array or nil

Returns the value at the given index, or a subarray using the given start and length, or a subarray specified by range.

This method should be used instead of direct access to a value because it provides some advantages:

  • References are automatically resolved.

  • Returns the native Ruby object for values with class HexaPDF::Object. However, all subclasses of HexaPDF::Object are returned as is (it makes no sense, for example, to return the hash that describes the Catalog instead of the Catalog object).

Note: Hash or Array values will always be returned as-is, i.e. not wrapped with Dictionary or PDFArray.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hexapdf/pdf_array.rb', line 71

def [](arg1, arg2 = nil)
  data = arg2 ? value[arg1, arg2] : value[arg1]
  return if data.nil?

  if arg2 || arg1.kind_of?(Range)
    index = (arg2 ? arg1 : arg1.begin)
    data.map! {|item| process_entry(item, index).tap { index += 1 } }
  else
    process_entry(data, arg1)
  end
end

#[]=(index, data) ⇒ Object

Stores the data under the given index in the array.

If the current value for this index has the class HexaPDF::Object (and only this, no subclasses) and the given data has not (including subclasses), the data is stored inside the HexaPDF::Object.



88
89
90
91
92
93
94
95
# File 'lib/hexapdf/pdf_array.rb', line 88

def []=(index, data)
  if value[index].instance_of?(HexaPDF::Object) && !data.kind_of?(HexaPDF::Object) &&
      !data.kind_of?(HexaPDF::Reference)
    value[index].value = data
  else
    value[index] = data
  end
end

#delete(object) ⇒ Object

Deletes all values from the PDFArray that are equal to the given object.

Returns the last deleted item, or nil if no matching item is found.



122
123
124
# File 'lib/hexapdf/pdf_array.rb', line 122

def delete(object)
  value.delete(object)
end

#delete_at(index) ⇒ Object

Deletes the value at the given index.



115
116
117
# File 'lib/hexapdf/pdf_array.rb', line 115

def delete_at(index)
  value.delete_at(index)
end

#eachObject

:call-seq:

array.each {|value| block}    -> array
array.each                    -> Enumerator

Calls the given block once for every value of the array.

Note that the yielded value is already preprocessed like in #[].



181
182
183
184
185
# File 'lib/hexapdf/pdf_array.rb', line 181

def each
  return to_enum(__method__) unless block_given?
  value.each_index {|index| yield(self[index]) }
  self
end

#empty?Boolean

Returns true if the array has no elements.

Returns:

  • (Boolean)


170
171
172
# File 'lib/hexapdf/pdf_array.rb', line 170

def empty?
  value.empty?
end

#index(*obj, &block) ⇒ Object

:call-seq:

array.index(obj)              -> int or nil
array.index {|item| block }   -> int or nil
array.index                   -> Enumerator

Returns the index of the first object such that object is == to obj, or, if a block is given, the index of the first object for which the block returns true.



159
160
161
# File 'lib/hexapdf/pdf_array.rb', line 159

def index(*obj, &block)
  find_index(*obj, &block)
end

#insert(index, *objects) ⇒ Object

Insert one or more values into the array at the given index.



110
111
112
# File 'lib/hexapdf/pdf_array.rb', line 110

def insert(index, *objects)
  value.insert(index, *objects)
end

#lengthObject Also known as: size

Returns the number of elements in the array.



164
165
166
# File 'lib/hexapdf/pdf_array.rb', line 164

def length
  value.length
end

#reject!Object

:call-seq:

array.reject! {|item| block }   -> array or nil
array.reject!                   -> Enumerator

Deletes all elements from the array for which the block returns true. If no changes were done, returns nil.



148
149
150
# File 'lib/hexapdf/pdf_array.rb', line 148

def reject!
  value.reject! {|item| yield(process_entry(item)) }
end

#slice!(arg1, arg2 = nil) ⇒ Object

:call-seq:

array.slice!(index)             -> obj or nil
array.slice!(start, length)     -> new_array or nil
array.slice!(range)             -> new_array or nil

Deletes the element(s) given by an index (and optionally a length) or by a range, and returns them or nil if the index is out of range.



133
134
135
136
137
138
139
140
# File 'lib/hexapdf/pdf_array.rb', line 133

def slice!(arg1, arg2 = nil)
  data = value.slice!(arg1, *arg2)
  if arg2 || arg1.kind_of?(Range)
    data.map! {|item| process_entry(item) }
  else
    process_entry(data)
  end
end

#to_aryObject

Returns an array containing the preprocessed values (like in #[]).



188
189
190
# File 'lib/hexapdf/pdf_array.rb', line 188

def to_ary
  each.to_a
end

#values_at(*indices) ⇒ Object

Returns the values at the given indices.

See #[] for details



100
101
102
# File 'lib/hexapdf/pdf_array.rb', line 100

def values_at(*indices)
  indices.map! {|index| self[index] }
end