Class: GD2::Font::TrueType

Inherits:
Object
  • Object
show all
Defined in:
lib/gd2/font.rb

Defined Under Namespace

Classes: FontconfigError, FreeTypeError

Constant Summary collapse

CHARMAP_UNICODE =
0
CHARMAP_SHIFT_JIS =
1
CHARMAP_BIG5 =
2
FTEX_LINESPACE =
1
FTEX_CHARMAP =
2
FTEX_RESOLUTION =
4
FTEX_DISABLE_KERNING =
8
FTEX_XSHOW =
16
FTEX_FONTPATHNAME =
32
FTEX_FONTCONFIG =
64
FTEX_RETURNFONTPATHNAME =
128
@@fontcount =
0
@@fontconfig =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fontname, ptsize, options = {}) ⇒ TrueType

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.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/gd2/font.rb', line 211

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 = GD2FFI.send(:gdImageStringFTEx, *args)

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

Instance Attribute Details

#charmapObject (readonly)

The chosen charmap



173
174
175
# File 'lib/gd2/font.rb', line 173

def charmap
  @charmap
end

#fontpathObject (readonly)

The effective path to this TrueType font



167
168
169
# File 'lib/gd2/font.rb', line 167

def fontpath
  @fontpath
end

#hdpiObject (readonly)

The chosen horizontal resolution hint



176
177
178
# File 'lib/gd2/font.rb', line 176

def hdpi
  @hdpi
end

#kerningObject (readonly)

Whether kerning is desired



182
183
184
# File 'lib/gd2/font.rb', line 182

def kerning
  @kerning
end

#linespacingObject (readonly)

The chosen linespacing



170
171
172
# File 'lib/gd2/font.rb', line 170

def linespacing
  @linespacing
end

#vdpiObject (readonly)

The chosen vertical resolution hint



179
180
181
# File 'lib/gd2/font.rb', line 179

def vdpi
  @vdpi
end

Class Method Details

.fontconfigObject

:nodoc:



143
144
145
# File 'lib/gd2/font.rb', line 143

def self.fontconfig   #:nodoc:
  @@fontconfig
end

.fontconfig=(want) ⇒ Object

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.

Raises:



156
157
158
159
160
# File 'lib/gd2/font.rb', line 156

def self.fontconfig=(want)
  avail = !GD2FFI.send(:gdFTUseFontConfig, want ? 1 : 0).zero?
  raise FontconfigError, 'Fontconfig not available' if want && !avail
  @@fontconfig = want
end

.fontconfig?Boolean

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

Returns:

  • (Boolean)


149
150
151
# File 'lib/gd2/font.rb', line 149

def self.fontconfig?
  fontconfig
end

.register(font) ⇒ Object

:nodoc:



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gd2/font.rb', line 116

def self.register(font)   #:nodoc:
  Thread.exclusive do
    count = @@fontcount
    @@fontcount += 1

    if count.zero?
      raise FreeTypeError, 'FreeType library failed to initialize' unless
        GD2FFI.send(:gdFontCacheSetup).zero?
    end

    ObjectSpace.define_finalizer(font, font_finalizer)
  end
end

Instance Method Details

#bounding_rectangle(string, angle = 0.0) ⇒ Object

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.



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/gd2/font.rb', line 311

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

#draw(image_ptr, x, y, angle, string, fg) ⇒ Object

:nodoc:

Raises:



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/gd2/font.rb', line 249

def draw(image_ptr, x, y, angle, string, fg)  #:nodoc:
  brect = FFI::MemoryPointer.new(:int, 8)
  strex = strex(true)
  args = [ image_ptr, brect, fg, @fontname, @ptsize, angle.to_f, x.to_i, y.to_i, string.gsub('&', '&'), strex ]

  r = GD2FFI.send(:gdImageStringFTEx, *args)
  raise FreeTypeError.new(r.read_string) unless r.null?
  brect = brect.read_array_of_int(8)

  if !strex[:xshow].null? && (xshow = strex[:xshow])
    begin
      xshow = xshow.read_string.split(' ').map { |e| e.to_f }
    ensure
      GD2FFI.send(:gdFree, strex[:xshow])
    end
  else
    xshow = []
  end

  sum = 0.0
  position = Array.new(xshow.length + 1)
  xshow.each_with_index do |advance, i|
    position[i] = sum
    sum += advance
  end
  position[-1] = sum

  { :lower_left   => [brect[0], brect[1]],
    :lower_right  => [brect[2], brect[3]],
    :upper_right  => [brect[4], brect[5]],
    :upper_left   => [brect[6], brect[7]],
    :position     => position
  }
end

#draw_circle(image_ptr, cx, cy, radius, text_radius, fill_portion, top, bottom, fgcolor) ⇒ Object

Raises:



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/gd2/font.rb', line 284

def draw_circle(
  image_ptr, cx, cy, radius, text_radius, fill_portion,
  top, bottom, fgcolor
) #:nodoc:
  r = GD2FFI.send(
    :gdImageStringFTCircle, image_ptr, cx.to_i, cy.to_i,
    radius.to_f, text_radius.to_f, fill_portion.to_f, @fontname, @ptsize,
    top || '', bottom || '', fgcolor.to_i
  )
  raise FreeTypeError.new(r.read_string) unless r.null?
  nil
end

#inspectObject

:nodoc:



239
240
241
242
243
244
245
246
247
# File 'lib/gd2/font.rb', line 239

def inspect   #:nodoc:
  result  = "#<#{self.class} #{@fontpath.inspect}, #{@ptsize}"
  result += ", :linespacing => #{@linespacing}" if @linespacing
  result += ", :charmap => #{@charmap}" if @charmap
  result += ", :hdpi => #{@hdpi}" if @hdpi
  result += ", :vdpi => #{@vdpi}" if @vdpi
  result += ", :kerning => #{@kerning}" unless @kerning
  result += '>'
end