pdf-codec 3.1.3 → 3.2.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 (92) hide show
  1. package/README.md +2 -2
  2. package/dist/annotations.cjs +104 -0
  3. package/dist/annotations.d.cts +9 -0
  4. package/dist/annotations.d.ts +9 -0
  5. package/dist/annotations.js +103 -0
  6. package/dist/attachments.cjs +65 -0
  7. package/dist/attachments.d.cts +8 -0
  8. package/dist/attachments.d.ts +8 -0
  9. package/dist/attachments.js +64 -0
  10. package/dist/bytes/flate.cjs +1 -1
  11. package/dist/bytes/flate.js +1 -1
  12. package/dist/codec.d.cts +109 -0
  13. package/dist/codec.d.ts +109 -0
  14. package/dist/content-read.cjs +1 -1
  15. package/dist/content-read.js +1 -1
  16. package/dist/content-write.cjs +2 -2
  17. package/dist/content-write.js +2 -2
  18. package/dist/document.cjs +12 -3
  19. package/dist/document.d.cts +2 -0
  20. package/dist/document.d.ts +2 -0
  21. package/dist/document.js +12 -3
  22. package/dist/embedded-font-write.cjs +1 -1
  23. package/dist/embedded-font-write.js +1 -1
  24. package/dist/encrypt.cjs +1 -1
  25. package/dist/encrypt.js +1 -1
  26. package/dist/filters.cjs +1 -1
  27. package/dist/filters.js +1 -1
  28. package/dist/font-read.cjs +1 -1
  29. package/dist/font-read.js +1 -1
  30. package/dist/font-registry.cjs +1 -1
  31. package/dist/font-registry.js +1 -1
  32. package/dist/form.cjs +138 -0
  33. package/dist/form.d.cts +9 -0
  34. package/dist/form.d.ts +9 -0
  35. package/dist/form.js +137 -0
  36. package/dist/image/png-decode.cjs +1 -1
  37. package/dist/image/png-decode.js +1 -1
  38. package/dist/image/png-encode.cjs +1 -1
  39. package/dist/image/png-encode.js +1 -1
  40. package/dist/index.cjs +12 -2
  41. package/dist/index.d.cts +3 -3
  42. package/dist/index.d.ts +3 -3
  43. package/dist/index.js +3 -3
  44. package/dist/interpret.cjs +70 -10
  45. package/dist/interpret.d.cts +9 -1
  46. package/dist/interpret.d.ts +9 -1
  47. package/dist/interpret.js +70 -10
  48. package/dist/layout.cjs +132 -2
  49. package/dist/layout.d.cts +335 -1
  50. package/dist/layout.d.ts +335 -1
  51. package/dist/layout.js +124 -4
  52. package/dist/math-content-write.cjs +1 -1
  53. package/dist/math-content-write.js +1 -1
  54. package/dist/math-font.cjs +1 -1
  55. package/dist/math-font.js +1 -1
  56. package/dist/names.cjs +41 -0
  57. package/dist/names.d.cts +11 -0
  58. package/dist/names.d.ts +11 -0
  59. package/dist/names.js +40 -0
  60. package/dist/navigation.cjs +210 -0
  61. package/dist/navigation.d.cts +18 -0
  62. package/dist/navigation.d.ts +18 -0
  63. package/dist/navigation.js +207 -0
  64. package/dist/optional-content.cjs +63 -0
  65. package/dist/optional-content.d.cts +12 -0
  66. package/dist/optional-content.d.ts +12 -0
  67. package/dist/optional-content.js +62 -0
  68. package/dist/parse.cjs +1 -1
  69. package/dist/parse.d.cts +1 -1
  70. package/dist/parse.d.ts +1 -1
  71. package/dist/parse.js +1 -1
  72. package/dist/pdf-text.cjs +21 -0
  73. package/dist/pdf-text.d.cts +5 -0
  74. package/dist/pdf-text.d.ts +5 -0
  75. package/dist/pdf-text.js +19 -0
  76. package/dist/read.cjs +128 -58
  77. package/dist/read.d.cts +2 -4
  78. package/dist/read.d.ts +2 -4
  79. package/dist/read.js +125 -53
  80. package/dist/serialize.cjs +5 -1
  81. package/dist/serialize.d.cts +3 -2
  82. package/dist/serialize.d.ts +3 -2
  83. package/dist/serialize.js +5 -2
  84. package/dist/write.cjs +51 -5
  85. package/dist/write.js +51 -5
  86. package/dist/xmp.cjs +46 -0
  87. package/dist/xmp.d.cts +14 -0
  88. package/dist/xmp.d.ts +14 -0
  89. package/dist/xmp.js +45 -0
  90. package/dist/xref.cjs +2 -2
  91. package/dist/xref.js +2 -2
  92. package/package.json +2 -2
