Module: Arrolio::FontMetrics::Registry

Defined in:
lib/arrolio/font_metrics/registry.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ttf_pathsObject (readonly)

Returns the value of attribute ttf_paths.



10
11
12
# File 'lib/arrolio/font_metrics/registry.rb', line 10

def ttf_paths
  @ttf_paths
end

Class Method Details

.[](font_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/arrolio/font_metrics/registry.rb', line 32

def [](font_name)
  key = font_name.to_s
  return @cache[key] if @cache.key?(key)

  if @ttf_paths.key?(key) && File.exist?(@ttf_paths[key])
    @cache[key] = TrueTypeMetrics.from_file(key, @ttf_paths[key])
    return @cache[key]
  end

  @cache[key] = AfmMetrics.for_name(key)
end

.known?(font_name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/arrolio/font_metrics/registry.rb', line 49

def known?(font_name)
  !self[font_name].nil?
end

.register_ttf(font_name, path) ⇒ Object



12
13
14
15
# File 'lib/arrolio/font_metrics/registry.rb', line 12

def register_ttf(font_name, path)
  @ttf_paths[font_name.to_s] = path
  @cache.delete(font_name.to_s)
end

.register_ttf_all(paths, strict: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/arrolio/font_metrics/registry.rb', line 17

def register_ttf_all(paths, strict: false)
  missing = []
  paths.each do |name, path|
    register_ttf(name, path)
    missing << [name, path] if strict && path && File.exist?(path) == false
  end
  return unless strict && missing.any?

  raise Arrolio::RenderError.new(
    'strict mode: missing required font files: ' +
      missing.map { |name, path| "#{name}=#{path}" }.join(', '),
    missing_fonts: missing
  )
end

.reset!Object



44
45
46
47
# File 'lib/arrolio/font_metrics/registry.rb', line 44

def reset!
  @cache.clear
  @ttf_paths.clear
end