prawn 1.0.0.rc2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +9 -0
  3. data/COPYING +2 -2
  4. data/Gemfile +8 -15
  5. data/LICENSE +1 -1
  6. data/Rakefile +25 -16
  7. data/data/images/16bit.alpha +0 -0
  8. data/data/images/16bit.color +0 -0
  9. data/data/images/dice.alpha +0 -0
  10. data/data/images/dice.color +0 -0
  11. data/data/images/indexed_color.dat +0 -0
  12. data/data/images/indexed_color.png +0 -0
  13. data/data/images/license.md +8 -0
  14. data/data/images/page_white_text.alpha +0 -0
  15. data/data/images/page_white_text.color +0 -0
  16. data/lib/prawn.rb +85 -23
  17. data/lib/prawn/document.rb +134 -116
  18. data/lib/prawn/document/bounding_box.rb +33 -4
  19. data/lib/prawn/document/column_box.rb +18 -6
  20. data/lib/prawn/document/graphics_state.rb +11 -74
  21. data/lib/prawn/document/internals.rb +24 -23
  22. data/lib/prawn/document/span.rb +12 -10
  23. data/lib/prawn/encoding.rb +8 -9
  24. data/lib/prawn/errors.rb +13 -32
  25. data/lib/prawn/font.rb +137 -105
  26. data/lib/prawn/font/afm.rb +76 -32
  27. data/lib/prawn/font/dfont.rb +4 -3
  28. data/lib/prawn/font/ttf.rb +33 -25
  29. data/lib/prawn/font_metric_cache.rb +47 -0
  30. data/lib/prawn/graphics.rb +177 -57
  31. data/lib/prawn/graphics/cap_style.rb +4 -3
  32. data/lib/prawn/graphics/color.rb +5 -4
  33. data/lib/prawn/graphics/dash.rb +53 -31
  34. data/lib/prawn/graphics/join_style.rb +9 -7
  35. data/lib/prawn/graphics/patterns.rb +4 -15
  36. data/lib/prawn/graphics/transformation.rb +10 -9
  37. data/lib/prawn/graphics/transparency.rb +3 -1
  38. data/lib/prawn/{layout/grid.rb → grid.rb} +72 -54
  39. data/lib/prawn/image_handler.rb +42 -0
  40. data/lib/prawn/images.rb +58 -54
  41. data/lib/prawn/images/image.rb +6 -22
  42. data/lib/prawn/images/jpg.rb +20 -14
  43. data/lib/prawn/images/png.rb +58 -121
  44. data/lib/prawn/layout.rb +12 -15
  45. data/lib/prawn/measurement_extensions.rb +10 -6
  46. data/lib/prawn/measurements.rb +27 -21
  47. data/lib/prawn/outline.rb +108 -147
  48. data/lib/prawn/repeater.rb +10 -8
  49. data/lib/prawn/security.rb +59 -40
  50. data/lib/prawn/security/arcfour.rb +52 -0
  51. data/lib/prawn/soft_mask.rb +4 -4
  52. data/lib/prawn/stamp.rb +5 -3
  53. data/lib/prawn/table.rb +83 -60
  54. data/lib/prawn/table/cell.rb +17 -21
  55. data/lib/prawn/table/cell/image.rb +2 -3
  56. data/lib/prawn/table/cell/in_table.rb +8 -2
  57. data/lib/prawn/table/cell/span_dummy.rb +5 -0
  58. data/lib/prawn/table/cell/subtable.rb +3 -2
  59. data/lib/prawn/table/cell/text.rb +14 -12
  60. data/lib/prawn/table/cells.rb +58 -14
  61. data/lib/prawn/table/column_width_calculator.rb +61 -0
  62. data/lib/prawn/text.rb +27 -26
  63. data/lib/prawn/text/box.rb +12 -6
  64. data/lib/prawn/text/formatted.rb +5 -4
  65. data/lib/prawn/text/formatted/arranger.rb +290 -0
  66. data/lib/prawn/text/formatted/box.rb +85 -57
  67. data/lib/prawn/text/formatted/fragment.rb +11 -11
  68. data/lib/prawn/text/formatted/line_wrap.rb +266 -0
  69. data/lib/prawn/text/formatted/parser.rb +11 -4
  70. data/lib/prawn/text/formatted/wrap.rb +156 -0
  71. data/lib/prawn/utilities.rb +5 -3
  72. data/manual/document_and_page_options/document_and_page_options.rb +2 -1
  73. data/manual/document_and_page_options/metadata.rb +3 -3
  74. data/manual/document_and_page_options/page_size.rb +2 -2
  75. data/manual/document_and_page_options/print_scaling.rb +20 -0
  76. data/manual/example_file.rb +2 -7
  77. data/manual/example_helper.rb +62 -81
  78. data/manual/graphics/common_lines.rb +2 -0
  79. data/manual/graphics/helper.rb +11 -4
  80. data/manual/graphics/stroke_dash.rb +19 -14
  81. data/manual/manual/cover.rb +16 -0
  82. data/manual/manual/manual.rb +1 -5
  83. data/manual/text/fallback_fonts.rb +4 -4
  84. data/manual/text/formatted_text.rb +5 -5
  85. data/manual/text/inline.rb +2 -4
  86. data/manual/text/registering_families.rb +12 -12
  87. data/manual/text/single_usage.rb +4 -4
  88. data/manual/text/text.rb +0 -2
  89. data/prawn.gemspec +21 -13
  90. data/spec/acceptance/png.rb +23 -0
  91. data/spec/annotations_spec.rb +16 -32
  92. data/spec/bounding_box_spec.rb +22 -5
  93. data/spec/cell_spec.rb +49 -5
  94. data/spec/column_box_spec.rb +32 -0
  95. data/spec/destinations_spec.rb +5 -5
  96. data/spec/document_spec.rb +112 -118
  97. data/spec/extensions/encoding_helpers.rb +5 -2
  98. data/spec/font_metric_cache_spec.rb +52 -0
  99. data/spec/font_spec.rb +121 -120
  100. data/spec/formatted_text_arranger_spec.rb +24 -24
  101. data/spec/formatted_text_box_spec.rb +31 -32
  102. data/spec/formatted_text_fragment_spec.rb +2 -2
  103. data/spec/graphics_spec.rb +63 -45
  104. data/spec/grid_spec.rb +24 -13
  105. data/spec/image_handler_spec.rb +54 -0
  106. data/spec/images_spec.rb +34 -21
  107. data/spec/inline_formatted_text_parser_spec.rb +69 -20
  108. data/spec/jpg_spec.rb +3 -3
  109. data/spec/line_wrap_spec.rb +25 -14
  110. data/spec/measurement_units_spec.rb +5 -5
  111. data/spec/outline_spec.rb +68 -64
  112. data/spec/png_spec.rb +15 -18
  113. data/spec/reference_spec.rb +2 -82
  114. data/spec/repeater_spec.rb +1 -1
  115. data/spec/security_spec.rb +41 -9
  116. data/spec/soft_mask_spec.rb +0 -40
  117. data/spec/span_spec.rb +6 -11
  118. data/spec/spec_helper.rb +20 -2
  119. data/spec/stamp_spec.rb +19 -20
  120. data/spec/stroke_styles_spec.rb +31 -13
  121. data/spec/table/span_dummy_spec.rb +17 -0
  122. data/spec/table_spec.rb +268 -43
  123. data/spec/text_at_spec.rb +13 -27
  124. data/spec/text_box_spec.rb +35 -30
  125. data/spec/text_spec.rb +56 -40
  126. data/spec/transparency_spec.rb +5 -5
  127. metadata +214 -217
  128. data/README.md +0 -98
  129. data/data/fonts/Action Man.dfont +0 -0
  130. data/data/fonts/Activa.ttf +0 -0
  131. data/data/fonts/Chalkboard.ttf +0 -0
  132. data/data/fonts/DejaVuSans.ttf +0 -0
  133. data/data/fonts/Dustismo_Roman.ttf +0 -0
  134. data/data/fonts/comicsans.ttf +0 -0
  135. data/data/fonts/gkai00mp.ttf +0 -0
  136. data/data/images/16bit.dat +0 -0
  137. data/data/images/barcode_issue.png +0 -0
  138. data/data/images/dice.dat +0 -0
  139. data/data/images/page_white_text.dat +0 -0
  140. data/data/images/rails.dat +0 -0
  141. data/data/images/rails.png +0 -0
  142. data/lib/prawn/compatibility.rb +0 -87
  143. data/lib/prawn/core.rb +0 -87
  144. data/lib/prawn/core/annotations.rb +0 -61
  145. data/lib/prawn/core/byte_string.rb +0 -9
  146. data/lib/prawn/core/destinations.rb +0 -90
  147. data/lib/prawn/core/document_state.rb +0 -79
  148. data/lib/prawn/core/literal_string.rb +0 -16
  149. data/lib/prawn/core/name_tree.rb +0 -177
  150. data/lib/prawn/core/object_store.rb +0 -320
  151. data/lib/prawn/core/page.rb +0 -212
  152. data/lib/prawn/core/pdf_object.rb +0 -125
  153. data/lib/prawn/core/reference.rb +0 -119
  154. data/lib/prawn/core/text.rb +0 -268
  155. data/lib/prawn/core/text/formatted/arranger.rb +0 -294
  156. data/lib/prawn/core/text/formatted/line_wrap.rb +0 -288
  157. data/lib/prawn/core/text/formatted/wrap.rb +0 -153
  158. data/lib/prawn/document/page_geometry.rb +0 -136
  159. data/lib/prawn/document/snapshot.rb +0 -89
  160. data/manual/manual/foreword.rb +0 -13
  161. data/manual/templates/full_template.rb +0 -23
  162. data/manual/templates/page_template.rb +0 -47
  163. data/manual/templates/templates.rb +0 -26
  164. data/manual/text/group.rb +0 -29
  165. data/spec/name_tree_spec.rb +0 -112
  166. data/spec/object_store_spec.rb +0 -170
  167. data/spec/pdf_object_spec.rb +0 -172
  168. data/spec/snapshot_spec.rb +0 -186
  169. data/spec/template_spec.rb +0 -351
