Class: Fontisan::Woff2::HmtxTransformer
- Inherits:
-
Object
- Object
- Fontisan::Woff2::HmtxTransformer
- Defined in:
- lib/fontisan/woff2/hmtx_transformer.rb
Overview
Reconstructs hmtx table from WOFF2 transformed format
WOFF2 hmtx transformation optimizes horizontal metrics by:
-
Using variable-length encoding for advance widths
-
Optionally deriving LSB from glyf bounding boxes
-
Omitting redundant trailing advance widths
Constant Summary collapse
- HMTX_FLAG_EXPLICIT_ADVANCE_WIDTHS =
Flags for hmtx transformation
0x01- HMTX_FLAG_EXPLICIT_LSB_VALUES =
0x02- HMTX_FLAG_SYMMETRIC =
0x04
Class Method Summary collapse
-
.build_hmtx_table(advance_widths, lsbs, num_h_metrics, num_glyphs) ⇒ String
Build standard hmtx table format.
-
.read_255_uint16(io) ⇒ Integer
Read variable-length 255UInt16 integer.
- .read_int16(io) ⇒ Object
- .read_uint16(io) ⇒ Object
-
.read_uint8(io) ⇒ Object
Helper methods for reading binary data.
-
.reconstruct(transformed_data, num_glyphs, num_h_metrics, glyf_lsbs = nil) ⇒ String
Reconstruct hmtx table from transformed data.
Class Method Details
.build_hmtx_table(advance_widths, lsbs, num_h_metrics, num_glyphs) ⇒ String
Build standard hmtx table format
Standard hmtx format:
-
longHorMetric (advanceWidth, lsb pairs)
-
int16[numGlyphs - numberOfHMetrics] (additional LSBs)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/fontisan/woff2/hmtx_transformer.rb', line 126 def self.build_hmtx_table(advance_widths, lsbs, num_h_metrics, num_glyphs) data = +"" # Write longHorMetric array (advanceWidth + lsb pairs) num_h_metrics.times do |i| advance_width = advance_widths[i] || advance_widths.last lsb = lsbs[i] || 0 data << [advance_width].pack("n") # uint16 advanceWidth data << [lsb].pack("n") # int16 lsb end # Write remaining LSB values # These glyphs all share the last advance width from the array (num_h_metrics...num_glyphs).each do |i| lsb = lsbs[i] || 0 data << [lsb].pack("n") # int16 lsb end data end |
.read_255_uint16(io) ⇒ Integer
Read variable-length 255UInt16 integer
Format from WOFF2 spec:
-
value < 253: one byte
-
value == 253: 253 + next uint16
-
value == 254: 253 * 2 + next uint16
-
value == 255: 253 * 3 + next uint16
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fontisan/woff2/hmtx_transformer.rb', line 100 def self.read_255_uint16(io) code = read_uint8(io) case code when 255 759 + read_uint16(io) # 253 * 3 + value when 254 506 + read_uint16(io) # 253 * 2 + value when 253 253 + read_uint16(io) else code end end |
.read_int16(io) ⇒ Object
158 159 160 161 |
# File 'lib/fontisan/woff2/hmtx_transformer.rb', line 158 def self.read_int16(io) value = read_uint16(io) value > 0x7FFF ? value - 0x10000 : value end |
.read_uint16(io) ⇒ Object
154 155 156 |
# File 'lib/fontisan/woff2/hmtx_transformer.rb', line 154 def self.read_uint16(io) io.read(2)&.unpack1("n") || raise(EOFError, "Unexpected end of stream") end |
.read_uint8(io) ⇒ Object
Helper methods for reading binary data
150 151 152 |
# File 'lib/fontisan/woff2/hmtx_transformer.rb', line 150 def self.read_uint8(io) io.read(1)&.unpack1("C") || raise(EOFError, "Unexpected end of stream") end |
.reconstruct(transformed_data, num_glyphs, num_h_metrics, glyf_lsbs = nil) ⇒ String
Reconstruct hmtx table from transformed data
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/fontisan/woff2/hmtx_transformer.rb', line 36 def self.reconstruct(transformed_data, num_glyphs, num_h_metrics, glyf_lsbs = nil) io = StringIO.new(transformed_data) # Read transformation flags flags = read_uint8(io) # Read advance widths advance_widths = [] if (flags & HMTX_FLAG_EXPLICIT_ADVANCE_WIDTHS).zero? # Proportional encoding - read deltas # First advance width is explicit first_advance = read_255_uint16(io) advance_widths << first_advance # Remaining are deltas from previous (num_h_metrics - 1).times do delta = read_int16(io) advance_widths << (advance_widths.last + delta) end else # Explicit advance widths in transformed format num_h_metrics.times do advance_widths << read_255_uint16(io) end end # Read LSB values lsbs = [] if (flags & HMTX_FLAG_EXPLICIT_LSB_VALUES) != 0 # Explicit LSB values num_glyphs.times do lsbs << read_int16(io) end elsif glyf_lsbs # Use LSB values from glyf bounding boxes lsbs = glyf_lsbs else # Need to read LSB values for long metrics num_h_metrics.times do lsbs << read_int16(io) end # Remaining LSBs for glyphs that share the last advance width (num_glyphs - num_h_metrics).times do lsbs << read_int16(io) end end # Build standard hmtx table build_hmtx_table(advance_widths, lsbs, num_h_metrics, num_glyphs) end |