Module: HexaPDF::FontLoader::VariantFromName

Defined in:
lib/hexapdf/font_loader/variant_from_name.rb

Overview

This module translates font names like ‘Helvetica bold’ into the arguments ‘Helvetica’ and :bold.

This eases the usage of font names where specifying a font variant is not straight-forward. The actual loading of the font is deferred to Document::Fonts#add.

Note that this should be the last entry in the list of font loaders to ensure correct operation.

Class Method Summary collapse

Class Method Details

.call(document, name, recursive_invocation: false, **options) ⇒ Object

Returns a font wrapper for the given font by splitting the font name into the font name part and variant selector part. If the the resulting font cannot be resolved, nil is returned.

A font name should have the form ‘Fontname selector’ where selector can be ‘bold’, ‘italic’ or ‘bold_italic’, for example ‘Helvetica bold’.

Note that a supplied :variant keyword argument is ignored!



60
61
62
63
64
65
66
67
# File 'lib/hexapdf/font_loader/variant_from_name.rb', line 60

def self.call(document, name, recursive_invocation: false, **options)
  return if recursive_invocation
  name, variant = name.split(/ (?=(?:bold|italic|bold_italic)\z)/, 2)
  return if variant.nil?

  options[:variant] = variant.to_sym
  document.fonts.add(name, **options, recursive_invocation: true) rescue nil
end