Class: PDF::Core::Page Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Low-level representation of a PDF page

Constant Summary collapse

ZERO_INDENTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

A convenince constant of no indents.

{
  left: 0,
  bottom: 0,
  right: 0,
  top: 0,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ Page

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Page.

Parameters:

  • document (Prawn::Document)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :margins (Hash{:left, :right, :top, :bottom => Number}, nil) — default: { left: 0, right: 0, top: 0, bottom: 0 }

    Page margins

  • :crop (Hash{:left, :right, :top, :bottom => Number}, nil) — default: ZERO_INDENTS

    Page crop box

  • :bleed (Hash{:left, :right, :top, :bottom => Number}, nil) — default: ZERO_INDENTS

    Page bleed box

  • :trims (Hash{:left, :right, :top, :bottom => Number}, nil) — default: ZERO_INDENTS

    Page trim box

  • :art_indents (Hash{:left, :right, :top, :bottom => Number}, Numeric)

    , nil] (ZERO_INDENTS) Page art box indents.

  • :graphic_state (PDF::Core::GraphicState, nil) — default: nil

    Initial graphic state

  • :size (String, Array<Numeric>, nil) — default: 'LETTER'

    Page size. A string identifies a named page size defined in PDF::Core::PageGeometry. An array must be a two element array specifying width and height in points.

  • :layout (:portrait, :landscape, nil) — default: :portrait

    Page orientation.

Source Code
lib/pdf/core/page.rb, line 84
84
def initialize(document, options = {})
85
  @document = document
86
  @margins = options[:margins] || {
87
    left: 36,
88
    right: 36,
89
    top: 36,
90
    bottom: 36,
91
  }
92
  @crops = options[:crops] || ZERO_INDENTS
93
  @bleeds = options[:bleeds] || ZERO_INDENTS
94
  @trims = options[:trims] || ZERO_INDENTS
95
  @art_indents = options[:art_indents] || ZERO_INDENTS
96
  @stack = GraphicStateStack.new(options[:graphic_state])
97
  @size = options[:size] || 'LETTER'
98
  @layout = options[:layout] || :portrait
99
100
  @stamp_stream = nil
101
  @stamp_dictionary = nil
102
103
  @content = document.ref({})
104
  content << 'q' << "\n"
105
  @dictionary = document.ref(
106
    Type: :Page,
107
    Parent: document.state.store.pages,
108
    MediaBox: dimensions,
109
    CropBox: crop_box,
110
    BleedBox: bleed_box,
111
    TrimBox: trim_box,
112
    ArtBox: art_box,
113
    Contents: content,
114
  )
115
116
  resources[:ProcSet] = %i[PDF Text ImageB ImageC ImageI]
117
end

Instance Attribute Details

#art_indentsHash<[:left, :right, :top, :bottom], Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page art box indents relative to page edges.

Returns:

  • (Hash<[:left, :right, :top, :bottom], Numeric>)

    Hash<[:left, :right, :top, :bottom], Numeric>

Source Code
lib/pdf/core/page.rb, line 14
14
def art_indents
15
  @art_indents
16
end

#bleedsHash<[:left, :right, :top, :bottom], Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page bleed box indents.

Returns:

  • (Hash<[:left, :right, :top, :bottom], Numeric>)

    Hash<[:left, :right, :top, :bottom], Numeric>

Source Code
lib/pdf/core/page.rb, line 19
19
def bleeds
20
  @bleeds
21
end

#contentPDF::Core::Reference<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Current content stream. Can be either the page content stream or a stamp content stream.

Returns:

Source Code
lib/pdf/core/page.rb, line 182
182
def content
183
  @stamp_stream || document.state.store[@content]
184
end

#cropsHash<[:left, :right, :top, :bottom], Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page crop box indents.

Returns:

  • (Hash<[:left, :right, :top, :bottom], Numeric>)

    Hash<[:left, :right, :top, :bottom], Numeric>

Source Code
lib/pdf/core/page.rb, line 24
24
def crops
25
  @crops
26
end

#dictionaryPDF::Core::Reference<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Current content dictionary. Can be either the page dictionary or a stamp dictionary.

Returns:

Source Code
lib/pdf/core/page.rb, line 190
190
def dictionary
191
  (defined?(@stamp_dictionary) && @stamp_dictionary) ||
192
    document.state.store[@dictionary]
193
end

#documentPrawn::Document

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Owning document.

Returns:

  • (Prawn::Document)
Source Code
lib/pdf/core/page.rb, line 39
39
def document
40
  @document
41
end

#marginsHash<[:left, :right, :top, :bottom], Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page margins.

Returns:

  • (Hash<[:left, :right, :top, :bottom], Numeric>)

    Hash<[:left, :right, :top, :bottom], Numeric>

Source Code
lib/pdf/core/page.rb, line 34
34
def margins
35
  @margins
36
end

#stackGraphicStateStack

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Graphic state stack.

Returns:

Source Code
lib/pdf/core/page.rb, line 44
44
def stack
45
  @stack
46
end

#trimsHash<[:left, :right, :top, :bottom], Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page trim box indents.

Returns:

  • (Hash<[:left, :right, :top, :bottom], Numeric>)

    Hash<[:left, :right, :top, :bottom], Numeric>

Source Code
lib/pdf/core/page.rb, line 29
29
def trims
30
  @trims
31
end

Instance Method Details

#art_boxArray<Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A rectangle, expressed in default user space units, defining the extent of the page’s meaningful content (including potential white space) as intended by the page’s creator.

Returns:

  • (Array<Numeric>)
Source Code
lib/pdf/core/page.rb, line 275
275
def art_box
276
  left, bottom, right, top = dimensions
