Sha256: 05f1c3c074f2a8fb81c1377e8a7ddb76693a1bf551e8294721b483413d690abf

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

class Prawn::SVG::FontRegistry
  DEFAULT_FONT_PATHS = [
    "/Library/Fonts",
    "/System/Library/Fonts",
    "#{ENV["HOME"]}/Library/Fonts",
    "/usr/share/fonts/truetype",
    "/mnt/c/Windows/Fonts", # Bash on Ubuntu on Windows
  ]

  @font_path = DEFAULT_FONT_PATHS.select { |path| Dir.exist?(path) }

  def initialize(font_families)
    @font_families = font_families
  end

  def installed_fonts
    merge_external_fonts
    @font_families
  end

  def correctly_cased_font_name(name)
    merge_external_fonts
    @font_case_mapping[name.downcase]
  end

  def load(family, weight = nil, style = nil)
    Prawn::SVG::CSS.parse_font_family_string(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

  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)

      @font_case_mapping = @font_families.keys.each.with_object({}) do |key, result|
        result[key.downcase] = key
      end
    end
  end

  class << self
    attr_reader :external_font_families, :font_path

    def load_external_fonts
      @external_font_families = {}

      external_font_paths.each do |filename|
        ttf = Prawn::SVG::TTF.new(filename)
        if 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
    end

    private

    def external_font_paths
      font_path
        .uniq
        .flat_map { |path| Dir["#{path}/**/*"] }
        .uniq
        .select { |path| File.file?(path) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
prawn-svg-0.27.1 lib/prawn/svg/font_registry.rb
prawn-svg-0.27.0 lib/prawn/svg/font_registry.rb
prawn-svg-0.26.0 lib/prawn/svg/font_registry.rb
prawn-svg-0.25.2 lib/prawn/svg/font_registry.rb