Skip to content

Commit

Permalink
ignore cmap tables with a format number > 14
Browse files Browse the repository at this point in the history
- ignore cmap tables with a format number > 14 (CJK fonts)
- migrate rspec expectations to expect API
  • Loading branch information
mojavelinux committed Aug 29, 2014
1 parent 595b639 commit e8302f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/ttfunk/table/cmap.rb
Expand Up @@ -14,8 +14,9 @@ def self.encode(charmap, encoding)

def unicode
# Because most callers just call .first on the result, put tables with highest-number format first.
# However, ignore tables with a format number greater than 12 since ttfunk doesn't support them
# (Tables with higher format numbers tend to be more complete, especially in higher/astral Unicode ranges.)
@unicode ||= @tables.select { |table| table.unicode? }.sort{|a,b| b.format <=> a.format }
@unicode ||= @tables.select { |table| table.unicode? && table.format <= 12 }.sort{|a,b| b.format <=> a.format }
end

private
Expand Down
Binary file added spec/fonts/Mplus1p.ttf
Binary file not shown.
52 changes: 45 additions & 7 deletions spec/integration/file_spec.rb
Expand Up @@ -5,13 +5,13 @@
describe TTFunk::File, "::open" do
it "opens file paths" do
font = TTFunk::File.open test_font("DejaVuSans")
font.contents.read(4).should == "\x00\x00\x00\x01"
expect(font.contents.read(4)).to eq("\x00\x00\x00\x01")
end

it "opens IO Objects" do
io = File.open test_font("DejaVuSans")
font = TTFunk::File.open io
font.contents.read(4).should == "\x00\x00\x00\x01"
expect(font.contents.read(4)).to eq("\x00\x00\x00\x01")
end
end

Expand All @@ -21,7 +21,7 @@
let!(:file) { TTFunk::File.open(test_font("DejaVuSans"))}

it "should extract the correct value" do
file.ascent.should == 1556
expect(file.ascent).to eq(1556)
end
end
end
Expand All @@ -32,7 +32,7 @@
let!(:file) { TTFunk::File.open(test_font("DejaVuSans"))}

it "should extract the correct value" do
file.descent.should == -492
expect(file.descent).to eq(-492)
end
end
end
Expand All @@ -43,7 +43,7 @@
let!(:file) { TTFunk::File.open(test_font("DejaVuSans"))}

it "should extract the correct value" do
file.line_gap.should == 410
expect(file.line_gap).to eq(410)
end
end
end
Expand All @@ -54,7 +54,7 @@
let!(:file) { TTFunk::File.open(test_font("DejaVuSans"))}

it "should extract the correct value" do
file.bbox.should == [-2090, -850, 3442, 2389]
expect(file.bbox).to eq([-2090, -850, 3442, 2389])
end
end
end
Expand All @@ -64,7 +64,45 @@
context "with DejaVuSans" do
let!(:file) { TTFunk::File.open(test_font("DejaVuSans"))}
it "should extract the correct value" do
file.name.preferred_family.first.should == 'DejaVu Sans'
expect(file.name.preferred_family.first).to eq('DejaVu Sans')
end
end
end

describe TTFunk::File, "#cmap" do

context "with DejaVuSans" do
let!(:file) { TTFunk::File.open(test_font("DejaVuSans"))}

it "should extract cmap tables in descending order of format" do
cmaps = file.cmap.unicode
expect(cmaps.size).to eq(4)
expect(cmaps.map(&:format)).to eq([12, 12, 4, 4])
end

it "should lookup code in cmap format 12 table" do
cmap_format_12 = file.cmap.unicode.first
expect(cmap_format_12.format).to eq(12)
expect(cmap_format_12[32]).to eq(3)
end
end

# M+ 1p is a CJK font that includes a cmap format 14 table
# we use a trimmed down version of the font generated with fontforge for testing purposes
context "with M+ 1p" do
let!(:file) { TTFunk::File.open(test_font("Mplus1p"))}

# this test verifies that the cmap format 14 table is ignored
it "should extract cmap tables in descending order of format" do
cmaps = file.cmap.unicode
expect(cmaps.size).to eq(2)
expect(cmaps.map(&:format)).to eq([4, 4])
end

it "should lookup code in cmap format 4 table" do
cmap_format_4 = file.cmap.unicode.first
expect(cmap_format_4.format).to eq(4)
expect(cmap_format_4[32]).to eq(4)
end
end
end
Expand Down

0 comments on commit e8302f2

Please sign in to comment.