Class: Prawn::Core::Page

Inherits:
Object
  • Object
show all
Includes:
GraphicsState
Defined in:
lib/prawn/core/page.rb,
lib/prawn/document/graphics_state.rb

Overview

:nodoc:

Defined Under Namespace

Modules: GraphicsState

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GraphicsState

#graphic_state

Constructor Details

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

Returns a new instance of Page.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prawn/core/page.rb', line 20

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

Instance Attribute Details

#contentObject

Returns the value of attribute content.



18
19
20
# File 'lib/prawn/core/page.rb', line 18

def content
  @content
end

#dictionaryObject

Returns the value of attribute dictionary.



18
19
20
# File 'lib/prawn/core/page.rb', line 18

def dictionary
  @dictionary
end

#documentObject

Returns the value of attribute document.



18
19
20
# File 'lib/prawn/core/page.rb', line 18

def document
  @document
end

#marginsObject

Returns the value of attribute margins.



18
19
20
# File 'lib/prawn/core/page.rb', line 18

def margins
  @margins
end

#stackObject

Returns the value of attribute stack.



18
19
20
# File 'lib/prawn/core/page.rb', line 18

def stack
  @stack
end

Instance Method Details

#dimensionsObject



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

def dimensions
  return inherited_dictionary_value(:MediaBox) if imported_page?

  coords = Prawn::Document::PageGeometry::SIZES[size] || size
  [0,0] + case(layout)
  when :portrait
    coords
  when :landscape
    coords.reverse
  else
    raise Prawn::Errors::InvalidPageLayout,
      "Layout must be either :portrait or :landscape"
  end
end

#ext_gstatesObject



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

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

#finalizeObject



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

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

#fontsObject



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

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

#imported_page?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/prawn/core/page.rb', line 140

def imported_page?
  @imported_page
end

#in_stamp_stream?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/prawn/core/page.rb', line 49

def in_stamp_stream?
  !!@stamp_stream
end

#layoutObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/prawn/core/page.rb', line 34

def layout
  return @layout if @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.



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

def new_content_stream
  return if in_stamp_stream?

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

#resourcesObject



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

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

#sizeObject



45
46
47
# File 'lib/prawn/core/page.rb', line 45

def size
  @size || dimensions[2,2]
end

#stamp_stream(dictionary) ⇒ Object



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

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.data[:Length] = @stamp_stream.length + 1
  @stamp_dictionary << @stamp_stream

  @stamp_stream      = nil
  @stamp_dictionary  = nil
end

#xobjectsObject



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

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