File: ttf_encoder_spec.rb

package info (click to toggle)
ruby-ttfunk 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,472 kB
  • sloc: ruby: 7,954; makefile: 7
file content (71 lines) | stat: -rw-r--r-- 2,392 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

require 'spec_helper'
require 'ttfunk/ttf_encoder'
require 'ttfunk/subset'

RSpec.describe TTFunk::TTFEncoder do
  let(:original_font_path) { test_font('DejaVuSans') }
  let(:original) { TTFunk::File.open(original_font_path) }

  let(:encoder_options) { {} }
  let(:encoder) do
    subset =
      TTFunk::Subset::Unicode.new(original).tap do |sub_set|
        # ASCII lowercase
        (97..122).each { |char| sub_set.use(char) }
      end

    described_class.new(original, subset, encoder_options)
  end

  describe '#encode' do
    subject(:encoded_ttf) { encoder.encode }

    let(:new_font) { TTFunk::File.open(StringIO.new(encoded_ttf)) }

    it 'includes all supported tables' do
      expect(new_font.directory.tables).to include('cmap')
      expect(new_font.directory.tables).to include('glyf')
      expect(new_font.directory.tables).to include('loca')
      expect(new_font.directory.tables).to include('hmtx')
      expect(new_font.directory.tables).to include('hhea')
      expect(new_font.directory.tables).to include('maxp')
      expect(new_font.directory.tables).to include('OS/2')
      expect(new_font.directory.tables).to include('post')
      expect(new_font.directory.tables).to include('name')
      expect(new_font.directory.tables).to include('head')
      expect(new_font.directory.tables).to include('prep')
      expect(new_font.directory.tables).to include('fpgm')
      expect(new_font.directory.tables).to include('cvt ')
    end

    context 'when asked to encode the kern table' do
      let(:encoder_options) { { kerning: true } }

      it 'includes the kern table' do
        expect(new_font.directory.tables).to include('kern')
      end
    end

    it 'lists tables in optimal order' do
      tables = described_class::OPTIMAL_TABLE_ORDER &
        new_font.directory.tables.keys

      tables.each_cons(2) do |first_table, second_table|
        expect(new_font.directory.tables[first_table][:offset]).to(
          be < new_font.directory.tables[second_table][:offset],
        )
      end
    end

    it 'is checksummed correctly' do
      head_offset = new_font.directory.tables['head'][:offset]
      checksum = encoded_ttf[head_offset + 8, 4].unpack1('N')

      # verified via the Font-Validator tool at:
      # https://github.com/HinTak/Font-Validator
      expect(checksum).to eq(0xEEAE9DCF)
    end
  end
end