class TaskJuggler::Painter::FontMetricsData

The FontMetricsData objects generate and store the font metrics data for a particular font. The glyph set is currently restricted to US ASCII characters.

Constants

MAX_GLYPH_INDEX
MIN_GLYPH_INDEX

Attributes

charWidth[R]
height[R]
kerningDelta[R]
ptSize[R]

Public Class Methods

new(fontName, type = :normal, ptSize = 24, height = nil, wData = nil, kData = nil) click to toggle source

The constructor can be used in two different modes. If all font data is supplied, the object just stores the supplied font data. If only the font name is given, the class uses the prawn library to generate the font metrics for the requested font.

# File lib/taskjuggler/Painter/FontMetricsData.rb, line 32
def initialize(fontName, type = :normal, ptSize = 24, height = nil,
               wData = nil, kData = nil)
  @fontName = fontName
  @type = type
  @height = height
  @ptSize = ptSize
  @averageWidth = 0.0

  if wData && kData
    @charWidth = wData
    @kerningDelta = kData
  else
    generateMetrics
  end
end

Public Instance Methods

averageWidth() click to toggle source

The average with of all glyphs.

# File lib/taskjuggler/Painter/FontMetricsData.rb, line 55
def averageWidth
  return (@averageWidth * (3.0 / 4.0)).to_i
end
glyphWidth(c) click to toggle source

Return the width of the glyph c. This must be a single character String. If the glyph is not known, nil is returned.

# File lib/taskjuggler/Painter/FontMetricsData.rb, line 50
def glyphWidth(c)
  return @charWidth[c]
end
to_ruby() click to toggle source

Generate the FontMetricsData initialization code for the particular font. The output will be Ruby syntax.

# File lib/taskjuggler/Painter/FontMetricsData.rb, line 61
def to_ruby
  indent = ' ' * 6
  s = "#{indent}Font_#{@fontName.gsub(/-/, '_')}_#{@type} = " +
      "Painter::FontMetricsData.new('#{@fontName}', :#{@type}, " +
      "#{@ptSize}, #{"%.3f" % @height},\n"
  s << "#{indent}  @charWidth = {"
  i = 0
  @charWidth.each do |c, w|
    s << (i % 4 == 0 ? "\n#{indent}    " : ' ')
    i += 1
    s << "'#{escapedChars(c)}' => #{"%0.3f" % w},"
  end
  s << "\n#{indent}  },\n"

  s << "#{indent}  @kerningDelta = {"
  i = 0
  @kerningDelta.each do |cp, w|
    s << (i % 4 == 0 ? "\n#{indent}    " : ' ')
    i += 1
    s << "'#{cp}' => #{"%.3f" % w},"
  end
  s << "\n#{indent}  }\n#{indent})\n"
end