Class: TTFunk::Collection
- Inherits:
-
Object
- Object
- TTFunk::Collection
- Includes:
- Enumerable
- Defined in:
- lib/ttfunk/collection.rb
Overview
TrueType font collection. Usually a file with .ttc
extension.
Class Method Summary collapse
-
.open(io) {|collection| ... } ⇒ any
.open(file_path) {|collection| ... } ⇒ any
Load a TrueType collection.
Instance Method Summary collapse
-
#[](index) ⇒ TTFunk::File
Get font by index.
-
#count ⇒ Integer
Number of fonts in this collection.
-
#each {|font| ... } ⇒ self
Iterate over fonts in the collection.
-
#initialize(io) ⇒ Collection
constructor
A new instance of Collection.
Constructor Details
#initialize(io) ⇒ Collection
Returns a new instance of Collection.
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.
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.
Source Code
lib/ttfunk/collection.rb, line 67
67 | def [](index) |
68 | @cache[index] ||= TTFunk::File.new(@contents, @offsets[index]) |
69 | end
|
#count ⇒ Integer
Number of fonts in this collection.
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.
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
|