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.



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

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] = [:PDF, :Text, :ImageB, :ImageC, :ImageI]
end

Instance Attribute Details

#art_indentsObject

Returns the value of attribute art_indents.



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

def art_indents
  @art_indents
end

#bleedsObject

Returns the value of attribute bleeds.



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

def bleeds
  @bleeds
end

#contentObject



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

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

#cropsObject

Returns the value of attribute crops.



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

def crops
  @crops
end

#dictionaryObject



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

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

#documentObject

Returns the value of attribute document.



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

def document
  @document
end

#marginsObject

Returns the value of attribute margins.



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

def margins
  @margins
end

#stackObject

Returns the value of attribute stack.



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

def stack
  @stack
end

#trimsObject

Returns the value of attribute trims.



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

def trims
  @trims
end

Instance Method Details

#art_boxObject



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

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



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

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

#crop_boxObject



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

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

#dimensionsObject



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

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



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

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

#finalizeObject



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

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



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

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

#graphic_stateObject



57
58
59
# File 'lib/pdf/core/page.rb', line 57

def graphic_state
  stack.current_state
end

#in_stamp_stream?Boolean

Returns:

  • (Boolean)


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

def in_stamp_stream?
  !@stamp_stream.nil?
end

#layoutObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/pdf/core/page.rb', line 61

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

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

#resourcesObject



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

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

#sizeObject



72
73
74
# File 'lib/pdf/core/page.rb', line 72

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

#stamp_stream(dictionary) ⇒ Object



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

def stamp_stream(dictionary)
  @stamp_stream     = ''
  @stamp_dictionary = dictionary
  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_dictionary << @stamp_stream

  @stamp_stream      = nil
  @stamp_dictionary  = nil
end

#trim_boxObject



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

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

#xobjectsObject



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

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