module Origami::Object

Parent module representing a PDF Object. PDF specification declares a set of primitive object types :

Attributes

file_offset[RW]
generation[RW]
no[RW]
objstm_offset[RW]
parent[RW]

Public Class Methods

included(base) click to toggle source

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
new(*cons) click to toggle source

Creates a new PDF Object.

Calls superclass method
# File lib/origami/object.rb, line 352
def initialize(*cons)
    @indirect = false
    @no, @generation = 0, 0
    @document = nil
    @parent = nil

    super(*cons) unless cons.empty?
end

Public Instance Methods

<=>(obj) click to toggle source

Compare two objects from their respective numbers.

# File lib/origami/object.rb, line 398
def <=>(obj)
    [@no, @generation] <=> [obj.no, obj.generation]
end
copy() click to toggle source

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
document() click to toggle source

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
export() click to toggle source

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
indirect?() click to toggle source

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
indirect_parent() click to toggle source

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
native_type() click to toggle source

Returns the native type of the Object.

# File lib/origami/object.rb, line 345
def native_type
    self.class.native_type
end
output(data)
Alias for: to_s
post_build() click to toggle source

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
pre_build() click to toggle source

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
reference() click to toggle source

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
set_document(doc) click to toggle source
# 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
set_indirect(bool) click to toggle source

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
solve() click to toggle source

Returns self.

# File lib/origami/object.rb, line 554
def solve
    self
end
to_o() click to toggle source

Returns self.

# File lib/origami/object.rb, line 547
def to_o
    self
end
to_obfuscated_str(data)
Alias for: to_s
to_s(data) click to toggle source

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
Also aliased as: to_obfuscated_str, output
type() click to toggle source

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
xrefs() click to toggle source

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