Class: PDF::Core::Page

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

Overview

:nodoc:

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Page.



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

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_indentsObject

Returns the value of attribute art_indents.



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

def art_indents
  @art_indents
end

#bleedsObject

Returns the value of attribute bleeds.



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

def bleeds
  @bleeds
end

#contentObject



100
101
102
# File 'lib/pdf/core/page.rb', line 100

def content
  @stamp_stream || document.state.store[@content]
end

#cropsObject

Returns the value of attribute crops.



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

def crops
  @crops
end

#dictionaryObject



104
105
106
107
# File 'lib/pdf/core/page.rb', line 104

def dictionary
  defined?(@stamp_dictionary) && @stamp_dictionary ||
    document.state.store[@dictionary]
end

#documentObject

Returns the value of attribute document.



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

def document
  @document
end

#marginsObject

Returns the value of attribute margins.



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

def margins
  @margins
end

#stackObject

Returns the value of attribute stack.



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

def stack
  @stack
end

#trimsObject

Returns the value of attribute trims.



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

def trims
  @trims
end

Instance Method Details

#art_boxObject



165
166
167
168
169
170
171
172
173
# File 'lib/pdf/core/page.rb', line 165

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_boxObject



175
176
177
178
179
180
181
182
183
# File 'lib/pdf/core/page.rb', line 175

def bleed_box
  left, bottom, right, top = dimensions
  [
    left + bleeds[:left],
    bottom + bleeds[:bottom],
    right - bleeds[:right],
    top - bleeds[:top]
  ]
end

#crop_boxObject



185
186
187
188
189
190
191
192
193
# File 'lib/pdf/core/page.rb', line 185

def crop_box
  left, bottom, right, top = dimensions
  [
    left + crops[:left],
    bottom + crops[:bottom],
    right - crops[:right],
    top - crops[:top]
  ]
end

#dimensionsObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pdf/core/page.rb', line 151

def dimensions
  coords = PDF::Core::PageGeometry::SIZES[size] || size
  [0, 0] +
    case layout
    when :portrait
      coords
    when :landscape
      coords.reverse
    else
      raise PDF::Core::Errors::InvalidPageLayout,
        'Layout must be either :portrait or :landscape'
    end
end

#ext_gstatesObject



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

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

#finalizeObject



141
142
143
144
145
146
147
148
149
# File 'lib/pdf/core/page.rb', line 141

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

#fontsObject



117
118
119
120
121
122
123
# File 'lib/pdf/core/page.rb', line 117

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

#graphic_stateObject



60
61
62
# File 'lib/pdf/core/page.rb', line 60

def graphic_state
  stack.current_state
end

#in_stamp_stream?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/pdf/core/page.rb', line 79

def in_stamp_stream?
  !@stamp_stream.nil?
end

#layoutObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/pdf/core/page.rb', line 64

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

  mb = dictionary.data[:MediaBox]
  if mb[3] > mb[2]
    :portrait
  else
    :landscape
  end
end

#resourcesObject



109
110
111
112
113
114
115
# File 'lib/pdf/core/page.rb', line 109

def resources
  if dictionary.data[:Resources]
    document.deref(dictionary.data[:Resources])
  else
    dictionary.data[:Resources] = {}
  end
end

#sizeObject



75
76
77
# File 'lib/pdf/core/page.rb', line 75

def size
  defined?(@size) && @size || dimensions[2, 2]
end

#stamp_stream(dictionary) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pdf/core/page.rb', line 83

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_boxObject



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

def trim_box
  left, bottom, right, top = dimensions
  [
    left + trims[:left],
    bottom + trims[:bottom],
    right - trims[:right],
    top - trims[:top]
  ]
end

#xobjectsObject



125
126
127
128
129
130
131
# File 'lib/pdf/core/page.rb', line 125

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