Class: Fontisan::Woff2Font

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/woff2_font.rb

Overview

Web Open Font Format 2.0 (WOFF2) font loader

This class manages WOFF2 font files and provides access to decompressed tables and transformed data.

Examples:

Reading a WOFF2 font

font = Woff2Font.from_file("font.woff2")
puts font.header.flavor
puts font.table_names

Defined Under Namespace

Classes: IOSource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWoff2Font

Returns a new instance of Woff2Font.



90
91
92
93
94
95
96
97
# File 'lib/fontisan/woff2_font.rb', line 90

def initialize
  @header = nil
  @table_entries = []
  @decompressed_tables = {}
  @parsed_tables = {}
  @io_source = nil
  @underlying_font = nil # Store the actual TrueTypeFont/OpenTypeFont
end

Instance Attribute Details

#decompressed_tablesObject

Allow both reading and setting for table delegation



88
89
90
# File 'lib/fontisan/woff2_font.rb', line 88

def decompressed_tables
  @decompressed_tables
end

#headerObject

Allow both reading and setting for table delegation



88
89
90
# File 'lib/fontisan/woff2_font.rb', line 88

def header
  @header
end

#io_sourceObject

Allow both reading and setting for table delegation



88
89
90
# File 'lib/fontisan/woff2_font.rb', line 88

def io_source
  @io_source
end

#parsed_tablesObject

Allow both reading and setting for table delegation



88
89
90
# File 'lib/fontisan/woff2_font.rb', line 88

def parsed_tables
  @parsed_tables
end

#table_entriesObject

Allow both reading and setting for table delegation



88
89
90
# File 'lib/fontisan/woff2_font.rb', line 88

def table_entries
  @table_entries
end

#underlying_fontObject

Allow both reading and setting for table delegation



88
89
90
# File 'lib/fontisan/woff2_font.rb', line 88

def underlying_font
  @underlying_font
end

Class Method Details

.apply_transformations!(table_entries, decompressed_tables) ⇒ void

This method returns an undefined value.

Apply table transformations for glyf/loca/hmtx tables

Parameters:

  • table_entries (Array<Woff2TableDirectoryEntry>)

    Table entries

  • decompressed_tables (Hash<String, String>)

    Decompressed tables



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/fontisan/woff2_font.rb', line 518

def self.apply_transformations!(table_entries, decompressed_tables)
  # Find entries that need transformation
  glyf_entry = table_entries.find { |e| e.tag == "glyf" }
  hmtx_entry = table_entries.find { |e| e.tag == "hmtx" }

  # Get required metadata for transformations
  maxp_data = decompressed_tables["maxp"]
  hhea_data = decompressed_tables["hhea"]

  return unless maxp_data && hhea_data

  # Parse num_glyphs from maxp table
  # maxp format: version(4) + numGlyphs(2) + ...
  num_glyphs = maxp_data[4, 2].unpack1("n")

  # Parse numberOfHMetrics from hhea table
  # hhea format: ... + numberOfHMetrics(2) at offset 34
  number_of_h_metrics = hhea_data[34, 2].unpack1("n")

  # Check if this is a variable font by checking for fvar table
  variable_font = table_entries.any? { |e| e.tag == "fvar" }

  # Transform glyf/loca if needed
  # transform_length is only set when table is actually transformed
  # Check that transform_length exists and is greater than 0
  if glyf_entry&.instance_variable_defined?(:@transform_length) &&
      glyf_entry.transform_length&.positive?
    transformed_glyf = decompressed_tables["glyf"]

    if transformed_glyf
      result = Woff2::GlyfTransformer.reconstruct(
        transformed_glyf,
        num_glyphs,
        variable_font: variable_font,
      )
      decompressed_tables["glyf"] = result[:glyf]
      decompressed_tables["loca"] = result[:loca]
    end
  end

  # Transform hmtx if needed
  # transform_length is only set when table is actually transformed
  # Check that transform_length exists and is greater than 0
  if hmtx_entry&.instance_variable_defined?(:@transform_length) &&
      hmtx_entry.transform_length&.positive?
    transformed_hmtx = decompressed_tables["hmtx"]

    if transformed_hmtx
      decompressed_tables["hmtx"] = Woff2::HmtxTransformer.reconstruct(
        transformed_hmtx,
        num_glyphs,
        number_of_h_metrics,
      )
    end
  end
