pdf-parser.js 5.2.30 → 5.3.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 (189) hide show
  1. package/dist/{afm-widths-BHD2HZIL.cjs → afm-widths-C6U0SQj9.cjs} +5 -1448
  2. package/dist/{afm-widths-B_IGbGky.js → afm-widths-p3UfaXme.js} +3 -1398
  3. package/dist/afm-widths.cjs +1 -1
  4. package/dist/afm-widths.js +1 -1
  5. package/dist/annotations.d.cts +3 -3
  6. package/dist/annotations.d.ts +3 -3
  7. package/dist/attachments.d.cts +2 -2
  8. package/dist/attachments.d.ts +2 -2
  9. package/dist/builtin-encoding.cjs +4 -3
  10. package/dist/builtin-encoding.js +2 -1
  11. package/dist/cmap.d.cts +1 -1
  12. package/dist/cmap.d.ts +1 -1
  13. package/dist/content-read.d.cts +1 -1
  14. package/dist/content-read.d.ts +1 -1
  15. package/dist/content-write.cjs +1 -1
  16. package/dist/content-write.d.cts +1 -1
  17. package/dist/content-write.d.ts +1 -1
  18. package/dist/content-write.js +1 -1
  19. package/dist/diagnostics-C9FxfZ6H.d.cts +22 -0
  20. package/dist/diagnostics-C9FxfZ6H.d.ts +22 -0
  21. package/dist/diagnostics.d.cts +1 -21
  22. package/dist/diagnostics.d.ts +1 -21
  23. package/dist/document.d.cts +1 -1
  24. package/dist/document.d.ts +1 -1
  25. package/dist/{embedded-font-ByD7ZiQS.d.ts → embedded-font-CQZbTTDM.d.ts} +1 -1
  26. package/dist/{embedded-font-DgVBedw6.d.cts → embedded-font-CqePfvrl.d.cts} +1 -1
  27. package/dist/embedded-font-write.d.cts +1 -1
  28. package/dist/embedded-font-write.d.ts +1 -1
  29. package/dist/embedded-font.cjs +2 -2
  30. package/dist/embedded-font.d.cts +1 -1
  31. package/dist/embedded-font.d.ts +1 -1
  32. package/dist/embedded-font.js +2 -2
  33. package/dist/encoding-glyph-unicode.cjs +353 -0
  34. package/dist/encoding-glyph-unicode.d.cts +4 -0
  35. package/dist/encoding-glyph-unicode.d.ts +4 -0
  36. package/dist/encoding-glyph-unicode.js +352 -0
  37. package/dist/encoding-standard-tables.cjs +531 -0
  38. package/dist/encoding-standard-tables.d.cts +7 -0
  39. package/dist/encoding-standard-tables.d.ts +7 -0
  40. package/dist/encoding-standard-tables.js +527 -0
  41. package/dist/encoding-symbol-tables.cjs +531 -0
  42. package/dist/encoding-symbol-tables.d.cts +7 -0
  43. package/dist/encoding-symbol-tables.d.ts +7 -0
  44. package/dist/encoding-symbol-tables.js +527 -0
  45. package/dist/encoding.cjs +1 -9
  46. package/dist/encoding.d.cts +1 -9
  47. package/dist/encoding.d.ts +1 -9
  48. package/dist/encoding.js +2 -2
  49. package/dist/encrypt.d.cts +1 -1
  50. package/dist/encrypt.d.ts +1 -1
  51. package/dist/filters.d.cts +1 -1
  52. package/dist/filters.d.ts +1 -1
  53. package/dist/font-read.cjs +4 -3
  54. package/dist/font-read.d.cts +2 -2
  55. package/dist/font-read.d.ts +2 -2
  56. package/dist/font-read.js +2 -1
  57. package/dist/font-registry.d.cts +1 -1
  58. package/dist/font-registry.d.ts +1 -1
  59. package/dist/form.d.cts +2 -2
  60. package/dist/form.d.ts +2 -2
  61. package/dist/gdef-table-D0Hzn4KD.d.cts +15 -0
  62. package/dist/gdef-table-DL0V8wEZ.d.ts +15 -0
  63. package/dist/gdef-table.d.cts +1 -14
  64. package/dist/gdef-table.d.ts +1 -14
  65. package/dist/glyf-contours.d.cts +12 -1
  66. package/dist/glyf-contours.d.ts +12 -1
  67. package/dist/gsub-context.cjs +13 -0
  68. package/dist/gsub-context.d.cts +34 -0
  69. package/dist/gsub-context.d.ts +34 -0
  70. package/dist/gsub-context.js +2 -0
  71. package/dist/gsub-table-BDpJWqpf.d.ts +52 -0
  72. package/dist/gsub-table-D6niKeLi.js +556 -0
  73. package/dist/gsub-table-DW7Wmi9V.d.cts +52 -0
  74. package/dist/gsub-table-hFfYmf1W.cjs +741 -0
  75. package/dist/gsub-table.cjs +21 -556
  76. package/dist/gsub-table.d.cts +2 -2
  77. package/dist/gsub-table.d.ts +2 -2
  78. package/dist/gsub-table.js +2 -554
  79. package/dist/images-read.d.cts +2 -2
  80. package/dist/images-read.d.ts +2 -2
  81. package/dist/index.cjs +6 -6
  82. package/dist/index.d.cts +2 -2
  83. package/dist/index.d.ts +2 -2
  84. package/dist/index.js +6 -6
  85. package/dist/interpret-CqXdXgkY.cjs +712 -0
  86. package/dist/interpret-aWWhdIzr.js +677 -0
  87. package/dist/interpret-colors.cjs +6 -0
  88. package/dist/interpret-colors.d.cts +9 -0
  89. package/dist/interpret-colors.d.ts +9 -0
  90. package/dist/interpret-colors.js +2 -0
  91. package/dist/interpret-marked.cjs +25 -0
  92. package/dist/interpret-marked.d.cts +11 -0
  93. package/dist/interpret-marked.d.ts +11 -0
  94. package/dist/interpret-marked.js +23 -0
  95. package/dist/interpret-shapes.cjs +212 -0
  96. package/dist/interpret-shapes.d.cts +32 -0
  97. package/dist/interpret-shapes.d.ts +32 -0
  98. package/dist/interpret-shapes.js +199 -0
  99. package/dist/interpret-types.cjs +0 -0
  100. package/dist/interpret-types.d.cts +123 -0
  101. package/dist/interpret-types.d.ts +123 -0
  102. package/dist/interpret-types.js +0 -0
  103. package/dist/interpret.cjs +3 -887
  104. package/dist/interpret.d.cts +5 -116
  105. package/dist/interpret.d.ts +5 -116
  106. package/dist/interpret.js +2 -887
  107. package/dist/math-font.cjs +1 -1
  108. package/dist/math-font.js +1 -1
  109. package/dist/measure.cjs +1 -1
  110. package/dist/measure.js +1 -1
  111. package/dist/names.d.cts +2 -2
  112. package/dist/names.d.ts +2 -2
  113. package/dist/navigation.d.cts +2 -2
  114. package/dist/navigation.d.ts +2 -2
  115. package/dist/optional-content.d.cts +2 -2
  116. package/dist/optional-content.d.ts +2 -2
  117. package/dist/ot-layout-common-BcsGRFBS.d.cts +10 -0
  118. package/dist/ot-layout-common-BcsGRFBS.d.ts +10 -0
  119. package/dist/ot-layout-common.d.cts +1 -9
  120. package/dist/ot-layout-common.d.ts +1 -9
  121. package/dist/parse.d.cts +1 -1
  122. package/dist/parse.d.ts +1 -1
  123. package/dist/predictors.d.cts +1 -1
  124. package/dist/predictors.d.ts +1 -1
  125. package/dist/raster-Cdm3idP_.js +794 -0
  126. package/dist/raster-EG2KYRZ0.cjs +829 -0
  127. package/dist/raster-text.cjs +5 -0
  128. package/dist/raster-text.d.cts +20 -0
  129. package/dist/raster-text.d.ts +20 -0
  130. package/dist/raster-text.js +2 -0
  131. package/dist/raster.cjs +4 -795
  132. package/dist/raster.d.cts +17 -6
  133. package/dist/raster.d.ts +17 -6
  134. package/dist/raster.js +2 -792
  135. package/dist/read-convert.cjs +310 -0
  136. package/dist/read-convert.d.cts +58 -0
  137. package/dist/read-convert.d.ts +58 -0
  138. package/dist/read-convert.js +295 -0
  139. package/dist/read.cjs +5 -293
  140. package/dist/read.d.cts +1 -1
  141. package/dist/read.d.ts +1 -1
  142. package/dist/read.js +4 -292
  143. package/dist/structure.d.cts +2 -2
  144. package/dist/structure.d.ts +2 -2
  145. package/dist/winansi.cjs +1 -1
  146. package/dist/winansi.js +1 -1
  147. package/dist/write-annotations.cjs +132 -0
  148. package/dist/write-annotations.d.cts +29 -0
  149. package/dist/write-annotations.d.ts +29 -0
  150. package/dist/write-annotations.js +121 -0
  151. package/dist/write-font-objects.cjs +76 -0
  152. package/dist/write-font-objects.d.cts +35 -0
  153. package/dist/write-font-objects.d.ts +35 -0
  154. package/dist/write-font-objects.js +75 -0
  155. package/dist/write-form.cjs +91 -0
  156. package/dist/write-form.d.cts +19 -0
  157. package/dist/write-form.d.ts +19 -0
  158. package/dist/write-form.js +89 -0
  159. package/dist/write-images.cjs +148 -0
  160. package/dist/write-images.d.cts +25 -0
  161. package/dist/write-images.d.ts +25 -0
  162. package/dist/write-images.js +140 -0
  163. package/dist/write-outline.cjs +46 -0
  164. package/dist/write-outline.d.cts +18 -0
  165. package/dist/write-outline.d.ts +18 -0
  166. package/dist/write-outline.js +45 -0
  167. package/dist/write-page-emit.cjs +97 -0
  168. package/dist/write-page-emit.d.cts +63 -0
  169. package/dist/write-page-emit.d.ts +63 -0
  170. package/dist/write-page-emit.js +96 -0
  171. package/dist/write-pages.cjs +37 -0
  172. package/dist/write-pages.d.cts +21 -0
  173. package/dist/write-pages.d.ts +21 -0
  174. package/dist/write-pages.js +36 -0
  175. package/dist/write-strings.cjs +41 -0
  176. package/dist/write-strings.d.cts +9 -0
  177. package/dist/write-strings.d.ts +9 -0
  178. package/dist/write-strings.js +37 -0
  179. package/dist/write.cjs +53 -553
  180. package/dist/write.d.cts +1 -1
  181. package/dist/write.d.ts +1 -1
  182. package/dist/write.js +49 -549
  183. package/dist/xref.d.cts +1 -1
  184. package/dist/xref.d.ts +1 -1
  185. package/package.json +1 -1
  186. package/dist/glyf-contours-B1PsI9oA.d.ts +0 -13
  187. package/dist/glyf-contours-NHBpgks-.d.cts +0 -13
  188. package/dist/gsub-table-Bm9kSMzk.d.cts +0 -16
  189. package/dist/gsub-table-CdfESH2-.d.ts +0 -16
