Class: PDF::Core::Page
- Inherits:
-
Object
- Object
- PDF::Core::Page
- 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
-
#art_indents ⇒ Object
Returns the value of attribute art_indents.
-
#bleeds ⇒ Object
Returns the value of attribute bleeds.
- #content ⇒ Object
-
#crops ⇒ Object
Returns the value of attribute crops.
- #dictionary ⇒ Object
-
#document ⇒ Object
Returns the value of attribute document.
-
#margins ⇒ Object
Returns the value of attribute margins.
-
#stack ⇒ Object
Returns the value of attribute stack.
-
#trims ⇒ Object
Returns the value of attribute trims.
Instance Method Summary collapse
- #art_box ⇒ Object
- #bleed_box ⇒ Object
- #crop_box ⇒ Object
- #dimensions ⇒ Object
- #ext_gstates ⇒ Object
- #finalize ⇒ Object
- #fonts ⇒ Object
- #graphic_state ⇒ Object
- #in_stamp_stream? ⇒ Boolean
-
#initialize(document, options = {}) ⇒ Page
constructor
A new instance of Page.
- #layout ⇒ Object
- #resources ⇒ Object
- #size ⇒ Object
- #stamp_stream(dictionary) ⇒ Object
- #trim_box ⇒ Object
- #xobjects ⇒ Object
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, = {}) @document = document @margins = [:margins] || { left: 36, right: 36, top: 36, bottom: 36 } @crops = [:crops] || ZERO_INDENTS @bleeds = [:bleeds] || ZERO_INDENTS @trims = [:trims] || ZERO_INDENTS @art_indents = [:art_indents] || ZERO_INDENTS @stack = GraphicStateStack.new([:graphic_state]) @size = [:size] || 'LETTER' @layout = [: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_indents ⇒ Object
Returns the value of attribute art_indents.
15 16 17 |
# File 'lib/pdf/core/page.rb', line 15 def art_indents @art_indents end |
#bleeds ⇒ Object
Returns the value of attribute bleeds.
15 16 17 |
# File 'lib/pdf/core/page.rb', line 15 def bleeds @bleeds end |
#content ⇒ Object
100 101 102 |
# File 'lib/pdf/core/page.rb', line 100 def content @stamp_stream || document.state.store[@content] end |
#crops ⇒ Object
Returns the value of attribute crops.
15 16 17 |
# File 'lib/pdf/core/page.rb', line 15 def crops @crops end |
#dictionary ⇒ Object
104 105 106 107 |
# File 'lib/pdf/core/page.rb', line 104 def dictionary defined?(@stamp_dictionary) && @stamp_dictionary || document.state.store[@dictionary] end |
#document ⇒ Object
Returns the value of attribute document.
15 16 17 |
# File 'lib/pdf/core/page.rb', line 15 def document @document end |
#margins ⇒ Object
Returns the value of attribute margins.
15 16 17 |
# File 'lib/pdf/core/page.rb', line 15 def margins @margins end |
#stack ⇒ Object
Returns the value of attribute stack.
15 16 17 |
# File 'lib/pdf/core/page.rb', line 15 def stack @stack end |
#trims ⇒ Object
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_box ⇒ Object
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_box ⇒ Object
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_box ⇒ Object
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 |
#dimensions ⇒ Object
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_gstates ⇒ Object
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 |
#finalize ⇒ Object
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 |
#fonts ⇒ Object
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_state ⇒ Object
60 61 62 |
# File 'lib/pdf/core/page.rb', line 60 def graphic_state stack.current_state end |
#in_stamp_stream? ⇒ Boolean
79 80 81 |
# File 'lib/pdf/core/page.rb', line 79 def in_stamp_stream? !@stamp_stream.nil? end |
#layout ⇒ Object
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 |
#resources ⇒ Object
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 |
#size ⇒ Object
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_box ⇒ Object
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 |
#xobjects ⇒ Object
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 |