Code coverage report for fontkit/src/opentype/GPOSProcessor.coffee

Statements: 73.96% (125 / 169)      Branches: 66.67% (52 / 78)      Functions: 100% (8 / 8)      Lines: 75.34% (110 / 146)      Ignored: none     

All files » fontkit/src/opentype/ » GPOSProcessor.coffee
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 2471   1   46 46 23   46     46 6   46           4439                             423 423   366 366   35                       35 35 35   23 23 23   23     8 8 8   6 6   6 6   6 6   6 6   6                 6 6 6 6   6 6 6         6     532 532     9 9 10   9   9 9   7 7 7 7     16 16     3 3 4   3   3 3   2   2 2 2 1   1   2 2 2 2     124 124     4 4 4   1     1 1               1     1                     8     3328               3336     9 9   9 9   9 9 9       30     18   18 178   18     184 184 6   6 6   6     18 9   9 9   9         18   1  
OpenTypeProcessor = require './OpenTypeProcessor'
 
class GPOSProcessor extends OpenTypeProcessor
  applyPositionValue: (sequenceIndex, value) ->
    position = @positions[@glyphIterator.peekIndex sequenceIndex]
    if value.xAdvance?
      position.xAdvance += value.xAdvance
      
    Iif value.yAdvance?
      position.yAdvance += value.yAdvance
      
    if value.xPlacement?
      position.xOffset += value.xPlacement
      
    Iif value.yPlacement?
      position.yOffset += value.yPlacement
            
    # TODO: device tables
  
  applyLookup: (lookupType, table) ->
    switch lookupType
      when 1 # Single positioning value
        index = @coverageIndex table.coverage
        return false if index is -1
        
        switch table.version
          when 1
            @applyPositionValue 0, table.value
            
          when 2
            @applyPositionValue 0, table.values.get(index)
            
        return true
    
      when 2 # Pair Adjustment Positioning
        nextGlyph = @glyphIterator.peek()
        return false unless nextGlyph
        
        index = @coverageIndex table.coverage
        return false if index is -1
      
        switch table.version
          when 1 # Adjustments for glyph pairs
            set = table.pairSets.get(index)
            
            for pair in set when pair.secondGlyph is nextGlyph.id
              @applyPositionValue 0, pair.value1
              @applyPositionValue 1, pair.value2
              return true
              
            return false
          
          when 2 # Class pair adjustment
            class1 = @getClassID @glyphIterator.cur.id, table.classDef1
            class2 = @getClassID nextGlyph.id, table.classDef2
            return false if class1 is -1 or class2 is -1
              
            pair = table.classRecords.get(class1).get(class2)
            @applyPositionValue 0, pair.value1
            @applyPositionValue 1, pair.value2
            
        return true
            
      when 3 # Cursive Attachment Positioning
        nextIndex = @glyphIterator.peekIndex()
        nextGlyph = @glyphs[nextIndex]
        return false unless nextGlyph
 
        curRecord = table.entryExitRecords[@coverageIndex table.coverage]
        Ireturn false unless curRecord?.exitAnchor
        
        nextRecord = table.entryExitRecords[@coverageIndex table.coverage, nextGlyph.id]
        Ireturn false unless nextRecord?.entryAnchor
        
        entry = @getAnchor nextRecord.entryAnchor
        exit = @getAnchor curRecord.exitAnchor
        
        cur = @positions[@glyphIterator.index]
        next = @positions[nextIndex]
                
        switch @direction
          when 'ltr'
            cur.xAdvance = exit.x + cur.xOffset
 
            d = entry.x + next.xOffset
            next.xAdvance -= d
            next.xOffset -= d
 
          when 'rtl'
            d = exit.x + cur.xOffset
            cur.xAdvance -= d
            cur.xOffset -= d
            next.xAdvance = entry.x + next.xOffset
 
        Eif @glyphIterator.flags.rightToLeft
          @glyphIterator.cur.cursiveAttachment = nextIndex
          cur.yOffset = entry.y - exit.y
        else
          nextGlyph.cursiveAttachment = @glyphIterator.index
          cur.yOffset = exit.y - entry.y
          
        return true
            
      when 4 # Mark to base positioning
        markIndex = @coverageIndex table.markCoverage
        return false if markIndex is -1
        
        # search backward for a base glyph
        baseGlyphIndex = @glyphIterator.index
        while --baseGlyphIndex >= 0
          break unless @glyphs[baseGlyphIndex].isMark
        
        Ireturn false if baseGlyphIndex < 0
        
        baseIndex = @coverageIndex table.baseCoverage, @glyphs[baseGlyphIndex].id
        return false if baseIndex is -1
        
        markRecord = table.markArray[markIndex]
        baseAnchor = table.baseArray[baseIndex][markRecord.class]
        @applyAnchor markRecord, baseAnchor, baseGlyphIndex
        return true
        
      when 5 # Mark to ligature positioning
        markIndex = @coverageIndex table.markCoverage
        return false if markIndex is -1
        
        # search backward for a base glyph
        baseGlyphIndex = @glyphIterator.index
        while --baseGlyphIndex >= 0
          break unless @glyphs[baseGlyphIndex].isMark
        
        Ireturn false if baseGlyphIndex < 0
        
        ligIndex = @coverageIndex table.ligatureCoverage, @glyphs[baseGlyphIndex].id
        return false if ligIndex is -1
        
        ligAttach = table.ligatureArray[ligIndex]
        
        markGlyph = @glyphIterator.cur
        ligGlyph = @glyphs[baseGlyphIndex]
        compIndex = if ligGlyph.ligatureID and ligGlyph.ligatureID is markGlyph.ligatureID and markGlyph.ligatureComponent?
          Math.min(markGlyph.ligatureComponent, ligGlyph.codePoints.length) - 1
        else
          ligGlyph.codePoints.length - 1
                    
        markRecord = table.markArray[markIndex]
        baseAnchor = ligAttach[compIndex][markRecord.class]
        @applyAnchor markRecord, baseAnchor, baseGlyphIndex
        return true
        
      when 6 # Mark to mark positioning
        mark1Index = @coverageIndex table.mark1Coverage
        return false if mark1Index is -1
        
        # get the previous mark to attach to
        prevIndex = @glyphIterator.peekIndex -1
        prev = @glyphs[prevIndex]
        return false unless prev?.isMark
        
        cur = @glyphIterator.cur
        
        # The following logic was borrowed from Harfbuzz
        good = no
        Iif cur.ligatureID is prev.ligatureID
          if not cur.ligatureID # Marks belonging to the same base
            good = yes
          else if cur.ligatureComponent is prev.ligatureComponent # Marks belonging to the same ligature component
            good = yes
        else
          # If ligature ids don't match, it may be the case that one of the marks
          # itself is a ligature, in which case match.
          Iif (cur.ligatureID and not cur.ligatureComponent) or (prev.ligatureID and not prev.ligatureComponent)
            good = yes
            
        Ereturn false unless good
        
        mark2Index = @coverageIndex table.mark2Coverage, prev.id
        return false if mark2Index is -1
        
        markRecord = table.mark1Array[mark1Index]
        baseAnchor = table.mark2Array[mark2Index][markRecord.class]
        @applyAnchor markRecord, baseAnchor, prevIndex
        return true
        
      when 7 # Contextual positioning
        @applyContext table
        
      when 8 # Chaining contextual positioning
        @applyChainingContext table
        
      when 9 # Extension positioning
        @applyLookup table.lookupType, table.extension
        
      else
        throw new Error "Unsupported GPOS table: #{lookupType}"
        
    return false
        
  applyAnchor: (markRecord, baseAnchor, baseGlyphIndex) ->
    baseCoords = @getAnchor baseAnchor
    markCoords = @getAnchor markRecord.markAnchor
    
    basePos = @positions[baseGlyphIndex]
    markPos = @positions[@glyphIterator.index]
    
    markPos.xOffset = baseCoords.x - markCoords.x
    markPos.yOffset = baseCoords.y - markCoords.y
    @glyphIterator.cur.markAttachment = baseGlyphIndex
        
  getAnchor: (anchor) ->
    # TODO: contour point, device tables
    return { x: anchor.xCoordinate, y: anchor.yCoordinate }
        
  applyFeatures: ->
    super
    
    for glyph, i in @glyphs
      @fixCursiveAttachment i
      
    @fixMarkAttachment i      
    
  fixCursiveAttachment: (i) ->
    glyph = @glyphs[i]
    if glyph.cursiveAttachment?
      j = glyph.cursiveAttachment
      
      glyph.cursiveAttachment = null
      @fixCursiveAttachment j
      
      @positions[i].yOffset += @positions[j].yOffset
      
  fixMarkAttachment: ->
    for glyph, i in @glyphs when glyph.markAttachment?
      j = glyph.markAttachment
      
      @positions[i].xOffset += @positions[j].xOffset
      @positions[i].yOffset += @positions[j].yOffset
      
      Iif @direction is 'ltr'
        for k in [j...i] by 1
          @positions[i].xOffset -= @positions[k].xAdvance
          @positions[i].yOffset -= @positions[k].yAdvance
        
    return
      
module.exports = GPOSProcessor