lib/prawn/table.rb in prawn-0.11.1 vs lib/prawn/table.rb in prawn-0.12.0

- old
+ new

@@ -119,10 +119,11 @@ # def initialize(data, document, options={}, &block) @pdf = document @cells = make_cells(data) @header = false + @epsilon = 1e-9 options.each { |k, v| send("#{k}=", v) } if block block.arity < 1 ? instance_eval(&block) : block[self] end @@ -166,11 +167,11 @@ when Array widths.each_with_index { |w, i| column(i).width = w } when Hash widths.each { |i, w| column(i).width = w } when Numeric - columns.width = widths + cells.width = widths else raise ArgumentError, "cannot interpret column widths" end end @@ -225,11 +226,11 @@ # y-position to get to the actual position on the current page. offset = @pdf.y # Reference bounds are the non-stretchy bounds used to decide when to # flow to a new column / page. - ref_bounds = @pdf.bounds.stretchy? ? @pdf.margin_box : @pdf.bounds + ref_bounds = @pdf.reference_bounds last_y = @pdf.y # Determine whether we're at the top of the current bounds (margin box or # bounding box). If we're at the top, we couldn't gain any more room by @@ -256,13 +257,21 @@ offset = @pdf.y started_new_page_at_row = 0 end end + # Track cells to be drawn on this page. They will all be drawn when this + # page is finished. + cells_this_page = [] + @cells.each do |cell| if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom && cell.row > started_new_page_at_row + # Ink all cells on the current page + Cell.draw_cells(cells_this_page) + cells_this_page = [] + # start a new page or column @pdf.bounds.move_past_bottom draw_header unless cell.row == 0 offset = @pdf.y - cell.y started_new_page_at_row = cell.row @@ -283,13 +292,15 @@ if @row_colors && (!@header || cell.row > 0) index = @header ? (cell.row - 1) : cell.row cell.background_color = @row_colors[index % @row_colors.length] end - cell.draw([x, y]) + cells_this_page << [cell, [x, y]] last_y = y end + # Draw the last page of cells + Cell.draw_cells(cells_this_page) @pdf.move_cursor_to(last_y - @cells.last.height) end # Calculate and return the constrained column widths, taking into account @@ -301,31 +312,31 @@ # you see weird problems like CannotFit errors or shockingly bad column # sizes, you should specify more column widths manually. # def column_widths @column_widths ||= begin - if width < cells.min_width + if width - cells.min_width < -epsilon raise Errors::CannotFit, "Table's width was set too small to contain its contents " + "(min width #{cells.min_width}, requested #{width})" end - if width > cells.max_width + if width - cells.max_width > epsilon raise Errors::CannotFit, "Table's width was set larger than its contents' maximum width " + "(max width #{cells.max_width}, requested #{width})" end - if width < natural_width + if width - natural_width < -epsilon # Shrink the table to fit the requested width. f = (width - cells.min_width).to_f / (natural_width - cells.min_width) (0...column_length).map do |c| min, nat = column(c).min_width, column(c).width (f * (nat - min)) + min end - elsif width > natural_width + elsif width - natural_width > epsilon # Expand the table to fit the requested width. f = (width - cells.width).to_f / (cells.max_width - cells.width) (0...column_length).map do |c| nat, max = column(c).width, column(c).max_width @@ -445,9 +456,14 @@ y_positions = row_heights.inject([0]) { |ary, y| ary << (ary.last - y); ary}[0..-2] y_positions.each_with_index { |y, i| row(i).y = y } end + private + + def epsilon + @epsilon + end end end