File: encoding.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 (277 lines) | stat: -rw-r--r-- 8,303 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: true

module TTFunk
  class Table
    class Cff < TTFunk::Table
      # CFF Encoding.
      class Encoding < TTFunk::SubTable
        include Enumerable

        # Predefined Standard Encoding ID.
        STANDARD_ENCODING_ID = 0

        # Predefined Expert Encoding ID.
        EXPERT_ENCODING_ID = 1

        # Default encoding ID.
        DEFAULT_ENCODING_ID = STANDARD_ENCODING_ID

        class << self
          # Get predefined encoding by ID.
          #
          # @param encoding_id [Integer]
          # @return [TTFunk::OneBasedArray<Integer>]
          def codes_for_encoding_id(encoding_id)
            case encoding_id
            when STANDARD_ENCODING_ID
              Encodings::STANDARD
            when EXPERT_ENCODING_ID
              Encodings::EXPERT
            end
          end
        end

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

        # Encodign format.
        # @return [Integer]
        attr_reader :format

        # Number of encoded items.
        # @return [Integer]
        attr_reader :items_count

        # Offset or encoding ID.
        # @return [Integer]
        attr_reader :offset_or_id

        # @overload initialize(top_dict, file, offset = nil, length = nil)
        #   @param top_dict [TTFunk::Table:Cff::TopDict]
        #   @param file [TTFunk::File]
        #   @param offset [Integer]
        #   @param length [Integer]
        # @overload initialize(top_dict, file, charset_id)
        #   @param top_dict [TTFunk::Table:Cff::TopDict]
        #   @param file [TTFunk::File]
        #   @param encoding_id [Integer] 0, 1, or 2
        def initialize(top_dict, file, offset_or_id = nil, length = nil)
          @top_dict = top_dict
          @offset_or_id = offset_or_id || DEFAULT_ENCODING_ID

          if offset
            super(file, offset, length)
            @supplemental = format >> 7 == 1
          else
            @items_count = self.class.codes_for_encoding_id(offset_or_id).size
            @supplemental = false
          end
        end

        # Iterate over character codes.
        #
        # @overload each()
        #   @yieldparam code [Integer]
        #   @return [void]
        # @overload each()
        #   @return [Enumerator]
        def each
          return to_enum(__method__) unless block_given?

          # +1 adjusts for the implicit .notdef glyph
          (items_count + 1).times { |i| yield(self[i]) }
        end

        # Get character code for glyph index.
        #
        # @param glyph_id [Integer]
        # @return [Integer, nil]
        def [](glyph_id)
          return 0 if glyph_id.zero?
          return code_for(glyph_id) if offset

          self.class.codes_for_encoding_id(offset_or_id)[glyph_id]
        end

        # Encoding offset in the file.
        #
        # @return [Integer, nil]
        def offset
          # Numbers from 0..1 mean encoding IDs instead of offsets. IDs are
          # pre-defined, generic encodings that define the characters present
          # in the font.
          #
          # In the case of an offset, add the CFF table's offset since the
          # charset offset is relative to the start of the CFF table. Otherwise
          # return nil (no offset).
          if offset_or_id > 1
            offset_or_id + top_dict.cff_offset
          end
        end

        # Encode encoding.
        #
        # @param charmap [Hash{Integer => Hash}] keys are the charac codes,
        #   values are hashes:
        #   * `:old` (<tt>Integer</tt>) - glyph ID in the original font.
        #   * `:new` (<tt>Integer</tt>) - glyph ID in the subset font.
        # @return [String]
        def encode(charmap)
          # Any subset encoding is all but guaranteed to be different from the
          # standard encoding so we don't even attempt to see if it matches. We
          # assume it's different and just encode it anew.

          return encode_supplemental(charmap) if supplemental?

          codes =
            charmap
              .reject { |_code, mapping| mapping[:new].zero? }
              .sort_by { |_code, mapping| mapping[:new] }
              .map { |(code, _m)| code }

          ranges = TTFunk::BinUtils.rangify(codes)

          # calculate whether storing the charset as a series of ranges is
          # more efficient (i.e. takes up less space) vs storing it as an
          # array of SID values
          total_range_size = (2 * ranges.size) +
            (element_width(:range_format) * ranges.size)

          total_array_size = codes.size * element_width(:array_format)

          if total_array_size <= total_range_size
            ([format_int(:array_format), codes.size] + codes).pack('C*')
          else
            element_fmt = element_format(:range_format)
            result = [format_int(:range_format), ranges.size].pack('CC')
            ranges.each { |range| result << range.pack(element_fmt) }
            result
          end
        end

        # Is this a supplemental encoding?
        #
        # @return [Boolean]
        def supplemental?
          # high-order bit set to 1 indicates supplemental encoding
          @supplemental
        end

        private

        def encode_supplemental(charmap)
          new_entries =
            charmap
              .reject { |_code, mapping| mapping[:new].zero? }
              .transform_values { |mapping| mapping[:new] }

          result = [format_int(:supplemental), new_entries.size].pack('CC')
          fmt = element_format(:supplemental)

          new_entries.each do |code, new_gid|
            result << [code, new_gid].pack(fmt)
          end

          result
        end

        def code_for(glyph_id)
          return 0 if glyph_id.zero?

          # rather than validating the glyph as part of one of the predefined
          # encodings, just pass it through
          return glyph_id unless offset

          case format_sym
          when :array_format, :supplemental
            @entries[glyph_id]

          when :range_format
            remaining = glyph_id

            @entries.each do |range|
              if range.size >= remaining
                return (range.first + remaining) - 1
              end

              remaining -= range.size
            end

            0
          end
        end

        def parse!
          @format, entry_count = read(2, 'C*')
          @length = entry_count * element_width

          case format_sym
          when :array_format
            @items_count = entry_count
            @entries = OneBasedArray.new(read(length, 'C*'))

          when :range_format
            @entries = []
            @items_count = 0

            entry_count.times do
              code, num_left = read(element_width, element_format)
              @entries << (code..(code + num_left))
              @items_count += num_left + 1
            end

          when :supplemental
            @entries = {}
            @items_count = entry_count

            entry_count.times do
              code, glyph = read(element_width, element_format)
              @entries[code] = glyph
            end
          end
        end

        def element_format(fmt = format_sym)
          {
            array_format: 'C',
            range_format: 'CC',
            supplemental: 'Cn',
          }[fmt]
        end

        # @TODO: handle supplemental encoding (necessary?)
        def element_width(fmt = format_sym)
          case fmt
          when :array_format then 1
          when :range_format then 2
          when :supplemental then 3
          else
            raise Error, "'#{fmt}' is an unsupported encoding format"
          end
        end

        def format_sym
          return :supplemental if supplemental?

          case @format
          when 0 then :array_format
          when 1 then :range_format
          else
            raise Error, "unsupported charset format '#{fmt}'"
          end
        end

        def format_int(sym = format_sym)
          case sym
          when :array_format then 0
          when :range_format then 1
          when :supplemental then 129
          else
            raise Error, "unsupported charset format '#{sym}'"
          end
        end
      end
    end
  end
end