pdf-codec.js 5.2.26 → 5.2.28

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 (221) hide show
  1. package/dist/{afm-widths-BYiX2L9D.cjs → afm-widths-BHD2HZIL.cjs} +553 -642
  2. package/dist/{afm-widths-blUDDjlI.js → afm-widths-B_IGbGky.js} +553 -642
  3. package/dist/afm-widths.cjs +1 -1
  4. package/dist/afm-widths.js +1 -1
  5. package/dist/annotations.cjs +5 -3
  6. package/dist/annotations.d.cts +2 -2
  7. package/dist/annotations.d.ts +2 -2
  8. package/dist/annotations.js +5 -3
  9. package/dist/attachments.d.cts +2 -2
  10. package/dist/attachments.d.ts +2 -2
  11. package/dist/builtin-encoding.cjs +17 -6
  12. package/dist/builtin-encoding.js +17 -6
  13. package/dist/bytes/crc32.cjs +5 -3
  14. package/dist/bytes/crc32.js +5 -3
  15. package/dist/bytes/flate.cjs +0 -1
  16. package/dist/bytes/flate.js +0 -1
  17. package/dist/cff-bounds.cjs +73 -45
  18. package/dist/cff-bounds.js +73 -45
  19. package/dist/cff.cjs +13 -6
  20. package/dist/cff.js +13 -6
  21. package/dist/cmap-table.cjs +54 -26
  22. package/dist/cmap-table.js +54 -26
  23. package/dist/cmap.cjs +5 -3
  24. package/dist/cmap.js +5 -3
  25. package/dist/content-read.d.cts +1 -1
  26. package/dist/content-read.d.ts +1 -1
  27. package/dist/content-write.cjs +21 -14
  28. package/dist/content-write.d.cts +2 -2
  29. package/dist/content-write.d.ts +2 -2
  30. package/dist/content-write.js +21 -14
  31. package/dist/crypto/aes.cjs +76 -55
  32. package/dist/crypto/aes.js +76 -55
  33. package/dist/crypto/md5.cjs +345 -149
  34. package/dist/crypto/md5.js +345 -149
  35. package/dist/crypto/rc4.cjs +5 -4
  36. package/dist/crypto/rc4.js +5 -4
  37. package/dist/crypto/sha2.cjs +92 -43
  38. package/dist/crypto/sha2.js +92 -43
  39. package/dist/document.cjs +17 -11
  40. package/dist/document.d.cts +1 -1
  41. package/dist/document.d.ts +1 -1
  42. package/dist/document.js +17 -11
  43. package/dist/{embedded-font-Bnhh4juV.d.ts → embedded-font-ByD7ZiQS.d.ts} +1 -1
  44. package/dist/{embedded-font-CEf-zBCA.d.cts → embedded-font-DgVBedw6.d.cts} +1 -1
  45. package/dist/embedded-font-write.d.cts +2 -2
  46. package/dist/embedded-font-write.d.ts +2 -2
  47. package/dist/embedded-font.cjs +4 -2
  48. package/dist/embedded-font.d.cts +1 -1
  49. package/dist/embedded-font.d.ts +1 -1
  50. package/dist/embedded-font.js +4 -2
  51. package/dist/encoding.cjs +1 -1
  52. package/dist/encoding.js +1 -1
  53. package/dist/encrypt-write.cjs +25 -21
  54. package/dist/encrypt-write.d.cts +1 -1
  55. package/dist/encrypt-write.d.ts +1 -1
  56. package/dist/encrypt-write.js +25 -21
  57. package/dist/encrypt.cjs +43 -64
  58. package/dist/encrypt.d.cts +1 -1
  59. package/dist/encrypt.d.ts +1 -1
  60. package/dist/encrypt.js +43 -64
  61. package/dist/filters.cjs +46 -26
  62. package/dist/filters.d.cts +1 -1
  63. package/dist/filters.d.ts +1 -1
  64. package/dist/filters.js +46 -26
  65. package/dist/font-face.cjs +2 -1
  66. package/dist/font-face.js +2 -1
  67. package/dist/font-read.cjs +12 -6
  68. package/dist/font-read.d.cts +1 -1
  69. package/dist/font-read.d.ts +1 -1
  70. package/dist/font-read.js +12 -6
  71. package/dist/font-registry.d.cts +3 -3
  72. package/dist/font-registry.d.ts +3 -3
  73. package/dist/font-tables.cjs +55 -28
  74. package/dist/font-tables.js +55 -28
  75. package/dist/form.cjs +13 -8
  76. package/dist/form.d.cts +2 -2
  77. package/dist/form.d.ts +2 -2
  78. package/dist/form.js +13 -8
  79. package/dist/gdef-table.cjs +6 -3
  80. package/dist/gdef-table.js +6 -3
  81. package/dist/{glyf-contours-C0epBNqP.d.ts → glyf-contours-B1PsI9oA.d.ts} +1 -1
  82. package/dist/{glyf-contours-BRpvd0rt.d.cts → glyf-contours-NHBpgks-.d.cts} +1 -1
  83. package/dist/glyf-contours.cjs +3 -1
  84. package/dist/glyf-contours.d.cts +1 -1
  85. package/dist/glyf-contours.d.ts +1 -1
  86. package/dist/glyf-contours.js +3 -1
  87. package/dist/glyf.cjs +22 -11
  88. package/dist/glyf.js +22 -11
  89. package/dist/gpos-table.cjs +37 -19
  90. package/dist/gpos-table.js +37 -19
  91. package/dist/gsub-table.cjs +40 -25
  92. package/dist/gsub-table.js +40 -25
  93. package/dist/hmtx-table.cjs +5 -3
  94. package/dist/hmtx-table.js +5 -3
  95. package/dist/image/ccitt-encode.cjs +8 -5
  96. package/dist/image/ccitt-encode.js +8 -5
  97. package/dist/image/ccitt.cjs +801 -209
  98. package/dist/image/ccitt.js +801 -209
  99. package/dist/image/jbig2-arith.cjs +340 -306
  100. package/dist/image/jbig2-arith.js +340 -306
  101. package/dist/image/jbig2-bitmap.cjs +8 -5
  102. package/dist/image/jbig2-bitmap.js +8 -5
  103. package/dist/image/jbig2-generic.cjs +240 -69
  104. package/dist/image/jbig2-generic.js +240 -69
  105. package/dist/image/jbig2-text.cjs +4 -2
  106. package/dist/image/jbig2-text.js +4 -2
  107. package/dist/image/jbig2.cjs +69 -31
  108. package/dist/image/jbig2.js +69 -31
  109. package/dist/image/jp2-boxes.cjs +52 -27
  110. package/dist/image/jp2-boxes.js +52 -27
  111. package/dist/image/jpeg-info.cjs +29 -24
  112. package/dist/image/jpeg-info.js +29 -24
  113. package/dist/image/jpeg2000-codestream.cjs +61 -32
  114. package/dist/image/jpeg2000-codestream.js +61 -32
  115. package/dist/image/jpeg2000-dwt.cjs +2 -1
  116. package/dist/image/jpeg2000-dwt.js +2 -1
  117. package/dist/image/jpeg2000-t1.cjs +21 -11
  118. package/dist/image/jpeg2000-t1.js +21 -11
  119. package/dist/image/jpeg2000-t2.cjs +12 -5
  120. package/dist/image/jpeg2000-t2.js +12 -5
  121. package/dist/image/jpeg2000-tagtree.cjs +8 -4
  122. package/dist/image/jpeg2000-tagtree.js +8 -4
  123. package/dist/image/jpeg2000.cjs +11 -6
  124. package/dist/image/jpeg2000.js +11 -6
  125. package/dist/image/png-decode.cjs +62 -39
  126. package/dist/image/png-decode.d.cts +3 -2
  127. package/dist/image/png-decode.d.ts +3 -2
  128. package/dist/image/png-decode.js +62 -39
  129. package/dist/image/png-filter.cjs +23 -15
  130. package/dist/image/png-filter.js +23 -15
  131. package/dist/images-read.cjs +41 -36
  132. package/dist/images-read.d.cts +2 -2
  133. package/dist/images-read.d.ts +2 -2
  134. package/dist/images-read.js +41 -36
  135. package/dist/index.cjs +1 -1
  136. package/dist/index.d.cts +1 -1
  137. package/dist/index.d.ts +1 -1
  138. package/dist/index.js +1 -1
  139. package/dist/interpret.cjs +68 -40
  140. package/dist/interpret.d.cts +1 -1
  141. package/dist/interpret.d.ts +1 -1
  142. package/dist/interpret.js +68 -40
  143. package/dist/lexer.cjs +86 -51
  144. package/dist/lexer.js +86 -51
  145. package/dist/math-content-write.cjs +9 -5
  146. package/dist/math-content-write.js +9 -5
  147. package/dist/math-font-write.cjs +2 -1
  148. package/dist/math-font-write.d.cts +1 -1
  149. package/dist/math-font-write.d.ts +1 -1
  150. package/dist/math-font-write.js +2 -1
  151. package/dist/math-font.cjs +16 -7
  152. package/dist/math-font.js +16 -7
  153. package/dist/math-table.cjs +25 -13
  154. package/dist/math-table.js +25 -13
  155. package/dist/matrix.cjs +3 -2
  156. package/dist/matrix.js +3 -2
  157. package/dist/measure.cjs +1 -1
  158. package/dist/measure.js +1 -1
  159. package/dist/names.cjs +7 -7
  160. package/dist/names.d.cts +2 -2
  161. package/dist/names.d.ts +2 -2
  162. package/dist/names.js +7 -7
  163. package/dist/navigation.cjs +16 -12
  164. package/dist/navigation.d.cts +4 -4
  165. package/dist/navigation.d.ts +4 -4
  166. package/dist/navigation.js +16 -12
  167. package/dist/{objects-B7HdMeWS.d.cts → objects-e2RS3-yT.d.cts} +3 -3
  168. package/dist/{objects-B7HdMeWS.d.ts → objects-e2RS3-yT.d.ts} +3 -3
  169. package/dist/objects.d.cts +1 -1
  170. package/dist/objects.d.ts +1 -1
  171. package/dist/optional-content.d.cts +2 -2
  172. package/dist/optional-content.d.ts +2 -2
  173. package/dist/ot-layout-common.cjs +22 -15
  174. package/dist/ot-layout-common.js +22 -15
  175. package/dist/parse.cjs +7 -5
  176. package/dist/parse.d.cts +1 -1
  177. package/dist/parse.d.ts +1 -1
  178. package/dist/parse.js +7 -5
  179. package/dist/pdf-text.cjs +5 -2
  180. package/dist/pdf-text.js +5 -2
  181. package/dist/predictors.cjs +14 -9
  182. package/dist/predictors.d.cts +1 -1
  183. package/dist/predictors.d.ts +1 -1
  184. package/dist/predictors.js +14 -9
  185. package/dist/raster.cjs +30 -19
  186. package/dist/raster.d.cts +8 -8
  187. package/dist/raster.d.ts +8 -8
  188. package/dist/raster.js +30 -19
  189. package/dist/read.cjs +29 -16
  190. package/dist/read.js +29 -16
  191. package/dist/serialize.cjs +6 -2
  192. package/dist/serialize.d.cts +1 -1
  193. package/dist/serialize.d.ts +1 -1
  194. package/dist/serialize.js +6 -2
  195. package/dist/sfnt-subset.cjs +25 -13
  196. package/dist/sfnt-subset.d.cts +1 -1
  197. package/dist/sfnt-subset.d.ts +1 -1
  198. package/dist/sfnt-subset.js +25 -13
  199. package/dist/sfnt.cjs +20 -10
  200. package/dist/sfnt.js +20 -10
  201. package/dist/structure.d.cts +2 -2
  202. package/dist/structure.d.ts +2 -2
  203. package/dist/text-group.cjs +0 -1
  204. package/dist/text-group.js +0 -1
  205. package/dist/text-layout.d.cts +1 -1
  206. package/dist/text-layout.d.ts +1 -1
  207. package/dist/tounicode.cjs +4 -2
  208. package/dist/tounicode.d.cts +1 -1
  209. package/dist/tounicode.d.ts +1 -1
  210. package/dist/tounicode.js +4 -2
  211. package/dist/winansi.cjs +1 -1
  212. package/dist/winansi.js +1 -1
  213. package/dist/write.cjs +20 -12
  214. package/dist/write.d.cts +1 -1
  215. package/dist/write.d.ts +1 -1
  216. package/dist/write.js +20 -12
  217. package/dist/xref.cjs +5 -2
  218. package/dist/xref.d.cts +1 -1
  219. package/dist/xref.d.ts +1 -1
  220. package/dist/xref.js +5 -2
  221. package/package.json +1 -1
