Class: HexaPDF::Document::Fonts
- Inherits:
-
Object
- Object
- HexaPDF::Document::Fonts
- 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
-
#add(name, **options) ⇒ Object
:call-seq: fonts.add(name, **options) -> font.
-
#configured_fonts ⇒ Object
Returns a hash of the form ‘font_name => [variants, …]’ with all the fonts that are configured.
-
#initialize(document) ⇒ Fonts
constructor
Creates a new Fonts object for the given PDF document.
Constructor Details
#initialize(document) ⇒ Fonts
Creates a new Fonts object for the given PDF document.
48 49 50 51 |
# File 'lib/hexapdf/document/fonts.rb', line 48 def initialize(document) @document = document @loaded_fonts_cache = {} end |
Instance Method Details
#add(name, **options) ⇒ Object
:call-seq:
fonts.add(name, **options) -> font
Adds the font to the document and returns it (using the font 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hexapdf/document/fonts.rb', line 60 def add(name, **) [:variant] ||= :none # assign default value for consistency with caching font = @loaded_fonts_cache[[name, ]] return font if font each_font_loader do |loader| font = loader.call(@document, name, **) break if font end if font @loaded_fonts_cache[[name, ]] = font else font_list = configured_fonts.sort.map do |font_name, variants| "#{font_name} (#{variants.join(', ')})" end.join(', ') raise HexaPDF::Error, "The requested font '#{name}' in variant '#{[:variant]}' " \ "couldn't be found. Configured fonts: #{font_list}" end end |
#configured_fonts ⇒ Object
Returns a hash of the form ‘font_name => [variants, …]’ with all the fonts that are configured. These fonts can be added to the document by using the #add method.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hexapdf/document/fonts.rb', line 83 def configured_fonts result = {} each_font_loader do |loader| next unless loader.respond_to?(:available_fonts) loader.available_fonts(@document).each do |name, variants| if result.key?(name) result[name].concat(variants).uniq! else result[name] = variants end end end result end |