getKerningValue method

dynamic getKerningValue(
  1. dynamic kerningLookups,
  2. dynamic leftIndex,
  3. dynamic rightIndex
)

Find a glyph pair in a list of lookup tables of type 2 and retrieve the xAdvance kerning value.

@param {integer} leftIndex - left glyph index @param {integer} rightIndex - right glyph index @returns {integer}

Implementation

getKerningValue(kerningLookups, leftIndex, rightIndex) {
    for (var i = 0; i < kerningLookups.length; i++) {
        var subtables = kerningLookups[i].subtables;
        for (var j = 0; j < subtables.length; j++) {
            var subtable = subtables[j];
            var covIndex = this.getCoverageIndex(subtable.coverage, leftIndex);
            if (covIndex < 0) continue;
            switch (subtable.posFormat) {
                case 1:
                    // Search Pair Adjustment Positioning Format 1
                    var pairSet = subtable.pairSets[covIndex];
                    for (var k = 0; k < pairSet.length; k++) {
                        var pair = pairSet[k];
                        if (pair.secondGlyph == rightIndex) {
                          if(pair.value1 != null) {
                            return pair.value1.xAdvance ?? 0;
                          } else {
                            return 0;
                          }
                        }
                    }
                    break;      // left glyph found, not right glyph - try next subtable
                case 2:
                    // Search Pair Adjustment Positioning Format 2
                    var class1 = this.getGlyphClass(subtable.classDef1, leftIndex);
                    var class2 = this.getGlyphClass(subtable.classDef2, rightIndex);
                    var pair = subtable.classRecords[class1][class2];
                    // return pair.value1 && pair.value1.xAdvance ?? 0;
                    if(pair.value1 != null) {
                      return pair.value1.xAdvance ?? 0;
                    } else {
                      return 0;
                    }
            }
        }
    }
    return 0;
}