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

- old
+ new

@@ -228,16 +228,46 @@ # 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 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 + # breaking to the next page -- this means, in particular, that if the + # first row is taller than the margin box, we will only move to the next + # page if we're below the top. Some floating-point tolerance is added to + # the calculation. + # + # Note that we use the actual bounds, not the reference bounds. This is + # because even if we are in a stretchy bounding box, flowing to the next + # page will not buy us any space if we are at the top. + if @pdf.y > @pdf.bounds.height + @pdf.bounds.absolute_bottom - 0.001 + # we're at the top of our bounds + started_new_page_at_row = 0 + else + started_new_page_at_row = -1 + + # If there isn't enough room left on the page to fit the first data row + # (excluding the header), start the table on the next page. + needed_height = row(0).height + needed_height += row(1).height if @header + if needed_height > @pdf.y - ref_bounds.absolute_bottom + @pdf.bounds.move_past_bottom + offset = @pdf.y + started_new_page_at_row = 0 + end + end + @cells.each do |cell| - if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom + if cell.height > (cell.y + offset) - ref_bounds.absolute_bottom && + cell.row > started_new_page_at_row # start a new page or column @pdf.bounds.move_past_bottom - draw_header + draw_header unless cell.row == 0 offset = @pdf.y - cell.y + started_new_page_at_row = cell.row end # Don't modify cell.x / cell.y here, as we want to reuse the original # values when re-inking the table. #draw should be able to be called # multiple times. @@ -273,15 +303,17 @@ # def column_widths @column_widths ||= begin if width < cells.min_width raise Errors::CannotFit, - "Table's width was set too small to contain its contents" + "Table's width was set too small to contain its contents " + + "(min width #{cells.min_width}, requested #{width})" end if width > cells.max_width raise Errors::CannotFit, - "Table's width was set larger than its contents' maximum width" + "Table's width was set larger than its contents' maximum width " + + "(max width #{cells.max_width}, requested #{width})" end if width < natural_width # Shrink the table to fit the requested width. f = (width - cells.min_width).to_f / (natural_width - cells.min_width)