end

.build_sfnt_in_memory(header, table_entries, decompressed_tables) ⇒ String

Build SFNT binary structure in memory

Parameters:

Returns:

  • (String)

    Complete SFNT binary data



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/fontisan/woff2_font.rb', line 624

def self.build_sfnt_in_memory(header, table_entries, decompressed_tables)
  sfnt_data = +""

  # Calculate offset table fields
  num_tables = table_entries.length
  entry_selector = (Math.log(num_tables) / Math.log(2)).floor
  search_range = (2**entry_selector) * 16
  range_shift = num_tables * 16 - search_range

  # Write offset table
  sfnt_data << [header.flavor].pack("N")
  sfnt_data << [num_tables].pack("n")
  sfnt_data << [search_range].pack("n")
  sfnt_data << [entry_selector].pack("n")
  sfnt_data << [range_shift].pack("n")

  # Calculate table offsets
  offset = 12 + (num_tables * 16) # Header + directory
  table_records = []

  table_entries.each do |entry|
    tag = entry.tag
    data = decompressed_tables[tag]
    next unless data

    length = data.bytesize

    # Calculate checksum
    checksum = Utilities::ChecksumCalculator.calculate_table_checksum(data)

    table_records << {
      tag: tag,
      checksum: checksum,
      offset: offset,
      length: length,
      data: data,
    }

    # Update offset for next table (with padding)
    offset += length
    padding = (Constants::TABLE_ALIGNMENT - (length % Constants::TABLE_ALIGNMENT)) %
      Constants::TABLE_ALIGNMENT
    offset += padding
  end

  # Write table directory
  table_records.each do |record|
    sfnt_data << record[:tag].ljust(4, "\x00")
    sfnt_data << [record[:checksum]].pack("N")
    sfnt_data << [record[:offset]].pack("N")
    sfnt_data << [record[:length]].pack("N")
  end

  # Write table data with padding
  table_records.each do |record|
    sfnt_data << record[:data]

    # Add padding
    padding = (Constants::TABLE_ALIGNMENT - (record[:length] % Constants::TABLE_ALIGNMENT)) %
      Constants::TABLE_ALIGNMENT
    sfnt_data << ("\x00" * padding) if padding.positive?
  end

  # Update checksumAdjustment in head table
  update_checksum_in_memory(sfnt_data, table_records)

  sfnt_data
end

.calculate_table_directory_size(table_entries) ⇒ Integer

Calculate size of table directory

Parameters:

Returns:

  • (Integer)

    Size in bytes



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/fontisan/woff2_font.rb', line 579

def self.calculate_table_directory_size(table_entries)
  size = 0
  table_entries.each do |entry|
    size += 1 # flags byte

    # Tag (4 bytes if custom, 0 if known)
    tag_index = entry.flags & 0x3F
    size += 4 if tag_index == 0x3F

    # orig_length (UIntBase128) - estimate
    size += uint_base128_size(entry.orig_length)

    # transform_length if present
    if entry.transform_version && !entry.transform_version.nil?
      size += uint_base128_size(entry.transform_length)
    end
  end
  size
end

.decompress_tables(io, header, table_entries) ⇒ Hash<String, String>

Decompress tables from WOFF2 compressed data block

Parameters:

Returns:

  • (Hash<String, String>)

    Map of tag to decompressed data



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/fontisan/woff2_font.rb', line 490

def self.decompress_tables(io, header, table_entries)
  # IO stream is already positioned at compressed data after reading table directory
  # No need to seek - just read from current position
  compressed_data = io.read(header.total_compressed_size)

  # Decompress entire data block with Brotli
  decompressed_data = Brotli.inflate(compressed_data)

  # Split decompressed data into individual tables
  decompressed_tables = {}
  offset = 0

  table_entries.each do |entry|
    table_size = entry.transform_length || entry.orig_length
    table_data = decompressed_data[offset, table_size]
    offset += table_size

    decompressed_tables[entry.tag] = table_data
  end

  decompressed_tables
