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.



69
70
71
72
73
74
75
# File 'lib/fontisan/woff2/directory.rb', line 69

def initialize
  @tag = nil
  @flags = 0
  @orig_length = 0
  @transform_length = nil
  @offset = 0
end

Instance Attribute Details

#flagsObject

Calculated during encoding



67
68
69
# File 'lib/fontisan/woff2/directory.rb', line 67

def flags
  @flags
end

#offsetObject

Calculated during encoding



67
68
69
# File 'lib/fontisan/woff2/directory.rb', line 67

def offset
  @offset
end

#orig_lengthObject

Calculated during encoding



67
68
69
# File 'lib/fontisan/woff2/directory.rb', line 67

def orig_length
  @orig_length
end

#tagObject

Calculated during encoding



67
68
69
# File 'lib/fontisan/woff2/directory.rb', line 67

def tag
  @tag
end

#transform_lengthObject

Calculated during encoding



67
68
69
# File 'lib/fontisan/woff2/directory.rb', line 67

def transform_length
  @transform_length
end

Instance Method Details

#calculate_flagsInteger

Calculate flags byte for this entry

Returns:

  • (Integer)

    Flags byte (0-255)



80
81
82
83
84
85
86
# File 'lib/fontisan/woff2/directory.rb', line 80

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 transformation version for this table

Returns the appropriate version based on:

  1. Whether table has transform_length set (is transformed)
  2. Which table it is (glyf/loca vs hmtx vs other)

Returns:

  • (Integer)

    Transform version (0-3)



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fontisan/woff2/directory.rb', line 123

def determine_transform_version
  if transformed?
    # Table IS transformed - use appropriate transform version
    case tag
    when "glyf", "loca"
      TRANSFORM_GLYF_LOCA  # Version 0 for transformed glyf/loca
    when "hmtx"
      TRANSFORM_HMTX       # Version 1 for transformed hmtx
    else
      TRANSFORM_NONE       # Shouldn't happen, but use safe default
    end
  else
    # Table is NOT transformed - use version that indicates no transformation
    case tag
    when "glyf", "loca"
      # For glyf/loca, version 0 means transformed
      # so use version 3 to indicate NOT transformed
      TRANSFORM_NONE # Version 3
    when "hmtx"
      # For hmtx, version 1 means transformed
      # so use version 0 to indicate NOT transformed
      0
    else
      # All other tables use version 0 (no transformation)
      0
    end
  end
end

#known_tag?Boolean

Check if table uses a known tag

Returns:

  • (Boolean)

    True if known tag



91
92
93
# File 'lib/fontisan/woff2/directory.rb', line 91

def known_tag?
  KNOWN_TAGS.include?(tag)
end

#serialized_sizeInteger

Calculate size of this entry when serialized

Returns:

  • (Integer)

    Size in bytes



162
163
164
165
166
167
168
# File 'lib/fontisan/woff2/directory.rb', line 162

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)



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

def tag_index
  flags & 0x3F
end

#transform_versionInteger

Get transformation version from flags

Returns:

  • (Integer)

    Transform version (0-3)



105
106
107
# File 'lib/fontisan/woff2/directory.rb', line 105

def transform_version
  (flags >> 6) & 0x03
end

#transformable?Boolean

Check if table can be transformed (glyf, loca, hmtx)

Returns:

  • (Boolean)

    True if transformable



155
156
157
# File 'lib/fontisan/woff2/directory.rb', line 155

def transformable?
  %w[glyf loca hmtx].include?(tag)
end

#transformed?Boolean

Check if table is transformed

Returns:

  • (Boolean)

    True if transformed



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

def transformed?
  !transform_length.nil? && transform_length.positive?
end