module Origami::Object
Parent module representing a PDF
Object
. PDF
specification declares a set of primitive object types :
Attributes
Public Class Methods
Modules or classes including this module are considered native types.
# File lib/origami/object.rb, line 322 def self.included(base) base.class_variable_set(:@@native_type, base) base.extend(ClassMethods) end
Public Instance Methods
Compare two objects from their respective numbers.
# File lib/origami/object.rb, line 398 def <=>(obj) [@no, @generation] <=> [obj.no, obj.generation] end
Deep copy of an object.
# File lib/origami/object.rb, line 412 def copy saved_doc = @document saved_parent = @parent @document = @parent = nil # do not process parent object and document in the copy # Perform the recursive copy (quite dirty). copyobj = Marshal.load(Marshal.dump(self)) # restore saved values @document = saved_doc @parent = saved_parent copyobj.set_document(saved_doc) if copyobj.indirect? copyobj.parent = parent copyobj end
Returns the PDF
which the object belongs to.
# File lib/origami/object.rb, line 561 def document if self.indirect? then @document else @parent.document unless @parent.nil? end end
Creates an exportable version of current object. The exportable version is a copy of self with solved references, no owning PDF
and no parent. References to Catalog
or PageTreeNode
objects have been destroyed.
When exported, an object can be moved into another document without hassle.
# File lib/origami/object.rb, line 469 def export exported_obj = self.logicalize exported_obj.no = exported_obj.generation = 0 exported_obj.set_document(nil) if exported_obj.indirect? exported_obj.parent = nil exported_obj.xref_cache.clear exported_obj end
Returns whether the objects is indirect, which means that it is not embedded into another object.
# File lib/origami/object.rb, line 405 def indirect? @indirect end
Returns the indirect object which contains this object. If the current object is already indirect, returns self.
# File lib/origami/object.rb, line 537 def indirect_parent obj = self obj = obj.parent until obj.indirect? obj end
Returns the native type of the Object
.
# File lib/origami/object.rb, line 345 def native_type self.class.native_type end
Generic method called just after the object is finalized. At this time, any indirect object has its own number and generation identifier.
# File lib/origami/object.rb, line 391 def post_build self end
Generic method called just before the object is finalized. At this time, no number nor generation allocation has yet been done.
# File lib/origami/object.rb, line 383 def pre_build self end
Returns an indirect reference to this object, or a Null
object is this object is not indirect.
# File lib/origami/object.rb, line 434 def reference raise InvalidObjectError, "Cannot reference a direct object" unless self.indirect? ref = Reference.new(@no, @generation) ref.parent = self ref end
# File lib/origami/object.rb, line 568 def set_document(doc) raise InvalidObjectError, "You cannot set the document of a direct object" unless self.indirect? @document = doc end
Sets whether the object is indirect or not. Indirect objects are allocated numbers at build time.
# File lib/origami/object.rb, line 365 def set_indirect(bool) unless bool == true or bool == false raise TypeError, "The argument must be boolean" end if bool == false @no = @generation = 0 @document = nil end @indirect = bool self end
Returns self.
# File lib/origami/object.rb, line 554 def solve self end
Returns self.
# File lib/origami/object.rb, line 547 def to_o self end
Outputs this object into PDF
code.
- data
-
The object data.
# File lib/origami/object.rb, line 681 def to_s(data) content = "" content << "#{no} #{generation} #{TOKENS.first}" << EOL if self.indirect? content << data content << EOL << TOKENS.last << EOL if self.indirect? content.force_encoding('binary') end
Returns the symbol type of this Object
.
# File lib/origami/object.rb, line 663 def type name = (self.class.name or self.class.superclass.name or self.native_type.name) name.split("::").last.to_sym end
Returns an array of references pointing to the current object.
# File lib/origami/object.rb, line 446 def xrefs raise InvalidObjectError, "Cannot find xrefs to a direct object" unless self.indirect? raise InvalidObjectError, "Not attached to any document" if self.document.nil? @document.each_object(compressed: true) .flat_map { |object| case object when Stream object.dictionary.xref_cache[self.reference] when Dictionary, Array object.xref_cache[self.reference] end } .compact! end