File: font_dict.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 (91 lines) | stat: -rw-r--r-- 2,735 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
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module TTFunk
  class Table
    class Cff < TTFunk::Table
      # CFF Font dict.
      class FontDict < TTFunk::Table::Cff::Dict
        # Length of placeholders.
        PLACEHOLDER_LENGTH = 5

        # Operators we care about in this dict.
        OPERATORS = { private: 18 }.freeze

        # Inverse operator mapping.
        OPERATOR_CODES = OPERATORS.invert

        # Top dict.
        # @return [TTFunk::Table::Cff::TopDict]
        attr_reader :top_dict

        # @param top_dict [TTFunk::Table:Cff::TopDict]
        # @param file [TTFunk::File]
        # @param offset [Integer]
        # @param length [Integer]
        def initialize(top_dict, file, offset, length = nil)
          @top_dict = top_dict
          super(file, offset, length)
        end

        # Encode dict.
        #
        # @return [TTFunk::EncodedString]
        def encode
          EncodedString.new do |result|
            each do |operator, operands|
              case OPERATOR_CODES[operator]
              when :private
                result << encode_private
              else
                operands.each { |operand| result << encode_operand(operand) }
              end

              result << encode_operator(operator)
            end
          end
        end

        # Finalize dict.
        #
        # @param new_cff_data [TTFunk::EncodedString]
        # @return [void]
        def finalize(new_cff_data)
          encoded_private_dict = private_dict.encode
          encoded_offset = encode_integer32(new_cff_data.length)
          encoded_length = encode_integer32(encoded_private_dict.length)

          new_cff_data.resolve_placeholder(:"private_length_#{@table_offset}", encoded_length)
          new_cff_data.resolve_placeholder(:"private_offset_#{@table_offset}", encoded_offset)

          private_dict.finalize(encoded_private_dict)
          new_cff_data << encoded_private_dict
        end

        # Private dict.
        #
        # @return [TTFunk::Table::Cff::PrivateDict, nil]
        def private_dict
          @private_dict ||=
            if (info = self[OPERATORS[:private]])
              private_dict_length, private_dict_offset = info

              PrivateDict.new(
                file,
                top_dict.cff_offset + private_dict_offset,
                private_dict_length,
              )
            end
        end

        private

        def encode_private
          EncodedString.new do |result|
            result << Placeholder.new(:"private_length_#{@table_offset}", length: PLACEHOLDER_LENGTH)
            result << Placeholder.new(:"private_offset_#{@table_offset}", length: PLACEHOLDER_LENGTH)
          end
        end
      end
    end
  end
end