@cantoo/fontkit 2.0.4 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/dist/browser-module.mjs +4448 -1142
  2. package/dist/browser-module.mjs.map +1 -1
  3. package/dist/browser.cjs +4449 -1142
  4. package/dist/browser.cjs.map +1 -1
  5. package/dist/main.cjs +4449 -1142
  6. package/dist/main.cjs.map +1 -1
  7. package/dist/module.mjs +4448 -1142
  8. package/dist/module.mjs.map +1 -1
  9. package/package.json +4 -1
  10. package/src/CmapProcessor.js +60 -21
  11. package/src/DFont.js +114 -14
  12. package/src/TTFFont.js +260 -62
  13. package/src/TrueTypeCollection.js +59 -15
  14. package/src/WOFF2Font.js +128 -22
  15. package/src/WOFFFont.js +45 -8
  16. package/src/aat/AATFeatureMap.js +83 -40
  17. package/src/aat/AATLayoutEngine.js +34 -2
  18. package/src/aat/AATLookupTable.js +24 -6
  19. package/src/aat/AATMorxProcessor.js +202 -65
  20. package/src/aat/AATStateMachine.js +35 -3
  21. package/src/base.js +20 -1
  22. package/src/cff/CFFCharsets.js +3 -0
  23. package/src/cff/CFFDict.js +81 -11
  24. package/src/cff/CFFEncodings.js +2 -0
  25. package/src/cff/CFFFont.js +120 -24
  26. package/src/cff/CFFIndex.js +66 -9
  27. package/src/cff/CFFOperand.js +53 -28
  28. package/src/cff/CFFPointer.js +41 -2
  29. package/src/cff/CFFPrivateDict.js +34 -24
  30. package/src/cff/CFFStandardStrings.js +68 -67
  31. package/src/cff/CFFTop.js +161 -68
  32. package/src/decorators.js +34 -15
  33. package/src/encodings.js +133 -116
  34. package/src/fs.js +10 -0
  35. package/src/glyph/BBox.js +14 -0
  36. package/src/glyph/CFFGlyph.js +293 -152
  37. package/src/glyph/COLRGlyph.js +40 -5
  38. package/src/glyph/Glyph.js +72 -13
  39. package/src/glyph/GlyphVariationProcessor.js +158 -62
  40. package/src/glyph/Path.js +119 -32
  41. package/src/glyph/SBIXGlyph.js +57 -8
  42. package/src/glyph/StandardNames.js +1 -0
  43. package/src/glyph/TTFGlyph.js +203 -63
  44. package/src/glyph/TTFGlyphEncoder.js +58 -26
  45. package/src/glyph/WOFF2Glyph.js +25 -6
  46. package/src/layout/GlyphPosition.js +6 -0
  47. package/src/layout/GlyphRun.js +28 -10
  48. package/src/layout/KernProcessor.js +63 -4
  49. package/src/layout/LayoutEngine.js +99 -26
  50. package/src/layout/Script.js +52 -26
  51. package/src/layout/UnicodeLayoutEngine.js +34 -10
  52. package/src/opentype/GPOSProcessor.js +156 -45
  53. package/src/opentype/GSUBProcessor.js +92 -33
  54. package/src/opentype/GlyphInfo.js +53 -5
  55. package/src/opentype/GlyphIterator.js +51 -2
  56. package/src/opentype/OTLayoutEngine.js +66 -8
  57. package/src/opentype/OTProcessor.js +284 -71
  58. package/src/opentype/ShapingPlan.js +52 -5
  59. package/src/opentype/shapers/ArabicShaper.js +27 -8
  60. package/src/opentype/shapers/DefaultShaper.js +54 -10
  61. package/src/opentype/shapers/HangulShaper.js +120 -42
  62. package/src/opentype/shapers/HebrewShaper.js +50 -16
  63. package/src/opentype/shapers/IndicShaper.js +159 -44
  64. package/src/opentype/shapers/ThaiShaper.js +50 -19
  65. package/src/opentype/shapers/UniversalShaper.js +83 -17
  66. package/src/opentype/shapers/index.js +48 -35
  67. package/src/opentype/shapers/indic-data.js +2 -2
  68. package/src/packages/unicode-properties/data-trie.js +2 -1
  69. package/src/packages/unicode-properties/data.js +13 -1
  70. package/src/packages/unicode-properties/dist/index.cjs +23 -10
  71. package/src/packages/unicode-properties/dist/index.cjs.map +2 -2
  72. package/src/packages/unicode-properties/dist/index.mjs +22 -10
  73. package/src/packages/unicode-properties/dist/index.mjs.map +2 -2
  74. package/src/packages/unicode-properties/generate.js +3 -4
  75. package/src/packages/unicode-properties/index.js +95 -30
  76. package/src/packages/unicode-properties/package.json +1 -1
  77. package/src/packages/unicode-trie/builder.js +146 -29
  78. package/src/packages/unicode-trie/dist/builder.cjs +135 -25
  79. package/src/packages/unicode-trie/dist/builder.cjs.map +2 -2
  80. package/src/packages/unicode-trie/dist/builder.mjs +134 -25
  81. package/src/packages/unicode-trie/dist/builder.mjs.map +2 -2
  82. package/src/packages/unicode-trie/dist/index.cjs +22 -9
  83. package/src/packages/unicode-trie/dist/index.cjs.map +2 -2
  84. package/src/packages/unicode-trie/dist/index.mjs +21 -9
  85. package/src/packages/unicode-trie/dist/index.mjs.map +2 -2
  86. package/src/packages/unicode-trie/index.js +63 -12
  87. package/src/packages/unicode-trie/package.json +1 -1
  88. package/src/packages/unicode-trie/swap.js +15 -2
  89. package/src/subset/CFFSubset.js +112 -22
  90. package/src/subset/Subset.js +23 -3
  91. package/src/subset/TTFSubset.js +84 -41
  92. package/src/tables/BASE.js +28 -27
  93. package/src/tables/COLR.js +10 -9
  94. package/src/tables/CPAL.js +2 -1
  95. package/src/tables/DSIG.js +8 -7
  96. package/src/tables/EBDT.js +10 -4
  97. package/src/tables/EBLC.js +70 -10
  98. package/src/tables/GDEF.js +18 -17
  99. package/src/tables/GPOS.js +153 -90
  100. package/src/tables/GSUB.js +50 -39
  101. package/src/tables/HVAR.js +51 -5
  102. package/src/tables/JSTF.js +17 -16
  103. package/src/tables/LTSH.js +4 -3
  104. package/src/tables/OS2.js +50 -49
  105. package/src/tables/PCLT.js +16 -15
  106. package/src/tables/STAT.js +46 -6
  107. package/src/tables/VDMX.js +18 -17
  108. package/src/tables/VORG.js +8 -7
  109. package/src/tables/WOFF2Directory.js +57 -9
  110. package/src/tables/WOFFDirectory.js +32 -20
  111. package/src/tables/aat.js +160 -35
  112. package/src/tables/avar.js +1 -0
  113. package/src/tables/bsln.js +3 -2
  114. package/src/tables/cmap.js +83 -63
  115. package/src/tables/cvt.js +1 -0
  116. package/src/tables/directory.js +56 -28
  117. package/src/tables/feat.js +24 -2
  118. package/src/tables/fpgm.js +1 -0
  119. package/src/tables/fvar.js +32 -4
  120. package/src/tables/gasp.js +6 -5
  121. package/src/tables/glyf.js +2 -1
  122. package/src/tables/gvar.js +29 -2
  123. package/src/tables/hdmx.js +18 -7
  124. package/src/tables/head.js +17 -16
  125. package/src/tables/hhea.js +15 -14
  126. package/src/tables/hmtx.js +18 -2
  127. package/src/tables/index.js +1 -5
  128. package/src/tables/just.js +15 -4
  129. package/src/tables/kern.js +84 -47
  130. package/src/tables/loca.js +23 -10
  131. package/src/tables/maxp.js +16 -15
  132. package/src/tables/morx.js +27 -20
  133. package/src/tables/name.js +120 -39
  134. package/src/tables/opbd.js +2 -1
  135. package/src/tables/opentype.js +126 -91
  136. package/src/tables/post.js +18 -11
  137. package/src/tables/prep.js +1 -0
  138. package/src/tables/sbix.js +12 -1
  139. package/src/tables/variations.js +29 -9
  140. package/src/tables/vhea.js +15 -14
  141. package/src/tables/vmtx.js +20 -4
  142. package/src/utils.js +15 -0
