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.



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
# File 'lib/pdf/core/page.rb', line 84

def initialize(document, options = {})
  @document = document
  @margins = options[:margins] || {
    left: 36,
    right: 36,
    top: 36,
    bottom: 36,
  }
  @crops = options[:crops] || ZERO_INDENTS
  @bleeds = options[:bleeds] || ZERO_INDENTS
  @trims = options[:trims] || ZERO_INDENTS
  @art_indents = options[:art_indents] || ZERO_INDENTS
  @stack = GraphicStateStack.new(options[:graphic_state])
  @size = options[:size] || 'LETTER'
  @layout = options[:layout] || :portrait

  @stamp_stream = nil
  @stamp_dictionary = nil

  @content = document.ref({})
  content << 'q' << "\n"
  @dictionary = document.ref(
    Type: :Page,
    Parent: document.state.store.pages,
    MediaBox: dimensions,
    CropBox: crop_box,
    BleedBox: bleed_box,
    TrimBox: trim_box,
    ArtBox: art_box,
    Contents: content,
  )

  resources[:ProcSet] = %i[PDF Text ImageB ImageC ImageI]
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>



14
15
16
# File 'lib/pdf/core/page.rb', line 14

def art_indents
  @art_indents
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>



19
20
21
# File 'lib/pdf/core/page.rb', line 19

def bleeds
  @bleeds
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:



182
183
184
# File 'lib/pdf/core/page.rb', line 182

def content
  @stamp_stream || document.state.store[@content]
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>



24
25
26
# File 'lib/pdf/core/page.rb', line 24

def crops
  @crops
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:



190
191
192
193
# File 'lib/pdf/core/page.rb', line 190

def dictionary
  (defined?(@stamp_dictionary) && @stamp_dictionary) ||
    document.state.store[@dictionary]
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)


39
40
41
# File 'lib/pdf/core/page.rb', line 39

def document
  @document
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>



34
35
36
# File 'lib/pdf/core/page.rb', line 34

def margins
  @margins
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:



44
45
46
# File 'lib/pdf/core/page.rb', line 44

def stack
  @stack
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>



29
30
31
# File 'lib/pdf/core/page.rb', line 29

def trims
  @trims
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>)


275
276
277
278
279
280
281
282
283
# File 'lib/pdf/core/page.rb', line 275

def art_box
  left, bottom, right, top = dimensions
  [
    left + art_indents[:left],
    bottom + art_indents[:bottom],
    right - art_indents[:right],
    top - art_indents[:top],
  ]
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>)


290
291
292
293
294
295
296
297
298
# File 'lib/pdf/core/page.rb', line 290

def bleed_box
  left, bottom, right, top = dimensions
  [
    left + bleeds[:left],
    bottom + bleeds[:bottom],
    right - bleeds[:right],
    top - bleeds[:top],
  ]
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>)


306
307
308
309
310
311
312
313
314
# File 'lib/pdf/core/page.rb', line 306

def crop_box
  left, bottom, right, top = dimensions
  [
    left + crops[:left],
    bottom + crops[:bottom],
    right - crops[:right],
    top - crops[:top],
  ]
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>)


255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/pdf/core/page.rb', line 255

def dimensions
  coords = PDF::Core::PageGeometry::SIZES[size] || size
  coords =
    case layout
    when :portrait
      coords
    when :landscape
      coords.reverse
    else
      raise PDF::Core::Errors::InvalidPageLayout,
        'Layout must be either :portrait or :landscape'
    end
  [0, 0].concat(coords)
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)


231
232
233
234
235
236
237
# File 'lib/pdf/core/page.rb', line 231

def ext_gstates
  if resources[:ExtGState]
    document.deref(resources[:ExtGState])
  else
    resources[:ExtGState] = {}
  end
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.



242
243
244
245
246
247
248
249
250
# File 'lib/pdf/core/page.rb', line 242

def finalize
  if dictionary.data[:Contents].is_a?(Array)
    dictionary.data[:Contents].each do |stream|
      stream.stream.compress! if document.compression_enabled?
    end
  elsif document.compression_enabled?
    content.stream.compress!
  end
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)


209
210
211
212
213
214
215
# File 'lib/pdf/core/page.rb', line 209

def fonts
  if resources[:Font]
    document.deref(resources[:Font])
  else
    resources[:Font] = {}
  end
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.



122
123
124
# File 'lib/pdf/core/page.rb', line 122

def graphic_state
  stack.current_state
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)


152
153
154
# File 'lib/pdf/core/page.rb', line 152

def in_stamp_stream?
  !@stamp_stream.nil?
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



130
131
132
133
134
135
136
137
138
139
# File 'lib/pdf/core/page.rb', line 130

def layout
  return @layout if defined?(@layout) && @layout

  mb = dictionary.data[:MediaBox]
  if mb[3] > mb[2]
    :portrait
  else
    :landscape
  end
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)


198
199
200
201
202
203
204
# File 'lib/pdf/core/page.rb', line 198

def resources
  if dictionary.data[:Resources]
    document.deref(dictionary.data[:Resources])
  else
    dictionary.data[:Resources] = {}
  end
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.



145
146
147
# File 'lib/pdf/core/page.rb', line 145

def size
  (defined?(@size) && @size) || dimensions[2, 2]
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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pdf/core/page.rb', line 161

def stamp_stream(dictionary)
  @stamp_dictionary = dictionary
  @stamp_stream = @stamp_dictionary.stream
  graphic_stack_size = stack.stack.size

  document.save_graphics_state
  document.__send__(:freeze_stamp_graphics)
  yield if block_given?

  until graphic_stack_size == stack.stack.size
    document.restore_graphics_state
  end

  @stamp_stream = nil
  @stamp_dictionary = nil
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>)


320
321
322
323
324
325
326
327
328
# File 'lib/pdf/core/page.rb', line 320

def trim_box
  left, bottom, right, top = dimensions
  [
    left + trims[:left],
    bottom + trims[:bottom],
    right - trims[:right],
    top - trims[:top],
  ]
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)


220
221
222
223
224
225
226
# File 'lib/pdf/core/page.rb', line 220

def xobjects
  if resources[:XObject]
    document.deref(resources[:XObject])
  else
    resources[:XObject] = {}
  end
end