class Prawn::SVG::FontRegistry

Constants

DEFAULT_FONT_PATHS

Attributes

external_font_families[R]
font_path[R]

Public Class Methods

new(font_families) click to toggle source
# File lib/prawn/svg/font_registry.rb, line 12
def initialize(font_families)
  @font_families = font_families
end

Private Class Methods

external_font_paths() click to toggle source
# File lib/prawn/svg/font_registry.rb, line 67
def external_font_paths
  font_path
    .uniq
    .flat_map { |path| Dir["#{path}/**/*"] }
    .uniq
    .select { |path| File.file?(path) }
end
load_external_fonts() click to toggle source
# File lib/prawn/svg/font_registry.rb, line 52
def load_external_fonts
  @external_font_families = {}

  external_font_paths.each do |filename|
    ttf = Prawn::SVG::TTF.new(filename)
    next unless ttf.family

    subfamily = (ttf.subfamily || 'normal').gsub(/\s+/, '_').downcase.to_sym
    subfamily = :normal if subfamily == :regular
    (external_font_families[ttf.family] ||= {})[subfamily] ||= filename
  end
end

Public Instance Methods

correctly_cased_font_name(name) click to toggle source
# File lib/prawn/svg/font_registry.rb, line 21
def correctly_cased_font_name(name)
  merge_external_fonts
  @font_case_mapping[name.downcase]
end
installed_fonts() click to toggle source
# File lib/prawn/svg/font_registry.rb, line 16
def installed_fonts
  merge_external_fonts
  @font_families
end
load(family, weight = nil, style = nil) click to toggle source
# File lib/prawn/svg/font_registry.rb, line 26
def load(family, weight = nil, style = nil)
  Prawn::SVG::CSS::FontFamilyParser.parse(family).detect do |name|
    name = name.gsub(/\s{2,}/, ' ').downcase

    font = Prawn::SVG::Font.new(name, weight, style, font_registry: self)
    break font if font.installed?
  end
end

Private Instance Methods

merge_external_fonts() click to toggle source
# File lib/prawn/svg/font_registry.rb, line 37
def merge_external_fonts
  if @font_case_mapping.nil?
    self.class.load_external_fonts unless self.class.external_font_families
    @font_families.merge!(self.class.external_font_families) do |_key, v1, _v2|
      v1
    end
    @font_case_mapping = @font_families.keys.each.with_object({}) do |key, result|
      result[key.downcase] = key
    end
  end
end