Class: HexaPDF::Object
- Inherits:
-
Object
- Object
- HexaPDF::Object
- Includes:
- Comparable
- Defined in:
- lib/hexapdf/object.rb
Overview
Objects of the PDF object system.
Overview
A PDF object is like a normal object but with an additional *object identifier* consisting of an object number and a generation number. If the object number is zero, then the PDF object represents a direct object. Otherwise the object identifier uniquely identifies this object as an indirect object and can be used for referencing it (from possibly multiple places).
Furthermore a PDF object may have an associated stream. However, this stream is only accessible if the subclass Stream is used.
A PDF object should be connected to a PDF document, otherwise some methods may not work.
Most PDF objects in a PDF document are represented by subclasses of this class that provide additional functionality.
The methods #hash and #eql? are implemented so that objects of this class can be used as hash keys. Furthermore the implementation is compatible to the one of Reference, i.e. the hash of a PDF Object is the same as the hash of its corresponding Reference object.
Allowed PDF Object Values
The PDF specification knows of the following object types:
-
Boolean (mapped to
true
andfalse
), -
Integer (mapped to Integer object)
-
Real (mapped to Float objects)
-
String (mapped to String objects with UTF-8 or binary encoding)
-
Names (mapped to Symbol objects)
-
Array (mapped to Array objects)
-
Dictionary (mapped to Hash objects)
-
Stream (mapped to the Stream class which is a Dictionary with the associated stream data)
-
Null (mapped to
nil
) -
Indirect Object (mapped to this class)
So working with PDF objects in HexaPDF is rather straightforward since the common Ruby objects can be used for most things, i.e. wrapping an plain Ruby object into an object of this class is not necessary (except if it should become an indirect object).
There are also some additional data structures built from these primitive ones. For example, Time objects are represented as specially formatted string objects and conversion from and to the string representation is handled automatically.
Important: Users of HexaPDF may use other plain Ruby objects but then there is no guarantee that everything will work correctly, especially when using other collection types than arrays and hashes.
See: HexaPDF::Dictionary, HexaPDF::Stream, HexaPDF::Reference, HexaPDF::Document
See: PDF1.7 s7.3.10, s7.3.8
Direct Known Subclasses
Constant Summary collapse
- NOT_DUPLICATABLE_CLASSES =
A list of classes whose objects cannot be duplicated.
[NilClass, FalseClass, TrueClass, Symbol, Integer, Float]
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The wrapped HexaPDF::PDFData value.
-
#document ⇒ Object
Returns the associated PDF document.
-
#must_be_indirect ⇒ Object
writeonly
Sets whether the object has to be an indirect object once it is written.
Class Method Summary collapse
-
.deep_copy(object) ⇒ Object
:call-seq: HexaPDF::Object.deep_copy(object) -> copy.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares this object to another object.
-
#==(other) ⇒ Object
Returns
true
if the other object is an Object and wraps the same #data structure. -
#deep_copy ⇒ Object
Makes a deep copy of the source PDF object and resets the object identifier.
-
#document? ⇒ Boolean
Returns
true
if a PDF document is associated. -
#eql?(other) ⇒ Boolean
Returns
true
if the other object references the same PDF object as this object. -
#gen ⇒ Object
Returns the generation number of the PDF object.
-
#gen=(gen) ⇒ Object
Sets the generation number of the PDF object.
-
#hash ⇒ Object
Computes the hash value based on the object and generation numbers.
-
#indirect? ⇒ Boolean
Returns
true
if the object is an indirect object (i.e. has an object number unequal to zero). -
#initialize(value, document: nil, oid: nil, gen: nil, stream: nil) ⇒ Object
constructor
Creates a new PDF object wrapping the value.
-
#inspect ⇒ Object
:nodoc:.
-
#must_be_indirect? ⇒ Boolean
Returns
true
if the object must be an indirect object once it is written. -
#null? ⇒ Boolean
Returns
true
if the object represents the PDF null object. -
#oid ⇒ Object
Returns the object number of the PDF object.
-
#oid=(oid) ⇒ Object
Sets the object number of the PDF object.
-
#type ⇒ Object
Returns the type (symbol) of the object.
-
#validate(auto_correct: true, &block) ⇒ Object
:call-seq: obj.validate(auto_correct: true) -> true or false obj.validate(auto_correct: true) {|msg, correctable| block } -> true or false.
-
#value ⇒ Object
Returns the object value.
-
#value=(val) ⇒ Object
Sets the object value.
Constructor Details
#initialize(value, document: nil, oid: nil, gen: nil, stream: nil) ⇒ Object
Creates a new PDF object wrapping the value.
The value
can either be a PDFData object in which case it is used directly. If it is a PDF Object, then its data is used. Otherwise the value
object is used as is. In all cases, the oid, gen and stream values may be overridden by the corresponding keyword arguments.
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/hexapdf/object.rb', line 164 def initialize(value, document: nil, oid: nil, gen: nil, stream: nil) @data = case value when PDFData then value when Object then value.data else PDFData.new(value) end @data.oid = oid if oid @data.gen = gen if gen @data.stream = stream if stream self.document = document self.must_be_indirect = false after_data_change end |
Instance Attribute Details
#data ⇒ Object (readonly)
The wrapped HexaPDF::PDFData value.
This attribute is not part of the public API!
151 152 153 |
# File 'lib/hexapdf/object.rb', line 151 def data @data end |
#document ⇒ Object
Returns the associated PDF document.
If no document is associated, an error is raised.
212 213 214 |
# File 'lib/hexapdf/object.rb', line 212 def document @document || raise(HexaPDF::Error, "No document associated with this object (#{inspect})") end |
#must_be_indirect=(value) ⇒ Object (writeonly)
Sets whether the object has to be an indirect object once it is written.
157 158 159 |
# File 'lib/hexapdf/object.rb', line 157 def must_be_indirect=(value) @must_be_indirect = value end |
Class Method Details
.deep_copy(object) ⇒ Object
:call-seq:
HexaPDF::Object.deep_copy(object) -> copy
Creates a deep copy of the given object which retains the references to indirect objects.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/hexapdf/object.rb', line 130 def self.deep_copy(object) case object when Hash object.each_with_object({}) {|(key, val), memo| memo[key] = deep_copy(val)} when Array object.map {|o| deep_copy(o)} when HexaPDF::Object (object.indirect? ? object : deep_copy(object.value)) when HexaPDF::Reference object when *NOT_DUPLICATABLE_CLASSES object else object.dup end end |
Instance Method Details
#<=>(other) ⇒ Object
Compares this object to another object.
If the other object does not respond to oid
or gen
, nil
is returned. Otherwise objects are ordered first by object number and then by generation number.
293 294 295 296 |
# File 'lib/hexapdf/object.rb', line 293 def <=>(other) return nil unless other.respond_to?(:oid) && other.respond_to?(:gen) (oid == other.oid ? gen <=> other.gen : oid <=> other.oid) end |
#==(other) ⇒ Object
Returns true
if the other object is an Object and wraps the same #data structure.
299 300 301 |
# File 'lib/hexapdf/object.rb', line 299 def ==(other) other.kind_of?(Object) && data == other.data end |
#deep_copy ⇒ Object
Makes a deep copy of the source PDF object and resets the object identifier.
279 280 281 282 283 284 285 286 287 |
# File 'lib/hexapdf/object.rb', line 279 def deep_copy obj = dup obj.instance_variable_set(:@data, @data.dup) obj.data.oid = 0 obj.data.gen = 0 obj.data.stream = @data.stream.dup if @data.stream.kind_of?(String) obj.data.value = self.class.deep_copy(@data.value) obj end |
#document? ⇒ Boolean
Returns true
if a PDF document is associated.
217 218 219 |
# File 'lib/hexapdf/object.rb', line 217 def document? !@document.nil? end |
#eql?(other) ⇒ Boolean
Returns true
if the other object references the same PDF object as this object.
304 305 306 |
# File 'lib/hexapdf/object.rb', line 304 def eql?(other) other.respond_to?(:oid) && oid == other.oid && other.respond_to?(:gen) && gen == other.gen end |
#gen ⇒ Object
Returns the generation number of the PDF object.
189 190 191 |
# File 'lib/hexapdf/object.rb', line 189 def gen data.gen end |
#gen=(gen) ⇒ Object
Sets the generation number of the PDF object.
194 195 196 |
# File 'lib/hexapdf/object.rb', line 194 def gen=(gen) data.gen = gen end |
#hash ⇒ Object
Computes the hash value based on the object and generation numbers.
309 310 311 |
# File 'lib/hexapdf/object.rb', line 309 def hash oid.hash ^ gen.hash end |
#indirect? ⇒ Boolean
Returns true
if the object is an indirect object (i.e. has an object number unequal to zero).
223 224 225 |
# File 'lib/hexapdf/object.rb', line 223 def indirect? oid != 0 end |
#inspect ⇒ Object
:nodoc:
313 314 315 |
# File 'lib/hexapdf/object.rb', line 313 def inspect #:nodoc: "#<#{self.class.name} [#{oid}, #{gen}] value=#{value.inspect}>" end |
#must_be_indirect? ⇒ Boolean
Returns true
if the object must be an indirect object once it is written.
228 229 230 |
# File 'lib/hexapdf/object.rb', line 228 def must_be_indirect? @must_be_indirect end |
#null? ⇒ Boolean
Returns true
if the object represents the PDF null object.
248 249 250 |
# File 'lib/hexapdf/object.rb', line 248 def null? value.nil? end |
#oid ⇒ Object
Returns the object number of the PDF object.
179 180 181 |
# File 'lib/hexapdf/object.rb', line 179 def oid data.oid end |
#oid=(oid) ⇒ Object
Sets the object number of the PDF object.
184 185 186 |
# File 'lib/hexapdf/object.rb', line 184 def oid=(oid) data.oid = oid end |
#type ⇒ Object
Returns the type (symbol) of the object.
Since the type system is implemented in such a way as to allow exchanging implementations of specific types, the class of an object can’t be reliably used for determining the actual type.
However, the Type and Subtype fields can easily be used for this. Subclasses for PDF objects that don’t have such fields may use a unique name that has to begin with XX (see PDF1.7 sE.2) and therefore doesn’t clash with names defined by the PDF specification.
For basic objects this always returns :Unknown
.
243 244 245 |
# File 'lib/hexapdf/object.rb', line 243 def type :Unknown end |
#validate(auto_correct: true, &block) ⇒ Object
:call-seq:
obj.validate(auto_correct: true) -> true or false
obj.validate(auto_correct: true) {|msg, correctable| block } -> true or false
Validates the object and, optionally, corrects problems when the option auto_correct
is set. The validation routine itself has to be implemented in the #perform_validation method - see its documentation for more information.
If a block is given, it is called on validation problems with a problem description and whether the problem is correctable.
Returns true
if the object is deemed valid and false
otherwise.
Note: Even if the return value is true
there may be problems since HexaPDF doesn’t currently implement the full PDF spec. However, if the return value is false
, there is certainly a problem!
268 269 270 271 272 273 274 275 276 |
# File 'lib/hexapdf/object.rb', line 268 def validate(auto_correct: true, &block) catch do |catch_tag| perform_validation do |msg, correctable| block.call(msg, correctable) if block throw(catch_tag, false) unless auto_correct && correctable end true end end |
#value ⇒ Object
Returns the object value.
199 200 201 |
# File 'lib/hexapdf/object.rb', line 199 def value data.value end |
#value=(val) ⇒ Object
Sets the object value. Unlike in #initialize the value is used as is!
204 205 206 207 |
# File 'lib/hexapdf/object.rb', line 204 def value=(val) data.value = val after_data_change end |