@margoengine/loaders 0.0.0-stage → 0.1.0

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 (184) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1198 -3
  4. package/dist/3dm.d.ts +161 -0
  5. package/dist/3dm.js +289 -0
  6. package/dist/3ds.d.ts +124 -0
  7. package/dist/3ds.js +392 -0
  8. package/dist/acclaim.d.ts +90 -0
  9. package/dist/acclaim.js +312 -0
  10. package/dist/amf.d.ts +45 -0
  11. package/dist/amf.js +256 -0
  12. package/dist/babylon.d.ts +197 -0
  13. package/dist/babylon.js +701 -0
  14. package/dist/common.d.ts +7 -0
  15. package/dist/common.js +6 -0
  16. package/dist/dae-animation.d.ts +66 -0
  17. package/dist/dae-animation.js +266 -0
  18. package/dist/dae-common.d.ts +95 -0
  19. package/dist/dae-common.js +226 -0
  20. package/dist/dae-controllers.d.ts +57 -0
  21. package/dist/dae-controllers.js +164 -0
  22. package/dist/dae-pose.d.ts +131 -0
  23. package/dist/dae-pose.js +381 -0
  24. package/dist/dae-scene.d.ts +203 -0
  25. package/dist/dae-scene.js +279 -0
  26. package/dist/dae.d.ts +161 -0
  27. package/dist/dae.js +633 -0
  28. package/dist/detect.d.ts +20 -0
  29. package/dist/detect.js +72 -0
  30. package/dist/dxf.d.ts +81 -0
  31. package/dist/dxf.js +774 -0
  32. package/dist/engine.d.ts +36 -0
  33. package/dist/engine.js +117 -0
  34. package/dist/font-opentype.d.ts +14 -0
  35. package/dist/font-opentype.js +33 -0
  36. package/dist/font-woff.d.ts +23 -0
  37. package/dist/font-woff.js +293 -0
  38. package/dist/font.d.ts +41 -0
  39. package/dist/font.js +26 -0
  40. package/dist/gcode.d.ts +55 -0
  41. package/dist/gcode.js +278 -0
  42. package/dist/geo-model.d.ts +84 -0
  43. package/dist/geo-model.js +220 -0
  44. package/dist/geo-mvt.d.ts +15 -0
  45. package/dist/geo-mvt.js +302 -0
  46. package/dist/geo-shp.d.ts +15 -0
  47. package/dist/geo-shp.js +234 -0
  48. package/dist/geo-xml.d.ts +6 -0
  49. package/dist/geo-xml.js +165 -0
  50. package/dist/geo.d.ts +8 -0
  51. package/dist/geo.js +59 -0
  52. package/dist/geometry.d.ts +60 -0
  53. package/dist/geometry.js +273 -0
  54. package/dist/index.d.ts +54 -0
  55. package/dist/index.js +28 -0
  56. package/dist/kmz.d.ts +78 -0
  57. package/dist/kmz.js +173 -0
  58. package/dist/las.d.ts +92 -0
  59. package/dist/las.js +110 -0
  60. package/dist/ldraw.d.ts +118 -0
  61. package/dist/ldraw.js +474 -0
  62. package/dist/load.d.ts +72 -0
  63. package/dist/load.js +205 -0
  64. package/dist/lottie.d.ts +154 -0
  65. package/dist/lottie.js +893 -0
  66. package/dist/lwo.d.ts +81 -0
  67. package/dist/lwo.js +454 -0
  68. package/dist/lzf.d.ts +13 -0
  69. package/dist/lzf.js +56 -0
  70. package/dist/materialx.d.ts +120 -0
  71. package/dist/materialx.js +563 -0
  72. package/dist/md2.d.ts +52 -0
  73. package/dist/md2.js +112 -0
  74. package/dist/mmcif.d.ts +14 -0
  75. package/dist/mmcif.js +246 -0
  76. package/dist/mmd.d.ts +244 -0
  77. package/dist/mmd.js +958 -0
  78. package/dist/mocap.d.ts +71 -0
  79. package/dist/mocap.js +178 -0
  80. package/dist/molecule-common.d.ts +4 -0
  81. package/dist/molecule-common.js +54 -0
  82. package/dist/mtl.d.ts +120 -0
  83. package/dist/mtl.js +469 -0
  84. package/dist/normals.d.ts +7 -0
  85. package/dist/normals.js +31 -0
  86. package/dist/nrrd.d.ts +63 -0
  87. package/dist/nrrd.js +743 -0
  88. package/dist/nurbs.d.ts +118 -0
  89. package/dist/nurbs.js +265 -0
  90. package/dist/obj.d.ts +143 -0
  91. package/dist/obj.js +456 -0
  92. package/dist/off.d.ts +10 -0
  93. package/dist/off.js +155 -0
  94. package/dist/outline-font.d.ts +87 -0
  95. package/dist/outline-font.js +110 -0
  96. package/dist/outline.d.ts +20 -0
  97. package/dist/outline.js +215 -0
  98. package/dist/pcd.d.ts +34 -0
  99. package/dist/pcd.js +258 -0
  100. package/dist/pdb.d.ts +67 -0
  101. package/dist/pdb.js +126 -0
  102. package/dist/ply.d.ts +35 -0
  103. package/dist/ply.js +392 -0
  104. package/dist/polygon.d.ts +9 -0
  105. package/dist/polygon.js +49 -0
  106. package/dist/prwm.d.ts +24 -0
  107. package/dist/prwm.js +114 -0
  108. package/dist/render.d.ts +27 -0
  109. package/dist/render.js +153 -0
  110. package/dist/sfnt-cff.d.ts +37 -0
  111. package/dist/sfnt-cff.js +749 -0
  112. package/dist/sfnt-layout.d.ts +24 -0
  113. package/dist/sfnt-layout.js +266 -0
  114. package/dist/sfnt-reader.d.ts +28 -0
  115. package/dist/sfnt-reader.js +87 -0
  116. package/dist/sfnt-variation.d.ts +40 -0
  117. package/dist/sfnt-variation.js +318 -0
  118. package/dist/shared/asset-url.js +82 -0
  119. package/dist/shared/crc32.d.ts +2 -0
  120. package/dist/shared/crc32.js +47 -0
  121. package/dist/shared/errors.js +36 -0
  122. package/dist/shared/inflate.js +284 -0
  123. package/dist/shared/normals.js +26 -0
  124. package/dist/shared/scene-scope.js +56 -0
  125. package/dist/shared/trailing-data.js +42 -0
  126. package/dist/stl.d.ts +64 -0
  127. package/dist/stl.js +309 -0
  128. package/dist/svg.d.ts +90 -0
  129. package/dist/svg.js +1007 -0
  130. package/dist/tds.d.ts +2 -0
  131. package/dist/tds.js +2 -0
  132. package/dist/textures.d.ts +42 -0
  133. package/dist/textures.js +53 -0
  134. package/dist/threemf-lattice.d.ts +139 -0
  135. package/dist/threemf-lattice.js +514 -0
  136. package/dist/threemf-materials.d.ts +75 -0
  137. package/dist/threemf-materials.js +314 -0
  138. package/dist/threemf-mesh.d.ts +60 -0
  139. package/dist/threemf-mesh.js +201 -0
  140. package/dist/threemf.d.ts +101 -0
  141. package/dist/threemf.js +415 -0
  142. package/dist/tilt.d.ts +62 -0
  143. package/dist/tilt.js +186 -0
  144. package/dist/tmx.d.ts +151 -0
  145. package/dist/tmx.js +394 -0
  146. package/dist/ttf.d.ts +78 -0
  147. package/dist/ttf.js +568 -0
  148. package/dist/types.d.ts +90 -0
  149. package/dist/types.js +35 -0
  150. package/dist/usd.d.ts +2 -0
  151. package/dist/usd.js +2 -0
  152. package/dist/usd.worker.d.ts +1 -0
  153. package/dist/usdz.d.ts +50 -0
  154. package/dist/usdz.js +646 -0
  155. package/dist/util.d.ts +75 -0
  156. package/dist/util.js +241 -0
  157. package/dist/vdb.d.ts +111 -0
  158. package/dist/vdb.js +663 -0
  159. package/dist/vrml.d.ts +53 -0
  160. package/dist/vrml.js +532 -0
  161. package/dist/vtk-cells.d.ts +29 -0
  162. package/dist/vtk-cells.js +211 -0
  163. package/dist/vtk-legacy.d.ts +57 -0
  164. package/dist/vtk-legacy.js +439 -0
  165. package/dist/vtk-reader.d.ts +19 -0
  166. package/dist/vtk-reader.js +134 -0
  167. package/dist/vtk-xml.d.ts +96 -0
  168. package/dist/vtk-xml.js +578 -0
  169. package/dist/vtk.d.ts +13 -0
  170. package/dist/vtk.js +234 -0
  171. package/dist/woff-glyf.d.ts +17 -0
  172. package/dist/woff-glyf.js +215 -0
  173. package/dist/worker.d.ts +10 -0
  174. package/dist/worker.js +93 -0
  175. package/dist/workers/usd.js +3 -0
  176. package/dist/xfile.d.ts +99 -0
  177. package/dist/xfile.js +706 -0
  178. package/dist/xml.d.ts +107 -0
  179. package/dist/xml.js +843 -0
  180. package/dist/xyz.d.ts +16 -0
  181. package/dist/xyz.js +110 -0
  182. package/dist/zip.d.ts +38 -0
  183. package/dist/zip.js +150 -0
  184. package/package.json +246 -6
