Class: Prawn::SVG::FontRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/font_registry.rb

Constant Summary collapse

DEFAULT_FONT_PATHS =
[
  '/Library/Fonts',
  '/System/Library/Fonts',
  "#{Dir.home}/Library/Fonts",
  '/usr/share/fonts/truetype',
  '/mnt/c/Windows/Fonts' # Bash on Ubuntu on Windows
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font_families) ⇒ FontRegistry

Returns a new instance of FontRegistry.



12
13
14
# File 'lib/prawn/svg/font_registry.rb', line 12

def initialize(font_families)
  @font_families = font_families
end

Class Attribute Details

.external_font_familiesObject (readonly)

Returns the value of attribute external_font_families.



50
51
52
# File 'lib/prawn/svg/font_registry.rb', line 50

def external_font_families
  @external_font_families
end

.font_pathObject (readonly)

Returns the value of attribute font_path.



50
51
52
# File 'lib/prawn/svg/font_registry.rb', line 50

def font_path
  @font_path
end

Class Method Details

.load_external_fontsObject



52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Method Details

#correctly_cased_font_name(name) ⇒ Object



21
22
23
24
# 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_fontsObject



16
17
18
19
# File 'lib/prawn/svg/font_registry.rb', line 16

def installed_fonts
  merge_external_fonts
  @font_families
end

#load(family, weight = nil, style = nil) ⇒ Object



26
27
28
29
30
31
32
33
# 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