Class: Fontisan::TrueTypeCollection

Inherits:
BinData::Record
  • Object
show all
Defined in:
lib/fontisan/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/fontisan/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/fontisan/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

#font(index, io) ⇒ TrueTypeFont?

Get a single font from the collection (Fontisan extension)

Parameters:

  • index (Integer)

    Index of the font (0-based)

  • io (IO)

    Open file handle

Returns:

  • (TrueTypeFont, nil)

    Font object or nil if index out of range



68
69
70
71
72
73
# File 'lib/fontisan/true_type_collection.rb', line 68

def font(index, io)
  return nil if index >= num_fonts

  require_relative "true_type_font"
  TrueTypeFont.from_ttc(io, font_offsets[index])
end

#font_countInteger

Get font count (Fontisan extension)

Returns:

  • (Integer)

    Number of fonts in collection



78
79
80
# File 'lib/fontisan/true_type_collection.rb', line 78

def font_count
  num_fonts
end

#valid?Boolean

Validate format correctness

Returns:

  • (Boolean)

    true if the format is valid, false otherwise



85
86
87
88
89
# File 'lib/fontisan/true_type_collection.rb', line 85

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

#versionObject

Get the TTC version as a single integer

@return [Integer] Version number (e.g., 0x00010000 for version 1.0)


94
95
96
# File 'lib/fontisan/true_type_collection.rb', line 94

def version
  (major_version << 16) | minor_version
end