@@ -1,9 +1,29 @@
1
1
  import * as r from 'restructure';
2
2
 
3
+ /** @typedef {import('restructure').StructValue} StructValue */
4
+
5
+ /**
6
+ * @param {StructValue} t
7
+ * @param {number} depth
8
+ * @param {number} nameIndex
9
+ * @returns {unknown}
10
+ */
11
+ function featureNameAt(t, depth, nameIndex) {
12
+ let cur = t;
13
+ for (let i = 0; i < depth; i++) {
14
+ cur = /** @type {StructValue} */ (cur.parent);
15
+ }
16
+ let name = /** @type {StructValue} */ (cur.name);
17
+ let records = /** @type {StructValue} */ (name.records);
18
+ let fontFeatures = /** @type {Record<number, unknown>} */ (records.fontFeatures);
19
+ return fontFeatures[nameIndex];
20
+ }
21
+
3
22
  let Setting = new r.Struct({
4
23
  setting: r.uint16,
5
24
  nameIndex: r.int16,
6
- name: t => t.parent.parent.parent.name.records.fontFeatures[t.nameIndex]
25
+ /** @param {StructValue} t @returns {unknown} */
26
+ name: t => featureNameAt(t, 3, /** @type {number} */ (t.nameIndex))
7
27
  });
8
28
 
