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
@@ -0,0 +1,295 @@
1
+ import "./notes-annotation-author.js";
2
+ import { asArray, asName, asNumber, dictGet } from "./objects.js";
3
+ import { decodePdfString } from "./pdf-text.js";
4
+ import { applyMatrix, matrixRotationDegrees, matrixScaleX, matrixScaleY, multiplyMatrices } from "./matrix.js";
5
+ import { crc32 } from "./bytes/crc32.js";
6
+ import { readImageXObject } from "./images-read.js";
7
+ import { bytesToBase64 } from "byte-codec";
8
+ //#region src/read-convert.ts
9
+ function convertExtractedItem(item, pageMatrix, fontResolver, images, imageIdCache, resolver, sink) {
10
+ if (item.kind === "text") return convertText(item, pageMatrix, fontResolver);
11
+ if (item.kind === "rect") return convertRect(item, pageMatrix);
12
+ if (item.kind === "ellipse") return convertEllipse(item, pageMatrix);
13
+ if (item.kind === "line") return convertLine(item, pageMatrix);
14
+ if (item.kind === "path") return convertPath(item, pageMatrix);
15
+ if (item.kind === "image") return convertImage(item, pageMatrix, images, imageIdCache, resolver, sink);
16
+ return convertInlineImage(item, pageMatrix, images, resolver, sink);
17
+ }
18
+ function convertText(item, pageMatrix, fontResolver) {
19
+ const font = fontResolver.resolve(item.fontResourceName, item.resources);
20
+ const text = font?.decodeToUnicode(item.codes) ?? "";
21
+ if (text.length === 0) return;
22
+ const startTrm = multiplyMatrices(item.startMatrix, pageMatrix);
23
+ const endTrm = multiplyMatrices(item.endMatrix, pageMatrix);
24
+ const widthPt = Math.hypot(endTrm[4] - startTrm[4], endTrm[5] - startTrm[5]);
25
+ const sizePt = matrixScaleX(startTrm);
26
+ const rotationDeg = matrixRotationDegrees(startTrm);
27
+ const layoutFont = {
28
+ family: font?.family ?? "Helvetica",
29
+ weight: font?.bold === true ? "bold" : "normal",
30
+ style: font?.italic === true ? "italic" : "normal"
31
+ };
32
+ return {
33
+ kind: "text",
34
+ text,
35
+ xPt: startTrm[4],
36
+ yPt: startTrm[5],
37
+ font: layoutFont,
38
+ sizePt: sizePt > 0 ? sizePt : item.sizePt,
39
+ color: item.color,
40
+ widthPt,
41
+ rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0,
42
+ ...item.vertical === true ? { writingMode: "vertical" } : {},
43
+ ...item.layerName !== void 0 ? { layer: item.layerName } : {},
44
+ ...item.actualText !== void 0 ? { actualText: item.actualText } : {},
45
+ ...item.alt !== void 0 ? { alt: item.alt } : {}
46
+ };
47
+ }
48
+ function paintFields(paint) {
49
+ return {
50
+ ...paint.fill !== void 0 ? { fill: paint.fill } : {},
51
+ ...paint.stroke !== void 0 ? { stroke: paint.stroke } : {}
52
+ };
53
+ }
54
+ function transformBox(item, pageMatrix) {
55
+ const p1 = applyMatrix(pageMatrix, {
56
+ x: item.xPt,
57
+ y: item.yPt
58
+ });
59
+ const p2 = applyMatrix(pageMatrix, {
60
+ x: item.xPt + item.widthPt,
61
+ y: item.yPt + item.heightPt
62
+ });
63
+ return {
64
+ xPt: Math.min(p1.x, p2.x),
65
+ yPt: Math.min(p1.y, p2.y),
66
+ widthPt: Math.abs(p2.x - p1.x),
67
+ heightPt: Math.abs(p2.y - p1.y)
68
+ };
69
+ }
70
+ function convertRect(item, pageMatrix) {
71
+ return {
72
+ kind: "rect",
73
+ ...transformBox(item, pageMatrix),
74
+ ...paintFields(item),
75
+ ...item.layerName !== void 0 ? { layer: item.layerName } : {}
76
+ };
77
+ }
78
+ function convertEllipse(item, pageMatrix) {
79
+ return {
80
+ kind: "ellipse",
81
+ ...transformBox(item, pageMatrix),
82
+ ...paintFields(item),
83
+ ...item.layerName !== void 0 ? { layer: item.layerName } : {}
84
+ };
85
+ }
86
+ function convertLine(item, pageMatrix) {
87
+ const p1 = applyMatrix(pageMatrix, {
88
+ x: item.x1Pt,
89
+ y: item.y1Pt
90
+ });
91
+ const p2 = applyMatrix(pageMatrix, {
92
+ x: item.x2Pt,
93
+ y: item.y2Pt
94
+ });
95
+ return {
96
+ kind: "line",
97
+ x1Pt: p1.x,
98
+ y1Pt: p1.y,
99
+ x2Pt: p2.x,
100
+ y2Pt: p2.y,
101
+ color: item.color,
102
+ widthPt: item.widthPt,
103
+ ...item.style !== void 0 ? { style: item.style } : {},
104
+ ...item.layerName !== void 0 ? { layer: item.layerName } : {}
105
+ };
106
+ }
107
+ function transformSubpath(subpath, pageMatrix) {
108
+ const start = applyMatrix(pageMatrix, {
109
+ x: subpath.startXPt,
110
+ y: subpath.startYPt
111
+ });
112
+ const segments = subpath.segments.map((segment) => {
113
+ if (segment.kind === "line") {
114
+ const p = applyMatrix(pageMatrix, {
115
+ x: segment.xPt,
116
+ y: segment.yPt
117
+ });
118
+ return {
119
+ kind: "line",
120
+ xPt: p.x,
121
+ yPt: p.y
122
+ };
123
+ }
124
+ const c1 = applyMatrix(pageMatrix, {
125
+ x: segment.c1xPt,
126
+ y: segment.c1yPt
127
+ });
128
+ const c2 = applyMatrix(pageMatrix, {
129
+ x: segment.c2xPt,
130
+ y: segment.c2yPt
131
+ });
132
+ const p = applyMatrix(pageMatrix, {
133
+ x: segment.xPt,
134
+ y: segment.yPt
135
+ });
136
+ return {
137
+ kind: "cubic",
138
+ c1xPt: c1.x,
139
+ c1yPt: c1.y,
140
+ c2xPt: c2.x,
141
+ c2yPt: c2.y,
142
+ xPt: p.x,
143
+ yPt: p.y
144
+ };
145
+ });
146
+ return {
147
+ startXPt: start.x,
148
+ startYPt: start.y,
149
+ segments,
150
+ closed: subpath.closed
151
+ };
152
+ }
153
+ function convertPath(item, pageMatrix) {
154
+ return {
155
+ kind: "path",
156
+ subpaths: item.subpaths.map((subpath) => transformSubpath(subpath, pageMatrix)),
157
+ ...item.fill !== void 0 ? { fill: item.fill } : {},
158
+ ...item.fill !== void 0 && item.fillRule === "evenodd" ? { fillRule: "evenodd" } : {},
159
+ ...item.stroke !== void 0 ? { stroke: item.stroke } : {},
160
+ ...item.style !== void 0 ? { style: item.style } : {},
161
+ ...item.layerName !== void 0 ? { layer: item.layerName } : {}
162
+ };
163
+ }
164
+ function imagePlacementFrom(matrix) {
165
+ const rotationDeg = matrixRotationDegrees(matrix);
166
+ return {
167
+ xPt: matrix[4],
168
+ yPt: matrix[5],
169
+ widthPt: matrixScaleX(matrix),
170
+ heightPt: matrixScaleY(matrix),
171
+ rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0
172
+ };
173
+ }
174
+ const HEX_RADIX = 16;
175
+ function registerExtractedImage(format, bytes, widthPx, heightPx, images, original) {
176
+ const imageId = `img${crc32(bytes).toString(HEX_RADIX)}`;
177
+ if (!(imageId in images)) images[imageId] = {
178
+ format,
179
+ base64: bytesToBase64(bytes),
180
+ widthPx,
181
+ heightPx,
182
+ ...original !== void 0 ? { original: {
183
+ filter: original.filter,
184
+ base64: bytesToBase64(original.bytes),
185
+ ...original.globalsBytes !== void 0 ? { jbig2GlobalsBase64: bytesToBase64(original.globalsBytes) } : {}
186
+ } } : {}
187
+ };
188
+ return imageId;
189
+ }
190
+ function resolveCachedImageId(dict, raw, images, cache, resolver, sink) {
191
+ if (cache.has(dict)) return cache.get(dict) ?? void 0;
192
+ const decoded = readImageXObject(dict, raw, resolver, sink);
193
+ if (decoded === void 0) {
194
+ cache.set(dict, null);
195
+ return;
196
+ }
197
+ const imageId = registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original);
198
+ cache.set(dict, imageId);
199
+ return imageId;
200
+ }
201
+ function convertImage(item, pageMatrix, images, cache, resolver, sink) {
202
+ const xobjects = resolver.resolveDict(dictGet(item.resources, "XObject"));
203
+ const xobj = xobjects !== void 0 ? resolver.resolve(dictGet(xobjects, item.resourceName)) : void 0;
204
+ if (xobj?.kind !== "stream") return;
205
+ const imageId = resolveCachedImageId(xobj.dict, xobj.raw, images, cache, resolver, sink);
206
+ if (imageId === void 0) return;
207
+ return {
208
+ kind: "image",
209
+ imageId,
210
+ ...imagePlacementFrom(multiplyMatrices(item.matrix, pageMatrix)),
211
+ ...item.layerName !== void 0 ? { layer: item.layerName } : {}
212
+ };
213
+ }
214
+ function convertInlineImage(item, pageMatrix, images, resolver, sink) {
215
+ const decoded = readImageXObject(item.dict, item.data, resolver, sink);
216
+ if (decoded === void 0) return;
217
+ return {
218
+ kind: "image",
219
+ imageId: registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original),
220
+ ...imagePlacementFrom(multiplyMatrices(item.matrix, pageMatrix)),
221
+ ...item.layerName !== void 0 ? { layer: item.layerName } : {}
222
+ };
223
+ }
224
+ function readLinkAnnotations(page, pageMatrix, resolver, destinationRegistry) {
225
+ const annotsArr = asArray(dictGet(page, "Annots"));
226
+ if (annotsArr === void 0) return [];
227
+ const links = [];
228
+ for (const annotRef of annotsArr) {
229
+ const annot = resolver.resolveDict(annotRef);
230
+ if (annot === void 0 || asName(dictGet(annot, "Subtype")) !== "Link") continue;
231
+ const rectArr = asArray(dictGet(annot, "Rect"));
232
+ if (rectArr === void 0) continue;
233
+ const uri = readLinkUri(annot, resolver);
234
+ const destination = uri === void 0 ? readInternalDestination(annot, resolver, destinationRegistry) : void 0;
235
+ if (uri === void 0 && destination === void 0) continue;
236
+ const x1 = asNumber(rectArr[0]) ?? 0;
237
+ const y1 = asNumber(rectArr[1]) ?? 0;
238
+ const x2 = asNumber(rectArr[2]) ?? 0;
239
+ const y2 = asNumber(rectArr[3]) ?? 0;
240
+ const p1 = applyMatrix(pageMatrix, {
241
+ x: Math.min(x1, x2),
242
+ y: Math.min(y1, y2)
243
+ });
244
+ const p2 = applyMatrix(pageMatrix, {
245
+ x: Math.max(x1, x2),
246
+ y: Math.max(y1, y2)
247
+ });
248
+ const contentsObj = dictGet(annot, "Contents");
249
+ const title = contentsObj?.kind === "string" ? decodePdfString(contentsObj.bytes) : void 0;
250
+ const box = {
251
+ xPt: Math.min(p1.x, p2.x),
252
+ yPt: Math.min(p1.y, p2.y),
253
+ widthPt: Math.abs(p2.x - p1.x),
254
+ heightPt: Math.abs(p2.y - p1.y),
255
+ ...title !== void 0 ? { title } : {}
256
+ };
257
+ if (uri !== void 0) links.push({
258
+ kind: "link",
259
+ uri,
260
+ ...box
261
+ });
262
+ else if (destination !== void 0) links.push({
263
+ kind: "internalLink",
264
+ destination,
265
+ ...box
266
+ });
267
+ }
268
+ return links;
269
+ }
270
+ function readLinkUri(annot, resolver) {
271
+ const action = resolver.resolveDict(dictGet(annot, "A"));
272
+ if (action === void 0 || asName(dictGet(action, "S")) !== "URI") return;
273
+ const uriObj = dictGet(action, "URI");
274
+ return uriObj?.kind === "string" ? decodePdfString(uriObj.bytes) : void 0;
275
+ }
276
+ function readInternalDestination(annot, resolver, destinationRegistry) {
277
+ const dest = dictGet(annot, "Dest");
278
+ if (dest !== void 0) return destinationRegistry.intern(dest);
279
+ const action = resolver.resolveDict(dictGet(annot, "A"));
280
+ if (action !== void 0 && asName(dictGet(action, "S")) === "GoTo") return destinationRegistry.intern(dictGet(action, "D"));
281
+ }
282
+ function readPageNotes(page, resolver) {
283
+ const annotsArr = asArray(dictGet(page, "Annots"));
284
+ if (annotsArr === void 0) return;
285
+ for (const annotRef of annotsArr) {
286
+ const annot = resolver.resolveDict(annotRef);
287
+ if (annot === void 0 || asName(dictGet(annot, "Subtype")) !== "Text") continue;
288
+ const titleObj = dictGet(annot, "T");
289
+ if ((titleObj?.kind === "string" ? decodePdfString(titleObj.bytes) : void 0) !== "documents.js:notes") continue;
290
+ const contentsObj = dictGet(annot, "Contents");
291
+ if (contentsObj?.kind === "string") return decodePdfString(contentsObj.bytes);
292
+ }
293
+ }
294
+ //#endregion
295
+ export { convertEllipse, convertExtractedItem, convertImage, convertLine, convertPath, convertRect, convertText, imagePlacementFrom, paintFields, readLinkAnnotations, readPageNotes, registerExtractedImage, resolveCachedImageId, transformBox, transformSubpath };
package/dist/read.cjs CHANGED
@@ -1,5 +1,4 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- require("./notes-annotation-author.cjs");
3
2
  const require_objects = require("./objects.cjs");
