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

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, #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.



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/rtf/node.rb', line 789

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.



770
771
772
# File 'lib/rtf/node.rb', line 770

def shading_colour
  @shading_colour
end

#styleObject

Attribute accessor.



770
771
772
# File 'lib/rtf/node.rb', line 770

def style
  @style
end

#widthObject

Width of cell



768
769
770
# File 'lib/rtf/node.rb', line 768

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.



829
830
831
832
833
834
835
836
# File 'lib/rtf/node.rb', line 829

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.



907
908
909
910
911
# File 'lib/rtf/node.rb', line 907

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.



924
925
926
# File 'lib/rtf/node.rb', line 924

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.



871
872
873
874
875
876
877
878
# File 'lib/rtf/node.rb', line 871

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.



929
930
931
# File 'lib/rtf/node.rb', line 929

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.



885
886
887
888
889
890
891
892
# File 'lib/rtf/node.rb', line 885

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(justification = CommandNode::LEFT_JUSTIFY, before = nil, after = nil, left = nil, right = nil, first = nil) ⇒ Object

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

Parameters

justification

The justification to be applied to the paragraph.

before

The amount of space, in twips, to be inserted before the paragraph. Defaults to nil.

after

The amount of space, in twips, to be inserted after the paragraph. Defaults to nil.

left

The amount of indentation to place on the left of the paragraph. Defaults to nil.

right

The amount of indentation to place on the right of the paragraph. Defaults to nil.

first

The amount of indentation to place on the left of the first line in the paragraph. Defaults to nil.



948
949
950
951
952
# File 'lib/rtf/node.rb', line 948

def paragraph(justification=CommandNode::LEFT_JUSTIFY, before=nil,
              after=nil, left=nil, right=nil, first=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.



959
960
961
# File 'lib/rtf/node.rb', line 959

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.



919
920
921
# File 'lib/rtf/node.rb', line 919

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.



857
858
859
860
861
862
863
864
# File 'lib/rtf/node.rb', line 857

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.



971
972
973
# File 'lib/rtf/node.rb', line 971

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.



976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
# File 'lib/rtf/node.rb', line 976

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.



914
915
916
# File 'lib/rtf/node.rb', line 914

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.



843
844
845
846
847
848
849
850
# File 'lib/rtf/node.rb', line 843

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