Class: TaskJuggler::Painter::FontMetricsData

Inherits:
Object
  • Object
show all
Defined in:
lib/taskjuggler/Painter/FontMetricsData.rb

Overview

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

Constant Summary collapse

MIN_GLYPH_INDEX =
32
MAX_GLYPH_INDEX =
126

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fontName, type = :normal, ptSize = 24, height = nil, wData = nil, kData = nil) ⇒ FontMetricsData

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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#charWidthObject (readonly)

Returns the value of attribute charWidth.



26
27
28
# File 'lib/taskjuggler/Painter/FontMetricsData.rb', line 26

def charWidth
  @charWidth
end

#heightObject (readonly)

Returns the value of attribute height.



26
27
28
# File 'lib/taskjuggler/Painter/FontMetricsData.rb', line 26

def height
  @height
end

#kerningDeltaObject (readonly)

Returns the value of attribute kerningDelta.



26
27
28
# File 'lib/taskjuggler/Painter/FontMetricsData.rb', line 26

def kerningDelta
  @kerningDelta
end

#ptSizeObject (readonly)

Returns the value of attribute ptSize.



26
27
28
# File 'lib/taskjuggler/Painter/FontMetricsData.rb', line 26

def ptSize
  @ptSize
end

Instance Method Details

#averageWidthObject

The average with of all glyphs.



55
56
57
# File 'lib/taskjuggler/Painter/FontMetricsData.rb', line 55

def averageWidth
  return (@averageWidth * (3.0 / 4.0)).to_i
end

#glyphWidth(c) ⇒ Object

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



50
51
52
# File 'lib/taskjuggler/Painter/FontMetricsData.rb', line 50

def glyphWidth(c)
  return @charWidth[c]
end

#to_rubyObject

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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