4
3
  const require_pdf_text = require("./pdf-text.cjs");
5
4
  const require_bytes_writer = require("./bytes/writer.cjs");
@@ -8,7 +7,7 @@ const require_matrix = require("./matrix.cjs");
8
7
  const require_annotations = require("./annotations.cjs");
9
8
  const require_filters = require("./filters.cjs");
10
9
  const require_attachments = require("./attachments.cjs");
11
- const require_bytes_crc32 = require("./bytes/crc32.cjs");
10
+ const require_read_convert = require("./read-convert.cjs");
12
11
  const require_form = require("./form.cjs");
13
12
  const require_xmp = require("./xmp.cjs");
14
13
  const require_optional_content = require("./optional-content.cjs");
@@ -18,10 +17,8 @@ const require_diagnostics = require("./diagnostics.cjs");
18
17
  const require_document = require("./document.cjs");
19
18
  const require_util_abort = require("./util/abort.cjs");
20
19
  const require_font_read = require("./font-read.cjs");
21
- const require_images_read = require("./images-read.cjs");
22
- const require_interpret = require("./interpret.cjs");
20
+ const require_interpret = require("./interpret-CqXdXgkY.cjs");
23
21
  const require_navigation = require("./navigation.cjs");
24
- let byte_codec = require("byte-codec");
25
22
  //#region src/read.ts
