Class: RRTF::FontTable
- Inherits:
-
Object
- Object
- RRTF::FontTable
- Defined in:
- lib/rrtf/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
-
#[](index) ⇒ Object
This method overloads the array dereference operator for the FontTable class.
-
#add(font) ⇒ Object
(also: #<<)
This method adds a font to a FontTable instance.
-
#each ⇒ Object
This method iterates over the contents of a FontTable object.
-
#index(font) ⇒ Object
This method fetches the index of a font within a FontTable object.
-
#initialize(*fonts) ⇒ FontTable
constructor
This is the constructor for the RTFTable class.
-
#size ⇒ Object
This method is used to retrieve a count of the number of fonts held within an instance of the FontTable class.
-
#to_rtf(indent = 0) ⇒ Object
This method generates the RTF text for a FontTable object.
-
#to_s(indent = 0) ⇒ Object
This method generates a textual description for a FontTable object.
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.
101 102 103 104 |
# File 'lib/rrtf/font.rb', line 101 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.
138 139 140 |
# File 'lib/rrtf/font.rb', line 138 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.
118 119 120 121 122 123 |
# File 'lib/rrtf/font.rb', line 118 def add(font) if font.instance_of?(Font) @fonts.push(font) if @fonts.index(font).nil? end self end |
#each ⇒ Object
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).
128 129 130 |
# File 'lib/rrtf/font.rb', line 128 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.
147 148 149 |
# File 'lib/rrtf/font.rb', line 147 def index(font) @fonts.index(font) end |
#size ⇒ Object
This method is used to retrieve a count of the number of fonts held within an instance of the FontTable class.
108 109 110 |
# File 'lib/rrtf/font.rb', line 108 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.
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rrtf/font.rb', line 169 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.
156 157 158 159 160 161 162 |
# File 'lib/rrtf/font.rb', line 156 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 |