class TaskJuggler::Painter::FontMetrics

Class to compute or store the raw data for glyph size and kerning infomation. Developers can use it to generate FontData.rb. This file contains pre-computed font metrics data for some selected fonts. This data can then be used to determine the width and height of a bounding box of a given String.

Developers can also use this file to generate FontData.rb using prawn as a back-end. We currently do not want to have prawn as a runtime dependency for TaskJuggler.

Constants

Font_LiberationSans_bold
Font_LiberationSans_bold_italic
Font_LiberationSans_italic
Font_LiberationSans_normal

Public Class Methods

new() click to toggle source

Initialize the FontMetrics object.

# File lib/taskjuggler/Painter/FontMetrics.rb, line 44
def initialize()
  @fonts = {}
  # We currently only support the LiberationSans font which is metric
  # compatible to Arial.
  @fonts['Arial'] = @fonts['LiberationSans'] =
    Font_LiberationSans_normal
  @fonts['Arial-Italic'] = @fonts['LiberationSans-Italic'] =
    Font_LiberationSans_italic
  @fonts['Arial-Bold'] = @fonts['LiberationSans-Bold'] =
    Font_LiberationSans_bold
  @fonts['Arial-BoldItalic'] = @fonts['LiberationSans-BoldItalic'] =
    Font_LiberationSans_bold_italic
end

Public Instance Methods

height(font, ptSize) click to toggle source

Return the height of the font with ptSize points in screen pixels.

# File lib/taskjuggler/Painter/FontMetrics.rb, line 59
def height(font, ptSize)
  checkFontName(font)
  # Calculate resulting height scaled to the font size and convert to
  # screen pixels instead of points.
  (@fonts[font].height * (ptSize.to_f / @fonts[font].ptSize) *
   (4.0 / 3.0)).to_i
end
width(font, ptSize, str) click to toggle source

Return the width of the string in screen pixels when using the font font with ptSize points.

# File lib/taskjuggler/Painter/FontMetrics.rb, line 69
def width(font, ptSize, str)
  checkFontName(font)
  w = 0
  lastC = nil
  str.each_char do |c|
    cw = @fonts[font].glyphWidth(c)
    w += cw || @font[font].averageWidth
    if lastC
      delta = @fonts[font].kerningDelta[lastC + c]
      w += delta if delta
    end
    lastC = c
  end
  # Calculate resulting width scaled to the font size and convert to
  # screen pixels instead of points.
  (w * (ptSize.to_f / @fonts[font].ptSize) * (4.0 / 3.0)).to_i
end