Class: RTF::TableCellNode

Inherits:
CommandNode show all
Defined in:
lib/rtf/node.rb

Overview

This class represents a cell within an RTF table. The TableCellNode is a specialised command node that is forbidden from creating tables or having its parent changed.

Constant Summary collapse

DEFAULT_WIDTH =

A definition for the default width for the cell.

300

Instance Attribute Summary collapse

Attributes inherited from CommandNode

#prefix, #split, #suffix, #wrap

Attributes inherited from ContainerNode

#children

Attributes inherited from Node

#parent

Instance Method Summary collapse

Methods inherited from CommandNode

#<<, #apply, #background, #bold, #colour, #font, #footnote, #foreground, #image, #italic, #line_break, #link, #list, #strike, #subscript, #superscript, #underline

Methods inherited from ContainerNode

#[], #each, #first, #last, #size, #store

Methods inherited from Node

#is_root?, #next_node, #previous_node, #root

Constructor Details

#initialize(row, width = DEFAULT_WIDTH, style = nil, top = nil, right = nil, bottom = nil, left = nil) ⇒ TableCellNode

This is the constructor for the TableCellNode class.

Parameters

row

The row that the cell belongs to.

width

The width to be assigned to the cell. This defaults to TableCellNode::DEFAULT_WIDTH.

style

The style that is applied to the cell. This must be a ParagraphStyle class. Defaults to nil.

top

The border width for the cells top border. Defaults to nil.

right

The border width for the cells right hand border. Defaults to nil.

bottom

The border width for the cells bottom border. Defaults to nil.

left

The border width for the cells left hand border. Defaults to nil.

Exceptions

RTFError

Generated whenever an invalid style setting is specified.



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/rtf/node.rb', line 952

def initialize(row, width=DEFAULT_WIDTH, style=nil, top=nil, right=nil,
               bottom=nil, left=nil)
   super(row, nil)
   if style != nil && style.is_paragraph_style? == false
      RTFError.fire("Non-paragraph style specified for TableCellNode "\
                    "constructor.")
   end

   @width          = (width != nil && width > 0) ? width : DEFAULT_WIDTH
   @borders        = [(top != nil && top > 0) ? top : nil,
                      (right != nil && right > 0) ? right : nil,
                      (bottom != nil && bottom > 0) ? bottom : nil,
                      (left != nil && left > 0) ? left : nil]
   @shading_colour = nil
   @style          = style
end

Instance Attribute Details

#shading_colourObject

Attribute accessor.



933
934
935
# File 'lib/rtf/node.rb', line 933

def shading_colour
  @shading_colour
end

#styleObject

Attribute accessor.



933
934
935
# File 'lib/rtf/node.rb', line 933

def style
  @style
end

#widthObject

Width of cell



931
932
933
# File 'lib/rtf/node.rb', line 931

def width
  @width
end

Instance Method Details

#border_width=(width) ⇒ Object

This method assigns a width, in twips, for the borders on all sides of the cell. Negative widths will be ignored and a width of zero will switch the border off.

Parameters

width

The setting for the width of the border.



992
993
994
995
996
997
998
999
# File 'lib/rtf/node.rb', line 992

def border_width=(width)
   size = width == nil ? 0 : width
   if size > 0
      @borders[0] = @borders[1] = @borders[2] = @borders[3] = size.to_i
   else
      @borders = [nil, nil, nil, nil]
   end
end

#border_widthsObject

This method retrieves an array with the cell border width settings. The values are inserted in top, right, bottom, left order.



1070
1071
1072
1073
1074
# File 'lib/rtf/node.rb', line 1070

def border_widths
   widths = []
   @borders.each {|entry| widths.push(entry == nil ? 0 : entry)}
   widths
end

#bottom_border_widthObject

This method fetches the width for bottom border of a cell.



1087
1088
1089
# File 'lib/rtf/node.rb', line 1087

def bottom_border_width
   @borders[2] == nil ? 0 : @borders[2]
end

#bottom_border_width=(width) ⇒ Object

