Repository URL to install this package:
|
Version:
0.5.0 ▾
|
#!/usr/bin/env ruby
require 'stringio'
module RTF
# This class represents a font for use with some RTF content.
class Font
# A declaration for a font family. This family is used for monospaced
# fonts (e.g. Courier New).
MODERN = :modern
# A declaration for a font family. This family is used for proportionally
# spaced serif fonts (e.g. Arial, Times New Roman).
ROMAN = :roman
# A declaration for a font family. This family is used for proportionally
# spaced sans serif fonts (e.g. Tahoma, Lucida Sans).
SWISS = :swiss
# A declaration for a font family. This family is used where none of the
# other families apply.
NIL = 'nil'.intern
# Attribute accessor.
attr_reader :family, :name
# This is the constructor for the Font class.
#
# ==== Parameters
# family:: The font family for the new font. This should be one of
# Font::MODERN, Font::ROMAN, Font::SWISS or
# Font::NIL.
# name:: A string containing the font name.
#
# ==== Exceptions
# RTFError:: Generated whenever an invalid font family is specified.
def initialize(family, name)
# Check that a valid family has been provided.
if ![MODERN, ROMAN, SWISS, NIL].include?(family)
RTFError::fire("Unknown font family specified for Font object.")
end
@family = family
@name = name
end
# This method overloads the equivalence test operator for the Font class
# to allow for Font comparisons.
#
# ==== Parameters
# object:: A reference to the object to be compared with.
def ==(object)
object.instance_of?(Font) &&
object.family == @family &&
object.name == @name
end
# This method fetches a textual description for a Font object.
#
# ==== Parameters
# indent:: The number of spaces to prefix to the lines generated by the
# method. Defaults to zero.
def to_s(indent=0)
prefix = indent > 0 ? ' ' * indent : ''
"#{prefix}Family: #{@family.id2name}, Name: #{@name}"
end
# This method generates the RTF representation for a Font object as it
# would appear within a document font table.
#
# ==== Parameters
# indent:: The number of spaces to prefix to the lines generated by the
# method. Defaults to zero.
def to_rtf(indent=0)
prefix = indent > 0 ? ' ' * indent : ''
"#{prefix}\\f#{@family.id2name} #{@name};"
end
end # End of the Font class.
# 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).
class 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.
def initialize(*fonts)
@fonts = []
fonts.each {|font| add(font)}
end
# This method is used to retrieve a count of the number of fonts held
# within an instance of the FontTable class.
def size
@fonts.size
end
# 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.
def add(font)
if font.instance_of?(Font)
@fonts.push(font) if @fonts.index(font).nil?
end
self
end
# 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).
def each
@fonts.each {|font| yield font} if block_given?
end
# 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.
def [](index)
@fonts[index]
end
# 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.
def index(font)
@fonts.index(font)
end
# 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.
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
# 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.
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
alias << add
end # End of the FontTable class.
end # End of the RTF module.