@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 1.0.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 (172) hide show
  1. package/README.md +1 -1
  2. package/package.json +42 -26
  3. package/{src/display → pdfjs/dist}/display_utils.js +344 -139
  4. package/{src/display → pdfjs/dist}/fetch_stream.js +115 -97
  5. package/pdfjs/dist/l10n_utils.js +140 -0
  6. package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
  7. package/{src/display → pdfjs/dist}/network.js +149 -87
  8. package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
  9. package/{src/display → pdfjs/dist}/node_stream.js +133 -98
  10. package/pdfjs/dist/pdf.js +12778 -0
  11. package/pdfjs/dist/pdf_link_service.js +638 -0
  12. package/pdfjs/dist/pdf_rendering_queue.js +199 -0
  13. package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
  14. package/pdfjs/dist/pdf_viewer.js +3598 -0
  15. package/pdfjs/dist/ui_utils.js +1033 -0
  16. package/{src/shared → pdfjs/dist}/util.js +301 -287
  17. package/pdfjs/dist/worker.js +62813 -0
  18. package/src/vcf-pdf-viewer.js +98 -46
  19. package/theme/lumo/vcf-pdf-viewer-styles.js +4 -4
  20. package/theme/material/vcf-pdf-viewer-styles.js +4 -4
  21. package/theme/material/vcf-pdf-viewer.js +2 -2
  22. package/src/core/.eslintrc +0 -13
  23. package/src/core/annotation.js +0 -2948
  24. package/src/core/arithmetic_decoder.js +0 -182
  25. package/src/core/ascii_85_stream.js +0 -98
  26. package/src/core/ascii_hex_stream.js +0 -79
  27. package/src/core/base_stream.js +0 -110
  28. package/src/core/bidi.js +0 -438
  29. package/src/core/calibri_factors.js +0 -308
  30. package/src/core/catalog.js +0 -1459
  31. package/src/core/ccitt.js +0 -1062
  32. package/src/core/ccitt_stream.js +0 -60
  33. package/src/core/cff_font.js +0 -116
  34. package/src/core/cff_parser.js +0 -1949
  35. package/src/core/charsets.js +0 -119
  36. package/src/core/chunked_stream.js +0 -557
  37. package/src/core/cmap.js +0 -1039
  38. package/src/core/colorspace.js +0 -1533
  39. package/src/core/core_utils.js +0 -464
  40. package/src/core/crypto.js +0 -1900
  41. package/src/core/decode_stream.js +0 -170
  42. package/src/core/decrypt_stream.js +0 -59
  43. package/src/core/default_appearance.js +0 -99
  44. package/src/core/document.js +0 -1456
  45. package/src/core/encodings.js +0 -301
  46. package/src/core/evaluator.js +0 -4601
  47. package/src/core/file_spec.js +0 -108
  48. package/src/core/flate_stream.js +0 -402
  49. package/src/core/font_renderer.js +0 -882
  50. package/src/core/fonts.js +0 -3260
  51. package/src/core/fonts_utils.js +0 -221
  52. package/src/core/function.js +0 -1257
  53. package/src/core/glyf.js +0 -706
  54. package/src/core/glyphlist.js +0 -4558
  55. package/src/core/helvetica_factors.js +0 -353
  56. package/src/core/image.js +0 -802
  57. package/src/core/image_utils.js +0 -291
  58. package/src/core/jbig2.js +0 -2572
  59. package/src/core/jbig2_stream.js +0 -73
  60. package/src/core/jpeg_stream.js +0 -105
  61. package/src/core/jpg.js +0 -1416
  62. package/src/core/jpx.js +0 -2343
  63. package/src/core/jpx_stream.js +0 -87
  64. package/src/core/liberationsans_widths.js +0 -221
  65. package/src/core/lzw_stream.js +0 -150
  66. package/src/core/metadata_parser.js +0 -146
  67. package/src/core/metrics.js +0 -2970
  68. package/src/core/murmurhash3.js +0 -139
  69. package/src/core/myriadpro_factors.js +0 -290
  70. package/src/core/name_number_tree.js +0 -153
  71. package/src/core/object_loader.js +0 -149
  72. package/src/core/opentype_file_builder.js +0 -154
  73. package/src/core/operator_list.js +0 -734
  74. package/src/core/parser.js +0 -1416
  75. package/src/core/pattern.js +0 -985
  76. package/src/core/pdf_manager.js +0 -217
  77. package/src/core/predictor_stream.js +0 -238
  78. package/src/core/primitives.js +0 -402
  79. package/src/core/ps_parser.js +0 -272
  80. package/src/core/run_length_stream.js +0 -61
  81. package/src/core/segoeui_factors.js +0 -308
  82. package/src/core/standard_fonts.js +0 -817
  83. package/src/core/stream.js +0 -103
  84. package/src/core/struct_tree.js +0 -335
  85. package/src/core/to_unicode_map.js +0 -103
  86. package/src/core/type1_font.js +0 -421
  87. package/src/core/type1_parser.js +0 -776
  88. package/src/core/unicode.js +0 -1649
  89. package/src/core/worker.js +0 -848
  90. package/src/core/worker_stream.js +0 -135
  91. package/src/core/writer.js +0 -278
  92. package/src/core/xfa/bind.js +0 -652
  93. package/src/core/xfa/builder.js +0 -207
  94. package/src/core/xfa/config.js +0 -1926
  95. package/src/core/xfa/connection_set.js +0 -202
  96. package/src/core/xfa/data.js +0 -82
  97. package/src/core/xfa/datasets.js +0 -76
  98. package/src/core/xfa/factory.js +0 -111
  99. package/src/core/xfa/fonts.js +0 -181
  100. package/src/core/xfa/formcalc_lexer.js +0 -385
  101. package/src/core/xfa/formcalc_parser.js +0 -1340
  102. package/src/core/xfa/html_utils.js +0 -639
  103. package/src/core/xfa/layout.js +0 -383
  104. package/src/core/xfa/locale_set.js +0 -345
  105. package/src/core/xfa/namespaces.js +0 -81
  106. package/src/core/xfa/parser.js +0 -184
  107. package/src/core/xfa/setup.js +0 -38
  108. package/src/core/xfa/signature.js +0 -40
  109. package/src/core/xfa/som.js +0 -338
  110. package/src/core/xfa/stylesheet.js +0 -40
  111. package/src/core/xfa/template.js +0 -6260
  112. package/src/core/xfa/text.js +0 -290
  113. package/src/core/xfa/unknown.js +0 -29
  114. package/src/core/xfa/utils.js +0 -217
  115. package/src/core/xfa/xdp.js +0 -59
  116. package/src/core/xfa/xfa_object.js +0 -1130
  117. package/src/core/xfa/xhtml.js +0 -543
  118. package/src/core/xfa_fonts.js +0 -208
  119. package/src/core/xml_parser.js +0 -507
  120. package/src/core/xref.js +0 -899
  121. package/src/display/annotation_layer.js +0 -2107
  122. package/src/display/annotation_storage.js +0 -113
  123. package/src/display/api.js +0 -3292
  124. package/src/display/base_factory.js +0 -180
  125. package/src/display/canvas.js +0 -2828
  126. package/src/display/font_loader.js +0 -484
  127. package/src/display/metadata.js +0 -41
  128. package/src/display/network_utils.js +0 -100
  129. package/src/display/node_utils.js +0 -83
  130. package/src/display/optional_content_config.js +0 -189
  131. package/src/display/pattern_helper.js +0 -659
  132. package/src/display/svg.js +0 -1709
  133. package/src/display/text_layer.js +0 -847
  134. package/src/display/transport_stream.js +0 -303
  135. package/src/display/worker_options.js +0 -40
  136. package/src/display/xfa_layer.js +0 -204
  137. package/src/doc_helper.js +0 -25
  138. package/src/images/logo.svg +0 -41
  139. package/src/interfaces.js +0 -169
  140. package/src/license_header.js +0 -14
  141. package/src/license_header_libre.js +0 -21
  142. package/src/pdf.image_decoders.js +0 -46
  143. package/src/pdf.js +0 -146
  144. package/src/pdf.sandbox.external.js +0 -181
  145. package/src/pdf.sandbox.js +0 -151
  146. package/src/pdf.scripting.js +0 -25
  147. package/src/pdf.worker.entry.js +0 -19
  148. package/src/pdf.worker.js +0 -23
  149. package/src/scripting_api/aform.js +0 -608
  150. package/src/scripting_api/app.js +0 -621
  151. package/src/scripting_api/color.js +0 -129
  152. package/src/scripting_api/common.js +0 -58
  153. package/src/scripting_api/console.js +0 -38
  154. package/src/scripting_api/constants.js +0 -208
  155. package/src/scripting_api/doc.js +0 -1195
  156. package/src/scripting_api/error.js +0 -23
  157. package/src/scripting_api/event.js +0 -232
  158. package/src/scripting_api/field.js +0 -620
  159. package/src/scripting_api/fullscreen.js +0 -145
  160. package/src/scripting_api/initialization.js +0 -223
  161. package/src/scripting_api/pdf_object.js +0 -24
  162. package/src/scripting_api/print_params.js +0 -146
  163. package/src/scripting_api/proxy.js +0 -139
  164. package/src/scripting_api/thermometer.js +0 -69
  165. package/src/scripting_api/util.js +0 -581
  166. package/src/shared/.eslintrc +0 -13
  167. package/src/shared/cffStandardStrings.js +0 -311
  168. package/src/shared/compatibility.js +0 -114
  169. package/src/shared/fonts_utils.js +0 -429
  170. package/src/shared/is_node.js +0 -27
  171. package/src/shared/scripting_utils.js +0 -85
  172. package/src/worker_loader.js +0 -32
