Class: Fontisan::ConversionOptions

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

Overview

Conversion options for font format transformations

Defines all options for opening (reading) and generating (writing) fonts during conversion operations. Includes validation, defaults, and presets.

Examples:

Using options directly

options = ConversionOptions.new(
  from: :ttf,
  to: :otf,
  opening: { convert_curves: true, scale_to_1000: true },
  generating: { hinting_mode: "auto" }
)

Using recommended options

options = ConversionOptions.recommended(from: :ttf, to: :otf)

Using a preset

options = ConversionOptions.from_preset(:type1_to_modern)

Constant Summary collapse

OPENING_OPTIONS =

Opening options (input processing)

%i[
  decompose_composites
  convert_curves
  scale_to_1000
  scale_from_1000
  autohint
  generate_unicode
  store_custom_tables
  store_native_hinting
  interpret_ot
  read_all_records
  preserve_encoding
].freeze
GENERATING_OPTIONS =

Generating options (output processing)

%i[
  write_pfm
  write_afm
  write_inf
  select_encoding_automatically
  hinting_mode
  decompose_on_output
  write_custom_tables
  optimize_tables
  reencode_first_256
  encoding_vector
  compression
  transform_tables
  preserve_metadata
  strip_metadata
  target_format
].freeze
HINTING_MODES =

Valid hinting modes

%w[preserve auto none full].freeze
COMPRESSION_MODES =

Valid compression modes

%w[zlib brotli none].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:, opening: {}, generating: {}) ⇒ ConversionOptions

Initialize conversion options

Parameters:

  • from (String, Symbol)

    Source format

  • to (String, Symbol)

    Target format

  • opening (Hash) (defaults to: {})

    Opening options

  • generating (Hash) (defaults to: {})

    Generating options



71
72
73
74
75
76
77
# File 'lib/fontisan/conversion_options.rb', line 71

def initialize(from:, to:, opening: {}, generating: {})
  @from = self.class.normalize_format(from)
  @to = self.class.normalize_format(to)
  @opening = apply_opening_defaults(opening)
  @generating = apply_generating_defaults(generating)
  validate!
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



63
64
65
# File 'lib/fontisan/conversion_options.rb', line 63

def from
  @from
end

#generatingObject (readonly)

Returns the value of attribute generating.



63
64
65
# File 'lib/fontisan/conversion_options.rb', line 63

def generating
  @generating
end

#openingObject (readonly)

Returns the value of attribute opening.



63
64
65
# File 'lib/fontisan/conversion_options.rb', line 63

def opening
  @opening
end

#toObject (readonly)

Returns the value of attribute to.



63
64
65
# File 'lib/fontisan/conversion_options.rb', line 63

def to
  @to
end

Class Method Details

.available_presetsArray<Symbol>

Get list of available presets

Returns:

  • (Array<Symbol>)

    Available preset names



136
137
138
# File 'lib/fontisan/conversion_options.rb', line 136

def self.available_presets
  PRESETS.keys
end

.from_preset(preset_name) ⇒ ConversionOptions

Load options from a named preset

Parameters:

  • preset_name (Symbol, String)

    Preset name

Returns:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fontisan/conversion_options.rb', line 101

def self.from_preset(preset_name)
  preset_key = preset_name.to_sym
  preset = PRESETS.fetch(preset_key) do
    raise ArgumentError, "Unknown preset: #{preset_name}. " \
                        "Available: #{PRESETS.keys.join(', ')}"
  end

  new(
    from: preset[:from],
    to: preset[:to],
    opening: preset[:opening] || {},
    generating: preset[:generating] || {},
  )
end

.normalize_format(format) ⇒ Symbol

Normalize format symbol

Parameters:

  • format (String, Symbol)

    Format identifier

Returns:

  • (Symbol)

    Normalized format symbol



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fontisan/conversion_options.rb', line 146

def self.normalize_format(format)
  case format.to_s.downcase
  when "ttf", "truetype", "truetype-tt", "ot-tt" then :ttf
  when "otf", "cff", "opentype", "ot-ps", "opentype-ps" then :otf
  when "type1", "type-1", "t1", "pfb", "pfa" then :type1
  when "ttc" then :ttc
  when "otc" then :otc
  when "dfont", "suitcase" then :dfont
  when "woff" then :woff
  when "woff2" then :woff2
  when "svg" then :svg
  else
    raise ArgumentError, "Unknown format: #{format}. " \
                        "Supported: ttf, otf, type1, ttc, otc, dfont, woff, woff2, svg"
  end
end

Get recommended options for a conversion pair

Parameters:

  • from (String, Symbol)

    Source format

  • to (String, Symbol)

    Target format

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fontisan/conversion_options.rb', line 84

def self.recommended(from:, to:)
  from_sym = normalize_format(from)
  to_sym = normalize_format(to)

  options = RECOMMENDED_OPTIONS.dig(from_sym, to_sym) || {}
  new(
    from: from_sym,
    to: to_sym,
    opening: options[:opening] || {},
    generating: options[:generating] || {},
  )
end

Instance Method Details

#generating_option?(key, value = true) ⇒ Boolean

Check if a generating option has a specific value

Parameters:

  • key (Symbol)

    Option key

  • value (Object) (defaults to: true)

    Value to check

Returns:

  • (Boolean)

    true if option equals value



129
130
131
# File 'lib/fontisan/conversion_options.rb', line 129

def generating_option?(key, value = true)
  @generating[key] == value
end

#opening_option?(key) ⇒ Boolean

Check if an opening option is set

Parameters:

  • key (Symbol)

    Option key

Returns:

  • (Boolean)

    true if option is truthy



120
121
122
# File 'lib/fontisan/conversion_options.rb', line 120

def opening_option?(key)
  !!@opening[key]
end