end

.from_file(path, mode: LoadingModes::FULL, lazy: false) ⇒ Woff2Font

Read WOFF2 font from a file and return Woff2Font instance

Parameters:

  • path (String)

    Path to the WOFF2 file

  • mode (Symbol) (defaults to: LoadingModes::FULL)

    Loading mode (:metadata or :full)

  • lazy (Boolean) (defaults to: false)

    If true, load tables on demand

Returns:

Raises:

  • (ArgumentError)

    if path is nil or empty

  • (Errno::ENOENT)

    if file does not exist

  • (InvalidFontError)

    if file format is invalid



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/fontisan/woff2_font.rb', line 307

def self.from_file(path, mode: LoadingModes::FULL, lazy: false)
  if path.nil? || path.to_s.empty?
    raise ArgumentError, "path cannot be nil or empty"
  end
  raise Errno::ENOENT, "File not found: #{path}" unless File.exist?(path)

  woff2 = new
  woff2.io_source = IOSource.new(path)

  File.open(path, "rb") do |io|
    # Read header to determine font flavor
    woff2.header = Woff2::Woff2Header.read(io)

    # Validate signature
    unless woff2.header.signature == Woff2::Woff2Header::SIGNATURE
      raise InvalidFontError,
            "Invalid WOFF2 signature: expected 0x#{Woff2::Woff2Header::SIGNATURE.to_s(16)}, " \
            "got 0x#{woff2.header.signature.to_i.to_s(16)}"
    end

    # Read table directory
    woff2.table_entries = read_table_directory_from_io(io, woff2.header)

    # Decompress table data
    woff2.decompressed_tables = decompress_tables(io, woff2.header,
                                                  woff2.table_entries)

    # Apply table transformations if present
    apply_transformations!(woff2.table_entries, woff2.decompressed_tables)

    # Build SFNT structure in memory
    sfnt_data = build_sfnt_in_memory(woff2.header, woff2.table_entries,
                                     woff2.decompressed_tables)

    # Create StringIO for reading
    sfnt_io = StringIO.new(sfnt_data)
    sfnt_io.rewind

    # Parse tables based on font type
    if woff2.truetype?
      font = TrueTypeFont.read(sfnt_io)
      font.initialize_storage
      font.loading_mode = mode
      font.lazy_load_enabled = lazy

      # Create fresh StringIO for table data reading
      table_io = StringIO.new(sfnt_data)
      font.read_table_data(table_io)

      # Store underlying font for table access delegation
      woff2.underlying_font = font
      woff2.parsed_tables = font.parsed_tables
    elsif woff2.cff?
      font = OpenTypeFont.read(sfnt_io)
      font.initialize_storage
      font.loading_mode = mode
      font.lazy_load_enabled = lazy

      # Create fresh StringIO for table data reading
      table_io = StringIO.new(sfnt_data)
      font.read_table_data(table_io)

      # Store underlying font for table access delegation
      woff2.underlying_font = font
      woff2.parsed_tables = font.parsed_tables
    else
      raise InvalidFontError,
            "Unknown WOFF2 flavor: 0x#{woff2.header.flavor.to_s(16)}"
    end
  end

  woff2
rescue BinData::ValidityError, EOFError => e
  raise InvalidFontError, "Invalid WOFF2 file: #{e.message}"
end

.read_table_directory_from_io(io, header) ⇒ Array<Woff2TableDirectoryEntry>

Read table directory from IO

Parameters:

Returns:



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/fontisan/woff2_font.rb', line 388

