Class: Fontisan::Woff2Font
- Inherits:
-
Object
- Object
- Fontisan::Woff2Font
- 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.
Defined Under Namespace
Classes: IOSource
Instance Attribute Summary collapse
-
#decompressed_tables ⇒ Object
Allow both reading and setting for table delegation.
-
#header ⇒ Object
Allow both reading and setting for table delegation.
-
#io_source ⇒ Object
Allow both reading and setting for table delegation.
-
#parsed_tables ⇒ Object
Allow both reading and setting for table delegation.
-
#table_entries ⇒ Object
Allow both reading and setting for table delegation.
-
#underlying_font ⇒ Object
Allow both reading and setting for table delegation.
Class Method Summary collapse
-
.apply_transformations!(table_entries, decompressed_tables) ⇒ void
Apply table transformations for glyf/loca/hmtx tables.
-
.build_sfnt_in_memory(header, table_entries, decompressed_tables) ⇒ String
Build SFNT binary structure in memory.
-
.calculate_table_directory_size(table_entries) ⇒ Integer
Calculate size of table directory.
-
.decompress_tables(io, header, table_entries) ⇒ Hash<String, String>
Decompress tables from WOFF2 compressed data block.
-
.from_file(path, mode: LoadingModes::FULL, lazy: false) ⇒ Woff2Font
Read WOFF2 font from a file and return Woff2Font instance.
-
.read_table_directory_from_io(io, header) ⇒ Array<Woff2TableDirectoryEntry>
Read table directory from IO.
-
.read_uint_base128_from_io(io) ⇒ Integer
Read variable-length UIntBase128 integer from IO.
-
.uint_base128_size(value) ⇒ Integer
Estimate size of UIntBase128 encoded value.
-
.update_checksum_in_memory(sfnt_data, table_records) ⇒ void
Update checksumAdjustment field in head table in memory.
Instance Method Summary collapse
-
#cff? ⇒ Boolean
Check if font has CFF flavor.
-
#collection? ⇒ Boolean
Whether this object represents a font collection rather than a single font.
-
#family_name ⇒ Object
Get font family name.
-
#find_table_entry(tag) ⇒ Object
Find table entry by tag.
-
#format ⇒ Symbol
High-level pipeline format identifier.
-
#full_name ⇒ Object
Get full font name.
-
#has_table?(tag) ⇒ Boolean
Check if table exists.
-
#initialize ⇒ Woff2Font
constructor
A new instance of Woff2Font.
-
#initialize_storage ⇒ Object
Initialize storage hashes.
-
#metadata ⇒ Object
Get metadata (if present).
-
#outline_type ⇒ Symbol
Outline representation, derived from the wrapped SFNT flavor.
-
#post_script_name ⇒ Object
Get PostScript name.
-
#preferred_family_name ⇒ Object
Get preferred family name.
-
#preferred_subfamily_name ⇒ Object
Get preferred subfamily name.
-
#subfamily_name ⇒ Object
Get font subfamily name.
-
#table(tag) ⇒ Object
Get parsed table object.
-
#table_data(tag = nil) ⇒ String, ...
Get decompressed table data.
-
#table_names ⇒ Object
Get list of table tags.
-
#to_otf(output_path) ⇒ Object
Convert to OTF.
-
#to_ttf(output_path) ⇒ Object
Convert to TTF.
-
#truetype? ⇒ Boolean
Check if font has TrueType flavor.
-
#units_per_em ⇒ Object
Get units per em.
-
#valid? ⇒ Boolean
Check if font is valid.
-
#validate_signature! ⇒ Object
Validate WOFF2 signature.
-
#variable_font? ⇒ Boolean
Check if font is a variable font.
-
#variation_type ⇒ Symbol
Variation profile.
Constructor Details
#initialize ⇒ Woff2Font
Returns a new instance of Woff2Font.
87 88 89 90 91 92 93 94 |
# File 'lib/fontisan/woff2_font.rb', line 87 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_tables ⇒ Object
Allow both reading and setting for table delegation
85 86 87 |
# File 'lib/fontisan/woff2_font.rb', line 85 def decompressed_tables @decompressed_tables end |
#header ⇒ Object
Allow both reading and setting for table delegation
85 86 87 |
# File 'lib/fontisan/woff2_font.rb', line 85 def header @header end |
#io_source ⇒ Object
Allow both reading and setting for table delegation
85 86 87 |
# File 'lib/fontisan/woff2_font.rb', line 85 def io_source @io_source end |
#parsed_tables ⇒ Object
Allow both reading and setting for table delegation
85 86 87 |
# File 'lib/fontisan/woff2_font.rb', line 85 def parsed_tables @parsed_tables end |
#table_entries ⇒ Object
Allow both reading and setting for table delegation
85 86 87 |
# File 'lib/fontisan/woff2_font.rb', line 85 def table_entries @table_entries end |
#underlying_font ⇒ Object
Allow both reading and setting for table delegation
85 86 87 |
# File 'lib/fontisan/woff2_font.rb', line 85 def @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
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 574 575 576 577 578 |
# File 'lib/fontisan/woff2_font.rb', line 540 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
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 692 693 694 695 696 697 698 |
# File 'lib/fontisan/woff2_font.rb', line 629 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
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/fontisan/woff2_font.rb', line 584 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
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/fontisan/woff2_font.rb', line 512 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
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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/fontisan/woff2_font.rb', line 329 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. = 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. = 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.}" end woff2 end |
.read_table_directory_from_io(io, header) ⇒ Array<Woff2TableDirectoryEntry>
Read table directory from IO
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 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/fontisan/woff2_font.rb', line 410 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
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/fontisan/woff2_font.rb', line 483 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
608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'lib/fontisan/woff2_font.rb', line 608 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
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
# File 'lib/fontisan/woff2_font.rb', line 705 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
110 111 112 113 114 |
# File 'lib/fontisan/woff2_font.rb', line 110 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.
149 |
# File 'lib/fontisan/woff2_font.rb', line 149 def collection? = false |
#family_name ⇒ Object
Get font family name
279 280 281 282 |
# File 'lib/fontisan/woff2_font.rb', line 279 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
171 172 173 |
# File 'lib/fontisan/woff2_font.rb', line 171 def find_table_entry(tag) @table_entries.find { |entry| entry.tag == tag } end |
#format ⇒ Symbol
High-level pipeline format identifier. Owned by the font class so the conversion pipeline can dispatch without case statements (OCP).
80 |
# File 'lib/fontisan/woff2_font.rb', line 80 def format = :woff2 |
#full_name ⇒ Object
Get full font name
291 292 293 294 |
# File 'lib/fontisan/woff2_font.rb', line 291 def full_name name_table = table("name") name_table&.english_name(Tables::Name::FULL_NAME) end |
#has_table?(tag) ⇒ Boolean
Check if table exists
141 142 143 |
# File 'lib/fontisan/woff2_font.rb', line 141 def has_table?(tag) @table_entries.any? { |entry| entry.tag == tag } end |
#initialize_storage ⇒ Object
Initialize storage hashes
97 98 99 100 |
# File 'lib/fontisan/woff2_font.rb', line 97 def initialize_storage @decompressed_tables ||= {} @initialize_storage ||= {} end |
#metadata ⇒ Object
Get metadata (if present)
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/fontisan/woff2_font.rb', line 260 def return nil unless @header&.&.positive? return nil unless @io_source begin File.open(@io_source.path, "rb") do |io| io.seek(@header.) = io.read(@header.) Brotli.inflate() end rescue StandardError => e warn "Failed to decompress metadata: #{e.}" nil end end |
#outline_type ⇒ Symbol
Outline representation, derived from the wrapped SFNT flavor.
166 167 168 |
# File 'lib/fontisan/woff2_font.rb', line 166 def outline_type truetype? ? :truetype : :cff end |
#post_script_name ⇒ Object
Get PostScript name
297 298 299 300 |
# File 'lib/fontisan/woff2_font.rb', line 297 def post_script_name name_table = table("name") name_table&.english_name(Tables::Name::POSTSCRIPT_NAME) end |
#preferred_family_name ⇒ Object
Get preferred family name
303 304 305 306 |
# File 'lib/fontisan/woff2_font.rb', line 303 def preferred_family_name name_table = table("name") name_table&.english_name(Tables::Name::PREFERRED_FAMILY) end |
#preferred_subfamily_name ⇒ Object
Get preferred subfamily name
309 310 311 312 |
# File 'lib/fontisan/woff2_font.rb', line 309 def preferred_subfamily_name name_table = table("name") name_table&.english_name(Tables::Name::PREFERRED_SUBFAMILY) end |
#subfamily_name ⇒ Object
Get font subfamily name
285 286 287 288 |
# File 'lib/fontisan/woff2_font.rb', line 285 def subfamily_name name_table = table("name") name_table&.english_name(Tables::Name::SUBFAMILY) end |
#table(tag) ⇒ Object
Get parsed table object
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/fontisan/woff2_font.rb', line 208 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/fontisan/woff2_font.rb', line 184 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 end # Tag provided - return specific table # First try underlying font's table data if available if @underlying_font.respond_to?(:table_data) = @underlying_font.table_data[tag] return if end # Fallback to decompressed_tables @decompressed_tables[tag] end |
#table_names ⇒ Object
Get list of table tags
176 177 178 |
# File 'lib/fontisan/woff2_font.rb', line 176 def table_names @table_entries.map(&:tag) end |
#to_otf(output_path) ⇒ Object
Convert to OTF
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/fontisan/woff2_font.rb', line 240 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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/fontisan/woff2_font.rb', line 220 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
103 104 105 106 107 |
# File 'lib/fontisan/woff2_font.rb', line 103 def truetype? return false unless @header [Constants::SFNT_VERSION_TRUETYPE, 0x00010000].include?(@header.flavor) end |
#units_per_em ⇒ Object
Get units per em
315 316 317 318 |
# File 'lib/fontisan/woff2_font.rb', line 315 def units_per_em head = table("head") head&.units_per_em end |
#valid? ⇒ Boolean
Check if font is valid
131 132 133 134 135 136 137 138 |
# File 'lib/fontisan/woff2_font.rb', line 131 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
124 125 126 127 128 |
# File 'lib/fontisan/woff2_font.rb', line 124 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
119 120 121 |
# File 'lib/fontisan/woff2_font.rb', line 119 def variable_font? has_table?("fvar") end |
#variation_type ⇒ Symbol
Variation profile. WOFF2 wraps a single SFNT font; variation tables, if present on the wrapped font, are reported here.
155 156 157 158 159 160 161 |
# File 'lib/fontisan/woff2_font.rb', line 155 def variation_type return :static unless has_table?("fvar") return :gvar if has_table?("gvar") return :cff2 if has_table?("CFF2") :static end |