// The Position object provides utility methods to manipulate // the GPOS position table. import Layout from './layout'; /** * @exports opentype.Position * @class * @extends opentype.Layout * @param {opentype.Font} * @constructor */ function Position(font) { Layout.call(this, font, 'gpos'); } Position.prototype = Layout.prototype; /** * Init some data for faster and easier access later. */ Position.prototype.init = function() { const script = this.getDefaultScriptName(); this.defaultKerningTables = this.getKerningTables(script); }; /** * 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} */ Position.prototype.getKerningValue = function(kerningLookups, leftIndex, rightIndex) { for (let i = 0; i < kerningLookups.length; i++) { const subtables = kerningLookups[i].subtables; for (let j = 0; j < subtables.length; j++) { const subtable = subtables[j]; const covIndex = this.getCoverageIndex(subtable.coverage, leftIndex); if (covIndex < 0) continue; switch (subtable.posFormat) { case 1: // Search Pair Adjustment Positioning Format 1 let pairSet = subtable.pairSets[covIndex]; for (let k = 0; k < pairSet.length; k++) { let pair = pairSet[k]; if (pair.secondGlyph === rightIndex) { return pair.value1 && pair.value1.xAdvance || 0; } } break; // left glyph found, not right glyph - try next subtable case 2: // Search Pair Adjustment Positioning Format 2 const class1 = this.getGlyphClass(subtable.classDef1, leftIndex); const class2 = this.getGlyphClass(subtable.classDef2, rightIndex); const pair = subtable.classRecords[class1][class2]; return pair.value1 && pair.value1.xAdvance || 0; } } } return 0; }; /** * List all kerning lookup tables. * * @param {string} [script='DFLT'] - use font.position.getDefaultScriptName() for a better default value * @param {string} [language='dflt'] * @return {object[]} The list of kerning lookup tables (may be empty), or undefined if there is no GPOS table (and we should use the kern table) */ Position.prototype.getKerningTables = function(script, language) { if (this.font.tables.gpos) { return this.getLookupTables(script, language, 'kern', 2); } }; export default Position;