def self.read_table_directory_from_io(io, header)
  table_entries = []

  header.num_tables.times do
    entry = Woff2TableDirectoryEntry.new

    # Read flags byte with nil check
    flags_data = io.read(1)
    if flags_data.nil?
      raise EOFError,
            "Unexpected EOF while reading table directory flags"
    end

    flags = flags_data.unpack1("C")
    entry.flags = flags

    # Determine tag
    tag_index = flags & 0x3F
    if tag_index == 0x3F
      # Custom tag (4 bytes)
      tag_data = io.read(4)
      if tag_data.nil? || tag_data.bytesize < 4
        raise EOFError,
              "Unexpected EOF while reading custom tag"
      end

      entry.tag = tag_data.force_encoding("UTF-8")
    else
      # Known tag from table
      entry.tag = Woff2TableDirectoryEntry::KNOWN_TAGS[tag_index]
      unless entry.tag
        raise InvalidFontError, "Invalid table tag index: #{tag_index}"
      end
    end

    # Read orig_length (UIntBase128)
    entry.orig_length = read_uint_base128_from_io(io)

    # Determine if transformLength should be read
    # According to WOFF2 spec section 4.2:
    # - transformLength is ONLY present when table is actually transformed
    # - For glyf/loca: transformation is indicated by transform_version = 0
    # - For hmtx: transformation is indicated by transform_version = 1
    # - For all other tables: no transformation, no transformLength
    transform_version = (flags >> 6) & 0x03

    # transformLength is present when table is actually transformed
    # glyf/loca use version 0 for transformation, hmtx uses version 1
    has_transform_length = if ["glyf", "loca"].include?(entry.tag)
                             # For glyf/loca, version 0 means transformed
                             transform_version.zero?
                           elsif entry.tag == "hmtx"
                             # For hmtx, version 1 means transformed
                             transform_version == 1
                           else
                             false
                           end

    if has_transform_length
      entry.transform_length = read_uint_base128_from_io(io)
      entry.transform_version = transform_version
    end

    table_entries << entry
  end

  table_entries
end

.read_uint_base128_from_io(io) ⇒ Integer

Read variable-length UIntBase128 integer from IO

Parameters:

  • io (IO)

    Open file handle

Returns:

  • (Integer)

    The decoded integer value

Raises:



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/fontisan/woff2_font.rb', line 461

def self.read_uint_base128_from_io(io)
  result = 0
  5.times do
    byte_data = io.read(1)
    if byte_data.nil?
      raise EOFError,
            "Unexpected EOF while reading UIntBase128"
    end

    byte = byte_data.unpack1("C")

    # Continue if high bit is set
    if (byte & 0x80).zero?
      return (result << 7) | byte
    else
      result = (result << 7) | (byte & 0x7F)
    end
  end

  # If we're here, the encoding is invalid
  raise InvalidFontError, "Invalid UIntBase128 encoding"
end

.uint_base128_size(value) ⇒ Integer

Estimate size of UIntBase128 encoded value

Parameters:

  • value (Integer)

    The value to encode

Returns:

  • (Integer)

    Estimated size in bytes



603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/fontisan/woff2_font.rb', line 603

def self.uint_base128_size(value)
  return 1 if value < 128

  bytes = 0
  v = value
  while v.positive?
    bytes += 1
    v >>= 7
  end
  [
    bytes,
    5,
  ].min # Max 5 bytes
end

.update_checksum_in_memory(sfnt_data, table_records) ⇒ void

This method returns an undefined value.

Update checksumAdjustment field in head table in memory

Parameters:

  • sfnt_data (String)

    The SFNT binary data

  • table_records (Array<Hash>)

    Table records with offsets



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/fontisan/woff2_font.rb', line 698

def self.update_checksum_in_memory(sfnt_data, table_records)
  # Find head table record
  head_record = table_records.find { |r| r[:tag] == Constants::HEAD_TAG }
  return unless head_record

  # Zero out checksumAdjustment field first
  head_offset = head_record[:offset]
  sfnt_data[head_offset + 8, 4] = "\x00\x00\x00\x00"

  # Calculate file checksum
  checksum = 0
  sfnt_data.bytes.each_slice(4) do |bytes|
    word = bytes.pack("C*").ljust(4, "\x00").unpack1("N")
    checksum = (checksum + word) & 0xFFFFFFFF
  end

  # Calculate adjustment
  adjustment = (0xB1B0AFBA - checksum) & 0xFFFFFFFF

  # Write adjustment to head table
  sfnt_data[head_offset + 8, 4] = [adjustment].pack("N")
