Class: Prawn::Core::ObjectStore

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/prawn/core/object_store.rb

Overview

:nodoc:

Constant Summary collapse

BASE_OBJECTS =
%w[info pages root]

Instance Method Summary collapse

Constructor Details

#initialize(info = {}) ⇒ ObjectStore

Returns a new instance of ObjectStore.



17
18
19
20
21
22
23
24
25
# File 'lib/prawn/core/object_store.rb', line 17

def initialize(info={})
  @objects = {}
  @identifiers = []
  
  # Create required PDF roots
  @info    = ref(info).identifier
  @pages   = ref(:Type => :Pages, :Count => 0, :Kids => []).identifier
  @root    = ref(:Type => :Catalog, :Pages => pages).identifier
end

Instance Method Details

#[](id) ⇒ Object



60
61
62
# File 'lib/prawn/core/object_store.rb', line 60

def [](id)
  @objects[id]
end

#compactObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/prawn/core/object_store.rb', line 69

def compact
  # Clear live markers
  each { |o| o.live = false }

  # Recursively mark reachable objects live, starting from the roots
  # (the only objects referenced in the trailer)
  root.mark_live
  info.mark_live

  # Renumber live objects to eliminate gaps (shrink the xref table)
  if @objects.any?{ |_, o| !o.live }
    new_id = 1
    new_objects = {}
    new_identifiers = []

    each do |obj|
      if obj.live
        obj.identifier = new_id
        new_objects[new_id] = obj
        new_identifiers << new_id
        new_id += 1
      end
    end

    @objects = new_objects
    @identifiers = new_identifiers
  end
end

#eachObject



54
55
56
57
58
# File 'lib/prawn/core/object_store.rb', line 54

def each
  @identifiers.each do |id|
    yield @objects[id]
  end
end

#push(*args, &block) ⇒ Object Also known as: <<

Adds the given reference to the store and returns the reference object. If the object provided is not a Prawn::Reference, one is created from the arguments provided.



41
42
43
44
45
46
47
48
49
50
# File 'lib/prawn/core/object_store.rb', line 41

def push(*args, &block)
  reference = if args.first.is_a?(Prawn::Reference)
          args.first
        else
          Prawn::Reference.new(*args, &block)
        end
  @objects[reference.identifier] = reference
  @identifiers << reference.identifier
  reference
end

#ref(data, &block) ⇒ Object



27
28
29
# File 'lib/prawn/core/object_store.rb', line 27

def ref(data, &block)
  push(size + 1, data, &block)
end

#sizeObject Also known as: length



64
65
66
# File 'lib/prawn/core/object_store.rb', line 64

def size
  @identifiers.size
end