Class: Fontisan::Pipeline::FormatDetector
- Inherits:
-
Object
- Object
- Fontisan::Pipeline::FormatDetector
- 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.
Instance Attribute Summary collapse
-
#file_path ⇒ String
readonly
Path to font file.
-
#font ⇒ TrueTypeFont, ...
readonly
Loaded font.
Instance Method Summary collapse
-
#collection? ⇒ Boolean
Check if font is a collection.
-
#compatible_with?(target_format) ⇒ Boolean
Check if format is compatible with target.
-
#detect ⇒ Hash
Detect format and capabilities.
-
#detect_capabilities ⇒ Hash
Detect font capabilities.
-
#detect_format ⇒ Symbol
Detect font format.
-
#detect_variation ⇒ Symbol
Detect variation type.
-
#initialize(file_path) ⇒ FormatDetector
constructor
Initialize detector.
-
#variable? ⇒ Boolean
Check if font is variable.
Constructor Details
#initialize(file_path) ⇒ FormatDetector
Initialize detector
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_path ⇒ String (readonly)
Returns Path to font file.
23 24 25 |
# File 'lib/fontisan/pipeline/format_detector.rb', line 23 def file_path @file_path end |
#font ⇒ TrueTypeFont, ... (readonly)
Returns Loaded font.
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
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
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 |
#detect ⇒ Hash
Detect format and 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_capabilities ⇒ Hash
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.
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_format ⇒ Symbol
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.
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_variation ⇒ Symbol
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.
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
112 113 114 |
# File 'lib/fontisan/pipeline/format_detector.rb', line 112 def variable? detect_variation != :static end |