package/dist/interpret.js CHANGED
@@ -1,887 +1,2 @@
1
- import { asArray, asName, asNumber, dictGet } from "./objects.js";
2
- import { decodePdfString } from "./pdf-text.js";
3
- import { BEZIER_KAPPA, IDENTITY_MATRIX, applyMatrix, multiplyMatrices, translationMatrix } from "./matrix.js";
4
- import { decodeStream } from "./filters.js";
5
- import { readContentStream } from "./content-read.js";
6
- import { COLOR_BLACK } from "document-schema.js";
7
- //#region src/interpret.ts
8
- const MAX_FORM_XOBJECT_DEPTH = 12;
9
- const FALLBACK_GLYPH_WIDTH_PER_1000 = 500;
10
- const GLYPH_SPACE_PER_TEXT_SPACE_UNIT = 1e3;
11
- const ASCII_SPACE = 32;
12
- const DEFAULT_LINE_WIDTH_PT = 1;
13
- function initialTextParameters() {
14
- return {
15
- fontResourceName: void 0,
16
- fontSizePt: 0,
17
- charSpace: 0,
18
- wordSpace: 0,
19
- horizScale: 1,
20
- leading: 0,
21
- rise: 0
22
- };
23
- }
24
- function defaultTextObjectState() {
25
- return {
26
- tm: IDENTITY_MATRIX,
27
- tlm: IDENTITY_MATRIX
28
- };
29
- }
30
- function computeTrm(gs, text) {
31
- const fontMatrix = [
32
- gs.fontSizePt * gs.horizScale,
33
- 0,
34
- 0,
35
- gs.fontSizePt,
36
- 0,
37
- gs.rise
38
- ];
39
- return multiplyMatrices(multiplyMatrices(fontMatrix, text.tm), gs.ctm);
40
- }
41
- function numAt(operands, index) {
42
- return asNumber(operands[index]) ?? 0;
43
- }
44
- function markedContentProperties(operand, resources, resolver) {
45
- if (operand?.kind === "dict") return operand;
46
- if (operand?.kind === "name") {
47
- const properties = resolver.resolveDict(dictGet(resources, "Properties"));
48
- return properties === void 0 ? void 0 : resolver.resolveDict(dictGet(properties, operand.name));
49
- }
50
- }
51
- function markedContentScopeProps(props) {
52
- if (props === void 0) return {};
53
- const actualTextObj = dictGet(props, "ActualText");
54
- const altObj = dictGet(props, "Alt");
55
- const mcid = asNumber(dictGet(props, "MCID"));
56
- return {
57
- ...actualTextObj?.kind === "string" ? { actualText: decodePdfString(actualTextObj.bytes) } : {},
58
- ...altObj?.kind === "string" ? { alt: decodePdfString(altObj.bytes) } : {},
59
- ...mcid !== void 0 && mcid >= 0 ? { mcid } : {}
60
- };
61
- }
62
- function grayColor(value) {
63
- return {
64
- r: value,
65
- g: value,
66
- b: value
67
- };
68
- }
69
- function rgbColor(operands) {
70
- return {
71
- r: numAt(operands, 0),
72
- g: numAt(operands, 1),
73
- b: numAt(operands, 2)
74
- };
75
- }
76
- const CMYK_K_INDEX = 3;
77
- function cmykColor(operands) {
78
- const c = numAt(operands, 0);
79
- const m = numAt(operands, 1);
80
- const y = numAt(operands, 2);
81
- const k = numAt(operands, CMYK_K_INDEX);
82
- return {
83
- r: (1 - c) * (1 - k),
84
- g: (1 - m) * (1 - k),
85
- b: (1 - y) * (1 - k)
86
- };
87
- }
88
- const RGB_COMPONENT_COUNT = 3;
89
- const CMYK_COMPONENT_COUNT = 4;
90
- function genericColor(operands) {
91
- const numericOperands = operands.filter((o) => o.kind === "number");
92
- if (numericOperands.length === 1) return grayColor(numAt(numericOperands, 0));
93
- if (numericOperands.length === RGB_COMPONENT_COUNT) return rgbColor(numericOperands);
94
- if (numericOperands.length === CMYK_COMPONENT_COUNT) return cmykColor(numericOperands);
95
- }
96
- const MATRIX_D_INDEX = 3;
97
- const MATRIX_E_INDEX = 4;
98
- const MATRIX_F_INDEX = 5;
99
- function matrixFromOperands(operands) {
100
- return [
101
- numAt(operands, 0),
102
- numAt(operands, 1),
103
- numAt(operands, 2),
104
- numAt(operands, MATRIX_D_INDEX),
105
- numAt(operands, MATRIX_E_INDEX),
106
- numAt(operands, MATRIX_F_INDEX)
107
- ];
108
- }
109
- const SHAPE_ABS_TOLERANCE_PT = .001;
110
- const SHAPE_REL_TOLERANCE = 1e-4;
111
- function shapeTolerance(extentPt) {
112
- return Math.max(SHAPE_ABS_TOLERANCE_PT, Math.abs(extentPt) * SHAPE_REL_TOLERANCE);
113
- }
114
- function nearlyEqual(a, b, tolerance) {
115
- return Math.abs(a - b) <= tolerance;
116
- }
117
- function nearlyEqualPoints(a, b, tolerance) {
118
- return nearlyEqual(a.x, b.x, tolerance) && nearlyEqual(a.y, b.y, tolerance);
119
- }
120
- function boundsOf(points) {
121
- const xs = points.map((p) => p.x);
122
- const ys = points.map((p) => p.y);
123
- return {
124
- minX: Math.min(...xs),
125
- minY: Math.min(...ys),
126
- maxX: Math.max(...xs),
127
- maxY: Math.max(...ys)
128
- };
129
- }
130
- function closedPolygonCorners(subpath) {
131
- if (!subpath.closed) return;
132
- const corners = [{
133
- x: subpath.startXPt,
134
- y: subpath.startYPt
135
- }];
136
- for (const segment of subpath.segments) {
137
- if (segment.kind !== "line") return;
138
- corners.push({
139
- x: segment.xPt,
140
- y: segment.yPt
141
- });
142
- }
143
- const first = corners[0];
144
- const last = corners[corners.length - 1];
145
- if (first === void 0 || last === void 0 || corners.length < 2) return;
146
- const bounds = boundsOf(corners);
147
- if (nearlyEqualPoints(first, last, shapeTolerance(Math.max(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY)))) corners.pop();
148
- return corners;
149
- }
150
- const RECTANGLE_CORNER_COUNT = 4;
151
- function detectRect(subpath, paint) {
152
- const corners = closedPolygonCorners(subpath);
153
- if (corners?.length !== RECTANGLE_CORNER_COUNT) return;
154
- const { minX, minY, maxX, maxY } = boundsOf(corners);
155
- const widthPt = maxX - minX;
156
- const heightPt = maxY - minY;
157
- const tolX = shapeTolerance(widthPt);
158
- const tolY = shapeTolerance(heightPt);
159
- if (widthPt <= tolX || heightPt <= tolY) return;
160
- for (const corner of corners) {
161
- if (!nearlyEqual(corner.x, minX, tolX) && !nearlyEqual(corner.x, maxX, tolX)) return;
162
- if (!nearlyEqual(corner.y, minY, tolY) && !nearlyEqual(corner.y, maxY, tolY)) return;
163
- }
164
- for (let i = 0; i < corners.length; i += 1) {
165
- const from = corners[i];
166
- const to = corners[(i + 1) % corners.length];
167
- if (from === void 0 || to === void 0) return;
168
- if (nearlyEqual(from.x, to.x, tolX) === nearlyEqual(from.y, to.y, tolY)) return;
169
- }
170
- return {
171
- kind: "rect",
172
- xPt: minX,
173
- yPt: minY,
174
- widthPt,
175
- heightPt,
176
- fill: paint.fill,
177
- stroke: paint.stroke
178
- };
179
- }
180
- function cardinalExtremeOf(point, center, rx, ry, tolX, tolY) {
181
- if (nearlyEqual(point.y, center.y, tolY)) {
182
- if (nearlyEqual(point.x, center.x + rx, tolX)) return "right";
183
- if (nearlyEqual(point.x, center.x - rx, tolX)) return "left";
184
- return;
185
- }
186
- if (nearlyEqual(point.x, center.x, tolX)) {
187
- if (nearlyEqual(point.y, center.y + ry, tolY)) return "top";
188
- if (nearlyEqual(point.y, center.y - ry, tolY)) return "bottom";
189
- }
190
- }
191
- function expectedEllipseControl(atExtreme, atExtremeKind, otherEnd, center, kx, ky) {
192
- if (atExtremeKind === "right" || atExtremeKind === "left") return {
193
- x: atExtreme.x,
194
- y: center.y + Math.sign(otherEnd.y - center.y) * ky
195
- };
196
- return {
197
- x: center.x + Math.sign(otherEnd.x - center.x) * kx,
198
- y: atExtreme.y
199
- };
200
- }
201
- const ELLIPSE_SEGMENT_COUNT = 4;
202
- function detectEllipse(subpath, paint) {
203
- const segments = subpath.segments;
204
- if (!subpath.closed || segments.length !== ELLIPSE_SEGMENT_COUNT || segments.some((segment) => segment.kind !== "cubic")) return;
205
- const start = {
206
- x: subpath.startXPt,
207
- y: subpath.startYPt
208
- };
209
- const onCurve = [start];
210
- for (const segment of segments.slice(0, 3)) onCurve.push({
211
- x: segment.xPt,
212
- y: segment.yPt
213
- });
214
- const { minX, minY, maxX, maxY } = boundsOf(onCurve);
215
- const rx = (maxX - minX) / 2;
216
- const ry = (maxY - minY) / 2;
217
- const center = {
218
- x: (minX + maxX) / 2,
219
- y: (minY + maxY) / 2
220
- };
221
- const tolX = shapeTolerance(rx);
222
- const tolY = shapeTolerance(ry);
223
- if (rx <= tolX || ry <= tolY) return;
224
- const finalSegment = segments[3];
225
- if (finalSegment === void 0 || !nearlyEqualPoints({
226
- x: finalSegment.xPt,
227
- y: finalSegment.yPt
228
- }, start, Math.max(tolX, tolY))) return;
229
- const extremes = onCurve.map((point) => cardinalExtremeOf(point, center, rx, ry, tolX, tolY));
230
- if (extremes.some((extreme) => extreme === void 0) || new Set(extremes).size !== ELLIPSE_SEGMENT_COUNT) return;
231
- const kx = rx * BEZIER_KAPPA;
232
- const ky = ry * BEZIER_KAPPA;
233
- const controlTolerance = Math.max(tolX, tolY);
234
- for (let i = 0; i < ELLIPSE_SEGMENT_COUNT; i += 1) {
235
- const segment = segments[i];
236
- const from = onCurve[i];
237
- const fromKind = extremes[i];
238
- const to = onCurve[(i + 1) % ELLIPSE_SEGMENT_COUNT];
239
- const toKind = extremes[(i + 1) % ELLIPSE_SEGMENT_COUNT];
240
- if (segment?.kind !== "cubic" || from === void 0 || to === void 0 || fromKind === void 0 || toKind === void 0) return;
241
- const expectedC1 = expectedEllipseControl(from, fromKind, to, center, kx, ky);
242
- const expectedC2 = expectedEllipseControl(to, toKind, from, center, kx, ky);
243
- if (!nearlyEqualPoints({
244
- x: segment.c1xPt,
245
- y: segment.c1yPt
246
- }, expectedC1, controlTolerance)) return;
247
- if (!nearlyEqualPoints({
248
- x: segment.c2xPt,
249
- y: segment.c2yPt
250
- }, expectedC2, controlTolerance)) return;
251
- }
252
- return {
253
- kind: "ellipse",
254
- xPt: minX,
255
- yPt: minY,
256
- widthPt: maxX - minX,
257
- heightPt: maxY - minY,
258
- fill: paint.fill,
259
- stroke: paint.stroke
260
- };
261
- }
262
- function strokeStyleFromDashArray(dashArray) {
263
- if (dashArray.length === 0) return;
264
- return dashArray.length === 2 && dashArray[0] === 0 ? "dotted" : "dashed";
265
- }
266
- function detectLine(subpath, paint, style) {
267
- const segment = subpath.segments[0];
268
- if (subpath.closed || subpath.segments.length !== 1 || segment?.kind !== "line") return;
269
- if (paint.fill !== void 0 || paint.stroke === void 0) return;
270
- return {
271
- kind: "line",
272
- x1Pt: subpath.startXPt,
273
- y1Pt: subpath.startYPt,
274
- x2Pt: segment.xPt,
275
- y2Pt: segment.yPt,
276
- color: paint.stroke.color,
277
- widthPt: paint.stroke.widthPt,
278
- ...style !== void 0 ? { style } : {}
279
- };
280
- }
281
- function classifyShape(subpaths, paint, style) {
282
- const subpath = subpaths[0];
283
- if (subpaths.length !== 1 || subpath === void 0) return;
284
- return detectRect(subpath, paint) ?? detectEllipse(subpath, paint) ?? detectLine(subpath, paint, style);
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;
309
- function interpretContentStream(bytes, resources, context) {
310
- return runContentStream(bytes, resources, {
311
- ctm: IDENTITY_MATRIX,
312
- fillColor: COLOR_BLACK,
313
- strokeColor: COLOR_BLACK,
314
- lineWidth: DEFAULT_LINE_WIDTH_PT,
315
- dashArray: [],
316
- ...initialTextParameters()
317
- }, context, 0, new MarkedContentStack());
318
- }
319
- function lastPointOf(subpath) {
320
- const last = subpath.segments[subpath.segments.length - 1];
321
- return last === void 0 ? {
322
- x: subpath.startXPt,
323
- y: subpath.startYPt
324
- } : {
325
- x: last.xPt,
326
- y: last.yPt
327
- };
328
- }
329
- function runContentStream(bytes, resources, initialState, context, depth, markedContent) {
330
- const items = [];
331
- const operations = readContentStream(bytes, context.sink);
332
- const gsStack = [];
333
- let gs = initialState;
334
- let text = defaultTextObjectState();
335
- let pathSubpaths = [];
336
- let currentSubpath;
337
- const inScope = (key) => markedContent.inScope(key);
338
- const pushItem = (item) => {
339
- if (item.kind === "text") {
340
- const layer = item.layerName ?? inScope("layerName");
341
- const actualText = inScope("actualText");
342
- const alt = inScope("alt");
343
- const mcid = inScope("mcid");
344
- items.push({
345
- ...item,
346
- ...layer !== void 0 ? { layerName: layer } : {},
347
- ...actualText !== void 0 ? { actualText } : {},
348
- ...alt !== void 0 ? { alt } : {},
349
- ...mcid !== void 0 ? { mcid } : {}
350
- });
351
- return;
352
- }
353
- const layer = item.layerName ?? inScope("layerName");
354
- const layerStamped = layer === void 0 ? item : {
355
- ...item,
356
- layerName: layer
357
- };
358
- const mcid = inScope("mcid");
359
- items.push(mcid === void 0 ? layerStamped : {
360
- ...layerStamped,
361
- mcid
362
- });
363
- };
364
- const finalizeCurrentSubpath = () => {
365
- if (currentSubpath !== void 0) {
366
- pathSubpaths.push(currentSubpath);
367
- currentSubpath = void 0;
368
- }
369
- };
370
- const resetPath = () => {
371
- pathSubpaths = [];
372
- currentSubpath = void 0;
373
- };
374
- const appendRectSubpath = (operands, ctm) => {
375
- finalizeCurrentSubpath();
376
- const x = numAt(operands, 0);
377
- const y = numAt(operands, 1);
378
- const w = numAt(operands, 2);
379
- const h = numAt(operands, RE_HEIGHT_INDEX);
380
- const p1 = applyMatrix(ctm, {
381
- x,
382
- y
383
- });
384
- const p2 = applyMatrix(ctm, {
385
- x: x + w,
386
- y
387
- });
388
- const p3 = applyMatrix(ctm, {
389
- x: x + w,
390
- y: y + h
391
- });
392
- const p4 = applyMatrix(ctm, {
393
- x,
394
- y: y + h
395
- });
396
- pathSubpaths.push({
397
- startXPt: p1.x,
398
- startYPt: p1.y,
399
- segments: [
400
- {
401
- kind: "line",
402
- xPt: p2.x,
403
- yPt: p2.y
404
- },
405
- {
406
- kind: "line",
407
- xPt: p3.x,
408
- yPt: p3.y
409
- },
410
- {
411
- kind: "line",
412
- xPt: p4.x,
413
- yPt: p4.y
414
- }
415
- ],
416
- closed: true
417
- });
418
- };
419
- const paintFillRuleFor = (operator) => operator.endsWith("*") ? "evenodd" : "nonzero";
420
- const emitPaint = (operator) => {
421
- if (operator === "n") {
422
- resetPath();
423
- return;
424
- }
425
- if ((operator === "s" || operator === "b" || operator === "b*") && currentSubpath !== void 0) currentSubpath.closed = true;
426
- finalizeCurrentSubpath();
427
- if (pathSubpaths.length > 0) {
428
- const isFillOp = operator === "f" || operator === "F" || operator === "f*" || operator === "B" || operator === "B*" || operator === "b" || operator === "b*";
429
- const isStrokeOp = operator === "S" || operator === "s" || operator === "B" || operator === "B*" || operator === "b" || operator === "b*";
430
- const paint = {
431
- fill: isFillOp ? gs.fillColor : void 0,
432
- stroke: isStrokeOp ? {
433
- color: gs.strokeColor,
434
- widthPt: gs.lineWidth
435
- } : void 0
436
- };
437
- const strokeStyle = isStrokeOp ? strokeStyleFromDashArray(gs.dashArray) : void 0;
438
- pushItem(classifyShape(pathSubpaths, paint, strokeStyle) ?? {
439
- kind: "path",
440
- subpaths: pathSubpaths,
441
- fillRule: paintFillRuleFor(operator),
442
- ...paint,
443
- ...strokeStyle !== void 0 ? { style: strokeStyle } : {}
444
- });
445
- }
446
- resetPath();
447
- };
448
- const advanceThroughString = (codes) => {
449
- const fontResourceName = gs.fontResourceName;
450
- if (fontResourceName === void 0) return;
451
- let offset = 0;
452
- while (offset < codes.length) {
453
- const glyph = context.fontMetrics.glyphAdvance(fontResourceName, resources, codes, offset);
454
- if (glyph === void 0) context.sink({
455
- code: "pdf/font-not-resolved",
456
- severity: "warning",
457
- message: `could not resolve font resource /${fontResourceName} to compute a glyph advance; assuming a fallback width`
458
- });
459
- const widthPer1000 = glyph?.widthPer1000 ?? FALLBACK_GLYPH_WIDTH_PER_1000;
460
- const byteLength = glyph?.byteLengthConsumed ?? 1;
461
- const isSingleByteSpace = byteLength === 1 && codes[offset] === ASCII_SPACE;
462
- const spacing = gs.charSpace + (isSingleByteSpace ? gs.wordSpace : 0);
463
- const vertical = glyph?.vertical;
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);
465
- offset += byteLength;
466
- }
467
- };
468
- const positionVectorOf = (codes) => {
469
- const fontResourceName = gs.fontResourceName;
470
- if (fontResourceName === void 0) return;
471
- const vertical = context.fontMetrics.glyphAdvance(fontResourceName, resources, codes, 0)?.vertical;
472
- if (vertical === void 0) return;
473
- return {
474
- x: vertical.positionXPer1000 / GLYPH_SPACE_PER_TEXT_SPACE_UNIT,
475
- y: vertical.positionYPer1000 / GLYPH_SPACE_PER_TEXT_SPACE_UNIT
476
- };
477
- };
478
- const showTextArray = (elements) => {
479
- const fontResourceName = gs.fontResourceName;
480
- if (fontResourceName === void 0) return;
481
- const vertical = context.fontMetrics.isVertical(fontResourceName, resources);
482
- const startMatrix = computeTrm(gs, text);
483
- const chunks = [];
484
- let totalLength = 0;
485
- let firstGlyphPosition;
486
- for (const el of elements) if (el.kind === "string") {
487
- firstGlyphPosition ??= vertical ? positionVectorOf(el.bytes) : void 0;
488
- chunks.push(el.bytes);
489
- totalLength += el.bytes.length;
490
- advanceThroughString(el.bytes);
491
- } else if (el.kind === "number") {
492
- const adjustment = -(el.value / GLYPH_SPACE_PER_TEXT_SPACE_UNIT) * gs.fontSizePt;
493
- text.tm = multiplyMatrices(vertical ? translationMatrix(0, adjustment) : translationMatrix(adjustment * gs.horizScale, 0), text.tm);
494
- }
495
- if (totalLength === 0) return;
496
- const combined = new Uint8Array(totalLength);
497
- let at = 0;
498
- for (const chunk of chunks) {
499
- combined.set(chunk, at);
500
- at += chunk.length;
501
- }
502
- const endMatrix = computeTrm(gs, text);
503
- const shift = firstGlyphPosition === void 0 ? void 0 : translationMatrix(-firstGlyphPosition.x, -firstGlyphPosition.y);
504
- pushItem({
505
- kind: "text",
506
- codes: combined,
507
- fontResourceName,
508
- resources,
509
- startMatrix: shift === void 0 ? startMatrix : multiplyMatrices(shift, startMatrix),
510
- endMatrix: shift === void 0 ? endMatrix : multiplyMatrices(shift, endMatrix),
511
- sizePt: gs.fontSizePt,
512
- color: gs.fillColor,
513
- ...vertical ? { vertical: true } : {}
514
- });
515
- };
516
- const showText = (textBytes) => {
517
- showTextArray([{
518
- kind: "string",
519
- bytes: textBytes,
520
- hex: false
521
- }]);
522
- };
523
- const nextLine = () => {
524
- text.tlm = multiplyMatrices(translationMatrix(0, -gs.leading), text.tlm);
525
- text.tm = text.tlm;
526
- };
527
- const handleDo = (name) => {
528
- if (name === void 0) return;
529
- const xobjects = context.resolver.resolveDict(dictGet(resources, "XObject"));
530
- const xobj = xobjects !== void 0 ? context.resolver.resolve(dictGet(xobjects, name)) : void 0;
531
- if (xobj?.kind !== "stream") {
532
- context.sink({
533
- code: "pdf/xobject-not-resolved",
534
- severity: "warning",
535
- message: `XObject resource /${name} did not resolve to a stream`
536
- });
537
- return;
538
- }
539
- const subtype = asName(dictGet(xobj.dict, "Subtype"));
540
- const ownLayer = context.layerNameOf?.(dictGet(xobj.dict, "OC"));
541
- if (subtype === "Image") {
542
- pushItem({
543
- kind: "image",
544
- resourceName: name,
545
- resources,
546
- matrix: gs.ctm,
547
- ...ownLayer !== void 0 ? { layerName: ownLayer } : {}
548
- });
549
- return;
550
- }
551
- if (subtype === "Form") {
552
- if (depth >= MAX_FORM_XOBJECT_DEPTH) {
553
- context.sink({
554
- code: "pdf/form-recursion-limit",
555
- severity: "warning",
556
- message: "form XObject recursion exceeded the depth limit; skipping further nesting"
557
- });
558
- return;
559
- }
560
- const formMatrixArr = asArray(dictGet(xobj.dict, "Matrix"));
561
- const formMatrix = formMatrixArr !== void 0 ? matrixFromOperands(formMatrixArr) : IDENTITY_MATRIX;
562
- const formResources = context.resolver.resolveDict(dictGet(xobj.dict, "Resources")) ?? resources;
563
- const decoded = decodeStream(xobj.raw, xobj.dict, context.sink);
564
- const formState = {
565
- ...gs,
566
- ctm: multiplyMatrices(formMatrix, gs.ctm)
567
- };
568
- const formLayer = ownLayer ?? inScope("layerName");
569
- const formMcid = dictGet(xobj.dict, "StructParents") !== void 0 ? void 0 : inScope("mcid");
570
- for (const formItem of runContentStream(decoded.bytes, formResources, formState, context, depth + 1, new MarkedContentStack([{
571
- ...formLayer !== void 0 ? { layerName: formLayer } : {},
572
- ...formMcid !== void 0 ? { mcid: formMcid } : {}
573
- }]))) items.push(formItem);
574
- }
575
- };
576
- for (const token of operations) {
577
- if (token.kind === "inlineImage") {
578
- pushItem({
579
- kind: "inlineImage",
580
- dict: token.image.dict,
581
- data: token.image.data,
582
- matrix: gs.ctm
583
- });
584
- continue;
585
- }
586
- const { operands, operator } = token.operation;
587
- switch (operator) {
588
- case "q":
589
- gsStack.push(gs);
590
- break;
591
- case "Q":
592
- gs = gsStack.pop() ?? gs;
593
- break;
594
- case "cm":
595
- gs = {
596
- ...gs,
597
- ctm: multiplyMatrices(matrixFromOperands(operands), gs.ctm)
598
- };
599
- break;
600
- case "g":
601
- gs = {
602
- ...gs,
603
- fillColor: grayColor(numAt(operands, 0))
604
- };
605
- break;
606
- case "G":
607
- gs = {
608
- ...gs,
609
- strokeColor: grayColor(numAt(operands, 0))
610
- };
611
- break;
612
- case "rg":
613
- gs = {
614
- ...gs,
615
- fillColor: rgbColor(operands)
616
- };
617
- break;
618
- case "RG":
619
- gs = {
620
- ...gs,
621
- strokeColor: rgbColor(operands)
622
- };
623
- break;
624
- case "k":
625
- gs = {
626
- ...gs,
627
- fillColor: cmykColor(operands)
628
- };
629
- break;
630
- case "K":
631
- gs = {
632
- ...gs,
633
- strokeColor: cmykColor(operands)
634
- };
635
- break;
636
- case "sc":
637
- case "scn": {
638
- const color = genericColor(operands);
639
- if (color !== void 0) gs = {
640
- ...gs,
641
- fillColor: color
642
- };
643
- break;
644
- }
645
- case "SC":
646
- case "SCN": {
647
- const color = genericColor(operands);
648
- if (color !== void 0) gs = {
649
- ...gs,
650
- strokeColor: color
651
- };
652
- break;
653
- }
654
- case "re":
655
- appendRectSubpath(operands, gs.ctm);
656
- break;
657
- case "m": {
658
- finalizeCurrentSubpath();
659
- const p = applyMatrix(gs.ctm, {
660
- x: numAt(operands, 0),
661
- y: numAt(operands, 1)
662
- });
663
- currentSubpath = {
664
- startXPt: p.x,
665
- startYPt: p.y,
666
- segments: [],
667
- closed: false
668
- };
669
- break;
670
- }
671
- case "l": {
672
- const p = applyMatrix(gs.ctm, {
673
- x: numAt(operands, 0),
674
- y: numAt(operands, 1)
675
- });
676
- currentSubpath?.segments.push({
677
- kind: "line",
678
- xPt: p.x,
679
- yPt: p.y
680
- });
681
- break;
682
- }
683
- case "c": {
684
- const c1 = applyMatrix(gs.ctm, {
685
- x: numAt(operands, 0),
686
- y: numAt(operands, 1)
687
- });
688
- const c2 = applyMatrix(gs.ctm, {
689
- x: numAt(operands, 2),
690
- y: numAt(operands, OPERAND_INDEX_3)
691
- });
692
- const p = applyMatrix(gs.ctm, {
693
- x: numAt(operands, OPERAND_INDEX_4),
694
- y: numAt(operands, OPERAND_INDEX_5)
695
- });
696
- currentSubpath?.segments.push({
697
- kind: "cubic",
698
- c1xPt: c1.x,
699
- c1yPt: c1.y,
700
- c2xPt: c2.x,
701
- c2yPt: c2.y,
702
- xPt: p.x,
703
- yPt: p.y
704
- });
705
- break;
706
- }
707
- case "v":
708
- if (currentSubpath !== void 0) {
709
- const cur = lastPointOf(currentSubpath);
710
- const c2 = applyMatrix(gs.ctm, {
711
- x: numAt(operands, 0),
712
- y: numAt(operands, 1)
713
- });
714
- const p = applyMatrix(gs.ctm, {
715
- x: numAt(operands, 2),
716
- y: numAt(operands, OPERAND_INDEX_3)
717
- });
718
- currentSubpath.segments.push({
719
- kind: "cubic",
720
- c1xPt: cur.x,
721
- c1yPt: cur.y,
722
- c2xPt: c2.x,
723
- c2yPt: c2.y,
724
- xPt: p.x,
725
- yPt: p.y
726
- });
727
- }
728
- break;
729
- case "y": {
730
- const c1 = applyMatrix(gs.ctm, {
731
- x: numAt(operands, 0),
732
- y: numAt(operands, 1)
733
- });
734
- const p = applyMatrix(gs.ctm, {
735
- x: numAt(operands, 2),
736
- y: numAt(operands, OPERAND_INDEX_3)
737
- });
738
- currentSubpath?.segments.push({
739
- kind: "cubic",
740
- c1xPt: c1.x,
741
- c1yPt: c1.y,
742
- c2xPt: p.x,
743
- c2yPt: p.y,
744
- xPt: p.x,
745
- yPt: p.y
746
- });
747
- break;
748
- }
749
- case "h":
750
- if (currentSubpath !== void 0) currentSubpath.closed = true;
751
- break;
752
- case "w":
753
- gs = {
754
- ...gs,
755
- lineWidth: numAt(operands, 0)
756
- };
757
- break;
758
- case "d":
759
- gs = {
760
- ...gs,
761
- dashArray: (asArray(operands[0]) ?? []).map((entry) => asNumber(entry) ?? 0)
762
- };
763
- break;
764
- case "f":
765
- case "F":
766
- case "f*":
767
- case "S":
768
- case "s":
769
- case "B":
770
- case "B*":
771
- case "b":
772
- case "b*":
773
- case "n":
774
- emitPaint(operator);
775
- break;
776
- case "BT":
777
- text = defaultTextObjectState();
778
- break;
779
- case "Tf":
780
- gs = {
781
- ...gs,
782
- fontResourceName: asName(operands[0]),
783
- fontSizePt: numAt(operands, 1)
784
- };
785
- break;
786
- case "Tc":
787
- gs = {
788
- ...gs,
789
- charSpace: numAt(operands, 0)
790
- };
791
- break;
792
- case "Tw":
793
- gs = {
794
- ...gs,
795
- wordSpace: numAt(operands, 0)
796
- };
797
- break;
798
- case "Tz":
799
- gs = {
800
- ...gs,
801
- horizScale: numAt(operands, 0) / TZ_PERCENT_DIVISOR
802
- };
803
- break;
804
- case "TL":
805
- gs = {
806
- ...gs,
807
- leading: numAt(operands, 0)
808
- };
809
- break;
810
- case "Ts":
811
- gs = {
812
- ...gs,
813
- rise: numAt(operands, 0)
814
- };
815
- break;
816
- case "Td":
817
- text.tlm = multiplyMatrices(translationMatrix(numAt(operands, 0), numAt(operands, 1)), text.tlm);
818
- text.tm = text.tlm;
819
- break;
820
- case "TD":
821
- gs = {
822
- ...gs,
823
- leading: -numAt(operands, 1)
824
- };
825
- text.tlm = multiplyMatrices(translationMatrix(numAt(operands, 0), numAt(operands, 1)), text.tlm);
826
- text.tm = text.tlm;
827
- break;
828
- case "Tm":
829
- text.tlm = matrixFromOperands(operands);
830
- text.tm = text.tlm;
831
- break;
832
- case "T*":
833
- nextLine();
834
- break;
835
- case "Tj": {
836
- const str = operands[0];
837
- if (str?.kind === "string") showText(str.bytes);
838
- break;
839
- }
840
- case "'": {
841
- nextLine();
842
- const str = operands[0];
843
- if (str?.kind === "string") showText(str.bytes);
844
- break;
845
- }
846
- case "\"": {
847
- gs = {
848
- ...gs,
849
- wordSpace: numAt(operands, 0),
850
- charSpace: numAt(operands, 1)
851
- };
852
- nextLine();
853
- const str = operands[2];
854
- if (str?.kind === "string") showText(str.bytes);
855
- break;
856
- }
857
- case "TJ": {
858
- const array = asArray(operands[0]);
859
- if (array !== void 0) showTextArray(array);
860
- break;
861
- }
862
- case "Do":
863
- handleDo(asName(operands[0]));
864
- break;
865
- case "BDC": {
866
- const props = markedContentProperties(operands[1], resources, context.resolver);
867
- const layerName = context.layerNameOf?.(props === void 0 ? void 0 : dictGet(props, "OC"));
868
- const scopeProps = {
869
- ...layerName !== void 0 ? { layerName } : {},
870
- ...markedContentScopeProps(props)
871
- };
872
- markedContent.open(depth === 0 ? scopeProps : {
873
- ...scopeProps,
874
- mcid: void 0
875
- });
876
- break;
877
- }
878
- case "BMC":
879
- markedContent.open({});
880
- break;
881
- case "EMC": markedContent.close();
882
- }
883
- }
884
- return items;
885
- }
886
- //#endregion
887
- export { interpretContentStream };
1
+ import { n as numAt, t as interpretContentStream } from "./interpret-aWWhdIzr.js";
2
+ export { interpretContentStream, numAt };