Class: RTF::FontTable

Inherits:
Object
  • Object
show all
Defined in:
lib/rtf/font.rb

Overview

This class represents the font table for an RTF document. An instance of the class is used internally by the Document class and should not need to be explicitly instantiated (although it can be obtained from a Document object if needed).

Instance Method Summary collapse

Constructor Details

#initialize(*fonts) ⇒ FontTable

This is the constructor for the RTFTable class.

Parameters

*fonts

Zero or more font objects that are to be added to the font table. Objects that are not Fonts will be ignored.



92
93
94
95
# File 'lib/rtf/font.rb', line 92

def initialize(*fonts)
   @fonts = []
   fonts.each {|font| add(font)}
end

Instance Method Details

#[](index) ⇒ Object

This method overloads the array dereference operator for the FontTable class.

Parameters

index

The index into the font table of the font to be retrieved. If the index is invalid then nil is returned.



129
130
131
# File 'lib/rtf/font.rb', line 129

def [](index)
   @fonts[index]
end

#add(font) ⇒ Object Also known as: <<

This method adds a font to a FontTable instance. This method returns a reference to the FontTable object updated.

Parameters

font

A reference to the font to be added. If this is not a Font object or already exists in the table it will be ignored.



109
110
111
112
113
114
# File 'lib/rtf/font.rb', line 109

def add(font)
   if font.instance_of?(Font)
      @fonts.push(font) if @fonts.index(font) == nil
   end
   self
end

#eachObject

This method iterates over the contents of a FontTable object. This method expects a block that takes a single parameter (the next font from the table).



119
120
121
# File 'lib/rtf/font.rb', line 119

def each
   @fonts.each {|font| yield font} if block_given?
end

#index(font) ⇒ Object

This method fetches the index of a font within a FontTable object. If the font does not exist in the table then nil is returned.

Parameters

font

A reference to the font to check for.



138
139
140
# File 'lib/rtf/font.rb', line 138

def index(font)
   @fonts.index(font)
end

#sizeObject

This method is used to retrieve a count of the number of fonts held within an instance of the FontTable class.



99
100
101
# File 'lib/rtf/font.rb', line 99

def size
   @fonts.size
end

#to_rtf(indent = 0) ⇒ Object

This method generates the RTF text for a FontTable object.

Parameters

indent

The number of spaces to prefix to the lines generated by the method. Defaults to zero.



160
161
162
163
164
165
166
167
168
169
# File 'lib/rtf/font.rb', line 160

def to_rtf(indent=0)
   prefix = indent > 0 ? ' ' * indent : ''
   text   = StringIO.new
   text << "#{prefix}{\\fonttbl"
   @fonts.each_index do |index|
      text << "\n#{prefix}{\\f#{index}#{@fonts[index].to_rtf}}"
   end
   text << "\n#{prefix}}"
   text.string
end

#to_s(indent = 0) ⇒ Object

This method generates a textual description for a FontTable object.

Parameters

indent

The number of spaces to prefix to the lines generated by the method. Defaults to zero.



147
148
149
150
151
152
153
# File 'lib/rtf/font.rb', line 147

def to_s(indent=0)
   prefix = indent > 0 ? ' ' * indent : ''
   text   = StringIO.new
   text << "#{prefix}Font Table (#{@fonts.size} fonts)"
   @fonts.each {|font| text << "\n#{prefix}   #{font}"}
   text.string
end