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.



961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/rtf/node.rb', line 961

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.



942
943
944
# File 'lib/rtf/node.rb', line 942

def shading_colour
  @shading_colour
end

#styleObject

Attribute accessor.



942
943
944
# File 'lib/rtf/node.rb', line 942

def style
  @style
end

#widthObject

Width of cell



940
941
942
# File 'lib/rtf/node.rb', line 940

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.



1001
1002
1003
1004
1005
1006
1007
1008
# File 'lib/rtf/node.rb', line 1001

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.



1079
1080
1081
1082
1083
# File 'lib/rtf/node.rb', line 1079

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.



1096
1097
1098
# File 'lib/rtf/node.rb', line 1096

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.



1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/rtf/node.rb', line 1043

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.



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

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.



1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/rtf/node.rb', line 1057

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



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

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.



1120
1121
1122
# File 'lib/rtf/node.rb', line 1120

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.



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

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.



1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/rtf/node.rb', line 1029

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.



1132
1133
1134
# File 'lib/rtf/node.rb', line 1132

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.



1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/rtf/node.rb', line 1137

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.



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

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.



1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/rtf/node.rb', line 1015

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