class GD2::Font::TrueType

Constants

CHARMAP_BIG5
CHARMAP_SHIFT_JIS
CHARMAP_UNICODE
FTEX_CHARMAP
FTEX_DISABLE_KERNING
FTEX_FONTCONFIG
FTEX_FONTPATHNAME
FTEX_LINESPACE
FTEX_RESOLUTION
FTEX_RETURNFONTPATHNAME
FTEX_XSHOW

Attributes

charmap[R]

The chosen charmap

fontpath[R]

The effective path to this TrueType font

hdpi[R]

The chosen horizontal resolution hint

kerning[R]

Whether kerning is desired

linespacing[R]

The chosen linespacing

vdpi[R]

The chosen vertical resolution hint

Public Class Methods

fontconfig=(want) click to toggle source

Set whether fontconfig support should be enabled. To use this, the GD library must have been built with fontconfig support. Raises an error if fontconfig support is unavailable.

# File lib/gd2/font.rb, line 140
def self.fontconfig=(want)
  avail = !::GD2::GD2FFI.send(:gdFTUseFontConfig, want ? 1 : 0).zero?
  raise FontconfigError, 'Fontconfig not available' if want && !avail
  Thread.current[:fontconfig] = !!want
end
fontconfig?() click to toggle source

Return a boolean indicating whether fontconfig support has been enabled. The default is false.

# File lib/gd2/font.rb, line 133
def self.fontconfig?
  fontconfig
end
new(fontname, ptsize, options = {}) click to toggle source

Instantiate a TrueType font given by fontname (either a pathname or a fontconfig pattern if fontconfig is enabled) and ptsize (a point size given as a floating point number).

The possible options are:

  • :linespacing => The desired line spacing for multiline text, expressed as a multiple of the font height. A line spacing of 1.0 is the minimum to guarantee that lines of text do not collide. The default according to GD is 1.05.

  • :charmap => Specify a preference for Unicode, Shift_JIS, or Big5 character encoding. Use one of the constants Font::TrueType::CHARMAP_UNICODE, Font::TrueType::CHARMAP_SHIFT_JIS, or Font::TrueType::CHARMAP_BIG5.

  • :hdpi => The horizontal resolution hint for the rendering engine. The default according to GD is 96 dpi.

  • :vdpi => The vertical resolution hint for the rendering engine. The default according to GD is 96 dpi.

  • :dpi => A shortcut to specify both :hdpi and :vdpi.

  • :kerning => A boolean to specify whether kerning tables should be used, if fontconfig is available. The default is true.

# File lib/gd2/font.rb, line 195
def initialize(fontname, ptsize, options = {})
  @fontname, @ptsize = fontname, ptsize.to_f
  @linespacing = options.delete(:linespacing)
  @linespacing = @linespacing.to_f if @linespacing
  @charmap = options.delete(:charmap)
  @hdpi = options.delete(:hdpi)
  @vdpi = options.delete(:vdpi)
  if dpi = options.delete(:dpi)
    @hdpi ||= dpi
    @vdpi ||= dpi
  end
  @kerning = options.delete(:kerning)
  @kerning = true if @kerning.nil?

  self.class.register(self)

  # Get the font path (and verify existence of file)

  strex = strex(false, true)
  args = [ nil, nil, 0, @fontname, @ptsize, 0.0, 0, 0, '', strex ]
  r = ::GD2::GD2FFI.send(:gdImageStringFTEx, *args)

  raise FreeTypeError.new(r.read_string) unless r.null?
  @fontpath = strex[:fontpath].read_string
ensure
  ::GD2::GD2FFI.send(:gdFree, strex[:fontpath])
end

Private Class Methods

font_finalizer() click to toggle source
# File lib/gd2/font.rb, line 107
def self.font_finalizer
  proc { unregister }
end
unregister() click to toggle source
# File lib/gd2/font.rb, line 111
def self.unregister
  if Thread.current[:fontcount]
    Thread.current[:fontcount] -= 1
  else
    Thread.current[:fontcount] = 0
  end

  ::GD2::GD2FFI.send(:gdFontCacheShutdown) if Thread.current[:fontcount].zero?
end

Public Instance Methods

bounding_rectangle(string, angle = 0.0) click to toggle source

Return a hash describing the rectangle that would enclose the given string rendered in this font at the given angle. The returned hash contains the following keys:

  • :lower_left => The [x, y] coordinates of the lower left corner.

  • :lower_right => The [x, y] coordinates of the lower right corner.

  • :upper_right => The [x, y] coordinates of the upper right corner.

  • :upper_left => The [x, y] coordinates of the upper left corner.

  • :position => An array of floating point character position offsets for each character of the string, beginning with 0.0. The array also includes a final position indicating where the last character ends.

The upper, lower, left, and right references are relative to the text of the string, regardless of the angle.

# File lib/gd2/font.rb, line 295
def bounding_rectangle(string, angle = 0.0)
  data = draw(nil, 0, 0, angle, string, 0)

  if string.length == 1
    # gd annoyingly fails to provide xshow data for strings of length 1
    position = draw(nil, 0, 0, angle, string + ' ', 0)[:position]
    data[:position] = position[0...-1]
  end

  data
end

Private Instance Methods

strex(xshow = false, returnfontpathname = false) click to toggle source
# File lib/gd2/font.rb, line 309
def strex(xshow = false, returnfontpathname = false)
  flags = 0
  flags |= FTEX_LINESPACE           if @linespacing
  flags |= FTEX_CHARMAP             if @charmap
  flags |= FTEX_RESOLUTION          if @hdpi || @vdpi
  flags |= FTEX_DISABLE_KERNING     unless @kerning
  flags |= FTEX_XSHOW               if xshow
  flags |= FTEX_RETURNFONTPATHNAME  if returnfontpathname

  strex = FFIStruct::FTStringExtraPtr.new
  strex[:flags] = flags
  strex[:linespacing] = @linespacing || 0.0
  strex[:charmap] = @charmap ? @charmap : 0
  strex[:hdpi] = @hdpi || @vdpi || 0
  strex[:vdpi] = @vdpi || @hdpi || 0
  strex
end