Module: Fontisan::Ufo::Convert::ToWoff2

Defined in:
lib/fontisan/ufo/convert/to_woff2.rb

Overview

UFO → WOFF2. Same two-step pipeline as ToWoff, but with the WOFF2 encoder (Brotli compression, optional glyf/loca transforms) instead of WOFF (zlib).

Returns the output_path; the WOFF2 binary itself lands at output_path.

Class Method Summary collapse

Class Method Details

.convert(ufo, output_path:, compiler: :ttf, **woff2_options) ⇒ String

Returns the output_path.

Parameters:

  • ufo (Fontisan::Ufo::Font)
  • output_path (String)
  • compiler (Symbol) (defaults to: :ttf)

    which intermediate format to use: :ttf (default) or :otf

  • woff2_options (Hash)

    forwarded to Woff2Encoder.convert (brotli_quality, transform_tables, quality legacy alias)

Returns:

  • (String)

    the output_path

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fontisan/ufo/convert/to_woff2.rb', line 22

def self.convert(ufo, output_path:, compiler: :ttf, **woff2_options)
  compiler_class = Convert::COMPILER_FOR_FORMAT[compiler.to_sym]
  raise ArgumentError, "unknown intermediate compiler: #{compiler.inspect}" unless compiler_class

  Dir.mktmpdir do |dir|
    intermediate_path = File.join(dir, "intermediate#{format_ext(compiler)}")
    compiler_class.new(ufo).compile(output_path: intermediate_path)

    loaded = Fontisan::FontLoader.load(intermediate_path)
    result = Fontisan::Converters::Woff2Encoder.new.convert(loaded, woff2_options)
    File.binwrite(output_path, result[:woff2_binary])
  end

  output_path
end