package/dist/interpret.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { asArray, asName, asNumber, dictGet } from "./objects.js";
2
- import { decodeStream } from "./filters.js";
2
+ import { decodePdfString } from "./pdf-text.js";
3
3
  import { BEZIER_KAPPA, IDENTITY_MATRIX, applyMatrix, multiplyMatrices, translationMatrix } from "./matrix.js";
4
+ import { decodeStream } from "./filters.js";
4
5
  import { readContentStream } from "./content-read.js";
5
6
  import { COLOR_BLACK } from "document-schema.js";
6
7
  //#region src/interpret.ts
@@ -34,6 +35,22 @@ function computeTrm(ctm, ts) {
34
35
  function numAt(operands, index) {
35
36
  return asNumber(operands[index]) ?? 0;
36
37
  }
38
+ function markedContentProperties(operand, resources, resolver) {
39
+ if (operand?.kind === "dict") return operand;
40
+ if (operand?.kind === "name") {
41
+ const properties = resolver.resolveDict(dictGet(resources, "Properties"));
42
+ return properties === void 0 ? void 0 : resolver.resolveDict(dictGet(properties, operand.name));
43
+ }
44
+ }
45
+ function markedContentStrings(props) {
46
+ if (props === void 0) return {};
47
+ const actualTextObj = dictGet(props, "ActualText");
48
+ const altObj = dictGet(props, "Alt");
49
+ return {
50
+ ...actualTextObj?.kind === "string" ? { actualText: decodePdfString(actualTextObj.bytes) } : {},
51
+ ...altObj?.kind === "string" ? { alt: decodePdfString(altObj.bytes) } : {}
52
+ };
53
+ }
37
54
  function grayColor(value) {
38
55
  return {
39
56
  r: value,
@@ -252,7 +269,7 @@ function interpretContentStream(bytes, resources, context) {
252
269
  fillColor: COLOR_BLACK,
253
270
  strokeColor: COLOR_BLACK,
254
271
  lineWidth: DEFAULT_LINE_WIDTH_PT
255
- }, context, items, 0);
272
+ }, context, items, 0, []);
256
273
  return items;
257
274
  }
258
275
  function lastPointOf(subpath) {
@@ -265,13 +282,38 @@ function lastPointOf(subpath) {
265
282
  y: last.yPt
266
283
  };
267
284
  }
