Class: Prawn::Font

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

Overview

Provides font information and helper functions.

Experimental API collapse

AFM =
Deprecated.
Fonts::AFM
TTF =
Fonts::TTF
DFont =
Fonts::DFont
TTC =
Fonts::TTC

Experimental API collapse

Experimental API collapse

Constructor Details

#initialize(document, name, options = {}) ⇒ Font

:nodoc:



331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/prawn/font.rb', line 331

def initialize(document, name, options = {}) # :nodoc:
  @document = document
  @name = name
  @options = options

  @family = options[:family]

  @identifier = generate_unique_id

  @references = {}
  @subset_name_cache = {}
end

Instance Attribute Details

#familyObject (readonly)

The current font family



300
301
302
# File 'lib/prawn/font.rb', line 300

def family
  @family
end

#nameObject (readonly)

The current font name



297
298
299
# File 'lib/prawn/font.rb', line 297

def name
  @name
end

#optionsObject (readonly)

The options hash used to initialize the font



303
304
305
# File 'lib/prawn/font.rb', line 303

def options
  @options
end

Class Method Details

.font_format(src, options) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
# File 'lib/prawn/font.rb', line 319

def self.font_format(src, options)
  return options.fetch(:format, 'ttf') if src.respond_to? :read

  case src.to_s
  when /\.ttf$/i then 'ttf'
  when /\.otf$/i then 'otf'
  when /\.dfont$/i then 'dfont'
  when /\.ttc$/i then 'ttc'
  else 'afm'
  end
end

.load(document, src, options = {}) ⇒ Object

Shortcut interface for constructing a font object. Filenames of the form *.ttf will call Fonts::TTF.new, *.dfont Fonts::DFont.new, *.ttc goes to Fonts::TTC.new, and anything else will be passed through to Fonts::AFM.new()



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

def self.load(document, src, options = {})
  case font_format(src, options)
  when 'ttf' then TTF.new(document, src, options)
  when 'otf' then Fonts::OTF.new(document, src, options)
  when 'dfont' then DFont.new(document, src, options)
  when 'ttc' then TTC.new(document, src, options)
  else AFM.new(document, src, options)
  end
end

Instance Method Details

#add_to_current_page(subset) ⇒ Object

Registers the given subset of the current font with the current PDF page. This is safe to call multiple times for a given font and subset, as it will only add the font the first time it is called.



398
399
400
401
# File 'lib/prawn/font.rb', line 398

def add_to_current_page(subset)
  @references[subset] ||= register(subset)
  @document.state.page.fonts[identifier_for(subset)] = @references[subset]
end

#ascenderObject

The size of the font ascender in PDF points



346
347
348
# File 'lib/prawn/font.rb', line 346

def ascender
  @ascender / 1000.0 * size
end

#descenderObject

The size of the font descender in PDF points



352
353
354
# File 'lib/prawn/font.rb', line 352

def descender
  -@descender / 1000.0 * size
end

#eql?(other) ⇒ Boolean

Compliments the #hash implementation above

Returns:

  • (Boolean)


422
423
424
425
# File 'lib/prawn/font.rb', line 422

def eql?(other) # :nodoc:
  self.class == other.class && name == other.name &&
    family == other.family && size == other.size
end

#hashObject

Return a hash (as in Object#hash) for the font based on the output of #inspect. This is required since font objects are used as keys in hashes that cache certain values (See Prawn::Table::Text#styled_with_of_single_character)



416
417
418
# File 'lib/prawn/font.rb', line 416

def hash # :nodoc:
  [self.class, name, family].hash
end

#heightObject

Gets height of current font in PDF points at current font size



390
391
392
# File 'lib/prawn/font.rb', line 390

def height
  height_at(size)
end

#height_at(size) ⇒ Object

Gets height of current font in PDF points at the given font size



383
384
385
386
# File 'lib/prawn/font.rb', line 383

def height_at(size)
  @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0
  @normalized_height * size
end

#identifier_for(subset) ⇒ Object

:nodoc:



403
404
405
# File 'lib/prawn/font.rb', line 403

def identifier_for(subset) # :nodoc:
  @subset_name_cache[subset] ||= "#{@identifier}.#{subset}".to_sym
end

#inspectObject

:nodoc:



407
408
409
# File 'lib/prawn/font.rb', line 407

def inspect # :nodoc:
  "#{self.class.name}< #{name}: #{size} >"
end

#line_gapObject

The size of the recommended gap between lines of text in PDF points



358
359
360
# File 'lib/prawn/font.rb', line 358

def line_gap
  @line_gap / 1000.0 * size
end

#normalize_encoding(_string) ⇒ Object

Normalizes the encoding of the string to an encoding supported by the font. The string is expected to be UTF-8 going in. It will be re-encoded and the new string will be returned. For an in-place (destructive) version, see normalize_encoding!.

Raises:

  • (NotImplementedError)


366
367
368
369
# File 'lib/prawn/font.rb', line 366

def normalize_encoding(_string)
  raise NotImplementedError,
    'subclasses of Prawn::Font must implement #normalize_encoding'
end

#normalize_encoding!(str) ⇒ Object

Deprecated.

Destructive version of normalize_encoding; normalizes the encoding of a string in place.



375
376
377
378
379
# File 'lib/prawn/font.rb', line 375

def normalize_encoding!(str)
  warn 'Font#normalize_encoding! is deprecated. ' \
    'Please use non-mutating version Font#normalize_encoding instead.'
  str.dup.replace(normalize_encoding(str))
end