9
29
  let FeatureName = new r.Struct({
@@ -16,9 +36,11 @@ let FeatureName = new r.Struct({
16
36
  ]),
17
37
  defaultSetting: r.uint8,
18
38
  nameIndex: r.int16,
19
- name: t => t.parent.parent.name.records.fontFeatures[t.nameIndex]
39
+ /** @param {StructValue} t @returns {unknown} */
40
+ name: t => featureNameAt(t, 2, /** @type {number} */ (t.nameIndex))
20
41
  });
21
42
 
43
+ /** @type {import('restructure').Struct} */
22
44
  export default new r.Struct({
23
45
  version: r.fixed32,
24
46
  featureNameCount: r.uint16,
@@ -3,6 +3,7 @@ import * as r from 'restructure';
3
3
  // A list of instructions that are executed once when a font is first used.
4
4
  // These instructions are known as the font program. The main use of this table
5
5
  // is for the definition of functions that are used in many different glyph programs.
6
+ /** @type {import('restructure').Struct} */
6
7
  export default new r.Struct({
7
8
  instructions: new r.Array(r.uint8)
8
9
  });
@@ -1,5 +1,21 @@
1
1
  import * as r from 'restructure';
2
2
 
3
+ /** @typedef {import('restructure').StructValue} StructValue */
4
+
5
+ /**
6
+ * @param {StructValue} t
7
+ * @param {number} nameID
8
+ * @returns {unknown}
9
+ */
10
+ function featureName(t, nameID) {
11
+ let parent = /** @type {StructValue} */ (t.parent);
12
+ let grand = /** @type {StructValue} */ (parent.parent);
13
+ let name = /** @type {StructValue} */ (grand.name);
14
+ let records = /** @type {StructValue} */ (name.records);
15
+ let fontFeatures = /** @type {Record<number, unknown>} */ (records.fontFeatures);
16
+ return fontFeatures[nameID];
17
+ }
18
+
3
19
  let Axis = new r.Struct({
4
20
  axisTag: new r.String(4),
5
21
  minValue: r.fixed32,
@@ -7,17 +23,29 @@ let Axis = new r.Struct({
7
23
  maxValue: r.fixed32,
8
24
  flags: r.uint16,
9
25
  nameID: r.uint16,
10
- name: t => t.parent.parent.name.records.fontFeatures[t.nameID]
26
+ /** @param {StructValue} t @returns {unknown} */
27
+ name: t => featureName(t, /** @type {number} */ (t.nameID))
11
28
  });
12
29
 
13
30
  let Instance = new r.Struct({
14
31
  nameID: r.uint16,
15
- name: t => t.parent.parent.name.records.fontFeatures[t.nameID],
32
+ /** @param {StructValue} t @returns {unknown} */
33
+ name: t => featureName(t, /** @type {number} */ (t.nameID)),
16
34
  flags: r.uint16,
17
- coord: new r.Array(r.fixed32, t => t.parent.axisCount),
18
- postscriptNameID: new r.Optional(r.uint16, t => t.parent.instanceSize - t._currentOffset > 0)
35
+ coord: new r.Array(
36
+ r.fixed32,
37
+ /** @param {StructValue} t @returns {number} */
38
+ t => /** @type {number} */ (/** @type {StructValue} */ (t.parent).axisCount)
39
+ ),
40
+ postscriptNameID: new r.Optional(
41
+ r.uint16,
42
+ /** @param {StructValue} t @returns {boolean} */
43
+ t => /** @type {number} */ (/** @type {StructValue} */ (t.parent).instanceSize)
44
+ - /** @type {number} */ (t._currentOffset) > 0
45
+ )
19
46
  });
20
47
 
48
+ /** @type {import('restructure').Struct} */
21
49
  export default new r.Struct({
22
50
  version: r.fixed32,
23
51
  offsetToData: r.uint16,
@@ -1,15 +1,16 @@
1
1
  import * as r from 'restructure';
2
2
 
3
3
  let GaspRange = new r.Struct({
4
- rangeMaxPPEM: r.uint16, // Upper limit of range, in ppem
5
- rangeGaspBehavior: new r.Bitfield(r.uint16, [ // Flags describing desired rasterizer behavior
4
+ rangeMaxPPEM: r.uint16, // Upper limit of range, in ppem
5
+ rangeGaspBehavior: new r.Bitfield(r.uint16, [ // Flags describing desired rasterizer behavior
6
6
  'grayscale', 'gridfit',
7
- 'symmetricSmoothing', 'symmetricGridfit' // only in version 1, for ClearType
7
+ 'symmetricSmoothing', 'symmetricGridfit' // only in version 1, for ClearType
8
8
  ])
9
9
  });
10
10
 
11
+ /** @type {import('restructure').Struct} */
11
12
  export default new r.Struct({
12
- version: r.uint16, // set to 0
13
- numRanges: r.uint16,
13
+ version: r.uint16, // set to 0
14
+ numRanges: r.uint16,
14
15
  gaspRanges: new r.Array(GaspRange, 'numRanges') // Sorted by ppem
15
16
  });
@@ -1,4 +1,5 @@
1
1
  import * as r from 'restructure';
2
2
 
3
3
  // only used for encoding
4
- export default new r.Array(new r.Buffer);
4
+ /** @type {import('restructure').ArrayT} */
5
+ export default new r.Array(new r.Buffer());
@@ -1,17 +1,32 @@
1
1
  import * as r from 'restructure';
2
2
 
3
+ /** @typedef {import('restructure').BaseType} BaseType */
4
+ /** @typedef {import('restructure').DecodeStream} DecodeStream */
5
+ /** @typedef {import('restructure').StructValue} StructValue */
6
+
3
7
  let shortFrac = new r.Fixed(16, 'BE', 14);
8
+
9
+ /**
10
+ * Offset whose width depends on gvar.flags (Apple short/long format).
11
+ * @implements {BaseType}
12
+ */
4
13
  class Offset {
14
+ /**
15
+ * @param {DecodeStream} stream
16
+ * @param {StructValue | null | undefined} parent
17
+ * @returns {number}
18
+ */
5
19
  static decode(stream, parent) {
6
20
  // In short format, offsets are multiplied by 2.
7
21
  // This doesn't seem to be documented by Apple, but it
8
22
  // is implemented this way in Freetype.
9
- return parent.flags
23
+ return parent && parent.flags
10
24
  ? stream.readUInt32BE()
11
25
  : stream.readUInt16BE() * 2;
12
26
  }
13
27
  }
14
28
 
29
+ /** @type {import('restructure').Struct} */
15
30
  let gvar = new r.Struct({
16
31
  version: r.uint16,
17
32
  reserved: new r.Reserved(r.uint16),
@@ -21,7 +36,19 @@ let gvar = new r.Struct({
21
36
  glyphCount: r.uint16,
22
37
  flags: r.uint16,
23
38
  offsetToData: r.uint32,
24
- offsets: new r.Array(new r.Pointer(Offset, 'void', { relativeTo: ctx => ctx.offsetToData, allowNull: false }), t => t.glyphCount + 1)
39
+ offsets: new r.Array(
40
+ new r.Pointer(
41
+ /** @type {BaseType} */ (Offset),
42
+ 'void',
43
+ {
44
+ /** @param {StructValue} ctx @returns {number} */
45
+ relativeTo: ctx => /** @type {number} */ (ctx.offsetToData),
46
+ allowNull: false
47
+ }
48
+ ),
49
+ /** @param {StructValue} t @returns {number} */
50
+ t => /** @type {number} */ (t.glyphCount) + 1
51
+ )
25
52
  });
26
53
 
27
54
  export default gvar;
@@ -1,15 +1,26 @@
1
1
  import * as r from 'restructure';
2
2
 
3
+ /** @typedef {import('restructure').StructValue} StructValue */
4
+
3
5
  let DeviceRecord = new r.Struct({
4
- pixelSize: r.uint8,
5
- maximumWidth: r.uint8,
6
- widths: new r.Array(r.uint8, t => t.parent.parent.maxp.numGlyphs)
6
+ pixelSize: r.uint8,
7
+ maximumWidth: r.uint8,
8
+ widths: new r.Array(
9
+ r.uint8,
10
+ /** @param {StructValue} t @returns {number} */
11
+ (t) => {
12
+ let parent = /** @type {StructValue} */ (t.parent);
13
+ let grand = /** @type {StructValue} */ (parent.parent);
14
+ return /** @type {number} */ (/** @type {StructValue} */ (grand.maxp).numGlyphs);
15
+ }
16
+ )
7
17
  });
8
18
 
9
19
  // The Horizontal Device Metrics table stores integer advance widths scaled to particular pixel sizes
20
+ /** @type {import('restructure').Struct} */
10
21
  export default new r.Struct({
11
- version: r.uint16,
12
- numRecords: r.int16,
13
- sizeDeviceRecord: r.int32,
14
- records: new r.Array(DeviceRecord, 'numRecords')
22
+ version: r.uint16,
23
+ numRecords: r.int16,
24
+ sizeDeviceRecord: r.int32,
25
+ records: new r.Array(DeviceRecord, 'numRecords')
15
26
  });
@@ -1,25 +1,26 @@
1
1
  import * as r from 'restructure';
2
2
 
3
3
  // font header
4
+ /** @type {import('restructure').Struct} */
4
5
  export default new r.Struct({
5
- version: r.int32, // 0x00010000 (version 1.0)
6
- revision: r.int32, // set by font manufacturer
6
+ version: r.int32, // 0x00010000 (version 1.0)
7
+ revision: r.int32, // set by font manufacturer
7
8
  checkSumAdjustment: r.uint32,
8
- magicNumber: r.uint32, // set to 0x5F0F3CF5
9
- flags: r.uint16,
10
- unitsPerEm: r.uint16, // range from 64 to 16384
11
- created: new r.Array(r.int32, 2),
12
- modified: new r.Array(r.int32, 2),
13
- xMin: r.int16, // for all glyph bounding boxes
14
- yMin: r.int16, // for all glyph bounding boxes
15
- xMax: r.int16, // for all glyph bounding boxes
16
- yMax: r.int16, // for all glyph bounding boxes
17
- macStyle: new r.Bitfield(r.uint16, [
9
+ magicNumber: r.uint32, // set to 0x5F0F3CF5
10
+ flags: r.uint16,
11
+ unitsPerEm: r.uint16, // range from 64 to 16384
12
+ created: new r.Array(r.int32, 2),
13
+ modified: new r.Array(r.int32, 2),
14
+ xMin: r.int16, // for all glyph bounding boxes
15
+ yMin: r.int16, // for all glyph bounding boxes
16
+ xMax: r.int16, // for all glyph bounding boxes
17
+ yMax: r.int16, // for all glyph bounding boxes
18
+ macStyle: new r.Bitfield(r.uint16, [
18
19
  'bold', 'italic', 'underline', 'outline',
19
20
  'shadow', 'condensed', 'extended'
20
21
  ]),
21
- lowestRecPPEM: r.uint16, // smallest readable size in pixels
22
- fontDirectionHint: r.int16,
23
- indexToLocFormat: r.int16, // 0 for short offsets, 1 for long
24
- glyphDataFormat: r.int16 // 0 for current format
22
+ lowestRecPPEM: r.uint16, // smallest readable size in pixels
23
+ fontDirectionHint: r.int16,
24
+ indexToLocFormat: r.int16, // 0 for short offsets, 1 for long
25
+ glyphDataFormat: r.int16 // 0 for current format
25
26
  });
@@ -1,19 +1,20 @@
1
1
  import * as r from 'restructure';
2
2
 
3
3
  // horizontal header
4
+ /** @type {import('restructure').Struct} */
4
5
  export default new r.Struct({
5
- version: r.int32,
6
- ascent: r.int16, // Distance from baseline of highest ascender
7
- descent: r.int16, // Distance from baseline of lowest descender
8
- lineGap: r.int16, // Typographic line gap
9
- advanceWidthMax: r.uint16, // Maximum advance width value in 'hmtx' table
10
- minLeftSideBearing: r.int16, // Maximum advance width value in 'hmtx' table
11
- minRightSideBearing: r.int16, // Minimum right sidebearing value
12
- xMaxExtent: r.int16,
13
- caretSlopeRise: r.int16, // Used to calculate the slope of the cursor (rise/run); 1 for vertical
14
- caretSlopeRun: r.int16, // 0 for vertical
15
- caretOffset: r.int16, // Set to 0 for non-slanted fonts
16
- reserved: new r.Reserved(r.int16, 4),
17
- metricDataFormat: r.int16, // 0 for current format
18
- numberOfMetrics: r.uint16 // Number of advance widths in 'hmtx' table
6
+ version: r.int32,
7
+ ascent: r.int16, // Distance from baseline of highest ascender
8
+ descent: r.int16, // Distance from baseline of lowest descender
9
+ lineGap: r.int16, // Typographic line gap
10
+ advanceWidthMax: r.uint16, // Maximum advance width value in 'hmtx' table
11
+ minLeftSideBearing: r.int16, // Maximum advance width value in 'hmtx' table
12
+ minRightSideBearing: r.int16, // Minimum right sidebearing value
13
+ xMaxExtent: r.int16,
14
+ caretSlopeRise: r.int16, // Used to calculate the slope of the cursor (rise/run); 1 for vertical
15
+ caretSlopeRun: r.int16, // 0 for vertical
16
+ caretOffset: r.int16, // Set to 0 for non-slanted fonts
17
+ reserved: new r.Reserved(r.int16, 4),
18
+ metricDataFormat: r.int16, // 0 for current format
19
+ numberOfMetrics: r.uint16 // Number of advance widths in 'hmtx' table
19
20
  });
@@ -1,11 +1,27 @@
1
1
  import * as r from 'restructure';
2
2
 
3
+ /** @typedef {import('restructure').StructValue} StructValue */
4
+
3
5
  let HmtxEntry = new r.Struct({
4
6
  advance: r.uint16,
5
7
  bearing: r.int16
6
8
  });
7
9
 
10
+ /** @type {import('restructure').Struct} */
8
11
  export default new r.Struct({
9
- metrics: new r.LazyArray(HmtxEntry, t => t.parent.hhea.numberOfMetrics),
10
- bearings: new r.LazyArray(r.int16, t => t.parent.maxp.numGlyphs - t.parent.hhea.numberOfMetrics)
12
+ metrics: new r.LazyArray(
13
+ HmtxEntry,
14
+ /** @param {StructValue} t @returns {number} */
15
+ t => /** @type {number} */ (/** @type {StructValue} */ (/** @type {StructValue} */ (t.parent).hhea).numberOfMetrics)
16
+ ),
17
+ bearings: new r.LazyArray(
18
+ r.int16,
19
+ /** @param {StructValue} t @returns {number} */
20
+ (t) => {
21
+ let parent = /** @type {StructValue} */ (t.parent);
22
+ let maxp = /** @type {StructValue} */ (parent.maxp);
23
+ let hhea = /** @type {StructValue} */ (parent.hhea);
24
+ return /** @type {number} */ (maxp.numGlyphs) - /** @type {number} */ (hhea.numberOfMetrics);
25
+ }
26
+ )
11
27
  });
@@ -1,3 +1,4 @@
1
+ /** @type {Record<string, import('../../types/fontkit').TableCodec>} */
1
2
  let tables = {};
2
3
  export default tables;
3
4
 
@@ -20,7 +21,6 @@ tables.name = name;
20
21
  tables['OS/2'] = OS2;
21
22
  tables.post = post;
22
23
 
23
-
24
24
  // TrueType Outlines
25
25
  import cvt from './cvt';
26
26
  import fpgm from './fpgm';
@@ -34,7 +34,6 @@ tables.prep = prep;
34
34
  tables['cvt '] = cvt;
35
35
  tables.glyf = glyf;
36
36
 
37
-
38
37
  // PostScript Outlines
39
38
  import CFFFont from '../cff/CFFFont';
40
39
  import VORG from './VORG';
@@ -43,7 +42,6 @@ tables['CFF '] = CFFFont;
43
42
  tables['CFF2'] = CFFFont;
44
43
  tables.VORG = VORG;
45
44
 
46
-
47
45
  // Bitmap Glyphs
48
46
  import EBLC from './EBLC';
49
47
  import sbix from './sbix';
@@ -56,7 +54,6 @@ tables.sbix = sbix;
56
54
  tables.COLR = COLR;
57
55
  tables.CPAL = CPAL;
58
56
 
59
-
60
57
  // Advanced OpenType Tables
61
58
  import BASE from './BASE';
62
59
  import GDEF from './GDEF';
@@ -98,7 +95,6 @@ tables.VDMX = VDMX;
98
95
  tables.vhea = vhea;
99
96
  tables.vmtx = vmtx;
100
97
 
101
-
102
98
  // Apple Advanced Typography Tables
103
99
  import avar from './avar';
104
100
  import bsln from './bsln';
@@ -1,11 +1,13 @@
1
1
  import * as r from 'restructure';
2
2
  import { LookupTable, StateTable1 } from './aat';
3
3
 
4
+ /** @typedef {import('restructure').StructValue} StructValue */
5
+
4
6
  let ClassTable = new r.Struct({
5
7
  length: r.uint16,
6
8
  coverage: r.uint16,
7
9
  subFeatureFlags: r.uint32,
8
- stateTable: new StateTable1
10
+ stateTable: StateTable1()
9
11
  });
10
12
 
11
13
  let WidthDeltaRecord = new r.Struct({
@@ -58,21 +60,30 @@ let Action = new r.Struct({
58
60
  actionType: r.uint16,
59
61
  actionLength: r.uint32,
60
62
  actionData: ActionData,
61
- padding: new r.Reserved(r.uint8, t => t.actionLength - t._currentOffset)
63
+ padding: new r.Reserved(
64
+ r.uint8,
65
+ /** @param {StructValue} t @returns {number} */
66
+ t => /** @type {number} */ (t.actionLength) - /** @type {number} */ (t._currentOffset)
67
+ )
62
68
  });
63
69
 
64
70
  let PostcompensationAction = new r.Array(Action, r.uint32);
65
71
  let PostCompensationTable = new r.Struct({
66
- lookupTable: new LookupTable(new r.Pointer(r.uint16, PostcompensationAction))
72
+ lookupTable: LookupTable(new r.Pointer(r.uint16, PostcompensationAction))
67
73
  });
68
74
 
69
75
  let JustificationTable = new r.Struct({
70
76
  classTable: new r.Pointer(r.uint16, ClassTable, { type: 'parent' }),
71
77
  wdcOffset: r.uint16,
72
78
  postCompensationTable: new r.Pointer(r.uint16, PostCompensationTable, { type: 'parent' }),
73
- widthDeltaClusters: new LookupTable(new r.Pointer(r.uint16, WidthDeltaCluster, { type: 'parent', relativeTo: ctx => ctx.wdcOffset }))
79
+ widthDeltaClusters: LookupTable(new r.Pointer(r.uint16, WidthDeltaCluster, {
80
+ type: 'parent',
81
+ /** @param {StructValue} ctx @returns {number} */
82
+ relativeTo: ctx => /** @type {number} */ (ctx.wdcOffset)
83
+ }))
74
84
  });
75
85
 
86
+ /** @type {import('restructure').Struct} */
76
87
  export default new r.Struct({
77
88
  version: r.uint32,
78
89
  format: r.uint16,
@@ -1,91 +1,128 @@
1
1
  import * as r from 'restructure';
2
2
 
3
+ /** @typedef {import('restructure').StructValue} StructValue */
4
+
3
5
  let KernPair = new r.Struct({
4
- left: r.uint16,
5
- right: r.uint16,
6
- value: r.int16
6
+ left: r.uint16,
7
+ right: r.uint16,
8
+ value: r.int16
7
9
  });
8
10
 
9
11
  let ClassTable = new r.Struct({
10
12
  firstGlyph: r.uint16,
11
13
  nGlyphs: r.uint16,
12
14
  offsets: new r.Array(r.uint16, 'nGlyphs'),
13
- max: t => t.offsets.length && Math.max.apply(Math, t.offsets)
15
+ /**
16
+ * @param {StructValue} t
17
+ * @returns {number}
18
+ */
19
+ max: (t) => {
20
+ let offsets = /** @type {number[]} */ (t.offsets);
21
+ return offsets.length ? Math.max.apply(Math, offsets) : 0;
22
+ }
14
23
  });
15
24
 
16
25
  let Kern2Array = new r.Struct({
17
- off: t => t._startOffset - t.parent.parent._startOffset,
18
- len: t => (((t.parent.leftTable.max - t.off) / t.parent.rowWidth) + 1) * (t.parent.rowWidth / 2),
26
+ /**
27
+ * @param {StructValue} t
28
+ * @returns {number}
29
+ */
30
+ off: t => /** @type {number} */ (t._startOffset)
31
+ - /** @type {number} */ (/** @type {StructValue} */ (/** @type {StructValue} */ (t.parent).parent)._startOffset),
32
+ /**
33
+ * @param {StructValue} t
34
+ * @returns {number}
35
+ */
36
+ len: (t) => {
37
+ let parent = /** @type {StructValue} */ (t.parent);
38
+ let leftTable = /** @type {StructValue} */ (parent.leftTable);
39
+ let rowWidth = /** @type {number} */ (parent.rowWidth);
40
+ let off = /** @type {number} */ (t.off);
41
+ return (((/** @type {number} */ (leftTable.max) - off) / rowWidth) + 1) * (rowWidth / 2);
42
+ },
19
43
  values: new r.LazyArray(r.int16, 'len')
20
44
  });
21
45
 
22
46
  let KernSubtable = new r.VersionedStruct('format', {
23
47
  0: {
24
- nPairs: r.uint16,
25
- searchRange: r.uint16,
26
- entrySelector: r.uint16,
27
- rangeShift: r.uint16,
28
- pairs: new r.Array(KernPair, 'nPairs')
48
+ nPairs: r.uint16,
49
+ searchRange: r.uint16,
50
+ entrySelector: r.uint16,
51
+ rangeShift: r.uint16,
52
+ pairs: new r.Array(KernPair, 'nPairs')
29
53
  },
30
54
 
31
55
  2: {
32
- rowWidth: r.uint16,
33
- leftTable: new r.Pointer(r.uint16, ClassTable, {type: 'parent'}),
34
- rightTable: new r.Pointer(r.uint16, ClassTable, {type: 'parent'}),
35
- array: new r.Pointer(r.uint16, Kern2Array, {type: 'parent'})
56
+ rowWidth: r.uint16,
57
+ leftTable: new r.Pointer(r.uint16, ClassTable, { type: 'parent' }),
58
+ rightTable: new r.Pointer(r.uint16, ClassTable, { type: 'parent' }),
59
+ array: new r.Pointer(r.uint16, Kern2Array, { type: 'parent' })
36
60
  },
37
61
 
38
62
  3: {
39
- glyphCount: r.uint16,
40
- kernValueCount: r.uint8,
41
- leftClassCount: r.uint8,
42
- rightClassCount: r.uint8,
43
- flags: r.uint8,
44
- kernValue: new r.Array(r.int16, 'kernValueCount'),
45
- leftClass: new r.Array(r.uint8, 'glyphCount'),
46
- rightClass: new r.Array(r.uint8, 'glyphCount'),
47
- kernIndex: new r.Array(r.uint8, t => t.leftClassCount * t.rightClassCount)
63
+ glyphCount: r.uint16,
64
+ kernValueCount: r.uint8,
65
+ leftClassCount: r.uint8,
66
+ rightClassCount: r.uint8,
67
+ flags: r.uint8,
68
+ kernValue: new r.Array(r.int16, 'kernValueCount'),
69
+ leftClass: new r.Array(r.uint8, 'glyphCount'),
70
+ rightClass: new r.Array(r.uint8, 'glyphCount'),
71
+ kernIndex: new r.Array(
72
+ r.uint8,
73
+ /** @param {StructValue} t @returns {number} */
74
+ t => /** @type {number} */ (t.leftClassCount) * /** @type {number} */ (t.rightClassCount)
75
+ )
48
76
  }
49
77
  });
50
78
 
51
79
  let KernTable = new r.VersionedStruct('version', {
52
80
  0: { // Microsoft uses this format
53
- subVersion: r.uint16, // Microsoft has an extra sub-table version number
54
- length: r.uint16, // Length of the subtable, in bytes
55
- format: r.uint8, // Format of subtable
56
- coverage: new r.Bitfield(r.uint8, [
57
- 'horizontal', // 1 if table has horizontal data, 0 if vertical
58
- 'minimum', // If set to 1, the table has minimum values. If set to 0, the table has kerning values.
59
- 'crossStream', // If set to 1, kerning is perpendicular to the flow of the text
60
- 'override' // If set to 1 the value in this table replaces the accumulated value
81
+ subVersion: r.uint16, // Microsoft has an extra sub-table version number
82
+ length: r.uint16, // Length of the subtable, in bytes
83
+ format: r.uint8, // Format of subtable
84
+ coverage: new r.Bitfield(r.uint8, [
85
+ 'horizontal', // 1 if table has horizontal data, 0 if vertical
86
+ 'minimum', // If set to 1, the table has minimum values. If set to 0, the table has kerning values.
87
+ 'crossStream', // If set to 1, kerning is perpendicular to the flow of the text
88
+ 'override' // If set to 1 the value in this table replaces the accumulated value
61
89
  ]),
62
- subtable: KernSubtable,
63
- padding: new r.Reserved(r.uint8, t => t.length - t._currentOffset)
90
+ subtable: KernSubtable,
91
+ padding: new r.Reserved(
92
+ r.uint8,
93
+ /** @param {StructValue} t @returns {number} */
94
+ t => /** @type {number} */ (t.length) - /** @type {number} */ (t._currentOffset)
95
+ )
64
96
  },
65
97
  1: { // Apple uses this format
66
- length: r.uint32,
67
- coverage: new r.Bitfield(r.uint8, [
98
+ length: r.uint32,
99
+ coverage: new r.Bitfield(r.uint8, [
68
100
  null, null, null, null, null,
69
- 'variation', // Set if table has variation kerning values
70
- 'crossStream', // Set if table has cross-stream kerning values
71
- 'vertical' // Set if table has vertical kerning values
101
+ 'variation', // Set if table has variation kerning values
102
+ 'crossStream', // Set if table has cross-stream kerning values
103
+ 'vertical' // Set if table has vertical kerning values
72
104
  ]),
73
- format: r.uint8,
105
+ format: r.uint8,
74
106
  tupleIndex: r.uint16,
75
- subtable: KernSubtable,
76
- padding: new r.Reserved(r.uint8, t => t.length - t._currentOffset)
107
+ subtable: KernSubtable,
108
+ padding: new r.Reserved(
109
+ r.uint8,
110
+ /** @param {StructValue} t @returns {number} */
111
+ t => /** @type {number} */ (t.length) - /** @type {number} */ (t._currentOffset)
112
+ )
77
113
  }
78
114
  });
79
115
 
116
+ /** @type {import('restructure').VersionedStruct} */
80
117
  export default new r.VersionedStruct(r.uint16, {
81
118
  0: { // Microsoft Version
82
- nTables: r.uint16,
83
- tables: new r.Array(KernTable, 'nTables')
119
+ nTables: r.uint16,
120
+ tables: new r.Array(KernTable, 'nTables')
84
121
  },
85
122
 
86
123
  1: { // Apple Version
87
- reserved: new r.Reserved(r.uint16), // the other half of the version number
88
- nTables: r.uint32,
89
- tables: new r.Array(KernTable, 'nTables')
124
+ reserved: new r.Reserved(r.uint16), // the other half of the version number
125
+ nTables: r.uint32,
126
+ tables: new r.Array(KernTable, 'nTables')
90
127
  }
91
128
  });
@@ -1,5 +1,16 @@
1
1
  import * as r from 'restructure';
2
2
 
3
+ /** @typedef {import('restructure').StructValue} StructValue */
4
+
5
+ /**
6
+ * @typedef {StructValue & {
7
+ * version: number,
8
+ * offsets: number[],
9
+ * _processed?: boolean
10
+ * }} LocaValue
11
+ */
12
+
13
+ /** @type {import('restructure').VersionedStruct} */
3
14
  let loca = new r.VersionedStruct('head.indexToLocFormat', {
4
15
  0: {
5
16
  offsets: new r.Array(r.uint16)
@@ -9,21 +20,23 @@ let loca = new r.VersionedStruct('head.indexToLocFormat', {
9
20
  }
10
21
  });
11
22
 
12
- loca.process = function() {
13
- if (this.version === 0 && !this._processed) {
14
- for (let i = 0; i < this.offsets.length; i++) {
15
- this.offsets[i] <<= 1;
23
+ loca.process = function () {
24
+ let self = /** @type {LocaValue} */ (this);
25
+ if (self.version === 0 && !self._processed) {
26
+ for (let i = 0; i < self.offsets.length; i++) {
27
+ self.offsets[i] <<= 1;
16
28
  }
17
- this._processed = true;
29
+ self._processed = true;
18
30
  }
19
31
  };
20
32
 
21
- loca.preEncode = function() {
22
- if (this.version === 0 && this._processed !== false) {
23
- for (let i = 0; i < this.offsets.length; i++) {
24
- this.offsets[i] >>>= 1;
33
+ loca.preEncode = function () {
34
+ let self = /** @type {LocaValue} */ (this);
35
+ if (self.version === 0 && self._processed !== false) {
36
+ for (let i = 0; i < self.offsets.length; i++) {
37
+ self.offsets[i] >>>= 1;
25
38
  }
26
- this._processed = false;
39
+ self._processed = false;
27
40
  }
28
41
  };
29
42