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(path) ⇒ Object
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.
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.
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.
67 68 69 |
# File 'lib/ttfunk/collection.rb', line 67 def [](index) @cache[index] ||= TTFunk::File.new(@contents, @offsets[index]) end |
#count ⇒ Integer
Number of fonts in this collection.
48 49 50 |
# File 'lib/ttfunk/collection.rb', line 48 def count @offsets.length end |
#each {|font| ... } ⇒ self
Iterate over fonts in the collection.
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 |