File: format20.rb

package info (click to toggle)
ruby-ttfunk 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,472 kB
  • sloc: ruby: 7,954; makefile: 7
file content (48 lines) | stat: -rw-r--r-- 1,309 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
# frozen_string_literal: true

require_relative 'format10'
require 'stringio'

module TTFunk
  class Table
    class Post
      # Version 2.0 is used for fonts that use glyph names that are not in the
      # set of Macintosh glyph names. A given font may map some of its glyphs to
      # the standard Macintosh glyph names, and some to its own custom names.
      # A version 2.0 `post` table can be used in fonts with TrueType or CFF
      # version 2 outlines.
      module Format20
        include Format10

        # Get glyph name for character code.
        #
        # @param code [Integer]
        # @return [String]
        def glyph_for(code)
          index = @glyph_name_index[code]
          return '.notdef' unless index

          if index <= 257
            POSTSCRIPT_GLYPHS[index]
          else
            @names[index - 258] || '.notdef'
          end
        end

        private

        def parse_format!
          number_of_glyphs = read(2, 'n').first
          @glyph_name_index = read(number_of_glyphs * 2, 'n*')
          @names = []

          strings = StringIO.new(io.read(offset + length - io.pos))
          until strings.eof?
            length = strings.read(1).unpack1('C')
            @names << strings.read(length)
          end
        end
      end
    end
  end
end