Class: Fontisan::Type1::CharStrings::CharStringParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/type1/charstrings.rb

Overview

CharString parser

Parses Type 1 CharString bytecode into commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_dict = nil) ⇒ CharStringParser

Initialize parser

Parameters:

  • private_dict (PrivateDict) (defaults to: nil)

    Private dictionary



272
273
274
275
276
# File 'lib/fontisan/type1/charstrings.rb', line 272

def initialize(private_dict = nil)
  @private_dict = private_dict || PrivateDict.new
  @commands = []
  @seac_components = nil
end

Instance Attribute Details

#commandsArray (readonly)

Returns Parsed commands.

Returns:

  • (Array)

    Parsed commands



261
262
263
# File 'lib/fontisan/type1/charstrings.rb', line 261

def commands
  @commands
end

#private_dictPrivateDict (readonly)

Returns Private dictionary.

Returns:



267
268
269
# File 'lib/fontisan/type1/charstrings.rb', line 267

def private_dict
  @private_dict
end

#seac_componentsHash? (readonly)

Returns seac components if seac found.

Returns:

  • (Hash, nil)

    seac components if seac found



264
265
266
# File 'lib/fontisan/type1/charstrings.rb', line 264

def seac_components
  @seac_components
end

Instance Method Details

#parse(charstring) ⇒ Array

Parse CharString bytecode

Parameters:

  • charstring (String)

    Binary CharString data

Returns:

  • (Array)

    Parsed commands



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/fontisan/type1/charstrings.rb', line 282

def parse(charstring)
  return [] if charstring.nil? || charstring.empty?

  @commands = []
  @seac_components = nil

  i = 0
  while i < charstring.length
    byte = charstring.getbyte(i)

    if byte <= 31
      # Operator
      parse_operator(charstring, byte, i)
      break if @seac_components # Stop at seac for now

      i += 1
    elsif byte == 255
      # Escaped number (2 bytes follow)
      num = charstring.getbyte(i + 1) |
        (charstring.getbyte(i + 2) << 8)
      num = num - 32768 if num >= 32768
      @commands << [:number, num]
      i += 3
    elsif byte >= 32 && byte <= 246
      # Small number (-107 to 107)
      num = byte - 139
      @commands << [:number, num]
      i += 1
    else
      # Unknown
      i += 1
    end
  end

  @commands
end