Class: Pdfrb::FontResolver
- Inherits:
-
Object
- Object
- Pdfrb::FontResolver
- Defined in:
- lib/pdfrb/font_resolver.rb
Overview
Font file resolver. Searches system font directories for font files by family name + style. Eliminates the need for external font-finding libraries like fontisan.
Defined Under Namespace
Classes: FontInfo
Constant Summary collapse
- DEFAULT_SEARCH_PATHS =
[ "/System/Library/Fonts", "/Library/Fonts", File.("~/Library/Fonts"), "/usr/share/fonts", File.("~/.local/share/fonts"), "/usr/local/share/fonts", ].freeze
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#search_paths ⇒ Object
readonly
Returns the value of attribute search_paths.
Instance Method Summary collapse
- #available_fonts ⇒ Object
- #find(family:, style: "Regular") ⇒ Object
- #find_by_ps_name(ps_name) ⇒ Object
-
#initialize(search_paths: DEFAULT_SEARCH_PATHS) ⇒ FontResolver
constructor
A new instance of FontResolver.
Constructor Details
#initialize(search_paths: DEFAULT_SEARCH_PATHS) ⇒ FontResolver
Returns a new instance of FontResolver.
21 22 23 24 |
# File 'lib/pdfrb/font_resolver.rb', line 21 def initialize(search_paths: DEFAULT_SEARCH_PATHS) @search_paths = search_paths.dup @cache = nil end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
19 20 21 |
# File 'lib/pdfrb/font_resolver.rb', line 19 def cache @cache end |
#search_paths ⇒ Object (readonly)
Returns the value of attribute search_paths.
19 20 21 |
# File 'lib/pdfrb/font_resolver.rb', line 19 def search_paths @search_paths end |
Instance Method Details
#available_fonts ⇒ Object
50 51 52 53 |
# File 'lib/pdfrb/font_resolver.rb', line 50 def available_fonts build_cache unless @cache @cache.dup end |
#find(family:, style: "Regular") ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pdfrb/font_resolver.rb', line 26 def find(family:, style: "Regular") build_cache unless @cache normalized_family = family.downcase.gsub(/\s+/, "") normalized_style = style.downcase.gsub(/\s+/, "") @cache.each do |info| next unless info.family&.downcase&.gsub(/\s+/, "") == normalized_family next unless match_style?(info.style, normalized_style) return info.path end nil end |
#find_by_ps_name(ps_name) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/pdfrb/font_resolver.rb', line 40 def find_by_ps_name(ps_name) build_cache unless @cache normalized = ps_name.downcase @cache.each do |info| return info.path if info.ps_name&.downcase == normalized end nil end |