all files / fontkit/src/aat/ AATStateMachine.coffee

95.35% Statements 41/43
78.57% Branches 11/14
100% Functions 3/3
100% Lines 36/36
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     27×           27× 27× 27×   879× 852× 852× 852×   852× 27×   825× 825× 91×   734× 734× 529×   852× 852× 852×   852× 734× 734×   852× 852× 852×   27×    
AATLookupTable = require './AATLookupTable'
 
class AATStateMachine
  constructor: (@stateTable) ->
    @lookupTable = new AATLookupTable @stateTable.classTable
      
  START_OF_TEXT_STATE = 0
  START_OF_LINE_STATE = 1
  
  END_OF_TEXT_CLASS = 0
  OUT_OF_BOUNDS_CLASS = 1
  DELETED_GLYPH_CLASS = 2
  END_OF_LINE_CLASS = 3
  
  DONT_ADVANCE = 0x4000
    
  process: (glyphs, reverse, processEntry) ->
    currentState = START_OF_TEXT_STATE # START_OF_LINE_STATE is used for kashida glyph insertions sometimes I think?
    index = Iif reverse then glyphs.length - 1 else 0
    dir = Iif reverse then -1 else 1
    
    while (dir is 1 and index <= glyphs.length) or (dir is -1 and index >= -1)
      glyph = null
      classCode = OUT_OF_BOUNDS_CLASS
      shouldAdvance = true
        
      if index in [glyphs.length, -1]
        classCode = END_OF_TEXT_CLASS
      else
        glyph = glyphs[index]
        if glyph.id is 0xffff # deleted glyph
          classCode = DELETED_GLYPH_CLASS
        else
          classCode = @lookupTable.lookup(glyph.id)
          unless classCode?
            classCode = OUT_OF_BOUNDS_CLASS
          
      row = @stateTable.stateArray.getItem currentState
      entryIndex = row[classCode]
      entry = @stateTable.entryTable.getItem entryIndex
      
      if classCode not in [END_OF_TEXT_CLASS, DELETED_GLYPH_CLASS]
        processEntry glyph, entry, index
        shouldAdvance = !(entry.flags & DONT_ADVANCE)
 
      currentState = entry.newState
      Eif shouldAdvance
        index += dir
        
    return glyphs
          
module.exports = AATStateMachine