pdf-codec 1.3.1 → 1.4.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.
- package/README.md +9 -0
- package/dist/afm-widths-BoeTOK2r.cjs +826 -0
- package/dist/afm-widths-Dxucrg7D.js +779 -0
- package/dist/afm-widths-DyDq56Ph.d.cts +19 -0
- package/dist/afm-widths-DyDq56Ph.d.ts +19 -0
- package/dist/afm-widths.cjs +4 -0
- package/dist/afm-widths.d.cts +2 -0
- package/dist/afm-widths.d.ts +2 -0
- package/dist/afm-widths.js +2 -0
- package/dist/assets/stix-two-math-font.cjs +5 -0
- package/dist/assets/stix-two-math-font.d.cts +4 -0
- package/dist/assets/stix-two-math-font.d.ts +4 -0
- package/dist/assets/stix-two-math-font.js +4 -0
- package/dist/bytes/crc32.cjs +21 -0
- package/dist/bytes/crc32.d.cts +4 -0
- package/dist/bytes/crc32.d.ts +4 -0
- package/dist/bytes/crc32.js +20 -0
- package/dist/bytes/flate.cjs +53 -0
- package/dist/bytes/flate.d.cts +12 -0
- package/dist/bytes/flate.d.ts +12 -0
- package/dist/bytes/flate.js +49 -0
- package/dist/bytes/reader.cjs +60 -0
- package/dist/bytes/reader.d.cts +20 -0
- package/dist/bytes/reader.d.ts +20 -0
- package/dist/bytes/reader.js +58 -0
- package/dist/bytes/writer.cjs +37 -0
- package/dist/bytes/writer.d.cts +13 -0
- package/dist/bytes/writer.d.ts +13 -0
- package/dist/bytes/writer.js +35 -0
- package/dist/cmap.cjs +110 -0
- package/dist/cmap.d.cts +8 -0
- package/dist/cmap.d.ts +8 -0
- package/dist/cmap.js +109 -0
- package/dist/codec.cjs +34 -0
- package/dist/codec.d.cts +167 -0
- package/dist/codec.d.ts +167 -0
- package/dist/codec.js +32 -0
- package/dist/content-read.cjs +126 -0
- package/dist/content-read.d.cts +21 -0
- package/dist/content-read.d.ts +21 -0
- package/dist/content-read.js +125 -0
- package/dist/content-write.cjs +138 -0
- package/dist/content-write.d.cts +24 -0
- package/dist/content-write.d.ts +24 -0
- package/dist/content-write.js +137 -0
- package/dist/diagnostics.cjs +21 -0
- package/dist/diagnostics.d.cts +19 -0
- package/dist/diagnostics.d.ts +19 -0
- package/dist/diagnostics.js +18 -0
- package/dist/document.cjs +162 -0
- package/dist/document.d.cts +12 -0
- package/dist/document.d.ts +12 -0
- package/dist/document.js +161 -0
- package/dist/encoding.cjs +5 -0
- package/dist/encoding.d.cts +6 -0
- package/dist/encoding.d.ts +6 -0
- package/dist/encoding.js +2 -0
- package/dist/filters.cjs +223 -0
- package/dist/filters.d.cts +14 -0
- package/dist/filters.d.ts +14 -0
- package/dist/filters.js +218 -0
- package/dist/font-read.cjs +199 -0
- package/dist/font-read.d.cts +23 -0
- package/dist/font-read.d.ts +23 -0
- package/dist/font-read.js +198 -0
- package/dist/font-style.cjs +35 -0
- package/dist/font-style.d.cts +14 -0
- package/dist/font-style.d.ts +14 -0
- package/dist/font-style.js +34 -0
- package/dist/fonts.cjs +124 -0
- package/dist/fonts.d.cts +15 -0
- package/dist/fonts.d.ts +15 -0
- package/dist/fonts.js +122 -0
- package/dist/formula.cjs +0 -0
- package/dist/formula.d.cts +10 -0
- package/dist/formula.d.ts +10 -0
- package/dist/formula.js +0 -0
- package/dist/image/jpeg-info.cjs +71 -0
- package/dist/image/jpeg-info.d.cts +12 -0
- package/dist/image/jpeg-info.d.ts +12 -0
- package/dist/image/jpeg-info.js +70 -0
- package/dist/image/png-decode.cjs +181 -0
- package/dist/image/png-decode.d.cts +14 -0
- package/dist/image/png-decode.d.ts +14 -0
- package/dist/image/png-decode.js +180 -0
- package/dist/image/png-encode.cjs +64 -0
- package/dist/image/png-encode.d.cts +8 -0
- package/dist/image/png-encode.d.ts +8 -0
- package/dist/image/png-encode.js +63 -0
- package/dist/image/png-filter.cjs +95 -0
- package/dist/image/png-filter.d.cts +6 -0
- package/dist/image/png-filter.d.ts +6 -0
- package/dist/image/png-filter.js +93 -0
- package/dist/images-read.cjs +250 -0
- package/dist/images-read.d.cts +13 -0
- package/dist/images-read.d.ts +13 -0
- package/dist/images-read.js +249 -0
- package/dist/index.cjs +45 -5912
- package/dist/index.d.cts +21 -483
- package/dist/index.d.ts +21 -483
- package/dist/index.js +18 -5885
- package/dist/interpret.cjs +587 -0
- package/dist/interpret.d.cts +84 -0
- package/dist/interpret.d.ts +84 -0
- package/dist/interpret.js +586 -0
- package/dist/lexer.cjs +180 -0
- package/dist/lexer.d.cts +29 -0
- package/dist/lexer.d.ts +29 -0
- package/dist/lexer.js +179 -0
- package/dist/math-cmap.cjs +81 -0
- package/dist/math-cmap.d.cts +6 -0
- package/dist/math-cmap.d.ts +6 -0
- package/dist/math-cmap.js +80 -0
- package/dist/math-content-write.cjs +83 -0
- package/dist/math-content-write.d.cts +11 -0
- package/dist/math-content-write.d.ts +11 -0
- package/dist/math-content-write.js +81 -0
- package/dist/math-font-write.cjs +100 -0
- package/dist/math-font-write.d.cts +19 -0
- package/dist/math-font-write.d.ts +19 -0
- package/dist/math-font-write.js +99 -0
- package/dist/math-font.cjs +103 -0
- package/dist/math-font.d.cts +25 -0
- package/dist/math-font.d.ts +25 -0
- package/dist/math-font.js +102 -0
- package/dist/math-hmtx.cjs +17 -0
- package/dist/math-hmtx.d.cts +8 -0
- package/dist/math-hmtx.d.ts +8 -0
- package/dist/math-hmtx.js +16 -0
- package/dist/math-table.cjs +117 -0
- package/dist/math-table.d.cts +45 -0
- package/dist/math-table.d.ts +45 -0
- package/dist/math-table.js +116 -0
- package/dist/math-types-Ba8qAf1p.d.cts +82 -0
- package/dist/math-types-Ba8qAf1p.d.ts +82 -0
- package/dist/math-types.cjs +0 -0
- package/dist/math-types.d.cts +2 -0
- package/dist/math-types.d.ts +2 -0
- package/dist/math-types.js +0 -0
- package/dist/matrix-B3c2_a2f.d.cts +18 -0
- package/dist/matrix-B3c2_a2f.d.ts +18 -0
- package/dist/matrix.cjs +93 -0
- package/dist/matrix.d.cts +2 -0
- package/dist/matrix.d.ts +2 -0
- package/dist/matrix.js +83 -0
- package/dist/measure.cjs +54 -0
- package/dist/measure.d.cts +20 -0
- package/dist/measure.d.ts +20 -0
- package/dist/measure.js +53 -0
- package/dist/objects.cjs +100 -0
- package/dist/objects.d.cts +58 -0
- package/dist/objects.d.ts +58 -0
- package/dist/objects.js +84 -0
- package/dist/parse.cjs +230 -0
- package/dist/parse.d.cts +13 -0
- package/dist/parse.d.ts +13 -0
- package/dist/parse.js +228 -0
- package/dist/predictors.cjs +64 -0
- package/dist/predictors.d.cts +13 -0
- package/dist/predictors.d.ts +13 -0
- package/dist/predictors.js +62 -0
- package/dist/read.cjs +418 -0
- package/dist/read.d.cts +21 -0
- package/dist/read.d.ts +21 -0
- package/dist/read.js +413 -0
- package/dist/serialize.cjs +69 -0
- package/dist/serialize.d.cts +8 -0
- package/dist/serialize.d.ts +8 -0
- package/dist/serialize.js +66 -0
- package/dist/sfnt.cjs +52 -0
- package/dist/sfnt.d.cts +17 -0
- package/dist/sfnt.d.ts +17 -0
- package/dist/sfnt.js +46 -0
- package/dist/text-layout.cjs +224 -0
- package/dist/text-layout.d.cts +37 -0
- package/dist/text-layout.d.ts +37 -0
- package/dist/text-layout.js +222 -0
- package/dist/util/abort.cjs +7 -0
- package/dist/util/abort.d.cts +4 -0
- package/dist/util/abort.d.ts +4 -0
- package/dist/util/abort.js +6 -0
- package/dist/util/base64.cjs +48 -0
- package/dist/util/base64.d.cts +5 -0
- package/dist/util/base64.d.ts +5 -0
- package/dist/util/base64.js +46 -0
- package/dist/winansi.cjs +5 -0
- package/dist/winansi.d.cts +20 -0
- package/dist/winansi.d.ts +20 -0
- package/dist/winansi.js +2 -0
- package/dist/write.cjs +421 -0
- package/dist/write.d.cts +16 -0
- package/dist/write.d.ts +16 -0
- package/dist/write.js +419 -0
- package/dist/xref.cjs +325 -0
- package/dist/xref.d.cts +21 -0
- package/dist/xref.d.ts +21 -0
- package/dist/xref.js +324 -0
- package/package.json +9 -1
package/dist/read.cjs
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_util_base64 = require("./util/base64.cjs");
|
|
3
|
+
const require_bytes_crc32 = require("./bytes/crc32.cjs");
|
|
4
|
+
const require_bytes_writer = require("./bytes/writer.cjs");
|
|
5
|
+
const require_diagnostics = require("./diagnostics.cjs");
|
|
6
|
+
const require_objects = require("./objects.cjs");
|
|
7
|
+
const require_filters = require("./filters.cjs");
|
|
8
|
+
const require_document = require("./document.cjs");
|
|
9
|
+
const require_util_abort = require("./util/abort.cjs");
|
|
10
|
+
const require_font_read = require("./font-read.cjs");
|
|
11
|
+
const require_images_read = require("./images-read.cjs");
|
|
12
|
+
const require_matrix = require("./matrix.cjs");
|
|
13
|
+
const require_interpret = require("./interpret.cjs");
|
|
14
|
+
require("./write.cjs");
|
|
15
|
+
let document_schema_js = require("document-schema.js");
|
|
16
|
+
//#region src/read.ts
|
|
17
|
+
const PDF_HEADER_BYTES = new TextEncoder().encode("%PDF-");
|
|
18
|
+
const HEADER_SEARCH_WINDOW = 1024;
|
|
19
|
+
const DEFAULT_PAGE_WIDTH_PT = 612;
|
|
20
|
+
const DEFAULT_PAGE_HEIGHT_PT = 792;
|
|
21
|
+
function hasPdfHeader(bytes) {
|
|
22
|
+
const window = bytes.subarray(0, Math.min(HEADER_SEARCH_WINDOW, bytes.length));
|
|
23
|
+
outer: for (let i = 0; i <= window.length - PDF_HEADER_BYTES.length; i++) {
|
|
24
|
+
for (let j = 0; j < PDF_HEADER_BYTES.length; j++) if (window[i + j] !== PDF_HEADER_BYTES[j]) continue outer;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
function readPdf(bytes, options) {
|
|
30
|
+
const sink = options?.sink ?? require_diagnostics.NOOP_DIAGNOSTIC_SINK;
|
|
31
|
+
const signal = options?.signal;
|
|
32
|
+
if (!hasPdfHeader(bytes)) throw new require_diagnostics.PdfParseError("pdf/no-header", "no \"%PDF-\" header found within the first bytes of the file; this does not look like a PDF at all");
|
|
33
|
+
const doc = require_document.openPdfDocument(bytes, sink);
|
|
34
|
+
const fontResolver = require_font_read.createFontResolver({
|
|
35
|
+
resolver: doc,
|
|
36
|
+
sink
|
|
37
|
+
});
|
|
38
|
+
const images = {};
|
|
39
|
+
const imageIdCache = /* @__PURE__ */ new Map();
|
|
40
|
+
const pages = doc.pages().map((pageDict) => {
|
|
41
|
+
require_util_abort.throwIfAborted(signal);
|
|
42
|
+
return readPage(pageDict, doc, fontResolver, images, imageIdCache, sink);
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
formatVersion: document_schema_js.LAYOUT_FORMAT_VERSION,
|
|
46
|
+
metadata: readMetadata(doc.trailer, doc),
|
|
47
|
+
pages,
|
|
48
|
+
images
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function readMediaBox(page) {
|
|
52
|
+
const arr = require_objects.asArray(require_objects.dictGet(page, "MediaBox"));
|
|
53
|
+
if (arr === void 0) return {
|
|
54
|
+
llx: 0,
|
|
55
|
+
lly: 0,
|
|
56
|
+
urx: DEFAULT_PAGE_WIDTH_PT,
|
|
57
|
+
ury: DEFAULT_PAGE_HEIGHT_PT
|
|
58
|
+
};
|
|
59
|
+
const a = require_objects.asNumber(arr[0]) ?? 0;
|
|
60
|
+
const b = require_objects.asNumber(arr[1]) ?? 0;
|
|
61
|
+
const c = require_objects.asNumber(arr[2]) ?? DEFAULT_PAGE_WIDTH_PT;
|
|
62
|
+
const d = require_objects.asNumber(arr[3]) ?? DEFAULT_PAGE_HEIGHT_PT;
|
|
63
|
+
return {
|
|
64
|
+
llx: Math.min(a, c),
|
|
65
|
+
lly: Math.min(b, d),
|
|
66
|
+
urx: Math.max(a, c),
|
|
67
|
+
ury: Math.max(b, d)
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function normalizeRotation(rotate) {
|
|
71
|
+
if (rotate === void 0) return 0;
|
|
72
|
+
const normalized = (Math.round(rotate / 90) * 90 % 360 + 360) % 360;
|
|
73
|
+
return normalized === 90 || normalized === 180 || normalized === 270 ? normalized : 0;
|
|
74
|
+
}
|
|
75
|
+
function pageRotationTransform(rotation, w, h) {
|
|
76
|
+
if (rotation === 90) return {
|
|
77
|
+
matrix: [
|
|
78
|
+
0,
|
|
79
|
+
-1,
|
|
80
|
+
1,
|
|
81
|
+
0,
|
|
82
|
+
0,
|
|
83
|
+
w
|
|
84
|
+
],
|
|
85
|
+
widthPt: h,
|
|
86
|
+
heightPt: w
|
|
87
|
+
};
|
|
88
|
+
if (rotation === 180) return {
|
|
89
|
+
matrix: [
|
|
90
|
+
-1,
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
-1,
|
|
94
|
+
w,
|
|
95
|
+
h
|
|
96
|
+
],
|
|
97
|
+
widthPt: w,
|
|
98
|
+
heightPt: h
|
|
99
|
+
};
|
|
100
|
+
if (rotation === 270) return {
|
|
101
|
+
matrix: [
|
|
102
|
+
0,
|
|
103
|
+
1,
|
|
104
|
+
-1,
|
|
105
|
+
0,
|
|
106
|
+
h,
|
|
107
|
+
0
|
|
108
|
+
],
|
|
109
|
+
widthPt: h,
|
|
110
|
+
heightPt: w
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
matrix: [
|
|
114
|
+
1,
|
|
115
|
+
0,
|
|
116
|
+
0,
|
|
117
|
+
1,
|
|
118
|
+
0,
|
|
119
|
+
0
|
|
120
|
+
],
|
|
121
|
+
widthPt: w,
|
|
122
|
+
heightPt: h
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function readPageContentBytes(page, resolver, sink) {
|
|
126
|
+
const contentsObj = resolver.resolve(require_objects.dictGet(page, "Contents"));
|
|
127
|
+
if (contentsObj?.kind === "stream") return require_filters.decodeStream(contentsObj.raw, contentsObj.dict, sink).bytes;
|
|
128
|
+
if (contentsObj?.kind === "array") {
|
|
129
|
+
const chunks = [];
|
|
130
|
+
for (const item of contentsObj.items) {
|
|
131
|
+
const streamObj = resolver.resolve(item);
|
|
132
|
+
if (streamObj?.kind === "stream") chunks.push(require_filters.decodeStream(streamObj.raw, streamObj.dict, sink).bytes, new Uint8Array([10]));
|
|
133
|
+
}
|
|
134
|
+
return require_bytes_writer.concatBytes(chunks);
|
|
135
|
+
}
|
|
136
|
+
return /* @__PURE__ */ new Uint8Array(0);
|
|
137
|
+
}
|
|
138
|
+
function readPage(page, resolver, fontResolver, images, imageIdCache, sink) {
|
|
139
|
+
const resources = resolver.resolveDict(require_objects.dictGet(page, "Resources"));
|
|
140
|
+
const mediaBox = readMediaBox(page);
|
|
141
|
+
const rotationResult = pageRotationTransform(normalizeRotation(require_objects.asNumber(require_objects.dictGet(page, "Rotate"))), mediaBox.urx - mediaBox.llx, mediaBox.ury - mediaBox.lly);
|
|
142
|
+
const pageMatrix = require_matrix.multiplyMatrices(require_matrix.translationMatrix(-mediaBox.llx, -mediaBox.lly), rotationResult.matrix);
|
|
143
|
+
const items = [];
|
|
144
|
+
if (resources !== void 0) {
|
|
145
|
+
const contentBytes = readPageContentBytes(page, resolver, sink);
|
|
146
|
+
const extracted = require_interpret.interpretContentStream(contentBytes, resources, {
|
|
147
|
+
fontMetrics: fontResolver.metrics,
|
|
148
|
+
resolver,
|
|
149
|
+
sink
|
|
150
|
+
});
|
|
151
|
+
for (const item of extracted) {
|
|
152
|
+
const converted = convertExtractedItem(item, pageMatrix, fontResolver, images, imageIdCache, resolver, sink);
|
|
153
|
+
if (converted !== void 0) items.push(converted);
|
|
154
|
+
}
|
|
155
|
+
} else sink({
|
|
156
|
+
code: "pdf/object-missing-value",
|
|
157
|
+
severity: "warning",
|
|
158
|
+
message: "page has no /Resources dict; its content stream cannot be interpreted"
|
|
159
|
+
});
|
|
160
|
+
items.push(...readLinkAnnotations(page, pageMatrix, resolver));
|
|
161
|
+
const notes = readPageNotes(page, resolver);
|
|
162
|
+
return {
|
|
163
|
+
widthPt: rotationResult.widthPt,
|
|
164
|
+
heightPt: rotationResult.heightPt,
|
|
165
|
+
items,
|
|
166
|
+
...notes !== void 0 ? { notes } : {}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function convertExtractedItem(item, pageMatrix, fontResolver, images, imageIdCache, resolver, sink) {
|
|
170
|
+
if (item.kind === "text") return convertText(item, pageMatrix, fontResolver);
|
|
171
|
+
if (item.kind === "rect") return convertRect(item, pageMatrix);
|
|
172
|
+
if (item.kind === "path") return convertPath(item, pageMatrix);
|
|
173
|
+
if (item.kind === "image") return convertImage(item, pageMatrix, images, imageIdCache, resolver, sink);
|
|
174
|
+
return convertInlineImage(item, pageMatrix, images, resolver, sink);
|
|
175
|
+
}
|
|
176
|
+
function convertText(item, pageMatrix, fontResolver) {
|
|
177
|
+
const font = fontResolver.resolve(item.fontResourceName, item.resources);
|
|
178
|
+
const text = font?.decodeToUnicode(item.codes) ?? "";
|
|
179
|
+
if (text.length === 0) return;
|
|
180
|
+
const startTrm = require_matrix.multiplyMatrices(item.startMatrix, pageMatrix);
|
|
181
|
+
const endTrm = require_matrix.multiplyMatrices(item.endMatrix, pageMatrix);
|
|
182
|
+
const widthPt = Math.hypot(endTrm[4] - startTrm[4], endTrm[5] - startTrm[5]);
|
|
183
|
+
const sizePt = require_matrix.matrixScaleX(startTrm);
|
|
184
|
+
const rotationDeg = require_matrix.matrixRotationDegrees(startTrm);
|
|
185
|
+
const layoutFont = {
|
|
186
|
+
family: font?.family ?? "Helvetica",
|
|
187
|
+
weight: font?.bold === true ? "bold" : "normal",
|
|
188
|
+
style: font?.italic === true ? "italic" : "normal"
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
kind: "text",
|
|
192
|
+
text,
|
|
193
|
+
xPt: startTrm[4],
|
|
194
|
+
yPt: startTrm[5],
|
|
195
|
+
font: layoutFont,
|
|
196
|
+
sizePt: sizePt > 0 ? sizePt : item.sizePt,
|
|
197
|
+
color: item.color,
|
|
198
|
+
widthPt,
|
|
199
|
+
rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function convertRect(item, pageMatrix) {
|
|
203
|
+
const p1 = require_matrix.applyMatrix(pageMatrix, {
|
|
204
|
+
x: item.xPt,
|
|
205
|
+
y: item.yPt
|
|
206
|
+
});
|
|
207
|
+
const p2 = require_matrix.applyMatrix(pageMatrix, {
|
|
208
|
+
x: item.xPt + item.widthPt,
|
|
209
|
+
y: item.yPt + item.heightPt
|
|
210
|
+
});
|
|
211
|
+
return {
|
|
212
|
+
kind: "rect",
|
|
213
|
+
xPt: Math.min(p1.x, p2.x),
|
|
214
|
+
yPt: Math.min(p1.y, p2.y),
|
|
215
|
+
widthPt: Math.abs(p2.x - p1.x),
|
|
216
|
+
heightPt: Math.abs(p2.y - p1.y),
|
|
217
|
+
fill: item.color
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function transformSubpath(subpath, pageMatrix) {
|
|
221
|
+
const start = require_matrix.applyMatrix(pageMatrix, {
|
|
222
|
+
x: subpath.startXPt,
|
|
223
|
+
y: subpath.startYPt
|
|
224
|
+
});
|
|
225
|
+
const segments = subpath.segments.map((segment) => {
|
|
226
|
+
if (segment.kind === "line") {
|
|
227
|
+
const p = require_matrix.applyMatrix(pageMatrix, {
|
|
228
|
+
x: segment.xPt,
|
|
229
|
+
y: segment.yPt
|
|
230
|
+
});
|
|
231
|
+
return {
|
|
232
|
+
kind: "line",
|
|
233
|
+
xPt: p.x,
|
|
234
|
+
yPt: p.y
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
const c1 = require_matrix.applyMatrix(pageMatrix, {
|
|
238
|
+
x: segment.c1xPt,
|
|
239
|
+
y: segment.c1yPt
|
|
240
|
+
});
|
|
241
|
+
const c2 = require_matrix.applyMatrix(pageMatrix, {
|
|
242
|
+
x: segment.c2xPt,
|
|
243
|
+
y: segment.c2yPt
|
|
244
|
+
});
|
|
245
|
+
const p = require_matrix.applyMatrix(pageMatrix, {
|
|
246
|
+
x: segment.xPt,
|
|
247
|
+
y: segment.yPt
|
|
248
|
+
});
|
|
249
|
+
return {
|
|
250
|
+
kind: "cubic",
|
|
251
|
+
c1xPt: c1.x,
|
|
252
|
+
c1yPt: c1.y,
|
|
253
|
+
c2xPt: c2.x,
|
|
254
|
+
c2yPt: c2.y,
|
|
255
|
+
xPt: p.x,
|
|
256
|
+
yPt: p.y
|
|
257
|
+
};
|
|
258
|
+
});
|
|
259
|
+
return {
|
|
260
|
+
startXPt: start.x,
|
|
261
|
+
startYPt: start.y,
|
|
262
|
+
segments,
|
|
263
|
+
closed: subpath.closed
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
function convertPath(item, pageMatrix) {
|
|
267
|
+
return {
|
|
268
|
+
kind: "path",
|
|
269
|
+
subpaths: item.subpaths.map((subpath) => transformSubpath(subpath, pageMatrix)),
|
|
270
|
+
...item.fill !== void 0 ? { fill: item.fill } : {},
|
|
271
|
+
...item.fill !== void 0 && item.fillRule === "evenodd" ? { fillRule: "evenodd" } : {},
|
|
272
|
+
...item.stroke !== void 0 ? { stroke: item.stroke } : {}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function imagePlacementFrom(matrix) {
|
|
276
|
+
const rotationDeg = require_matrix.matrixRotationDegrees(matrix);
|
|
277
|
+
return {
|
|
278
|
+
xPt: matrix[4],
|
|
279
|
+
yPt: matrix[5],
|
|
280
|
+
widthPt: require_matrix.matrixScaleX(matrix),
|
|
281
|
+
heightPt: require_matrix.matrixScaleY(matrix),
|
|
282
|
+
rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function registerExtractedImage(format, bytes, widthPx, heightPx, images) {
|
|
286
|
+
const imageId = `img${require_bytes_crc32.crc32(bytes).toString(16)}`;
|
|
287
|
+
if (!(imageId in images)) images[imageId] = {
|
|
288
|
+
format,
|
|
289
|
+
base64: require_util_base64.bytesToBase64(bytes),
|
|
290
|
+
widthPx,
|
|
291
|
+
heightPx
|
|
292
|
+
};
|
|
293
|
+
return imageId;
|
|
294
|
+
}
|
|
295
|
+
function resolveCachedImageId(dict, raw, images, cache, resolver, sink) {
|
|
296
|
+
if (cache.has(dict)) return cache.get(dict) ?? void 0;
|
|
297
|
+
const decoded = require_images_read.readImageXObject(dict, raw, resolver, sink);
|
|
298
|
+
if (decoded === void 0) {
|
|
299
|
+
cache.set(dict, null);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const imageId = registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images);
|
|
303
|
+
cache.set(dict, imageId);
|
|
304
|
+
return imageId;
|
|
305
|
+
}
|
|
306
|
+
function convertImage(item, pageMatrix, images, cache, resolver, sink) {
|
|
307
|
+
const xobjects = resolver.resolveDict(require_objects.dictGet(item.resources, "XObject"));
|
|
308
|
+
const xobj = xobjects !== void 0 ? resolver.resolve(require_objects.dictGet(xobjects, item.resourceName)) : void 0;
|
|
309
|
+
if (xobj?.kind !== "stream") return;
|
|
310
|
+
const imageId = resolveCachedImageId(xobj.dict, xobj.raw, images, cache, resolver, sink);
|
|
311
|
+
if (imageId === void 0) return;
|
|
312
|
+
return {
|
|
313
|
+
kind: "image",
|
|
314
|
+
imageId,
|
|
315
|
+
...imagePlacementFrom(require_matrix.multiplyMatrices(item.matrix, pageMatrix))
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function convertInlineImage(item, pageMatrix, images, resolver, sink) {
|
|
319
|
+
const decoded = require_images_read.readImageXObject(item.dict, item.data, resolver, sink);
|
|
320
|
+
if (decoded === void 0) return;
|
|
321
|
+
return {
|
|
322
|
+
kind: "image",
|
|
323
|
+
imageId: registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images),
|
|
324
|
+
...imagePlacementFrom(require_matrix.multiplyMatrices(item.matrix, pageMatrix))
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function readLinkAnnotations(page, pageMatrix, resolver) {
|
|
328
|
+
const annotsArr = require_objects.asArray(require_objects.dictGet(page, "Annots"));
|
|
329
|
+
if (annotsArr === void 0) return [];
|
|
330
|
+
const links = [];
|
|
331
|
+
for (const annotRef of annotsArr) {
|
|
332
|
+
const annot = resolver.resolveDict(annotRef);
|
|
333
|
+
if (annot === void 0 || require_objects.asName(require_objects.dictGet(annot, "Subtype")) !== "Link") continue;
|
|
334
|
+
const uri = readLinkUri(annot, resolver);
|
|
335
|
+
const rectArr = require_objects.asArray(require_objects.dictGet(annot, "Rect"));
|
|
336
|
+
if (uri === void 0 || rectArr === void 0) continue;
|
|
337
|
+
const x1 = require_objects.asNumber(rectArr[0]) ?? 0;
|
|
338
|
+
const y1 = require_objects.asNumber(rectArr[1]) ?? 0;
|
|
339
|
+
const x2 = require_objects.asNumber(rectArr[2]) ?? 0;
|
|
340
|
+
const y2 = require_objects.asNumber(rectArr[3]) ?? 0;
|
|
341
|
+
const p1 = require_matrix.applyMatrix(pageMatrix, {
|
|
342
|
+
x: Math.min(x1, x2),
|
|
343
|
+
y: Math.min(y1, y2)
|
|
344
|
+
});
|
|
345
|
+
const p2 = require_matrix.applyMatrix(pageMatrix, {
|
|
346
|
+
x: Math.max(x1, x2),
|
|
347
|
+
y: Math.max(y1, y2)
|
|
348
|
+
});
|
|
349
|
+
links.push({
|
|
350
|
+
kind: "link",
|
|
351
|
+
uri,
|
|
352
|
+
xPt: Math.min(p1.x, p2.x),
|
|
353
|
+
yPt: Math.min(p1.y, p2.y),
|
|
354
|
+
widthPt: Math.abs(p2.x - p1.x),
|
|
355
|
+
heightPt: Math.abs(p2.y - p1.y)
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
return links;
|
|
359
|
+
}
|
|
360
|
+
function readLinkUri(annot, resolver) {
|
|
361
|
+
const action = resolver.resolveDict(require_objects.dictGet(annot, "A"));
|
|
362
|
+
if (action === void 0 || require_objects.asName(require_objects.dictGet(action, "S")) !== "URI") return;
|
|
363
|
+
const uriObj = require_objects.dictGet(action, "URI");
|
|
364
|
+
return uriObj?.kind === "string" ? decodePdfString(uriObj.bytes) : void 0;
|
|
365
|
+
}
|
|
366
|
+
function readPageNotes(page, resolver) {
|
|
367
|
+
const annotsArr = require_objects.asArray(require_objects.dictGet(page, "Annots"));
|
|
368
|
+
if (annotsArr === void 0) return;
|
|
369
|
+
for (const annotRef of annotsArr) {
|
|
370
|
+
const annot = resolver.resolveDict(annotRef);
|
|
371
|
+
if (annot === void 0 || require_objects.asName(require_objects.dictGet(annot, "Subtype")) !== "Text") continue;
|
|
372
|
+
const titleObj = require_objects.dictGet(annot, "T");
|
|
373
|
+
if ((titleObj?.kind === "string" ? decodePdfString(titleObj.bytes) : void 0) !== "documents.js:notes") continue;
|
|
374
|
+
const contentsObj = require_objects.dictGet(annot, "Contents");
|
|
375
|
+
if (contentsObj?.kind === "string") return decodePdfString(contentsObj.bytes);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
function decodePdfString(bytes) {
|
|
379
|
+
if (bytes.length >= 2 && bytes[0] === 254 && bytes[1] === 255) {
|
|
380
|
+
let out = "";
|
|
381
|
+
for (let i = 2; i + 1 < bytes.length; i += 2) out += String.fromCharCode((bytes[i] ?? 0) << 8 | (bytes[i + 1] ?? 0));
|
|
382
|
+
return out;
|
|
383
|
+
}
|
|
384
|
+
return Array.from(bytes, (b) => String.fromCharCode(b)).join("");
|
|
385
|
+
}
|
|
386
|
+
const PDF_DATE_PATTERN = /^D:(\d{4})(\d{2})?(\d{2})?(\d{2})?(\d{2})?(\d{2})?([+\-Z])?(\d{2})?'?(\d{2})?'?$/;
|
|
387
|
+
function parsePdfDate(raw) {
|
|
388
|
+
if (raw === void 0) return;
|
|
389
|
+
const match = PDF_DATE_PATTERN.exec(raw);
|
|
390
|
+
if (match === null) return;
|
|
391
|
+
const [, year, month = "01", day = "01", hour = "00", minute = "00", second = "00", tzSign, tzHour = "00", tzMinute = "00"] = match;
|
|
392
|
+
return `${year}-${month}-${day}T${hour}:${minute}:${second}${tzSign === void 0 || tzSign === "Z" ? "Z" : `${tzSign}${tzHour}:${tzMinute}`}`;
|
|
393
|
+
}
|
|
394
|
+
function readMetadata(trailer, resolver) {
|
|
395
|
+
const info = resolver.resolveDict(require_objects.dictGet(trailer, "Info"));
|
|
396
|
+
if (info === void 0) return {};
|
|
397
|
+
const stringField = (key) => {
|
|
398
|
+
const obj = require_objects.dictGet(info, key);
|
|
399
|
+
return obj?.kind === "string" ? decodePdfString(obj.bytes) : void 0;
|
|
400
|
+
};
|
|
401
|
+
const keywords = stringField("Keywords")?.split(",").map((k) => k.trim()).filter((k) => k.length > 0);
|
|
402
|
+
return {
|
|
403
|
+
title: stringField("Title"),
|
|
404
|
+
author: stringField("Author"),
|
|
405
|
+
subject: stringField("Subject"),
|
|
406
|
+
keywords: keywords !== void 0 && keywords.length > 0 ? keywords : void 0,
|
|
407
|
+
creator: stringField("Creator"),
|
|
408
|
+
producer: stringField("Producer"),
|
|
409
|
+
createdIso: parsePdfDate(stringField("CreationDate")),
|
|
410
|
+
modifiedIso: parsePdfDate(stringField("ModDate"))
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
//#endregion
|
|
414
|
+
exports.decodePdfString = decodePdfString;
|
|
415
|
+
exports.normalizeRotation = normalizeRotation;
|
|
416
|
+
exports.pageRotationTransform = pageRotationTransform;
|
|
417
|
+
exports.parsePdfDate = parsePdfDate;
|
|
418
|
+
exports.readPdf = readPdf;
|
package/dist/read.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PdfDiagnosticSink } from "./diagnostics.cjs";
|
|
2
|
+
import { n as Matrix } from "./matrix-B3c2_a2f.cjs";
|
|
3
|
+
import { LayoutDocument } from "document-schema.js";
|
|
4
|
+
//#region src/read.d.ts
|
|
5
|
+
interface ReadPdfOptions {
|
|
6
|
+
readonly sink?: PdfDiagnosticSink;
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
}
|
|
9
|
+
declare function readPdf(bytes: Uint8Array<ArrayBuffer>, options?: ReadPdfOptions): LayoutDocument;
|
|
10
|
+
type PageRotation = 0 | 90 | 180 | 270;
|
|
11
|
+
declare function normalizeRotation(rotate: number | undefined): PageRotation;
|
|
12
|
+
interface PageRotationResult {
|
|
13
|
+
readonly matrix: Matrix;
|
|
14
|
+
readonly widthPt: number;
|
|
15
|
+
readonly heightPt: number;
|
|
16
|
+
}
|
|
17
|
+
declare function pageRotationTransform(rotation: PageRotation, w: number, h: number): PageRotationResult;
|
|
18
|
+
declare function decodePdfString(bytes: Uint8Array<ArrayBuffer>): string;
|
|
19
|
+
declare function parsePdfDate(raw: string | undefined): string | undefined;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ReadPdfOptions, decodePdfString, normalizeRotation, pageRotationTransform, parsePdfDate, readPdf };
|
package/dist/read.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PdfDiagnosticSink } from "./diagnostics.js";
|
|
2
|
+
import { n as Matrix } from "./matrix-B3c2_a2f.js";
|
|
3
|
+
import { LayoutDocument } from "document-schema.js";
|
|
4
|
+
//#region src/read.d.ts
|
|
5
|
+
interface ReadPdfOptions {
|
|
6
|
+
readonly sink?: PdfDiagnosticSink;
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
}
|
|
9
|
+
declare function readPdf(bytes: Uint8Array<ArrayBuffer>, options?: ReadPdfOptions): LayoutDocument;
|
|
10
|
+
type PageRotation = 0 | 90 | 180 | 270;
|
|
11
|
+
declare function normalizeRotation(rotate: number | undefined): PageRotation;
|
|
12
|
+
interface PageRotationResult {
|
|
13
|
+
readonly matrix: Matrix;
|
|
14
|
+
readonly widthPt: number;
|
|
15
|
+
readonly heightPt: number;
|
|
16
|
+
}
|
|
17
|
+
declare function pageRotationTransform(rotation: PageRotation, w: number, h: number): PageRotationResult;
|
|
18
|
+
declare function decodePdfString(bytes: Uint8Array<ArrayBuffer>): string;
|
|
19
|
+
declare function parsePdfDate(raw: string | undefined): string | undefined;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ReadPdfOptions, decodePdfString, normalizeRotation, pageRotationTransform, parsePdfDate, readPdf };
|