26
23
  const PDF_HEADER_BYTES = new TextEncoder().encode("%PDF-");
27
24
  const HEADER_SEARCH_WINDOW = 1024;
@@ -314,7 +311,7 @@ function readPage(pageIndex, page, resolver, fontResolver, images, imageIdCache,
314
311
  layerNameOf
315
312
  });
316
313
  for (const item of extracted) {
317
- const converted = convertExtractedItem(item, pageMatrix, fontResolver, images, imageIdCache, resolver, sink);
314
+ const converted = require_read_convert.convertExtractedItem(item, pageMatrix, fontResolver, images, imageIdCache, resolver, sink);
318
315
  if (converted === void 0 || !itemIntersectsVisibleRegion(converted, widthPt, heightPt)) continue;
319
316
  const owner = item.mcid === void 0 ? void 0 : structureOwnerOf(pageIndex, item.mcid);
320
317
  items.push(owner === void 0 ? converted : withStructure(converted, owner));
@@ -324,8 +321,8 @@ function readPage(pageIndex, page, resolver, fontResolver, images, imageIdCache,
324
321
  severity: "warning",
325
322
  message: "page has no /Resources dict; its content stream cannot be interpreted"
326
323
  });
327
- items.push(...readLinkAnnotations(page, pageMatrix, resolver, destinationRegistry));
328
- const notes = readPageNotes(page, resolver);
324
+ items.push(...require_read_convert.readLinkAnnotations(page, pageMatrix, resolver, destinationRegistry));
325
+ const notes = require_read_convert.readPageNotes(page, resolver);
329
326
  const annotations = require_annotations.readPageAnnotations(page, pageMatrix, resolver, sink);
