Class: TTFunk::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ttfunk/collection.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • io (IO(#read & #rewind))

Raises:

  • (ArgumentError)

    if ‘io` doesn’t start with a ttc tag



32
33
34
35
36
37
38
39
40
41
42
43
# 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

Class Method Details

.open(io) {|collection| ... } ⇒ any .open(file_path) {|collection| ... } ⇒ any

Load a TrueType collection.

Overloads:

  • .open(io) {|collection| ... } ⇒ any

    Returns whatever the block returns.

    Parameters:

    • io (IO)

      IO to read the collection from.

    Yield Parameters:

    Returns:

    • (any)

      whatever the block returns

  • .open(file_path) {|collection| ... } ⇒ any

    Returns whatever the block returns.

    Parameters:

    • file_path (String, Pathname)

      Path to the font collection file.

    Yield Parameters:

    Returns:

    • (any)

      whatever the block returns



18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#[](index) ⇒ TTFunk::File

Get font by index.

Parameters:

  • index (Integer)

Returns:



67
68
69
# File 'lib/ttfunk/collection.rb', line 67

def [](index)
  @cache[index] ||= TTFunk::File.new(@contents, @offsets[index])
end

#countInteger

Number of fonts in this collection.

Returns:

  • (Integer)


48
49
50
# File 'lib/ttfunk/collection.rb', line 48

def count
  @offsets.length
end

#each {|font| ... } ⇒ self

Iterate over fonts in the collection.

Yield Parameters:

Returns:

  • (self)


56
57
58
59
60
61
# File 'lib/ttfunk/collection.rb', line 56

def each
  count.times do |index|
    yield(self[index])
  end
  self
end