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.

Cells may declare colspan / rowspan to occupy multiple columns or rows. Cell entries can be plain Boxes (treated as 1x1 cells) or TableBox::Cell instances created via TableBox.cell(box, colspan:, rowspan:).

Defined Under Namespace

Classes: Cell

Instance Attribute Summary collapse

Attributes inherited from Box

#height, #style, #width

Class Method Summary collapse

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.



34
35
36
37
38
# File 'lib/pdfrb/layout/table_box.rb', line 34

def initialize(rows:, column_widths: nil, **)
  super(**)
  @rows = rows.map { |row| row.map { |c| wrap_cell(c) } }
  @column_widths = column_widths
end

Instance Attribute Details

#column_widthsObject (readonly)

Returns the value of attribute column_widths.



27
28
29
# File 'lib/pdfrb/layout/table_box.rb', line 27

def column_widths
  @column_widths
end

#rowsObject (readonly)

Returns the value of attribute rows.



27
28
29
# File 'lib/pdfrb/layout/table_box.rb', line 27

def rows
  @rows
end

Class Method Details

.cell(box, colspan: 1, rowspan: 1) ⇒ Object

Build a Cell wrapping box with the given spans.



30
31
32
# File 'lib/pdfrb/layout/table_box.rb', line 30

def self.cell(box, colspan: 1, rowspan: 1)
  Cell.new(box: box, colspan: colspan, rowspan: rowspan)
end

Instance Method Details

#draw_content(canvas, x, y) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pdfrb/layout/table_box.rb', line 47

def draw_content(canvas, x, y)
  offset_y = y
  row_heights.each_with_index do |row_h, row_index|
    offset_x = x
    @column_widths.each_with_index do |_w, col_index|
      cell = cell_at(row_index, col_index)
      if cell && origin?(row_index, col_index, cell)
        span_w = spanned_width(col_index, cell.colspan)
        span_h = spanned_height(row_index, cell.rowspan)
        cell.box.fit?(span_w, span_h)
        cell.draw(canvas, offset_x, offset_y)
      end
      offset_x += @column_widths[col_index]
    end
    offset_y -= row_h
  end
end

#empty?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/pdfrb/layout/table_box.rb', line 65

def empty?
  @rows.empty?
end

#fit?(available_width, available_height) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/pdfrb/layout/table_box.rb', line 40

def fit?(available_width, available_height)
  compute_column_widths(available_width)
  layout_grid!
  fit_rows!(available_height)
  @height <= available_height
end

#row_heightsObject

Height of each row, indexed by row position. Populated by fit?.



71
72
73
# File 'lib/pdfrb/layout/table_box.rb', line 71

def row_heights
  @row_heights || []
end