package/dist/interpret.js CHANGED
@@ -7,6 +7,8 @@ import { COLOR_BLACK } from "document-schema.js";
7
7
  //#region src/interpret.ts
8
8
  const MAX_FORM_XOBJECT_DEPTH = 12;
9
9
  const FALLBACK_GLYPH_WIDTH_PER_1000 = 500;
10
+ const GLYPH_SPACE_PER_TEXT_SPACE_UNIT = 1e3;
11
+ const ASCII_SPACE = 32;
10
12
  const DEFAULT_LINE_WIDTH_PT = 1;
11
13
  function initialTextParameters() {
12
14
  return {
@@ -71,31 +73,37 @@ function rgbColor(operands) {
71
73
  b: numAt(operands, 2)
72
74
  };
73
75
  }
76
+ const CMYK_K_INDEX = 3;
74
77
  function cmykColor(operands) {
75
78
  const c = numAt(operands, 0);
76
79
  const m = numAt(operands, 1);
77
80
  const y = numAt(operands, 2);
78
- const k = numAt(operands, 3);
81
+ const k = numAt(operands, CMYK_K_INDEX);
79
82
  return {
80
83
  r: (1 - c) * (1 - k),
81
84
  g: (1 - m) * (1 - k),
82
85
  b: (1 - y) * (1 - k)
83
86
  };
84
87
  }
88
+ const RGB_COMPONENT_COUNT = 3;
89
+ const CMYK_COMPONENT_COUNT = 4;
85
90
  function genericColor(operands) {
86
91
  const numericOperands = operands.filter((o) => o.kind === "number");
87
92
  if (numericOperands.length === 1) return grayColor(numAt(numericOperands, 0));
88
- if (numericOperands.length === 3) return rgbColor(numericOperands);
89
- if (numericOperands.length === 4) return cmykColor(numericOperands);
93
+ if (numericOperands.length === RGB_COMPONENT_COUNT) return rgbColor(numericOperands);
94
+ if (numericOperands.length === CMYK_COMPONENT_COUNT) return cmykColor(numericOperands);
90
95
  }
96
+ const MATRIX_D_INDEX = 3;
97
+ const MATRIX_E_INDEX = 4;
98
+ const MATRIX_F_INDEX = 5;
91
99
  function matrixFromOperands(operands) {
92
100
  return [
93
101
  numAt(operands, 0),
94
102
  numAt(operands, 1),
95
103
  numAt(operands, 2),
96
- numAt(operands, 3),
97
- numAt(operands, 4),
98
- numAt(operands, 5)
104
+ numAt(operands, MATRIX_D_INDEX),
105
+ numAt(operands, MATRIX_E_INDEX),
106
+ numAt(operands, MATRIX_F_INDEX)
99
107
  ];
100
108
  }
101
109
  const SHAPE_ABS_TOLERANCE_PT = .001;
@@ -139,9 +147,10 @@ function closedPolygonCorners(subpath) {
139
147
  if (nearlyEqualPoints(first, last, shapeTolerance(Math.max(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY)))) corners.pop();
140
148
  return corners;
141
149
  }
150
+ const RECTANGLE_CORNER_COUNT = 4;
142
151
  function detectRect(subpath, paint) {
143
152
  const corners = closedPolygonCorners(subpath);
144
- if (corners?.length !== 4) return;
153
+ if (corners?.length !== RECTANGLE_CORNER_COUNT) return;
145
154
  const { minX, minY, maxX, maxY } = boundsOf(corners);
146
155
  const widthPt = maxX - minX;
147
156
  const heightPt = maxY - minY;
@@ -189,9 +198,10 @@ function expectedEllipseControl(atExtreme, atExtremeKind, otherEnd, center, kx,
189
198
  y: atExtreme.y
190
199
  };
191
200
  }
201
+ const ELLIPSE_SEGMENT_COUNT = 4;
192
202
  function detectEllipse(subpath, paint) {
193
203
  const segments = subpath.segments;
194
- if (!subpath.closed || segments.length !== 4 || segments.some((segment) => segment.kind !== "cubic")) return;
204
+ if (!subpath.closed || segments.length !== ELLIPSE_SEGMENT_COUNT || segments.some((segment) => segment.kind !== "cubic")) return;
195
205
  const start = {
196
206
  x: subpath.startXPt,
197
207
  y: subpath.startYPt
@@ -217,16 +227,16 @@ function detectEllipse(subpath, paint) {
217
227
  y: finalSegment.yPt
218
228
  }, start, Math.max(tolX, tolY))) return;
219
229
  const extremes = onCurve.map((point) => cardinalExtremeOf(point, center, rx, ry, tolX, tolY));
220
- if (extremes.some((extreme) => extreme === void 0) || new Set(extremes).size !== 4) return;
230
+ if (extremes.some((extreme) => extreme === void 0) || new Set(extremes).size !== ELLIPSE_SEGMENT_COUNT) return;
221
231
  const kx = rx * BEZIER_KAPPA;
222
232
  const ky = ry * BEZIER_KAPPA;
223
233
  const controlTolerance = Math.max(tolX, tolY);
224
- for (let i = 0; i < 4; i += 1) {
234
+ for (let i = 0; i < ELLIPSE_SEGMENT_COUNT; i += 1) {
225
235
  const segment = segments[i];
226
236
  const from = onCurve[i];
227
237
  const fromKind = extremes[i];
228
- const to = onCurve[(i + 1) % 4];
229
- const toKind = extremes[(i + 1) % 4];
238
+ const to = onCurve[(i + 1) % ELLIPSE_SEGMENT_COUNT];
239
+ const toKind = extremes[(i + 1) % ELLIPSE_SEGMENT_COUNT];
230
240
  if (segment?.kind !== "cubic" || from === void 0 || to === void 0 || fromKind === void 0 || toKind === void 0) return;
231
241
  const expectedC1 = expectedEllipseControl(from, fromKind, to, center, kx, ky);
232
242
  const expectedC2 = expectedEllipseControl(to, toKind, from, center, kx, ky);
@@ -273,17 +283,38 @@ function classifyShape(subpaths, paint, style) {
273
283
  if (subpaths.length !== 1 || subpath === void 0) return;
274
284
  return detectRect(subpath, paint) ?? detectEllipse(subpath, paint) ?? detectLine(subpath, paint, style);
275
285
  }
286
+ var MarkedContentStack = class {
287
+ #frames;
288
+ constructor(seed = []) {
289
+ this.#frames = [...seed];
290
+ }
291
+ open(frame) {
292
+ this.#frames.push(frame);
293
+ }
294
+ close() {
295
+ this.#frames.pop();
296
+ }
297
+ inScope(key) {
298
+ for (let i = this.#frames.length - 1; i >= 0; i--) {
299
+ const frame = this.#frames[i];
300
+ if (frame !== void 0 && key in frame) return frame[key];
301
+ }
302
+ }
303
+ };
304
+ const RE_HEIGHT_INDEX = 3;
305
+ const OPERAND_INDEX_3 = 3;
306
+ const OPERAND_INDEX_4 = 4;
307
+ const OPERAND_INDEX_5 = 5;
308
+ const TZ_PERCENT_DIVISOR = 100;
276
309
  function interpretContentStream(bytes, resources, context) {
277
- const items = [];
278
- runContentStream(bytes, resources, {
310
+ return runContentStream(bytes, resources, {
279
311
  ctm: IDENTITY_MATRIX,
280
312
  fillColor: COLOR_BLACK,
281
313
  strokeColor: COLOR_BLACK,
282
314
  lineWidth: DEFAULT_LINE_WIDTH_PT,
283
315
  dashArray: [],
284
316
  ...initialTextParameters()
285
- }, context, items, 0, []);
286
- return items;
317
+ }, context, 0, new MarkedContentStack());
287
318
  }
288
319
  function lastPointOf(subpath) {
289
320
  const last = subpath.segments[subpath.segments.length - 1];
@@ -295,19 +326,15 @@ function lastPointOf(subpath) {
295
326
  y: last.yPt
296
327
  };
297
328
  }
298
- function runContentStream(bytes, resources, initialState, context, items, depth, markedContent) {
329
+ function runContentStream(bytes, resources, initialState, context, depth, markedContent) {
330
+ const items = [];
299
331
  const operations = readContentStream(bytes, context.sink);
300
332
  const gsStack = [];
301
333
  let gs = initialState;
302
334
  let text = defaultTextObjectState();
303
335
  let pathSubpaths = [];
304
336
  let currentSubpath;
305
- const inScope = (key) => {
306
- for (let i = markedContent.length - 1; i >= 0; i--) {
307
- const frame = markedContent[i];
308
- if (frame !== void 0 && key in frame) return frame[key];
309
- }
310
- };
337
+ const inScope = (key) => markedContent.inScope(key);
311
338
  const pushItem = (item) => {
312
339
  if (item.kind === "text") {
313
340
  const layer = item.layerName ?? inScope("layerName");
@@ -349,7 +376,7 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
349
376
  const x = numAt(operands, 0);
350
377
  const y = numAt(operands, 1);
351
378
  const w = numAt(operands, 2);
352
- const h = numAt(operands, 3);
379
+ const h = numAt(operands, RE_HEIGHT_INDEX);
353
380
  const p1 = applyMatrix(ctm, {
354
381
  x,
355
382
  y
@@ -431,10 +458,10 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
431
458
  });
432
459
  const widthPer1000 = glyph?.widthPer1000 ?? FALLBACK_GLYPH_WIDTH_PER_1000;
433
460
  const byteLength = glyph?.byteLengthConsumed ?? 1;
434
- const isSingleByteSpace = byteLength === 1 && codes[offset] === 32;
461
+ const isSingleByteSpace = byteLength === 1 && codes[offset] === ASCII_SPACE;
435
462
  const spacing = gs.charSpace + (isSingleByteSpace ? gs.wordSpace : 0);
436
463
  const vertical = glyph?.vertical;
437
- text.tm = vertical === void 0 ? multiplyMatrices(translationMatrix((widthPer1000 / 1e3 * gs.fontSizePt + spacing) * gs.horizScale, 0), text.tm) : multiplyMatrices(translationMatrix(0, vertical.displacementPer1000 / 1e3 * gs.fontSizePt + spacing), text.tm);
464
+ text.tm = vertical === void 0 ? multiplyMatrices(translationMatrix((widthPer1000 / GLYPH_SPACE_PER_TEXT_SPACE_UNIT * gs.fontSizePt + spacing) * gs.horizScale, 0), text.tm) : multiplyMatrices(translationMatrix(0, vertical.displacementPer1000 / GLYPH_SPACE_PER_TEXT_SPACE_UNIT * gs.fontSizePt + spacing), text.tm);
438
465
  offset += byteLength;
439
466
  }
440
467
  };
@@ -444,8 +471,8 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
444
471
  const vertical = context.fontMetrics.glyphAdvance(fontResourceName, resources, codes, 0)?.vertical;
445
472
  if (vertical === void 0) return;
446
473
  return {
447
- x: vertical.positionXPer1000 / 1e3,
448
- y: vertical.positionYPer1000 / 1e3
474
+ x: vertical.positionXPer1000 / GLYPH_SPACE_PER_TEXT_SPACE_UNIT,
475
+ y: vertical.positionYPer1000 / GLYPH_SPACE_PER_TEXT_SPACE_UNIT
449
476
  };
450
477
  };
451
478
  const showTextArray = (elements) => {
@@ -462,7 +489,7 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
462
489
  totalLength += el.bytes.length;
463
490
  advanceThroughString(el.bytes);
464
491
  } else if (el.kind === "number") {
465
- const adjustment = -(el.value / 1e3) * gs.fontSizePt;
492
+ const adjustment = -(el.value / GLYPH_SPACE_PER_TEXT_SPACE_UNIT) * gs.fontSizePt;
466
493
  text.tm = multiplyMatrices(vertical ? translationMatrix(0, adjustment) : translationMatrix(adjustment * gs.horizScale, 0), text.tm);
467
494
  }
468
495
  if (totalLength === 0) return;
@@ -540,10 +567,10 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
540
567
  };
541
568
  const formLayer = ownLayer ?? inScope("layerName");
542
569
  const formMcid = dictGet(xobj.dict, "StructParents") !== void 0 ? void 0 : inScope("mcid");
543
- runContentStream(decoded.bytes, formResources, formState, context, items, depth + 1, [{
570
+ for (const formItem of runContentStream(decoded.bytes, formResources, formState, context, depth + 1, new MarkedContentStack([{
544
571
  ...formLayer !== void 0 ? { layerName: formLayer } : {},
545
572
  ...formMcid !== void 0 ? { mcid: formMcid } : {}
546
- }]);
573
+ }]))) items.push(formItem);
547
574
  }
548
575
  };
549
576
  for (const token of operations) {
@@ -660,11 +687,11 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
660
687
  });
