pdf-codec 1.3.0 → 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 +38 -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/winansi.cjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_afm_widths = require("./afm-widths-BoeTOK2r.cjs");
|
|
3
|
+
exports.encodeForShow = require_afm_widths.encodeForShow;
|
|
4
|
+
exports.sanitizeToWinAnsi = require_afm_widths.sanitizeToWinAnsi;
|
|
5
|
+
exports.winAnsiCodeToUnicode = require_afm_widths.winAnsiCodeToUnicode;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { r as StandardFontName } from "./afm-widths-DyDq56Ph.cjs";
|
|
2
|
+
//#region src/winansi.d.ts
|
|
3
|
+
declare function winAnsiCodeToUnicode(code: number): number | undefined;
|
|
4
|
+
interface WinAnsiSubstitution {
|
|
5
|
+
readonly from: string;
|
|
6
|
+
readonly to: string;
|
|
7
|
+
}
|
|
8
|
+
interface SanitizeResult {
|
|
9
|
+
readonly codes: Uint8Array<ArrayBuffer>;
|
|
10
|
+
readonly substitutions: readonly WinAnsiSubstitution[];
|
|
11
|
+
}
|
|
12
|
+
declare function sanitizeToWinAnsi(text: string): SanitizeResult;
|
|
13
|
+
interface EncodedShow {
|
|
14
|
+
readonly codes: Uint8Array<ArrayBuffer>;
|
|
15
|
+
readonly width1000: number;
|
|
16
|
+
readonly substitutions: readonly WinAnsiSubstitution[];
|
|
17
|
+
}
|
|
18
|
+
declare function encodeForShow(text: string, font: StandardFontName): EncodedShow;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { EncodedShow, SanitizeResult, WinAnsiSubstitution, encodeForShow, sanitizeToWinAnsi, winAnsiCodeToUnicode };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { r as StandardFontName } from "./afm-widths-DyDq56Ph.js";
|
|
2
|
+
//#region src/winansi.d.ts
|
|
3
|
+
declare function winAnsiCodeToUnicode(code: number): number | undefined;
|
|
4
|
+
interface WinAnsiSubstitution {
|
|
5
|
+
readonly from: string;
|
|
6
|
+
readonly to: string;
|
|
7
|
+
}
|
|
8
|
+
interface SanitizeResult {
|
|
9
|
+
readonly codes: Uint8Array<ArrayBuffer>;
|
|
10
|
+
readonly substitutions: readonly WinAnsiSubstitution[];
|
|
11
|
+
}
|
|
12
|
+
declare function sanitizeToWinAnsi(text: string): SanitizeResult;
|
|
13
|
+
interface EncodedShow {
|
|
14
|
+
readonly codes: Uint8Array<ArrayBuffer>;
|
|
15
|
+
readonly width1000: number;
|
|
16
|
+
readonly substitutions: readonly WinAnsiSubstitution[];
|
|
17
|
+
}
|
|
18
|
+
declare function encodeForShow(text: string, font: StandardFontName): EncodedShow;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { EncodedShow, SanitizeResult, WinAnsiSubstitution, encodeForShow, sanitizeToWinAnsi, winAnsiCodeToUnicode };
|
package/dist/winansi.js
ADDED
package/dist/write.cjs
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_afm_widths = require("./afm-widths-BoeTOK2r.cjs");
|
|
3
|
+
const require_util_base64 = require("./util/base64.cjs");
|
|
4
|
+
const require_bytes_writer = require("./bytes/writer.cjs");
|
|
5
|
+
const require_bytes_flate = require("./bytes/flate.cjs");
|
|
6
|
+
const require_objects = require("./objects.cjs");
|
|
7
|
+
const require_util_abort = require("./util/abort.cjs");
|
|
8
|
+
const require_fonts = require("./fonts.cjs");
|
|
9
|
+
const require_image_jpeg_info = require("./image/jpeg-info.cjs");
|
|
10
|
+
const require_image_png_decode = require("./image/png-decode.cjs");
|
|
11
|
+
const require_serialize = require("./serialize.cjs");
|
|
12
|
+
const require_content_write = require("./content-write.cjs");
|
|
13
|
+
const require_math_content_write = require("./math-content-write.cjs");
|
|
14
|
+
const require_math_font = require("./math-font.cjs");
|
|
15
|
+
const require_math_font_write = require("./math-font-write.cjs");
|
|
16
|
+
const require_measure = require("./measure.cjs");
|
|
17
|
+
//#region src/write.ts
|
|
18
|
+
const MATH_FONT_RESOURCE_NAME = "MF";
|
|
19
|
+
const FIRST_CHAR = 32;
|
|
20
|
+
const LAST_CHAR = 255;
|
|
21
|
+
const NOMINAL_STEM_V_REGULAR = 80;
|
|
22
|
+
const NOMINAL_STEM_V_BOLD = 120;
|
|
23
|
+
const FLAG_FIXED_PITCH = 1;
|
|
24
|
+
const FLAG_SERIF = 2;
|
|
25
|
+
const FLAG_NONSYMBOLIC = 32;
|
|
26
|
+
const FLAG_ITALIC = 64;
|
|
27
|
+
const FLAG_FORCE_BOLD = 262144;
|
|
28
|
+
function textToPdfString(text) {
|
|
29
|
+
const bytes = new Uint8Array(2 + text.length * 2);
|
|
30
|
+
bytes[0] = 254;
|
|
31
|
+
bytes[1] = 255;
|
|
32
|
+
for (let i = 0; i < text.length; i++) {
|
|
33
|
+
const code = text.charCodeAt(i);
|
|
34
|
+
bytes[2 + i * 2] = code >> 8 & 255;
|
|
35
|
+
bytes[2 + i * 2 + 1] = code & 255;
|
|
36
|
+
}
|
|
37
|
+
return require_objects.pdfHexString(bytes);
|
|
38
|
+
}
|
|
39
|
+
function pad2(n) {
|
|
40
|
+
return n.toString().padStart(2, "0");
|
|
41
|
+
}
|
|
42
|
+
function formatPdfDate(iso) {
|
|
43
|
+
const date = new Date(iso);
|
|
44
|
+
return `D:${date.getUTCFullYear()}${pad2(date.getUTCMonth() + 1)}${pad2(date.getUTCDate())}${pad2(date.getUTCHours())}${pad2(date.getUTCMinutes())}${pad2(date.getUTCSeconds())}Z`;
|
|
45
|
+
}
|
|
46
|
+
function buildInfoDict(doc) {
|
|
47
|
+
const entries = /* @__PURE__ */ new Map();
|
|
48
|
+
entries.set("Producer", textToPdfString("documents.js"));
|
|
49
|
+
if (doc.metadata.title !== void 0) entries.set("Title", textToPdfString(doc.metadata.title));
|
|
50
|
+
if (doc.metadata.author !== void 0) entries.set("Author", textToPdfString(doc.metadata.author));
|
|
51
|
+
if (doc.metadata.subject !== void 0) entries.set("Subject", textToPdfString(doc.metadata.subject));
|
|
52
|
+
if (doc.metadata.keywords !== void 0) entries.set("Keywords", textToPdfString(doc.metadata.keywords.join(", ")));
|
|
53
|
+
if (doc.metadata.creator !== void 0) entries.set("Creator", textToPdfString(doc.metadata.creator));
|
|
54
|
+
if (doc.metadata.createdIso !== void 0) entries.set("CreationDate", textToPdfString(formatPdfDate(doc.metadata.createdIso)));
|
|
55
|
+
if (doc.metadata.modifiedIso !== void 0) entries.set("ModDate", textToPdfString(formatPdfDate(doc.metadata.modifiedIso)));
|
|
56
|
+
return require_objects.pdfDict(entries);
|
|
57
|
+
}
|
|
58
|
+
function computeFontFlags(standardName, metrics) {
|
|
59
|
+
let flags = FLAG_NONSYMBOLIC;
|
|
60
|
+
if (standardName.startsWith("Courier")) flags |= FLAG_FIXED_PITCH;
|
|
61
|
+
if (standardName.startsWith("Times")) flags |= FLAG_SERIF;
|
|
62
|
+
if (metrics.italicAngle !== 0) flags |= FLAG_ITALIC;
|
|
63
|
+
if (standardName.includes("Bold")) flags |= FLAG_FORCE_BOLD;
|
|
64
|
+
return flags;
|
|
65
|
+
}
|
|
66
|
+
function widthForWidthsArray(standardName, code) {
|
|
67
|
+
if (require_afm_widths.STANDARD_METRICS[standardName].fixedWidth === void 0 && require_afm_widths.winAnsiGlyphName(code) === void 0) return 0;
|
|
68
|
+
return require_afm_widths.widthOfCode(standardName, code);
|
|
69
|
+
}
|
|
70
|
+
function buildFontObjects(standardName, descriptorRef) {
|
|
71
|
+
const metrics = require_afm_widths.STANDARD_METRICS[standardName];
|
|
72
|
+
const widths = [];
|
|
73
|
+
for (let code = FIRST_CHAR; code <= LAST_CHAR; code++) widths.push(require_objects.pdfNum(widthForWidthsArray(standardName, code)));
|
|
74
|
+
return {
|
|
75
|
+
font: require_objects.pdfDict({
|
|
76
|
+
Type: require_objects.pdfName("Font"),
|
|
77
|
+
Subtype: require_objects.pdfName("Type1"),
|
|
78
|
+
BaseFont: require_objects.pdfName(standardName),
|
|
79
|
+
Encoding: require_objects.pdfName("WinAnsiEncoding"),
|
|
80
|
+
FirstChar: require_objects.pdfNum(FIRST_CHAR),
|
|
81
|
+
LastChar: require_objects.pdfNum(LAST_CHAR),
|
|
82
|
+
Widths: require_objects.pdfArray(widths),
|
|
83
|
+
FontDescriptor: descriptorRef
|
|
84
|
+
}),
|
|
85
|
+
descriptor: require_objects.pdfDict({
|
|
86
|
+
Type: require_objects.pdfName("FontDescriptor"),
|
|
87
|
+
FontName: require_objects.pdfName(standardName),
|
|
88
|
+
Flags: require_objects.pdfNum(computeFontFlags(standardName, metrics)),
|
|
89
|
+
FontBBox: require_objects.pdfArray(metrics.fontBBox.map((n) => require_objects.pdfNum(n))),
|
|
90
|
+
ItalicAngle: require_objects.pdfNum(metrics.italicAngle),
|
|
91
|
+
Ascent: require_objects.pdfNum(metrics.ascender),
|
|
92
|
+
Descent: require_objects.pdfNum(metrics.descender),
|
|
93
|
+
CapHeight: require_objects.pdfNum(metrics.capHeight),
|
|
94
|
+
XHeight: require_objects.pdfNum(metrics.xHeight),
|
|
95
|
+
StemV: require_objects.pdfNum(standardName.includes("Bold") ? NOMINAL_STEM_V_BOLD : NOMINAL_STEM_V_REGULAR)
|
|
96
|
+
})
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function prepareJpegImage(bytes) {
|
|
100
|
+
const info = require_image_jpeg_info.readJpegInfo(bytes);
|
|
101
|
+
const colorSpace = info.components === 1 ? "DeviceGray" : info.components === 4 ? "DeviceCMYK" : "DeviceRGB";
|
|
102
|
+
const entries = /* @__PURE__ */ new Map([
|
|
103
|
+
["Type", require_objects.pdfName("XObject")],
|
|
104
|
+
["Subtype", require_objects.pdfName("Image")],
|
|
105
|
+
["Width", require_objects.pdfNum(info.width)],
|
|
106
|
+
["Height", require_objects.pdfNum(info.height)],
|
|
107
|
+
["ColorSpace", require_objects.pdfName(colorSpace)],
|
|
108
|
+
["BitsPerComponent", require_objects.pdfNum(info.precision)],
|
|
109
|
+
["Filter", require_objects.pdfName("DCTDecode")]
|
|
110
|
+
]);
|
|
111
|
+
if (info.components === 4 && (info.adobeTransform === 2 || info.adobeTransform === void 0)) entries.set("Decode", require_objects.pdfArray([
|
|
112
|
+
1,
|
|
113
|
+
0,
|
|
114
|
+
1,
|
|
115
|
+
0,
|
|
116
|
+
1,
|
|
117
|
+
0,
|
|
118
|
+
1,
|
|
119
|
+
0
|
|
120
|
+
].map((n) => require_objects.pdfNum(n))));
|
|
121
|
+
return {
|
|
122
|
+
dict: require_objects.pdfDict(entries),
|
|
123
|
+
raw: bytes
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function pngImageDict(width, height, colorSpace, compress) {
|
|
127
|
+
const entries = /* @__PURE__ */ new Map([
|
|
128
|
+
["Type", require_objects.pdfName("XObject")],
|
|
129
|
+
["Subtype", require_objects.pdfName("Image")],
|
|
130
|
+
["Width", require_objects.pdfNum(width)],
|
|
131
|
+
["Height", require_objects.pdfNum(height)],
|
|
132
|
+
["ColorSpace", require_objects.pdfName(colorSpace)],
|
|
133
|
+
["BitsPerComponent", require_objects.pdfNum(8)]
|
|
134
|
+
]);
|
|
135
|
+
if (compress) entries.set("Filter", require_objects.pdfName("FlateDecode"));
|
|
136
|
+
return require_objects.pdfDict(entries);
|
|
137
|
+
}
|
|
138
|
+
function preparePngImage(bytes, compress) {
|
|
139
|
+
const raw = require_image_png_decode.decodePng(bytes);
|
|
140
|
+
const colorSpace = raw.channels === 1 ? "DeviceGray" : "DeviceRGB";
|
|
141
|
+
return {
|
|
142
|
+
dict: pngImageDict(raw.width, raw.height, colorSpace, compress),
|
|
143
|
+
raw: compress ? require_bytes_flate.deflate(raw.data) : raw.data,
|
|
144
|
+
alpha: raw.alpha === void 0 ? void 0 : {
|
|
145
|
+
dict: pngImageDict(raw.width, raw.height, "DeviceGray", compress),
|
|
146
|
+
raw: compress ? require_bytes_flate.deflate(raw.alpha) : raw.alpha
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function prepareImage(asset, compress) {
|
|
151
|
+
const bytes = require_util_base64.base64ToBytes(asset.base64);
|
|
152
|
+
return asset.format === "jpeg" ? prepareJpegImage(bytes) : preparePngImage(bytes, compress);
|
|
153
|
+
}
|
|
154
|
+
function buildLinkAnnotDict(link) {
|
|
155
|
+
return require_objects.pdfDict({
|
|
156
|
+
Type: require_objects.pdfName("Annot"),
|
|
157
|
+
Subtype: require_objects.pdfName("Link"),
|
|
158
|
+
Rect: require_objects.pdfArray([
|
|
159
|
+
link.xPt,
|
|
160
|
+
link.yPt,
|
|
161
|
+
link.xPt + link.widthPt,
|
|
162
|
+
link.yPt + link.heightPt
|
|
163
|
+
].map((n) => require_objects.pdfNum(n))),
|
|
164
|
+
Border: require_objects.pdfArray([
|
|
165
|
+
0,
|
|
166
|
+
0,
|
|
167
|
+
0
|
|
168
|
+
].map((n) => require_objects.pdfNum(n))),
|
|
169
|
+
A: require_objects.pdfDict({
|
|
170
|
+
Type: require_objects.pdfName("Action"),
|
|
171
|
+
S: require_objects.pdfName("URI"),
|
|
172
|
+
URI: require_objects.pdfHexString(new TextEncoder().encode(link.uri))
|
|
173
|
+
})
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
function isLinkItem(item) {
|
|
177
|
+
return item.kind === "link";
|
|
178
|
+
}
|
|
179
|
+
const NOTES_ANNOTATION_HIDDEN_FLAG = 2;
|
|
180
|
+
const NOTES_ANNOTATION_AUTHOR = "documents.js:notes";
|
|
181
|
+
function buildNotesAnnotDict(notes) {
|
|
182
|
+
return require_objects.pdfDict({
|
|
183
|
+
Type: require_objects.pdfName("Annot"),
|
|
184
|
+
Subtype: require_objects.pdfName("Text"),
|
|
185
|
+
Rect: require_objects.pdfArray([
|
|
186
|
+
0,
|
|
187
|
+
0,
|
|
188
|
+
0,
|
|
189
|
+
0
|
|
190
|
+
].map((n) => require_objects.pdfNum(n))),
|
|
191
|
+
Contents: textToPdfString(notes),
|
|
192
|
+
T: textToPdfString(NOTES_ANNOTATION_AUTHOR),
|
|
193
|
+
F: require_objects.pdfNum(NOTES_ANNOTATION_HIDDEN_FLAG)
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
function xrefEntry(offset, generation, inUse) {
|
|
197
|
+
return `${offset.toString().padStart(10, "0")} ${generation.toString().padStart(5, "0")} ${inUse ? "n" : "f"} \n`;
|
|
198
|
+
}
|
|
199
|
+
function writePdf(doc, options = {}) {
|
|
200
|
+
const compress = options.compress ?? true;
|
|
201
|
+
const measurer = require_measure.createStandardFontMeasurer();
|
|
202
|
+
let nextObjNum = 1;
|
|
203
|
+
const catalogNum = nextObjNum++;
|
|
204
|
+
const pagesNum = nextObjNum++;
|
|
205
|
+
const infoNum = nextObjNum++;
|
|
206
|
+
const fontNames = /* @__PURE__ */ new Set();
|
|
207
|
+
const imageIds = /* @__PURE__ */ new Set();
|
|
208
|
+
for (const page of doc.pages) for (const item of page.items) if (item.kind === "text") fontNames.add(require_fonts.resolveStandardFont(item.font.family, item.font.weight === "bold", item.font.style === "italic").standardName);
|
|
209
|
+
else if (item.kind === "image") imageIds.add(item.imageId);
|
|
210
|
+
const fontAllocs = /* @__PURE__ */ new Map();
|
|
211
|
+
for (const [index, name] of [...fontNames].sort().entries()) {
|
|
212
|
+
const fontNum = nextObjNum++;
|
|
213
|
+
const descNum = nextObjNum++;
|
|
214
|
+
fontAllocs.set(name, {
|
|
215
|
+
fontNum,
|
|
216
|
+
descNum,
|
|
217
|
+
resourceName: `F${index + 1}`
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
const imageAllocs = /* @__PURE__ */ new Map();
|
|
221
|
+
for (const [index, imageId] of [...imageIds].sort().entries()) {
|
|
222
|
+
const asset = doc.images[imageId];
|
|
223
|
+
if (asset === void 0) throw new Error(`LayoutDocument references image "${imageId}" but it is not present in images`);
|
|
224
|
+
const prepared = prepareImage(asset, compress);
|
|
225
|
+
const imageNum = nextObjNum++;
|
|
226
|
+
const smaskNum = prepared.alpha === void 0 ? void 0 : nextObjNum++;
|
|
227
|
+
imageAllocs.set(imageId, {
|
|
228
|
+
imageNum,
|
|
229
|
+
smaskNum,
|
|
230
|
+
resourceName: `Im${index + 1}`,
|
|
231
|
+
prepared
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const formulas = options.formulas ?? [];
|
|
235
|
+
const mathFontAlloc = formulas.length === 0 ? void 0 : {
|
|
236
|
+
type0Num: nextObjNum++,
|
|
237
|
+
cidFontNum: nextObjNum++,
|
|
238
|
+
descriptorNum: nextObjNum++,
|
|
239
|
+
fontFileNum: nextObjNum++,
|
|
240
|
+
toUnicodeNum: nextObjNum++,
|
|
241
|
+
resourceName: MATH_FONT_RESOURCE_NAME
|
|
242
|
+
};
|
|
243
|
+
const pageAllocs = doc.pages.map(() => ({
|
|
244
|
+
pageNum: nextObjNum++,
|
|
245
|
+
contentsNum: nextObjNum++
|
|
246
|
+
}));
|
|
247
|
+
const objects = [];
|
|
248
|
+
objects.push({
|
|
249
|
+
num: catalogNum,
|
|
250
|
+
value: require_objects.pdfDict({
|
|
251
|
+
Type: require_objects.pdfName("Catalog"),
|
|
252
|
+
Pages: require_objects.pdfRef(pagesNum, 0)
|
|
253
|
+
})
|
|
254
|
+
});
|
|
255
|
+
objects.push({
|
|
256
|
+
num: pagesNum,
|
|
257
|
+
value: require_objects.pdfDict({
|
|
258
|
+
Type: require_objects.pdfName("Pages"),
|
|
259
|
+
Kids: require_objects.pdfArray(pageAllocs.map((p) => require_objects.pdfRef(p.pageNum, 0))),
|
|
260
|
+
Count: require_objects.pdfNum(doc.pages.length)
|
|
261
|
+
})
|
|
262
|
+
});
|
|
263
|
+
objects.push({
|
|
264
|
+
num: infoNum,
|
|
265
|
+
value: buildInfoDict(doc)
|
|
266
|
+
});
|
|
267
|
+
for (const [standardName, alloc] of fontAllocs) {
|
|
268
|
+
const { font, descriptor } = buildFontObjects(standardName, require_objects.pdfRef(alloc.descNum, 0));
|
|
269
|
+
objects.push({
|
|
270
|
+
num: alloc.fontNum,
|
|
271
|
+
value: font
|
|
272
|
+
});
|
|
273
|
+
objects.push({
|
|
274
|
+
num: alloc.descNum,
|
|
275
|
+
value: descriptor
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
for (const alloc of imageAllocs.values()) {
|
|
279
|
+
if (alloc.smaskNum !== void 0 && alloc.prepared.alpha !== void 0) {
|
|
280
|
+
alloc.prepared.dict.entries.set("SMask", require_objects.pdfRef(alloc.smaskNum, 0));
|
|
281
|
+
objects.push({
|
|
282
|
+
num: alloc.smaskNum,
|
|
283
|
+
value: require_objects.pdfStream(alloc.prepared.alpha.dict, alloc.prepared.alpha.raw)
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
objects.push({
|
|
287
|
+
num: alloc.imageNum,
|
|
288
|
+
value: require_objects.pdfStream(alloc.prepared.dict, alloc.prepared.raw)
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
const mathFont = mathFontAlloc === void 0 ? void 0 : require_math_font.loadMathFont().font;
|
|
292
|
+
const usedGlyphs = mathFontAlloc === void 0 || mathFont === void 0 ? void 0 : require_math_content_write.collectUsedGlyphs(formulas, mathFont);
|
|
293
|
+
if (mathFontAlloc !== void 0 && mathFont !== void 0 && usedGlyphs !== void 0) {
|
|
294
|
+
const built = require_math_font_write.buildMathFontObjects(mathFont, usedGlyphs, {
|
|
295
|
+
cidFontRef: require_objects.pdfRef(mathFontAlloc.cidFontNum, 0),
|
|
296
|
+
descriptorRef: require_objects.pdfRef(mathFontAlloc.descriptorNum, 0),
|
|
297
|
+
fontFileRef: require_objects.pdfRef(mathFontAlloc.fontFileNum, 0),
|
|
298
|
+
toUnicodeRef: require_objects.pdfRef(mathFontAlloc.toUnicodeNum, 0)
|
|
299
|
+
}, compress);
|
|
300
|
+
objects.push({
|
|
301
|
+
num: mathFontAlloc.type0Num,
|
|
302
|
+
value: built.type0
|
|
303
|
+
});
|
|
304
|
+
objects.push({
|
|
305
|
+
num: mathFontAlloc.cidFontNum,
|
|
306
|
+
value: built.cidFont
|
|
307
|
+
});
|
|
308
|
+
objects.push({
|
|
309
|
+
num: mathFontAlloc.descriptorNum,
|
|
310
|
+
value: built.descriptor
|
|
311
|
+
});
|
|
312
|
+
objects.push({
|
|
313
|
+
num: mathFontAlloc.fontFileNum,
|
|
314
|
+
value: built.fontFile
|
|
315
|
+
});
|
|
316
|
+
objects.push({
|
|
317
|
+
num: mathFontAlloc.toUnicodeNum,
|
|
318
|
+
value: built.toUnicode
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
const resourceEntries = /* @__PURE__ */ new Map();
|
|
322
|
+
if (fontAllocs.size > 0 || mathFontAlloc !== void 0) {
|
|
323
|
+
const fontEntries = new Map([...fontAllocs.values()].map((alloc) => [alloc.resourceName, require_objects.pdfRef(alloc.fontNum, 0)]));
|
|
324
|
+
if (mathFontAlloc !== void 0) fontEntries.set(mathFontAlloc.resourceName, require_objects.pdfRef(mathFontAlloc.type0Num, 0));
|
|
325
|
+
resourceEntries.set("Font", require_objects.pdfDict(fontEntries));
|
|
326
|
+
}
|
|
327
|
+
if (imageAllocs.size > 0) resourceEntries.set("XObject", require_objects.pdfDict(new Map([...imageAllocs.values()].map((alloc) => [alloc.resourceName, require_objects.pdfRef(alloc.imageNum, 0)]))));
|
|
328
|
+
const resourcesDict = require_objects.pdfDict(resourceEntries);
|
|
329
|
+
const formulasByPage = /* @__PURE__ */ new Map();
|
|
330
|
+
for (const formula of formulas) {
|
|
331
|
+
const forPage = formulasByPage.get(formula.pageIndex);
|
|
332
|
+
if (forPage === void 0) formulasByPage.set(formula.pageIndex, [formula]);
|
|
333
|
+
else forPage.push(formula);
|
|
334
|
+
}
|
|
335
|
+
const context = {
|
|
336
|
+
measurer,
|
|
337
|
+
resolveFont: (font) => {
|
|
338
|
+
const standardName = require_fonts.resolveStandardFont(font.family, font.weight === "bold", font.style === "italic").standardName;
|
|
339
|
+
const alloc = fontAllocs.get(standardName);
|
|
340
|
+
if (alloc === void 0) throw new Error(`font "${standardName}" was not pre-allocated -- this is a writePdf internal invariant violation`);
|
|
341
|
+
return {
|
|
342
|
+
resourceName: alloc.resourceName,
|
|
343
|
+
standardName
|
|
344
|
+
};
|
|
345
|
+
},
|
|
346
|
+
resolveImage: (imageId) => {
|
|
347
|
+
const alloc = imageAllocs.get(imageId);
|
|
348
|
+
if (alloc === void 0) throw new Error(`image "${imageId}" was not pre-allocated -- this is a writePdf internal invariant violation`);
|
|
349
|
+
return { resourceName: alloc.resourceName };
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
doc.pages.forEach((page, pageIndex) => {
|
|
353
|
+
require_util_abort.throwIfAborted(options.signal);
|
|
354
|
+
const { pageNum, contentsNum } = pageAllocs[pageIndex];
|
|
355
|
+
const { bytes: contentBytes, substitutions } = require_content_write.writeContentStream(page.items, context);
|
|
356
|
+
for (const substitution of substitutions) options.onSubstitution?.(substitution, { pageIndex });
|
|
357
|
+
const pageFormulas = formulasByPage.get(pageIndex);
|
|
358
|
+
const formulaBytes = pageFormulas === void 0 || mathFontAlloc === void 0 || mathFont === void 0 ? void 0 : require_math_content_write.writeFormulaContentStream(pageFormulas, {
|
|
359
|
+
font: mathFont,
|
|
360
|
+
resourceName: mathFontAlloc.resourceName
|
|
361
|
+
});
|
|
362
|
+
const combinedContentBytes = formulaBytes === void 0 ? contentBytes : require_bytes_writer.concatBytes([contentBytes, formulaBytes]);
|
|
363
|
+
const finalContentBytes = compress ? require_bytes_flate.deflate(combinedContentBytes) : combinedContentBytes;
|
|
364
|
+
const contentsDict = require_objects.pdfDict(compress ? { Filter: require_objects.pdfName("FlateDecode") } : {});
|
|
365
|
+
objects.push({
|
|
366
|
+
num: contentsNum,
|
|
367
|
+
value: require_objects.pdfStream(contentsDict, finalContentBytes)
|
|
368
|
+
});
|
|
369
|
+
const annots = page.items.filter(isLinkItem).map((link) => buildLinkAnnotDict(link));
|
|
370
|
+
if (page.notes !== void 0 && page.notes.length > 0) annots.push(buildNotesAnnotDict(page.notes));
|
|
371
|
+
const pageEntries = /* @__PURE__ */ new Map([
|
|
372
|
+
["Type", require_objects.pdfName("Page")],
|
|
373
|
+
["Parent", require_objects.pdfRef(pagesNum, 0)],
|
|
374
|
+
["MediaBox", require_objects.pdfArray([
|
|
375
|
+
0,
|
|
376
|
+
0,
|
|
377
|
+
page.widthPt,
|
|
378
|
+
page.heightPt
|
|
379
|
+
].map((n) => require_objects.pdfNum(n)))],
|
|
380
|
+
["Resources", resourcesDict],
|
|
381
|
+
["Contents", require_objects.pdfRef(contentsNum, 0)]
|
|
382
|
+
]);
|
|
383
|
+
if (annots.length > 0) pageEntries.set("Annots", require_objects.pdfArray(annots));
|
|
384
|
+
objects.push({
|
|
385
|
+
num: pageNum,
|
|
386
|
+
value: require_objects.pdfDict(pageEntries)
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
const writer = new require_bytes_writer.ByteWriter();
|
|
390
|
+
writer.writeAscii("%PDF-1.7\n");
|
|
391
|
+
const offsets = /* @__PURE__ */ new Map();
|
|
392
|
+
for (const { num, value } of objects) {
|
|
393
|
+
offsets.set(num, writer.length);
|
|
394
|
+
writer.writeAscii(`${num} 0 obj\n`);
|
|
395
|
+
require_serialize.writeObject(writer, value);
|
|
396
|
+
writer.writeAscii("\nendobj\n");
|
|
397
|
+
}
|
|
398
|
+
const maxObjNum = nextObjNum - 1;
|
|
399
|
+
const xrefOffset = writer.length;
|
|
400
|
+
writer.writeAscii("xref\n");
|
|
401
|
+
writer.writeAscii(`0 ${maxObjNum + 1}\n`);
|
|
402
|
+
writer.writeAscii(xrefEntry(0, 65535, false));
|
|
403
|
+
for (let num = 1; num <= maxObjNum; num++) {
|
|
404
|
+
const offset = offsets.get(num);
|
|
405
|
+
if (offset === void 0) throw new Error(`object ${num} was allocated but never written -- this is a writePdf internal invariant violation`);
|
|
406
|
+
writer.writeAscii(xrefEntry(offset, 0, true));
|
|
407
|
+
}
|
|
408
|
+
writer.writeAscii("trailer\n");
|
|
409
|
+
require_serialize.writeObject(writer, require_objects.pdfDict({
|
|
410
|
+
Size: require_objects.pdfNum(maxObjNum + 1),
|
|
411
|
+
Root: require_objects.pdfRef(catalogNum, 0),
|
|
412
|
+
Info: require_objects.pdfRef(infoNum, 0)
|
|
413
|
+
}));
|
|
414
|
+
writer.writeAscii("\nstartxref\n");
|
|
415
|
+
writer.writeAscii(`${xrefOffset}\n`);
|
|
416
|
+
writer.writeAscii("%%EOF");
|
|
417
|
+
return writer.toBytes();
|
|
418
|
+
}
|
|
419
|
+
//#endregion
|
|
420
|
+
exports.NOTES_ANNOTATION_AUTHOR = NOTES_ANNOTATION_AUTHOR;
|
|
421
|
+
exports.writePdf = writePdf;
|
package/dist/write.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WinAnsiSubstitution } from "./winansi.cjs";
|
|
2
|
+
import { PositionedFormula } from "./formula.cjs";
|
|
3
|
+
import { LayoutDocument } from "document-schema.js";
|
|
4
|
+
//#region src/write.d.ts
|
|
5
|
+
interface WritePdfOptions {
|
|
6
|
+
readonly compress?: boolean;
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
readonly onSubstitution?: (substitution: WinAnsiSubstitution, context: {
|
|
9
|
+
readonly pageIndex: number;
|
|
10
|
+
}) => void;
|
|
11
|
+
readonly formulas?: readonly PositionedFormula[];
|
|
12
|
+
}
|
|
13
|
+
declare const NOTES_ANNOTATION_AUTHOR = "documents.js:notes";
|
|
14
|
+
declare function writePdf(doc: LayoutDocument, options?: WritePdfOptions): Uint8Array<ArrayBuffer>;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { NOTES_ANNOTATION_AUTHOR, WritePdfOptions, writePdf };
|
package/dist/write.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WinAnsiSubstitution } from "./winansi.js";
|
|
2
|
+
import { PositionedFormula } from "./formula.js";
|
|
3
|
+
import { LayoutDocument } from "document-schema.js";
|
|
4
|
+
//#region src/write.d.ts
|
|
5
|
+
interface WritePdfOptions {
|
|
6
|
+
readonly compress?: boolean;
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
readonly onSubstitution?: (substitution: WinAnsiSubstitution, context: {
|
|
9
|
+
readonly pageIndex: number;
|
|
10
|
+
}) => void;
|
|
11
|
+
readonly formulas?: readonly PositionedFormula[];
|
|
12
|
+
}
|
|
13
|
+
declare const NOTES_ANNOTATION_AUTHOR = "documents.js:notes";
|
|
14
|
+
declare function writePdf(doc: LayoutDocument, options?: WritePdfOptions): Uint8Array<ArrayBuffer>;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { NOTES_ANNOTATION_AUTHOR, WritePdfOptions, writePdf };
|