Module: Fontisan::Converters::ConversionStrategy

Included in:
OutlineConverter, SvgGenerator, TableCopier, Woff2Encoder, WoffWriter
Defined in:
lib/fontisan/converters/conversion_strategy.rb

Overview

Interface module for font format conversion strategies

[‘ConversionStrategy`](lib/fontisan/converters/conversion_strategy.rb) defines the contract that all conversion strategy classes must implement. This follows the Strategy pattern to enable polymorphic handling of different conversion types (TTF→OTF, OTF→TTF, same-format copying).

Each strategy must implement:

  • convert(font, options) - Perform the actual conversion

  • supported_conversions - Return array of [source, target] format pairs

  • validate(font, target_format) - Validate conversion is possible

Strategies are selected by [‘FormatConverter`](lib/fontisan/converters/format_converter.rb) based on source and target formats.

Examples:

Implementing a strategy

class MyStrategy
  include Fontisan::Converters::ConversionStrategy

  def convert(font, options = {})
    # Perform conversion
    tables = {...}
    tables
  end

  def supported_conversions
    [[:ttf, :otf], [:otf, :ttf]]
  end

  def validate(font, target_format)
    # Validate font can be converted
    raise Error unless valid
  end
end

Instance Method Summary collapse

Instance Method Details

#convert(font, options = {}) ⇒ Hash<String, String>

Convert font to target format

This method must return a hash of table tags to binary data, which will be assembled into a complete font by FontWriter.

Parameters:

Returns:

  • (Hash<String, String>)

    Map of table tags to binary data

Raises:

  • (NotImplementedError)

    If not implemented by strategy



49
50
51
52
# File 'lib/fontisan/converters/conversion_strategy.rb', line 49

def convert(font, options = {})
  raise NotImplementedError,
        "#{self.class.name} must implement convert(font, options)"
end

#supported_conversionsArray<Array<Symbol>>

Get list of supported conversions

Returns an array of [source_format, target_format] pairs that this strategy can handle.

Examples:

strategy.supported_conversions
# => [[:ttf, :otf], [:otf, :ttf]]

Returns:

  • (Array<Array<Symbol>>)

    Supported conversion pairs

Raises:

  • (NotImplementedError)

    If not implemented by strategy



65
66
67
68
# File 'lib/fontisan/converters/conversion_strategy.rb', line 65

def supported_conversions
  raise NotImplementedError,
        "#{self.class.name} must implement supported_conversions"
end

#supports?(source_format, target_format) ⇒ Boolean

Check if strategy supports a conversion

Parameters:

  • source_format (Symbol)

    Source format

  • target_format (Symbol)

    Target format

Returns:

  • (Boolean)

    True if supported



91
92
93
# File 'lib/fontisan/converters/conversion_strategy.rb', line 91

def supports?(source_format, target_format)
  supported_conversions.include?([source_format, target_format])
end

#validate(font, target_format) ⇒ Boolean

Validate that conversion is possible

Checks if the given font can be converted to the target format. Should raise an error with a clear message if conversion is not possible.

Parameters:

Returns:

  • (Boolean)

    True if valid

Raises:

  • (Error)

    If conversion is not possible

  • (NotImplementedError)

    If not implemented by strategy



81
82
83
84
# File 'lib/fontisan/converters/conversion_strategy.rb', line 81

def validate(font, target_format)
  raise NotImplementedError,
        "#{self.class.name} must implement validate(font, target_format)"
end