class HexaPDF::Document::Fonts
HexaPDF::Document#fonts method.
This class provides utility functions for working with fonts. It is available through the
def add(name, **options)
configuration option 'font_loaders').
Adds the font to the document and returns it (using the font loaders specified with the
fonts.add(name, **options) -> font
:call-seq:
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
def configured_fonts
Returns a hash of the form 'font_name => [variants, ...]' with all the fonts that are
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
def each_font_loader
fonts.each_font_loader {|loader| block}
:call-seq:
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
def initialize(document)
def initialize(document) @document = document @loaded_fonts_cache = {} end