lib/prawn/grid.rb in prawn-2.0.1 vs lib/prawn/grid.rb in prawn-2.0.2

- old
+ new

@@ -31,21 +31,25 @@ # @pdf.grid([0,1]) # Get the GridBox at [0,1] # @pdf.grid([0,1], [1,2]) # Get a multi-box spanning from [0,1] to [1,2] # def grid(*args) @boxes ||= {} - @boxes[args] ||= if args.empty? - @grid + return @boxes[args] if @boxes[args] + + if args.empty? + @boxes[args] = @grid else g1, g2 = args - if(g1.class == Array && g2.class == Array && - g1.length == 2 && g2.length == 2) - multi_box(single_box(*g1), single_box(*g2)) + + if g1.class == Array && g2.class == Array && g1.length == 2 && g2.length == 2 + @boxes[args] = multi_box(single_box(*g1), single_box(*g2)) else - single_box(g1, g2) + @boxes[args] = single_box(g1, g2) end end + + @boxes[args] end # A Grid represents the entire grid system of a Page and calculates # the column width and row height of the base box. # @@ -67,18 +71,18 @@ @column_width ||= subdivide(pdf.bounds.width, columns, column_gutter) end # Calculates the base height of boxes. def row_height - @row_height ||= subdivide(pdf.bounds.height, rows, row_gutter) + @row_height ||= subdivide(pdf.bounds.height, rows, row_gutter) end # Diagnostic tool to show all of the grids. Defaults to gray. def show_all(color = "CCCCCC") self.rows.times do |i| self.columns.times do |j| - pdf.grid(i,j).show(color) + pdf.grid(i, j).show(color) end end end private @@ -86,11 +90,11 @@ def subdivide(total, num, gutter) (total.to_f - (gutter * (num - 1).to_f)) / num.to_f end def set_gutter(options) - if options.has_key?(:gutter) + if options.key?(:gutter) @gutter = options[:gutter].to_f @row_gutter, @column_gutter = @gutter, @gutter else @row_gutter = options[:row_gutter].to_f @column_gutter = options[:column_gutter].to_f @@ -197,10 +201,11 @@ pdf.stroke_color = original_stroke_color end end private + def grid pdf.grid end end @@ -212,11 +217,11 @@ @pdf = pdf @bs = [b1, b2] end def name - @bs.map {|b| b.name}.join(":") + @bs.map(&:name).join(":") end def total_height @bs[0].total_height end @@ -248,27 +253,29 @@ def bottom bottom_box.bottom end private + def left_box - @left_box ||= @bs.min {|a,b| a.left <=> b.left} + @left_box ||= @bs.min { |a, b| a.left <=> b.left } end def right_box - @right_box ||= @bs.max {|a,b| a.right <=> b.right} + @right_box ||= @bs.max { |a, b| a.right <=> b.right } end def top_box - @top_box ||= @bs.max {|a,b| a.top <=> b.top} + @top_box ||= @bs.max { |a, b| a.top <=> b.top } end def bottom_box - @bottom_box ||= @bs.min {|a,b| a.bottom <=> b.bottom} + @bottom_box ||= @bs.min { |a, b| a.bottom <=> b.bottom } end end private + def single_box(i, j) GridBox.new(self, i, j) end def multi_box(b1, b2)