Class: Prawn::Table::ColumnWidthCalculator
- Inherits:
-
Object
- Object
- Prawn::Table::ColumnWidthCalculator
- Defined in:
- lib/prawn/table/column_width_calculator.rb
Instance Method Summary collapse
-
#initialize(cells) ⇒ ColumnWidthCalculator
constructor
A new instance of ColumnWidthCalculator.
- #natural_widths ⇒ Object
Constructor Details
#initialize(cells) ⇒ ColumnWidthCalculator
Returns a new instance of ColumnWidthCalculator.
4 5 6 7 8 9 |
# File 'lib/prawn/table/column_width_calculator.rb', line 4 def initialize(cells) @cells = cells @widths_by_column = Hash.new(0) @rows_with_a_span_dummy = Hash.new(false) end |
Instance Method Details
#natural_widths ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/prawn/table/column_width_calculator.rb', line 11 def natural_widths @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) 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 |