Class: Fontisan::Woff2::Directory::Entry
- Inherits:
-
Object
- Object
- Fontisan::Woff2::Directory::Entry
- Defined in:
- lib/fontisan/woff2/directory.rb
Overview
WOFF2 Table Directory Entry
Represents a single table in the WOFF2 font with all metadata needed for decompression and reconstruction.
Instance Attribute Summary collapse
-
#flags ⇒ Object
Calculated during encoding.
-
#offset ⇒ Object
Calculated during encoding.
-
#orig_length ⇒ Object
Calculated during encoding.
-
#tag ⇒ Object
Calculated during encoding.
-
#transform_length ⇒ Object
Calculated during encoding.
Instance Method Summary collapse
-
#calculate_flags ⇒ Integer
Calculate flags byte for this entry.
-
#determine_transform_version ⇒ Integer
Determine the transform version to encode in the flags byte.
-
#initialize ⇒ Entry
constructor
A new instance of Entry.
-
#known_tag? ⇒ Boolean
Check if table uses a known tag.
-
#serialized_size ⇒ Integer
Calculate size of this entry when serialized.
-
#tag_index ⇒ Integer
Get tag index from flags.
-
#transform_version ⇒ Integer
Get transformation version from flags.
-
#transformable? ⇒ Boolean
Check if table can be transformed (glyf, loca, hmtx).
-
#transformed? ⇒ Boolean
Whether this entry will be serialized with a transformLength field.
Constructor Details
#initialize ⇒ Entry
Returns a new instance of Entry.
76 77 78 79 80 81 82 |
# File 'lib/fontisan/woff2/directory.rb', line 76 def initialize @tag = nil @flags = 0 @orig_length = 0 @transform_length = nil @offset = 0 end |
Instance Attribute Details
#flags ⇒ Object
Calculated during encoding
74 75 76 |
# File 'lib/fontisan/woff2/directory.rb', line 74 def flags @flags end |
#offset ⇒ Object
Calculated during encoding
74 75 76 |
# File 'lib/fontisan/woff2/directory.rb', line 74 def offset @offset end |
#orig_length ⇒ Object
Calculated during encoding
74 75 76 |
# File 'lib/fontisan/woff2/directory.rb', line 74 def orig_length @orig_length end |
#tag ⇒ Object
Calculated during encoding
74 75 76 |
# File 'lib/fontisan/woff2/directory.rb', line 74 def tag @tag end |
#transform_length ⇒ Object
Calculated during encoding
74 75 76 |
# File 'lib/fontisan/woff2/directory.rb', line 74 def transform_length @transform_length end |
Instance Method Details
#calculate_flags ⇒ Integer
Calculate flags byte for this entry
87 88 89 90 91 92 93 |
# File 'lib/fontisan/woff2/directory.rb', line 87 def calculate_flags tag_index = KNOWN_TAGS.index(tag) || CUSTOM_TAG_INDEX transform_version = determine_transform_version # Combine tag index (bits 0-5) and transform version (bits 6-7) (transform_version << 6) | tag_index end |
#determine_transform_version ⇒ Integer
Determine the transform version to encode in the flags byte.
Per WOFF2 spec section 5.1, glyf/loca use version 0 for the
transformed format and version 3 for the null transform; hmtx
uses version 1 for transformed and version 0 for null. The
encoder sets transform_length (or leaves it nil) to indicate
which case applies — see transformed?.
139 140 141 142 143 144 145 146 147 |
# File 'lib/fontisan/woff2/directory.rb', line 139 def determine_transform_version if ["glyf", "loca"].include?(tag) transformed? ? TRANSFORM_GLYF_LOCA : TRANSFORM_NULL elsif tag == "hmtx" transformed? ? TRANSFORM_HMTX : TRANSFORM_HMTX_NULL else 0 end end |
#known_tag? ⇒ Boolean
Check if table uses a known tag
98 99 100 |
# File 'lib/fontisan/woff2/directory.rb', line 98 def known_tag? KNOWN_TAGS.include?(tag) end |
#serialized_size ⇒ Integer
Calculate size of this entry when serialized
159 160 161 162 163 164 165 |
# File 'lib/fontisan/woff2/directory.rb', line 159 def serialized_size size = 1 # flags byte size += 4 unless known_tag? # custom tag size += uint_base128_size(orig_length) size += uint_base128_size(transform_length) if transformed? size end |
#tag_index ⇒ Integer
Get tag index from flags
126 127 128 |
# File 'lib/fontisan/woff2/directory.rb', line 126 def tag_index flags & 0x3F end |
#transform_version ⇒ Integer
Get transformation version from flags
119 120 121 |
# File 'lib/fontisan/woff2/directory.rb', line 119 def transform_version (flags >> 6) & 0x03 end |
#transformable? ⇒ Boolean
Check if table can be transformed (glyf, loca, hmtx)
152 153 154 |
# File 'lib/fontisan/woff2/directory.rb', line 152 def transformable? %w[glyf loca hmtx].include?(tag) end |
#transformed? ⇒ Boolean
Whether this entry will be serialized with a transformLength field.
Per WOFF2 spec section 4.1, transformLength is encoded when the
table is in a transformed format. For loca the value is always 0
(spec section 5.3), so we cannot use .positive? to detect the
transformed state — the encoder explicitly sets transform_length
(even to 0) when emitting a transformed entry, and leaves it nil
otherwise.
112 113 114 |
# File 'lib/fontisan/woff2/directory.rb', line 112 def transformed? !transform_length.nil? end |