Class: Fontist::Import::Files::FontDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/import/files/font_detector.rb

Constant Summary collapse

FONT_EXTENSIONS =

Font format to extension mapping Only used when file extension is not a recognized platform-specific format

{
  "truetype" => "ttf",
  "cff" => "otf",
}.freeze
PLATFORM_SPECIFIC_EXTENSIONS =

Platform-specific font extensions that should be preserved as-is These are valid font containers that don’t need conversion

%w[dfont otc].freeze

Class Method Summary collapse

Class Method Details

.detect(path, error_collector: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fontist/import/files/font_detector.rb', line 19

def detect(path, error_collector: nil)
  info = brief_info(path, error_collector: error_collector)
  return :other unless info

  # Check if it's a collection based on the info type
  if collection_info?(info)
    :collection
  elsif font_info?(info)
    :font
  else
    :other
  end
end

.standard_extension(path, error_collector: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fontist/import/files/font_detector.rb', line 33

def standard_extension(path, error_collector: nil)
  # Check if file has a platform-specific extension that should be preserved
  file_ext = File.extname(path).sub(/^\./, "").downcase
  return file_ext if PLATFORM_SPECIFIC_EXTENSIONS.include?(file_ext)

  info = brief_info(path, error_collector: error_collector)
  return nil unless info

  # For collections, always use ttc unless it has a platform-specific extension
  if collection_info?(info)
    return "ttc"
  end

  # For single fonts, map format to extension
  font_format = get_font_format(info)
  extension = FONT_EXTENSIONS[font_format]
  return extension if extension

  # Fallback to file extension if format unknown
  file_ext
rescue StandardError
  raise Errors::UnknownFontTypeError.new(path)
end