661
688
  const c2 = applyMatrix(gs.ctm, {
662
689
  x: numAt(operands, 2),
663
- y: numAt(operands, 3)
690
+ y: numAt(operands, OPERAND_INDEX_3)
664
691
  });
665
692
  const p = applyMatrix(gs.ctm, {
666
- x: numAt(operands, 4),
667
- y: numAt(operands, 5)
693
+ x: numAt(operands, OPERAND_INDEX_4),
694
+ y: numAt(operands, OPERAND_INDEX_5)
668
695
  });
669
696
  currentSubpath?.segments.push({
670
697
  kind: "cubic",
@@ -686,7 +713,7 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
686
713
  });
687
714
  const p = applyMatrix(gs.ctm, {
688
715
  x: numAt(operands, 2),
689
- y: numAt(operands, 3)
716
+ y: numAt(operands, OPERAND_INDEX_3)
690
717
  });
691
718
  currentSubpath.segments.push({
692
719
  kind: "cubic",
@@ -706,7 +733,7 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
706
733
  });
707
734
  const p = applyMatrix(gs.ctm, {
708
735
  x: numAt(operands, 2),
709
- y: numAt(operands, 3)
736
+ y: numAt(operands, OPERAND_INDEX_3)
710
737
  });
711
738
  currentSubpath?.segments.push({
712
739
  kind: "cubic",
@@ -771,7 +798,7 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
771
798
  case "Tz":
772
799
  gs = {
773
800
  ...gs,
774
- horizScale: numAt(operands, 0) / 100
801
+ horizScale: numAt(operands, 0) / TZ_PERCENT_DIVISOR
775
802
  };
776
803
  break;
777
804
  case "TL":
@@ -842,18 +869,19 @@ function runContentStream(bytes, resources, initialState, context, items, depth,
842
869
  ...layerName !== void 0 ? { layerName } : {},
843
870
  ...markedContentScopeProps(props)
844
871
  };
845
- markedContent.push(depth === 0 ? scopeProps : {
872
+ markedContent.open(depth === 0 ? scopeProps : {
846
873
  ...scopeProps,
847
874
  mcid: void 0
848
875
  });
849
876
  break;
850
877
  }
851
878
  case "BMC":
852
- markedContent.push({});
879
+ markedContent.open({});
853
880
  break;
854
- case "EMC": markedContent.pop();
881
+ case "EMC": markedContent.close();
855
882
  }
856
883
  }
884
+ return items;
857
885
  }
