Module: GD::GIS::FontHelper
- Defined in:
- lib/gd/gis/font_helper.rb
Overview
This helper does not validate glyph coverage (e.g. CJK support). It only locates font files present on the system.
Provides helper methods for discovering and selecting font files available on the local system.
This module is primarily used to supply font paths to text-rendering components (such as PointsLayer labels) in environments where font availability is system-dependent.
Font discovery is performed by scanning a set of well-known directories for TrueType and OpenType font files. The results are cached for the lifetime of the process.
Supported font formats:
-
TrueType (.ttf)
-
OpenType (.otf)
-
TrueType Collection (.ttc)
Constant Summary collapse
- PATHS =
[ "/usr/share/fonts", "/usr/local/share/fonts", File.("~/.fonts") ].freeze
- EXTENSIONS =
%w[ttf otf ttc].freeze
Class Method Summary collapse
-
.all ⇒ Array<String>
Returns the list of all font files discovered on the system.
-
.find(name) ⇒ String?
Finds a font file whose filename includes the given name fragment.
-
.random ⇒ String
Returns a randomly selected font file from the system.
Class Method Details
.all ⇒ Array<String>
Returns the list of all font files discovered on the system.
The search is performed once and cached. Subsequent calls return the cached result.
Font files are discovered by recursively scanning the directories defined in PATHS for files matching the extensions in EXTENSIONS.
47 48 49 50 51 52 53 54 55 |
# File 'lib/gd/gis/font_helper.rb', line 47 def self.all @all ||= PATHS.flat_map do |path| next [] unless Dir.exist?(path) EXTENSIONS.flat_map do |ext| Dir.glob("#{path}/**/*.#{ext}") end end.compact.uniq end |
.find(name) ⇒ String?
Finds a font file whose filename includes the given name fragment.
The match is case-insensitive and performed against the basename of each discovered font file.
85 86 87 88 89 |
# File 'lib/gd/gis/font_helper.rb', line 85 def self.find(name) all.find do |f| File.basename(f).downcase.include?(name.downcase) end end |
.random ⇒ String
The selected font is not guaranteed to support any particular character set.
Returns a randomly selected font file from the system.
This is primarily intended as a fallback mechanism when no explicit font is configured by the caller.
71 72 73 |
# File 'lib/gd/gis/font_helper.rb', line 71 def self.random all.sample or raise "GD::GIS::FontHelper: no fonts found on system" end |