277
  [
278
    left + art_indents[:left],
279
    bottom + art_indents[:bottom],
280
    right - art_indents[:right],
281
    top - art_indents[:top],
282
  ]
283
end

#bleed_boxArray<Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page bleed box. A rectangle, expressed in default user space units, defining the region to which the contents of the page should be clipped when output in a production environment.

Returns:

  • (Array<Numeric>)
Source Code
lib/pdf/core/page.rb, line 290
290
def bleed_box
291
  left, bottom, right, top = dimensions
292
  [
293
    left + bleeds[:left],
294
    bottom + bleeds[:bottom],
295
    right - bleeds[:right],
296
    top - bleeds[:top],
297
  ]
298
end

#crop_boxArray<Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A rectangle, expressed in default user space units, defining the visible region of default user space. When the page is displayed or printed, its contents are to be clipped (cropped) to this rectangle and then imposed on the output medium in some implementation-defined manner.

Returns:

  • (Array<Numeric>)
Source Code
lib/pdf/core/page.rb, line 306
306
def crop_box
307
  left, bottom, right, top = dimensions
308
  [
309
    left + crops[:left],
310
    bottom + crops[:bottom],
311
    right - crops[:right],
312
    top - crops[:top],
313
  ]
314
end

#dimensionsArray<Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page dimensions.

Returns:

  • (Array<Numeric>)
Source Code
lib/pdf/core/page.rb, line 255
255
def dimensions
256
  coords = PDF::Core::PageGeometry::SIZES[size] || size
257
  coords =
258
    case layout
259
    when :portrait
260
      coords
261
    when :landscape
262
      coords.reverse
263
    else
264
      raise PDF::Core::Errors::InvalidPageLayout,
265
        'Layout must be either :portrait or :landscape'
266
    end
267
  [0, 0].concat(coords)
268
end

#ext_gstatesHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Graphic state parameter dictionary.

Returns:

  • (Hash)
Source Code
lib/pdf/core/page.rb, line 231
231
def ext_gstates
232
  if resources[:ExtGState]
233
    document.deref(resources[:ExtGState])
234
  else
235
    resources[:ExtGState] = {}
236
  end
237
end

#finalizevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Finalize page.

Source Code
lib/pdf/core/page.rb, line 242
242
def finalize
243
  if dictionary.data[:Contents].is_a?(Array)
244
    dictionary.data[:Contents].each do |stream|
245
      stream.stream.compress! if document.compression_enabled?
246
    end
247
  elsif document.compression_enabled?
248
    content.stream.compress!
249
  end
250
end

#fontsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fonts dictionary.

Returns:

  • (Hash)
Source Code
lib/pdf/core/page.rb, line 209
209
def fonts
210
  if resources[:Font]
211
    document.deref(resources[:Font])
212
  else
213
    resources[:Font] = {}
214
  end
215
end

#graphic_statePDF::Core::GraphicState

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Current graphic state.

Source Code
lib/pdf/core/page.rb, line 122
122
def graphic_state
123
  stack.current_state
124
end

#in_stamp_stream?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Are we drawing to a stamp right now?

Returns:

  • (Boolean)
Source Code
lib/pdf/core/page.rb, line 152
152
def in_stamp_stream?
153
  !@stamp_stream.nil?
154
end

#layout:portrait, :landscape

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page layout.

Returns:

  • (:portrait)

    if page is talled than wider

  • (:landscape)

    otherwise

Source Code
lib/pdf/core/page.rb, line 130
130
def layout
131
  return @layout if defined?(@layout) && @layout
132
133
  mb = dictionary.data[:MediaBox]
134
  if mb[3] > mb[2]
135
    :portrait
136
  else
137
    :landscape
138
  end
139
end

#resourcesHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page resources dictionary.

Returns:

  • (Hash)
Source Code
lib/pdf/core/page.rb, line 198
198
def resources
199
  if dictionary.data[:Resources]
200
    document.deref(dictionary.data[:Resources])
201
  else
202
    dictionary.data[:Resources] = {}
203
  end
204
end

#sizeArray<Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Page size.

Returns:

  • (Array<Numeric>)

    a two-element array containing width and height of the page.

Source Code
lib/pdf/core/page.rb, line 145
145
def size
146
  (defined?(@size) && @size) || dimensions[2, 2]
147
end

#stamp_stream(dictionary) { ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Draw to stamp.

Parameters:

Yields:

  • outputs to the stamp

Source Code
lib/pdf/core/page.rb, line 161
161
def stamp_stream(dictionary)
162
  @stamp_dictionary = dictionary
163
  @stamp_stream = @stamp_dictionary.stream
164
  graphic_stack_size = stack.stack.size
165
166
  document.save_graphics_state
167
  document.__send__(:freeze_stamp_graphics)
168
  yield if block_given?
169
170
  until graphic_stack_size == stack.stack.size
171
    document.restore_graphics_state
172
  end
173
174
  @stamp_stream = nil
175
  @stamp_dictionary = nil
176
end

#trim_boxArray<Numeric>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A rectangle, expressed in default user space units, defining the intended dimensions of the finished page after trimming.

Returns:

  • (Array<Numeric>)
Source Code
lib/pdf/core/page.rb, line 320
320
def trim_box
321
  left, bottom, right, top = dimensions
322
  [
323
    left + trims[:left],
324
    bottom + trims[:bottom],
325
    right - trims[:right],
326
    top - trims[:top],
327
  ]
328
end

#xobjectsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

External objects dictionary.

Returns:

  • (Hash)
Source Code
lib/pdf/core/page.rb, line 220
220
def xobjects
221
  if resources[:XObject]
222
    document.deref(resources[:XObject])
223
  else
224
    resources[:XObject] = {}
225
  end
226
end