@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
@@ -2,6 +2,10 @@ import UnicodeTrie from '../unicode-trie/index.js';
2
2
  import data from './data.js';
3
3
  import trieBase64 from './data-trie.js';
4
4
 
5
+ /**
6
+ * @param {string} base64
7
+ * @returns {Uint8Array}
8
+ */
5
9
  function decodeBase64(base64) {
6
10
  if (typeof Buffer !== 'undefined') {
7
11
  return new Uint8Array(Buffer.from(base64, 'base64'));
@@ -17,8 +21,13 @@ function decodeBase64(base64) {
17
21
 
18
22
  const trie = new UnicodeTrie(decodeBase64(trieBase64));
19
23
 
24
+ /** @type {(n: number) => number} */
20
25
  const log2 = Math.log2 || (n => Math.log(n) / Math.LN2);
21
- const bits = (n) => ((log2(n) + 1) | 0);
26
+ /**
27
+ * @param {number} n
28
+ * @returns {number}
29
+ */
30
+ const bits = n => ((log2(n) + 1) | 0);
22
31
 
23
32
  // compute the number of bits stored for each field
24
33
  const CATEGORY_BITS = bits(data.categories.length - 1);
@@ -38,26 +47,46 @@ const SCRIPT_MASK = (1 << SCRIPT_BITS) - 1;
38
47
  const EAW_MASK = (1 << EAW_BITS) - 1;
39
48
  const NUMBER_MASK = (1 << NUMBER_BITS) - 1;
40
49
 
50
+ /**
51
+ * @param {number} codePoint
52
+ * @returns {string}
53
+ */
41
54
  export function getCategory(codePoint) {
42
55
  const val = trie.get(codePoint);
43
56
  return data.categories[(val >> CATEGORY_SHIFT) & CATEGORY_MASK];
44
57
  }
45
58
 
59
+ /**
60
+ * @param {number} codePoint
61
+ * @returns {string}
62
+ */
46
63
  export function getCombiningClass(codePoint) {
47
64
  const val = trie.get(codePoint);
48
65
  return data.combiningClasses[(val >> COMBINING_SHIFT) & COMBINING_MASK];
49
66
  }
50
67
 
68
+ /**
69
+ * @param {number} codePoint
70
+ * @returns {string}
71
+ */
51
72
  export function getScript(codePoint) {
52
73
  const val = trie.get(codePoint);
53
74
  return data.scripts[(val >> SCRIPT_SHIFT) & SCRIPT_MASK];
54
75
  }
55
76
 
77
+ /**
78
+ * @param {number} codePoint
79
+ * @returns {string}
80
+ */
56
81
  export function getEastAsianWidth(codePoint) {
57
82
  const val = trie.get(codePoint);
58
83
  return data.eaw[(val >> EAW_SHIFT) & EAW_MASK];
59
84
  }
60
85
 
86
+ /**
87
+ * @param {number} codePoint
88
+ * @returns {number | null}
89
+ */
61
90
  export function getNumericValue(codePoint) {
62
91
  let val = trie.get(codePoint);
63
92
  let num = val & NUMBER_MASK;
@@ -90,78 +119,114 @@ export function getNumericValue(codePoint) {
90
119
  }
91
120
  }
92
121
 
122
+ /**
123
+ * @param {number} codePoint
124
+ * @returns {boolean}
125
+ */
93
126
  export function isAlphabetic(codePoint) {
94
127
  const category = getCategory(codePoint);
95
128
  return (
96
- category === 'Lu' ||
97
- category === 'Ll' ||
98
- category === 'Lt' ||
99
- category === 'Lm' ||
100
- category === 'Lo' ||
101
- category === 'Nl'
129
+ category === 'Lu'
130
+ || category === 'Ll'
131
+ || category === 'Lt'
132
+ || category === 'Lm'
133
+ || category === 'Lo'
134
+ || category === 'Nl'
102
135
  );
103
136
  }
104
137
 
138
+ /**
139
+ * @param {number} codePoint
140
+ * @returns {boolean}
141
+ */
105
142
  export function isDigit(codePoint) {
106
143
  return getCategory(codePoint) === 'Nd';
107
144
  }
108
145
 
146
+ /**
147
+ * @param {number} codePoint
148
+ * @returns {boolean}
149
+ */
109
150
  export function isPunctuation(codePoint) {
110
151
  const category = getCategory(codePoint);
111
152
  return (
112
- category === 'Pc' ||
113
- category === 'Pd' ||
114
- category === 'Pe' ||
115
- category === 'Pf' ||
116
- category === 'Pi' ||
117
- category === 'Po' ||
118
- category === 'Ps'
153
+ category === 'Pc'
154
+ || category === 'Pd'
155
+ || category === 'Pe'
156
+ || category === 'Pf'
157
+ || category === 'Pi'
158
+ || category === 'Po'
159
+ || category === 'Ps'
119
160
  );
120
161
  }
121
162
 
163
+ /**
164
+ * @param {number} codePoint
165
+ * @returns {boolean}
166
+ */
122
167
  export function isLowerCase(codePoint) {
123
168
  return getCategory(codePoint) === 'Ll';
124
169
  }
125
170
 
171
+ /**
172
+ * @param {number} codePoint
173
+ * @returns {boolean}
174
+ */
126
175
  export function isUpperCase(codePoint) {
127
176
  return getCategory(codePoint) === 'Lu';
128
177
  }
129
178
 
179
+ /**
180
+ * @param {number} codePoint
181
+ * @returns {boolean}
182
+ */
130
183
  export function isTitleCase(codePoint) {
131
184
  return getCategory(codePoint) === 'Lt';
132
185
  }
133
186
 
187
+ /**
188
+ * @param {number} codePoint
189
+ * @returns {boolean}
190
+ */
134
191
  export function isWhiteSpace(codePoint) {
135
192
  const category = getCategory(codePoint);
136
193
  return (
137
- category === 'Zs' ||
138
- category === 'Zl' ||
139
- category === 'Zp'
194
+ category === 'Zs'
195
+ || category === 'Zl'
196
+ || category === 'Zp'
140
197
  );
141
198
  }
142
199
 
200
+ /**
201
+ * @param {number} codePoint
202
+ * @returns {boolean}
203
+ */
143
204
  export function isBaseForm(codePoint) {
144
205
  const category = getCategory(codePoint);
145
206
  return (
146
- category === 'Nd' ||
147
- category === 'No' ||
148
- category === 'Nl' ||
149
- category === 'Lu' ||
150
- category === 'Ll' ||
151
- category === 'Lt' ||
152
- category === 'Lm' ||
153
- category === 'Lo' ||
154
- category === 'Me' ||
155
- category === 'Mc'
207
+ category === 'Nd'
208
+ || category === 'No'
209
+ || category === 'Nl'
210
+ || category === 'Lu'
211
+ || category === 'Ll'
212
+ || category === 'Lt'
213
+ || category === 'Lm'
214
+ || category === 'Lo'
215
+ || category === 'Me'
216
+ || category === 'Mc'
156
217
  );
157
218
  }
158
219
 
220
+ /**
221
+ * @param {number} codePoint
222
+ * @returns {boolean}
223
+ */
159
224
  export function isMark(codePoint) {
160
225
  const category = getCategory(codePoint);
161
226
  return (
162
- category === 'Mn' ||
163
- category === 'Me' ||
164
- category === 'Mc'
227
+ category === 'Mn'
228
+ || category === 'Me'
229
+ || category === 'Mc'
165
230
  );
166
231
  }
167
232
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cantoo/unicode-properties",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Fast access to unicode character properties (vendored for @cantoo/fontkit)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -1,6 +1,6 @@
1
- import { deflateSync } from "fflate";
2
- import UnicodeTrie from "./index.js";
3
- import { swap32LE } from "./swap.js";
1
+ import { deflateSync } from 'fflate';
2
+ import UnicodeTrie from './index.js';
3
+ import { swap32LE } from './swap.js';
4
4
 
5
5
  // Shift size for getting the index-1 table offset.
6
6
  const SHIFT_1 = 6 + 5;
@@ -57,7 +57,7 @@ const INDEX_2_BMP_LENGTH = LSCP_INDEX_2_OFFSET + LSCP_INDEX_2_LENGTH;
57
57
  // The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
58
58
  // Length 32=0x20 for lead bytes C0..DF, regardless of SHIFT_2.
59
59
  const UTF8_2B_INDEX_2_OFFSET = INDEX_2_BMP_LENGTH;
60
- const UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6; // U+0800 is the first code point after 2-byte UTF-8
60
+ const UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6; // U+0800 is the first code point after 2-byte UTF-8
61
61
 
62
62
  // The index-1 table, only used for supplementary code points, at offset 2112=0x840.
63
63
  // Variable length, for code points up to highStart, where the last single-value range starts.
@@ -139,6 +139,13 @@ const INDEX_2_START_OFFSET = INDEX_2_NULL_OFFSET + INDEX_2_BLOCK_LENGTH;
139
139
  // (0x110000>>SHIFT_2)+UTF8_2B_INDEX_2_LENGTH+MAX_INDEX_1_LENGTH.)
140
140
  const MAX_INDEX_LENGTH = 0xffff;
141
141
 
142
+ /**
143
+ * @param {ArrayLike<number>} a
144
+ * @param {number} s
145
+ * @param {number} t
146
+ * @param {number} length
147
+ * @returns {boolean}
148
+ */
142
149
  const equal_int = (a, s, t, length) => {
143
150
  for (let i = 0; i < length; i++) {
144
151
  if (a[s + i] !== a[t + i]) {
@@ -150,24 +157,37 @@ const equal_int = (a, s, t, length) => {
150
157
  };
151
158
 
152
159
  class UnicodeTrieBuilder {
160
+ /**
161
+ * @param {number} [initialValue=0]
162
+ * @param {number} [errorValue=0]
163
+ */
153
164
  constructor(initialValue, errorValue) {
154
165
  let i, j;
155
166
  if (initialValue == null) {
156
167
  initialValue = 0;
157
168
  }
169
+ /** @type {number} */
158
170
  this.initialValue = initialValue;
159
171
  if (errorValue == null) {
160
172
  errorValue = 0;
161
173
  }
174
+ /** @type {number} */
162
175
  this.errorValue = errorValue;
176
+ /** @type {Int32Array} */
163
177
  this.index1 = new Int32Array(INDEX_1_LENGTH);
178
+ /** @type {Int32Array} */
164
179
  this.index2 = new Int32Array(MAX_INDEX_2_LENGTH);
180
+ /** @type {number} */
165
181
  this.highStart = 0x110000;
166
182
 
183
+ /** @type {Uint32Array} */
167
184
  this.data = new Uint32Array(INITIAL_DATA_LENGTH);
185
+ /** @type {number} */
168
186
  this.dataCapacity = INITIAL_DATA_LENGTH;
169
187
 
188
+ /** @type {number} */
170
189
  this.firstFreeBlock = 0;
190
+ /** @type {boolean} */
171
191
  this.isCompacted = false;
172
192
 
173
193
  // Multi-purpose per-data-block table.
@@ -183,13 +203,14 @@ class UnicodeTrieBuilder {
183
203
  //
184
204
  // Map of adjusted indexes, used in compactData() and compactIndex2().
185
205
  // Maps from original indexes to new ones.
206
+ /** @type {Int32Array} */
186
207
  this.map = new Int32Array(MAX_DATA_LENGTH_BUILDTIME >> SHIFT_2);
187
208
 
188
209
  for (i = 0; i < 0x80; i++) {
189
210
  this.data[i] = this.initialValue;
190
211
  }
191
212
 
192
- for (i = i; i < 0xc0; i++) {
213
+ for (; i < 0xc0; i++) {
193
214
  this.data[i] = this.errorValue;
194
215
  }
195
216
 
@@ -197,7 +218,9 @@ class UnicodeTrieBuilder {
197
218
  this.data[i] = this.initialValue;
198
219
  }
199
220
 
221
+ /** @type {number} */
200
222
  this.dataNullOffset = DATA_NULL_OFFSET;
223
+ /** @type {number} */
201
224
  this.dataLength = NEW_DATA_START_OFFSET;
202
225
 
203
226
  // set the index-2 indexes for the 2=0x80>>SHIFT_2 ASCII data blocks
@@ -208,7 +231,7 @@ class UnicodeTrieBuilder {
208
231
  }
209
232
 
210
233
  // reference counts for the bad-UTF-8-data block
211
- for (j = j; j < 0xc0; j += DATA_BLOCK_LENGTH) {
234
+ for (; j < 0xc0; j += DATA_BLOCK_LENGTH) {
212
235
  this.map[i++] = 0;
213
236
  }
214
237
 
@@ -218,7 +241,7 @@ class UnicodeTrieBuilder {
218
241
  // i==newTrie->dataNullOffset
219
242
  this.map[i++] = ((0x110000 >> SHIFT_2) - (0x80 >> SHIFT_2)) + 1 + LSCP_INDEX_2_LENGTH;
220
243
  j += DATA_BLOCK_LENGTH;
221
- for (j = j; j < NEW_DATA_START_OFFSET; j += DATA_BLOCK_LENGTH) {
244
+ for (; j < NEW_DATA_START_OFFSET; j += DATA_BLOCK_LENGTH) {
222
245
  this.map[i++] = 0;
223
246
  }
224
247
 
@@ -239,7 +262,9 @@ class UnicodeTrieBuilder {
239
262
  this.index2[INDEX_2_NULL_OFFSET + i] = DATA_NULL_OFFSET;
240
263
  }
241
264
 
265
+ /** @type {number} */
242
266
  this.index2NullOffset = INDEX_2_NULL_OFFSET;
267
+ /** @type {number} */
243
268
  this.index2Length = INDEX_2_START_OFFSET;
244
269
 
245
270
  // set the index-1 indexes for the linear index-2 block
@@ -250,7 +275,7 @@ class UnicodeTrieBuilder {
250
275
  }
251
276
 
252
277
  // set the remaining index-1 indexes to the null index-2 block
253
- for (i = i; i < INDEX_1_LENGTH; i++) {
278
+ for (; i < INDEX_1_LENGTH; i++) {
254
279
  this.index1[i] = INDEX_2_NULL_OFFSET;
255
280
  }
256
281
 
@@ -260,9 +285,13 @@ class UnicodeTrieBuilder {
260
285
  for (i = 0x80; i < 0x800; i += DATA_BLOCK_LENGTH) {
261
286
  this.set(i, this.initialValue);
262
287
  }
263
-
264
288
  }
265
289
 
290
+ /**
291
+ * @param {number} codePoint
292
+ * @param {number} value
293
+ * @returns {this}
294
+ */
266
295
  set(codePoint, value) {
267
296
  if ((codePoint < 0) || (codePoint > 0x10ffff)) {
268
297
  throw new Error('Invalid code point');
@@ -277,6 +306,13 @@ class UnicodeTrieBuilder {
277
306
  return this;
278
307
  }
279
308
 
309
+ /**
310
+ * @param {number} start
311
+ * @param {number} end
312
+ * @param {number} value
313
+ * @param {boolean} [overwrite=true]
314
+ * @returns {this}
315
+ */
280
316
  setRange(start, end, value, overwrite) {
281
317
  let block, repeatBlock;
282
318
  if (overwrite == null) {
@@ -346,7 +382,6 @@ class UnicodeTrieBuilder {
346
382
  // protected block: just write the values into this block
347
383
  this._fillBlock(block, 0, DATA_BLOCK_LENGTH, value, this.initialValue, overwrite);
348
384
  }
349
-
350
385
  } else if ((this.data[block] !== value) && (overwrite || (block === this.dataNullOffset))) {
351
386
  // Set the repeatBlock instead of the null block or previous repeat block:
352
387
  //
@@ -387,6 +422,11 @@ class UnicodeTrieBuilder {
387
422
  return this;
388
423
  }
389
424
 
425
+ /**
426
+ * @param {number} c
427
+ * @param {boolean} [fromLSCP=true]
428
+ * @returns {number}
429
+ */
390
430
  get(c, fromLSCP) {
391
431
  let i2;
392
432
  if (fromLSCP == null) {
@@ -410,6 +450,11 @@ class UnicodeTrieBuilder {
410
450
  return this.data[block + (c & DATA_MASK)];
411
451
  }
412
452
 
453
+ /**
454
+ * @param {number} c
455
+ * @param {boolean} forLSCP
456
+ * @returns {boolean}
457
+ */
413
458
  _isInNullBlock(c, forLSCP) {
414
459
  let i2;
415
460
  if (((c & 0xfffffc00) === 0xd800) && forLSCP) {
@@ -422,6 +467,9 @@ class UnicodeTrieBuilder {
422
467
  return block === this.dataNullOffset;
423
468
  }
424
469
 
470
+ /**
471
+ * @returns {number}
472
+ */
425
473
  _allocIndex2Block() {
426
474
  const newBlock = this.index2Length;
427
475
  const newTop = newBlock + INDEX_2_BLOCK_LENGTH;
@@ -429,7 +477,7 @@ class UnicodeTrieBuilder {
429
477
  // Should never occur.
430
478
  // Either MAX_BUILD_TIME_INDEX_LENGTH is incorrect,
431
479
  // or the code writes more values than should be possible.
432
- throw new Error("Internal error in Trie2 creation.");
480
+ throw new Error('Internal error in Trie2 creation.');
433
481
  }
434
482
 
435
483
  this.index2Length = newTop;
@@ -438,6 +486,11 @@ class UnicodeTrieBuilder {
438
486
  return newBlock;
439
487
  }
440
488
 
489
+ /**
490
+ * @param {number} c
491
+ * @param {boolean} forLSCP
492
+ * @returns {number}
493
+ */
441
494
  _getIndex2Block(c, forLSCP) {
442
495
  if ((c >= 0xd800) && (c < 0xdc00) && forLSCP) {
443
496
  return LSCP_INDEX_2_OFFSET;
@@ -453,10 +506,18 @@ class UnicodeTrieBuilder {
453
506
  return i2;
454
507
  }
455
508
 
509
+ /**
510
+ * @param {number} block
511
+ * @returns {boolean}
512
+ */
456
513
  _isWritableBlock(block) {
457
514
  return (block !== this.dataNullOffset) && (this.map[block >> SHIFT_2] === 1);
458
515
  }
459
516
 
517
+ /**
518
+ * @param {number} copyBlock
519
+ * @returns {number}
520
+ */
460
521
  _allocDataBlock(copyBlock) {
461
522
  let newBlock;
462
523
  if (this.firstFreeBlock !== 0) {
@@ -478,7 +539,7 @@ class UnicodeTrieBuilder {
478
539
  // Should never occur.
479
540
  // Either MAX_DATA_LENGTH_BUILDTIME is incorrect,
480
541
  // or the code writes more values than should be possible.
481
- throw new Error("Internal error in Trie2 creation.");
542
+ throw new Error('Internal error in Trie2 creation.');
482
543
  }
483
544
 
484
545
  const newData = new Uint32Array(capacity);
@@ -495,14 +556,23 @@ class UnicodeTrieBuilder {
495
556
  return newBlock;
496
557
  }
497
558
 
559
+ /**
560
+ * @param {number} block
561
+ * @returns {void}
562
+ */
498
563
  _releaseDataBlock(block) {
499
564
  // put this block at the front of the free-block chain
500
565
  this.map[block >> SHIFT_2] = -this.firstFreeBlock;
501
566
  this.firstFreeBlock = block;
502
567
  }
503
568
 
569
+ /**
570
+ * @param {number} i2
571
+ * @param {number} block
572
+ * @returns {void}
573
+ */
504
574
  _setIndex2Entry(i2, block) {
505
- ++this.map[block >> SHIFT_2]; // increment first, in case block == oldBlock!
575
+ ++this.map[block >> SHIFT_2]; // increment first, in case block == oldBlock!
506
576
  const oldBlock = this.index2[i2];
507
577
  if (--this.map[oldBlock >> SHIFT_2] === 0) {
508
578
  this._releaseDataBlock(oldBlock);
@@ -511,6 +581,11 @@ class UnicodeTrieBuilder {
511
581
  this.index2[i2] = block;
512
582
  }
513
583
 
584
+ /**
585
+ * @param {number} c
586
+ * @param {boolean} forLSCP
587
+ * @returns {number}
588
+ */
514
589
  _getDataBlock(c, forLSCP) {
515
590
  let i2 = this._getIndex2Block(c, forLSCP);
516
591
  i2 += (c >> SHIFT_2) & INDEX_2_MASK;
@@ -526,6 +601,15 @@ class UnicodeTrieBuilder {
526
601
  return newBlock;
527
602
  }
528
603
 
604
+ /**
605
+ * @param {number} block
606
+ * @param {number} start
607
+ * @param {number} limit
608
+ * @param {number} value
609
+ * @param {number} initialValue
610
+ * @param {boolean} overwrite
611
+ * @returns {void}
612
+ */
529
613
  _fillBlock(block, start, limit, value, initialValue, overwrite) {
530
614
  let i;
531
615
  if (overwrite) {
@@ -541,6 +625,11 @@ class UnicodeTrieBuilder {
541
625
  }
542
626
  }
543
627
 
628
+ /**
629
+ * @param {number} block
630
+ * @param {number} value
631
+ * @returns {void}
632
+ */
544
633
  _writeBlock(block, value) {
545
634
  const limit = block + DATA_BLOCK_LENGTH;
546
635
  while (block < limit) {
@@ -548,6 +637,10 @@ class UnicodeTrieBuilder {
548
637
  }
549
638
  }
550
639
 
640
+ /**
641
+ * @param {number} highValue
642
+ * @returns {number}
643
+ */
551
644
  _findHighStart(highValue) {
552
645
  let prevBlock, prevI2Block;
553
646
  const data32 = this.data;
@@ -620,6 +713,12 @@ class UnicodeTrieBuilder {
620
713
  return 0;
621
714
  }
622
715
 
716
+ /**
717
+ * @param {number} dataLength
718
+ * @param {number} otherBlock
719
+ * @param {number} blockLength
720
+ * @returns {number}
721
+ */
623
722
  _findSameDataBlock(dataLength, otherBlock, blockLength) {
624
723
  // ensure that we do not even partially get past dataLength
625
724
  dataLength -= blockLength;
@@ -634,6 +733,11 @@ class UnicodeTrieBuilder {
634
733
  return -1;
635
734
  }
636
735
 
736
+ /**
737
+ * @param {number} index2Length
738
+ * @param {number} otherBlock
739
+ * @returns {number}
740
+ */
637
741
  _findSameIndex2Block(index2Length, otherBlock) {
638
742
  // ensure that we do not even partially get past index2Length
639
743
  index2Length -= INDEX_2_BLOCK_LENGTH;
@@ -646,6 +750,9 @@ class UnicodeTrieBuilder {
646
750
  return -1;
647
751
  }
648
752
 
753
+ /**
754
+ * @returns {void}
755
+ */
649
756
  _compactData() {
650
757
  // do not compact linear-ASCII data
651
758
  let newStart = DATA_START_OFFSET;
@@ -719,7 +826,6 @@ class UnicodeTrieBuilder {
719
826
  for (i = blockLength - overlap; i > 0; i--) {
720
827
  this.data[newStart++] = this.data[start++];
721
828
  }
722
-
723
829
  } else { // no overlap && newStart==start
724
830
  mapIndex = start >> SHIFT_2;
725
831
  for (i = blockCount; i > 0; i--) {
@@ -751,6 +857,9 @@ class UnicodeTrieBuilder {
751
857
  this.dataLength = newStart;
752
858
  }
753
859
 
860
+ /**
861
+ * @returns {void}
862
+ */
754
863
  _compactIndex2() {
755
864
  // do not compact linear-BMP index-2 blocks
756
865
  let newStart = INDEX_2_BMP_LENGTH;
@@ -799,7 +908,6 @@ class UnicodeTrieBuilder {
799
908
  for (i = INDEX_2_BLOCK_LENGTH - overlap; i > 0; i--) {
800
909
  this.index2[newStart++] = this.index2[start++];
801
910
  }
802
-
803
911
  } else { // no overlap && newStart==start
804
912
  this.map[start >> SHIFT_1_2] = start;
805
913
  start += INDEX_2_BLOCK_LENGTH;
@@ -827,6 +935,9 @@ class UnicodeTrieBuilder {
827
935
  this.index2Length = newStart;
828
936
  }
829
937
 
938
+ /**
939
+ * @returns {void}
940
+ */
830
941
  _compact() {
831
942
  // find highStart and round it up
832
943
  let highValue = this.get(0x10ffff);
@@ -861,6 +972,9 @@ class UnicodeTrieBuilder {
861
972
  this.isCompacted = true;
862
973
  }
863
974
 
975
+ /**
976
+ * @returns {import('./index.js').default}
977
+ */
864
978
  freeze() {
865
979
  let allIndexesLength, i;
866
980
  if (!this.isCompacted) {
@@ -876,11 +990,11 @@ class UnicodeTrieBuilder {
876
990
  const dataMove = allIndexesLength;
877
991
 
878
992
  // are indexLength and dataLength within limits?
879
- if ((allIndexesLength > MAX_INDEX_LENGTH) || // for unshifted indexLength
880
- ((dataMove + this.dataNullOffset) > 0xffff) || // for unshifted dataNullOffset
881
- ((dataMove + DATA_0800_OFFSET) > 0xffff) || // for unshifted 2-byte UTF-8 index-2 values
882
- ((dataMove + this.dataLength) > MAX_DATA_LENGTH_RUNTIME)) { // for shiftedDataLength
883
- throw new Error("Trie data is too large.");
993
+ if ((allIndexesLength > MAX_INDEX_LENGTH) // for unshifted indexLength
994
+ || ((dataMove + this.dataNullOffset) > 0xffff) // for unshifted dataNullOffset
995
+ || ((dataMove + DATA_0800_OFFSET) > 0xffff) // for unshifted 2-byte UTF-8 index-2 values
996
+ || ((dataMove + this.dataLength) > MAX_DATA_LENGTH_RUNTIME)) { // for shiftedDataLength
997
+ throw new Error('Trie data is too large.');
884
998
  }
885
999
 
886
1000
  // calculate the sizes of, and allocate, the index and data arrays
@@ -898,7 +1012,7 @@ class UnicodeTrieBuilder {
898
1012
  data[destIdx++] = (dataMove + BAD_UTF8_DATA_OFFSET);
899
1013
  }
900
1014
 
901
- for (i = i; i < 0xe0 - 0xc0; i++) { // C2..DF
1015
+ for (; i < 0xe0 - 0xc0; i++) { // C2..DF
902
1016
  data[destIdx++] = (dataMove + this.index2[i << (6 - SHIFT_2)]);
903
1017
  }
904
1018
 
@@ -932,13 +1046,16 @@ class UnicodeTrieBuilder {
932
1046
  return dest;
933
1047
  }
934
1048
 
935
- // Generates a Buffer containing the serialized and compressed trie.
936
- // Trie data is compressed once with raw deflate (a second pass does not help).
937
- // Format:
938
- // uint32_t highStart;
939
- // uint32_t errorValue;
940
- // uint32_t uncompressedDataLength;
941
- // uint8_t trieData[dataLength];
1049
+ /**
1050
+ * Generates a Buffer containing the serialized and compressed trie.
1051
+ * Trie data is compressed once with raw deflate (a second pass does not help).
1052
+ * Format:
1053
+ * uint32_t highStart;
1054
+ * uint32_t errorValue;
1055
+ * uint32_t uncompressedDataLength;
1056
+ * uint8_t trieData[dataLength];
1057
+ * @returns {Buffer}
1058
+ */
942
1059
  toBuffer() {
943
1060
  const trie = this.freeze();
944
1061
 
@@ -962,4 +1079,4 @@ class UnicodeTrieBuilder {
962
1079
  }
963
1080
  }
964
1081
 
965
- export default UnicodeTrieBuilder;
1082
+ export default UnicodeTrieBuilder;