Class: Fontisan::Converters::WoffWriter
- Inherits:
-
Object
- Object
- Fontisan::Converters::WoffWriter
- Includes:
- ConversionStrategy
- Defined in:
- lib/fontisan/converters/woff_writer.rb
Overview
WOFF font writer for creating WOFF files from TTF/OTF fonts
WoffWriter converts
TrueType/OpenType fonts to WOFF 1.0 format. The WOFF spec mandates zlib
compression; this writer exposes the spec-legal knobs only:
zlib_level(0–9) — zlib compression leveluncompressed(bool) — store tables uncompressed (legal per WOFF 1.0 §5.1;compLength == origLength)compression_threshold(bytes) — skip compression for tables smaller than N bytes (rarely needed; keeps tiny tables uncompressed)metadata_xml(string) — optional metadata blockprivate_data(string) — optional private data block
Cross-format options (e.g., brotli_quality) are rejected by
ConversionStrategy#validate_options! — see ConversionStrategy.
Constant Summary collapse
- WOFF_SIGNATURE =
WOFF signature constant
0x774F4646- WOFF_VERSION_MAJOR =
WOFF version 1.0
1- WOFF_VERSION_MINOR =
0
Instance Method Summary collapse
-
#convert(font, options = {}) ⇒ String
Convert font to WOFF format.
-
#initialize ⇒ WoffWriter
constructor
Initialize writer.
-
#supported_conversions ⇒ Array<Array<Symbol>>
Get supported conversions.
-
#validate(font, target_format) ⇒ Boolean
Validate that the given font can be converted to WOFF.
-
#write_font(font, zlib_level:, uncompressed:, compression_threshold:, metadata: nil, private_data: nil) ⇒ Hash{Symbol => String}
Write font to WOFF binary.
Methods included from ConversionStrategy
Constructor Details
#initialize ⇒ WoffWriter
Initialize writer. The writer is stateless per call; all knobs come through the per-convert options hash.
62 |
# File 'lib/fontisan/converters/woff_writer.rb', line 62 def initialize; end |
Instance Method Details
#convert(font, options = {}) ⇒ String
Convert font to WOFF format.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/fontisan/converters/woff_writer.rb', line 75 def convert(font, = {}) self.class.(()) validate(font, :woff) opts = self.class..merge(()) write_font( font, zlib_level: opts[:zlib_level], uncompressed: opts[:uncompressed], compression_threshold: opts[:compression_threshold], metadata: opts[:metadata_xml], private_data: opts[:private_data], )[:woff_binary] end |
#supported_conversions ⇒ Array<Array<Symbol>>
Get supported conversions.
93 94 95 96 97 98 |
# File 'lib/fontisan/converters/woff_writer.rb', line 93 def supported_conversions [ %i[ttf woff], %i[otf woff], ] end |
#validate(font, target_format) ⇒ Boolean
Validate that the given font can be converted to WOFF.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fontisan/converters/woff_writer.rb', line 107 def validate(font, target_format) unless target_format == :woff raise Fontisan::Error, "WoffWriter only supports conversion to woff, got: #{target_format}" end raise ArgumentError, "Font cannot be nil" if font.nil? unless font.respond_to?(:tables) && font.respond_to?(:table_data) raise ArgumentError, "Font must respond to :tables and :table_data" end end |
#write_font(font, zlib_level:, uncompressed:, compression_threshold:, metadata: nil, private_data: nil) ⇒ Hash{Symbol => String}
Write font to WOFF binary.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/fontisan/converters/woff_writer.rb', line 129 def write_font(font, zlib_level:, uncompressed:, compression_threshold:, metadata: nil, private_data: nil) tables_data = collect_tables_data(font) compressed_tables = compress_tables( tables_data, zlib_level: uncompressed ? 0 : zlib_level, skip_compression: uncompressed, compression_threshold: compression_threshold, ) = (, zlib_level: zlib_level, skip_compression: uncompressed) binary = build_woff_file(compressed_tables, font, , private_data) { woff_binary: binary } end |