Class: Prawn::Font::Metrics::TTF
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
[], data, #font_height, #string_height
Methods included from Wrapping
#naive_wrap
Constructor Details
#initialize(font) ⇒ TTF
Returns a new instance of TTF.
221
222
223
224
225
226
227
228
|
# File 'lib/prawn/font/metrics.rb', line 221
def initialize(font)
@ttf = TTFunk::File.new(font)
@attributes = {}
@glyph_widths = {}
@bounding_boxes = {}
@char_widths = {}
@has_kerning_data = !! @ttf.kern? && @ttf.kern.sub_tables[0]
end
|
Instance Attribute Details
#ttf ⇒ Object
Returns the value of attribute ttf.
219
220
221
|
# File 'lib/prawn/font/metrics.rb', line 219
def ttf
@ttf
end
|
Instance Method Details
#ascender ⇒ Object
308
309
310
|
# File 'lib/prawn/font/metrics.rb', line 308
def ascender
Integer(@ttf.hhea.ascent * scale_factor)
end
|
#basename ⇒ Object
320
321
322
|
# File 'lib/prawn/font/metrics.rb', line 320
def basename
@basename ||= @ttf.name.postscript_name
end
|
#bbox ⇒ Object
302
303
304
305
306
|
# File 'lib/prawn/font/metrics.rb', line 302
def bbox
[:x_min, :y_min, :x_max, :y_max].map do |atr|
Integer(@ttf.head.send(atr)) * scale_factor
end
end
|
#cmap ⇒ Object
230
231
232
|
# File 'lib/prawn/font/metrics.rb', line 230
def cmap
@cmap ||= @ttf.cmap.formats[4]
end
|
#convert_text(text, options) ⇒ Object
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/prawn/font/metrics.rb', line 349
def convert_text(text,options)
text = text.chomp
if options[:kerning]
kern(text)
else
unicode_codepoints = text.unpack("U*")
glyph_codes = unicode_codepoints.map { |u| cmap[u] }
text = glyph_codes.pack("n*")
end
end
|
#descender ⇒ Object
312
313
314
|
# File 'lib/prawn/font/metrics.rb', line 312
def descender
Integer(@ttf.hhea.descent * scale_factor)
end
|
#glyph_widths ⇒ Object
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/prawn/font/metrics.rb', line 286
def glyph_widths
glyphs = cmap.values.uniq.sort
first_glyph = glyphs.shift
widths = [first_glyph, [Integer(hmtx[first_glyph][0] * scale_factor)]]
prev_glyph = first_glyph
glyphs.each do |glyph|
unless glyph == prev_glyph + 1
widths << glyph
widths << []
end
widths.last << Integer(hmtx[glyph][0] * scale_factor )
prev_glyph = glyph
end
widths
end
|
#has_kerning_data? ⇒ Boolean
341
342
343
|
# File 'lib/prawn/font/metrics.rb', line 341
def has_kerning_data?
@has_kerning_data
end
|
#kern(string, options = {}) ⇒ Object
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
284
|
# File 'lib/prawn/font/metrics.rb', line 252
def kern(string,options={})
a = []
string.unpack("U*").each do |r|
if a.last.is_a? Array
if kern = kern_pairs_table[[cmap[a.last.last], cmap[r]]]
kern *= scale_factor
a << kern << [r]
else
a.last << r
end
else
a << [r]
end
a
end
a.map { |r|
if options[:skip_conversion]
r.is_a?(Array) ? r.pack("U*") : r
else
i = r.is_a?(Array) ? r.pack("U*") : r
x = if i.is_a?(String)
unicode_codepoints = i.unpack("U*")
glyph_codes = unicode_codepoints.map { |u| cmap[u] }
glyph_codes.pack("n*")
else
i
end
x.is_a?(Numeric) ? -x : x
end
}
end
|
#kern_pairs_table ⇒ Object
337
338
339
|
# File 'lib/prawn/font/metrics.rb', line 337
def kern_pairs_table
@kerning_data ||= has_kerning_data? ? @ttf.kern.sub_tables[0] : {}
end
|
#line_gap ⇒ Object
316
317
318
|
# File 'lib/prawn/font/metrics.rb', line 316
def line_gap
Integer(@ttf.hhea.line_gap * scale_factor)
end
|
#string_width(string, font_size, options = {}) ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/prawn/font/metrics.rb', line 234
def string_width(string, font_size, options = {})
scale = font_size / 1000.0
if options[:kerning]
kern(string,:skip_conversion => true).inject(0) do |s,r|
if r.is_a? String
s + string_width(r, font_size, :kerning => false)
else
s + r * scale
end
end
else
string.unpack("U*").inject(0) do |s,r|
s + character_width_by_code(r)
end * scale
end
end
|
#to_unicode_cmap ⇒ Object
TODO: instead of creating a map that contains every glyph in the font,
only include the glyphs that were used
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/prawn/font/metrics.rb', line 326
def to_unicode_cmap
return @to_unicode if @to_unicode
@to_unicode = Prawn::Font::CMap.new
unicode_for_glyph = cmap.invert
glyphs = unicode_for_glyph.keys.uniq.sort
glyphs.each do |glyph|
@to_unicode[unicode_for_glyph[glyph]] = glyph
end
@to_unicode
end
|
#type0? ⇒ Boolean
345
346
347
|
# File 'lib/prawn/font/metrics.rb', line 345
def type0?
true
end
|