Class: Fontisan::Pipeline::FormatDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/pipeline/format_detector.rb

Overview

Detects font format and capabilities

This class analyzes font files to determine:

  • Format: TTF, OTF, TTC, OTC, WOFF, WOFF2, SVG

  • Variation type: static, gvar (TrueType variable), CFF2 (OpenType variable)

  • Capabilities: outline type, variation support, collection support

Used by the universal transformation pipeline to determine conversion strategies and validate compatibility.

Examples:

Detecting a font’s format

detector = FormatDetector.new("font.ttf")
info = detector.detect
puts info[:format]        # => :ttf
puts info[:variation_type] # => :gvar
puts info[:capabilities][:outline] # => :truetype

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FormatDetector

Initialize detector

Parameters:

  • file_path (String)

    Path to font file



33
34
35
36
# File 'lib/fontisan/pipeline/format_detector.rb', line 33

def initialize(file_path)
  @file_path = file_path
  @font = nil
end

Instance Attribute Details

#file_pathString (readonly)

Returns Path to font file.

Returns:

  • (String)

    Path to font file



25
26
27
# File 'lib/fontisan/pipeline/format_detector.rb', line 25

def file_path
  @file_path
end

#fontTrueTypeFont, ... (readonly)

Returns Loaded font.

Returns:



28
29
30
# File 'lib/fontisan/pipeline/format_detector.rb', line 28

def font
  @font
end

Instance Method Details

#collection?Boolean

Check if font is a collection

Returns:

  • (Boolean)

    True if collection (TTC/OTC)



151
152
153
154
# File 'lib/fontisan/pipeline/format_detector.rb', line 151

def collection?
  @font.is_a?(Fontisan::TrueTypeCollection) ||
    @font.is_a?(Fontisan::OpenTypeCollection)
end

#compatible_with?(target_format) ⇒ Boolean

Check if format is compatible with target

Parameters:

  • target_format (Symbol)

    Target format (:ttf, :otf, etc.)

Returns:

  • (Boolean)

    True if conversion is possible



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/fontisan/pipeline/format_detector.rb', line 167

def compatible_with?(target_format)
  current_format = detect_format
  variation_type = detect_variation

  # Same format is always compatible
  return true if current_format == target_format

  # Collection formats
  if %i[ttc otc].include?(current_format)
    return %i[ttc otc].include?(target_format)
  end

  # Variable font constraints
  if variation_type == :static
    # Static fonts can convert to any format
    true
  else
    case variation_type
    when :gvar
      # TrueType variable can convert to TrueType formats
      %i[ttf ttc woff woff2].include?(target_format)
    when :cff2
      # OpenType variable can convert to OpenType formats
      %i[otf otc woff woff2].include?(target_format)
    end
  end
end

#detectHash

Detect format and capabilities

Returns:

  • (Hash)

    Detection results with :format, :variation_type, :capabilities



41
42
43
44
45
46
47
48
49
# File 'lib/fontisan/pipeline/format_detector.rb', line 41

def detect
  load_font

  {
    format: detect_format,
    variation_type: detect_variation,
    capabilities: detect_capabilities,
  }
end

#detect_capabilitiesHash

Detect font capabilities

Returns:

  • (Hash)

    Capabilities hash



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fontisan/pipeline/format_detector.rb', line 115

def detect_capabilities
  return default_capabilities unless @font

  # Check if this is a collection
  is_collection = collection?

  font_to_check = if is_collection
                    # Collections don't have fonts method, need to load first font
                    nil # Will handle in API usage
                  else
                    @font
                  end

  # For collections, return basic capabilities
  if is_collection
    return {
      outline: :unknown, # Would need to load first font to know
      variation: false,  # Would need to load first font to know
      collection: true,
      tables: [],
    }
  end

  return default_capabilities unless font_to_check

  {
    outline: detect_outline_type(font_to_check),
    variation: detect_variation != :static,
    collection: false,
    tables: available_tables(font_to_check),
  }
end

#detect_formatSymbol

Detect font format

Returns:

  • (Symbol)

    One of :ttf, :otf, :ttc, :otc, :woff, :woff2, :svg



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fontisan/pipeline/format_detector.rb', line 54

def detect_format
  # Check for SVG first (from file extension even if font failed to load)
  return :svg if @file_path.end_with?(".svg")

  return :unknown unless @font

  # Use is_a? for proper class checking
  case @font
  when Fontisan::TrueTypeCollection
    :ttc
  when Fontisan::OpenTypeCollection
    :otc
  when Fontisan::TrueTypeFont
    if @file_path.end_with?(".woff")
      :woff
    elsif @file_path.end_with?(".woff2")
      :woff2
    else
      :ttf
    end
  when Fontisan::OpenTypeFont
    if @file_path.end_with?(".woff")
      :woff
    elsif @file_path.end_with?(".woff2")
      :woff2
    else
      :otf
    end
  else
    :unknown
  end
end

#detect_variationSymbol

Detect variation type

Returns:

  • (Symbol)

    One of :static, :gvar, :cff2



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fontisan/pipeline/format_detector.rb', line 90

def detect_variation
  return :static unless @font

  # Collections don't have has_table? method
  # Return :static for collections (variation detection would need to load first font)
  return :static if collection?

  # Check for variable font tables
  if @font.has_table?("fvar")
    # Variable font detected - check variation type
    if @font.has_table?("gvar")
      :gvar # TrueType variable font
    elsif @font.has_table?("CFF2")
      :cff2 # OpenType variable font (CFF2)
    else
      :static # Has fvar but no variation data (shouldn't happen)
    end
  else
    :static
  end
end

#variable?Boolean

Check if font is variable

Returns:

  • (Boolean)

    True if variable font



159
160
161
# File 'lib/fontisan/pipeline/format_detector.rb', line 159

def variable?
  detect_variation != :static
end