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

89.19% Statements 33/37
89.47% Branches 17/19
100% Functions 3/3
93.94% Lines 31/33
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     841×   64×     520× 520×   520× 1469× 1469×     1469× 20×   1449× 903× 546× 362×   184× 142×   42×     254× 254×   254× 700× 700×     700×     700× 391× 309× 281×   28×             542×    
class AATLookupTable
  constructor: (@table) ->
  lookup: (glyph) ->
    switch @table.version
      when 0 # simple array format
        return @table.values.getItem glyph
        
      when 2, 4 # segment format
        min = 0
        max = @table.binarySearchHeader.nUnits - 1
        
        while min <= max
          mid = (min + max) >> 1
          seg = @table.segments[mid]
          
          # special end of search value
          if seg.firstGlyph is 0xffff
            return null
          
          if glyph < seg.firstGlyph
            max = mid - 1
          else if glyph > seg.lastGlyph
            min = mid + 1
          else
            if @table.version is 2
              return seg.value
            else
              return seg.values[glyph - seg.firstGlyph]
              
      when 6 # lookup single
        min = 0
        max = @table.binarySearchHeader.nUnits - 1
        
        while min <= max
          mid = (min + max) >> 1
          seg = @table.segments[mid]
          
          # special end of search value
          Iif seg.glyph is 0xffff
            return null
            
          if glyph < seg.glyph
            max = mid - 1
          else if glyph > seg.glyph
            min = mid + 1
          else
            return seg.value
            
      when 8 # lookup trimmed
        return @table.values[glyph - @table.firstGlyph]
            
      else
        throw new Error "Unknown lookup table format: #{@table.version}"
          
    return null
    
module.exports = AATLookupTable