Method: Prawn::Document#layout

Defined in:
lib/barkest_core/extensions/prawn_document_extensions.rb

#layout(options = {}, &block) ⇒ Object

Uses the grid system to create a bounding box and then executes the provided code block.

The bounding box can be defined in percentage as opposed to absolutes.

Valid options:

rectangle

Allows you to specify the dimensions of the box with a single value. You can provide an array or a hash to this option.

[ column, row, width, height ]
{ :column => ?, :row => ?, :width => ?, :height => ? }
{ :left => ?, :top => ?, :width => ?, :height => ? }
column

Sets the starting column for the bounding box. This will be overridden if rectangle is also set. This can be either an absolute grid location (5) or a percentage (5%).

row

Sets the starting row for the bounding box. This will be overridden if rectangle is also set. This can be either an absolute grid location (5) or a percentage (5%).

width

Sets the width for the bounding box. This will be overridden if rectangle is also set. This can be either an absolute grid location (5) or a percentage (5%).

height

Sets the height for the bounding box. This will be overridden if rectangle is also set. This can be either an absolute grid location (5) or a percentage (5%).

fill_width

If set, the width will spring out to fill the grid from the starting column. This overrides both rectangle and width.

fill_height

If set, the height will spring out to fill the grid from the starting row. This overrides both rectangle and height.

Raises:

  • (StandardError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/barkest_core/extensions/prawn_document_extensions.rb', line 97

def layout(options = {}, &block)
  options ||= {}

  col_max = grid.columns
  row_max = grid.rows

  if options[:rectangle]
    r = options[:rectangle]
    if r.is_a?(Array)
      options[:column] ||= r[0]
      options[:row] ||= r[1]
      options[:width] ||= r[2]
      options[:height] ||= r[3]
    elsif r.is_a?(Hash)
      options[:column] ||= r[:column] || r[:left]
      options[:row] ||= r[:row] || r[:top]
      options[:width] ||= r[:width]
      options[:height] ||= r[:height]
    end
  end

  col = options[:column] || 1
  row = options[:row] || 1
  width = options[:width] || 1
  height = options[:height] || 1

  ptov = Proc.new do |pval,max,plus|
    if pval.is_a?(String)
      if pval[-1] == '%'
        pval = (pval.to_f * 0.01 * max).to_i + plus
        pval = max if pval > max
        pval = 1 if pval < 1
      else
        pval = pval.to_i
      end
    end
    pval
  end

  col = ptov.call(col, col_max, 1)
  width = ptov.call(width, col_max, 0)
  row = ptov.call(row, row_max, 1)
  height = ptov.call(height, row_max, 0)

  width = col_max - col + 1 if options[:fill_width]
  height = row_max - row + 1 if options[:fill_height]

  raise StandardError.new("column must be between 1 and #{col_max}") unless col >= 1 && col <= col_max
  raise StandardError.new("row must be between 1 and #{row_max}") unless row >= 1 && row <= row_max
  raise StandardError.new('width is invalid') unless width >= 1 && col + width - 1 <= col_max
  raise StandardError.new('height is invalid') unless height >= 1 && row + height - 1 <= row_max

  col -= 1
  row -= 1

  grid([row, col], [row + height - 1, col + width - 1]).bounding_box(&block)
end