@cantoo/fontkit 2.0.4

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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +274 -0
  3. package/dist/browser-module.mjs +13660 -0
  4. package/dist/browser-module.mjs.map +1 -0
  5. package/dist/browser.cjs +13689 -0
  6. package/dist/browser.cjs.map +1 -0
  7. package/dist/main.cjs +13712 -0
  8. package/dist/main.cjs.map +1 -0
  9. package/dist/module.mjs +13673 -0
  10. package/dist/module.mjs.map +1 -0
  11. package/package.json +99 -0
  12. package/src/CmapProcessor.js +285 -0
  13. package/src/DFont.js +113 -0
  14. package/src/TTFFont.js +621 -0
  15. package/src/TrueTypeCollection.js +65 -0
  16. package/src/WOFF2Font.js +222 -0
  17. package/src/WOFFFont.js +56 -0
  18. package/src/aat/AATFeatureMap.js +545 -0
  19. package/src/aat/AATLayoutEngine.js +48 -0
  20. package/src/aat/AATLookupTable.js +125 -0
  21. package/src/aat/AATMorxProcessor.js +429 -0
  22. package/src/aat/AATStateMachine.js +96 -0
  23. package/src/base.js +29 -0
  24. package/src/cff/CFFCharsets.js +99 -0
  25. package/src/cff/CFFDict.js +166 -0
  26. package/src/cff/CFFEncodings.js +48 -0
  27. package/src/cff/CFFFont.js +158 -0
  28. package/src/cff/CFFIndex.js +150 -0
  29. package/src/cff/CFFOperand.js +135 -0
  30. package/src/cff/CFFPointer.js +50 -0
  31. package/src/cff/CFFPrivateDict.js +39 -0
  32. package/src/cff/CFFStandardStrings.js +71 -0
  33. package/src/cff/CFFTop.js +236 -0
  34. package/src/decorators.js +36 -0
  35. package/src/encodings.js +219 -0
  36. package/src/fs.js +12 -0
  37. package/src/glyph/BBox.js +72 -0
  38. package/src/glyph/CBDTGlyph.js +9 -0
  39. package/src/glyph/CFFGlyph.js +635 -0
  40. package/src/glyph/COLRGlyph.js +108 -0
  41. package/src/glyph/Glyph.js +270 -0
  42. package/src/glyph/GlyphVariationProcessor.js +487 -0
  43. package/src/glyph/Path.js +257 -0
  44. package/src/glyph/SBIXGlyph.js +54 -0
  45. package/src/glyph/StandardNames.js +27 -0
  46. package/src/glyph/TTFGlyph.js +408 -0
  47. package/src/glyph/TTFGlyphEncoder.js +158 -0
  48. package/src/glyph/WOFF2Glyph.js +70 -0
  49. package/src/index.js +15 -0
  50. package/src/layout/GlyphPosition.js +30 -0
  51. package/src/layout/GlyphRun.js +108 -0
  52. package/src/layout/KernProcessor.js +97 -0
  53. package/src/layout/LayoutEngine.js +190 -0
  54. package/src/layout/Script.js +231 -0
  55. package/src/layout/UnicodeLayoutEngine.js +250 -0
  56. package/src/node.js +16 -0
  57. package/src/opentype/GPOSProcessor.js +311 -0
  58. package/src/opentype/GSUBProcessor.js +208 -0
  59. package/src/opentype/GlyphInfo.js +57 -0
  60. package/src/opentype/GlyphIterator.js +82 -0
  61. package/src/opentype/OTLayoutEngine.js +117 -0
  62. package/src/opentype/OTProcessor.js +472 -0
  63. package/src/opentype/ShapingPlan.js +121 -0
  64. package/src/opentype/shapers/ArabicShaper.js +124 -0
  65. package/src/opentype/shapers/DefaultShaper.js +116 -0
  66. package/src/opentype/shapers/HangulShaper.js +285 -0
  67. package/src/opentype/shapers/HebrewShaper.js +105 -0
  68. package/src/opentype/shapers/IndicShaper.js +914 -0
  69. package/src/opentype/shapers/ThaiShaper.js +238 -0
  70. package/src/opentype/shapers/UniversalShaper.js +186 -0
  71. package/src/opentype/shapers/data.trie +0 -0
  72. package/src/opentype/shapers/gen-indic.js +208 -0
  73. package/src/opentype/shapers/gen-use.js +287 -0
  74. package/src/opentype/shapers/generate-data.js +33 -0
  75. package/src/opentype/shapers/index.js +109 -0
  76. package/src/opentype/shapers/indic-data.js +175 -0
  77. package/src/opentype/shapers/indic.json +1 -0
  78. package/src/opentype/shapers/indic.machine +31 -0
  79. package/src/opentype/shapers/indic.trie +0 -0
  80. package/src/opentype/shapers/use.json +1 -0
  81. package/src/opentype/shapers/use.machine +44 -0
  82. package/src/opentype/shapers/use.trie +0 -0
  83. package/src/packages/unicode-properties/LICENSE +8 -0
  84. package/src/packages/unicode-properties/README.md +12 -0
  85. package/src/packages/unicode-properties/data-trie.js +1 -0
  86. package/src/packages/unicode-properties/data.js +1 -0
  87. package/src/packages/unicode-properties/data.json +1 -0
  88. package/src/packages/unicode-properties/data.trie +0 -0
  89. package/src/packages/unicode-properties/dist/index.cjs +257 -0
  90. package/src/packages/unicode-properties/dist/index.cjs.map +7 -0
  91. package/src/packages/unicode-properties/dist/index.mjs +234 -0
  92. package/src/packages/unicode-properties/dist/index.mjs.map +7 -0
  93. package/src/packages/unicode-properties/generate.js +107 -0
  94. package/src/packages/unicode-properties/index.js +184 -0
  95. package/src/packages/unicode-properties/package.json +50 -0
  96. package/src/packages/unicode-trie/LICENSE +8 -0
  97. package/src/packages/unicode-trie/README.md +13 -0
  98. package/src/packages/unicode-trie/builder.js +965 -0
  99. package/src/packages/unicode-trie/dist/builder.cjs +711 -0
  100. package/src/packages/unicode-trie/dist/builder.cjs.map +7 -0
  101. package/src/packages/unicode-trie/dist/builder.mjs +690 -0
  102. package/src/packages/unicode-trie/dist/builder.mjs.map +7 -0
  103. package/src/packages/unicode-trie/dist/index.cjs +112 -0
  104. package/src/packages/unicode-trie/dist/index.cjs.map +7 -0
  105. package/src/packages/unicode-trie/dist/index.mjs +91 -0
  106. package/src/packages/unicode-trie/dist/index.mjs.map +7 -0
  107. package/src/packages/unicode-trie/index.js +135 -0
  108. package/src/packages/unicode-trie/package.json +49 -0
  109. package/src/packages/unicode-trie/swap.js +21 -0
  110. package/src/subset/CFFSubset.js +176 -0
  111. package/src/subset/Subset.js +63 -0
  112. package/src/subset/TTFSubset.js +130 -0
  113. package/src/tables/BASE.js +78 -0
  114. package/src/tables/COLR.js +25 -0
  115. package/src/tables/CPAL.js +24 -0
  116. package/src/tables/DSIG.js +21 -0
  117. package/src/tables/EBDT.js +91 -0
  118. package/src/tables/EBLC.js +80 -0
  119. package/src/tables/GDEF.js +57 -0
  120. package/src/tables/GPOS.js +209 -0
  121. package/src/tables/GSUB.js +85 -0
  122. package/src/tables/HVAR.js +44 -0
  123. package/src/tables/JSTF.js +43 -0
  124. package/src/tables/LTSH.js +10 -0
  125. package/src/tables/OS2.js +84 -0
  126. package/src/tables/PCLT.js +21 -0
  127. package/src/tables/STAT.js +99 -0
  128. package/src/tables/VDMX.js +33 -0
  129. package/src/tables/VORG.js +14 -0
  130. package/src/tables/WOFF2Directory.js +73 -0
  131. package/src/tables/WOFFDirectory.js +37 -0
  132. package/src/tables/aat.js +157 -0
  133. package/src/tables/avar.js +19 -0
  134. package/src/tables/bsln.js +31 -0
  135. package/src/tables/cmap.js +127 -0
  136. package/src/tables/cvt.js +6 -0
  137. package/src/tables/directory.js +58 -0
  138. package/src/tables/feat.js +28 -0
  139. package/src/tables/fpgm.js +8 -0
  140. package/src/tables/fvar.js +31 -0
  141. package/src/tables/gasp.js +15 -0
  142. package/src/tables/glyf.js +4 -0
  143. package/src/tables/gvar.js +27 -0
  144. package/src/tables/hdmx.js +15 -0
  145. package/src/tables/head.js +25 -0
  146. package/src/tables/hhea.js +19 -0
  147. package/src/tables/hmtx.js +11 -0
  148. package/src/tables/index.js +119 -0
  149. package/src/tables/just.js +81 -0
  150. package/src/tables/kern.js +91 -0
  151. package/src/tables/loca.js +30 -0
  152. package/src/tables/maxp.js +20 -0
  153. package/src/tables/morx.js +79 -0
  154. package/src/tables/name.js +157 -0
  155. package/src/tables/opbd.js +15 -0
  156. package/src/tables/opentype.js +217 -0
  157. package/src/tables/post.js +34 -0
  158. package/src/tables/prep.js +6 -0
  159. package/src/tables/sbix.js +17 -0
  160. package/src/tables/variations.js +80 -0
  161. package/src/tables/vhea.js +19 -0
  162. package/src/tables/vmtx.js +12 -0
  163. package/src/utils.js +62 -0
