Class: Fontisan::Woff2Font

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



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

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



86
87
88
# File 'lib/fontisan/woff2_font.rb', line 86

def decompressed_tables
  @decompressed_tables
end

#headerObject

Allow both reading and setting for table delegation



86
87
88
# File 'lib/fontisan/woff2_font.rb', line 86

def header
  @header
end

#io_sourceObject

Allow both reading and setting for table delegation



86
87
88
# File 'lib/fontisan/woff2_font.rb', line 86

def io_source
  @io_source
end

#parsed_tablesObject

Allow both reading and setting for table delegation



86
87
88
# File 'lib/fontisan/woff2_font.rb', line 86

def parsed_tables
  @parsed_tables
end

#table_entriesObject

Allow both reading and setting for table delegation



86
87
88
# File 'lib/fontisan/woff2_font.rb', line 86

def table_entries
  @table_entries
end

#underlying_fontObject

Allow both reading and setting for table delegation



86
87
88
# File 'lib/fontisan/woff2_font.rb', line 86

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



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
574
575
576
577
578
579
# File 'lib/fontisan/woff2_font.rb', line 541

def self.apply_transformations!(table_entries, decompressed_tables)
  glyf_entry = table_entries.find { |e| e.tag == "glyf" }
  hmtx_entry = table_entries.find { |e| e.tag == "hmtx" }

  maxp_data = decompressed_tables["maxp"]
  hhea_data = decompressed_tables["hhea"]
  head_data = decompressed_tables["head"]

  return unless maxp_data && hhea_data && head_data

  # maxp format: version(4) + numGlyphs(2) + ...
  num_glyphs = maxp_data[4, 2].unpack1("n")
  # hhea format: ... + numberOfHMetrics(2) at offset 34
  number_of_h_metrics = hhea_data[34, 2].unpack1("n")
  # head format: ... + indexToLocFormat(2) at offset 50
  index_format = head_data[50, 2].unpack1("n")

  # Reconstruct glyf/loca per spec section 5.1/5.3 when glyf is
  # transformed. Per spec section 4.1, transformLength is present IFF
  # the table is transformed, so a non-nil value indicates transform.
  if glyf_entry&.transform_length && decompressed_tables["glyf"]
    result = Woff2::GlyfLocaReconstruct.new(
      transformed_glyf: decompressed_tables["glyf"],
      num_glyphs:,
      index_format:,
    ).reconstruct
    decompressed_tables["glyf"] = result[:glyf]
    decompressed_tables["loca"] = result[:loca]
  end

  # Reconstruct hmtx per spec section 5.4 when hmtx is transformed.
  if hmtx_entry&.transform_length && decompressed_tables["hmtx"]
    decompressed_tables["hmtx"] = Woff2::HmtxTransformer.reconstruct(
      decompressed_tables["hmtx"],
      num_glyphs,
      number_of_h_metrics,
    )
  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



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
692
693
694
695
696
697
698
699
# File 'lib/fontisan/woff2_font.rb', line 630

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 (all entries first)
  # rubocop:disable Style/CombinableLoops - Must write directory entries first, then data
  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

  # Then write all 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
  # rubocop:enable Style/CombinableLoops

  # 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



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/fontisan/woff2_font.rb', line 585

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



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/fontisan/woff2_font.rb', line 513

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



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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/fontisan/woff2_font.rb', line 330

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
  rescue BinData::ValidityError, EOFError => e
    raise InvalidFontError, "Invalid WOFF2 file: #{e.message}"
  end

  woff2
end

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

Read table directory from IO

Parameters:

Returns:



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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/fontisan/woff2_font.rb', line 411

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:



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/fontisan/woff2_font.rb', line 484

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



609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/fontisan/woff2_font.rb', line 609

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



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
# File 'lib/fontisan/woff2_font.rb', line 706

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)


111
112
113
114
115
# File 'lib/fontisan/woff2_font.rb', line 111

def cff?
  return false unless @header

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

#collection?Boolean

Whether this object represents a font collection rather than a single font. Each font class is the authority on this question.

Returns:

  • (Boolean)


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

def collection? = false

#family_nameObject

Get font family name



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

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



172
173
174
# File 'lib/fontisan/woff2_font.rb', line 172

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

#formatSymbol

High-level pipeline format identifier. Owned by the font class so the conversion pipeline can dispatch without case statements (OCP).

Returns:

  • (Symbol)

    :woff2



81
# File 'lib/fontisan/woff2_font.rb', line 81

def format = :woff2

#full_nameObject

Get full font name



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

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)


142
143
144
# File 'lib/fontisan/woff2_font.rb', line 142

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

#initialize_storageObject

Initialize storage hashes



98
99
100
101
# File 'lib/fontisan/woff2_font.rb', line 98

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

#metadataObject

Get metadata (if present)



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/fontisan/woff2_font.rb', line 261

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

#outline_typeSymbol

Outline representation, derived from the wrapped SFNT flavor.

Returns:

  • (Symbol)

    :truetype or :cff



167
168
169
# File 'lib/fontisan/woff2_font.rb', line 167

def outline_type
  truetype? ? :truetype : :cff
end

#post_script_nameObject

Get PostScript name



298
299
300
301
# File 'lib/fontisan/woff2_font.rb', line 298

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

#preferred_family_nameObject

Get preferred family name



304
305
306
307
# File 'lib/fontisan/woff2_font.rb', line 304

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

#preferred_subfamily_nameObject

Get preferred subfamily name



310
311
312
313
# File 'lib/fontisan/woff2_font.rb', line 310

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

#subfamily_nameObject

Get font subfamily name



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

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

#table(tag) ⇒ Object

Get parsed table object



209
210
211
212
213
214
215
216
217
218
# File 'lib/fontisan/woff2_font.rb', line 209

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
  tag_key = tag.to_s
  tag_key.force_encoding("UTF-8") unless tag_key.encoding == 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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/fontisan/woff2_font.rb', line 185

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.is_a?(SfntFont)
      return @underlying_font.table_data
    end

    # Fallback to decompressed_tables
    return @decompressed_tables
  end

  # Tag provided - return specific table
  # First try underlying font's table data if available
  if @underlying_font.is_a?(SfntFont)
    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



177
178
179
# File 'lib/fontisan/woff2_font.rb', line 177

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

#to_otf(output_path) ⇒ Object

Convert to OTF



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/fontisan/woff2_font.rb', line 241

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



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

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)


104
105
106
107
108
# File 'lib/fontisan/woff2_font.rb', line 104

def truetype?
  return false unless @header

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

#units_per_emObject

Get units per em



316
317
318
319
# File 'lib/fontisan/woff2_font.rb', line 316

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

#valid?Boolean

Check if font is valid

Returns:

  • (Boolean)


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

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



125
126
127
128
129
# File 'lib/fontisan/woff2_font.rb', line 125

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)



120
121
122
# File 'lib/fontisan/woff2_font.rb', line 120

def variable_font?
  has_table?("fvar")
end

#variation_typeSymbol

Variation profile. WOFF2 wraps a single SFNT font; variation tables, if present on the wrapped font, are reported here.

Returns:

  • (Symbol)

    :static, :gvar, or :cff2



156
157
158
159
160
161
162
# File 'lib/fontisan/woff2_font.rb', line 156

def variation_type
  return :static unless has_table?("fvar")
  return :gvar if has_table?("gvar")
  return :cff2 if has_table?("CFF2")

  :static
end