end

Instance Method Details

#cff?Boolean

Check if font has CFF flavor

Returns:

  • (Boolean)


113
114
115
116
117
# File 'lib/fontisan/woff2_font.rb', line 113

def cff?
  return false unless @header

  [Constants::SFNT_VERSION_OTTO, 0x4F54544F].include?(@header.flavor)
end

#family_nameObject

Get font family name



257
258
259
260
# File 'lib/fontisan/woff2_font.rb', line 257

def family_name
  name_table = table("name")
  name_table&.english_name(Tables::Name::FAMILY)
end

#find_table_entry(tag) ⇒ Object

Find table entry by tag



149
150
151
# File 'lib/fontisan/woff2_font.rb', line 149

def find_table_entry(tag)
  @table_entries.find { |entry| entry.tag == tag }
end

#full_nameObject

Get full font name



269
270
271
272
# File 'lib/fontisan/woff2_font.rb', line 269

def full_name
  name_table = table("name")
  name_table&.english_name(Tables::Name::FULL_NAME)
end

#has_table?(tag) ⇒ Boolean

Check if table exists

Returns:

  • (Boolean)


144
145
146
# File 'lib/fontisan/woff2_font.rb', line 144

def has_table?(tag)
  @table_entries.any? { |entry| entry.tag == tag }
end

#initialize_storageObject

Initialize storage hashes



100
101
102
103
# File 'lib/fontisan/woff2_font.rb', line 100

def initialize_storage
  @decompressed_tables ||= {}
  @initialize_storage ||= {}
end

#metadataObject

Get metadata (if present)



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fontisan/woff2_font.rb', line 238

def 
  return nil unless @header&.meta_length&.positive?
  return nil unless @io_source

  begin
    File.open(@io_source.path, "rb") do |io|
      io.seek(@header.meta_offset)
      compressed_meta = io.read(@header.meta_length)
      Brotli.inflate(compressed_meta)
    end
  rescue StandardError => e
    warn "Failed to decompress metadata: #{e.message}"
    nil
  end
end

#post_script_nameObject

Get PostScript name



275
276
277
278
# File 'lib/fontisan/woff2_font.rb', line 275

def post_script_name
  name_table = table("name")
  name_table&.english_name(Tables::Name::POSTSCRIPT_NAME)
end

#preferred_family_nameObject

Get preferred family name



281
282
283
284
# File 'lib/fontisan/woff2_font.rb', line 281

def preferred_family_name
  name_table = table("name")
  name_table&.english_name(Tables::Name::PREFERRED_FAMILY)
end

#preferred_subfamily_nameObject

Get preferred subfamily name



287
288
289
290
# File 'lib/fontisan/woff2_font.rb', line 287

def preferred_subfamily_name
  name_table = table("name")
  name_table&.english_name(Tables::Name::PREFERRED_SUBFAMILY)
end

#subfamily_nameObject

Get font subfamily name



263
264
265
266
# File 'lib/fontisan/woff2_font.rb', line 263

def subfamily_name
  name_table = table("name")
  name_table&.english_name(Tables::Name::SUBFAMILY)
end

#table(tag) ⇒ Object

Get parsed table object



186
187
188
189
190
191
192
193
194
195
# File 'lib/fontisan/woff2_font.rb', line 186

def table(tag)
  # Delegate to underlying font if available
  return @underlying_font.table(tag) if @underlying_font

  # Fallback to parsed_tables hash
  # Normalize tag to UTF-8 string for hash lookup
  # Use dup to create mutable copy since force_encoding modifies in place
  tag_key = tag.to_s.dup.force_encoding("UTF-8")
  @parsed_tables[tag_key]
end

#table_data(tag = nil) ⇒ String, ...