858
886
  //#endregion
859
887
  export { interpretContentStream };
package/dist/lexer.cjs CHANGED
@@ -1,44 +1,79 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_bytes_reader = require("./bytes/reader.cjs");
3
3
  //#region src/lexer.ts
4
+ const LEFT_PAREN = 40;
5
+ const RIGHT_PAREN = 41;
6
+ const LESS_THAN = 60;
7
+ const GREATER_THAN = 62;
8
+ const LEFT_BRACKET = 91;
9
+ const RIGHT_BRACKET = 93;
10
+ const LEFT_BRACE = 123;
11
+ const RIGHT_BRACE = 125;
12
+ const SOLIDUS = 47;
13
+ const PERCENT_SIGN = 37;
14
+ const REVERSE_SOLIDUS = 92;
15
+ const NUMBER_SIGN = 35;
16
+ const PLUS_SIGN = 43;
17
+ const MINUS_SIGN = 45;
18
+ const FULL_STOP = 46;
19
+ const LINE_FEED = 10;
20
+ const CARRIAGE_RETURN = 13;
21
+ const HORIZONTAL_TAB = 9;
22
+ const BACKSPACE = 8;
23
+ const FORM_FEED = 12;
24
+ const DIGIT_ZERO = 48;
25
+ const DIGIT_SEVEN = 55;
26
+ const DIGIT_NINE = 57;
27
+ const UPPER_A = 65;
28
+ const UPPER_F = 70;
29
+ const LOWER_A = 97;
30
+ const LOWER_B = 98;
31
+ const LOWER_F = 102;
32
+ const LOWER_N = 110;
33
+ const LOWER_R = 114;
34
+ const LOWER_T = 116;
35
+ const HEX_DIGIT_ALPHA_OFFSET = 10;
36
+ const HEX_RADIX = 16;
37
+ const OCTAL_RADIX = 8;
38
+ const BYTE_MASK = 255;
4
39
  const DELIMITER_BYTES = /* @__PURE__ */ new Set([
5
- 40,
6
- 41,
7
- 60,
8
- 62,
9
- 91,
10
- 93,
11
- 123,
12
- 125,
13
- 47,
14
- 37
40
+ LEFT_PAREN,
41
+ RIGHT_PAREN,
42
+ LESS_THAN,
43
+ GREATER_THAN,
44
+ LEFT_BRACKET,
45
+ RIGHT_BRACKET,
46
+ LEFT_BRACE,
47
+ RIGHT_BRACE,
48
+ SOLIDUS,
49
+ PERCENT_SIGN
15
50
  ]);
