Class: RTF::Document

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

Overview

This class represents an RTF document. In actuality it is just a specialised Node type that cannot be assigned a parent and that holds document font, colour and information tables.

Constant Summary collapse

CS_ANSI =

A definition for a document character set setting.

:ansi
CS_MAC =

A definition for a document character set setting.

:mac
CS_PC =

A definition for a document character set setting.

:pc
CS_PCA =

A definition for a document character set setting.

:pca
LC_AFRIKAANS =

A definition for a document language setting.

1078
LC_ARABIC =

A definition for a document language setting.

1025
LC_CATALAN =

A definition for a document language setting.

1027
LC_CHINESE_TRADITIONAL =

A definition for a document language setting.

1028
LC_CHINESE_SIMPLIFIED =

A definition for a document language setting.

2052
LC_CZECH =

A definition for a document language setting.

1029
LC_DANISH =

A definition for a document language setting.

1030
LC_DUTCH =

A definition for a document language setting.

1043
LC_DUTCH_BELGIAN =

A definition for a document language setting.

2067
LC_ENGLISH_UK =

A definition for a document language setting.

2057
LC_ENGLISH_US =

A definition for a document language setting.

1033
LC_FINNISH =

A definition for a document language setting.

1035
LC_FRENCH =

A definition for a document language setting.

1036
LC_FRENCH_BELGIAN =

A definition for a document language setting.

2060
LC_FRENCH_CANADIAN =

A definition for a document language setting.

3084
LC_FRENCH_SWISS =

A definition for a document language setting.

4108
LC_GERMAN =

A definition for a document language setting.

1031
LC_GERMAN_SWISS =

A definition for a document language setting.

2055
LC_GREEK =

A definition for a document language setting.

1032
LC_HEBREW =

A definition for a document language setting.

1037
LC_HUNGARIAN =

A definition for a document language setting.

1038
LC_ICELANDIC =

A definition for a document language setting.

1039
LC_INDONESIAN =

A definition for a document language setting.

1057
LC_ITALIAN =

A definition for a document language setting.

1040
LC_JAPANESE =

A definition for a document language setting.

1041
LC_KOREAN =

A definition for a document language setting.

1042
LC_NORWEGIAN_BOKMAL =

A definition for a document language setting.

1044
LC_NORWEGIAN_NYNORSK =

A definition for a document language setting.

2068
LC_POLISH =

A definition for a document language setting.

1045
LC_PORTUGUESE =

A definition for a document language setting.

2070
LC_POTUGUESE_BRAZILIAN =

A definition for a document language setting.

1046
LC_ROMANIAN =

A definition for a document language setting.

1048
LC_RUSSIAN =

A definition for a document language setting.

1049
LC_SERBO_CROATIAN_CYRILLIC =

A definition for a document language setting.

2074
LC_SERBO_CROATIAN_LATIN =

A definition for a document language setting.

1050
LC_SLOVAK =

A definition for a document language setting.

1051
LC_SPANISH_CASTILLIAN =

A definition for a document language setting.

1034
LC_SPANISH_MEXICAN =

A definition for a document language setting.

2058
LC_SWAHILI =

A definition for a document language setting.

1089
LC_SWEDISH =

A definition for a document language setting.

1053
LC_THAI =

A definition for a document language setting.

1054
LC_TURKISH =

A definition for a document language setting.

1055
LC_UNKNOWN =

A definition for a document language setting.

1024
LC_VIETNAMESE =

A definition for a document language setting.

1066

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, #paragraph, #strike, #subscript, #superscript, #table, #underline

Methods inherited from ContainerNode

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

Methods inherited from Node

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

Constructor Details

#initialize(font, style = nil, character = CS_ANSI, language = LC_ENGLISH_UK) ⇒ Document

This is a constructor for the Document class.

Parameters

font

The default font to be used by the document.

style

The style settings to be applied to the document. This defaults to nil.

character

The character set to be applied to the document. This defaults to Document::CS_ANSI.

