Class: Fontisan::Woff2::Directory::Entry

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeEntry

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

#flagsObject

Calculated during encoding



74
75
76
# File 'lib/fontisan/woff2/directory.rb', line 74

def flags
  @flags
end

#offsetObject

Calculated during encoding



74
75
76
# File 'lib/fontisan/woff2/directory.rb', line 74

def offset
  @offset
end

#orig_lengthObject

Calculated during encoding



74
75
76
# File 'lib/fontisan/woff2/directory.rb', line 74

def orig_length
  @orig_length
end

#tagObject

Calculated during encoding



74
75
76
# File 'lib/fontisan/woff2/directory.rb', line 74

def tag
  @tag
end

#transform_lengthObject

Calculated during encoding



74
75
76
# File 'lib/fontisan/woff2/directory.rb', line 74

def transform_length
  @transform_length
end

Instance Method Details

#calculate_flagsInteger

Calculate flags byte for this entry

Returns:

  • (Integer)

    Flags byte (0-255)



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_versionInteger

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?.

Returns:

  • (Integer)

    Transform version (0-3)



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

Returns:

  • (Boolean)

    True if known tag



98
99
100
# File 'lib/fontisan/woff2/directory.rb', line 98

def known_tag?
  KNOWN_TAGS.include?(tag)
end

#serialized_sizeInteger

Calculate size of this entry when serialized

Returns:

  • (Integer)

    Size in bytes



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_indexInteger

Get tag index from flags

Returns:

  • (Integer)

    Tag index (0-63)



126
127
128
# File 'lib/fontisan/woff2/directory.rb', line 126

def tag_index
  flags & 0x3F
end

#transform_versionInteger

Get transformation version from flags

Returns:

  • (Integer)

    Transform version (0-3)



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)

Returns:

  • (Boolean)

    True if transformable



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.

Returns:

  • (Boolean)

    True if a transformLength field should be written



112
113
114
# File 'lib/fontisan/woff2/directory.rb', line 112

def transformed?
  !transform_length.nil?
end