Class: Prawn::Font Abstract

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

Overview

This class is abstract.

Provides font information and helper functions.

Direct Known Subclasses

Prawn::Fonts::AFM, Prawn::Fonts::TTF

Experimental API collapse

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

Experimental API collapse

Experimental API collapse

Instance Attribute Details

#familyString (readonly)

The font family.

Returns:

  • (String)
Source Code
lib/prawn/font.rb, line 359
359
def family
360
  @family
361
end

#nameString (readonly)

The font name.

Returns:

  • (String)
Source Code
lib/prawn/font.rb, line 355
355
def name
356
  @name
357
end

#optionsHash (readonly)

The options hash used to initialize the font.

Returns:

  • (Hash)
Source Code
lib/prawn/font.rb, line 363
363
def options
364
  @options
365
end

Class Method Details

.load(document, src, options = {}) ⇒ Prawn::Fonts::Font

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

Parameters:

  • document (Prawn::Document)

    owning document

  • src (String)

    font file path

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :family (String)
  • :style (Symbol)

Returns:

  • (Prawn::Fonts::Font)
Source Code
lib/prawn/font.rb, line 377
377
def self.load(document, src, options = {})
378
  case font_format(src, options)
379
  when 'ttf' then TTF.new(document, src, options)
380
  when 'otf' then Fonts::OTF.new(document, src, options)
381
  when 'dfont' then DFont.new(document, src, options)
382
  when 'ttc' then TTC.new(document, src, options)
383
  else AFM.new(document, src, options)
384
  end
385
end

Instance Method Details

#add_to_current_page(subset) ⇒ void

This method returns an undefined value.

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.

Parameters:

  • subset (Integer)
Source Code
lib/prawn/font.rb, line 496
496
def add_to_current_page(subset)
497
  @references[subset] ||= register(subset)
498
  @document.state.page.fonts[identifier_for(subset)] = @references[subset]
499
end

#ascenderNumber

The size of the font ascender in PDF points.

Returns:

  • (Number)
Source Code
lib/prawn/font.rb, line 430
430
def ascender
431
  @ascender / 1000.0 * size
432
end

#descenderNumber

The size of the font descender in PDF points.

Returns:

  • (Number)
Source Code
lib/prawn/font.rb, line 437
437
def descender
438
  -@descender / 1000.0 * size
439
end

#eql?(other) ⇒ Boolean

Compliments the #hash implementation.

Parameters:

  • other (Object)

Returns:

  • (Boolean)
Source Code
lib/prawn/font.rb, line 532
532
def eql?(other)
533
  self.class == other.class && name == other.name &&
534
    family == other.family && size == other.size
535
end

#hashInteger

Return a hash (as in Object#hash) for the font. This is required since font objects are used as keys in hashes that cache certain values.

Returns:

  • (Integer)
Source Code
lib/prawn/font.rb, line 524
524
def hash
525
  [self.class, name, family].hash
526
end

#heightNumber

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

Returns:

  • (Number)
Source Code
lib/prawn/font.rb, line 486
486
def height
487
  height_at(size)
488
end

#height_at(size) ⇒ Number

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

Parameters:

  • size (Number)

Returns:

  • (Number)
Source Code
lib/prawn/font.rb, line 478
478
def height_at(size)
479
  @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0
480
  @normalized_height * size
481
end

#inspectString

Returns a string containing a human-readable representation of this font.

Returns:

  • (String)
Source Code
lib/prawn/font.rb, line 516
516
def inspect
517
  "#{self.class.name}< #{name}: #{size} >"
518
end

#line_gapNumber

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

Returns:

  • (Number)
Source Code
lib/prawn/font.rb, line 444
444
def line_gap
445
  @line_gap / 1000.0 * size
446
end

#normalize_encoding(string) ⇒ String

This method is abstract.

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.

Parameters:

  • string (String)

Returns:

  • (String)

Raises:

  • (NotImplementedError)
Source Code
lib/prawn/font.rb, line 456
456
def normalize_encoding(_string)
457
  raise NotImplementedError,
458
    'subclasses of Prawn::Font must implement #normalize_encoding'
459
end

#normalize_encoding!(str) ⇒ String

Deprecated.
Note:

This method doesn’t mutate its argument any more.

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

Parameters:

  • str (String)

Returns:

  • (String)
Source Code
lib/prawn/font.rb, line 469
469
def normalize_encoding!(str)
470
  warn('Font#normalize_encoding! is deprecated. Please use non-mutating version Font#normalize_encoding instead.')
471
  str.dup.replace(normalize_encoding(str))
472
end