Get decompressed table data

Parameters:

  • tag (String, nil) (defaults to: nil)

    The table tag (optional)

Returns:

  • (String, Hash, nil)

    Table data if tag provided, or hash of all tables if no tag



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/fontisan/woff2_font.rb', line 162

def table_data(tag = nil)
  # If no tag provided, return all tables
  if tag.nil?
    # First try underlying font's table data if available
    if @underlying_font.respond_to?(:table_data)
      return @underlying_font.table_data
    end

    # Fallback to decompressed_tables
    return @decompressed_tables.dup
  end

  # Tag provided - return specific table
  # First try underlying font's table data if available
  if @underlying_font.respond_to?(:table_data)
    underlying_data = @underlying_font.table_data[tag]
    return underlying_data if underlying_data
  end

  # Fallback to decompressed_tables
  @decompressed_tables[tag]
end

#table_namesObject

Get list of table tags



154
155
156
# File 'lib/fontisan/woff2_font.rb', line 154

def table_names
  @table_entries.map(&:tag)
end

#to_otf(output_path) ⇒ Object

Convert to OTF



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/fontisan/woff2_font.rb', line 218

def to_otf(output_path)
  unless cff?
    raise InvalidFontError,
          "Cannot convert to OTF: font is not CFF flavored"
  end

  # Build SFNT and create OpenTypeFont
  sfnt_data = self.class.build_sfnt_in_memory(@header, @table_entries,
                                              @decompressed_tables)
  sfnt_io = StringIO.new(sfnt_data)

  # Create actual OpenTypeFont and save for table delegation
  @underlying_font = OpenTypeFont.read(sfnt_io)
  @underlying_font.initialize_storage
  @underlying_font.read_table_data(sfnt_io)

  FontWriter.write_to_file(@underlying_font.tables, output_path)
end

#to_ttf(output_path) ⇒ Object

Convert to TTF



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fontisan/woff2_font.rb', line 198

def to_ttf(output_path)
  unless truetype?
    raise InvalidFontError,
          "Cannot convert to TTF: font is not TrueType flavored"
  end

  # Build SFNT and create TrueTypeFont
  sfnt_data = self.class.build_sfnt_in_memory(@header, @table_entries,
                                              @decompressed_tables)
  sfnt_io = StringIO.new(sfnt_data)

  # Create actual TrueTypeFont and save for table delegation
  @underlying_font = TrueTypeFont.read(sfnt_io)
  @underlying_font.initialize_storage
  @underlying_font.read_table_data(sfnt_io)

  FontWriter.write_to_file(@underlying_font.tables, output_path)
end

#truetype?Boolean

Check if font has TrueType flavor

Returns:

  • (Boolean)


106
107
108
109
110
# File 'lib/fontisan/woff2_font.rb', line 106

def truetype?
  return false unless @header

  [Constants::SFNT_VERSION_TRUETYPE, 0x00010000].include?(@header.flavor)
end

#units_per_emObject

Get units per em



293
294
295
296
# File 'lib/fontisan/woff2_font.rb', line 293

def units_per_em
  head = table("head")
  head&.units_per_em
end

#valid?Boolean

Check if font is valid

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
# File 'lib/fontisan/woff2_font.rb', line 134

def valid?
  return false unless @header
  return false unless @header.signature == Woff2::Woff2Header::SIGNATURE
  return false unless @header.num_tables == @table_entries.length
  return false unless has_table?("head")

  true
end

#validate_signature!Object

Validate WOFF2 signature



127
128
129
130
131
# File 'lib/fontisan/woff2_font.rb', line 127

def validate_signature!
  unless @header && @header.signature == Woff2::Woff2Header::SIGNATURE
    raise InvalidFontError, "Invalid WOFF2 signature"
  end
end

#variable_font?Boolean

Check if font is a variable font

Returns:

  • (Boolean)

    true if font has fvar table (variable font)



122
123
124
# File 'lib/fontisan/woff2_font.rb', line 122

def variable_font?
  has_table?("fvar")
end