@@ -0,0 +1,965 @@
1
+ import { deflateSync } from "fflate";
2
+ import UnicodeTrie from "./index.js";
3
+ import { swap32LE } from "./swap.js";
4
+
5
+ // Shift size for getting the index-1 table offset.
6
+ const SHIFT_1 = 6 + 5;
7
+
8
+ // Shift size for getting the index-2 table offset.
9
+ const SHIFT_2 = 5;
10
+
11
+ // Difference between the two shift sizes,
12
+ // for getting an index-1 offset from an index-2 offset. 6=11-5
13
+ const SHIFT_1_2 = SHIFT_1 - SHIFT_2;
14
+
15
+ // Number of index-1 entries for the BMP. 32=0x20
16
+ // This part of the index-1 table is omitted from the serialized form.
17
+ const OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> SHIFT_1;
18
+
19
+ // Number of code points per index-1 table entry. 2048=0x800
20
+ const CP_PER_INDEX_1_ENTRY = 1 << SHIFT_1;
21
+
22
+ // Number of entries in an index-2 block. 64=0x40
23
+ const INDEX_2_BLOCK_LENGTH = 1 << SHIFT_1_2;
24
+
25
+ // Mask for getting the lower bits for the in-index-2-block offset. */
26
+ const INDEX_2_MASK = INDEX_2_BLOCK_LENGTH - 1;
27
+
28
+ // Number of entries in a data block. 32=0x20
29
+ const DATA_BLOCK_LENGTH = 1 << SHIFT_2;
30
+
31
+ // Mask for getting the lower bits for the in-data-block offset.
32
+ const DATA_MASK = DATA_BLOCK_LENGTH - 1;
33
+
34
+ // Shift size for shifting left the index array values.
35
+ // Increases possible data size with 16-bit index values at the cost
36
+ // of compactability.
37
+ // This requires data blocks to be aligned by DATA_GRANULARITY.
38
+ const INDEX_SHIFT = 2;
39
+
40
+ // The alignment size of a data block. Also the granularity for compaction.
41
+ const DATA_GRANULARITY = 1 << INDEX_SHIFT;
42
+
43
+ // The BMP part of the index-2 table is fixed and linear and starts at offset 0.
44
+ // Length=2048=0x800=0x10000>>SHIFT_2.
45
+ const INDEX_2_OFFSET = 0;
46
+
47
+ // The part of the index-2 table for U+D800..U+DBFF stores values for
48
+ // lead surrogate code _units_ not code _points_.
49
+ // Values for lead surrogate code _points_ are indexed with this portion of the table.
50
+ // Length=32=0x20=0x400>>SHIFT_2. (There are 1024=0x400 lead surrogates.)
51
+ const LSCP_INDEX_2_OFFSET = 0x10000 >> SHIFT_2;
52
+ const LSCP_INDEX_2_LENGTH = 0x400 >> SHIFT_2;
53
+
54
+ // Count the lengths of both BMP pieces. 2080=0x820
55
+ const INDEX_2_BMP_LENGTH = LSCP_INDEX_2_OFFSET + LSCP_INDEX_2_LENGTH;
56
+
57
+ // The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
58
+ // Length 32=0x20 for lead bytes C0..DF, regardless of SHIFT_2.
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
61
+
62
+ // The index-1 table, only used for supplementary code points, at offset 2112=0x840.
63
+ // Variable length, for code points up to highStart, where the last single-value range starts.
64
+ // Maximum length 512=0x200=0x100000>>SHIFT_1.
65
+ // (For 0x100000 supplementary code points U+10000..U+10ffff.)
66
+ //
67
+ // The part of the index-2 table for supplementary code points starts
68
+ // after this index-1 table.
69
+ //
70
+ // Both the index-1 table and the following part of the index-2 table
71
+ // are omitted completely if there is only BMP data.
72
+ const INDEX_1_OFFSET = UTF8_2B_INDEX_2_OFFSET + UTF8_2B_INDEX_2_LENGTH;
73
+ const MAX_INDEX_1_LENGTH = 0x100000 >> SHIFT_1;
74
+
75
+ // The illegal-UTF-8 data block follows the ASCII block, at offset 128=0x80.
76
+ // Used with linear access for single bytes 0..0xbf for simple error handling.
77
+ // Length 64=0x40, not DATA_BLOCK_LENGTH.
78
+ const BAD_UTF8_DATA_OFFSET = 0x80;
79
+
80
+ // The start of non-linear-ASCII data blocks, at offset 192=0xc0.
81
+ // !!!!
82
+ const DATA_START_OFFSET = 0xc0;
83
+
84
+ // The null data block.
85
+ // Length 64=0x40 even if DATA_BLOCK_LENGTH is smaller,
86
+ // to work with 6-bit trail bytes from 2-byte UTF-8.
87
+ const DATA_NULL_OFFSET = DATA_START_OFFSET;
88
+
89
+ // The start of allocated data blocks.
90
+ const NEW_DATA_START_OFFSET = DATA_NULL_OFFSET + 0x40;
91
+
92
+ // The start of data blocks for U+0800 and above.
93
+ // Below, compaction uses a block length of 64 for 2-byte UTF-8.
94
+ // From here on, compaction uses DATA_BLOCK_LENGTH.
95
+ // Data values for 0x780 code points beyond ASCII.
96
+ const DATA_0800_OFFSET = NEW_DATA_START_OFFSET + 0x780;
97
+
98
+ // Start with allocation of 16k data entries. */
99
+ const INITIAL_DATA_LENGTH = 1 << 14;
100
+
101
+ // Grow about 8x each time.
102
+ const MEDIUM_DATA_LENGTH = 1 << 17;
103
+
104
+ // Maximum length of the runtime data array.
105
+ // Limited by 16-bit index values that are left-shifted by INDEX_SHIFT,
106
+ // and by uint16_t UTrie2Header.shiftedDataLength.
107
+ const MAX_DATA_LENGTH_RUNTIME = 0xffff << INDEX_SHIFT;
108
+
109
+ const INDEX_1_LENGTH = 0x110000 >> SHIFT_1;
110
+
111
+ // Maximum length of the build-time data array.
112
+ // One entry per 0x110000 code points, plus the illegal-UTF-8 block and the null block,
113
+ // plus values for the 0x400 surrogate code units.
114
+ const MAX_DATA_LENGTH_BUILDTIME = 0x110000 + 0x40 + 0x40 + 0x400;
115
+
116
+ // At build time, leave a gap in the index-2 table,
117
+ // at least as long as the maximum lengths of the 2-byte UTF-8 index-2 table
118
+ // and the supplementary index-1 table.
119
+ // Round up to INDEX_2_BLOCK_LENGTH for proper compacting.
120
+ const INDEX_GAP_OFFSET = INDEX_2_BMP_LENGTH;
121
+ const INDEX_GAP_LENGTH = ((UTF8_2B_INDEX_2_LENGTH + MAX_INDEX_1_LENGTH) + INDEX_2_MASK) & ~INDEX_2_MASK;
122
+
123
+ // Maximum length of the build-time index-2 array.
124
+ // Maximum number of Unicode code points (0x110000) shifted right by SHIFT_2,
125
+ // plus the part of the index-2 table for lead surrogate code points,
126
+ // plus the build-time index gap,
127
+ // plus the null index-2 block.)
128
+ const MAX_INDEX_2_LENGTH = (0x110000 >> SHIFT_2) + LSCP_INDEX_2_LENGTH + INDEX_GAP_LENGTH + INDEX_2_BLOCK_LENGTH;
129
+
130
+ // The null index-2 block, following the gap in the index-2 table.
131
+ const INDEX_2_NULL_OFFSET = INDEX_GAP_OFFSET + INDEX_GAP_LENGTH;
132
+
133
+ // The start of allocated index-2 blocks.
134
+ const INDEX_2_START_OFFSET = INDEX_2_NULL_OFFSET + INDEX_2_BLOCK_LENGTH;
135
+
136
+ // Maximum length of the runtime index array.
137
+ // Limited by its own 16-bit index values, and by uint16_t UTrie2Header.indexLength.
138
+ // (The actual maximum length is lower,
139
+ // (0x110000>>SHIFT_2)+UTF8_2B_INDEX_2_LENGTH+MAX_INDEX_1_LENGTH.)
140
+ const MAX_INDEX_LENGTH = 0xffff;
141
+
142
+ const equal_int = (a, s, t, length) => {
143
+ for (let i = 0; i < length; i++) {
144
+ if (a[s + i] !== a[t + i]) {
145
+ return false;
146
+ }
147
+ }
148
+
149
+ return true;
150
+ };
151
+
152
+ class UnicodeTrieBuilder {
153
+ constructor(initialValue, errorValue) {
154
+ let i, j;
155
+ if (initialValue == null) {
156
+ initialValue = 0;
157
+ }
158
+ this.initialValue = initialValue;
159
+ if (errorValue == null) {
160
+ errorValue = 0;
161
+ }
162
+ this.errorValue = errorValue;
163
+ this.index1 = new Int32Array(INDEX_1_LENGTH);
164
+ this.index2 = new Int32Array(MAX_INDEX_2_LENGTH);
165
+ this.highStart = 0x110000;
166
+
167
+ this.data = new Uint32Array(INITIAL_DATA_LENGTH);
168
+ this.dataCapacity = INITIAL_DATA_LENGTH;
169
+
170
+ this.firstFreeBlock = 0;
171
+ this.isCompacted = false;
172
+
173
+ // Multi-purpose per-data-block table.
174
+ //
175
+ // Before compacting:
176
+ //
177
+ // Per-data-block reference counters/free-block list.
178
+ // 0: unused
179
+ // >0: reference counter (number of index-2 entries pointing here)
180
+ // <0: next free data block in free-block list
181
+ //
182
+ // While compacting:
183
+ //
184
+ // Map of adjusted indexes, used in compactData() and compactIndex2().
185
+ // Maps from original indexes to new ones.
186
+ this.map = new Int32Array(MAX_DATA_LENGTH_BUILDTIME >> SHIFT_2);
187
+
188
+ for (i = 0; i < 0x80; i++) {
189
+ this.data[i] = this.initialValue;
190
+ }
191
+
192
+ for (i = i; i < 0xc0; i++) {
193
+ this.data[i] = this.errorValue;
194
+ }
195
+
196
+ for (i = DATA_NULL_OFFSET; i < NEW_DATA_START_OFFSET; i++) {
197
+ this.data[i] = this.initialValue;
198
+ }
199
+
200
+ this.dataNullOffset = DATA_NULL_OFFSET;
201
+ this.dataLength = NEW_DATA_START_OFFSET;
202
+
203
+ // set the index-2 indexes for the 2=0x80>>SHIFT_2 ASCII data blocks
204
+ i = 0;
205
+ for (j = 0; j < 0x80; j += DATA_BLOCK_LENGTH) {
206
+ this.index2[i] = j;
207
+ this.map[i++] = 1;
208
+ }
209
+
210
+ // reference counts for the bad-UTF-8-data block
211
+ for (j = j; j < 0xc0; j += DATA_BLOCK_LENGTH) {
212
+ this.map[i++] = 0;
213
+ }
214
+
215
+ // Reference counts for the null data block: all blocks except for the ASCII blocks.
216
+ // Plus 1 so that we don't drop this block during compaction.
217
+ // Plus as many as needed for lead surrogate code points.
218
+ // i==newTrie->dataNullOffset
219
+ this.map[i++] = ((0x110000 >> SHIFT_2) - (0x80 >> SHIFT_2)) + 1 + LSCP_INDEX_2_LENGTH;
220
+ j += DATA_BLOCK_LENGTH;
221
+ for (j = j; j < NEW_DATA_START_OFFSET; j += DATA_BLOCK_LENGTH) {
222
+ this.map[i++] = 0;
223
+ }
224
+
225
+ // set the remaining indexes in the BMP index-2 block
226
+ // to the null data block
227
+ for (i = 0x80 >> SHIFT_2; i < INDEX_2_BMP_LENGTH; i++) {
228
+ this.index2[i] = DATA_NULL_OFFSET;
229
+ }
230
+
231
+ // Fill the index gap with impossible values so that compaction
232
+ // does not overlap other index-2 blocks with the gap.
233
+ for (i = 0; i < INDEX_GAP_LENGTH; i++) {
234
+ this.index2[INDEX_GAP_OFFSET + i] = -1;
235
+ }
236
+
237
+ // set the indexes in the null index-2 block
238
+ for (i = 0; i < INDEX_2_BLOCK_LENGTH; i++) {
239
+ this.index2[INDEX_2_NULL_OFFSET + i] = DATA_NULL_OFFSET;
240
+ }
241
+
242
+ this.index2NullOffset = INDEX_2_NULL_OFFSET;
243
+ this.index2Length = INDEX_2_START_OFFSET;
244
+
245
+ // set the index-1 indexes for the linear index-2 block
246
+ j = 0;
247
+ for (i = 0; i < OMITTED_BMP_INDEX_1_LENGTH; i++) {
248
+ this.index1[i] = j;
249
+ j += INDEX_2_BLOCK_LENGTH;
250
+ }
251
+
252
+ // set the remaining index-1 indexes to the null index-2 block
253
+ for (i = i; i < INDEX_1_LENGTH; i++) {
254
+ this.index1[i] = INDEX_2_NULL_OFFSET;
255
+ }
256
+
257
+ // Preallocate and reset data for U+0080..U+07ff,
258
+ // for 2-byte UTF-8 which will be compacted in 64-blocks
259
+ // even if DATA_BLOCK_LENGTH is smaller.
260
+ for (i = 0x80; i < 0x800; i += DATA_BLOCK_LENGTH) {
261
+ this.set(i, this.initialValue);
262
+ }
263
+
264
+ }
265
+
266
+ set(codePoint, value) {
267
+ if ((codePoint < 0) || (codePoint > 0x10ffff)) {
268
+ throw new Error('Invalid code point');
269
+ }
270
+
271
+ if (this.isCompacted) {
272
+ throw new Error('Already compacted');
273
+ }
274
+
275
+ const block = this._getDataBlock(codePoint, true);
276
+ this.data[block + (codePoint & DATA_MASK)] = value;
277
+ return this;
278
+ }
279
+
280
+ setRange(start, end, value, overwrite) {
281
+ let block, repeatBlock;
282
+ if (overwrite == null) {
283
+ overwrite = true;
284
+ }
285
+ if ((start > 0x10ffff) || (end > 0x10ffff) || (start > end)) {
286
+ throw new Error('Invalid code point');
287
+ }
288
+
289
+ if (this.isCompacted) {
290
+ throw new Error('Already compacted');
291
+ }
292
+
293
+ if (!overwrite && (value === this.initialValue)) {
294
+ return this; // nothing to do
295
+ }
296
+
297
+ let limit = end + 1;
298
+ if ((start & DATA_MASK) !== 0) {
299
+ // set partial block at [start..following block boundary
300
+ block = this._getDataBlock(start, true);
301
+
302
+ const nextStart = (start + DATA_BLOCK_LENGTH) & ~DATA_MASK;
303
+ if (nextStart <= limit) {
304
+ this._fillBlock(block, start & DATA_MASK, DATA_BLOCK_LENGTH, value, this.initialValue, overwrite);
305
+ start = nextStart;
306
+ } else {
307
+ this._fillBlock(block, start & DATA_MASK, limit & DATA_MASK, value, this.initialValue, overwrite);
308
+ return this;
309
+ }
310
+ }
311
+
312
+ // number of positions in the last, partial block
313
+ const rest = limit & DATA_MASK;
314
+
315
+ // round down limit to a block boundary
316
+ limit &= ~DATA_MASK;
317
+
318
+ // iterate over all-value blocks
319
+ if (value === this.initialValue) {
320
+ repeatBlock = this.dataNullOffset;
321
+ } else {
322
+ repeatBlock = -1;
323
+ }
324
+
325
+ while (start < limit) {
326
+ let setRepeatBlock = false;
327
+
328
+ if ((value === this.initialValue) && this._isInNullBlock(start, true)) {
329
+ start += DATA_BLOCK_LENGTH; // nothing to do
330
+ continue;
331
+ }
332
+
333
+ // get index value
334
+ let i2 = this._getIndex2Block(start, true);
335
+ i2 += (start >> SHIFT_2) & INDEX_2_MASK;
336
+
337
+ block = this.index2[i2];
338
+ if (this._isWritableBlock(block)) {
339
+ // already allocated
340
+ if (overwrite && (block >= DATA_0800_OFFSET)) {
341
+ // We overwrite all values, and it's not a
342
+ // protected (ASCII-linear or 2-byte UTF-8) block:
343
+ // replace with the repeatBlock.
344
+ setRepeatBlock = true;
345
+ } else {
346
+ // protected block: just write the values into this block
347
+ this._fillBlock(block, 0, DATA_BLOCK_LENGTH, value, this.initialValue, overwrite);
348
+ }
349
+
350
+ } else if ((this.data[block] !== value) && (overwrite || (block === this.dataNullOffset))) {
351
+ // Set the repeatBlock instead of the null block or previous repeat block:
352
+ //
353
+ // If !isWritableBlock() then all entries in the block have the same value
354
+ // because it's the null block or a range block (the repeatBlock from a previous
355
+ // call to utrie2_setRange32()).
356
+ // No other blocks are used multiple times before compacting.
357
+ //
358
+ // The null block is the only non-writable block with the initialValue because
359
+ // of the repeatBlock initialization above. (If value==initialValue, then
360
+ // the repeatBlock will be the null data block.)
361
+ //
362
+ // We set our repeatBlock if the desired value differs from the block's value,
363
+ // and if we overwrite any data or if the data is all initial values
364
+ // (which is the same as the block being the null block, see above).
365
+ setRepeatBlock = true;
366
+ }
367
+
368
+ if (setRepeatBlock) {
369
+ if (repeatBlock >= 0) {
370
+ this._setIndex2Entry(i2, repeatBlock);
371
+ } else {
372
+ // create and set and fill the repeatBlock
373
+ repeatBlock = this._getDataBlock(start, true);
374
+ this._writeBlock(repeatBlock, value);
375
+ }
376
+ }
377
+
378
+ start += DATA_BLOCK_LENGTH;
379
+ }
380
+
381
+ if (rest > 0) {
382
+ // set partial block at [last block boundary..limit
383
+ block = this._getDataBlock(start, true);
384
+ this._fillBlock(block, 0, rest, value, this.initialValue, overwrite);
385
+ }
386
+
387
+ return this;
388
+ }
389
+
390
+ get(c, fromLSCP) {
391
+ let i2;
392
+ if (fromLSCP == null) {
393
+ fromLSCP = true;
394
+ }
395
+ if ((c < 0) || (c > 0x10ffff)) {
396
+ return this.errorValue;
397
+ }
398
+
399
+ if ((c >= this.highStart) && (!((c >= 0xd800) && (c < 0xdc00)) || fromLSCP)) {
400
+ return this.data[this.dataLength - DATA_GRANULARITY];
401
+ }
402
+
403
+ if (((c >= 0xd800) && (c < 0xdc00)) && fromLSCP) {
404
+ i2 = (LSCP_INDEX_2_OFFSET - (0xd800 >> SHIFT_2)) + (c >> SHIFT_2);
405
+ } else {
406
+ i2 = this.index1[c >> SHIFT_1] + ((c >> SHIFT_2) & INDEX_2_MASK);
407
+ }
408
+
409
+ const block = this.index2[i2];
410
+ return this.data[block + (c & DATA_MASK)];
411
+ }
412
+
413
+ _isInNullBlock(c, forLSCP) {
414
+ let i2;
415
+ if (((c & 0xfffffc00) === 0xd800) && forLSCP) {
416
+ i2 = (LSCP_INDEX_2_OFFSET - (0xd800 >> SHIFT_2)) + (c >> SHIFT_2);
417
+ } else {
418
+ i2 = this.index1[c >> SHIFT_1] + ((c >> SHIFT_2) & INDEX_2_MASK);
419
+ }
420
+
421
+ const block = this.index2[i2];
422
+ return block === this.dataNullOffset;
423
+ }
424
+
425
+ _allocIndex2Block() {
426
+ const newBlock = this.index2Length;
427
+ const newTop = newBlock + INDEX_2_BLOCK_LENGTH;
428
+ if (newTop > this.index2.length) {
429
+ // Should never occur.
430
+ // Either MAX_BUILD_TIME_INDEX_LENGTH is incorrect,
431
+ // or the code writes more values than should be possible.
432
+ throw new Error("Internal error in Trie2 creation.");
433
+ }
434
+
435
+ this.index2Length = newTop;
436
+ this.index2.set(this.index2.subarray(this.index2NullOffset, this.index2NullOffset + INDEX_2_BLOCK_LENGTH), newBlock);
437
+
438
+ return newBlock;
439
+ }
440
+
441
+ _getIndex2Block(c, forLSCP) {
442
+ if ((c >= 0xd800) && (c < 0xdc00) && forLSCP) {
443
+ return LSCP_INDEX_2_OFFSET;
444
+ }
445
+
446
+ const i1 = c >> SHIFT_1;
447
+ let i2 = this.index1[i1];
448
+ if (i2 === this.index2NullOffset) {
449
+ i2 = this._allocIndex2Block();
450
+ this.index1[i1] = i2;
451
+ }
452
+
453
+ return i2;
454
+ }
455
+
456
+ _isWritableBlock(block) {
457
+ return (block !== this.dataNullOffset) && (this.map[block >> SHIFT_2] === 1);
458
+ }
459
+
460
+ _allocDataBlock(copyBlock) {
461
+ let newBlock;
462
+ if (this.firstFreeBlock !== 0) {
463
+ // get the first free block
464
+ newBlock = this.firstFreeBlock;
465
+ this.firstFreeBlock = -this.map[newBlock >> SHIFT_2];
466
+ } else {
467
+ // get a new block from the high end
468
+ newBlock = this.dataLength;
469
+ const newTop = newBlock + DATA_BLOCK_LENGTH;
470
+ if (newTop > this.dataCapacity) {
471
+ // out of memory in the data array
472
+ let capacity;
473
+ if (this.dataCapacity < MEDIUM_DATA_LENGTH) {
474
+ capacity = MEDIUM_DATA_LENGTH;
475
+ } else if (this.dataCapacity < MAX_DATA_LENGTH_BUILDTIME) {
476
+ capacity = MAX_DATA_LENGTH_BUILDTIME;
477
+ } else {
478
+ // Should never occur.
479
+ // Either MAX_DATA_LENGTH_BUILDTIME is incorrect,
480
+ // or the code writes more values than should be possible.
481
+ throw new Error("Internal error in Trie2 creation.");
482
+ }
483
+
484
+ const newData = new Uint32Array(capacity);
485
+ newData.set(this.data.subarray(0, this.dataLength));
486
+ this.data = newData;
487
+ this.dataCapacity = capacity;
488
+ }
489
+
490
+ this.dataLength = newTop;
491
+ }
492
+
493
+ this.data.set(this.data.subarray(copyBlock, copyBlock + DATA_BLOCK_LENGTH), newBlock);
494
+ this.map[newBlock >> SHIFT_2] = 0;
495
+ return newBlock;
496
+ }
497
+
498
+ _releaseDataBlock(block) {
499
+ // put this block at the front of the free-block chain
500
+ this.map[block >> SHIFT_2] = -this.firstFreeBlock;
501
+ this.firstFreeBlock = block;
502
+ }
503
+
504
+ _setIndex2Entry(i2, block) {
505
+ ++this.map[block >> SHIFT_2]; // increment first, in case block == oldBlock!
506
+ const oldBlock = this.index2[i2];
507
+ if (--this.map[oldBlock >> SHIFT_2] === 0) {
508
+ this._releaseDataBlock(oldBlock);
509
+ }
510
+
511
+ this.index2[i2] = block;
512
+ }
513
+
514
+ _getDataBlock(c, forLSCP) {
515
+ let i2 = this._getIndex2Block(c, forLSCP);
516
+ i2 += (c >> SHIFT_2) & INDEX_2_MASK;
517
+
518
+ const oldBlock = this.index2[i2];
519
+ if (this._isWritableBlock(oldBlock)) {
520
+ return oldBlock;
521
+ }
522
+
523
+ // allocate a new data block
524
+ const newBlock = this._allocDataBlock(oldBlock);
525
+ this._setIndex2Entry(i2, newBlock);
526
+ return newBlock;
527
+ }
528
+
529
+ _fillBlock(block, start, limit, value, initialValue, overwrite) {
530
+ let i;
531
+ if (overwrite) {
532
+ for (i = block + start; i < block + limit; i++) {
533
+ this.data[i] = value;
534
+ }
535
+ } else {
536
+ for (i = block + start; i < block + limit; i++) {
537
+ if (this.data[i] === initialValue) {
538
+ this.data[i] = value;
539
+ }
540
+ }
541
+ }
542
+ }
543
+
544
+ _writeBlock(block, value) {
545
+ const limit = block + DATA_BLOCK_LENGTH;
546
+ while (block < limit) {
547
+ this.data[block++] = value;
548
+ }
549
+ }
550
+
551
+ _findHighStart(highValue) {
552
+ let prevBlock, prevI2Block;
553
+ const data32 = this.data;
554
+ const { initialValue } = this;
555
+ const { index2NullOffset } = this;
556
+ const nullBlock = this.dataNullOffset;
557
+
558
+ // set variables for previous range
559
+ if (highValue === initialValue) {
560
+ prevI2Block = index2NullOffset;
561
+ prevBlock = nullBlock;
562
+ } else {
563
+ prevI2Block = -1;
564
+ prevBlock = -1;
565
+ }
566
+
567
+ const prev = 0x110000;
568
+
569
+ // enumerate index-2 blocks
570
+ let i1 = INDEX_1_LENGTH;
571
+ let c = prev;
572
+ while (c > 0) {
573
+ const i2Block = this.index1[--i1];
574
+ if (i2Block === prevI2Block) {
575
+ // the index-2 block is the same as the previous one, and filled with highValue
576
+ c -= CP_PER_INDEX_1_ENTRY;
577
+ continue;
578
+ }
579
+
580
+ prevI2Block = i2Block;
581
+ if (i2Block === index2NullOffset) {
582
+ // this is the null index-2 block
583
+ if (highValue !== initialValue) {
584
+ return c;
585
+ }
586
+ c -= CP_PER_INDEX_1_ENTRY;
587
+ } else {
588
+ // enumerate data blocks for one index-2 block
589
+ let i2 = INDEX_2_BLOCK_LENGTH;
590
+ while (i2 > 0) {
591
+ const block = this.index2[i2Block + --i2];
592
+ if (block === prevBlock) {
593
+ // the block is the same as the previous one, and filled with highValue
594
+ c -= DATA_BLOCK_LENGTH;
595
+ continue;
596
+ }
597
+
598
+ prevBlock = block;
599
+ if (block === nullBlock) {
600
+ // this is the null data block
601
+ if (highValue !== initialValue) {
602
+ return c;
603
+ }
604
+ c -= DATA_BLOCK_LENGTH;
605
+ } else {
606
+ let j = DATA_BLOCK_LENGTH;
607
+ while (j > 0) {
608
+ const value = data32[block + --j];
609
+ if (value !== highValue) {
610
+ return c;
611
+ }
612
+ --c;
613
+ }
614
+ }
615
+ }
616
+ }
617
+ }
618
+
619
+ // deliver last range
620
+ return 0;
621
+ }
622
+
623
+ _findSameDataBlock(dataLength, otherBlock, blockLength) {
624
+ // ensure that we do not even partially get past dataLength
625
+ dataLength -= blockLength;
626
+ let block = 0;
627
+ while (block <= dataLength) {
628
+ if (equal_int(this.data, block, otherBlock, blockLength)) {
629
+ return block;
630
+ }
631
+ block += DATA_GRANULARITY;
632
+ }
633
+
634
+ return -1;
635
+ }
636
+
637
+ _findSameIndex2Block(index2Length, otherBlock) {
638
+ // ensure that we do not even partially get past index2Length
639
+ index2Length -= INDEX_2_BLOCK_LENGTH;
640
+ for (let block = 0; block <= index2Length; block++) {
641
+ if (equal_int(this.index2, block, otherBlock, INDEX_2_BLOCK_LENGTH)) {
642
+ return block;
643
+ }
644
+ }
645
+
646
+ return -1;
647
+ }
648
+
649
+ _compactData() {
650
+ // do not compact linear-ASCII data
651
+ let newStart = DATA_START_OFFSET;
652
+ let start = 0;
653
+ let i = 0;
654
+
655
+ while (start < newStart) {
656
+ this.map[i++] = start;
657
+ start += DATA_BLOCK_LENGTH;
658
+ }
659
+
660
+ // Start with a block length of 64 for 2-byte UTF-8,
661
+ // then switch to DATA_BLOCK_LENGTH.
662
+ let blockLength = 64;
663
+ let blockCount = blockLength >> SHIFT_2;
664
+ start = newStart;
665
+ while (start < this.dataLength) {
666
+ // start: index of first entry of current block
667
+ // newStart: index where the current block is to be moved
668
+ // (right after current end of already-compacted data)
669
+ var mapIndex, movedStart;
670
+ if (start === DATA_0800_OFFSET) {
671
+ blockLength = DATA_BLOCK_LENGTH;
672
+ blockCount = 1;
673
+ }
674
+
675
+ // skip blocks that are not used
676
+ if (this.map[start >> SHIFT_2] <= 0) {
677
+ // advance start to the next block
678
+ start += blockLength;
679
+
680
+ // leave newStart with the previous block!
681
+ continue;
682
+ }
683
+
684
+ // search for an identical block
685
+ if ((movedStart = this._findSameDataBlock(newStart, start, blockLength)) >= 0) {
686
+ // found an identical block, set the other block's index value for the current block
687
+ mapIndex = start >> SHIFT_2;
688
+ for (i = blockCount; i > 0; i--) {
689
+ this.map[mapIndex++] = movedStart;
690
+ movedStart += DATA_BLOCK_LENGTH;
691
+ }
692
+
693
+ // advance start to the next block
694
+ start += blockLength;
695
+
696
+ // leave newStart with the previous block!
697
+ continue;
698
+ }
699
+
700
+ // see if the beginning of this block can be overlapped with the end of the previous block
701
+ // look for maximum overlap (modulo granularity) with the previous, adjacent block
702
+ let overlap = blockLength - DATA_GRANULARITY;
703
+ while ((overlap > 0) && !equal_int(this.data, (newStart - overlap), start, overlap)) {
704
+ overlap -= DATA_GRANULARITY;
705
+ }
706
+
707
+ if ((overlap > 0) || (newStart < start)) {
708
+ // some overlap, or just move the whole block
709
+ movedStart = newStart - overlap;
710
+ mapIndex = start >> SHIFT_2;
711
+
712
+ for (i = blockCount; i > 0; i--) {
713
+ this.map[mapIndex++] = movedStart;
714
+ movedStart += DATA_BLOCK_LENGTH;
715
+ }
716
+
717
+ // move the non-overlapping indexes to their new positions
718
+ start += overlap;
719
+ for (i = blockLength - overlap; i > 0; i--) {
720
+ this.data[newStart++] = this.data[start++];
721
+ }
722
+
723
+ } else { // no overlap && newStart==start
724
+ mapIndex = start >> SHIFT_2;
725
+ for (i = blockCount; i > 0; i--) {
726
+ this.map[mapIndex++] = start;
727
+ start += DATA_BLOCK_LENGTH;
728
+ }
729
+
730
+ newStart = start;
731
+ }
732
+ }
733
+
734
+ // now adjust the index-2 table
735
+ i = 0;
736
+ while (i < this.index2Length) {
737
+ // Gap indexes are invalid (-1). Skip over the gap.
738
+ if (i === INDEX_GAP_OFFSET) {
739
+ i += INDEX_GAP_LENGTH;
740
+ }
741
+ this.index2[i] = this.map[this.index2[i] >> SHIFT_2];
742
+ ++i;
743
+ }
744
+
745
+ this.dataNullOffset = this.map[this.dataNullOffset >> SHIFT_2];
746
+
747
+ // ensure dataLength alignment
748
+ while ((newStart & (DATA_GRANULARITY - 1)) !== 0) {
749
+ this.data[newStart++] = this.initialValue;
750
+ }
751
+ this.dataLength = newStart;
752
+ }
753
+
754
+ _compactIndex2() {
755
+ // do not compact linear-BMP index-2 blocks
756
+ let newStart = INDEX_2_BMP_LENGTH;
757
+ let start = 0;
758
+ let i = 0;
759
+
760
+ while (start < newStart) {
761
+ this.map[i++] = start;
762
+ start += INDEX_2_BLOCK_LENGTH;
763
+ }
764
+
765
+ // Reduce the index table gap to what will be needed at runtime.
766
+ newStart += UTF8_2B_INDEX_2_LENGTH + ((this.highStart - 0x10000) >> SHIFT_1);
767
+ start = INDEX_2_NULL_OFFSET;
768
+ while (start < this.index2Length) {
769
+ // start: index of first entry of current block
770
+ // newStart: index where the current block is to be moved
771
+ // (right after current end of already-compacted data)
772
+
773
+ // search for an identical block
774
+ var movedStart;
775
+ if ((movedStart = this._findSameIndex2Block(newStart, start)) >= 0) {
776
+ // found an identical block, set the other block's index value for the current block
777
+ this.map[start >> SHIFT_1_2] = movedStart;
778
+
779
+ // advance start to the next block
780
+ start += INDEX_2_BLOCK_LENGTH;
781
+
782
+ // leave newStart with the previous block!
783
+ continue;
784
+ }
785
+
786
+ // see if the beginning of this block can be overlapped with the end of the previous block
787
+ // look for maximum overlap with the previous, adjacent block
788
+ let overlap = INDEX_2_BLOCK_LENGTH - 1;
789
+ while ((overlap > 0) && !equal_int(this.index2, (newStart - overlap), start, overlap)) {
790
+ --overlap;
791
+ }
792
+
793
+ if ((overlap > 0) || (newStart < start)) {
794
+ // some overlap, or just move the whole block
795
+ this.map[start >> SHIFT_1_2] = newStart - overlap;
796
+
797
+ // move the non-overlapping indexes to their new positions
798
+ start += overlap;
799
+ for (i = INDEX_2_BLOCK_LENGTH - overlap; i > 0; i--) {
800
+ this.index2[newStart++] = this.index2[start++];
801
+ }
802
+
803
+ } else { // no overlap && newStart==start
804
+ this.map[start >> SHIFT_1_2] = start;
805
+ start += INDEX_2_BLOCK_LENGTH;
806
+ newStart = start;
807
+ }
808
+ }
809
+
810
+ // now adjust the index-1 table
811
+ for (i = 0; i < INDEX_1_LENGTH; i++) {
812
+ this.index1[i] = this.map[this.index1[i] >> SHIFT_1_2];
813
+ }
814
+
815
+ this.index2NullOffset = this.map[this.index2NullOffset >> SHIFT_1_2];
816
+
817
+ // Ensure data table alignment:
818
+ // Needs to be granularity-aligned for 16-bit trie
819
+ // (so that dataMove will be down-shiftable),
820
+ // and 2-aligned for uint32_t data.
821
+
822
+ // Arbitrary value: 0x3fffc not possible for real data.
823
+ while ((newStart & ((DATA_GRANULARITY - 1) | 1)) !== 0) {
824
+ this.index2[newStart++] = 0x0000ffff << INDEX_SHIFT;
825
+ }
826
+
827
+ this.index2Length = newStart;
828
+ }
829
+
830
+ _compact() {
831
+ // find highStart and round it up
832
+ let highValue = this.get(0x10ffff);
833
+ let highStart = this._findHighStart(highValue);
834
+ highStart = (highStart + (CP_PER_INDEX_1_ENTRY - 1)) & ~(CP_PER_INDEX_1_ENTRY - 1);
835
+ if (highStart === 0x110000) {
836
+ highValue = this.errorValue;
837
+ }
838
+
839
+ // Set trie->highStart only after utrie2_get32(trie, highStart).
840
+ // Otherwise utrie2_get32(trie, highStart) would try to read the highValue.
841
+ this.highStart = highStart;
842
+ if (this.highStart < 0x110000) {
843
+ // Blank out [highStart..10ffff] to release associated data blocks.
844
+ const suppHighStart = this.highStart <= 0x10000 ? 0x10000 : this.highStart;
845
+ this.setRange(suppHighStart, 0x10ffff, this.initialValue, true);
846
+ }
847
+
848
+ this._compactData();
849
+ if (this.highStart > 0x10000) {
850
+ this._compactIndex2();
851
+ }
852
+
853
+ // Store the highValue in the data array and round up the dataLength.
854
+ // Must be done after compactData() because that assumes that dataLength
855
+ // is a multiple of DATA_BLOCK_LENGTH.
856
+ this.data[this.dataLength++] = highValue;
857
+ while ((this.dataLength & (DATA_GRANULARITY - 1)) !== 0) {
858
+ this.data[this.dataLength++] = this.initialValue;
859
+ }
860
+
861
+ this.isCompacted = true;
862
+ }
863
+
864
+ freeze() {
865
+ let allIndexesLength, i;
866
+ if (!this.isCompacted) {
867
+ this._compact();
868
+ }
869
+
870
+ if (this.highStart <= 0x10000) {
871
+ allIndexesLength = INDEX_1_OFFSET;
872
+ } else {
873
+ allIndexesLength = this.index2Length;
874
+ }
875
+
876
+ const dataMove = allIndexesLength;
877
+
878
+ // 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.");
884
+ }
885
+
886
+ // calculate the sizes of, and allocate, the index and data arrays
887
+ const indexLength = allIndexesLength + this.dataLength;
888
+ const data = new Int32Array(indexLength);
889
+
890
+ // write the index-2 array values shifted right by INDEX_SHIFT, after adding dataMove
891
+ let destIdx = 0;
892
+ for (i = 0; i < INDEX_2_BMP_LENGTH; i++) {
893
+ data[destIdx++] = ((this.index2[i] + dataMove) >> INDEX_SHIFT);
894
+ }
895
+
896
+ // write UTF-8 2-byte index-2 values, not right-shifted
897
+ for (i = 0; i < 0xc2 - 0xc0; i++) { // C0..C1
898
+ data[destIdx++] = (dataMove + BAD_UTF8_DATA_OFFSET);
899
+ }
900
+
901
+ for (i = i; i < 0xe0 - 0xc0; i++) { // C2..DF
902
+ data[destIdx++] = (dataMove + this.index2[i << (6 - SHIFT_2)]);
903
+ }
904
+
905
+ if (this.highStart > 0x10000) {
906
+ const index1Length = (this.highStart - 0x10000) >> SHIFT_1;
907
+ const index2Offset = INDEX_2_BMP_LENGTH + UTF8_2B_INDEX_2_LENGTH + index1Length;
908
+
909
+ // write 16-bit index-1 values for supplementary code points
910
+ for (i = 0; i < index1Length; i++) {
911
+ data[destIdx++] = (INDEX_2_OFFSET + this.index1[i + OMITTED_BMP_INDEX_1_LENGTH]);
912
+ }
913
+
914
+ // write the index-2 array values for supplementary code points,
915
+ // shifted right by INDEX_SHIFT, after adding dataMove
916
+ for (i = 0; i < this.index2Length - index2Offset; i++) {
917
+ data[destIdx++] = ((dataMove + this.index2[index2Offset + i]) >> INDEX_SHIFT);
918
+ }
919
+ }
920
+
921
+ // write 16-bit data values
922
+ for (i = 0; i < this.dataLength; i++) {
923
+ data[destIdx++] = this.data[i];
924
+ }
925
+
926
+ const dest = new UnicodeTrie({
927
+ data,
928
+ highStart: this.highStart,
929
+ errorValue: this.errorValue
930
+ });
931
+
932
+ return dest;
933
+ }
934
+
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];
942
+ toBuffer() {
943
+ const trie = this.freeze();
944
+
945
+ const data = new Uint8Array(trie.data.buffer);
946
+
947
+ // swap bytes to little-endian
948
+ swap32LE(data);
949
+
950
+ const compressed = deflateSync(data);
951
+
952
+ const buf = Buffer.alloc(compressed.length + 12);
953
+ buf.writeUInt32LE(trie.highStart, 0);
954
+ buf.writeUInt32LE(trie.errorValue, 4);
955
+ buf.writeUInt32LE(data.length, 8);
956
+ for (let i = 0; i < compressed.length; i++) {
957
+ const b = compressed[i];
958
+ buf[i + 12] = b;
959
+ }
960
+
961
+ return buf;
962
+ }
963
+ }
964
+
965
+ export default UnicodeTrieBuilder;