|
| 1 | +// The `avar` table stores information on how to modify a variation along a variation axis |
| 2 | +// https://learn.microsoft.com/en-us/typography/opentype/spec/avar |
| 3 | + |
| 4 | +import check from '../check.js'; |
| 5 | +import { Parser } from '../parse.js'; |
| 6 | +import table from '../table.js'; |
| 7 | + |
| 8 | +function makeAvarAxisValueMap(n, axisValueMap) { |
| 9 | + return new table.Record('axisValueMap_' + n, [ |
| 10 | + {name: 'fromCoordinate_' + n, type: 'F2DOT14', value: axisValueMap.fromCoordinate}, |
| 11 | + {name: 'toCoordinate_' + n, type: 'F2DOT14', value: axisValueMap.toCoordinate} |
| 12 | + ]); |
| 13 | +} |
| 14 | + |
| 15 | +function makeAvarSegmentMap(n, axis) { |
| 16 | + const returnTable = new table.Record('segmentMap_' + n, [ |
| 17 | + {name: 'positionMapCount_' + n, type: 'USHORT', value: axis.axisValueMaps.length} |
| 18 | + ]); |
| 19 | + |
| 20 | + let axisValueMaps = []; |
| 21 | + for (let i = 0; i < axis.axisValueMaps.length; i++) { |
| 22 | + const valueMap = makeAvarAxisValueMap(`${n}_${i}`, axis.axisValueMaps[i]); |
| 23 | + axisValueMaps = axisValueMaps.concat(valueMap.fields); |
| 24 | + } |
| 25 | + |
| 26 | + returnTable.fields = returnTable.fields.concat(axisValueMaps); |
| 27 | + |
| 28 | + return returnTable; |
| 29 | +} |
| 30 | + |
| 31 | +function makeAvarTable(avar, fvar) { |
| 32 | + check.argument(avar.axisSegmentMaps.length === fvar.axes.length, 'avar axis count must correspond to fvar axis count'); |
| 33 | + |
| 34 | + const result = new table.Table('avar', [ |
| 35 | + {name: 'majorVersion', type: 'USHORT', value: 1}, |
| 36 | + {name: 'minorVersion', type: 'USHORT', value: 0}, |
| 37 | + {name: 'reserved', type: 'USHORT', value: 0}, |
| 38 | + {name: 'axisCount', type: 'USHORT', value: avar.axisSegmentMaps.length}, |
| 39 | + ]); |
| 40 | + |
| 41 | + for (let i = 0; i < avar.axisSegmentMaps.length; i++) { |
| 42 | + const axisRecord = makeAvarSegmentMap(i, avar.axisSegmentMaps[i]); |
| 43 | + result.fields = result.fields.concat(axisRecord.fields); |
| 44 | + } |
| 45 | + |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +function parseAvarTable(data, start, fvar) { |
| 50 | + if (!start) { |
| 51 | + start = 0; |
| 52 | + } |
| 53 | + |
| 54 | + const p = new Parser(data, start); |
| 55 | + const tableVersionMajor = p.parseUShort(); |
| 56 | + const tableVersionMinor = p.parseUShort(); |
| 57 | + |
| 58 | + if (tableVersionMajor !== 1) { |
| 59 | + console.warn(`Unsupported avar table version ${tableVersionMajor}.${tableVersionMinor}`); |
| 60 | + } |
| 61 | + |
| 62 | + p.skip('uShort', 1); // reserved |
| 63 | + const axisCount = p.parseUShort(); |
| 64 | + |
| 65 | + check.argument(axisCount === fvar.axes.length, 'avar axis count must correspond to fvar axis count'); |
| 66 | + |
| 67 | + const axisSegmentMaps = []; |
| 68 | + for (let i = 0; i < axisCount; i++) { |
| 69 | + const axisValueMaps = []; |
| 70 | + const positionMapCount = p.parseUShort(); |
| 71 | + for (let j = 0; j < positionMapCount; j++) { |
| 72 | + const fromCoordinate = p.parseF2Dot14(); |
| 73 | + const toCoordinate = p.parseF2Dot14(); |
| 74 | + axisValueMaps.push({ |
| 75 | + fromCoordinate, |
| 76 | + toCoordinate |
| 77 | + }); |
| 78 | + } |
| 79 | + axisSegmentMaps.push({ |
| 80 | + axisValueMaps |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + version: [tableVersionMajor, tableVersionMinor], |
| 86 | + axisSegmentMaps |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +export default { make: makeAvarTable, parse: parseAvarTable }; |
0 commit comments