Class: Wizport::Rtf::Table

Inherits:
Element
  • Object
show all
Defined in:
lib/wizport/document/rtf/table.rb

Constant Summary collapse

DEFAULT_COLUMN_WIDTH =
40

Instance Method Summary collapse

Methods inherited from Element

#cmd, #group, #txt

Constructor Details

#initialize(rtf, rows = [], options = {}, &block) ⇒ Table

Returns a new instance of Table.



11
12
13
14
15
16
17
18
19
# File 'lib/wizport/document/rtf/table.rb', line 11

def initialize(rtf, rows = [], options = {}, &block)
  super(rtf)
  @row_spans = {}
  @column_widths = options[:column_widths] || DEFAULT_COLUMN_WIDTH
  rows.each_index do |index|
    add_row rows[index]
  end
  block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
end

Instance Method Details

#add_cell(cell, merge = false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wizport/document/rtf/table.rb', line 33

def add_cell(cell,merge = false)
  add_cell "",true if !merge && row_spanned?(@col_offset)
  if cell.is_a?(Hash)
    rowspan = cell[:rowspan]
    colspan = cell[:colspan]
    content = cell[:content]
    @row_spans[@col_offset] = cell if cell[:rowspan]
  end
  colspan = colspan || 1
  rowspan = rowspan || 1
  content = content || cell

  if rowspan > 1
    v_merge = :gf
  elsif row_spanned? @col_offset
    v_merge = :rg
    @row_spans[@col_offset][:rowspan] -= 1
    colspan = @row_spans[@col_offset][:colspan] || colspan
  end
  colspan.times do
    @right_width += column_width(@col_offset)
    @col_offset += 1
  end

  cmd :celld
  cmd :clvmgf if v_merge == :gf
  cmd :clvmrg if v_merge == :rg
  cmd :cellx, @right_width
  txt content
  cmd :cell

  add_cell "",true if  row_spanned?(@col_offset)
end

#add_row(cells = []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wizport/document/rtf/table.rb', line 21

def add_row(cells = [])
  cmd :trowd
  cmd :trautofit1
  cmd :intbl
  @col_offset = 1
  @right_width = 0
  cells.each do |cell|
    add_cell cell
  end
  cmd :row
end

#column_width(offset) ⇒ Object



71
72
73
74
# File 'lib/wizport/document/rtf/table.rb', line 71

def column_width(offset)
  return 20 * (@column_widths[offset] || DEFAULT_COLUMN_WIDTH) if @column_widths.is_a?(Hash)
  @column_widths * 20
end

#row_spanned?(offset) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/wizport/document/rtf/table.rb', line 67

def row_spanned?(offset)
  @row_spans[offset] && @row_spans[offset][:rowspan].to_i > 1
end