16
51
  function isRegularByte(byte) {
17
52
  return !require_bytes_reader.isAsciiWhitespace(byte) && !DELIMITER_BYTES.has(byte);
18
53
  }
19
54
  function isDigit(byte) {
20
- return byte !== void 0 && byte >= 48 && byte <= 57;
55
+ return byte !== void 0 && byte >= DIGIT_ZERO && byte <= DIGIT_NINE;
21
56
  }
22
57
  function isHexDigit(byte) {
23
- return byte !== void 0 && (byte >= 48 && byte <= 57 || byte >= 65 && byte <= 70 || byte >= 97 && byte <= 102);
58
+ return byte !== void 0 && (byte >= DIGIT_ZERO && byte <= DIGIT_NINE || byte >= UPPER_A && byte <= UPPER_F || byte >= LOWER_A && byte <= LOWER_F);
24
59
  }
25
60
  function hexDigitValue(byte) {
26
- if (byte >= 48 && byte <= 57) return byte - 48;
27
- if (byte >= 65 && byte <= 70) return byte - 65 + 10;
28
- return byte - 97 + 10;
61
+ if (byte >= DIGIT_ZERO && byte <= DIGIT_NINE) return byte - DIGIT_ZERO;
62
+ if (byte >= UPPER_A && byte <= UPPER_F) return byte - UPPER_A + HEX_DIGIT_ALPHA_OFFSET;
63
+ return byte - LOWER_A + HEX_DIGIT_ALPHA_OFFSET;
29
64
  }
