Class: Skrift::FontSet

Inherits:
Object
  • Object
show all
Defined in:
lib/skrift/font_set.rb

Overview

An ordered set of fonts with per-codepoint fallback. Entries may be Font objects, or font file paths / fontconfig names that are loaded lazily and resolved via (in order) an explicit path, ~/.local/share/fonts, then ‘fc-match`. A single Font is just a one-element set.

Instance Method Summary collapse

Constructor Details

#initialize(fonts) ⇒ FontSet

Returns a new instance of FontSet.



9
10
11
12
# File 'lib/skrift/font_set.rb', line 9

def initialize(fonts)
  @entries = Array(fonts)
  @loaded  = {}
end

Instance Method Details

#[](index) ⇒ Object

The i-th font, lazily loaded/resolved. Returns nil if unresolvable.



17
18
19
20
# File 'lib/skrift/font_set.rb', line 17

def [](index)
  return @loaded[index] if @loaded.key?(index)
  @loaded[index] = resolve(@entries[index])
end

#eachObject

Yields each resolvable font in order.



23
24
25
26
# File 'lib/skrift/font_set.rb', line 23

def each
  return enum_for(:each) unless block_given?
  @entries.each_index { |i| (f = self[i]) && yield(f) }
end

#lookup(codepoint) ⇒ Object

The first [font, glyph_id] whose font maps codepoint, or nil if no font in the set has a cmap entry for it. (A font that maps the codepoint to the missing glyph, gid 0, still counts as a match — matching the prior behaviour; skipping .notdef to keep searching is a deliberate non-goal here.)



33
34
35
36
37
38
39
40
41
# File 'lib/skrift/font_set.rb', line 33

def lookup(codepoint)
  @entries.each_index do |i|
    font = self[i]
    next unless font
    gid = font.glyph_id(codepoint)
    return [font, gid] unless gid.nil?
  end
  nil
end

#sizeObject



14
# File 'lib/skrift/font_set.rb', line 14

def size = @entries.length