268
- function runContentStream(bytes, resources, initialState, context, items, depth) {
285
+ function runContentStream(bytes, resources, initialState, context, items, depth, markedContent) {
269
286
  const operations = readContentStream(bytes, context.sink);
270
287
  const gsStack = [];
271
288
  let gs = initialState;
272
289
  let ts = defaultTextState();
273
290
  let pathSubpaths = [];
274
291
  let currentSubpath;
292
+ const inScope = (key) => {
293
+ for (let i = markedContent.length - 1; i >= 0; i--) {
294
+ const value = markedContent[i]?.[key];
295
+ if (value !== void 0) return value;
296
+ }
297
+ };
298
+ const pushItem = (item) => {
299
+ if (item.kind === "text") {
300
+ const layer = item.layerName ?? inScope("layerName");
301
+ const actualText = inScope("actualText");
302
+ const alt = inScope("alt");
303
+ items.push({
304
+ ...item,
305
+ ...layer !== void 0 ? { layerName: layer } : {},
306
+ ...actualText !== void 0 ? { actualText } : {},
307
+ ...alt !== void 0 ? { alt } : {}
308
+ });
309
+ return;
310
+ }
311
+ const layer = item.layerName ?? inScope("layerName");
312
+ items.push(layer === void 0 ? item : {
313
+ ...item,
314
+ layerName: layer
315
+ });
316
+ };
275
317
  const finalizeCurrentSubpath = () => {
276
318
  if (currentSubpath !== void 0) {
277
319
  pathSubpaths.push(currentSubpath);
@@ -345,7 +387,7 @@ function runContentStream(bytes, resources, initialState, context, items, depth)
345
387
  widthPt: gs.lineWidth
346
388
  } : void 0
347
389
  };
348
- items.push(classifyShape(pathSubpaths, paint) ?? {
390
+ pushItem(classifyShape(pathSubpaths, paint) ?? {
349
391
  kind: "path",
350
392
  subpaths: pathSubpaths,
351
393
  fillRule: paintFillRuleFor(operator),
@@ -393,7 +435,7 @@ function runContentStream(bytes, resources, initialState, context, items, depth)
393
435
  at += chunk.length;
394
436
  }
395
437
  const endMatrix = computeTrm(gs.ctm, ts);
396
- items.push({
438
+ pushItem({
397
439
  kind: "text",
398
440
  codes: combined,
399
441
  fontResourceName: ts.fontResourceName,
@@ -428,12 +470,14 @@ function runContentStream(bytes, resources, initialState, context, items, depth)
428
470
  return;
429
471
  }
430
472
  const subtype = asName(dictGet(xobj.dict, "Subtype"));
473
+ const ownLayer = context.layerNameOf?.(dictGet(xobj.dict, "OC"));
431
474
  if (subtype === "Image") {
432
- items.push({
475
+ pushItem({
433
476
  kind: "image",
434
477
  resourceName: name,
435
478
  resources,
436
- matrix: gs.ctm
479
+ matrix: gs.ctm,
480
+ ...ownLayer !== void 0 ? { layerName: ownLayer } : {}
437
481
  });
438
482
  return;
439
483
  }
@@ -454,12 +498,13 @@ function runContentStream(bytes, resources, initialState, context, items, depth)
454
498
  ...gs,
455
499
  ctm: multiplyMatrices(formMatrix, gs.ctm)
456
500
  };
457
- runContentStream(decoded.bytes, formResources, formState, context, items, depth + 1);
501
+ const formLayer = ownLayer ?? inScope("layerName");
502
+ runContentStream(decoded.bytes, formResources, formState, context, items, depth + 1, [formLayer !== void 0 ? { layerName: formLayer } : {}]);
458
503
  }
459
504
  };
460
505
  for (const token of operations) {
461
506
  if (token.kind === "inlineImage") {
462
- items.push({
507
+ pushItem({
463
508
  kind: "inlineImage",
464
509
  dict: token.image.dict,
465
510
  data: token.image.data,
@@ -713,7 +758,22 @@ function runContentStream(bytes, resources, initialState, context, items, depth)
713
758
  if (array !== void 0) showTextArray(array);
714
759
  break;
715
760
  }
716
- case "Do": handleDo(asName(operands[0]));
761
+ case "Do":
762
+ handleDo(asName(operands[0]));
763
+ break;
764
+ case "BDC": {
765
+ const props = markedContentProperties(operands[1], resources, context.resolver);
766
+ const layerName = context.layerNameOf?.(props === void 0 ? void 0 : dictGet(props, "OC"));
767
+ markedContent.push({
768
+ ...layerName !== void 0 ? { layerName } : {},
769
+ ...markedContentStrings(props)
770
+ });
771
+ break;
772
+ }
773
+ case "BMC":
774
+ markedContent.push({});
775
+ break;
776
+ case "EMC": markedContent.pop();
717
777
  }
718
778
  }
719
779
  }
package/dist/layout.cjs CHANGED
@@ -14,6 +14,9 @@ const LayoutTextSchema = zod.z.object({
14
14
  widthPt: zod.z.number().nonnegative().optional(),
15
15
  rotationDeg: zod.z.number().optional(),
16
16
  underline: zod.z.boolean().optional(),
17
+ layer: zod.z.string().optional(),
18
+ actualText: zod.z.string().optional(),
19
+ alt: zod.z.string().optional(),
17
20
  sourcePath: zod.z.string().optional()
18
21
  });
19
22
  const LayoutImageSchema = zod.z.object({
@@ -24,6 +27,7 @@ const LayoutImageSchema = zod.z.object({
24
27
  widthPt: zod.z.number().positive(),
25
28
  heightPt: zod.z.number().positive(),
26
29
  rotationDeg: zod.z.number().optional(),
30
+ layer: zod.z.string().optional(),
27
31
  sourcePath: zod.z.string().optional()
28
32
  });
29
33
  const LayoutRectSchema = zod.z.object({
@@ -37,6 +41,7 @@ const LayoutRectSchema = zod.z.object({
37
41
  color: document_schema_js.ColorSchema,
38
42
  widthPt: zod.z.number().positive()
39
43
  }).optional(),
44
+ layer: zod.z.string().optional(),
40
45
  sourcePath: zod.z.string().optional()
41
46
  });
42
47
  const LayoutLineSchema = zod.z.object({
@@ -48,6 +53,7 @@ const LayoutLineSchema = zod.z.object({
48
53
  color: document_schema_js.ColorSchema,
49
54
  widthPt: zod.z.number().positive(),
50
55
  style: document_schema_js.ContentStrokeStyleSchema.optional(),
56
+ layer: zod.z.string().optional(),
51
57
  sourcePath: zod.z.string().optional()
52
58
  });
53
59
  const LayoutEllipseSchema = zod.z.object({
@@ -61,6 +67,7 @@ const LayoutEllipseSchema = zod.z.object({
61
67
  color: document_schema_js.ColorSchema,
62
68
  widthPt: zod.z.number().positive()
63
69
  }).optional(),
70
+ layer: zod.z.string().optional(),
64
71
  sourcePath: zod.z.string().optional()
65
72
  });
66
73
  const LayoutPathSegmentSchema = zod.z.discriminatedUnion("kind", [zod.z.object({
@@ -92,6 +99,7 @@ const LayoutPathSchema = zod.z.object({
92
99
  widthPt: zod.z.number().positive()
93
100
  }).optional(),
94
101
  style: document_schema_js.ContentStrokeStyleSchema.optional(),
102
+ layer: zod.z.string().optional(),
95
103
  sourcePath: zod.z.string().optional()
96
104
  });
97
105
  const LayoutLinkSchema = zod.z.object({
@@ -101,8 +109,18 @@ const LayoutLinkSchema = zod.z.object({
101
109
  yPt: zod.z.number(),
102
110
  widthPt: zod.z.number().nonnegative(),
103
111
  heightPt: zod.z.number().nonnegative(),
112
+ title: zod.z.string().optional(),
104
113
  sourcePath: zod.z.string().optional()
105
114
  });
115
+ const LayoutInternalLinkSchema = zod.z.object({
116
+ kind: zod.z.literal("internalLink"),
117
+ destination: zod.z.string(),
118
+ xPt: zod.z.number(),
119
+ yPt: zod.z.number(),
120
+ widthPt: zod.z.number().nonnegative(),
121
+ heightPt: zod.z.number().nonnegative(),
122
+ title: zod.z.string().optional()
123
+ });
106
124
  const LayoutItemSchema = zod.z.discriminatedUnion("kind", [
107
125
  LayoutTextSchema,
108
126
  LayoutImageSchema,
@@ -110,12 +128,44 @@ const LayoutItemSchema = zod.z.discriminatedUnion("kind", [
110
128
  LayoutLineSchema,
111
129
  LayoutEllipseSchema,
112
130
  LayoutPathSchema,
113
- LayoutLinkSchema
131
+ LayoutLinkSchema,
132
+ LayoutInternalLinkSchema
114
133
  ]);
134
+ const LayoutAnnotationQuadSchema = zod.z.tuple([
135
+ zod.z.object({
136
+ xPt: zod.z.number(),
137
+ yPt: zod.z.number()
138
+ }),
139
+ zod.z.object({
140
+ xPt: zod.z.number(),
141
+ yPt: zod.z.number()
142
+ }),
143
+ zod.z.object({
144
+ xPt: zod.z.number(),
145
+ yPt: zod.z.number()
146
+ }),
147
+ zod.z.object({
148
+ xPt: zod.z.number(),
149
+ yPt: zod.z.number()
150
+ })
151
+ ]);
152
+ const LayoutAnnotationSchema = zod.z.object({
153
+ subtype: zod.z.string(),
154
+ xPt: zod.z.number(),
155
+ yPt: zod.z.number(),
156
+ widthPt: zod.z.number().nonnegative(),
157
+ heightPt: zod.z.number().nonnegative(),
158
+ contents: zod.z.string().optional(),
159
+ author: zod.z.string().optional(),
160
+ modifiedIso: zod.z.string().optional(),
161
+ quads: zod.z.array(LayoutAnnotationQuadSchema).optional(),
162
+ source: document_schema_js.SourceResidueSchema.optional()
163
+ });
115
164
  const LayoutPageSchema = zod.z.object({
116
165
  widthPt: zod.z.number().positive(),
117
166
  heightPt: zod.z.number().positive(),
118
167
  items: zod.z.array(LayoutItemSchema),
168
+ annotations: zod.z.array(LayoutAnnotationSchema).optional(),
119
169
  notes: zod.z.string().optional()
120
170
  });
121
171
  const LayoutImageAssetSchema = zod.z.object({
@@ -124,21 +174,101 @@ const LayoutImageAssetSchema = zod.z.object({
124
174
  widthPx: zod.z.number().int().positive(),
125
175
  heightPx: zod.z.number().int().positive()
126
176
  });
177
+ const LayoutDestinationTargetSchema = zod.z.object({
178
+ kind: zod.z.enum([
179
+ "xyz",
180
+ "fit",
181
+ "fitH",
182
+ "fitV",
183
+ "fitR",
184
+ "fitB",
185
+ "fitBH",
186
+ "fitBV"
187
+ ]),
188
+ leftPt: zod.z.number().optional(),
189
+ topPt: zod.z.number().optional(),
190
+ bottomPt: zod.z.number().optional(),
191
+ rightPt: zod.z.number().optional(),
192
+ zoom: zod.z.number().optional()
193
+ });
194
+ const LayoutDestinationSchema = zod.z.object({
195
+ name: zod.z.string(),
196
+ pageIndex: zod.z.number().int().nonnegative(),
197
+ target: LayoutDestinationTargetSchema
198
+ });
199
+ const LayoutOutlineItemSchema = zod.z.lazy(() => zod.z.object({
200
+ title: zod.z.string(),
201
+ destination: zod.z.string().optional(),
202
+ children: zod.z.array(LayoutOutlineItemSchema)
203
+ }));
204
+ const LayoutAttachmentSchema = zod.z.object({
205
+ name: zod.z.string(),
206
+ description: zod.z.string().optional(),
207
+ mimeType: zod.z.string().optional(),
208
+ base64: zod.z.string()
209
+ });
210
+ const LayoutFormWidgetSchema = zod.z.object({
211
+ pageIndex: zod.z.number().int().nonnegative(),
212
+ xPt: zod.z.number(),
213
+ yPt: zod.z.number(),
214
+ widthPt: zod.z.number().nonnegative(),
215
+ heightPt: zod.z.number().nonnegative()
216
+ });
217
+ const LayoutFormFieldSchema = zod.z.lazy(() => zod.z.object({
218
+ name: zod.z.string(),
219
+ fieldType: zod.z.enum([
220
+ "text",
221
+ "checkbox",
222
+ "radio",
223
+ "button",
224
+ "listbox",
225
+ "combobox",
226
+ "signature",
227
+ "group"
228
+ ]),
229
+ value: zod.z.string().optional(),
230
+ checked: zod.z.boolean().optional(),
231
+ options: zod.z.array(zod.z.string()).optional(),
232
+ alias: zod.z.string().optional(),
233
+ readOnly: zod.z.boolean().optional(),
234
+ widgets: zod.z.array(LayoutFormWidgetSchema),
235
+ children: zod.z.array(LayoutFormFieldSchema)
236
+ }));
237
+ const LayoutLayerSchema = zod.z.object({
238
+ name: zod.z.string(),
239
+ visible: zod.z.boolean()
240
+ });
127
241
  const LayoutDocumentSchema = zod.z.object({
128
242
  formatVersion: zod.z.literal(1),
129
243
  metadata: document_schema_js.LayoutMetadataSchema,
130
244
  pages: zod.z.array(LayoutPageSchema),
131
- images: zod.z.record(zod.z.string(), LayoutImageAssetSchema)
245
+ images: zod.z.record(zod.z.string(), LayoutImageAssetSchema),
246
+ destinations: zod.z.array(LayoutDestinationSchema).optional(),
247
+ outline: zod.z.array(LayoutOutlineItemSchema).optional(),
248
+ attachments: zod.z.array(LayoutAttachmentSchema).optional(),
249
+ layers: zod.z.array(LayoutLayerSchema).optional(),
250
+ form: zod.z.array(LayoutFormFieldSchema).optional(),
251
+ source: zod.z.record(zod.z.string(), document_schema_js.SourceResidueSchema).optional()
132
252
  });
133
253
  //#endregion
134
254
  exports.LAYOUT_FORMAT_VERSION = LAYOUT_FORMAT_VERSION;
255
+ exports.LayoutAnnotationQuadSchema = LayoutAnnotationQuadSchema;
256
+ exports.LayoutAnnotationSchema = LayoutAnnotationSchema;
257
+ exports.LayoutAttachmentSchema = LayoutAttachmentSchema;
258
+ exports.LayoutDestinationSchema = LayoutDestinationSchema;
259
+ exports.LayoutDestinationTargetSchema = LayoutDestinationTargetSchema;
135
260
  exports.LayoutDocumentSchema = LayoutDocumentSchema;
136
261
  exports.LayoutEllipseSchema = LayoutEllipseSchema;
262
+ exports.LayoutFormFieldSchema = LayoutFormFieldSchema;
263
+ exports.LayoutFormWidgetSchema = LayoutFormWidgetSchema;
137
264
  exports.LayoutImageAssetSchema = LayoutImageAssetSchema;
138
265
  exports.LayoutImageSchema = LayoutImageSchema;
266
+ exports.LayoutInternalLinkSchema = LayoutInternalLinkSchema;
139
267
  exports.LayoutItemSchema = LayoutItemSchema;
268
+ exports.LayoutLayerSchema = LayoutLayerSchema;
140
269
  exports.LayoutLineSchema = LayoutLineSchema;
141
270
  exports.LayoutLinkSchema = LayoutLinkSchema;
271
+ exports.LayoutOutlineItemSchema = LayoutOutlineItemSchema;
142
272
  exports.LayoutPageSchema = LayoutPageSchema;
143
273
  exports.LayoutPathSchema = LayoutPathSchema;
144
274
  exports.LayoutPathSegmentSchema = LayoutPathSegmentSchema;