package/src/core/glyf.js DELETED
@@ -1,706 +0,0 @@
1
- /* Copyright 2021 Mozilla Foundation
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- const ON_CURVE_POINT = 1 << 0;
17
- const X_SHORT_VECTOR = 1 << 1;
18
- const Y_SHORT_VECTOR = 1 << 2;
19
- const REPEAT_FLAG = 1 << 3;
20
- const X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR = 1 << 4;
21
- const Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR = 1 << 5;
22
- const OVERLAP_SIMPLE = 1 << 6;
23
-
24
- const ARG_1_AND_2_ARE_WORDS = 1 << 0;
25
- const ARGS_ARE_XY_VALUES = 1 << 1;
26
- // const ROUND_XY_TO_GRID = 1 << 2;
27
- const WE_HAVE_A_SCALE = 1 << 3;
28
- const MORE_COMPONENTS = 1 << 5;
29
- const WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
30
- const WE_HAVE_A_TWO_BY_TWO = 1 << 7;
31
- const WE_HAVE_INSTRUCTIONS = 1 << 8;
32
- // const USE_MY_METRICS = 1 << 9;
33
- // const OVERLAP_COMPOUND = 1 << 10;
34
- // const SCALED_COMPONENT_OFFSET = 1 << 11;
35
- // const UNSCALED_COMPONENT_OFFSET = 1 << 12;
36
-
37
- /**
38
- * GlyfTable object represents a glyf table containing glyph information:
39
- * - glyph header (xMin, yMin, xMax, yMax);
40
- * - contours if any;
41
- * - components if the glyph is a composite.
42
- *
43
- * It's possible to re-scale each glyph in order to have a new font which
44
- * exactly fits an other one: the goal is to be able to build some substitution
45
- * font for well-known fonts (Myriad, Arial, ...).
46
- *
47
- * A full description of glyf table can be found here
48
- * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6glyf.html
49
- */
50
- class GlyfTable {
51
- constructor({ glyfTable, isGlyphLocationsLong, locaTable, numGlyphs }) {
52
- this.glyphs = [];
53
- const loca = new DataView(
54
- locaTable.buffer,
55
- locaTable.byteOffset,
56
- locaTable.byteLength
57
- );
58
- const glyf = new DataView(
59
- glyfTable.buffer,
60
- glyfTable.byteOffset,
61
- glyfTable.byteLength
62
- );
63
- const offsetSize = isGlyphLocationsLong ? 4 : 2;
64
- let prev = isGlyphLocationsLong ? loca.getUint32(0) : 2 * loca.getUint16(0);
65
- let pos = 0;
66
- for (let i = 0; i < numGlyphs; i++) {
67
- pos += offsetSize;
68
- const next = isGlyphLocationsLong
69
- ? loca.getUint32(pos)
70
- : 2 * loca.getUint16(pos);
71
- if (next === prev) {
72
- this.glyphs.push(new Glyph({}));
73
- continue;
74
- }
75
-
76
- const glyph = Glyph.parse(prev, glyf);
77
- this.glyphs.push(glyph);
78
-
79
- prev = next;
80
- }
81
- }
82
-
83
- getSize() {
84
- return this.glyphs.reduce((a, g) => {
85
- const size = g.getSize();
86
- // Round to next multiple of 4 if needed.
87
- return a + ((size + 3) & ~3);
88
- }, 0);
89
- }
90
-
91
- write() {
92
- const totalSize = this.getSize();
93
- const glyfTable = new DataView(new ArrayBuffer(totalSize));
94
- const isLocationLong = totalSize > /* 0xffff * 2 */ 0x1fffe;
95
- const offsetSize = isLocationLong ? 4 : 2;
96
- const locaTable = new DataView(
97
- new ArrayBuffer((this.glyphs.length + 1) * offsetSize)
98
- );
99
-
100
- if (isLocationLong) {
101
- locaTable.setUint32(0, 0);
102
- } else {
103
- locaTable.setUint16(0, 0);
104
- }
105
-
106
- let pos = 0;
107
- let locaIndex = 0;
108
- for (const glyph of this.glyphs) {
109
- pos += glyph.write(pos, glyfTable);
110
- // Round to next multiple of 4 if needed.
111
- pos = (pos + 3) & ~3;
112
-
113
- locaIndex += offsetSize;
114
- if (isLocationLong) {
115
- locaTable.setUint32(locaIndex, pos);
116
- } else {
117
- locaTable.setUint16(locaIndex, pos >> 1);
118
- }
119
- }
120
-
121
- return {
122
- isLocationLong,
123
- loca: new Uint8Array(locaTable.buffer),
124
- glyf: new Uint8Array(glyfTable.buffer),
125
- };
126
- }
127
-
128
- scale(factors) {
129
- for (let i = 0, ii = this.glyphs.length; i < ii; i++) {
130
- this.glyphs[i].scale(factors[i]);
131
- }
132
- }
133
- }
134
-
135
- class Glyph {
136
- constructor({ header = null, simple = null, composites = null }) {
137
- this.header = header;
138
- this.simple = simple;
139
- this.composites = composites;
140
- }
141
-
142
- static parse(pos, glyf) {
143
- const [read, header] = GlyphHeader.parse(pos, glyf);
144
- pos += read;
145
-
146
- if (header.numberOfContours < 0) {
147
- // Composite glyph.
148
- const composites = [];
149
- while (true) {
150
- const [n, composite] = CompositeGlyph.parse(pos, glyf);
151
- pos += n;
152
- composites.push(composite);
153
- if (!(composite.flags & MORE_COMPONENTS)) {
154
- break;
155
- }
156
- }
157
-
158
- return new Glyph({ header, composites });
159
- }
160
-
161
- const simple = SimpleGlyph.parse(pos, glyf, header.numberOfContours);
162
-
163
- return new Glyph({ header, simple });
164
- }
165
-
166
- getSize() {
167
- if (!this.header) {
168
- return 0;
169
- }
170
- const size = this.simple
171
- ? this.simple.getSize()
172
- : this.composites.reduce((a, c) => a + c.getSize(), 0);
173
- return this.header.getSize() + size;
174
- }
175
-
176
- write(pos, buf) {
177
- if (!this.header) {
178
- return 0;
179
- }
180
-
181
- const spos = pos;
182
- pos += this.header.write(pos, buf);
183
- if (this.simple) {
184
- pos += this.simple.write(pos, buf);
185
- } else {
186
- for (const composite of this.composites) {
187
- pos += composite.write(pos, buf);
188
- }
189
- }
190
-
191
- return pos - spos;
192
- }
193
-
194
- scale(factor) {
195
- if (!this.header) {
196
- return;
197
- }
198
-
199
- const xMiddle = (this.header.xMin + this.header.xMax) / 2;
200
- this.header.scale(xMiddle, factor);
201
- if (this.simple) {
202
- this.simple.scale(xMiddle, factor);
203
- } else {
204
- for (const composite of this.composites) {
205
- composite.scale(xMiddle, factor);
206
- }
207
- }
208
- }
209
- }
210
-
211
- class GlyphHeader {
212
- constructor({ numberOfContours, xMin, yMin, xMax, yMax }) {
213
- this.numberOfContours = numberOfContours;
214
- this.xMin = xMin;
215
- this.yMin = yMin;
216
- this.xMax = xMax;
217
- this.yMax = yMax;
218
- }
219
-
220
- static parse(pos, glyf) {
221
- return [
222
- 10,
223
- new GlyphHeader({
224
- numberOfContours: glyf.getInt16(pos),
225
- xMin: glyf.getInt16(pos + 2),
226
- yMin: glyf.getInt16(pos + 4),
227
- xMax: glyf.getInt16(pos + 6),
228
- yMax: glyf.getInt16(pos + 8),
229
- }),
230
- ];
231
- }
232
-
233
- getSize() {
234
- return 10;
235
- }
236
-
237
- write(pos, buf) {
238
- buf.setInt16(pos, this.numberOfContours);
239
- buf.setInt16(pos + 2, this.xMin);
240
- buf.setInt16(pos + 4, this.yMin);
241
- buf.setInt16(pos + 6, this.xMax);
242
- buf.setInt16(pos + 8, this.yMax);
243
-
244
- return 10;
245
- }
246
-
247
- scale(x, factor) {
248
- this.xMin = Math.round(x + (this.xMin - x) * factor);
249
- this.xMax = Math.round(x + (this.xMax - x) * factor);
250
- }
251
- }
252
-
253
- class Contour {
254
- constructor({ flags, xCoordinates, yCoordinates }) {
255
- this.xCoordinates = xCoordinates;
256
- this.yCoordinates = yCoordinates;
257
- this.flags = flags;
258
- }
259
- }
260
-
261
- class SimpleGlyph {
262
- constructor({ contours, instructions }) {
263
- this.contours = contours;
264
- this.instructions = instructions;
265
- }
266
-
267
- static parse(pos, glyf, numberOfContours) {
268
- const endPtsOfContours = [];
269
- for (let i = 0; i < numberOfContours; i++) {
270
- const endPt = glyf.getUint16(pos);
271
- pos += 2;
272
- endPtsOfContours.push(endPt);
273
- }
274
- const numberOfPt = endPtsOfContours[numberOfContours - 1] + 1;
275
- const instructionLength = glyf.getUint16(pos);
276
- pos += 2;
277
- const instructions = new Uint8Array(glyf).slice(
278
- pos,
279
- pos + instructionLength
280
- );
281
- pos += instructionLength;
282
-
283
- const flags = [];
284
- for (let i = 0; i < numberOfPt; pos++, i++) {
285
- let flag = glyf.getUint8(pos);
286
- flags.push(flag);
287
- if (flag & REPEAT_FLAG) {
288
- const count = glyf.getUint8(++pos);
289
- flag ^= REPEAT_FLAG;
290
- for (let m = 0; m < count; m++) {
291
- flags.push(flag);
292
- }
293
- i += count;
294
- }
295
- }
296
-
297
- const allXCoordinates = [];
298
- let xCoordinates = [];
299
- let yCoordinates = [];
300
- let pointFlags = [];
301
- const contours = [];
302
- let endPtsOfContoursIndex = 0;
303
- let lastCoordinate = 0;
304
-
305
- // Get x coordinates.
306
- for (let i = 0; i < numberOfPt; i++) {
307
- const flag = flags[i];
308
- if (flag & X_SHORT_VECTOR) {
309
- // 8-bits unsigned value.
310
- const x = glyf.getUint8(pos++);
311
- lastCoordinate += flag & X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR ? x : -x;
312
- xCoordinates.push(lastCoordinate);
313
- } else if (flag & X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR) {
314
- // IS_SAME.
315
- xCoordinates.push(lastCoordinate);
316
- } else {
317
- lastCoordinate += glyf.getInt16(pos);
318
- pos += 2;
319
- xCoordinates.push(lastCoordinate);
320
- }
321
-
322
- if (endPtsOfContours[endPtsOfContoursIndex] === i) {
323
- // Next entry is the first one of a new contour.
324
- endPtsOfContoursIndex++;
325
- allXCoordinates.push(xCoordinates);
326
- xCoordinates = [];
327
- }
328
- }
329
-
330
- lastCoordinate = 0;
331
- endPtsOfContoursIndex = 0;
332
- for (let i = 0; i < numberOfPt; i++) {
333
- const flag = flags[i];
334
- if (flag & Y_SHORT_VECTOR) {
335
- // 8-bits unsigned value.
336
- const y = glyf.getUint8(pos++);
337
- lastCoordinate += flag & Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR ? y : -y;
338
- yCoordinates.push(lastCoordinate);
339
- } else if (flag & Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR) {
340
- // IS_SAME.
341
- yCoordinates.push(lastCoordinate);
342
- } else {
343
- lastCoordinate += glyf.getInt16(pos);
344
- pos += 2;
345
- yCoordinates.push(lastCoordinate);
346
- }
347
-
348
- pointFlags.push((flag & ON_CURVE_POINT) | (flag & OVERLAP_SIMPLE));
349
-
350
- if (endPtsOfContours[endPtsOfContoursIndex] === i) {
351
- // Next entry is the first one of a new contour.
352
- xCoordinates = allXCoordinates[endPtsOfContoursIndex];
353
- endPtsOfContoursIndex++;
354
- contours.push(
355
- new Contour({
356
- flags: pointFlags,
357
- xCoordinates,
358
- yCoordinates,
359
- })
360
- );
361
- yCoordinates = [];
362
- pointFlags = [];
363
- }
364
- }
365
-
366
- return new SimpleGlyph({
367
- contours,
368
- instructions,
369
- });
370
- }
371
-
372
- getSize() {
373
- let size = this.contours.length * 2 + 2 + this.instructions.length;
374
- let lastX = 0;
375
- let lastY = 0;
376
- for (const contour of this.contours) {
377
- size += contour.flags.length;
378
- for (let i = 0, ii = contour.xCoordinates.length; i < ii; i++) {
379
- const x = contour.xCoordinates[i];
380
- const y = contour.yCoordinates[i];
381
- let abs = Math.abs(x - lastX);
382
- if (abs > 255) {
383
- size += 2;
384
- } else if (abs > 0) {
385
- size += 1;
386
- }
387
- lastX = x;
388
-
389
- abs = Math.abs(y - lastY);
390
- if (abs > 255) {
391
- size += 2;
392
- } else if (abs > 0) {
393
- size += 1;
394
- }
395
- lastY = y;
396
- }
397
- }
398
- return size;
399
- }
400
-
401
- write(pos, buf) {
402
- const spos = pos;
403
- const xCoordinates = [];
404
- const yCoordinates = [];
405
- const flags = [];
406
- let lastX = 0;
407
- let lastY = 0;
408
-
409
- for (const contour of this.contours) {
410
- for (let i = 0, ii = contour.xCoordinates.length; i < ii; i++) {
411
- let flag = contour.flags[i];
412
- const x = contour.xCoordinates[i];
413
- let delta = x - lastX;
414
- if (delta === 0) {
415
- flag |= X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR;
416
- xCoordinates.push(0);
417
- } else {
418
- const abs = Math.abs(delta);
419
- if (abs <= 255) {
420
- flag |=
421
- delta >= 0
422
- ? X_SHORT_VECTOR | X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR
423
- : X_SHORT_VECTOR;
424
- xCoordinates.push(abs);
425
- } else {
426
- xCoordinates.push(delta);
427
- }
428
- }
429
- lastX = x;
430
-
431
- const y = contour.yCoordinates[i];
432
- delta = y - lastY;
433
- if (delta === 0) {
434
- flag |= Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR;
435
- yCoordinates.push(0);
436
- } else {
437
- const abs = Math.abs(delta);
438
- if (abs <= 255) {
439
- flag |=
440
- delta >= 0
441
- ? Y_SHORT_VECTOR | Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR
442
- : Y_SHORT_VECTOR;
443
- yCoordinates.push(abs);
444
- } else {
445
- yCoordinates.push(delta);
446
- }
447
- }
448
- lastY = y;
449
-
450
- flags.push(flag);
451
- }
452
-
453
- // Write endPtsOfContours entry.
454
- buf.setUint16(pos, xCoordinates.length - 1);
455
- pos += 2;
456
- }
457
-
458
- // Write instructionLength.
459
- buf.setUint16(pos, this.instructions.length);
460
- pos += 2;
461
- if (this.instructions.length) {
462
- // Write instructions.
463
- new Uint8Array(buf.buffer, 0, buf.buffer.byteLength).set(
464
- this.instructions,
465
- pos
466
- );
467
- pos += this.instructions.length;
468
- }
469
-
470
- // Write flags.
471
- for (const flag of flags) {
472
- buf.setUint8(pos++, flag);
473
- }
474
-
475
- // Write xCoordinates.
476
- for (let i = 0, ii = xCoordinates.length; i < ii; i++) {
477
- const x = xCoordinates[i];
478
- const flag = flags[i];
479
- if (flag & X_SHORT_VECTOR) {
480
- buf.setUint8(pos++, x);
481
- } else if (!(flag & X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR)) {
482
- buf.setInt16(pos, x);
483
- pos += 2;
484
- }
485
- }
486
-
487
- // Write yCoordinates.
488
- for (let i = 0, ii = yCoordinates.length; i < ii; i++) {
489
- const y = yCoordinates[i];
490
- const flag = flags[i];
491
- if (flag & Y_SHORT_VECTOR) {
492
- buf.setUint8(pos++, y);
493
- } else if (!(flag & Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR)) {
494
- buf.setInt16(pos, y);
495
- pos += 2;
496
- }
497
- }
498
-
499
- return pos - spos;
500
- }
501
-
502
- scale(x, factor) {
503
- for (const contour of this.contours) {
504
- if (contour.xCoordinates.length === 0) {
505
- continue;
506
- }
507
-
508
- for (let i = 0, ii = contour.xCoordinates.length; i < ii; i++) {
509
- contour.xCoordinates[i] = Math.round(
510
- x + (contour.xCoordinates[i] - x) * factor
511
- );
512
- }
513
- }
514
- }
515
- }
516
-
517
- class CompositeGlyph {
518
- constructor({
519
- flags,
520
- glyphIndex,
521
- argument1,
522
- argument2,
523
- transf,
524
- instructions,
525
- }) {
526
- this.flags = flags;
527
- this.glyphIndex = glyphIndex;
528
- this.argument1 = argument1;
529
- this.argument2 = argument2;
530
- this.transf = transf;
531
- this.instructions = instructions;
532
- }
533
-
534
- static parse(pos, glyf) {
535
- const spos = pos;
536
- const transf = [];
537
- let flags = glyf.getUint16(pos);
538
- const glyphIndex = glyf.getUint16(pos + 2);
539
- pos += 4;
540
-
541
- let argument1, argument2;
542
- if (flags & ARG_1_AND_2_ARE_WORDS) {
543
- if (flags & ARGS_ARE_XY_VALUES) {
544
- argument1 = glyf.getInt16(pos);
545
- argument2 = glyf.getInt16(pos + 2);
546
- } else {
547
- argument1 = glyf.getUint16(pos);
548
- argument2 = glyf.getUint16(pos + 2);
549
- }
550
- pos += 4;
551
- flags ^= ARG_1_AND_2_ARE_WORDS;
552
- } else {
553
- argument1 = glyf.getUint8(pos);
554
- argument2 = glyf.getUint8(pos + 1);
555
- if (flags & ARGS_ARE_XY_VALUES) {
556
- const abs1 = argument1 & 0x7f;
557
- argument1 = argument1 & 0x80 ? -abs1 : abs1;
558
-
559
- const abs2 = argument2 & 0x7f;
560
- argument2 = argument2 & 0x80 ? -abs2 : abs2;
561
- }
562
- pos += 2;
563
- }
564
-
565
- if (flags & WE_HAVE_A_SCALE) {
566
- // Single F2.14.
567
- transf.push(glyf.getUint16(pos));
568
- pos += 2;
569
- } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
570
- // Two F2.14.
571
- transf.push(glyf.getUint16(pos), glyf.getUint16(pos + 2));
572
- pos += 4;
573
- } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
574
- // Four F2.14.
575
- transf.push(
576
- glyf.getUint16(pos),
577
- glyf.getUint16(pos + 2),
578
- glyf.getUint16(pos + 4),
579
- glyf.getUint16(pos + 6)
580
- );
581
- pos += 8;
582
- }
583
-
584
- let instructions = null;
585
- if (flags & WE_HAVE_INSTRUCTIONS) {
586
- const instructionLength = glyf.getUint16(pos);
587
- pos += 2;
588
- instructions = new Uint8Array(glyf).slice(pos, pos + instructionLength);
589
- pos += instructionLength;
590
- }
591
-
592
- return [
593
- pos - spos,
594
- new CompositeGlyph({
595
- flags,
596
- glyphIndex,
597
- argument1,
598
- argument2,
599
- transf,
600
- instructions,
601
- }),
602
- ];
603
- }
604
-
605
- getSize() {
606
- let size = 2 + 2 + this.transf.length * 2;
607
- if (this.flags & WE_HAVE_INSTRUCTIONS) {
608
- size += 2 + this.instructions.length;
609
- }
610
-
611
- size += 2;
612
- if (this.flags & 2) {
613
- // Arguments are signed.
614
- if (
615
- !(
616
- this.argument1 >= -128 &&
617
- this.argument1 <= 127 &&
618
- this.argument2 >= -128 &&
619
- this.argument2 <= 127
620
- )
621
- ) {
622
- size += 2;
623
- }
624
- } else {
625
- if (
626
- !(
627
- this.argument1 >= 0 &&
628
- this.argument1 <= 255 &&
629
- this.argument2 >= 0 &&
630
- this.argument2 <= 255
631
- )
632
- ) {
633
- size += 2;
634
- }
635
- }
636
-
637
- return size;
638
- }
639
-
640
- write(pos, buf) {
641
- const spos = pos;
642
-
643
- if (this.flags & ARGS_ARE_XY_VALUES) {
644
- // Arguments are signed.
645
- if (
646
- !(
647
- this.argument1 >= -128 &&
648
- this.argument1 <= 127 &&
649
- this.argument2 >= -128 &&
650
- this.argument2 <= 127
651
- )
652
- ) {
653
- this.flags |= ARG_1_AND_2_ARE_WORDS;
654
- }
655
- } else {
656
- if (
657
- !(
658
- this.argument1 >= 0 &&
659
- this.argument1 <= 255 &&
660
- this.argument2 >= 0 &&
661
- this.argument2 <= 255
662
- )
663
- ) {
664
- this.flags |= ARG_1_AND_2_ARE_WORDS;
665
- }
666
- }
667
-
668
- buf.setUint16(pos, this.flags);
669
- buf.setUint16(pos + 2, this.glyphIndex);
670
- pos += 4;
671
-
672
- if (this.flags & ARG_1_AND_2_ARE_WORDS) {
673
- if (this.flags & ARGS_ARE_XY_VALUES) {
674
- buf.setInt16(pos, this.argument1);
675
- buf.setInt16(pos + 2, this.argument2);
676
- } else {
677
- buf.setUint16(pos, this.argument1);
678
- buf.setUint16(pos + 2, this.argument2);
679
- }
680
- pos += 4;
681
- } else {
682
- buf.setUint8(pos, this.argument1);
683
- buf.setUint8(pos + 1, this.argument2);
684
- pos += 2;
685
- }
686
-
687
- if (this.flags & WE_HAVE_INSTRUCTIONS) {
688
- buf.setUint16(pos, this.instructions.length);
689
- pos += 2;
690
- // Write instructions.
691
- if (this.instructions.length) {
692
- new Uint8Array(buf.buffer, 0, buf.buffer.byteLength).set(
693
- this.instructions,
694
- pos
695
- );
696
- pos += this.instructions.length;
697
- }
698
- }
699
-
700
- return pos - spos;
701
- }
702
-
703
- scale(x, factor) {}
704
- }
705
-
706
- export { GlyfTable };