Class: Fontisan::Converters::CollectionConverter
- Inherits:
-
Object
- Object
- Fontisan::Converters::CollectionConverter
- Defined in:
- lib/fontisan/converters/collection_converter.rb
Overview
CollectionConverter handles conversion between collection formats
Main responsibility: Convert between TTC, OTC, and dfont collection formats using a three-step strategy:
-
Unpack: Extract individual fonts from source collection
-
Convert: Transform each font’s outline format if requested
-
Repack: Rebuild collection in target format
Supported conversions:
-
TTC ↔ OTC (preserves mixed TTF+OTF by default)
-
TTC → dfont (repackage)
-
OTC → dfont (repackage)
-
dfont → TTC (preserves mixed formats)
-
dfont → OTC (preserves mixed formats)
Instance Method Summary collapse
-
#convert(collection_path, target_type:, options: {}) ⇒ Hash
Convert collection to target format.
Instance Method Details
#convert(collection_path, target_type:, options: {}) ⇒ Hash
Convert collection to target format
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/fontisan/converters/collection_converter.rb', line 60 def convert(collection_path, target_type:, options: {}) validate_parameters!(collection_path, target_type, ) # Extract ConversionOptions if provided = () verbose = .fetch(:verbose, false) output_path = [:output] # Determine target format from ConversionOptions or options hash target_format = if &.(:target_format, "otf") .[:target_format] else .fetch(:target_format, "preserve").to_s end # Validate target_format unless %w[preserve ttf otf].include?(target_format) raise ArgumentError, "Invalid target_format: #{target_format}. Must be 'preserve', 'ttf', or 'otf'" end puts "Converting collection to #{target_type.to_s.upcase}..." if verbose # Step 1: Unpack - extract fonts from source collection puts " Unpacking fonts from source collection..." if verbose fonts, source_type = unpack_fonts(collection_path) # Check if conversion is needed if source_type == target_type puts " Source and target formats are the same, copying collection..." if verbose FileUtils.cp(collection_path, output_path) return build_result(collection_path, output_path, source_type, target_type, fonts.size, []) end # Step 2: Convert - transform fonts if requested puts " Converting #{fonts.size} font(s)..." if verbose converted_fonts, conversions = convert_fonts(fonts, source_type, target_type, .merge(target_format: target_format), ) # Step 3: Repack - build target collection puts " Repacking into #{target_type.to_s.upcase} format..." if verbose repack_fonts(converted_fonts, target_type, output_path, ) # Build result result = build_result(collection_path, output_path, source_type, target_type, fonts.size, conversions) if verbose display_result(result) end result end |