Class: PDF::Core::Page

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Page.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pdf/core/page.rb', line 18

def initialize(document, options={})
  @document = document
  @margins  = options[:margins] || { :left    => 36,
                                     :right   => 36,
                                     :top     => 36,
                                     :bottom  => 36  }
  @stack = GraphicStateStack.new(options[:graphic_state])
  if options[:object_id]
    init_from_object(options)
  else
    init_new_page(options)
  end
end

Instance Attribute Details

#contentObject



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

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

#dictionaryObject



93
94
95
# File 'lib/pdf/core/page.rb', line 93

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

Instance Method Details

#dimensionsObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pdf/core/page.rb', line 143

def dimensions
  return inherited_dictionary_value(:MediaBox) if imported_page?

  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



121
122
123
124
125
126
127
# File 'lib/pdf/core/page.rb', line 121

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

#finalizeObject



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

def finalize
  if dictionary.data[:Contents].is_a?(Array)
    dictionary.data[:Contents].each do |stream|
      stream.stream.compress! if document.compression_enabled?
    end
  else
    content.stream.compress! if document.compression_enabled?
  end
end

#fontsObject



105
106
107
108
109
110
111
# File 'lib/pdf/core/page.rb', line 105

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

#graphic_stateObject



32
33
34
# File 'lib/pdf/core/page.rb', line 32

def graphic_state
  stack.current_state
end

#imported_page?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/pdf/core/page.rb', line 139

def imported_page?
  @imported_page
end

#in_stamp_stream?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/pdf/core/page.rb', line 51

def in_stamp_stream?
  !!@stamp_stream
end

#layoutObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/pdf/core/page.rb', line 36

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

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

#new_content_streamObject

As per the PDF spec, each page can have multiple content streams. This will add a fresh, empty content stream this the page, mainly for use in loading template files.



82
83
84
85
86
87
88
89
90
91
# File 'lib/pdf/core/page.rb', line 82

def new_content_stream
  return if in_stamp_stream?

  unless dictionary.data[:Contents].is_a?(Array)
    dictionary.data[:Contents] = [content]
  end
  @content    = document.ref({})
  dictionary.data[:Contents] << document.state.store[@content]
  document.open_graphics_state
end

#resourcesObject



97
98
99
100
101
102
103
# File 'lib/pdf/core/page.rb', line 97

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

#sizeObject



47
48
49
# File 'lib/pdf/core/page.rb', line 47

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

#stamp_stream(dictionary) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pdf/core/page.rb', line 55

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

#xobjectsObject



113
114
115
116
117
118
119
# File 'lib/pdf/core/page.rb', line 113

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