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



31
32
33
34
# File 'lib/fontisan/pipeline/format_detector.rb', line 31

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



23
24
25
# File 'lib/fontisan/pipeline/format_detector.rb', line 23

def file_path
  @file_path
end

#fontTrueTypeFont, ... (readonly)

Returns Loaded font.

Returns:



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

def font
  @font
end

Instance Method Details

#collection?Boolean

Check if font is a collection

Returns:

  • (Boolean)

    True if the loaded object is a font collection



103
104
105
106
107
# File 'lib/fontisan/pipeline/format_detector.rb', line 103

def collection?
  return false unless @font

  @font.collection?
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



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 120

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



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

def detect
  load_font

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

#detect_capabilitiesHash

Detect font capabilities

Aggregates four pieces of self-knowledge owned by the font class: outline representation, variation support, collection status, and the table directory. Each is exposed as a method on the font object so this method stays free of class-specific branching.

Returns:

  • (Hash)

    Capabilities hash



89
90
91
92
93
94
95
96
97
98
# File 'lib/fontisan/pipeline/format_detector.rb', line 89

def detect_capabilities
  return default_capabilities unless @font

  {
    outline: @font.outline_type,
    variation: @font.variation_type != :static,
    collection: @font.collection?,
    tables: @font.table_names,
  }
end

#detect_formatSymbol

Detect font format

Delegates to the loaded font object's own #format method. Each font class (TrueTypeFont, OpenTypeFont, WoffFont, Woff2Font, Type1Font, *Collection) is the single source of truth for its format identifier. This keeps FormatDetector closed for modification when new font classes are added (OCP): no case statement to edit.

Returns:

  • (Symbol)

    One of :ttf, :otf, :ttc, :otc, :woff, :woff2, :type1, :dfont, or :unknown



59
60
61
62
63
64
65
66
# File 'lib/fontisan/pipeline/format_detector.rb', line 59

def detect_format
  # SVG fonts are an export-only target; no font object exists yet.
  return :svg if @file_path.end_with?(".svg")

  return :unknown unless @font

  @font.format
end

#detect_variationSymbol

Detect variation type

Delegates to the loaded font object's own #variation_type method. Each font class is the single source of truth for its variation profile, so this method needs no class-specific branching.

Returns:

  • (Symbol)

    :static, :gvar (TrueType variable), or :cff2



75
76
77
78
79
# File 'lib/fontisan/pipeline/format_detector.rb', line 75

def detect_variation
  return :static unless @font

  @font.variation_type
end

#variable?Boolean

Check if font is variable

Returns:

  • (Boolean)

    True if variable font



112
113
114
# File 'lib/fontisan/pipeline/format_detector.rb', line 112

def variable?
  detect_variation != :static
end