@@ -0,0 +1,749 @@
1
+ import { BinaryReader, fontError } from './sfnt-reader.js';
2
+ import { FontItemStore } from './sfnt-variation.js';
3
+ // CFF and CFF2 outlines, clean room from Adobe Technical Notes #5176 (The Compact Font Format Specification) and
4
+ // #5177 (The Type 2 Charstring Format), and the OpenType specification's CFF2 chapter.
5
+ // Numerical character-set data from Adobe Technical Note #5176, Appendix C (GID order).
6
+ const cffExpert = [
7
+ 0,
8
+ 1,
9
+ 229,
10
+ 230,
11
+ 231,
12
+ 232,
13
+ 233,
14
+ 234,
15
+ 235,
16
+ 236,
17
+ 237,
18
+ 238,
19
+ 13,
20
+ 14,
21
+ 15,
22
+ 99,
23
+ ...Array.from({ length: 10 }, (_, i) => 239 + i),
24
+ 27,
25
+ 28,
26
+ ...Array.from({ length: 18 }, (_, i) => 249 + i),
27
+ 109,
28
+ 110,
29
+ ...Array.from({ length: 52 }, (_, i) => 267 + i),
30
+ 158,
31
+ 155,
32
+ 163,
33
+ ...Array.from({ length: 8 }, (_, i) => 319 + i),
34
+ 150,
35
+ 164,
36
+ 169,
37
+ ...Array.from({ length: 52 }, (_, i) => 327 + i),
38
+ ];
39
+ const cffExpertSubset = [
40
+ 0,
41
+ 1,
42
+ 231,
43
+ 232,
44
+ 235,
45
+ 236,
46
+ 237,
47
+ 238,
48
+ 13,
49
+ 14,
50
+ 15,
51
+ 99,
52
+ ...Array.from({ length: 10 }, (_, i) => 239 + i),
53
+ 27,
54
+ 28,
55
+ 249,
56
+ 250,
57
+ 251,
58
+ ...Array.from({ length: 14 }, (_, i) => 253 + i),
59
+ 109,
60
+ 110,
61
+ 267,
62
+ 268,
63
+ 269,
64
+ 270,
65
+ 272,
66
+ 300,
67
+ 301,
68
+ 302,
69
+ 305,
70
+ 314,
71
+ 315,
72
+ 158,
73
+ 155,
74
+ 163,
75
+ ...Array.from({ length: 7 }, (_, i) => 320 + i),
76
+ 150,
77
+ 164,
78
+ 169,
79
+ ...Array.from({ length: 20 }, (_, i) => 327 + i),
80
+ ];
81
+ function standardSID(code) {
82
+ if (!Number.isInteger(code) || code < 0 || code > 255)
83
+ fontError('a Type 2 composite character code is invalid');
84
+ if (code >= 32 && code <= 126)
85
+ return code - 31;
86
+ const ranges = [
87
+ [161, 175, 96],
88
+ [177, 180, 111],
89
+ [182, 189, 115],
90
+ [191, 191, 123],
91
+ [193, 200, 124],
92
+ [202, 203, 132],
93
+ [205, 208, 134],
94
+ [225, 225, 138],
95
+ [227, 227, 139],
96
+ [232, 235, 140],
97
+ [241, 241, 144],
98
+ [245, 245, 145],
99
+ [248, 251, 146],
100
+ ];
101
+ for (const [first, last, sid] of ranges)
102
+ if (code >= first && code <= last)
103
+ return sid + code - first;
104
+ return 0;
105
+ }
106
+ const DEFAULT_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
107
+ /** Bytes a charstring and its subroutines may execute before the glyph is refused. */
108
+ const EXECUTION_BUDGET = 100_000;
109
+ const validMatrix = (m) => Array.isArray(m) && m.length === 6 && m.every((v) => Number.isFinite(v));
110
+ /** Local subroutines all the Font DICTs of one CID-keyed font may list (they may share one INDEX). */
111
+ const MAX_LOCAL_SUBRS = 1 << 20;
112
+ /** The affine matrix `outer(inner(point))`. */
113
+ function concat([a, b, c, d, x, y], [e, f, g, h, u, v]) {
114
+ return [a * e + c * f, b * e + d * f, a * g + c * h, b * g + d * h, a * u + c * v + x, b * u + d * v + y];
115
+ }
116
+ /**
117
+ * The CFF (`'CFF '`, name-keyed or CID-keyed) or CFF2 outlines of one font. `commands(glyph)` runs the glyph's Type 2 charstring (blends at the
118
+ * font's variation coordinates for CFF2) into move, line and cubic commands in font units: the FontMatrix (and a
119
+ * CFF2 Font DICT's own matrix) times units per em.
120
+ */
121
+ export class CFFOutlines {
122
+ constructor(r, cff2, glyphCount, unitsPerEm, coordinates) {
123
+ this.cff2 = cff2;
124
+ this.glyphCount = glyphCount;
125
+ this.unitsPerEm = unitsPerEm;
126
+ this.coordinates = coordinates;
127
+ this.localSubrs = [];
128
+ this.charset = [];
129
+ this.matrix = DEFAULT_MATRIX;
130
+ this.cid = false;
131
+ this.fontDicts = [];
132
+ this.select = [];
133
+ const parsed = cff2 ? this.parseCFF2(r) : this.parseCFF(r);
134
+ this.charStrings = parsed.charStrings;
135
+ this.globalSubrs = parsed.globalSubrs;
136
+ }
137
+ /** Font units of a font matrix: the matrix times units per em. */
138
+ scaled(m) {
139
+ return m.map((v) => v * this.unitsPerEm);
140
+ }
141
+ parseCFF2(r) {
142
+ if (r.u8() !== 2 || r.u8() !== 0)
143
+ fontError('the CFF2 table version is not 2.0', 'unsupported');
144
+ const header = r.u8(), topLength = r.u16();
145
+ if (header < 5)
146
+ fontError('the CFF2 header is shorter than 5 bytes');
147
+ const top = this.parseDict(r.slice(header, topLength).bytes);
148
+ r.seek(header + topLength);
149
+ const globalSubrs = this.readIndex(r);
150
+ const offset = (dict, op) => {
151
+ const v = dict.get(op);
152
+ if (!Number.isInteger(v) || v < 0)
153
+ fontError(`CFF2 offset operator ${op} is invalid`);
154
+ return v;
155
+ };
156
+ if (top.get(24) !== undefined) {
157
+ r.seek(offset(top, 24));
158
+ const length = r.u16();
159
+ this.store = new FontItemStore(r.slice(r.pos, length), this.coordinates);
160
+ }
161
+ r.seek(offset(top, 17));
162
+ const charStrings = this.readIndex(r);
163
+ if (charStrings.length !== this.glyphCount)
164
+ fontError("the CFF2 CharStrings count differs from 'maxp'");
165
+ const matrix = top.get(0x0c07) ?? DEFAULT_MATRIX;
166
+ if (!validMatrix(matrix))
167
+ fontError('the CFF2 FontMatrix is invalid');
168
+ r.seek(offset(top, 0x0c24));
169
+ const dicts = this.readIndex(r);
170
+ if (!dicts.length)
171
+ fontError('the CFF2 Font DICT INDEX is empty');
172
+ this.fontDicts = dicts.map((bytes) => {
173
+ const dict = this.parseDict(bytes), own = this.privateDict(r, dict), local = dict.get(0x0c07);
174
+ if (local !== undefined && !validMatrix(local))
175
+ fontError('a CFF2 Font DICT matrix is invalid');
176
+ const combined = local === undefined ? matrix : concat(matrix, local);
177
+ return { ...own, matrix: this.scaled(combined) };
178
+ });
179
+ if (top.has(0x0c25))
180
+ this.readFDSelect(r, offset(top, 0x0c25), dicts.length, true);
181
+ else if (dicts.length !== 1)
182
+ fontError('the CFF2 font has several Font DICTs and no FDSelect');
183
+ else
184
+ this.select = new Array(this.glyphCount).fill(0);
185
+ return { charStrings, globalSubrs };
186
+ }
187
+ parseCFF(r) {
188
+ if (r.u8() !== 1)
189
+ fontError('the CFF table version is not 1', 'unsupported');
190
+ r.u8();
191
+ const headerSize = r.u8(), offSize = r.u8();
192
+ if (headerSize < 4 || offSize < 1 || offSize > 4)
193
+ fontError('the CFF header is invalid');
194
+ r.seek(headerSize);
195
+ const names = this.readIndex(r), topItems = this.readIndex(r);
196
+ if (names.length !== 1 || topItems.length !== 1)
197
+ fontError('the CFF FontSet holds more or fewer than one font');
198
+ const top = this.parseDict(topItems[0]);
199
+ // A CIDFont names its registry (ROS) and splits Private DICTs over an FDArray chosen per glyph by FDSelect
200
+ // (TN #5176, sections 18 and 19).
201
+ const cid = top.has(0x0c1e);
202
+ if (!cid && (top.has(0x0c24) || top.has(0x0c25)))
203
+ fontError('a CFF font holds an FDArray without a ROS operator');
204
+ if (top.has(0x0c14))
205
+ fontError('the font has synthetic CFF outlines, which are not read', 'unsupported');
206
+ if (top.has(0x0c06) && top.get(0x0c06) !== 2)
207
+ fontError('the CFF CharstringType is not 2', 'unsupported');
208
+ const matrix = top.get(0x0c07) ?? DEFAULT_MATRIX;
209
+ if (!validMatrix(matrix))
210
+ fontError('the CFF FontMatrix is invalid');
211
+ this.matrix = this.scaled(matrix);
212
+ this.cid = cid;
213
+ this.readIndex(r);
214
+ const globalSubrs = this.readIndex(r);
215
+ const charOffset = top.get(17);
216
+ if (typeof charOffset !== 'number')
217
+ fontError('the CFF CharStrings offset is missing');
218
+ r.seek(charOffset);
219
+ const charStrings = this.readIndex(r);
220
+ if (charStrings.length !== this.glyphCount)
221
+ fontError("the CFF CharStrings count differs from 'maxp'");
222
+ const charsetOffset = top.get(15) ?? 0;
223
+ if (typeof charsetOffset !== 'number')
224
+ fontError('the CFF charset offset is invalid');
225
+ if (charsetOffset <= 2) {
226
+ const preset = charsetOffset === 0
227
+ ? Array.from({ length: 229 }, (_, i) => i)
228
+ : charsetOffset === 1
229
+ ? cffExpert
230
+ : cffExpertSubset;
231
+ if (!Number.isInteger(charsetOffset) || charsetOffset < 0 || charStrings.length > preset.length)
232
+ fontError('the CFF predefined charset is invalid');
233
+ this.charset = preset.slice(0, charStrings.length);
234
+ }
235
+ else {
236
+ r.seek(charsetOffset);
237
+ const format = r.u8();
238
+ this.charset = [0];
239
+ if (format > 2)
240
+ fontError(`CFF charset format ${format} is unknown`);
241
+ while (this.charset.length < charStrings.length) {
242
+ const first = r.u16(), left = format === 0 ? 0 : format === 1 ? r.u8() : r.u16();
243
+ if (this.charset.length + left + 1 > charStrings.length || first + left > 65535)
244
+ fontError('a CFF charset range is invalid');
245
+ for (let i = 0; i <= left; i++)
246
+ this.charset.push(first + i);
247
+ }
248
+ }
249
+ if (new Set(this.charset).size !== this.charset.length)
250
+ fontError('the CFF charset repeats a SID');
251
+ if (!cid)
252
+ this.localSubrs = this.privateDict(r, top).subrs;
253
+ else {
254
+ const at = (op) => {
255
+ const v = top.get(op);
256
+ if (!Number.isInteger(v) || v <= 0)
257
+ fontError(`the CID-keyed CFF lacks the offset of operator ${op}`);
258
+ return v;
259
+ };
260
+ r.seek(at(0x0c24));
261
+ const dicts = this.readIndex(r);
262
+ if (!dicts.length || dicts.length > 256)
263
+ fontError('a CID-keyed CFF holds 1 to 256 Font DICTs');
264
+ let subrs = 0;
265
+ this.fontDicts = dicts.map((bytes) => {
266
+ const dict = this.parseDict(bytes), own = this.privateDict(r, dict);
267
+ if ((subrs += own.subrs.length) > MAX_LOCAL_SUBRS)
268
+ fontError(`the Font DICTs list more than ${MAX_LOCAL_SUBRS} subroutines`, 'limit');
269
+ const local = dict.get(0x0c07);
270
+ if (local !== undefined && !validMatrix(local))
271
+ fontError('a CFF Font DICT matrix is invalid');
272
+ // Both matrices apply, the Font DICT's first; one that is absent is the identity (both absent: the default).
273
+ const combined = local === undefined ? matrix : top.has(0x0c07) ? concat(matrix, local) : local;
274
+ return { subrs: own.subrs, vsindex: 0, matrix: this.scaled(combined) };
275
+ });
276
+ this.readFDSelect(r, at(0x0c25), dicts.length, false);
277
+ }
278
+ return { charStrings, globalSubrs };
279
+ }
280
+ /** The local subroutines and default `vsindex` of the Private DICT a Top or Font DICT points to, if any. */
281
+ privateDict(r, from) {
282
+ const priv = from.get(18);
283
+ if (priv === undefined)
284
+ return { subrs: [], vsindex: 0 };
285
+ if (!Array.isArray(priv) || priv.length !== 2)
286
+ fontError('a CFF Private DICT range is invalid');
287
+ const [length, start] = priv, dict = this.parseDict(r.slice(start, length).bytes);
288
+ let subrs = [];
289
+ if (dict.has(19)) {
290
+ const rel = dict.get(19);
291
+ if (typeof rel !== 'number')
292
+ fontError('a CFF Subrs offset is invalid');
293
+ r.seek(start + rel);
294
+ subrs = this.readIndex(r);
295
+ }
296
+ return { subrs, vsindex: Number(dict.get(22) ?? 0) };
297
+ }
298
+ /** The Font DICT of every glyph: FDSelect formats 0 and 3, and CFF2's 32-bit format 4. */
299
+ readFDSelect(r, offset, dicts, cff2) {
300
+ this.select = new Array(this.glyphCount).fill(0);
301
+ r.seek(offset);
302
+ const format = r.u8();
303
+ if (format === 0)
304
+ for (let i = 0; i < this.glyphCount; i++)
305
+ this.select[i] = r.u8();
306
+ else if (format === 3 || (cff2 && format === 4)) {
307
+ const wide = format === 4, n = wide ? r.u32() : r.u16();
308
+ r.check(r.pos, n * (wide ? 6 : 3));
309
+ const ranges = Array.from({ length: n }, () => ({
310
+ first: wide ? r.u32() : r.u16(),
311
+ fd: wide ? r.u16() : r.u8(),
312
+ }));
313
+ const sentinel = wide ? r.u32() : r.u16();
314
+ if (!n || ranges[0].first !== 0 || sentinel !== this.glyphCount)
315
+ fontError('the CFF FDSelect range is invalid');
316
+ for (let i = 0; i < n; i++) {
317
+ const end = i + 1 < n ? ranges[i + 1].first : sentinel;
318
+ if (end <= ranges[i].first || end > this.glyphCount)
319
+ fontError('the CFF FDSelect ranges are out of order');
320
+ this.select.fill(ranges[i].fd, ranges[i].first, end);
321
+ }
322
+ }
323
+ else
324
+ fontError(`CFF FDSelect format ${format} is unknown`, 'unsupported');
325
+ if (this.select.some((i) => i >= dicts))
326
+ fontError('the CFF FDSelect refers to a missing Font DICT');
327
+ }
328
+ readIndex(r) {
329
+ const count = this.cff2 ? r.u32() : r.u16();
330
+ if (count > 65536)
331
+ fontError('a CFF INDEX holds more than 65536 items');
332
+ if (!count)
333
+ return [];
334
+ const size = r.u8();
335
+ if (size < 1 || size > 4)
336
+ fontError('a CFF INDEX offSize is outside 1 to 4');
337
+ r.check(r.pos, (count + 1) * size);
338
+ const offsets = Array.from({ length: count + 1 }, () => {
339
+ let offset = 0;
340
+ for (let j = 0; j < size; j++)
341
+ offset = offset * 256 + r.u8();
342
+ return offset;
343
+ });
344
+ if (offsets[0] !== 1)
345
+ fontError("a CFF INDEX's first offset is not 1");
346
+ const start = r.pos, items = [];
347
+ for (let i = 0; i < count; i++)
348
+ items.push(r.slice(start + offsets[i] - 1, offsets[i + 1] - offsets[i]).bytes);
349
+ r.seek(start + offsets[count] - 1);
350
+ return items;
351
+ }
352
+ parseDict(bytes) {
353
+ const r = new BinaryReader(bytes), cff2 = this.cff2, dict = new Map(), stack = [];
354
+ while (r.pos < r.length) {
355
+ const b = r.u8();
356
+ if (cff2 && b === 23) {
357
+ const n = stack.pop();
358
+ const weights = this.store?.weights(Number(dict.get(22) ?? 0)) ?? fontError('a CFF2 DICT blends with no variation store');
359
+ const start = stack.length - n * (weights.length + 1);
360
+ if (!Number.isInteger(n) || n < 1 || start < 0)
361
+ fontError('a CFF2 DICT blend operand count is invalid');
362
+ for (let i = 0; i < n; i++)
363
+ for (let j = 0; j < weights.length; j++)
364
+ stack[start + i] += weights[j] * stack[start + n + i * weights.length + j];
365
+ stack.length = start + n;
366
+ }
367
+ else if (b <= (cff2 ? 25 : 21)) {
368
+ const op = b === 12 ? 0x0c00 + r.u8() : b;
369
+ dict.set(op, stack.length === 1 ? stack[0] : [...stack]);
370
+ stack.length = 0;
371
+ }
372
+ else if (b === 28)
373
+ stack.push(r.i16());
374
+ else if (b === 29)
375
+ stack.push(r.i32());
376
+ else if (b >= 32 && b <= 246)
377
+ stack.push(b - 139);
378
+ else if (b >= 247 && b <= 250)
379
+ stack.push((b - 247) * 256 + r.u8() + 108);
380
+ else if (b >= 251 && b <= 254)
381
+ stack.push(-(b - 251) * 256 - r.u8() - 108);
382
+ else if (b === 30) {
383
+ let value = '', done = false;
384
+ while (!done) {
385
+ const byte = r.u8();
386
+ for (const n of [byte >> 4, byte & 15]) {
387
+ if (n === 15) {
388
+ done = true;
389
+ break;
390
+ }
391
+ if (n === 13)
392
+ fontError('a CFF DICT real number has a reserved nibble');
393
+ value += n < 10 ? String(n) : n === 10 ? '.' : n === 11 ? 'E' : n === 12 ? 'E-' : '-';
394
+ }
395
+ }
396
+ const v = Number(value);
397
+ if (!Number.isFinite(v))
398
+ fontError('a CFF DICT real number is invalid');
399
+ stack.push(v);
400
+ }
401
+ else
402
+ fontError(`a CFF DICT holds the reserved byte ${b}`);
403
+ if (stack.length > (cff2 ? 513 : 48))
404
+ fontError('a CFF DICT operand stack exceeds its format limit');
405
+ }
406
+ if (stack.length)
407
+ fontError('a CFF DICT ends with operands and no operator');
408
+ return dict;
409
+ }
410
+ /** The glyph's outline in font units: one `'M'` per contour, `'L'` and `'C'` segments, `'Z'` at each close. */
411
+ commands(glyph) {
412
+ const [a, b, c, d, tx, ty] = this.fontDicts.length ? this.fontDicts[this.select[glyph]].matrix : this.matrix;
413
+ const at = (x, y) => [a * x + c * y + tx, b * x + d * y + ty];
414
+ return this.raw(glyph, false).map((cmd) => {
415
+ if (cmd[0] === 'M' || cmd[0] === 'L')
416
+ return [cmd[0], ...at(cmd[1], cmd[2])];
417
+ if (cmd[0] === 'C')
418
+ return ['C', ...at(cmd[1], cmd[2]), ...at(cmd[3], cmd[4]), ...at(cmd[5], cmd[6])];
419
+ return cmd;
420
+ });
421
+ }
422
+ /** Charstring space commands of one glyph; `component` marks a `seac`-style endchar accent or base. */
423
+ raw(glyphIndex, component) {
424
+ const charString = this.charStrings[glyphIndex], out = [];
425
+ if (!charString)
426
+ return out;
427
+ const cff2 = this.cff2, fontDict = this.fontDicts.length ? this.fontDicts[this.select[glyphIndex]] : undefined, stack = [], transient = new Array(32).fill(0);
428
+ let x = 0, y = 0, open = false, widthSeen = cff2, stems = 0, ended = false, operations = 0, seed = 1, vsindex = fontDict ? fontDict.vsindex : 0;
429
+ const close = () => {
430
+ if (open)
431
+ out.push(['Z']);
432
+ open = false;
433
+ };
434
+ const pop = () => (stack.length ? stack.pop() : fontError('a Type 2 charstring pops an empty stack'));
435
+ const need = (n) => {
436
+ if (stack.length !== n)
437
+ fontError('a Type 2 operator has the wrong operand count');
438
+ };
439
+ const width = (n, hints = false) => {
440
+ if (!widthSeen) {
441
+ if (hints ? stack.length % 2 === 1 : stack.length === n + 1)
442
+ stack.shift();
443
+ widthSeen = true;
444
+ }
445
+ };
446
+ const move = (dx, dy) => {
447
+ close();
448
+ x += dx;
449
+ y += dy;
450
+ out.push(['M', x, y]);
451
+ open = true;
452
+ };
453
+ const line = (dx, dy) => {
454
+ if (!dx && !dy)
455
+ return;
456
+ x += dx;
457
+ y += dy;
458
+ out.push(['L', x, y]);
459
+ };
460
+ const curve = (dx1, dy1, dx2, dy2, dx3, dy3) => {
461
+ const x1 = x + dx1, y1 = y + dy1, x2 = x1 + dx2, y2 = y1 + dy2;
462
+ x = x2 + dx3;
463
+ y = y2 + dy3;
464
+ out.push(['C', x1, y1, x2, y2, x, y]);
465
+ };
466
+ const execute = (bytes, depth) => {
467
+ if (depth > 10)
468
+ fontError('Type 2 subroutines nest deeper than 10', 'limit');
469
+ const r = new BinaryReader(bytes);
470
+ while (r.pos < r.length && !ended) {
471
+ if (++operations > EXECUTION_BUDGET)
472
+ fontError(`a Type 2 charstring runs more than ${EXECUTION_BUDGET} operations`, 'limit');
473
+ const b = r.u8();
474
+ if (b === 28)
475
+ stack.push(r.i16());
476
+ else if (b >= 32 && b <= 246)
477
+ stack.push(b - 139);
478
+ else if (b >= 247 && b <= 250)
479
+ stack.push((b - 247) * 256 + r.u8() + 108);
480
+ else if (b >= 251 && b <= 254)
481
+ stack.push(-(b - 251) * 256 - r.u8() - 108);
482
+ else if (b === 255)
483
+ stack.push(r.i32() / 65536);
484
+ else if (cff2 && b === 15) {
485
+ need(1);
486
+ vsindex = pop();
487
+ this.store?.weights(vsindex);
488
+ }
489
+ else if (cff2 && b === 16) {
490
+ const n = pop(), weights = this.store?.weights(vsindex) ?? fontError('a CFF2 charstring blends with no variation store');
491
+ const start = stack.length - n * (weights.length + 1);
492
+ if (!Number.isInteger(n) || n < 1 || start < 0)
493
+ fontError('a CFF2 blend operand count is invalid');
494
+ for (let i = 0; i < n; i++)
495
+ for (let j = 0; j < weights.length; j++)
496
+ stack[start + i] += weights[j] * stack[start + n + i * weights.length + j];
497
+ stack.length = start + n;
498
+ }
499
+ else if (b === 10 || b === 29) {
500
+ const subrs = (b === 10 ? (fontDict ? fontDict.subrs : this.localSubrs) : this.globalSubrs) ?? [];
501
+ const index = pop() + (subrs.length < 1240 ? 107 : subrs.length < 33900 ? 1131 : 32768);
502
+ if (!Number.isInteger(index) || !subrs[index])
503
+ fontError('a Type 2 subroutine index is invalid');
504
+ execute(subrs[index], depth + 1);
505
+ }
506
+ else if (b === 11) {
507
+ if (cff2)
508
+ fontError('a CFF2 charstring uses the reserved return operator');
509
+ if (!depth)
510
+ fontError('a Type 2 charstring returns outside a subroutine');
511
+ return;
512
+ }
513
+ else if (b === 1 || b === 3 || b === 18 || b === 23 || b === 19 || b === 20) {
514
+ width(0, true);
515
+ if (stack.length % 2)
516
+ fontError('a Type 2 stem hint has an odd operand count');
517
+ if (b !== 19 && b !== 20 && stack.length < 2)
518
+ fontError('a Type 2 stem hint has no pair');
519
+ stems += stack.length / 2;
520
+ stack.length = 0;
521
+ if (stems > 96)
522
+ fontError('a Type 2 charstring declares more than 96 hints', 'limit');
523
+ if (b === 19 || b === 20) {
524
+ if (!stems)
525
+ fontError('a Type 2 hint mask comes before any stem hint');
526
+ r.skip(Math.ceil(stems / 8));
527
+ }
528
+ }
529
+ else if (b === 4 || b === 21 || b === 22) {
530
+ const count = b === 21 ? 2 : 1;
531
+ width(count);
532
+ need(count);
533
+ move(b === 4 ? 0 : stack[0], b === 22 ? 0 : stack[count - 1]);
534
+ stack.length = 0;
535
+ }
536
+ else if (b === 14) {
537
+ if (cff2)
538
+ fontError('a CFF2 charstring uses the reserved endchar operator');
539
+ width(stack.length >= 4 ? 4 : 0);
540
+ if (stack.length === 4) {
541
+ // endchar with four operands: an accented character from two standard-encoding glyphs (TN #5177, Appendix C).
542
+ if (component || out.length)
543
+ fontError('a Type 2 endchar composite is nested or follows a path');
544
+ if (this.cid)
545
+ fontError('a CID-keyed font has no standard-encoding glyphs for an endchar composite');
546
+ const [dx, dy, base, accent] = stack;
547
+ const resolve = (code) => {
548
+ const sid = standardSID(code), index = this.charset.indexOf(sid);
549
+ if (!sid || index < 0)
550
+ fontError("a Type 2 endchar composite's component is missing from the charset");
551
+ return this.raw(index, true);
552
+ };
553
+ const first = resolve(base), second = resolve(accent);
554
+ out.push(...first);
555
+ for (const cmd of second)
556
+ out.push(cmd[0] === 'Z'
557
+ ? cmd
558
+ : cmd.map((v, i) => (i === 0 ? v : v + (i % 2 ? dx : dy))));
559
+ }
560
+ else if (stack.length)
561
+ fontError('a Type 2 endchar has the wrong operand count');
562
+ close();
563
+ ended = true;
564
+ }
565
+ else if (b === 12) {
566
+ const op = r.u8();
567
+ if (cff2 && op < 34)
568
+ fontError('a CFF2 charstring uses a reserved arithmetic operator');
569
+ if (op >= 34 && op <= 37 && !open)
570
+ fontError('a Type 2 flex comes before any moveto');
571
+ if (op === 34) {
572
+ need(7);
573
+ const [a, b, c, d, e, f, g] = stack;
574
+ curve(a, 0, b, c, d, 0);
575
+ curve(e, 0, f, -c, g, 0);
576
+ stack.length = 0;
577
+ }
578
+ else if (op === 35) {
579
+ need(13);
580
+ curve(...stack.slice(0, 6));
581
+ curve(...stack.slice(6, 12));
582
+ stack.length = 0;
583
+ }
584
+ else if (op === 36) {
585
+ need(9);
586
+ const [a, b, c, d, e, f, g, h, i] = stack;
587
+ curve(a, b, c, d, e, 0);
588
+ curve(f, 0, g, h, i, -b - d - h);
589
+ stack.length = 0;
590
+ }
591
+ else if (op === 37) {
592
+ need(11);
593
+ const v = [...stack], sx = v[0] + v[2] + v[4] + v[6] + v[8], sy = v[1] + v[3] + v[5] + v[7] + v[9];
594
+ curve(...v.slice(0, 6));
595
+ curve(v[6], v[7], v[8], v[9], Math.abs(sx) > Math.abs(sy) ? v[10] : -sx, Math.abs(sx) > Math.abs(sy) ? -sy : v[10]);
596
+ stack.length = 0;
597
+ }
598
+ else if (op === 0) {
599
+ /* The deprecated dotsection does not change outlines. */
600
+ }
601
+ else if (op === 18)
602
+ pop();
603
+ else if (op === 27) {
604
+ const a = pop();
605
+ stack.push(a, a);
606
+ }
607
+ else if (op === 28) {
608
+ const b = pop(), a = pop();
609
+ stack.push(b, a);
610
+ }
611
+ else if (op === 29) {
612
+ const i = Math.max(0, Math.trunc(pop()));
613
+ if (!stack.length)
614
+ fontError('a Type 2 index operator reads an empty stack');
615
+ stack.push(stack[Math.max(0, stack.length - 1 - i)]);
616
+ }
617
+ else if (op === 30) {
618
+ const j = pop(), n = pop();
619
+ if (!Number.isInteger(n) || !Number.isInteger(j) || n < 0 || n > stack.length)
620
+ fontError('a Type 2 roll range is invalid');
621
+ if (n) {
622
+ const a = stack.splice(stack.length - n), k = ((j % n) + n) % n;
623
+ stack.push(...a.slice(n - k), ...a.slice(0, n - k));
624
+ }
625
+ }
626
+ else if (op === 20 || op === 21) {
627
+ const i = pop();
628
+ if (!Number.isInteger(i) || i < 0 || i >= 32)
629
+ fontError('a Type 2 transient array index is outside 0 to 31');
630
+ if (op === 20)
631
+ transient[i] = pop();
632
+ else
633
+ stack.push(transient[i]);
634
+ }
635
+ else if (op === 22) {
636
+ const v2 = pop(), v1 = pop(), s2 = pop(), s1 = pop();
637
+ stack.push(v1 <= v2 ? s1 : s2);
638
+ }
639
+ else if (op === 23) {
640
+ seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
641
+ stack.push((seed + 1) / 4294967296);
642
+ }
643
+ else if (op === 5 || op === 9 || op === 14 || op === 26) {
644
+ const a = pop();
645
+ stack.push(op === 5 ? Number(!a) : op === 9 ? Math.abs(a) : op === 14 ? -a : Math.sqrt(a));
646
+ }
647
+ else if ([3, 4, 10, 11, 12, 15, 24].includes(op)) {
648
+ const b = pop(), a = pop();
649
+ stack.push(op === 3
650
+ ? Number(!!a && !!b)
651
+ : op === 4
652
+ ? Number(!!a || !!b)
653
+ : op === 10
654
+ ? a + b
655
+ : op === 11
656
+ ? a - b
657
+ : op === 12
658
+ ? a / b
659
+ : op === 15
660
+ ? Number(a === b)
661
+ : a * b);
662
+ }
663
+ else
664
+ fontError(`Type 2 escaped operator ${op} is unknown`, 'unsupported');
665
+ }
666
+ else {
667
+ const v = [...stack], n = v.length;
668
+ stack.length = 0;
669
+ const valid = (ok) => {
670
+ if (!ok)
671
+ fontError(`Type 2 operator ${b} has the wrong operand count`);
672
+ };
673
+ if (!open)
674
+ fontError('a Type 2 path does not begin with a moveto');
675
+ if (b === 5) {
676
+ valid(n >= 2 && n % 2 === 0);
677
+ for (let i = 0; i < n; i += 2)
678
+ line(v[i], v[i + 1]);
679
+ }
680
+ else if (b === 6 || b === 7) {
681
+ valid(n >= 1);
682
+ for (let i = 0; i < n; i++)
683
+ line((i % 2 === 0) === (b === 6) ? v[i] : 0, (i % 2 === 0) === (b === 7) ? v[i] : 0);
684
+ }
685
+ else if (b === 8) {
686
+ valid(n >= 6 && n % 6 === 0);
687
+ for (let i = 0; i < n; i += 6)
688
+ curve(...v.slice(i, i + 6));
689
+ }
690
+ else if (b === 24) {
691
+ valid(n >= 8 && (n - 2) % 6 === 0);
692
+ for (let i = 0; i < n - 2; i += 6)
693
+ curve(...v.slice(i, i + 6));
694
+ line(v[n - 2], v[n - 1]);
695
+ }
696
+ else if (b === 25) {
697
+ valid(n >= 8 && (n - 6) % 2 === 0);
698
+ for (let i = 0; i < n - 6; i += 2)
699
+ line(v[i], v[i + 1]);
700
+ curve(...v.slice(n - 6));
701
+ }
702
+ else if (b === 26 || b === 27) {
703
+ valid(n >= 4 && (n % 4 === 0 || n % 4 === 1));
704
+ let i = n % 4, extra = i ? v[0] : 0;
705
+ for (; i < n; i += 4) {
706
+ if (b === 27)
707
+ curve(v[i], extra, v[i + 1], v[i + 2], v[i + 3], 0);
708
+ else
709
+ curve(extra, v[i], v[i + 1], v[i + 2], 0, v[i + 3]);
710
+ extra = 0;
711
+ }
712
+ }
713
+ else if (b === 30 || b === 31) {
714
+ valid(n >= 4 && (n % 4 === 0 || n % 4 === 1));
715
+ let horizontal = b === 31, i = 0;
716
+ while (i < n) {
717
+ const last = n - i === 5, extra = last ? v[i + 4] : 0;
718
+ if (horizontal)
719
+ curve(v[i], 0, v[i + 1], v[i + 2], extra, v[i + 3]);
720
+ else
721
+ curve(0, v[i], v[i + 1], v[i + 2], v[i + 3], extra);
722
+ i += last ? 5 : 4;
723
+ horizontal = !horizontal;
724
+ }
725
+ }
726
+ else
727
+ fontError(`Type 2 operator ${b} is unknown`, 'unsupported');
728
+ }
729
+ if (stack.length > (cff2 ? 513 : 48) ||
730
+ stack.some((v) => !Number.isFinite(v)) ||
731
+ !Number.isFinite(x) ||
732
+ !Number.isFinite(y))
733
+ fontError(`a Type 2 operand is not finite, or the stack exceeds ${cff2 ? 513 : 48}`);
734
+ }
735
+ if (cff2) {
736
+ if (!depth) {
737
+ if (stack.length)
738
+ fontError('a CFF2 charstring ends with operands and no operator');
739
+ close();
740
+ ended = true;
741
+ }
742
+ }
743
+ else if (!ended)
744
+ fontError(depth ? 'a Type 2 subroutine has no return' : 'a Type 2 charstring has no endchar');
745
+ };
746
+ execute(charString, 0);
747
+ return out;
748
+ }
749
+ }