30
65
  function skipWhitespaceAndComments(reader) {
31
66
  for (;;) {
32
67
  reader.skipWhitespace();
33
- if (reader.peek() !== 37) return;
34
- while (!reader.atEnd() && reader.peek() !== 10 && reader.peek() !== 13) reader.next();
68
+ if (reader.peek() !== PERCENT_SIGN) return;
69
+ while (!reader.atEnd() && reader.peek() !== LINE_FEED && reader.peek() !== CARRIAGE_RETURN) reader.next();
35
70
  }
36
71
  }
37
72
  function readNumberToken(reader) {
38
73
  const start = reader.offset;
39
- if (reader.peek() === 43 || reader.peek() === 45) reader.next();
74
+ if (reader.peek() === PLUS_SIGN || reader.peek() === MINUS_SIGN) reader.next();
40
75
  while (isDigit(reader.peek())) reader.next();
41
- if (reader.peek() === 46) {
76
+ if (reader.peek() === FULL_STOP) {
42
77
  reader.next();
43
78
  while (isDigit(reader.peek())) reader.next();
44
79
  }
@@ -54,11 +89,11 @@ function readNameToken(reader) {
54
89
  for (;;) {
55
90
  const byte = reader.peek();
56
91
  if (byte === void 0 || !isRegularByte(byte)) break;
57
- if (byte === 35 && isHexDigit(reader.peek(1)) && isHexDigit(reader.peek(2))) {
92
+ if (byte === NUMBER_SIGN && isHexDigit(reader.peek(1)) && isHexDigit(reader.peek(2))) {
58
93
  reader.next();
59
94
  const hi = hexDigitValue(reader.next());
60
95
  const lo = hexDigitValue(reader.next());
61
- bytes.push(hi * 16 + lo);
96
+ bytes.push(hi * HEX_RADIX + lo);
62
97
  } else bytes.push(reader.next());
63
98
  }
64
99
  return {
@@ -73,38 +108,38 @@ function readLiteralStringToken(reader) {
73
108
  for (;;) {
74
109
  const byte = reader.next();
75
110
  if (byte === void 0) break;
76
- if (byte === 92) {
111
+ if (byte === REVERSE_SOLIDUS) {
77
112
  const esc = reader.next();
78
113
  if (esc === void 0) break;
79
- if (esc === 110) bytes.push(10);
80
- else if (esc === 114) bytes.push(13);
81
- else if (esc === 116) bytes.push(9);
82
- else if (esc === 98) bytes.push(8);
83
- else if (esc === 102) bytes.push(12);
84
- else if (esc === 40 || esc === 41 || esc === 92) bytes.push(esc);
85
- else if (esc === 13) {
86
- if (reader.peek() === 10) reader.next();
87
- } else if (esc === 10) {} else if (esc >= 48 && esc <= 55) {
88
- let value = esc - 48;
89
- for (let i = 0; i < 2 && reader.peek() !== void 0 && reader.peek() >= 48 && reader.peek() <= 55; i++) value = value * 8 + (reader.next() - 48);
90
- bytes.push(value & 255);
114
+ if (esc === LOWER_N) bytes.push(LINE_FEED);
115
+ else if (esc === LOWER_R) bytes.push(CARRIAGE_RETURN);
116
+ else if (esc === LOWER_T) bytes.push(HORIZONTAL_TAB);
117
+ else if (esc === LOWER_B) bytes.push(BACKSPACE);
118
+ else if (esc === LOWER_F) bytes.push(FORM_FEED);
119
+ else if (esc === LEFT_PAREN || esc === RIGHT_PAREN || esc === REVERSE_SOLIDUS) bytes.push(esc);
120
+ else if (esc === CARRIAGE_RETURN) {
121
+ if (reader.peek() === LINE_FEED) reader.next();
122
+ } else if (esc === LINE_FEED) {} else if (esc >= DIGIT_ZERO && esc <= DIGIT_SEVEN) {
123
+ let value = esc - DIGIT_ZERO;
124
+ for (let i = 0; i < 2 && reader.peek() !== void 0 && reader.peek() >= DIGIT_ZERO && reader.peek() <= DIGIT_SEVEN; i++) value = value * OCTAL_RADIX + (reader.next() - DIGIT_ZERO);
125
+ bytes.push(value & BYTE_MASK);
91
126
  } else bytes.push(esc);
92
127
  continue;
93
128
  }
94
- if (byte === 40) {
129
+ if (byte === LEFT_PAREN) {
95
130
  depth++;
96
131
  bytes.push(byte);
97
132
  continue;
98
133
  }
99
- if (byte === 41) {
134
+ if (byte === RIGHT_PAREN) {
100
135
  depth--;
101
136
  if (depth === 0) break;
102
137
  bytes.push(byte);
103
138
  continue;
104
139
  }
105
- if (byte === 13) {
106
- if (reader.peek() === 10) reader.next();
107
- bytes.push(10);
140
+ if (byte === CARRIAGE_RETURN) {
141
+ if (reader.peek() === LINE_FEED) reader.next();
142
+ bytes.push(LINE_FEED);
108
143
  continue;
109
144
  }
110
145
  bytes.push(byte);
@@ -119,12 +154,12 @@ function readHexStringToken(reader) {
119
154
  const digits = [];
120
155
  for (;;) {
121
156
  const byte = reader.next();
122
- if (byte === void 0 || byte === 62) break;
157
+ if (byte === void 0 || byte === GREATER_THAN) break;
123
158
  if (isHexDigit(byte)) digits.push(hexDigitValue(byte));
124
159
  }
125
160
  if (digits.length % 2 === 1) digits.push(0);
126
161
  const bytes = new Uint8Array(digits.length / 2);
127
- for (let i = 0; i < bytes.length; i++) bytes[i] = digits[i * 2] * 16 + digits[i * 2 + 1];
162
+ for (let i = 0; i < bytes.length; i++) bytes[i] = digits[i * 2] * HEX_RADIX + digits[i * 2 + 1];
128
163
  return {
129
164
  kind: "hexString",
130
165
  value: bytes
@@ -142,18 +177,18 @@ function nextToken(reader) {
142
177
  skipWhitespaceAndComments(reader);
143
178
  const byte = reader.peek();
144
179
  if (byte === void 0) return;
145
- if (byte === 47) return readNameToken(reader);
146
- if (byte === 40) return readLiteralStringToken(reader);
147
- if (byte === 60) {
148
- if (reader.peek(1) === 60) {
180
+ if (byte === SOLIDUS) return readNameToken(reader);
181
+ if (byte === LEFT_PAREN) return readLiteralStringToken(reader);
182
+ if (byte === LESS_THAN) {
183
+ if (reader.peek(1) === LESS_THAN) {
149
184
  reader.next();
150
185
  reader.next();
151
186
  return { kind: "dictStart" };
152
187
  }
153
188
  return readHexStringToken(reader);
154
189
  }
155
- if (byte === 62) {
156
- if (reader.peek(1) === 62) {
190
+ if (byte === GREATER_THAN) {
191
+ if (reader.peek(1) === GREATER_THAN) {
157
192
  reader.next();
158
193
  reader.next();
159
194
  return { kind: "dictEnd" };
@@ -161,19 +196,19 @@ function nextToken(reader) {
161
196
  reader.next();
162
197
  return nextToken(reader);
163
198
  }
164
- if (byte === 91) {
199
+ if (byte === LEFT_BRACKET) {
165
200
  reader.next();
166
201
  return { kind: "arrayStart" };
167
202
  }
168
- if (byte === 93) {
203
+ if (byte === RIGHT_BRACKET) {
169
204
  reader.next();
170
205
  return { kind: "arrayEnd" };
171
206
  }
172
- if (byte === 123 || byte === 125) {
207
+ if (byte === LEFT_BRACE || byte === RIGHT_BRACE) {
173
208
  reader.next();
174
209
  return nextToken(reader);
175
210
  }
176
- if (byte === 43 || byte === 45 || byte === 46 || isDigit(byte)) return readNumberToken(reader);
211
+ if (byte === PLUS_SIGN || byte === MINUS_SIGN || byte === FULL_STOP || isDigit(byte)) return readNumberToken(reader);
177
212
  return readKeywordToken(reader);
178
213
  }
179
214
  //#endregion