File: renderer.rb

package info (click to toggle)
ruby-pdf-core 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 408 kB
  • sloc: ruby: 2,270; makefile: 4
file content (349 lines) | stat: -rw-r--r-- 10,223 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# frozen_string_literal: true

require 'stringio'

module PDF
  module Core
    # Document renderer serializes document into its binary representation.
    class Renderer
      # @param state [PDF::Core::DocumentState]
      def initialize(state)
        @state = state
        @state.populate_pages_from_store(self)

        min_version(state.store.min_version) if state.store.min_version

        @page_number = 0
      end

      # Document state
      # @return [PDF::Core::DocumentState]
      attr_reader :state

      # Creates a new Reference and adds it to the Document's object list.
      #
      # @param data [any] anything that {PDF::Core.pdf_object} can convert.
      # @return [Integer] the identifier of the reference
      def ref(data)
        ref!(data).identifier
      end

      # Like {ref}, but returns the actual reference instead of its identifier.
      #
      # While you can use this to build up nested references within the object
      # tree, it is recommended to persist only identifiers, and then provide
      # helper methods to look up the actual references in the {ObjectStore} if
      # needed. If you take this approach, `Document::Snapshot` will probably
      # work with your extension.
      #
      # @param data [any] anything that {PDF::Core.pdf_object} can convert.
      # @return [PDF::Core::Reference]
      def ref!(data)
        state.store.ref(data)
      end

      # At any stage in the object tree an object can be replaced with an
      # indirect reference. To get access to the object safely, regardless
      # of if it's hidden behind a {Reference}, wrap it in `deref()`.
      #
      # @param obj [PDF::Core::Reference, any]
      # @return [any]
      def deref(obj)
        obj.is_a?(PDF::Core::Reference) ? obj.data : obj
      end

      # Appends a raw string to the current page content.
      #
      # @example Raw line drawing example
      #   x1, y1, x2, y2 = 100, 500, 300, 550
      #
      #   pdf.add_content("#{PDF::Core.real_params([x1, y1])} m")   # move
      #   pdf.add_content("#{PDF::Core.real_params([ x2, y2 ])} l") # draw path
      #   pdf.add_content('S') # stroke
      #
      # @param str [String]
      # @return [void]
      def add_content(str)
        save_graphics_state if graphic_state.nil?
        state.page.content << str << "\n"
      end

      # The Name dictionary for this document. It is lazily initialized, so that
      # documents that do not need a name dictionary do not incur the additional
      # overhead.
      #
      # @return [PDF::Core::Reference<Hash>]
      # @see # PDF 1.7 spec, section 3.6.3 Name Dictionary
      def names
        state.store.root.data[:Names] ||= ref!(Type: :Names)
      end

      # Returns true if the Names dictionary is in use for this document.
      #
      # @return [Boolean]
      def names?
        state.store.root.data.key?(:Names)
      end

      # Defines a block to be called just before the document is rendered.
      #
      # @yieldparam document_state [PDF::Core::DocumentState]
      # @return [void]
      def before_render(&block)
        state.before_render_callbacks << block
      end

      # Defines a block to be called just before a new page is started.
      #
      # @yieldparam document_state [PDF::Core::DocumentState]
      # @return [void]
      def on_page_create(&block)
        state.on_page_create_callback = block
      end

      # Create a new page and set it current.
      #
      # @param options [Hash]
      # @option options :size [String, Array<Numeric>]
      # @option options :layout [:portrait, :landscape]
      # @return [void]
      def start_new_page(options = {})
        last_page = state.page
        if last_page
          last_page_size = last_page.size
          last_page_layout = last_page.layout
          last_page_margins = last_page.margins
        end

        page_options = {
          size: options[:size] || last_page_size,
          layout: options[:layout] || last_page_layout,
          margins: last_page_margins,
        }
        if last_page
          if last_page.graphic_state
            new_graphic_state = last_page.graphic_state.dup
          end

          # Erase the color space so that it gets reset on new page for fussy
          # pdf-readers
          if new_graphic_state
            new_graphic_state.color_space = {}
          end
          page_options[:graphic_state] = new_graphic_state
        end

        state.page = PDF::Core::Page.new(self, page_options)

        state.insert_page(state.page, @page_number)
        @page_number += 1

        state.on_page_create_action(self)
      end

      # Number of pages in the document.
      #
      # @return [Integer]
      def page_count
        state.page_count
      end

      # Re-opens the page with the given (1-based) page number so that you can
      # draw on it.
      #
      # @param page_number [Integer]
      # @return [void]
      # @see # Prawn::Document#number_pages for a sample usage of this capability.
      def go_to_page(page_number)
        @page_number = page_number
        state.page = state.pages[page_number - 1]
      end

      # Finalize all pages
      #
      # @api private
      # @return [void]
      def finalize_all_page_contents
        (1..page_count).each do |i|
          go_to_page(i)
          while graphic_stack.present?
            restore_graphics_state
          end
          state.page.finalize
        end
      end

      # Raise the PDF version of the file we're going to generate.
      # A private method, designed for internal use when the user adds a feature
      # to their document that requires a particular version.
      #
      # @param min [Float]
      # @return [void]
      # @api private
      def min_version(min)
        state.version = min if min > state.version
      end

      # Renders the PDF document to string.
      # Pass an open file descriptor to render to file.
      #
      # @param output [#<<]
      # @return [String]
      def render(output = nil)
        buffer = StringIO.new.binmode

        finalize_all_page_contents

        render_header(buffer)
        render_body(buffer)
        render_xref(buffer)
        render_trailer(buffer)

        if output.respond_to?(:<<)
          output << buffer.string
        end

        buffer.string
      end

      # Renders the PDF document to file.
      #
      # @example
      #   pdf.render_file 'foo.pdf'
      #
      # @param filename [String, #to_path, Integer]
      # @return [void]
      def render_file(filename)
        File.open(filename, 'wb') { |f| render(f) }
      end

      # Write out the PDF Header, as per spec 3.4.1
      #
      # @api private
      # @param output [#<<]
      # @return [void]
      def render_header(output)
        state.before_render_actions(self)

        # pdf version
        output << "%PDF-#{state.version}\n"

        # 4 binary chars, as recommended by the spec
        output << "%\xFF\xFF\xFF\xFF\n"
      end

      # Write out the PDF Body, as per spec 3.4.2
      #
      # @api private
      # @param output [(#<<, #size)]
      # @return [void]
      def render_body(output)
        state.render_body(output)
      end

      # Write out the PDF Cross Reference Table, as per spec 3.4.3
      #
      # @api private
      # @param output [(#<<, #size)]
      # @return [void]
      def render_xref(output)
        @xref_offset = output.size
        output << "xref\n"
        output << "0 #{state.store.size + 1}\n"
        output << "0000000000 65535 f \n"
        state.store.each do |ref|
          output.printf('%<offset>010d', offset: ref.offset)
          output << " 00000 n \n"
        end
      end

      # Write out the PDF Trailer, as per spec 3.4.4
      #
      # @api private
      # @param output [#<<]
      # @return [void]
      def render_trailer(output)
        trailer_hash = {
          Size: state.store.size + 1,
          Root: state.store.root,
          Info: state.store.info,
        }
        trailer_hash.merge!(state.trailer) if state.trailer

        output << "trailer\n"
        output << PDF::Core.pdf_object(trailer_hash) << "\n"
        output << "startxref\n"
        output << @xref_offset << "\n"
        output << '%%EOF' << "\n"
      end

      # Open (save) current graphic state in the content stream.
      #
      # @return [void]
      def open_graphics_state
        add_content('q')
      end

      # Close current graphic state (restore previous) in the content stream.
      #
      # @return [void]
      def close_graphics_state
        add_content('Q')
      end

      # Save surrent graphic state both in the graphic state stack and in the
      # page content stream.
      #
      # If a block is given graphic state is automatically restored after the
      # block execution.
      #
      # @param graphic_state [PDF::Core::GraphicState]
      # @yield
      # @return [void]
      def save_graphics_state(graphic_state = nil)
        graphic_stack.save_graphic_state(graphic_state)
        open_graphics_state
        if block_given?
          yield
          restore_graphics_state
        end
      end

      # Returns true if content streams will be compressed before rendering,
      # false otherwise
      #
      # @return [Boolean]
      def compression_enabled?
        state.compress
      end

      # Pops the last saved graphics state off the graphics state stack and
      # restores the state to those values
      #
      # @return [void]
      def restore_graphics_state
        if graphic_stack.empty?
          raise PDF::Core::Errors::EmptyGraphicStateStack,
            "\n You have reached the end of the graphic state stack"
        end
        close_graphics_state
        graphic_stack.restore_graphic_state
      end

      # Graphic state stack of the current document.
      #
      # @return [PDF::Core::GraphicStateStack]
      def graphic_stack
        state.page.stack
      end

      # Current graphic state
      #
      # @return [PDF::Core::GraphicState]
      def graphic_state
        save_graphics_state unless graphic_stack.current_state
        graphic_stack.current_state
      end
    end
  end
end