Class: Pdfrb::Layout::TableBox

Inherits:
Box
  • Object
show all
Defined in:
lib/pdfrb/layout/table_box.rb

Overview

A table of cells. Each cell holds a Box. Column widths are auto unless specified; row heights computed from content.

Instance Attribute Summary collapse

Attributes inherited from Box

#height, #style, #width

Instance Method Summary collapse

Methods inherited from Box

#draw, #supports_position_flow?

Constructor Details

#initialize(rows:, column_widths: nil) ⇒ TableBox

Returns a new instance of TableBox.



10
11
12
13
14
# File 'lib/pdfrb/layout/table_box.rb', line 10

def initialize(rows:, column_widths: nil, **)
  super(**)
  @rows = rows
  @column_widths = column_widths
end

Instance Attribute Details

#column_widthsObject (readonly)

Returns the value of attribute column_widths.



8
9
10
# File 'lib/pdfrb/layout/table_box.rb', line 8

def column_widths
  @column_widths
end

#rowsObject (readonly)

Returns the value of attribute rows.



8
9
10
# File 'lib/pdfrb/layout/table_box.rb', line 8

def rows
  @rows
end

Instance Method Details

#draw_content(canvas, x, y) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pdfrb/layout/table_box.rb', line 34

def draw_content(canvas, x, y)
  offset_y = y
  @rows.each do |row|
    max_h = 0
    offset_x = x
    row.each_with_index do |cell, col|
      cell.draw(canvas, offset_x, offset_y)
      offset_x += @column_widths[col]
      max_h = cell.height if cell.height > max_h
    end
    offset_y -= max_h
  end
end

#empty?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/pdfrb/layout/table_box.rb', line 48

def empty?
  @rows.empty?
end

#fit?(available_width, available_height) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pdfrb/layout/table_box.rb', line 16

def fit?(available_width, available_height)
  compute_column_widths(available_width)
  remaining = available_height
  @rows.each do |row|
    row_heights = []
    row.each_with_index do |cell, col|
      cell_w = @column_widths[col]
      cell.fit?(cell_w, remaining) or return false
      row_heights << cell.height
    end
    row_h = row_heights.max || 0
    remaining -= row_h
  end
  @width = available_width
  @height = available_height - remaining
  true
end