Class: Prawn::Icon::FontData

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/icon/font_data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, opts = {}) ⇒ FontData

Returns a new instance of FontData.



47
48
49
50
# File 'lib/prawn/icon/font_data.rb', line 47

def initialize(document, opts = {})
  @set = opts.fetch(:set)
  load_fonts(document)
end

Instance Attribute Details

#setObject (readonly)

Returns the value of attribute set.



45
46
47
# File 'lib/prawn/icon/font_data.rb', line 45

def set
  @set
end

Class Method Details

.load(document, set) ⇒ Object

Font data lazy-loader that will initialize icon fonts by document.



17
18
19
20
21
22
# File 'lib/prawn/icon/font_data.rb', line 17

def load(document, set)
  set = set.to_sym
  @data ||= {}
  @data[set] ||= FontData.new(document, set: set)
  @data[set].load_fonts(document)
end

.release_dataObject

Release all font references if requested.



25
26
27
# File 'lib/prawn/icon/font_data.rb', line 25

def release_data
  @data = {}
end

.specifier_from_key(key) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/prawn/icon/font_data.rb', line 35

def specifier_from_key(key)
  if key.nil? || key == ''
    raise Errors::IconKeyEmpty,
          'Icon key provided was nil.'
  end

  key.split('-')[0].to_sym
end

.unicode_from_key(document, key) ⇒ Object



29
30
31
32
33
# File 'lib/prawn/icon/font_data.rb', line 29

def unicode_from_key(document, key)
  set = specifier_from_key(key)
  key = key.sub(Regexp.new("#{set}-"), '')
  load(document, set).unicode(key)
end

Instance Method Details

#font_versionObject



52
53
54
# File 'lib/prawn/icon/font_data.rb', line 52

def font_version
  yaml[specifier]['__font_version__']
end

#keysObject



94
95
96
97
# File 'lib/prawn/icon/font_data.rb', line 94

def keys
  # Strip the first element: __font_version__
  yaml[specifier].keys.map { |k| "#{specifier}-#{k}" }.drop(1)
end

#legend_pathObject



56
57
58
# File 'lib/prawn/icon/font_data.rb', line 56

def legend_path
  File.join(File.dirname(path), "#{@set}.yml")
end

#load_fonts(document) ⇒ Object



60
61
62
63
# File 'lib/prawn/icon/font_data.rb', line 60

def load_fonts(document)
  document.font_families[@set.to_s] ||= { normal: path }
  self
end

#pathObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prawn/icon/font_data.rb', line 65

def path
  @path = begin
    font = Icon.configuration.font_directory
               .join(@set.to_s)
               .glob('*.ttf')
               .first

    if font.nil?
      raise Prawn::Errors::UnknownFont,
            "Icon font not found for set: #{@set}"
    end

    font.to_s
  end
end

#specifierObject



81
82
83
# File 'lib/prawn/icon/font_data.rb', line 81

def specifier
  @specifier ||= yaml.keys[0]
end

#unicode(key) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/prawn/icon/font_data.rb', line 85

def unicode(key)
  yaml[specifier][key].tap do |char|
    unless char
      raise Errors::IconNotFound,
            "Key: #{specifier}-#{key} not found"
    end
  end
end

#yamlObject



99
100
101
# File 'lib/prawn/icon/font_data.rb', line 99

def yaml
  @yaml ||= YAML.load_file legend_path
end