Class: Fontist::SystemFont

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

Overview

TODO: This is actually a SystemIndex font entry

Class Method Summary collapse

Class Method Details

.disable_find_styles_cacheObject

Disable caching mode and clear the cache



67
68
69
70
# File 'lib/fontist/system_font.rb', line 67

def self.disable_find_styles_cache
  @find_styles_cache = nil
  @find_styles_cache_enabled = false
end

.enable_find_styles_cacheObject

Enable caching mode for find_styles lookups This caches results during a single operation (e.g., manifest compilation) to avoid repeated index lookups



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

def self.enable_find_styles_cache
  @find_styles_cache ||= {}
  @find_styles_cache_enabled = true
end

.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



77
78
79
80
81
82
# File 'lib/fontist/system_font.rb', line 77

def self.find(font)
  styles = find_styles(font)
  return unless styles

  styles.map(&:path)
end

.find_styles(font, style = nil) ⇒ Object

This returns a SystemIndexEntry



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fontist/system_font.rb', line 85

def self.find_styles(font, style = nil)
  # Check cache first if enabled
  if @find_styles_cache_enabled
    cache_key = "#{font}:#{style}"
    cached = @find_styles_cache[cache_key]
    return cached.dup if cached
  end

  # Search across all three indexes
  results = []

  # Search fontist-managed fonts
  fontist_fonts = Fontist::Indexes::FontistIndex.instance.find(font, style)
  results.concat(fontist_fonts) if fontist_fonts

  # Search user location fonts
  user_fonts = Fontist::Indexes::UserIndex.instance.find(font, style)
  results.concat(user_fonts) if user_fonts

  # Search system fonts
  system_fonts = Fontist::Indexes::SystemIndex.instance.find(font, style)
  results.concat(system_fonts) if system_fonts

  # Remove duplicates by path and return
  return nil if results.empty?

  results = results.uniq(&:path)

  # Cache the result if caching is enabled
  if @find_styles_cache_enabled
    @find_styles_cache["#{font}:#{style}"] = results.dup
  end

  results
end

.font_pathsObject



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

def self.font_paths
  system_font_paths + fontist_font_paths
end

.fontist_font_pathsObject



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

def self.fontist_font_paths
  patterns = Fontist::Utils.font_file_patterns(Fontist.fonts_path.join("**").to_s)
  @fontist_font_paths ||= patterns.flat_map { |pattern| Dir.glob(pattern) }
end

.load_system_font_pathsObject

This detects all fonts on the system from the configuration file.



15
16
17
18
19
20
21
22
23
# File 'lib/fontist/system_font.rb', line 15

def self.load_system_font_paths
  os = Fontist::Utils::System.user_os.to_s
  templates = system_config["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_find_styles_cacheObject

Reset the find styles cache



73
74
75
# File 'lib/fontist/system_font.rb', line 73

def self.reset_find_styles_cache
  @find_styles_cache = nil
end

.reset_font_paths_cacheObject



52
53
54
55
56
# File 'lib/fontist/system_font.rb', line 52

def self.reset_font_paths_cache
  reset_system_font_paths_cache
  reset_fontist_font_paths_cache
  reset_find_styles_cache
end

.reset_fontist_font_paths_cacheObject



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

def self.reset_fontist_font_paths_cache
  @fontist_font_paths = nil
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_configObject



25
26
27
# File 'lib/fontist/system_font.rb', line 25

def self.system_config
  YAML.load_file(Fontist.system_file_path)
end

.system_font_pathsObject



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

def self.system_font_paths
  @system_font_paths ||= load_system_font_paths
end