language

The language setting to be applied to document. This defaults to Document::LC_ENGLISH_UK.



1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
# File 'lib/rtf/node.rb', line 1651

def initialize(font, style=nil, character=CS_ANSI, language=LC_ENGLISH_UK)
   super(nil, '\rtf1')
   @fonts         = FontTable.new(font)
   @lists         = ListTable.new
   @default_font  = 0
   @colours       = ColourTable.new
   @information   = Information.new
   @character_set = character
   @language      = language
   @style         = style == nil ? DocumentStyle.new : style
   @headers       = [nil, nil, nil, nil]
   @footers       = [nil, nil, nil, nil]
   @id            = 0
end

Instance Attribute Details

#character_setObject

Attribute accessor.



1634
1635
1636
# File 'lib/rtf/node.rb', line 1634

def character_set
  @character_set
end

#coloursObject (readonly)

Attribute accessor.



1634
1635
1636
# File 'lib/rtf/node.rb', line 1634

def colours
  @colours
end

#fontsObject (readonly)

Attribute accessor.



1634
1635
1636
# File 'lib/rtf/node.rb', line 1634

def fonts
  @fonts
end

#informationObject (readonly)

Attribute accessor.



1634
1635
1636
# File 'lib/rtf/node.rb', line 1634

def information
  @information
end

#languageObject

Attribute accessor.



1634
1635
1636
# File 'lib/rtf/node.rb', line 1634

def language
  @language
end

#listsObject (readonly)

Attribute accessor.



1634
1635
1636
# File 'lib/rtf/node.rb', line 1634

def lists
  @lists
end

#styleObject (readonly)

Attribute accessor.



1634
1635
1636
# File 'lib/rtf/node.rb', line 1634

def style
  @style
end

Instance Method Details

#body_heightObject

This method fetches the height of the available work area space for a a typical Document object page.



1793
1794
1795
# File 'lib/rtf/node.rb', line 1793

def body_height
   @style.body_height
end

#body_widthObject

This method fetches the width of the available work area space for a typical Document object page.



1787
1788
1789
# File 'lib/rtf/node.rb', line 1787

def body_width
   @style.body_width
end

#default_fontObject

Attribute accessor.



1674
1675
1676
# File 'lib/rtf/node.rb', line 1674

def default_font
   @fonts[@default_font]
end

#default_font=(font) ⇒ Object

Attribute mutator.

Parameters

font

The new default font for the Document object.



1756
1757
1758
1759
# File 'lib/rtf/node.rb', line 1756

def default_font=(font)
   @fonts << font
   @default_font = @fonts.index(font)
end

This method fetches a footer from a Document object.

Parameters

type

One of the footer types defined in the footer class. Defaults to FooterNode::UNIVERSAL.



1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
# File 'lib/rtf/node.rb', line 1740

def footer(type=FooterNode::UNIVERSAL)
   index = 0
   if type == FooterNode::LEFT_PAGE
      index = 1
   elsif type == FooterNode::RIGHT_PAGE
      index = 2
   elsif type == FooterNode::FIRST_PAGE
      index = 3
   end
   @footers[index]
end

#footer=(footer) ⇒ Object

This method assigns a new footer to a document. A Document object can have up to four footers - a default footer, a footer for left pages, a footer for right pages and a footer for the first page. The method checks the footer type and stores it appropriately.

Parameters

footer

A reference to the footer object to be stored. Existing footer objects are overwritten.



1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
# File 'lib/rtf/node.rb', line 1706

def footer=(footer)
   if footer.type == FooterNode::UNIVERSAL
      @footers[0] = footer
   elsif footer.type == FooterNode::LEFT_PAGE
      @footers[1] = footer
   elsif footer.type == FooterNode::RIGHT_PAGE
      @footers[2] = footer
   elsif footer.type == FooterNode::FIRST_PAGE
      @footers[3] = footer
   end
end

#get_idObject

