Class: Fontisan::Woff2Collection
- Inherits:
-
Object
- Object
- Fontisan::Woff2Collection
- Includes:
- Collection::SharedLogic
- Defined in:
- lib/fontisan/woff2_collection.rb
Overview
WOFF2 font collection (spec section 4.2).
Wraps a WOFF2 collection file (flavor='ttcf') and exposes the same
interface as TrueTypeCollection/OpenTypeCollection so collection
consumers don't need to know which container format they're holding.
Unlike TTC/OTC (which subclass BaseCollection for BinData parsing),
WOFF2 collections are decoded eagerly via Woff2::CollectionDecoder
because the per-font tables require Brotli decompression +
glyf/loca reconstruction before they can be returned.
Instance Attribute Summary collapse
-
#data ⇒ String
readonly
Source WOFF2 binary.
Class Method Summary collapse
-
.from_file(path) ⇒ Woff2Collection
Load a WOFF2 collection from a file path.
Instance Method Summary collapse
-
#collection? ⇒ Boolean
Whether this object represents a font collection.
-
#extract_fonts(_io = nil) ⇒ Array<TrueTypeFont, OpenTypeFont>
Extract every font in the collection.
-
#font(index, _io = nil, _mode: LoadingModes::FULL) ⇒ TrueTypeFont, ...
Get a single font by index.
-
#font_offsets ⇒ Array<Integer>
Per-font synthetic offsets.
-
#format ⇒ Symbol
High-level pipeline format identifier.
-
#initialize(data) ⇒ Woff2Collection
constructor
A new instance of Woff2Collection.
-
#num_fonts ⇒ Integer
(also: #font_count)
Number of fonts in the collection.
-
#table_names ⇒ Array<String>
Collections have no single SFNT table directory.
-
#valid? ⇒ Boolean
Validate format correctness.
Methods included from Collection::SharedLogic
#calculate_table_sharing_for_fonts
Constructor Details
#initialize(data) ⇒ Woff2Collection
Returns a new instance of Woff2Collection.
23 24 25 |
# File 'lib/fontisan/woff2_collection.rb', line 23 def initialize(data) @data = data.force_encoding(Encoding::BINARY) end |
Instance Attribute Details
#data ⇒ String (readonly)
Returns Source WOFF2 binary.
20 21 22 |
# File 'lib/fontisan/woff2_collection.rb', line 20 def data @data end |
Class Method Details
.from_file(path) ⇒ Woff2Collection
Load a WOFF2 collection from a file path.
31 32 33 |
# File 'lib/fontisan/woff2_collection.rb', line 31 def self.from_file(path) new(File.binread(path)) end |
Instance Method Details
#collection? ⇒ Boolean
Whether this object represents a font collection. WOFF2 collections are always collections.
78 |
# File 'lib/fontisan/woff2_collection.rb', line 78 def collection? = true |
#extract_fonts(_io = nil) ⇒ Array<TrueTypeFont, OpenTypeFont>
Extract every font in the collection.
57 58 59 |
# File 'lib/fontisan/woff2_collection.rb', line 57 def extract_fonts(_io = nil) decoded.map { |entry| build_font(entry) } end |
#font(index, _io = nil, _mode: LoadingModes::FULL) ⇒ TrueTypeFont, ...
Get a single font by index.
68 69 70 71 72 |
# File 'lib/fontisan/woff2_collection.rb', line 68 def font(index, _io = nil, _mode: LoadingModes::FULL) return nil if index >= num_fonts build_font(decoded[index]) end |
#font_offsets ⇒ Array<Integer>
Per-font synthetic offsets. WOFF2 collections don't have byte offsets
like TTC (each font is reconstructed in memory); the index is the
only identifier. Returned for compatibility with BaseCollection's
interface.
49 50 51 |
# File 'lib/fontisan/woff2_collection.rb', line 49 def font_offsets (0...num_fonts).to_a end |
#format ⇒ Symbol
High-level pipeline format identifier. Owned by the collection class so the conversion pipeline can dispatch without case statements.
99 |
# File 'lib/fontisan/woff2_collection.rb', line 99 def format = :woff2_collection |
#num_fonts ⇒ Integer Also known as: font_count
Number of fonts in the collection.
38 39 40 |
# File 'lib/fontisan/woff2_collection.rb', line 38 def num_fonts decoded.length end |
#table_names ⇒ Array<String>
Collections have no single SFNT table directory.
83 |
# File 'lib/fontisan/woff2_collection.rb', line 83 def table_names = [] |
#valid? ⇒ Boolean
Validate format correctness.
88 89 90 91 92 93 |
# File 'lib/fontisan/woff2_collection.rb', line 88 def valid? flavor = @data[4, 4].unpack1("N") flavor == Woff2::CollectionDecoder::TTC_FLAVOR rescue StandardError false end |