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



65
66
67
68
# File 'lib/fontist/system_font.rb', line 65

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



59
60
61
62
# File 'lib/fontist/system_font.rb', line 59

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

.expand_paths(paths) ⇒ Object



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

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



75
76
77
78
79
80
# File 'lib/fontist/system_font.rb', line 75

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



83
84
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
# File 'lib/fontist/system_font.rb', line 83

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



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

def self.font_paths
  system_font_paths + fontist_font_paths
end

.fontist_font_pathsObject



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

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.



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

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



71
72
73
# File 'lib/fontist/system_font.rb', line 71

def self.reset_find_styles_cache
  @find_styles_cache = nil
end

.reset_font_paths_cacheObject



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

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



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

def self.reset_fontist_font_paths_cache
  @fontist_font_paths = nil
end

.reset_system_font_paths_cacheObject



27
28
29
# File 'lib/fontist/system_font.rb', line 27

def self.reset_system_font_paths_cache
  @system_font_paths = nil
end

.system_configObject



23
24
25
# File 'lib/fontist/system_font.rb', line 23

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

.system_font_pathsObject



8
9
10
# File 'lib/fontist/system_font.rb', line 8

def self.system_font_paths
  @system_font_paths ||= load_system_font_paths
end