@@ -1,79 +0,0 @@
1
- module Prawn
2
- module Core
3
- class DocumentState #:nodoc:
4
- def initialize(options)
5
- normalize_metadata(options)
6
-
7
- if options[:template]
8
- @store = Prawn::Core::ObjectStore.new(:template => options[:template])
9
- @store.info.data.merge!(options[:info]) if options[:info]
10
- else
11
- @store = Prawn::Core::ObjectStore.new(:info => options[:info])
12
- end
13
-
14
- @version = 1.3
15
- @pages = []
16
- @page = nil
17
- @trailer = {}
18
- @compress = options.fetch(:compress, false)
19
- @encrypt = options.fetch(:encrypt, false)
20
- @encryption_key = options[:encryption_key]
21
- @optimize_objects = options.fetch(:optimize_objects, false)
22
- @skip_encoding = options.fetch(:skip_encoding, false)
23
- @before_render_callbacks = []
24
- @on_page_create_callback = nil
25
- end
26
-
27
- attr_accessor :store, :version, :pages, :page, :trailer, :compress,
28
- :encrypt, :encryption_key, :optimize_objects, :skip_encoding,
29
- :before_render_callbacks, :on_page_create_callback
30
-
31
- def populate_pages_from_store(document)
32
- return 0 if @store.page_count <= 0 || @pages.size > 0
33
-
34
- count = (1..@store.page_count)
35
- @pages = count.map do |index|
36
- orig_dict_id = @store.object_id_for_page(index)
37
- Prawn::Core::Page.new(document, :object_id => orig_dict_id)
38
- end
39
-
40
- end
41
-
42
- def normalize_metadata(options)
43
- options[:info] ||= {}
44
- options[:info][:Creator] ||= "Prawn"
45
- options[:info][:Producer] ||= "Prawn"
46
-
47
- info = options[:info]
48
- end
49
-
50
- def insert_page(page, page_number)
51
- pages.insert(page_number, page)
52
- store.pages.data[:Kids].insert(page_number, page.dictionary)
53
- store.pages.data[:Count] += 1
54
- end
55
-
56
- def on_page_create_action(doc)
57
- on_page_create_callback[doc] if on_page_create_callback
58
- end
59
-
60
- def before_render_actions(doc)
61
- before_render_callbacks.each{ |c| c.call(self) }
62
- end
63
-
64
- def page_count
65
- pages.length
66
- end
67
-
68
- def render_body(output)
69
- store.compact if optimize_objects
70
- store.each do |ref|
71
- ref.offset = output.size
72
- output << (@encrypt ? ref.encrypted_object(@encryption_key) :
73
- ref.object)
74
- end
75
- end
76
-
77
- end
78
- end
79
- end
@@ -1,16 +0,0 @@
1
- # encoding: utf-8
2
- module Prawn
3
- module Core
4
- # This is used to differentiate strings that must be encoded as
5
- # a *literal* string, versus those that can be encoded in
6
- # the PDF hexadecimal format.
7
- #
8
- # Some features of the PDF format appear to require that literal
9
- # strings be used. One such feature is the /Dest key of a link
10
- # annotation; if a hex encoded string is used there, the links
11
- # do not work (as tested in Mac OS X Preview, and Adobe Acrobat
12
- # Reader).
13
- class LiteralString < String #:nodoc:
14
- end
15
- end
16
- end
@@ -1,177 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # name_tree.rb : Implements NameTree for PDF
4
- #
5
- # Copyright November 2008, Jamis Buck. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
- #
9
- module Prawn
10
- module Core
11
- module NameTree #:nodoc:
12
- class Node #:nodoc:
13
- attr_reader :children
14
- attr_reader :limit
15
- attr_reader :document
16
- attr_accessor :parent
17
- attr_accessor :ref
18
-
19
- def initialize(document, limit, parent=nil)
20
- @document = document
21
- @children = []
22
- @limit = limit
23
- @parent = parent
24
- @ref = nil
25
- end
26
-
27
- def empty?
28
- children.empty?
29
- end
30
-
31
- def size
32
- leaf? ? children.size : children.inject(0) { |sum, child| sum + child.size }
33
- end
34
-
35
- def leaf?
36
- children.empty? || children.first.is_a?(Value)
37
- end
38
-
39
- def add(name, value)
40
- self << Value.new(name, value)
41
- end
42
-
43
- def to_hash
44
- hash = {}
45
-
46
- hash[:Limits] = [least, greatest] if parent
47
- if leaf?
48
- hash[:Names] = children if leaf?
49
- else
50
- hash[:Kids] = children.map { |child| child.ref }
51
- end
52
-
53
- return hash
54
- end
55
-
56
- def least
57
- if leaf?
58
- children.first.name
59
- else
60
- children.first.least
61
- end
62
- end
63
-
64
- def greatest
65
- if leaf?
66
- children.last.name
67
- else
68
- children.last.greatest
69
- end
70
- end
71
-
72
- def <<(value)
73
- if children.empty?
74
- children << value
75
- elsif leaf?
76
- children.insert(insertion_point(value), value)
77
- split! if children.length > limit
78
- else
79
- fit = children.detect { |child| child >= value }
80
- fit = children.last unless fit
81
- fit << value
82
- end
83
-
84
- value
85
- end
86
-
87
- def >=(value)
88
- children.empty? || children.last >= value
89
- end
90
-
91
- def split!
92
- if parent
93
- parent.split(self)
94
- else
95
- left, right = new_node(self), new_node(self)
96
- split_children(self, left, right)
97
- children.replace([left, right])
98
- end
99
- end
100
-
101
- # Returns a deep copy of this node, without copying expensive things
102
- # like the ref to @document.
103
- #
104
- def deep_copy
105
- node = dup
106
- node.instance_variable_set("@children",
107
- Marshal.load(Marshal.dump(children)))
108
- node.instance_variable_set("@ref",
109
- node.ref ? node.ref.deep_copy : nil)
110
- node
111
- end
112
-
113
- protected
114
-
115
- def split(node)
116
- new_child = new_node(self)
117
- split_children(node, node, new_child)
118
- index = children.index(node)
119
- children.insert(index+1, new_child)
120
- split! if children.length > limit
121
- end
122
-
123
- private
124
-
125
- def new_node(parent=nil)
126
- node = Node.new(document, limit, parent)
127
- node.ref = document.ref!(node)
128
- return node
129
- end
130
-
131
- def split_children(node, left, right)
132
- half = (node.limit+1)/2
133
-
134
- left_children, right_children = node.children[0...half], node.children[half..-1]
135
-
136
- left.children.replace(left_children)
137
- right.children.replace(right_children)
138
-
139
- unless node.leaf?
140
- left_children.each { |child| child.parent = left }
141
- right_children.each { |child| child.parent = right }
142
- end
143
- end
144
-
145
- def insertion_point(value)
146
- children.each_with_index do |child, index|
147
- return index if child >= value
148
- end
149
- return children.length
150
- end
151
- end
152
-
153
- class Value #:nodoc:
154
- include Comparable
155
-
156
- attr_reader :name
157
- attr_reader :value
158
-
159
- def initialize(name, value)
160
- @name, @value = Prawn::Core::LiteralString.new(name), value
161
- end
162
-
163
- def <=>(leaf)
164
- name <=> leaf.name
165
- end
166
-
167
- def inspect
168
- "#<Value: #{name.inspect} : #{value.inspect}>"
169
- end
170
-
171
- def to_s
172
- "#{name} : #{value}"
173
- end
174
- end
175
- end
176
- end
177
- end
@@ -1,320 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # prawn/core/object_store.rb : Implements PDF object repository for Prawn
4
- #
5
- # Copyright August 2009, Brad Ediger. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
-
9
-
10
- require 'pdf/reader'
11
-
12
- module Prawn
13
- module Core
14
- class ObjectStore #:nodoc:
15
- include Enumerable
16
-
17
- attr_reader :min_version
18
-
19
- BASE_OBJECTS = %w[info pages root]
20
-
21
- def initialize(opts = {})
22
- @objects = {}
23
- @identifiers = []
24
-
25
- load_file(opts[:template]) if opts[:template]
26
-
27
- @info ||= ref(opts[:info] || {}).identifier
28
- @root ||= ref(:Type => :Catalog).identifier
29
- if pages.nil?
30
- root.data[:Pages] = ref(:Type => :Pages, :Count => 0, :Kids => [])
31
- end
32
- end
33
-
34
- def ref(data, &block)
35
- push(size + 1, data, &block)
36
- end
37
-
38
- def info
39
- @objects[@info]
40
- end
41
-
42
- def root
43
- @objects[@root]
44
- end
45
-
46
- def pages
47
- root.data[:Pages]
48
- end
49
-
50
- def page_count
51
- pages.data[:Count]
52
- end
53
-
54
- # Adds the given reference to the store and returns the reference object.
55
- # If the object provided is not a Prawn::Core::Reference, one is created from the
56
- # arguments provided.
57
- #
58
- def push(*args, &block)
59
- reference = if args.first.is_a?(Prawn::Core::Reference)
60
- args.first
61
- else
62
- Prawn::Core::Reference.new(*args, &block)
63
- end
64
-
65
- @objects[reference.identifier] = reference
66
- @identifiers << reference.identifier
67
- reference
68
- end
69
-
70
- alias_method :<<, :push
71
-
72
- def each
73
- @identifiers.each do |id|
74
- yield @objects[id]
75
- end
76
- end
77
-
78
- def [](id)
79
- @objects[id]
80
- end
81
-
82
- def size
83
- @identifiers.size
84
- end
85
- alias_method :length, :size
86
-
87
- def compact
88
- # Clear live markers
89
- each { |o| o.live = false }
90
-
91
- # Recursively mark reachable objects live, starting from the roots
92
- # (the only objects referenced in the trailer)
93
- root.mark_live
94
- info.mark_live
95
-
96
- # Renumber live objects to eliminate gaps (shrink the xref table)
97
- if @objects.any?{ |_, o| !o.live }
98
- new_id = 1
99
- new_objects = {}
100
- new_identifiers = []
101
-
102
- each do |obj|
103
- if obj.live
104
- obj.identifier = new_id
105
- new_objects[new_id] = obj
106
- new_identifiers << new_id
107
- new_id += 1
108
- end
109
- end
110
-
111
- @objects = new_objects
112
- @identifiers = new_identifiers
113
- end
114
- end
115
-
116
- # returns the object ID for a particular page in the document. Pages
117
- # are indexed starting at 1 (not 0!).
118
- #
119
- # object_id_for_page(1)
120
- # => 5
121
- # object_id_for_page(10)
122
- # => 87
123
- # object_id_for_page(-11)
124
- # => 17
125
- #
126
- def object_id_for_page(k)
127
- k -= 1 if k > 0
128
- flat_page_ids = get_page_objects(pages).flatten
129
- flat_page_ids[k]
130
- end
131
-
132
- # imports all objects required to render a page from another PDF. The
133
- # objects are added to the current object store, but NOT linked
134
- # anywhere.
135
- #
136
- # The object ID of the root Page object is returned, it's up to the
137
- # calling code to link that into the document structure somewhere. If
138
- # this isn't done the imported objects will just be removed when the
139
- # store is compacted.
140
- #
141
- # Imports nothing and returns nil if the requested page number doesn't
142
- # exist. page_num is 1 indexed, so 1 indicates the first page.
143
- #
144
- def import_page(input, page_num)
145
- @loaded_objects = {}
146
- if template_id = indexed_template(input, page_num)
147
- return template_id
148
- end
149
-
150
- io = if input.respond_to?(:seek) && input.respond_to?(:read)
151
- input
152
- elsif File.file?(input.to_s)
153
- StringIO.new(File.binread(input.to_s))
154
- else
155
- raise ArgumentError, "input must be an IO-like object or a filename"
156
- end
157
-
158
- # unless File.file?(filename)
159
- # raise ArgumentError, "#{filename} does not exist"
160
- # end
161
-
162
- hash = indexed_hash(input, io)
163
- ref = hash.page_references[page_num - 1]
164
-
165
- if ref.nil?
166
- nil
167
- else
168
- index_template(input, page_num, load_object_graph(hash, ref).identifier)
169
- end
170
-
171
- rescue PDF::Reader::MalformedPDFError, PDF::Reader::InvalidObjectError
172
- msg = "Error reading template file. If you are sure it's a valid PDF, it may be a bug."
173
- raise Prawn::Errors::TemplateError, msg
174
- rescue PDF::Reader::UnsupportedFeatureError
175
- msg = "Template file contains unsupported PDF features"
176
- raise Prawn::Errors::TemplateError, msg
177
- end
178
-
179
- private
180
-
181
- # An index for page templates so that their loaded object graph
182
- # can be reused without multiple loading
183
- def template_index
184
- @template_index ||= {}
185
- end
186
-
187
- # An index for the read object hash of a pdf template so that the
188
- # object hash does not need to be parsed multiple times when using
189
- # different pages of the pdf as page templates
190
- def hash_index
191
- @hash_index ||= {}
192
- end
193
-
194
- # returns the indexed object graph identifier for a template page if
195
- # it exists
196
- def indexed_template(input, page_number)
197
- key = indexing_key(input)
198
- template_index[key] && template_index[key][page_number]
199
- end
200
-
201
- # indexes the identifier for a page from a template
202
- def index_template(input, page_number, id)
203
- (template_index[indexing_key(input)] ||= {})[page_number] ||= id
204
- end
205
-
206
- # reads and indexes a new IO for a template
207
- # if the IO has been indexed already then the parsed object hash
208
- # is returned directly
209
- def indexed_hash(input, io)
210
- hash_index[indexing_key(input)] ||= PDF::Reader::ObjectHash.new(io)
211
- end
212
-
213
- # the index key for the input.
214
- # uses object_id so that both a string filename or an IO stream can be
215
- # indexed and reused provided the same object gets used in multiple page
216
- # template calls.
217
- def indexing_key(input)
218
- input.object_id
219
- end
220
-
221
- # returns a nested array of object IDs for all pages in this object store.
222
- #
223
- def get_page_objects(obj)
224
- if obj.data[:Type] == :Page
225
- obj.identifier
226
- elsif obj.data[:Type] == :Pages
227
- obj.data[:Kids].map { |kid| get_page_objects(kid) }
228
- end
229
- end
230
-
231
- # takes a source PDF and uses it as a template for this document.
232
- #
233
- def load_file(template)
234
- unless (template.respond_to?(:seek) && template.respond_to?(:read)) ||
235
- File.file?(template)
236
- raise ArgumentError, "#{template} does not exist"
237
- end
238
-
239
- hash = PDF::Reader::ObjectHash.new(template)
240
- src_info = hash.trailer[:Info]
241
- src_root = hash.trailer[:Root]
242
- @min_version = hash.pdf_version.to_f
243
-
244
- if hash.trailer[:Encrypt]
245
- msg = "Template file is an encrypted PDF, it can't be used as a template"
246
- raise Prawn::Errors::TemplateError, msg
247
- end
248
-
249
- if src_info
250
- @info = load_object_graph(hash, src_info).identifier
251
- end
252
-
253
- if src_root
254
- @root = load_object_graph(hash, src_root).identifier
255
- end
256
- rescue PDF::Reader::MalformedPDFError, PDF::Reader::InvalidObjectError
257
- msg = "Error reading template file. If you are sure it's a valid PDF, it may be a bug."
258
- raise Prawn::Errors::TemplateError, msg
259
- rescue PDF::Reader::UnsupportedFeatureError
260
- msg = "Template file contains unsupported PDF features"
261
- raise Prawn::Errors::TemplateError, msg
262
- end
263
-
264
- # recurse down an object graph from a source PDF, importing all the
265
- # indirect objects we find.
266
- #
267
- # hash is the PDF::Reader::ObjectHash to extract objects from, object is
268
- # the object to extract.
269
- #
270
- def load_object_graph(hash, object)
271
- @loaded_objects ||= {}
272
- case object
273
- when ::Hash then
274
- object.each { |key,value| object[key] = load_object_graph(hash, value) }
275
- object
276
- when Array then
277
- object.map { |item| load_object_graph(hash, item)}
278
- when PDF::Reader::Reference then
279
- unless @loaded_objects.has_key?(object.id)
280
- @loaded_objects[object.id] = ref(nil)
281
- new_obj = load_object_graph(hash, hash[object])
282
- if new_obj.kind_of?(PDF::Reader::Stream)
283
- stream_dict = load_object_graph(hash, new_obj.hash)
284
- @loaded_objects[object.id].data = stream_dict
285
- @loaded_objects[object.id] << new_obj.data
286
- else
287
- @loaded_objects[object.id].data = new_obj
288
- end
289
- end
290
- @loaded_objects[object.id]
291
- when PDF::Reader::Stream
292
- # Stream is a subclass of string, so this is here to prevent the stream
293
- # being wrapped in a LiteralString
294
- object
295
- when String
296
- is_utf8?(object) ? object : Prawn::Core::ByteString.new(object)
297
- else
298
- object
299
- end
300
- end
301
-
302
- ruby_18 do
303
- def is_utf8?(str)
304
- begin
305
- str.unpack("U*")
306
- true
307
- rescue
308
- false
309
- end
310
- end
311
- end
312
- ruby_19 do
313
- def is_utf8?(str)
314
- str.force_encoding("utf-8")
315
- str.valid_encoding?
316
- end
317
- end
318
- end
319
- end
320
- end