Class: Arrolio::Flowables::TableFlowable

Inherits:
Arrolio::Flowable show all
Defined in:
lib/arrolio/flowables/table_flowable.rb

Overview

Real table layout: equal-width columns by default (TODO 04 adds auto/fixed), borders per cell, header row repeat.

Instance Attribute Summary collapse

Attributes inherited from Arrolio::Flowable

#style

Instance Method Summary collapse

Methods inherited from Arrolio::Flowable

#keep_together?, #keep_with_next?, #page_break_after?, #page_break_before?, #space_after, #space_before, #split

Constructor Details

#initialize(table, style: Style::Definition.new) ⇒ TableFlowable

Returns a new instance of TableFlowable.



10
11
12
13
# File 'lib/arrolio/flowables/table_flowable.rb', line 10

def initialize(table, style: Style::Definition.new)
  super(style: style)
  @table = table
end

Instance Attribute Details

#column_widthsObject (readonly)

Returns the value of attribute column_widths.



8
9
10
# File 'lib/arrolio/flowables/table_flowable.rb', line 8

def column_widths
  @column_widths
end

#tableObject (readonly)

Returns the value of attribute table.



8
9
10
# File 'lib/arrolio/flowables/table_flowable.rb', line 8

def table
  @table
end

Instance Method Details

#do_split(width, remaining_height, _context = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/arrolio/flowables/table_flowable.rb', line 50

def do_split(width, remaining_height, _context = nil)
  cols = column_widths_for(width)
  head_rows = []
  tail_rows = []
  used = 0.0
  header, body = partition_rows

  body.each do |row|
    rh = row_height(row, cols)
    if used + rh <= remaining_height || head_rows.empty?
      head_rows << row
      used += rh
    else
      tail_rows << row
    end
  end

  if head_rows.any?
    head_table = rebuild_table(header, head_rows)
    head = self.class.new(head_table, style: @style)
  else
    head = nil
  end

  tail_table = rebuild_table(header, tail_rows)
  tail = self.class.new(tail_table, style: @style)
  [head, tail]
end

#emit(x, y, width, context = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/arrolio/flowables/table_flowable.rb', line 24

def emit(x, y, width, context = nil)
  cols = column_widths_for(width)
  boxes = []
  consumed = 0.0
  cursor = y
  header_rows, body_rows = partition_rows

  (header_rows + body_rows).each do |row|
    rh = row_height(row, cols)
    cursor -= rh
    consumed += rh
    cell_x = x.to_f
    row.cells.each_with_index do |cell, ci|
      cw = cols[ci] || (width / [row.cells.length, 1].max)
      render_cell(cell, cell_x, cursor, cw, rh, boxes, context)
      cell_x += cw
    end
  end

  [boxes, consumed]
end

#height(width, _context = nil) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/arrolio/flowables/table_flowable.rb', line 15

def height(width, _context = nil)
  cols = column_widths_for(width)
  total = 0.0
  rows.each do |row|
    total += row_height(row, cols)
  end
  total
end

#splittable?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/arrolio/flowables/table_flowable.rb', line 46

def splittable?
  true
end