Class: Prawn::Core::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/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.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/prawn/core/page.rb', line 14

def initialize(document, options={})
  @document = document
  @margins  = options[:margins] || { :left    => 36,
                                     :right   => 36,
                                     :top     => 36,
                                     :bottom  => 36  }

  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.



12
13
14
# File 'lib/prawn/core/page.rb', line 12

def content
  @content
end

#dictionaryObject

Returns the value of attribute dictionary.



12
13
14
# File 'lib/prawn/core/page.rb', line 12

def dictionary
  @dictionary
end

#documentObject

Returns the value of attribute document.



12
13
14
# File 'lib/prawn/core/page.rb', line 12

def document
  @document
end

#marginsObject

Returns the value of attribute margins.



12
13
14
# File 'lib/prawn/core/page.rb', line 12

def margins
  @margins
end

Instance Method Details

#dimensionsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/prawn/core/page.rb', line 132

def dimensions
  return dictionary.data[: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



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

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

#finalizeObject



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

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



92
93
94
95
96
97
98
# File 'lib/prawn/core/page.rb', line 92

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

#imported_page?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/prawn/core/page.rb', line 128

def imported_page?
  @imported_page
end

#in_stamp_stream?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/prawn/core/page.rb', line 43

def in_stamp_stream?
  !!@stamp_stream
end

#layoutObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/prawn/core/page.rb', line 28

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.



70
71
72
73
74
75
76
77
78
# File 'lib/prawn/core/page.rb', line 70

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]
end

#resourcesObject



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

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

#sizeObject



39
40
41
# File 'lib/prawn/core/page.rb', line 39

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

#stamp_stream(dictionary) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/prawn/core/page.rb', line 47

def stamp_stream(dictionary)
  @stamp_stream     = ""
  @stamp_dictionary = dictionary

  document.send(:update_colors)
  yield if block_given?
  document.send(:update_colors)

  @stamp_dictionary.data[:Length] = @stamp_stream.length + 1
  @stamp_dictionary << @stamp_stream

  @stamp_stream      = nil
  @stamp_dictionary  = nil
end

#xobjectsObject



100
101
102
103
104
105
106
# File 'lib/prawn/core/page.rb', line 100

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