Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'ttfunk' | ||
|
||
module TTFunk | ||
class Collection | ||
include Enumerable | ||
|
||
def self.open(path) | ||
::File.open(path, 'rb') do |io| | ||
yield new(io) | ||
end | ||
end | ||
|
||
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).unpack('N').first | ||
@offsets = io.read(count * 4).unpack('N*') | ||
|
||
io.rewind | ||
@contents = io.read | ||
@cache = [] | ||
end | ||
|
||
def count | ||
@offsets.length | ||
end | ||
|
||
def each | ||
count.times do |index| | ||
yield self[index] | ||
end | ||
self | ||
end | ||
|
||
def [](index) | ||
@cache[index] ||= TTFunk::File.new(@contents, @offsets[index]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# coding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe TTFunk::Collection, '::open' do | ||
it 'will not open non-TTC files' do | ||
expect { TTFunk::Collection.open test_font('DejaVuSans') } | ||
.to raise_error(ArgumentError) | ||
end | ||
|
||
it 'will open TTC files' do | ||
success = false | ||
|
||
TTFunk::Collection.open(test_font('DejaVuSans', :ttc)) do |_ttc| | ||
success = true | ||
end | ||
|
||
expect(success).to be true | ||
end | ||
|
||
it 'will report fonts in TTC' do | ||
TTFunk::Collection.open(test_font('DejaVuSans', :ttc)) do |ttc| | ||
expect(ttc.count).to eq 2 | ||
expect(ttc[0].name.font_name.first).to eq 'DejaVu Sans' | ||
expect(ttc[1].name.font_name.first).to eq 'DejaVu Sans Bold' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters