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

Source Code
lib/ttfunk/collection.rb, line 32
32
def initialize(io)
33
  tag = io.read(4)
34
  raise ArgumentError, 'not a TTC file' unless tag == 'ttcf'
35
36
  _major, _minor = io.read(4).unpack('n*')
37
  count = io.read(4).unpack1('N')
38
  @offsets = io.read(count * 4).unpack('N*')
39
40
  io.rewind
41
  @contents = io.read
42
  @cache = []
43
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

Source Code
lib/ttfunk/collection.rb, line 18
18
def self.open(path)
19
  if path.respond_to?(:read)
20
    result = yield(new(path))
21
    path.rewind
22
    result
23
  else
24
    ::File.open(path, 'rb') do |io|
25
      yield(new(io))
26
    end
27
  end
28
end

Instance Method Details

#[](index) ⇒ TTFunk::File

Get font by index.

Parameters:

  • index (Integer)

Returns:

Source Code
lib/ttfunk/collection.rb, line 67
67
def [](index)
68
  @cache[index] ||= TTFunk::File.new(@contents, @offsets[index])
69
end

#countInteger

Number of fonts in this collection.

Returns:

  • (Integer)
Source Code
lib/ttfunk/collection.rb, line 48
48
def count
49
  @offsets.length
50
end

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

Iterate over fonts in the collection.

Yield Parameters:

Returns:

  • (self)
Source Code
lib/ttfunk/collection.rb, line 56
56
def each
57
  count.times do |index|
58
    yield(self[index])
59
  end
60
  self
61
end