Class: ExtractTtc::TrueTypeCollection

Inherits:
BinData::Record
  • Object
show all
Defined in:
lib/extract_ttc/true_type_collection.rb

Overview

TrueType Collection domain object using BinData

Represents a complete TrueType Collection file using BinData’s declarative DSL for binary structure definition. The structure definition IS the documentation, and BinData handles all low-level reading/writing.

Examples:

Reading and extracting fonts

File.open("Helvetica.ttc", "rb") do |io|
  ttc = TrueTypeCollection.read(io)
  puts ttc.num_fonts  # => 6
  fonts = ttc.extract_fonts(io)  # => [TrueTypeFont, TrueTypeFont, ...]
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_file(path) ⇒ TrueTypeCollection

Read TrueType Collection from a file

Parameters:

  • path (String)

    Path to the TTC file

Returns:

Raises:

  • (ArgumentError)

    if path is nil or empty

  • (Errno::ENOENT)

    if file does not exist

  • (RuntimeError)

    if file format is invalid



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/extract_ttc/true_type_collection.rb', line 35

def self.from_file(path)
  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)

  File.open(path, "rb") { |io| read(io) }
rescue BinData::ValidityError => e
  raise "Invalid TTC file: #{e.message}"
rescue EOFError => e
  raise "Invalid TTC file: unexpected end of file - #{e.message}"
end

Instance Method Details

#extract_fonts(io) ⇒ Array<TrueTypeFont>

Extract fonts as TrueTypeFont objects

Reads each font from the TTC file and returns them as TrueTypeFont objects.

Parameters:

  • io (IO)

    Open file handle to read fonts from

Returns:



55
56
57
58
59
60
61
# File 'lib/extract_ttc/true_type_collection.rb', line 55

def extract_fonts(io)
  require_relative "true_type_font"

  font_offsets.map do |offset|
    TrueTypeFont.from_ttc(io, offset)
  end
end

#valid?Boolean

Validate format correctness

Returns:

  • (Boolean)

    true if the format is valid, false otherwise



66
67
68
69
70
# File 'lib/extract_ttc/true_type_collection.rb', line 66

def valid?
  tag == Constants::TTC_TAG && num_fonts.positive? && font_offsets.length == num_fonts
rescue StandardError
  false
end

#versionInteger

Get the TTC version as a single integer

Returns:

  • (Integer)

    Version number (e.g., 0x00010000 for version 1.0)



75
76
77
# File 'lib/extract_ttc/true_type_collection.rb', line 75

def version
  (major_version << 16) | minor_version
end