330
327
  return {
331
328
  page: {
@@ -345,291 +342,6 @@ function withStructure(item, structure) {
345
342
  structure
346
343
  };
347
344
  }
348
- function convertExtractedItem(item, pageMatrix, fontResolver, images, imageIdCache, resolver, sink) {
349
- if (item.kind === "text") return convertText(item, pageMatrix, fontResolver);
350
- if (item.kind === "rect") return convertRect(item, pageMatrix);
351
- if (item.kind === "ellipse") return convertEllipse(item, pageMatrix);
352
- if (item.kind === "line") return convertLine(item, pageMatrix);
353
- if (item.kind === "path") return convertPath(item, pageMatrix);
354
- if (item.kind === "image") return convertImage(item, pageMatrix, images, imageIdCache, resolver, sink);
355
- return convertInlineImage(item, pageMatrix, images, resolver, sink);
356
- }
357
- function convertText(item, pageMatrix, fontResolver) {
358
- const font = fontResolver.resolve(item.fontResourceName, item.resources);
359
- const text = font?.decodeToUnicode(item.codes) ?? "";
360
- if (text.length === 0) return;
361
- const startTrm = require_matrix.multiplyMatrices(item.startMatrix, pageMatrix);
362
- const endTrm = require_matrix.multiplyMatrices(item.endMatrix, pageMatrix);
363
- const widthPt = Math.hypot(endTrm[4] - startTrm[4], endTrm[5] - startTrm[5]);
364
- const sizePt = require_matrix.matrixScaleX(startTrm);
365
- const rotationDeg = require_matrix.matrixRotationDegrees(startTrm);
366
- const layoutFont = {
367
- family: font?.family ?? "Helvetica",
368
- weight: font?.bold === true ? "bold" : "normal",
369
- style: font?.italic === true ? "italic" : "normal"
370
- };
371
- return {
372
- kind: "text",
373
- text,
374
- xPt: startTrm[4],
375
- yPt: startTrm[5],
376
- font: layoutFont,
377
- sizePt: sizePt > 0 ? sizePt : item.sizePt,
378
- color: item.color,
379
- widthPt,
380
- rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0,
381
- ...item.vertical === true ? { writingMode: "vertical" } : {},
382
- ...item.layerName !== void 0 ? { layer: item.layerName } : {},
383
- ...item.actualText !== void 0 ? { actualText: item.actualText } : {},
384
- ...item.alt !== void 0 ? { alt: item.alt } : {}
385
- };
386
- }
387
- function paintFields(paint) {
388
- return {
389
- ...paint.fill !== void 0 ? { fill: paint.fill } : {},
390
- ...paint.stroke !== void 0 ? { stroke: paint.stroke } : {}
391
- };
392
- }
393
- function transformBox(item, pageMatrix) {
394
- const p1 = require_matrix.applyMatrix(pageMatrix, {
395
- x: item.xPt,
396
- y: item.yPt
397
- });
398
- const p2 = require_matrix.applyMatrix(pageMatrix, {
399
- x: item.xPt + item.widthPt,
400
- y: item.yPt + item.heightPt
401
- });
402
- return {
403
- xPt: Math.min(p1.x, p2.x),
404
- yPt: Math.min(p1.y, p2.y),
405
- widthPt: Math.abs(p2.x - p1.x),
406
- heightPt: Math.abs(p2.y - p1.y)
407
- };
408
- }
409
- function convertRect(item, pageMatrix) {
410
- return {
411
- kind: "rect",
412
- ...transformBox(item, pageMatrix),
413
- ...paintFields(item),
414
- ...item.layerName !== void 0 ? { layer: item.layerName } : {}
415
- };
416
- }
417
- function convertEllipse(item, pageMatrix) {
418
- return {
419
- kind: "ellipse",
420
- ...transformBox(item, pageMatrix),
421
- ...paintFields(item),
422
- ...item.layerName !== void 0 ? { layer: item.layerName } : {}
423
- };
424
- }
425
- function convertLine(item, pageMatrix) {
426
- const p1 = require_matrix.applyMatrix(pageMatrix, {
427
- x: item.x1Pt,
428
- y: item.y1Pt
429
- });
430
- const p2 = require_matrix.applyMatrix(pageMatrix, {
431
- x: item.x2Pt,
432
- y: item.y2Pt
433
- });
434
- return {
435
- kind: "line",
436
- x1Pt: p1.x,
437
- y1Pt: p1.y,
438
- x2Pt: p2.x,
439
- y2Pt: p2.y,
440
- color: item.color,
441
- widthPt: item.widthPt,
442
- ...item.style !== void 0 ? { style: item.style } : {},
443
- ...item.layerName !== void 0 ? { layer: item.layerName } : {}
444
- };
445
- }
446
- function transformSubpath(subpath, pageMatrix) {
447
- const start = require_matrix.applyMatrix(pageMatrix, {
448
- x: subpath.startXPt,
449
- y: subpath.startYPt
450
- });
451
- const segments = subpath.segments.map((segment) => {
452
- if (segment.kind === "line") {
453
- const p = require_matrix.applyMatrix(pageMatrix, {
454
- x: segment.xPt,
455
- y: segment.yPt
456
- });
457
- return {
458
- kind: "line",
459
- xPt: p.x,
460
- yPt: p.y
461
- };
462
- }
463
- const c1 = require_matrix.applyMatrix(pageMatrix, {
464
- x: segment.c1xPt,
465
- y: segment.c1yPt
466
- });
467
- const c2 = require_matrix.applyMatrix(pageMatrix, {
468
- x: segment.c2xPt,
469
- y: segment.c2yPt
470
- });
471
- const p = require_matrix.applyMatrix(pageMatrix, {
472
- x: segment.xPt,
473
- y: segment.yPt
474
- });
475
- return {
476
- kind: "cubic",
477
- c1xPt: c1.x,
478
- c1yPt: c1.y,
479
- c2xPt: c2.x,
480
- c2yPt: c2.y,
481
- xPt: p.x,
482
- yPt: p.y
483
- };
484
- });
485
- return {
486
- startXPt: start.x,
487
- startYPt: start.y,
488
- segments,
489
- closed: subpath.closed
490
- };
491
- }
492
- function convertPath(item, pageMatrix) {
493
- return {
494
- kind: "path",
495
- subpaths: item.subpaths.map((subpath) => transformSubpath(subpath, pageMatrix)),
496
- ...item.fill !== void 0 ? { fill: item.fill } : {},
497
- ...item.fill !== void 0 && item.fillRule === "evenodd" ? { fillRule: "evenodd" } : {},
498
- ...item.stroke !== void 0 ? { stroke: item.stroke } : {},
499
- ...item.style !== void 0 ? { style: item.style } : {},
500
- ...item.layerName !== void 0 ? { layer: item.layerName } : {}
501
- };
502
- }
503
- function imagePlacementFrom(matrix) {
504
- const rotationDeg = require_matrix.matrixRotationDegrees(matrix);
505
- return {
506
- xPt: matrix[4],
507
- yPt: matrix[5],
508
- widthPt: require_matrix.matrixScaleX(matrix),
509
- heightPt: require_matrix.matrixScaleY(matrix),
510
- rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0
511
- };
512
- }
513
- const HEX_RADIX = 16;
514
- function registerExtractedImage(format, bytes, widthPx, heightPx, images, original) {
515
- const imageId = `img${require_bytes_crc32.crc32(bytes).toString(HEX_RADIX)}`;
516
- if (!(imageId in images)) images[imageId] = {
517
- format,
518
- base64: (0, byte_codec.bytesToBase64)(bytes),
519
- widthPx,
520
- heightPx,
521
- ...original !== void 0 ? { original: {
522
- filter: original.filter,
523
- base64: (0, byte_codec.bytesToBase64)(original.bytes),
524
- ...original.globalsBytes !== void 0 ? { jbig2GlobalsBase64: (0, byte_codec.bytesToBase64)(original.globalsBytes) } : {}
525
- } } : {}
526
- };
527
- return imageId;
528
- }
529
- function resolveCachedImageId(dict, raw, images, cache, resolver, sink) {
530
- if (cache.has(dict)) return cache.get(dict) ?? void 0;
531
- const decoded = require_images_read.readImageXObject(dict, raw, resolver, sink);
532
- if (decoded === void 0) {
533
- cache.set(dict, null);
534
- return;
535
- }
536
- const imageId = registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original);
537
- cache.set(dict, imageId);
538
- return imageId;
539
- }
540
- function convertImage(item, pageMatrix, images, cache, resolver, sink) {
541
- const xobjects = resolver.resolveDict(require_objects.dictGet(item.resources, "XObject"));
542
- const xobj = xobjects !== void 0 ? resolver.resolve(require_objects.dictGet(xobjects, item.resourceName)) : void 0;
543
- if (xobj?.kind !== "stream") return;
544
- const imageId = resolveCachedImageId(xobj.dict, xobj.raw, images, cache, resolver, sink);
545
- if (imageId === void 0) return;
546
- return {
547
- kind: "image",
548
- imageId,
549
- ...imagePlacementFrom(require_matrix.multiplyMatrices(item.matrix, pageMatrix)),
550
- ...item.layerName !== void 0 ? { layer: item.layerName } : {}
551
- };
552
- }
553
- function convertInlineImage(item, pageMatrix, images, resolver, sink) {
554
- const decoded = require_images_read.readImageXObject(item.dict, item.data, resolver, sink);
555
- if (decoded === void 0) return;
556
- return {
557
- kind: "image",
558
- imageId: registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original),
559
- ...imagePlacementFrom(require_matrix.multiplyMatrices(item.matrix, pageMatrix)),
560
- ...item.layerName !== void 0 ? { layer: item.layerName } : {}
561
- };
562
- }
563
- function readLinkAnnotations(page, pageMatrix, resolver, destinationRegistry) {
564
- const annotsArr = require_objects.asArray(require_objects.dictGet(page, "Annots"));
565
- if (annotsArr === void 0) return [];
566
- const links = [];
567
- for (const annotRef of annotsArr) {
568
- const annot = resolver.resolveDict(annotRef);
569
- if (annot === void 0 || require_objects.asName(require_objects.dictGet(annot, "Subtype")) !== "Link") continue;
570
- const rectArr = require_objects.asArray(require_objects.dictGet(annot, "Rect"));
571
- if (rectArr === void 0) continue;
572
- const uri = readLinkUri(annot, resolver);
573
- const destination = uri === void 0 ? readInternalDestination(annot, resolver, destinationRegistry) : void 0;
574
- if (uri === void 0 && destination === void 0) continue;
575
- const x1 = require_objects.asNumber(rectArr[0]) ?? 0;
576
- const y1 = require_objects.asNumber(rectArr[1]) ?? 0;
577
- const x2 = require_objects.asNumber(rectArr[2]) ?? 0;
578
- const y2 = require_objects.asNumber(rectArr[3]) ?? 0;
579
- const p1 = require_matrix.applyMatrix(pageMatrix, {
580
- x: Math.min(x1, x2),
581
- y: Math.min(y1, y2)
582
- });
583
- const p2 = require_matrix.applyMatrix(pageMatrix, {
584
- x: Math.max(x1, x2),
585
- y: Math.max(y1, y2)
586
- });
587
- const contentsObj = require_objects.dictGet(annot, "Contents");
588
- const title = contentsObj?.kind === "string" ? require_pdf_text.decodePdfString(contentsObj.bytes) : void 0;
589
- const box = {
590
- xPt: Math.min(p1.x, p2.x),
591
- yPt: Math.min(p1.y, p2.y),
592
- widthPt: Math.abs(p2.x - p1.x),
593
- heightPt: Math.abs(p2.y - p1.y),
594
- ...title !== void 0 ? { title } : {}
595
- };
596
- if (uri !== void 0) links.push({
597
- kind: "link",
598
- uri,
599
- ...box
600
- });
601
- else if (destination !== void 0) links.push({
602
- kind: "internalLink",
603
- destination,
604
- ...box
605
- });
606
- }
607
- return links;
608
- }
609
- function readLinkUri(annot, resolver) {
610
- const action = resolver.resolveDict(require_objects.dictGet(annot, "A"));
611
- if (action === void 0 || require_objects.asName(require_objects.dictGet(action, "S")) !== "URI") return;
612
- const uriObj = require_objects.dictGet(action, "URI");
613
- return uriObj?.kind === "string" ? require_pdf_text.decodePdfString(uriObj.bytes) : void 0;
614
- }
615
- function readInternalDestination(annot, resolver, destinationRegistry) {
616
- const dest = require_objects.dictGet(annot, "Dest");
617
- if (dest !== void 0) return destinationRegistry.intern(dest);
618
- const action = resolver.resolveDict(require_objects.dictGet(annot, "A"));
619
- if (action !== void 0 && require_objects.asName(require_objects.dictGet(action, "S")) === "GoTo") return destinationRegistry.intern(require_objects.dictGet(action, "D"));
620
- }
621
- function readPageNotes(page, resolver) {
622
- const annotsArr = require_objects.asArray(require_objects.dictGet(page, "Annots"));
623
- if (annotsArr === void 0) return;
624
- for (const annotRef of annotsArr) {
625
- const annot = resolver.resolveDict(annotRef);
626
- if (annot === void 0 || require_objects.asName(require_objects.dictGet(annot, "Subtype")) !== "Text") continue;
627
- const titleObj = require_objects.dictGet(annot, "T");
628
- if ((titleObj?.kind === "string" ? require_pdf_text.decodePdfString(titleObj.bytes) : void 0) !== "documents.js:notes") continue;
629
- const contentsObj = require_objects.dictGet(annot, "Contents");
630
- if (contentsObj?.kind === "string") return require_pdf_text.decodePdfString(contentsObj.bytes);
631
- }
632
- }
633
345
  function readMetadata(trailer, resolver, catalog, sink) {
634
346
  const info = resolver.resolveDict(require_objects.dictGet(trailer, "Info"));
635
347
  if (info === void 0) return {};
package/dist/read.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { PdfDiagnosticSink } from "./diagnostics.cjs";
1
+ import { i as PdfDiagnosticSink } from "./diagnostics-C9FxfZ6H.cjs";
2
2
  import { r as Matrix } from "./matrix-B7_SBKQ5.cjs";
3
3
  import { LayoutDocument } from "./layout.cjs";
4
4
  //#region src/read.d.ts
package/dist/read.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PdfDiagnosticSink } from "./diagnostics.js";
1
+ import { i as PdfDiagnosticSink } from "./diagnostics-C9FxfZ6H.js";
2
2
  import { r as Matrix } from "./matrix-B7_SBKQ5.js";
3
3
  import { LayoutDocument } from "./layout.js";
4
4
  //#region src/read.d.ts