This method provides a method that can be called to generate an identifier that is unique within the document.



1668
1669
1670
1671
# File 'lib/rtf/node.rb', line 1668

def get_id
   @id += 1
   Time.now().strftime('%d%m%y') + @id.to_s
end

#header(type = HeaderNode::UNIVERSAL) ⇒ Object

This method fetches a header from a Document object.

Parameters

type

One of the header types defined in the header class. Defaults to HeaderNode::UNIVERSAL.



1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
# File 'lib/rtf/node.rb', line 1723

def header(type=HeaderNode::UNIVERSAL)
   index = 0
   if type == HeaderNode::LEFT_PAGE
      index = 1
   elsif type == HeaderNode::RIGHT_PAGE
      index = 2
   elsif type == HeaderNode::FIRST_PAGE
      index = 3
   end
   @headers[index]
end

#header=(header) ⇒ Object

This method assigns a new header to a document. A Document object can have up to four header - a default header, a header for left pages, a header for right pages and a header for the first page. The method checks the header type and stores it appropriately.

Parameters

header

A reference to the header object to be stored. Existing header objects are overwritten.



1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
# File 'lib/rtf/node.rb', line 1686

def header=(header)
   if header.type == HeaderNode::UNIVERSAL
      @headers[0] = header
   elsif header.type == HeaderNode::LEFT_PAGE
      @headers[1] = header
   elsif header.type == HeaderNode::RIGHT_PAGE
      @headers[2] = header
   elsif header.type == HeaderNode::FIRST_PAGE
      @headers[3] = header
   end
end

#page_breakObject

This method inserts a page break into a document.



1780
1781
1782
1783
# File 'lib/rtf/node.rb', line 1780

def page_break
   self.store(CommandNode.new(self, '\page', nil, false))
   nil
end

#paperObject

This method provides a short cut for obtaining the Paper object associated with a Document object.



1763
1764
1765
# File 'lib/rtf/node.rb', line 1763

def paper
   @style.paper
end

#parent=(parent) ⇒ Object

This method overrides the parent=() method inherited from the CommandNode class to disallow setting a parent on a Document object.

Parameters

parent

A reference to the new parent node for the Document object.

Exceptions

RTFError

Generated whenever this method is called.



1775
1776
1777
# File 'lib/rtf/node.rb', line 1775

def parent=(parent)
   RTFError.fire("Document objects may not have a parent.")
end

#to_rtfObject

This method generates the RTF text for a Document object.



1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
# File 'lib/rtf/node.rb', line 1798

def to_rtf
   text = StringIO.new

   text << "{#{prefix}\\#{@character_set.id2name}"
   text << "\\deff#{@default_font}"
   text << "\\deflang#{@language}" if @language != nil
   text << "\\plain\\fs24\\fet1"
   text << "\n#{@fonts.to_rtf}"
   text << "\n#{@colours.to_rtf}" if @colours.size > 0
   text << "\n#{@information.to_rtf}"
   text << "\n#{@lists.to_rtf}"
   if @headers.compact != []
      text << "\n#{@headers[3].to_rtf}" if @headers[3] != nil
      text << "\n#{@headers[2].to_rtf}" if @headers[2] != nil
      text << "\n#{@headers[1].to_rtf}" if @headers[1] != nil
      if @headers[1] == nil or @headers[2] == nil
         text << "\n#{@headers[0].to_rtf}"
      end
   end
   if @footers.compact != []
      text << "\n#{@footers[3].to_rtf}" if @footers[3] != nil
      text << "\n#{@footers[2].to_rtf}" if @footers[2] != nil
      text << "\n#{@footers[1].to_rtf}" if @footers[1] != nil
      if @footers[1] == nil or @footers[2] == nil
         text << "\n#{@footers[0].to_rtf}"
      end
   end
   text << "\n#{@style.prefix(self)}" if @style != nil
   self.each {|entry| text << "\n#{entry.to_rtf}"}
   text << "\n}"

   text.string
end