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.

Raises:



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

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)
  r, rs = SYM[:gdImageStringFTEx].call(nil,
    Array.new(8) { 0 }, 0, @fontname, @ptsize, 0.0, 0, 0, '', strex)
  raise FreeTypeError, r if r

  strex[:fontpath].free = SYM[:gdFree]
  @fontpath = strex[:fontpath].to_s
end

Instance Attribute Details

#charmapObject (readonly)

The chosen charmap



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

def charmap
  @charmap
end

#fontpathObject (readonly)

The effective path to this TrueType font



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

def fontpath
  @fontpath
end

#hdpiObject (readonly)

The chosen horizontal resolution hint



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

def hdpi
  @hdpi
end

#kerningObject (readonly)

Whether kerning is desired



186
187
188
# File 'lib/gd2/font.rb', line 186

def kerning
  @kerning
end

#linespacingObject (readonly)

The chosen linespacing



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

def linespacing
  @linespacing
end

#vdpiObject (readonly)

The chosen vertical resolution hint



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

def vdpi
  @vdpi
end

Class Method Details

.fontconfigObject

:nodoc:



147
148
149
# File 'lib/gd2/font.rb', line 147

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:



160
161
162
163
164
# File 'lib/gd2/font.rb', line 160

def self.fontconfig=(want)
  avail = !SYM[:gdFTUseFontConfig].call(want ? 1 : 0)[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)


153
154
155
# File 'lib/gd2/font.rb', line 153

def self.fontconfig?
  fontconfig
end

.register(font) ⇒ Object

:nodoc:



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

def self.register(font)   #:nodoc:
  Thread.critical = true

  count = @@fontcount
  @@fontcount += 1

  if count.zero?
    raise FreeTypeError, 'FreeType library failed to initialize' unless
      SYM[:gdFontCacheSetup].call[0].zero?
  end

  ObjectSpace.define_finalizer(font, font_finalizer)
ensure
  Thread.critical = false
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.



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/gd2/font.rb', line 308

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:



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
283
# File 'lib/gd2/font.rb', line 252

def draw(image_ptr, x, y, angle, string, fg)  #:nodoc:
  brect = Array.new(8) { 0 }
  strex = strex(true)
  r, rs = SYM[:gdImageStringFTEx].call(image_ptr,
    brect, fg, @fontname, @ptsize, angle.to_f, x, y,
    string.gsub('&', '&'), strex)
  raise FreeTypeError, r if r

  brect = rs[1].to_a('I', 8)

  if xshow = strex[:xshow]
    xshow.free = SYM[:gdFree]
    xshow = xshow.to_s.split(' ').map { |e| e.to_f }
  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:



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

def draw_circle(image_ptr, cx, cy, radius, text_radius, fill_portion,
    top, bottom, fgcolor)   #:nodoc:
  r, rs = SYM[:gdImageStringFTCircle].call(image_ptr, cx, cy,
    radius.to_f, text_radius.to_f, fill_portion.to_f, @fontname, @ptsize,
    top || '', bottom || '', fgcolor)
  raise FreeTypeError, r if r
  nil
end

#inspectObject

:nodoc:



242
243
244
245
246
247
248
249
250
# File 'lib/gd2/font.rb', line 242

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