This method assigns a border width to the bottom side of a table cell. Negative values are ignored and a value of 0 switches the border off.

Parameters

width

The new border width setting.



1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/rtf/node.rb', line 1034

def bottom_border_width=(width)
   size = width == nil ? 0 : width
   if size > 0
      @borders[2] = size.to_i
   else
      @borders[2] = nil
   end
end

#left_border_widthObject

This method fetches the width for left border of a cell.



1092
1093
1094
# File 'lib/rtf/node.rb', line 1092

def left_border_width
   @borders[3] == nil ? 0 : @borders[3]
end

#left_border_width=(width) ⇒ Object

This method assigns a border width to the left side of a table cell. Negative values are ignored and a value of 0 switches the border off.

Parameters

width

The new border width setting.



1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/rtf/node.rb', line 1048

def left_border_width=(width)
   size = width == nil ? 0 : width
   if size > 0
      @borders[3] = size.to_i
   else
      @borders[3] = nil
   end
end

#paragraph(style = nil) ⇒ Object

This method overloads the paragraph method inherited from the ComamndNode class to forbid the creation of paragraphs.

Parameters

style

The paragraph style, ignored



1101
1102
1103
1104
# File 'lib/rtf/node.rb', line 1101

def paragraph(style=nil)
   RTFError.fire("TableCellNode#paragraph() called. Table cells cannot "\
                 "contain paragraphs.")
end

#parent=(parent) ⇒ Object

This method overloads the parent= method inherited from the Node class to forbid the alteration of the cells parent.

Parameters

parent

A reference to the new node parent.



1111
1112
1113
# File 'lib/rtf/node.rb', line 1111

def parent=(parent)
   RTFError.fire("Table cell nodes cannot have their parent changed.")
end

#right_border_widthObject

This method fetches the width for right border of a cell.



1082
1083
1084
# File 'lib/rtf/node.rb', line 1082

def right_border_width
   @borders[1] == nil ? 0 : @borders[1]
end

#right_border_width=(width) ⇒ Object

This method assigns a border width to the right side of a table cell. Negative values are ignored and a value of 0 switches the border off.

Parameters

width

The new border width setting.



1020
1021
1022
1023
1024
1025
1026
1027
# File 'lib/rtf/node.rb', line 1020

def right_border_width=(width)
   size = width == nil ? 0 : width
   if size > 0
      @borders[1] = size.to_i
   else
      @borders[1] = nil
   end
end

#table(rows, columns, *widths) ⇒ Object

This method overrides the table method inherited from CommandNode to forbid its use in table cells.

Parameters

rows

The number of rows for the table.

columns

The number of columns for the table.

*widths

One or more integers representing the widths for the table columns.



1123
1124
1125
# File 'lib/rtf/node.rb', line 1123

def table(rows, columns, *widths)
   RTFError.fire("TableCellNode#table() called. Nested tables not allowed.")
end

#to_rtfObject

This method generates the RTF document text for a TableCellNode object.



1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
# File 'lib/rtf/node.rb', line 1128

def to_rtf
   text      = StringIO.new
   separator = split? ? "\n" : " "
   line      = (separator == " ")

   text << "\\pard\\intbl"
   text << @style.prefix(nil, nil) if @style != nil
   text << separator
   self.each do |entry|
      text << "\n" if line
      line = true
      text << entry.to_rtf
   end
   text << (split? ? "\n" : " ")
   text << "\\cell"

   text.string
end

#top_border_widthObject

This method fetches the width for top border of a cell.



1077
1078
1079
# File 'lib/rtf/node.rb', line 1077

def top_border_width
   @borders[0] == nil ? 0 : @borders[0]
end

#top_border_width=(width) ⇒ Object

This method assigns a border width to the top side of a table cell. Negative values are ignored and a value of 0 switches the border off.

Parameters

width

The new border width setting.



1006
1007
1008
1009
1010
1011
1012
1013
# File 'lib/rtf/node.rb', line 1006

def top_border_width=(width)
   size = width == nil ? 0 : width
   if size > 0
      @borders[0] = size.to_i
   else
      @borders[0] = nil
   end
end