Permalink
Browse files

Ensure natural column width respects colspan

Fixes #323.
  • Loading branch information...
1 parent 3d5e557 commit f5635ea3ec75f8c7ea65e04806c8ee4b3b945460 @bradediger bradediger committed Feb 11, 2012
Showing with 29 additions and 1 deletion.
  1. +10 −1 lib/prawn/table/cells.rb
  2. +19 −0 spec/table_spec.rb
View
11 lib/prawn/table/cells.rb
@@ -122,7 +122,16 @@ def style(options={}, &block)
# Returns the total width of all columns in the selected set.
#
def width
- aggregate_cell_values(:column, :width_ignoring_span, :max)
+ widths = {}
+ each do |cell|
+ index = cell.column
+ per_cell_width = cell.width_ignoring_span.to_f / cell.colspan
+ cell.colspan.times do |n|
+ widths[cell.column+n] = [widths[cell.column+n], per_cell_width].
+ compact.max
+ end
+ end
+ widths.values.inject(0, &:+)
end
# Returns minimum width required to contain cells in the set.
View
19 spec/table_spec.rb
@@ -1001,6 +1001,25 @@
widths[0].should == widths[1]
end
+ it "honors a large, explicitly set table width" do
+ t = @pdf.table([[{:content => "AAAAAAAAAA", :colspan => 3}],
+ ["A", "B", "C"]],
+ :width => 400)
+
+ t.column_widths.inject(0) { |sum, w| sum + w }.
+ should.be.close(400, 0.01)
+ end
+
+ it "honors a small, explicitly set table width" do
+ t = @pdf.table([[{:content => "Lorem ipsum dolor sit amet " * 20,
+ :colspan => 3}],
+ ["A", "B", "C"]],
+ :width => 200)
+
+ t.column_widths.inject(0) { |sum, w| sum + w }.
+ should.be.close(200, 0.01)
+ end
+
it "splits natural_content_height between rows in the group" do
t = @pdf.table([[{:content => "foo", :rowspan => 2}]])
heights = t.row_heights

0 comments on commit f5635ea

Please sign in to comment.