Skip to content

Commit

Permalink
fixes colspan width calculations, e.g. #407
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrandl authored and practicingruby committed Jan 2, 2014
1 parent da81ed2 commit a04fb0c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
37 changes: 32 additions & 5 deletions lib/prawn/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -552,15 +552,42 @@ def natural_column_widths
@natural_column_widths ||=
begin
widths_by_column = Hash.new(0)

#calculate widths of rows without any span dummy
rows_with_a_span_dummy = Hash.new(false)
cells.each do |cell|
rows_with_a_span_dummy[cell.row] = true if cell.is_a?(Cell::SpanDummy)
end

#calculate natural column width for all rows that do not include a span dummy
cells.each do |cell|
unless rows_with_a_span_dummy[cell.row]
widths_by_column[cell.column] = [widths_by_column[cell.column], cell.width.to_f].max
end
end

#integrate natural column widths for all rows that do include a span dummy
cells.each do |cell|
next unless rows_with_a_span_dummy[cell.row]
#the width of a SpanDummy cell will be calculated by the "mother" cell
next if cell.is_a?(Cell::SpanDummy)

# Split the width of colspanned cells evenly by columns
width_per_column = cell.width.to_f / cell.colspan
cell.colspan.times do |i|
widths_by_column[cell.column + i] =
[widths_by_column[cell.column + i], width_per_column].max
if cell.colspan == 1
widths_by_column[cell.column] = [widths_by_column[cell.column], cell.width.to_f].max
else
#calculate the current with of all cells that will be spanned by the current cell
current_width_of_spanned_cells = widths_by_column.to_a[cell.column..(cell.column + cell.colspan - 1)].collect{|key, value| value}.inject(0, :+)
#update the Hash only if the new with is at least equal to the old one
if cell.width.to_f > current_width_of_spanned_cells
# Split the width of colspanned cells evenly by columns
width_per_column = cell.width.to_f / cell.colspan
# Update the Hash
cell.colspan.times do |i|
widths_by_column[cell.column + i] = width_per_column
end
end
end

end
widths_by_column.sort_by { |col, _| col }.map { |_, w| w }
end
Expand Down
24 changes: 22 additions & 2 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,35 @@
pdf = Prawn::Document.new
first = {:content=>"Foooo fo foooooo",:width=>50,:align=>:center}
second = {:content=>"Foooo",:colspan=>2,:width=>70,:align=>:center}
third = {:content=>"fooooooooooo, fooooooooooooo, fooo, foooooo fooooo",:width=>55,:align=>:center}
fourth = {:content=>"Bar",:width=>15,:align=>:center}
third = {:content=>"fooooooooooo, fooooooooooooo, fooo, foooooo fooooo",:width=>50,:align=>:center}
fourth = {:content=>"Bar",:width=>20,:align=>:center}
table_content = [[
first,
[[second],[third,fourth]]
]]
pdf.move_down(20)
table = Prawn::Table.new table_content, pdf
pdf.table(table_content)
end

#https://github.com/prawnpdf/prawn/issues/407#issuecomment-28556698
it "correctly computes column widths with empty cells + colspan" do
data = [['', ''],
[{:content => '', :colspan => 2}]
]
pdf = Prawn::Document.new
table = Prawn::Table.new data, pdf, :column_widths => [50, 200]
table.column_widths.should == [50.0, 200.0]
end

it "illustrates a variant of problem in issue #407 - comment 28556698" do
pdf = Prawn::Document.new
table_data = [["a", "b", "c"], [{:content=>"d", :colspan=>3}]]
column_widths = [50, 60, 400]
pdf.table(table_data,
:column_widths => column_widths
)
end
end

describe "#initialize" do
Expand Down

0 comments on commit a04fb0c

Please sign in to comment.