Class: Fontist::SystemFont

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/system_font.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font:, style: nil) ⇒ SystemFont

Returns a new instance of SystemFont.



5
6
7
8
# File 'lib/fontist/system_font.rb', line 5

def initialize(font:, style: nil)
  @font = font
  @style = style
end

Class Method Details

.expand_paths(paths) ⇒ Object



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

def self.expand_paths(paths)
  paths.map do |path|
    require "etc"
    passwd = Etc.getpwuid
    username = passwd ? passwd.name : Etc.getlogin

    username ? path.gsub("{username}", username) : path
  end
end

.find(font) ⇒ Object



47
48
49
# File 'lib/fontist/system_font.rb', line 47

def self.find(font)
  new(font: font).find
end

.find_styles(font, style) ⇒ Object



51
52
53
# File 'lib/fontist/system_font.rb', line 51

def self.find_styles(font, style)
  new(font: font, style: style).find_styles
end

.font_pathsObject



10
11
12
# File 'lib/fontist/system_font.rb', line 10

def self.font_paths
  system_font_paths + fontist_font_paths
end

.fontist_font_pathsObject



43
44
45
# File 'lib/fontist/system_font.rb', line 43

def self.fontist_font_paths
  Dir.glob(Fontist.fonts_path.join("**"))
end

.load_system_font_pathsObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/fontist/system_font.rb', line 18

def self.load_system_font_paths
  config_path = Fontist.system_file_path
  os = Fontist::Utils::System.user_os.to_s
  templates = YAML.load_file(config_path)["system"][os]["paths"]
  patterns = expand_paths(templates)

  Dir.glob(patterns)
  # File::FNM_CASEFOLD is officially ignored -- see https://ruby-doc.org/core-3.1.1/Dir.html#method-c-glob
  # "Case sensitivity depends on your system"
end

.reset_system_font_paths_cacheObject



29
30
31
# File 'lib/fontist/system_font.rb', line 29

def self.reset_system_font_paths_cache
  @system_font_paths = nil
end

.system_font_pathsObject



14
15
16
# File 'lib/fontist/system_font.rb', line 14

def self.system_font_paths
  @system_font_paths ||= load_system_font_paths
end

Instance Method Details

#findObject



55
56
57
58
59
60
# File 'lib/fontist/system_font.rb', line 55

def find
  styles = find_styles
  return unless styles

  styles.map { |x| x[:path] }
end

#find_stylesObject



62
63
64
# File 'lib/fontist/system_font.rb', line 62

def find_styles
  find_by_index
end