Class: Pdfrb::FontResolver

Inherits:
Object
  • Object
show all
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.expand_path("~/Library/Fonts"),
  "/usr/share/fonts",
  File.expand_path("~/.local/share/fonts"),
  "/usr/local/share/fonts",
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cacheObject (readonly)

Returns the value of attribute cache.



19
20
21
# File 'lib/pdfrb/font_resolver.rb', line 19

def cache
  @cache
end

#search_pathsObject (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_fontsObject



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