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



305
306
307
308
309
# File 'lib/fontisan/type1/charstrings.rb', line 305

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



294
295
296
# File 'lib/fontisan/type1/charstrings.rb', line 294

def commands
  @commands
end

#private_dictPrivateDict (readonly)

Returns Private dictionary.

Returns:



300
301
302
# File 'lib/fontisan/type1/charstrings.rb', line 300

def private_dict
  @private_dict
end

#seac_componentsHash? (readonly)

Returns seac components if seac found.

Returns:

  • (Hash, nil)

    seac components if seac found



297
298
299
# File 'lib/fontisan/type1/charstrings.rb', line 297

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



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/fontisan/type1/charstrings.rb', line 315

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