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.



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/rtf/node.rb', line 772

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.



750
751
752
# File 'lib/rtf/node.rb', line 750

def shading_colour
  @shading_colour
end

#styleObject

Attribute accessor.



750
751
752
# File 'lib/rtf/node.rb', line 750

def style
  @style
end

#widthObject

Attribute accessor.



750
751
752
# File 'lib/rtf/node.rb', line 750

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.



812
813
814
815
816
817
818
819
# File 'lib/rtf/node.rb', line 812

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.



890
891
892
893
894
# File 'lib/rtf/node.rb', line 890

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.



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

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.



854
855
856
857
858
859
860
861
# File 'lib/rtf/node.rb', line 854

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.



912
913
914
# File 'lib/rtf/node.rb', line 912

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.



868
869
870
871
872
873
874
875
# File 'lib/rtf/node.rb', line 868

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.



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

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.



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

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.



902
903
904
# File 'lib/rtf/node.rb', line 902

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.



840
841
842
843
844
845
846
847
# File 'lib/rtf/node.rb', line 840

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.



954
955
956
# File 'lib/rtf/node.rb', line 954

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.



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

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.



897
898
899
# File 'lib/rtf/node.rb', line 897

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.



826
827
828
829
830
831
832
833
# File 'lib/rtf/node.rb', line 826

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