Class: RRTF::TableCellNode

Inherits:
CommandNode show all
Defined in:
lib/rrtf/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
TOP =

Top border

0
RIGHT =

Right border

1
BOTTOM =

Bottom border

2
LEFT =

Left border

3

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.



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

def initialize(row, width=DEFAULT_WIDTH, style=nil, top=nil, right=nil,
               bottom=nil, left=nil)
   super(row, nil)
   if !style.nil? and !style.is_paragraph_style?
      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.



943
944
945
# File 'lib/rrtf/node.rb', line 943

def shading_colour
  @shading_colour
end

#styleObject

Attribute accessor.



943
944
945
# File 'lib/rrtf/node.rb', line 943

def style
  @style
end

#widthObject

Width of cell



941
942
943
# File 'lib/rrtf/node.rb', line 941

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.



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

def border_width=(width)
   size = width.nil? ? 0 : width
   if size > 0
      @borders[TOP] = @borders[RIGHT] = @borders[BOTTOM] = @borders[LEFT] = 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.



1080
1081
1082
1083
1084
# File 'lib/rrtf/node.rb', line 1080

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.



1097
1098
1099
# File 'lib/rrtf/node.rb', line 1097

def bottom_border_width
   @borders[BOTTOM].nil? ? 0 : @borders[BOTTOM]
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.



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

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

#left_border_widthObject

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



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

def left_border_width
   @borders[LEFT].nil? ? 0 : @borders[LEFT]
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.



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

def left_border_width=(width)
   size = width.nil? ? 0 : width
   if size > 0
      @borders[LEFT] = size.to_i
   else
      @borders[LEFT] = 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



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

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.



1121
1122
1123
# File 'lib/rrtf/node.rb', line 1121

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.



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

def right_border_width
   @borders[RIGHT].nil? ? 0 : @borders[RIGHT]
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.



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

def right_border_width=(width)
   size = width.nil? ? 0 : width
   if size > 0
      @borders[RIGHT] = size.to_i
   else
      @borders[RIGHT] = 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.



1133
1134
1135
# File 'lib/rrtf/node.rb', line 1133

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.



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

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

   text << "\\pard\\intbl"
   text << @style.prefix(root) 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.



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

def top_border_width
   @borders[TOP].nil? ? 0 : @borders[TOP]
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.



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

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