class HexaPDF::Document::Fonts
This class provides utility functions for working with fonts. It is available through the HexaPDF::Document#fonts
method.
Public Class Methods
new(document)
click to toggle source
Creates a new Fonts
object for the given PDF document.
# File lib/hexapdf/document/fonts.rb, line 47 def initialize(document) @document = document @loaded_fonts_cache = {} end
Public Instance Methods
add(name, **options) → font
click to toggle source
Adds the font to the document and returns it (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.
# File lib/hexapdf/document/fonts.rb, line 59 def add(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 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 '#{options[:variant]}' " \ "couldn't be found. Configured fonts: #{font_list}" end end
configured_fonts()
click to toggle source
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.
# File lib/hexapdf/document/fonts.rb, line 82 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
Private Instance Methods
each_font_loader {|loader| block}
click to toggle source
Iterates over all configured font loaders.
# File lib/hexapdf/document/fonts.rb, line 103 def each_font_loader @document.config['font_loader'].each_index do |index| loader = @document.config.constantize('font_loader', index) do raise HexaPDF::Error, "Couldn't retrieve font loader ##{index} from configuration" end yield(loader) end end