class TTFunk::Collection

TrueType font collection. Usually a file with ‘.ttc` extension.

Public Class Methods

new(io) click to toggle source

@param io [IO(read & rewind)] @raise [ArgumentError] if ‘io` doesn’t start with a ttc tag

# File lib/ttfunk/collection.rb, line 32
def initialize(io)
  tag = io.read(4)
  raise ArgumentError, 'not a TTC file' unless tag == 'ttcf'

  _major, _minor = io.read(4).unpack('n*')
  count = io.read(4).unpack1('N')
  @offsets = io.read(count * 4).unpack('N*')

  io.rewind
  @contents = io.read
  @cache = []
end
open(path) { |new(path)| ... } click to toggle source

Load a TrueType collection.

@overload open(io)

@param io [IO] IO to read the collection from.
@yieldparam collection [TTFunk::Collection]
@return [any] whatever the block returns

@overload open(file_path)

@param file_path [String, Pathname] Path to the font collection file.
@yieldparam collection [TTFunk::Collection]
@return [any] whatever the block returns
# File lib/ttfunk/collection.rb, line 18
def self.open(path)
  if path.respond_to?(:read)
    result = yield(new(path))
    path.rewind
    result
  else
    ::File.open(path, 'rb') do |io|
      yield(new(io))
    end
  end
end

Public Instance Methods

[](index) click to toggle source

Get font by index.

@param index [Integer] @return [TTFunk::File]

# File lib/ttfunk/collection.rb, line 67
def [](index)
  @cache[index] ||= TTFunk::File.new(@contents, @offsets[index])
end
count() click to toggle source

Number of fonts in this collection.

@return [Integer]

# File lib/ttfunk/collection.rb, line 48
def count
  @offsets.length
end
each() { |self| ... } click to toggle source

Iterate over fonts in the collection.

@yieldparam font [TTFunk::File] @return [self]

# File lib/ttfunk/collection.rb, line 56
def each
  count.times do |index|
    yield(self[index])
  end
  self
end