File: fd_selector_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 (83 lines) | stat: -rw-r--r-- 2,547 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
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

require 'spec_helper'
require 'ttfunk/table/cff'

RSpec.describe TTFunk::Table::Cff::FdSelector do
  let(:font) { TTFunk::File.open(font_path) }
  let(:fd_selector) { font.cff.top_index[0].font_dict_selector }

  context 'with an array-formatted selector' do
    # sadly it appears the array format isn't widely used, so it's necessary to
    # test the functionality manually
    let(:io) { StringIO.new(contents) }
    let(:contents) { "\x00\x01\x02\x03\x04\x05\x06" }
    let(:entry_count) { 6 }
    let(:top_dict) do
      instance_double(
        TTFunk::Table::Cff::TopDict,
        :top_dict,
        charstrings_index: charstrings_index,
      )
    end
    let(:charstrings_index) do
      instance_double(
        TTFunk::Table::Cff::CharstringsIndex,
        :charstrings_index,
        items_count: entry_count,
      )
    end
    let(:fd_selector) do
      described_class.new(top_dict, TestFile.new(io), 0, io.length)
    end

    before do
      allow(fd_selector).to receive(:charstrings_index).and_return(charstrings_index)
    end

    it 'includes entries for all the glyphs in the font' do
      expect(fd_selector.items_count).to eq(entry_count)
    end

    it 'parses the entries correctly' do
      expect(fd_selector.to_a).to eq([1, 2, 3, 4, 5, 6])
    end

    it 'encodes correctly' do
      charmap = {
        0x20 => { old: 1, new: 1 },
        0x22 => { old: 3, new: 3 },
        0x24 => { old: 5, new: 5 },
      }
      expect(fd_selector.encode(charmap)).to eq("\x00\x02\x04\x06")
    end
  end

  context 'with a range-formatted selector' do
    let(:font_path) { test_font('NotoSansCJKsc-Thin', :otf) }

    it 'includes entries for all the glyphs in the font' do
      # the charstrings index doesn't contain an entry for the .notdef glyph
      expect(fd_selector.items_count).to eq(font.cff.top_index[0].charstrings_index.items_count + 1)
    end

    it 'parses the entries correctly' do
      fd_indices = fd_selector.to_a

      expect(fd_indices[0..10]).to eq [5, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15]

      expect(fd_indices[10..20]).to eq [15, 15, 15, 15, 15, 15, 15, 17, 17, 17, 17]

      expect(fd_indices[-10..]).to eq [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
    end

    it 'encodes correctly' do
      charmap = (0..15).to_h { |i| [i, { old: i, new: i }] }
      result = fd_selector.encode(charmap)
      expect(result).to(
        #   fmt | count |  range 1  |  range 2  | n glyphs
        eq("\x03\x00\x02\x00\x00\x05\x00\x01\x0F\x00\x10"),
      )
    end
  end
end