Class: PDF::Core::DocumentState

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/core/document_state.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DocumentState

Returns a new instance of DocumentState.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pdf/core/document_state.rb', line 4

def initialize(options)
  (options)

  if options[:template]
    @store = PDF::Core::ObjectStore.new(:template => options[:template],
                                          :print_scaling => options[:print_scaling])
    @store.info.data.merge!(options[:info]) if options[:info]
  else
    @store = PDF::Core::ObjectStore.new(:info => options[:info], :print_scaling => options[:print_scaling])
  end

  @version                 = 1.3
  @pages                   = []
  @page                    = nil
  @trailer                 = {}
  @compress                = options.fetch(:compress, false)
  @encrypt                 = options.fetch(:encrypt, false)
  @encryption_key          = options[:encryption_key]
  @optimize_objects        = options.fetch(:optimize_objects, false)
  @skip_encoding           = options.fetch(:skip_encoding, false)
  @before_render_callbacks = []
  @on_page_create_callback = nil
end

Instance Attribute Details

#before_render_callbacksObject

Returns the value of attribute before_render_callbacks.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def before_render_callbacks
  @before_render_callbacks
end

#compressObject

Returns the value of attribute compress.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def compress
  @compress
end

#encryptObject

Returns the value of attribute encrypt.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def encrypt
  @encrypt
end

#encryption_keyObject

Returns the value of attribute encryption_key.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def encryption_key
  @encryption_key
end

#on_page_create_callbackObject

Returns the value of attribute on_page_create_callback.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def on_page_create_callback
  @on_page_create_callback
end

#optimize_objectsObject

Returns the value of attribute optimize_objects.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def optimize_objects
  @optimize_objects
end

#pageObject

Returns the value of attribute page.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def page
  @page
end

#pagesObject

Returns the value of attribute pages.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def pages
  @pages
end

#skip_encodingObject

Returns the value of attribute skip_encoding.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def skip_encoding
  @skip_encoding
end

#storeObject

Returns the value of attribute store.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def store
  @store
end

#trailerObject

Returns the value of attribute trailer.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def trailer
  @trailer
end

#versionObject

Returns the value of attribute version.



28
29
30
# File 'lib/pdf/core/document_state.rb', line 28

def version
  @version
end

Instance Method Details

#before_render_actions(doc) ⇒ Object



61
62
63
# File 'lib/pdf/core/document_state.rb', line 61

def before_render_actions(doc)
  before_render_callbacks.each{ |c| c.call(self) }
end

#insert_page(page, page_number) ⇒ Object



51
52
53
54
55
# File 'lib/pdf/core/document_state.rb', line 51

def insert_page(page, page_number)
  pages.insert(page_number, page)
  store.pages.data[:Kids].insert(page_number, page.dictionary)
  store.pages.data[:Count] += 1
end

#normalize_metadata(options) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/pdf/core/document_state.rb', line 43

def (options)
  options[:info] ||= {}
  options[:info][:Creator] ||= "Prawn"
  options[:info][:Producer] ||= "Prawn"

  options[:info]
end

#on_page_create_action(doc) ⇒ Object



57
58
59
# File 'lib/pdf/core/document_state.rb', line 57

def on_page_create_action(doc)
  on_page_create_callback[doc] if on_page_create_callback
end

#page_countObject



65
66
67
# File 'lib/pdf/core/document_state.rb', line 65

def page_count
  pages.length
end

#populate_pages_from_store(document) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/pdf/core/document_state.rb', line 32

def populate_pages_from_store(document)
  return 0 if @store.page_count <= 0 || @pages.size > 0

  count = (1..@store.page_count)
  @pages = count.map do |index|
    orig_dict_id = @store.object_id_for_page(index)
    PDF::Core::Page.new(document, :object_id => orig_dict_id)
  end

end

#render_body(output) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/pdf/core/document_state.rb', line 69

def render_body(output)
  store.compact if optimize_objects
  store.each do |ref|
    ref.offset = output.size
    output << (@encrypt ? ref.encrypted_object(@encryption_key) :
                          ref.object)
  end
end