Class: HexaPDF::Document::Fonts

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/document/fonts.rb

Overview

This class provides utility functions for working with fonts. It is available through the HexaPDF::Document#fonts method.

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Fonts

Creates a new Fonts object for the given PDF document.



45
46
47
48
# File 'lib/hexapdf/document/fonts.rb', line 45

def initialize(document)
  @document = document
  @loaded_fonts_cache = {}
end

Instance Method Details

#load(name, **options) ⇒ Object

:call-seq:

fonts.load(name, **options)            -> font

Loads and returns the font (using the loaders specified with the configuration option ‘font_loaders’).

If a font with the same parameters has been loaded before, the cached font object is used.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hexapdf/document/fonts.rb', line 57

def load(name, **options)
  options[:variant] ||= :none # assign default value for consistency with caching
  font = @loaded_fonts_cache[[name, options]]
  return font if font

  each_font_loader do |loader|
    font = loader.call(@document, name, **options)
    break if font
  end

  if font
    @loaded_fonts_cache[[name, options]] = font
  else
    raise HexaPDF::Error, "The requested font '#{name}' in variant '#{options[:variant]}' " \
      "couldn't be found"
  end
end