@gerege-systems/eid-mongolia-sdk 0.15.4 → 0.17.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 +96 -21
- package/bin/convert-assets.mjs +79 -0
- package/bin/eid-convert.worker.js +18 -0
- package/dist/{browser-BZ3hvv6-.d.ts → browser-BPhROZkH.d.ts} +20 -49
- package/dist/{browser-8k_VDSP8.d.cts → browser-DFnRVCfN.d.cts} +20 -49
- package/dist/browser.cjs +11 -47
- package/dist/browser.d.cts +2 -2
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +4 -8
- package/dist/chunk-WOWGH65C.js +13 -0
- package/dist/{chunk-TB2KQH3W.js → chunk-Y3JYLJPG.js} +1 -5
- package/dist/convert.d.ts +113 -0
- package/dist/convert.js +1638 -0
- package/dist/{errors-Z7SgLVTr.d.cts → errors-pK1L0s4N.d.cts} +9 -9
- package/dist/{errors-Z7SgLVTr.d.ts → errors-pK1L0s4N.d.ts} +9 -9
- package/dist/index.cjs +13 -53
- package/dist/index.d.cts +9 -7
- package/dist/index.d.ts +9 -7
- package/dist/index.js +15 -19
- package/dist/pades.cjs +343 -46
- package/dist/pades.d.cts +19 -2
- package/dist/pades.d.ts +19 -2
- package/dist/pades.js +344 -47
- package/package.json +23 -4
- package/dist/chunk-REWNE7PO.js +0 -51
package/dist/convert.js
ADDED
|
@@ -0,0 +1,1638 @@
|
|
|
1
|
+
// src/pades/limits.ts
|
|
2
|
+
var MAX_INPUT_BYTES = (10 << 20) - (256 << 10);
|
|
3
|
+
|
|
4
|
+
// src/pades/pdf/inflate.ts
|
|
5
|
+
var LEN_BASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258];
|
|
6
|
+
var LEN_EXTRA = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0];
|
|
7
|
+
var DIST_BASE = [
|
|
8
|
+
1,
|
|
9
|
+
2,
|
|
10
|
+
3,
|
|
11
|
+
4,
|
|
12
|
+
5,
|
|
13
|
+
7,
|
|
14
|
+
9,
|
|
15
|
+
13,
|
|
16
|
+
17,
|
|
17
|
+
25,
|
|
18
|
+
33,
|
|
19
|
+
49,
|
|
20
|
+
65,
|
|
21
|
+
97,
|
|
22
|
+
129,
|
|
23
|
+
193,
|
|
24
|
+
257,
|
|
25
|
+
385,
|
|
26
|
+
513,
|
|
27
|
+
769,
|
|
28
|
+
1025,
|
|
29
|
+
1537,
|
|
30
|
+
2049,
|
|
31
|
+
3073,
|
|
32
|
+
4097,
|
|
33
|
+
6145,
|
|
34
|
+
8193,
|
|
35
|
+
12289,
|
|
36
|
+
16385,
|
|
37
|
+
24577
|
|
38
|
+
];
|
|
39
|
+
var DIST_EXTRA = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
|
|
40
|
+
var CL_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
|
|
41
|
+
function build(lengths, n) {
|
|
42
|
+
const counts = new Uint16Array(16);
|
|
43
|
+
const symbols = new Uint16Array(n);
|
|
44
|
+
for (let i = 0; i < n; i++) counts[lengths[i]]++;
|
|
45
|
+
counts[0] = 0;
|
|
46
|
+
const offs = new Uint16Array(16);
|
|
47
|
+
for (let i = 1; i < 16; i++) offs[i] = offs[i - 1] + counts[i - 1];
|
|
48
|
+
for (let i = 0; i < n; i++) if (lengths[i]) symbols[offs[lengths[i]]++] = i;
|
|
49
|
+
return { counts, symbols };
|
|
50
|
+
}
|
|
51
|
+
var fixedLit;
|
|
52
|
+
var fixedDist;
|
|
53
|
+
function fixed() {
|
|
54
|
+
if (!fixedLit || !fixedDist) {
|
|
55
|
+
const l = new Uint8Array(288);
|
|
56
|
+
l.fill(8, 0, 144);
|
|
57
|
+
l.fill(9, 144, 256);
|
|
58
|
+
l.fill(7, 256, 280);
|
|
59
|
+
l.fill(8, 280, 288);
|
|
60
|
+
fixedLit = build(l, 288);
|
|
61
|
+
fixedDist = build(new Uint8Array(30).fill(5), 30);
|
|
62
|
+
}
|
|
63
|
+
return [fixedLit, fixedDist];
|
|
64
|
+
}
|
|
65
|
+
var InflateError = class extends Error {
|
|
66
|
+
};
|
|
67
|
+
var Truncated = class extends InflateError {
|
|
68
|
+
};
|
|
69
|
+
function zlibInflate(src, limit = 64 << 20) {
|
|
70
|
+
if (src.length < 2) throw new InflateError("zlib: header \u0430\u043B\u0433\u0430");
|
|
71
|
+
const cmf = src[0];
|
|
72
|
+
const flg = src[1];
|
|
73
|
+
if ((cmf & 15) !== 8 || cmf >> 4 > 7 || (cmf << 8 | flg) % 31 !== 0) throw new InflateError("zlib: header \u0431\u0443\u0440\u0443\u0443");
|
|
74
|
+
if (flg & 32) throw new InflateError("zlib: preset dictionary");
|
|
75
|
+
return inflateRaw(src.subarray(2), limit);
|
|
76
|
+
}
|
|
77
|
+
function inflateRaw(src, limit = 64 << 20) {
|
|
78
|
+
let pos = 0;
|
|
79
|
+
let bitBuf = 0;
|
|
80
|
+
let bitCnt = 0;
|
|
81
|
+
let out = new Uint8Array(Math.min(limit, Math.max(1024, src.length * 4)));
|
|
82
|
+
let len = 0;
|
|
83
|
+
const need = (n) => {
|
|
84
|
+
if (len + n <= out.length) return;
|
|
85
|
+
if (len + n > limit) throw new InflateError("inflate: \u0445\u044D\u043C\u0436\u044D\u044D\u043D\u0438\u0439 \u0445\u044F\u0437\u0433\u0430\u0430\u0440");
|
|
86
|
+
let cap = out.length * 2;
|
|
87
|
+
while (cap < len + n) cap *= 2;
|
|
88
|
+
const nb = new Uint8Array(Math.min(cap, limit));
|
|
89
|
+
nb.set(out.subarray(0, len));
|
|
90
|
+
out = nb;
|
|
91
|
+
};
|
|
92
|
+
const bits = (n) => {
|
|
93
|
+
while (bitCnt < n) {
|
|
94
|
+
if (pos >= src.length) throw new Truncated("inflate: \u04E9\u0433\u04E9\u0433\u0434\u04E9\u043B \u0434\u0443\u0442\u0443\u0443");
|
|
95
|
+
bitBuf |= src[pos++] << bitCnt;
|
|
96
|
+
bitCnt += 8;
|
|
97
|
+
}
|
|
98
|
+
const v = bitBuf & (1 << n) - 1;
|
|
99
|
+
bitBuf >>>= n;
|
|
100
|
+
bitCnt -= n;
|
|
101
|
+
return v;
|
|
102
|
+
};
|
|
103
|
+
const decode = (h) => {
|
|
104
|
+
let code = 0;
|
|
105
|
+
let first = 0;
|
|
106
|
+
let index = 0;
|
|
107
|
+
for (let l = 1; l < 16; l++) {
|
|
108
|
+
code |= bits(1);
|
|
109
|
+
const count = h.counts[l];
|
|
110
|
+
if (code - count < first) return h.symbols[index + (code - first)];
|
|
111
|
+
index += count;
|
|
112
|
+
first += count;
|
|
113
|
+
first <<= 1;
|
|
114
|
+
code <<= 1;
|
|
115
|
+
}
|
|
116
|
+
throw new InflateError("inflate: \u043A\u043E\u0434 \u0431\u0443\u0440\u0443\u0443");
|
|
117
|
+
};
|
|
118
|
+
try {
|
|
119
|
+
inflateBlocks();
|
|
120
|
+
} catch (e) {
|
|
121
|
+
if (!(e instanceof Truncated)) throw e;
|
|
122
|
+
}
|
|
123
|
+
return out.slice(0, len);
|
|
124
|
+
function inflateBlocks() {
|
|
125
|
+
for (; ; ) {
|
|
126
|
+
const final = bits(1);
|
|
127
|
+
const type = bits(2);
|
|
128
|
+
if (type === 0) {
|
|
129
|
+
bitBuf = 0;
|
|
130
|
+
bitCnt = 0;
|
|
131
|
+
if (pos + 4 > src.length) throw new InflateError("inflate: stored header");
|
|
132
|
+
const n = src[pos] | src[pos + 1] << 8;
|
|
133
|
+
const nn = src[pos + 2] | src[pos + 3] << 8;
|
|
134
|
+
pos += 4;
|
|
135
|
+
if ((n ^ 65535) !== nn) throw new InflateError("inflate: stored \u0443\u0440\u0442");
|
|
136
|
+
if (pos + n > src.length) throw new InflateError("inflate: stored \u0434\u0443\u0442\u0443\u0443");
|
|
137
|
+
need(n);
|
|
138
|
+
out.set(src.subarray(pos, pos + n), len);
|
|
139
|
+
len += n;
|
|
140
|
+
pos += n;
|
|
141
|
+
} else if (type === 1 || type === 2) {
|
|
142
|
+
let lit;
|
|
143
|
+
let dist;
|
|
144
|
+
if (type === 1) {
|
|
145
|
+
[lit, dist] = fixed();
|
|
146
|
+
} else {
|
|
147
|
+
const hlit = bits(5) + 257;
|
|
148
|
+
const hdist = bits(5) + 1;
|
|
149
|
+
const hclen = bits(4) + 4;
|
|
150
|
+
const cl = new Uint8Array(19);
|
|
151
|
+
for (let i = 0; i < hclen; i++) cl[CL_ORDER[i]] = bits(3);
|
|
152
|
+
const clh = build(cl, 19);
|
|
153
|
+
const lens = new Uint8Array(hlit + hdist);
|
|
154
|
+
for (let i = 0; i < hlit + hdist; ) {
|
|
155
|
+
const sym = decode(clh);
|
|
156
|
+
if (sym < 16) {
|
|
157
|
+
lens[i++] = sym;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
let rep = 0;
|
|
161
|
+
let val = 0;
|
|
162
|
+
if (sym === 16) {
|
|
163
|
+
if (i === 0) throw new InflateError("inflate: \u0434\u0430\u0432\u0442\u0430\u043B\u0442 \u044D\u0445\u044D\u043D\u0434");
|
|
164
|
+
val = lens[i - 1];
|
|
165
|
+
rep = 3 + bits(2);
|
|
166
|
+
} else if (sym === 17) rep = 3 + bits(3);
|
|
167
|
+
else rep = 11 + bits(7);
|
|
168
|
+
if (i + rep > hlit + hdist) throw new InflateError("inflate: \u0443\u0440\u0442 \u0445\u044D\u0442\u044D\u0440\u0441\u044D\u043D");
|
|
169
|
+
lens.fill(val, i, i + rep);
|
|
170
|
+
i += rep;
|
|
171
|
+
}
|
|
172
|
+
lit = build(lens.subarray(0, hlit), hlit);
|
|
173
|
+
dist = build(lens.subarray(hlit), hdist);
|
|
174
|
+
}
|
|
175
|
+
for (; ; ) {
|
|
176
|
+
const sym = decode(lit);
|
|
177
|
+
if (sym < 256) {
|
|
178
|
+
need(1);
|
|
179
|
+
out[len++] = sym;
|
|
180
|
+
} else if (sym === 256) {
|
|
181
|
+
break;
|
|
182
|
+
} else {
|
|
183
|
+
const li = sym - 257;
|
|
184
|
+
if (li >= 29) throw new InflateError("inflate: \u0443\u0440\u0442 \u043A\u043E\u0434");
|
|
185
|
+
const l = LEN_BASE[li] + bits(LEN_EXTRA[li]);
|
|
186
|
+
const di = decode(dist);
|
|
187
|
+
if (di >= 30) throw new InflateError("inflate: \u0437\u0430\u0439 \u043A\u043E\u0434");
|
|
188
|
+
const d = DIST_BASE[di] + bits(DIST_EXTRA[di]);
|
|
189
|
+
if (d > len) throw new InflateError("inflate: \u0437\u0430\u0439 \u0445\u044D\u0442\u044D\u0440\u0441\u044D\u043D");
|
|
190
|
+
need(l);
|
|
191
|
+
for (let k = 0; k < l; k++, len++) out[len] = out[len - d];
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
throw new InflateError("inflate: block \u0442\u04E9\u0440\u04E9\u043B");
|
|
196
|
+
}
|
|
197
|
+
if (final) break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/pades/bytes.ts
|
|
203
|
+
var enc = new TextEncoder();
|
|
204
|
+
function latin1(s) {
|
|
205
|
+
const out = new Uint8Array(s.length);
|
|
206
|
+
for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i) & 255;
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
function fromLatin1(b, start2 = 0, end = b.length) {
|
|
210
|
+
let s = "";
|
|
211
|
+
for (let i = start2; i < end; i += 32768) s += String.fromCharCode(...b.subarray(i, Math.min(end, i + 32768)));
|
|
212
|
+
return s;
|
|
213
|
+
}
|
|
214
|
+
var HEX = "0123456789abcdef";
|
|
215
|
+
function toHex(b) {
|
|
216
|
+
let s = "";
|
|
217
|
+
for (const x of b) s += HEX[x >> 4] + HEX[x & 15];
|
|
218
|
+
return s;
|
|
219
|
+
}
|
|
220
|
+
var ByteWriter = class {
|
|
221
|
+
buf = new Uint8Array(1 << 16);
|
|
222
|
+
len = 0;
|
|
223
|
+
grow(n) {
|
|
224
|
+
if (this.len + n <= this.buf.length) return;
|
|
225
|
+
let cap = this.buf.length * 2;
|
|
226
|
+
while (cap < this.len + n) cap *= 2;
|
|
227
|
+
const nb = new Uint8Array(cap);
|
|
228
|
+
nb.set(this.buf.subarray(0, this.len));
|
|
229
|
+
this.buf = nb;
|
|
230
|
+
}
|
|
231
|
+
bytes(b) {
|
|
232
|
+
this.grow(b.length);
|
|
233
|
+
this.buf.set(b, this.len);
|
|
234
|
+
this.len += b.length;
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
/** ASCII/latin1 мөр. */
|
|
238
|
+
str(s) {
|
|
239
|
+
this.grow(s.length);
|
|
240
|
+
for (let i = 0; i < s.length; i++) this.buf[this.len + i] = s.charCodeAt(i) & 255;
|
|
241
|
+
this.len += s.length;
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
byte(x) {
|
|
245
|
+
this.grow(1);
|
|
246
|
+
this.buf[this.len++] = x & 255;
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
/** Хуулбар. */
|
|
250
|
+
result() {
|
|
251
|
+
return this.buf.slice(0, this.len);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// src/pades/pdf/reader.ts
|
|
256
|
+
var PdfError = class extends Error {
|
|
257
|
+
};
|
|
258
|
+
var NO_PTR = { id: 0, gen: 0 };
|
|
259
|
+
var PInt = class {
|
|
260
|
+
constructor(v) {
|
|
261
|
+
this.v = v;
|
|
262
|
+
}
|
|
263
|
+
v;
|
|
264
|
+
};
|
|
265
|
+
var PReal = class {
|
|
266
|
+
constructor(v) {
|
|
267
|
+
this.v = v;
|
|
268
|
+
}
|
|
269
|
+
v;
|
|
270
|
+
};
|
|
271
|
+
var PStr = class {
|
|
272
|
+
constructor(b) {
|
|
273
|
+
this.b = b;
|
|
274
|
+
}
|
|
275
|
+
b;
|
|
276
|
+
};
|
|
277
|
+
var PName = class {
|
|
278
|
+
constructor(n) {
|
|
279
|
+
this.n = n;
|
|
280
|
+
}
|
|
281
|
+
n;
|
|
282
|
+
};
|
|
283
|
+
var PRef = class {
|
|
284
|
+
constructor(id, gen) {
|
|
285
|
+
this.id = id;
|
|
286
|
+
this.gen = gen;
|
|
287
|
+
}
|
|
288
|
+
id;
|
|
289
|
+
gen;
|
|
290
|
+
};
|
|
291
|
+
var PKeyword = class {
|
|
292
|
+
constructor(k) {
|
|
293
|
+
this.k = k;
|
|
294
|
+
}
|
|
295
|
+
k;
|
|
296
|
+
};
|
|
297
|
+
var PStream = class {
|
|
298
|
+
constructor(hdr, ptr, offset) {
|
|
299
|
+
this.hdr = hdr;
|
|
300
|
+
this.ptr = ptr;
|
|
301
|
+
this.offset = offset;
|
|
302
|
+
}
|
|
303
|
+
hdr;
|
|
304
|
+
ptr;
|
|
305
|
+
offset;
|
|
306
|
+
};
|
|
307
|
+
var ObjDef = class {
|
|
308
|
+
constructor(ptr, obj) {
|
|
309
|
+
this.ptr = ptr;
|
|
310
|
+
this.obj = obj;
|
|
311
|
+
}
|
|
312
|
+
ptr;
|
|
313
|
+
obj;
|
|
314
|
+
};
|
|
315
|
+
var EOF_TOKEN = /* @__PURE__ */ Symbol("eof");
|
|
316
|
+
var isSpace = (c) => c === 0 || c === 9 || c === 10 || c === 12 || c === 13 || c === 32;
|
|
317
|
+
var isDelim = (c) => c === 60 || c === 62 || c === 40 || c === 41 || c === 91 || c === 93 || c === 123 || c === 125 || c === 47 || c === 37;
|
|
318
|
+
function unhex(c) {
|
|
319
|
+
if (c >= 48 && c <= 57) return c - 48;
|
|
320
|
+
if (c >= 97 && c <= 102) return c - 97 + 10;
|
|
321
|
+
if (c >= 65 && c <= 70) return c - 65 + 10;
|
|
322
|
+
return -1;
|
|
323
|
+
}
|
|
324
|
+
var isKw = (t, k) => t instanceof PKeyword && t.k === k;
|
|
325
|
+
var Lexer = class {
|
|
326
|
+
constructor(data, start2, allowEOF = false) {
|
|
327
|
+
this.data = data;
|
|
328
|
+
this.allowEOF = allowEOF;
|
|
329
|
+
this.pos = start2;
|
|
330
|
+
}
|
|
331
|
+
data;
|
|
332
|
+
allowEOF;
|
|
333
|
+
pos;
|
|
334
|
+
eof = false;
|
|
335
|
+
unread = [];
|
|
336
|
+
objptr = NO_PTR;
|
|
337
|
+
readByte() {
|
|
338
|
+
if (this.pos >= this.data.length) {
|
|
339
|
+
if (!this.allowEOF) throw new PdfError(`malformed PDF: reading at offset ${this.pos}: EOF`);
|
|
340
|
+
this.eof = true;
|
|
341
|
+
this.pos = this.data.length + 1;
|
|
342
|
+
return 10;
|
|
343
|
+
}
|
|
344
|
+
return this.data[this.pos++];
|
|
345
|
+
}
|
|
346
|
+
unreadByte() {
|
|
347
|
+
if (this.pos > 0) this.pos--;
|
|
348
|
+
}
|
|
349
|
+
unreadToken(t) {
|
|
350
|
+
this.unread.push(t);
|
|
351
|
+
}
|
|
352
|
+
/** Stream-ийн өгөгдлийн эхлэл (readOffset). */
|
|
353
|
+
offset() {
|
|
354
|
+
return this.pos;
|
|
355
|
+
}
|
|
356
|
+
seek(p) {
|
|
357
|
+
this.pos = p;
|
|
358
|
+
}
|
|
359
|
+
readToken() {
|
|
360
|
+
const u = this.unread.pop();
|
|
361
|
+
if (u !== void 0) return u;
|
|
362
|
+
let c = this.readByte();
|
|
363
|
+
for (; ; ) {
|
|
364
|
+
if (isSpace(c)) {
|
|
365
|
+
if (this.eof) return EOF_TOKEN;
|
|
366
|
+
c = this.readByte();
|
|
367
|
+
} else if (c === 37) {
|
|
368
|
+
while (c !== 13 && c !== 10) c = this.readByte();
|
|
369
|
+
} else break;
|
|
370
|
+
}
|
|
371
|
+
switch (c) {
|
|
372
|
+
case 60:
|
|
373
|
+
if (this.readByte() === 60) return new PKeyword("<<");
|
|
374
|
+
this.unreadByte();
|
|
375
|
+
return this.readHexString();
|
|
376
|
+
case 40:
|
|
377
|
+
return this.readLiteralString();
|
|
378
|
+
case 91:
|
|
379
|
+
case 93:
|
|
380
|
+
case 123:
|
|
381
|
+
case 125:
|
|
382
|
+
return new PKeyword(String.fromCharCode(c));
|
|
383
|
+
case 47:
|
|
384
|
+
return this.readName();
|
|
385
|
+
case 62:
|
|
386
|
+
if (this.readByte() === 62) return new PKeyword(">>");
|
|
387
|
+
this.unreadByte();
|
|
388
|
+
// fallthrough
|
|
389
|
+
default:
|
|
390
|
+
if (isDelim(c)) throw new PdfError(`unexpected delimiter ${String.fromCharCode(c)}`);
|
|
391
|
+
this.unreadByte();
|
|
392
|
+
return this.readKeyword();
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
readHexString() {
|
|
396
|
+
const out = [];
|
|
397
|
+
for (; ; ) {
|
|
398
|
+
let c = this.readByte();
|
|
399
|
+
while (isSpace(c)) c = this.readByte();
|
|
400
|
+
if (c === 62) break;
|
|
401
|
+
let c2 = this.readByte();
|
|
402
|
+
while (isSpace(c2)) c2 = this.readByte();
|
|
403
|
+
const x = unhex(c) << 4 | unhex(c2);
|
|
404
|
+
if (unhex(c) < 0 || unhex(c2) < 0 || x < 0) throw new PdfError("malformed hex string");
|
|
405
|
+
out.push(x);
|
|
406
|
+
}
|
|
407
|
+
return new PStr(Uint8Array.from(out));
|
|
408
|
+
}
|
|
409
|
+
readLiteralString() {
|
|
410
|
+
const out = [];
|
|
411
|
+
let depth = 1;
|
|
412
|
+
for (; ; ) {
|
|
413
|
+
let c = this.readByte();
|
|
414
|
+
if (c === 40) {
|
|
415
|
+
depth++;
|
|
416
|
+
out.push(c);
|
|
417
|
+
} else if (c === 41) {
|
|
418
|
+
if (--depth === 0) break;
|
|
419
|
+
out.push(c);
|
|
420
|
+
} else if (c === 92) {
|
|
421
|
+
c = this.readByte();
|
|
422
|
+
switch (c) {
|
|
423
|
+
case 110:
|
|
424
|
+
out.push(10);
|
|
425
|
+
break;
|
|
426
|
+
case 114:
|
|
427
|
+
out.push(13);
|
|
428
|
+
break;
|
|
429
|
+
case 98:
|
|
430
|
+
out.push(8);
|
|
431
|
+
break;
|
|
432
|
+
case 116:
|
|
433
|
+
out.push(9);
|
|
434
|
+
break;
|
|
435
|
+
case 102:
|
|
436
|
+
out.push(12);
|
|
437
|
+
break;
|
|
438
|
+
case 40:
|
|
439
|
+
case 41:
|
|
440
|
+
case 92:
|
|
441
|
+
out.push(c);
|
|
442
|
+
break;
|
|
443
|
+
case 13:
|
|
444
|
+
if (this.readByte() !== 10) this.unreadByte();
|
|
445
|
+
break;
|
|
446
|
+
case 10:
|
|
447
|
+
break;
|
|
448
|
+
default:
|
|
449
|
+
if (c >= 48 && c <= 55) {
|
|
450
|
+
let x = c - 48;
|
|
451
|
+
for (let i = 0; i < 2; i++) {
|
|
452
|
+
c = this.readByte();
|
|
453
|
+
if (c < 48 || c > 55) {
|
|
454
|
+
this.unreadByte();
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
x = x * 8 + (c - 48);
|
|
458
|
+
}
|
|
459
|
+
if (x > 255) throw new PdfError("invalid octal escape");
|
|
460
|
+
out.push(x);
|
|
461
|
+
} else {
|
|
462
|
+
throw new PdfError(`invalid escape sequence \\${String.fromCharCode(c)}`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
} else {
|
|
466
|
+
out.push(c);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return new PStr(Uint8Array.from(out));
|
|
470
|
+
}
|
|
471
|
+
readName() {
|
|
472
|
+
let s = "";
|
|
473
|
+
for (; ; ) {
|
|
474
|
+
const c = this.readByte();
|
|
475
|
+
if (isDelim(c) || isSpace(c)) {
|
|
476
|
+
this.unreadByte();
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
if (c === 35) {
|
|
480
|
+
const h = unhex(this.readByte());
|
|
481
|
+
const l = unhex(this.readByte());
|
|
482
|
+
if (h < 0 || l < 0) throw new PdfError("malformed name");
|
|
483
|
+
s += String.fromCharCode(h << 4 | l);
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
s += String.fromCharCode(c);
|
|
487
|
+
}
|
|
488
|
+
return new PName(s);
|
|
489
|
+
}
|
|
490
|
+
readKeyword() {
|
|
491
|
+
let s = "";
|
|
492
|
+
for (; ; ) {
|
|
493
|
+
const c = this.readByte();
|
|
494
|
+
if (isDelim(c) || isSpace(c)) {
|
|
495
|
+
this.unreadByte();
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
s += String.fromCharCode(c);
|
|
499
|
+
}
|
|
500
|
+
if (s === "true") return true;
|
|
501
|
+
if (s === "false") return false;
|
|
502
|
+
if (/^[+-]?[0-9]+$/.test(s)) {
|
|
503
|
+
const v = BigInt(s);
|
|
504
|
+
if (v > 0x7fffffffffffffffn || v < -0x8000000000000000n) throw new PdfError(`invalid integer ${s}`);
|
|
505
|
+
return new PInt(v);
|
|
506
|
+
}
|
|
507
|
+
if (/^[+-]?[0-9.]+$/.test(s) && /^[+-]?[^.]*\.[^.]*$/.test(s) && /[0-9]/.test(s)) {
|
|
508
|
+
return new PReal(parseFloat(s.startsWith("+") ? s.slice(1) : s));
|
|
509
|
+
}
|
|
510
|
+
if (/^[+-]?\.$/.test(s)) throw new PdfError(`invalid real ${s}`);
|
|
511
|
+
return new PKeyword(s);
|
|
512
|
+
}
|
|
513
|
+
readObject() {
|
|
514
|
+
const tok = this.readToken();
|
|
515
|
+
if (tok instanceof PKeyword) {
|
|
516
|
+
switch (tok.k) {
|
|
517
|
+
case "null":
|
|
518
|
+
return null;
|
|
519
|
+
case "endobj":
|
|
520
|
+
this.unreadToken(tok);
|
|
521
|
+
return null;
|
|
522
|
+
case "<<":
|
|
523
|
+
return this.readDict();
|
|
524
|
+
case "[":
|
|
525
|
+
return this.readArray();
|
|
526
|
+
}
|
|
527
|
+
throw new PdfError(`unexpected keyword ${tok.k} parsing object`);
|
|
528
|
+
}
|
|
529
|
+
if (tok instanceof PInt && tok.v >= 0n && tok.v <= 0xffffffffn) {
|
|
530
|
+
const tok2 = this.readToken();
|
|
531
|
+
if (tok2 instanceof PInt && tok2.v >= 0n && tok2.v <= 0xffffn) {
|
|
532
|
+
const tok3 = this.readToken();
|
|
533
|
+
if (isKw(tok3, "R")) return new PRef(Number(tok.v), Number(tok2.v));
|
|
534
|
+
if (isKw(tok3, "obj")) {
|
|
535
|
+
const old = this.objptr;
|
|
536
|
+
const ptr = { id: Number(tok.v), gen: Number(tok2.v) };
|
|
537
|
+
this.objptr = ptr;
|
|
538
|
+
const obj = this.readObject();
|
|
539
|
+
if (!(obj instanceof PStream)) {
|
|
540
|
+
const tok4 = this.readToken();
|
|
541
|
+
if (!isKw(tok4, "endobj")) throw new PdfError("missing endobj after indirect object definition");
|
|
542
|
+
}
|
|
543
|
+
this.objptr = old;
|
|
544
|
+
return new ObjDef(ptr, asObj(obj));
|
|
545
|
+
}
|
|
546
|
+
this.unreadToken(tok3);
|
|
547
|
+
}
|
|
548
|
+
this.unreadToken(tok2);
|
|
549
|
+
}
|
|
550
|
+
return tok;
|
|
551
|
+
}
|
|
552
|
+
readArray() {
|
|
553
|
+
const x = [];
|
|
554
|
+
for (; ; ) {
|
|
555
|
+
const tok = this.readToken();
|
|
556
|
+
if (isKw(tok, "]")) break;
|
|
557
|
+
if (tok === EOF_TOKEN) throw new PdfError("unterminated array");
|
|
558
|
+
this.unreadToken(tok);
|
|
559
|
+
x.push(asObj(this.readObject()));
|
|
560
|
+
}
|
|
561
|
+
return x;
|
|
562
|
+
}
|
|
563
|
+
readDict() {
|
|
564
|
+
const x = /* @__PURE__ */ new Map();
|
|
565
|
+
for (; ; ) {
|
|
566
|
+
const tok2 = this.readToken();
|
|
567
|
+
if (isKw(tok2, ">>")) break;
|
|
568
|
+
if (tok2 === EOF_TOKEN) throw new PdfError("unterminated dictionary");
|
|
569
|
+
if (!(tok2 instanceof PName)) throw new PdfError("unexpected non-name key parsing dictionary");
|
|
570
|
+
x.set(tok2.n, asObj(this.readObject()));
|
|
571
|
+
}
|
|
572
|
+
const tok = this.readToken();
|
|
573
|
+
if (!isKw(tok, "stream")) {
|
|
574
|
+
this.unreadToken(tok);
|
|
575
|
+
return x;
|
|
576
|
+
}
|
|
577
|
+
const c = this.readByte();
|
|
578
|
+
if (c === 13) {
|
|
579
|
+
if (this.readByte() !== 10) this.unreadByte();
|
|
580
|
+
} else if (c !== 10) {
|
|
581
|
+
throw new PdfError("stream keyword not followed by newline");
|
|
582
|
+
}
|
|
583
|
+
return new PStream(x, this.objptr, this.offset());
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
function asObj(o) {
|
|
587
|
+
if (o instanceof ObjDef || o === EOF_TOKEN || o instanceof PKeyword) throw new PdfError("unexpected token in object");
|
|
588
|
+
return o;
|
|
589
|
+
}
|
|
590
|
+
var ZERO_XREF = () => ({ ptr: NO_PTR, inStream: false, stream: NO_PTR, offset: 0 });
|
|
591
|
+
var ptrEq = (a, b) => a.id === b.id && a.gen === b.gen;
|
|
592
|
+
var Value = class _Value {
|
|
593
|
+
constructor(r, ptr, data) {
|
|
594
|
+
this.r = r;
|
|
595
|
+
this.ptr = ptr;
|
|
596
|
+
this.data = data;
|
|
597
|
+
}
|
|
598
|
+
r;
|
|
599
|
+
ptr;
|
|
600
|
+
data;
|
|
601
|
+
isNull() {
|
|
602
|
+
return this.data === null || this.data === void 0;
|
|
603
|
+
}
|
|
604
|
+
kind() {
|
|
605
|
+
const d = this.data;
|
|
606
|
+
if (d === null || d === void 0 || d instanceof PRef) return "null";
|
|
607
|
+
if (typeof d === "boolean") return "bool";
|
|
608
|
+
if (d instanceof PInt) return "int";
|
|
609
|
+
if (d instanceof PReal) return "real";
|
|
610
|
+
if (d instanceof PStr) return "string";
|
|
611
|
+
if (d instanceof PName) return "name";
|
|
612
|
+
if (d instanceof Map) return "dict";
|
|
613
|
+
if (Array.isArray(d)) return "array";
|
|
614
|
+
return "stream";
|
|
615
|
+
}
|
|
616
|
+
dict() {
|
|
617
|
+
const d = this.data;
|
|
618
|
+
if (d instanceof Map) return d;
|
|
619
|
+
if (d instanceof PStream) return d.hdr;
|
|
620
|
+
return void 0;
|
|
621
|
+
}
|
|
622
|
+
key(k) {
|
|
623
|
+
const d = this.dict();
|
|
624
|
+
if (!d) return new _Value(this.r, NO_PTR, void 0);
|
|
625
|
+
return this.r.resolve(this.ptr, d.has(k) ? d.get(k) : void 0);
|
|
626
|
+
}
|
|
627
|
+
/** Байтын эрэмбээр (Go sort.Strings). */
|
|
628
|
+
keys() {
|
|
629
|
+
const d = this.dict();
|
|
630
|
+
if (!d) return [];
|
|
631
|
+
return [...d.keys()].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
632
|
+
}
|
|
633
|
+
index(i) {
|
|
634
|
+
const d = this.data;
|
|
635
|
+
if (!Array.isArray(d) || i < 0 || i >= d.length) return new _Value(this.r, NO_PTR, void 0);
|
|
636
|
+
return this.r.resolve(this.ptr, d[i]);
|
|
637
|
+
}
|
|
638
|
+
len() {
|
|
639
|
+
return Array.isArray(this.data) ? this.data.length : 0;
|
|
640
|
+
}
|
|
641
|
+
name() {
|
|
642
|
+
return this.data instanceof PName ? this.data.n : "";
|
|
643
|
+
}
|
|
644
|
+
int64() {
|
|
645
|
+
return this.data instanceof PInt ? Number(this.data.v) : 0;
|
|
646
|
+
}
|
|
647
|
+
bool() {
|
|
648
|
+
return this.data === true;
|
|
649
|
+
}
|
|
650
|
+
float64() {
|
|
651
|
+
if (this.data instanceof PReal) return this.data.v;
|
|
652
|
+
if (this.data instanceof PInt) return Number(this.data.v);
|
|
653
|
+
return 0;
|
|
654
|
+
}
|
|
655
|
+
rawString() {
|
|
656
|
+
return this.data instanceof PStr ? this.data.b : new Uint8Array();
|
|
657
|
+
}
|
|
658
|
+
/** digitorus Text(): UTF-16BE (BOM) эсвэл PDFDocEncoding (ASCII хэсэгт ижил). */
|
|
659
|
+
text() {
|
|
660
|
+
if (!(this.data instanceof PStr)) return "";
|
|
661
|
+
const b = this.data.b;
|
|
662
|
+
if (b.length >= 2 && b[0] === 254 && b[1] === 255) {
|
|
663
|
+
let s = "";
|
|
664
|
+
for (let i = 2; i + 1 < b.length; i += 2) s += String.fromCharCode(b[i] << 8 | b[i + 1]);
|
|
665
|
+
return s;
|
|
666
|
+
}
|
|
667
|
+
return fromLatin1(b);
|
|
668
|
+
}
|
|
669
|
+
/** Stream-ийн тайлсан өгөгдөл (Value.Reader). */
|
|
670
|
+
streamData() {
|
|
671
|
+
const x = this.data;
|
|
672
|
+
if (!(x instanceof PStream)) throw new PdfError("stream not present");
|
|
673
|
+
const n = this.key("Length").int64();
|
|
674
|
+
if (n < 0 || x.offset + n > this.r.bytes.length) throw new PdfError("stream \u0443\u0440\u0442 \u0444\u0430\u0439\u043B\u0430\u0430\u0441 \u0445\u044D\u0442\u044D\u0440\u0441\u044D\u043D");
|
|
675
|
+
let data = this.r.bytes.subarray(x.offset, x.offset + n);
|
|
676
|
+
const filter = this.key("Filter");
|
|
677
|
+
const param = this.key("DecodeParms");
|
|
678
|
+
switch (filter.kind()) {
|
|
679
|
+
case "null":
|
|
680
|
+
break;
|
|
681
|
+
case "name":
|
|
682
|
+
data = applyFilter(data, filter.name(), param);
|
|
683
|
+
break;
|
|
684
|
+
case "array":
|
|
685
|
+
for (let i = 0; i < filter.len(); i++) data = applyFilter(data, filter.index(i).name(), param.index(i));
|
|
686
|
+
break;
|
|
687
|
+
default:
|
|
688
|
+
throw new PdfError("unsupported filter");
|
|
689
|
+
}
|
|
690
|
+
return data;
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
function applyFilter(data, name, param) {
|
|
694
|
+
if (name !== "FlateDecode") throw new PdfError(`unknown filter ${name}`);
|
|
695
|
+
const out = zlibInflate(data);
|
|
696
|
+
const pred = param.key("Predictor");
|
|
697
|
+
if (pred.kind() === "null") return out;
|
|
698
|
+
if (pred.int64() !== 12) throw new PdfError("unknown predictor");
|
|
699
|
+
const columns = param.key("Columns").int64();
|
|
700
|
+
const row = 1 + columns;
|
|
701
|
+
const hist = new Uint8Array(row);
|
|
702
|
+
const rows = Math.floor(out.length / row);
|
|
703
|
+
const res = new Uint8Array(rows * columns);
|
|
704
|
+
for (let r = 0; r < rows; r++) {
|
|
705
|
+
if (out[r * row] !== 2) {
|
|
706
|
+
return res.slice(0, r * columns);
|
|
707
|
+
}
|
|
708
|
+
for (let i = 0; i < row; i++) hist[i] = hist[i] + out[r * row + i] & 255;
|
|
709
|
+
res.set(hist.subarray(1), r * columns);
|
|
710
|
+
}
|
|
711
|
+
return res;
|
|
712
|
+
}
|
|
713
|
+
var Reader = class {
|
|
714
|
+
constructor(bytes) {
|
|
715
|
+
this.bytes = bytes;
|
|
716
|
+
const b = bytes;
|
|
717
|
+
const ok17 = b.length >= 8 && fromLatin1(b, 0, 7) === "%PDF-1." && b[7] >= 48 && b[7] <= 55;
|
|
718
|
+
const ok20 = b.length >= 8 && fromLatin1(b, 0, 7) === "%PDF-2." && b[7] === 48;
|
|
719
|
+
if (!ok17 && !ok20) throw new PdfError("not a PDF file: invalid header");
|
|
720
|
+
this.pdfVersion = fromLatin1(b, 5, 8);
|
|
721
|
+
const startxrefPos = findStartXref(b);
|
|
722
|
+
const lx = new Lexer(b, startxrefPos);
|
|
723
|
+
if (!isKw(lx.readToken(), "startxref")) throw new PdfError("malformed PDF file: missing startxref");
|
|
724
|
+
const sx = lx.readToken();
|
|
725
|
+
if (!(sx instanceof PInt)) throw new PdfError("malformed PDF file: startxref not followed by integer");
|
|
726
|
+
this.startXref = Number(sx.v);
|
|
727
|
+
if (this.startXref < 0 || this.startXref >= b.length) throw new PdfError("startxref \u0431\u0443\u0440\u0443\u0443");
|
|
728
|
+
const x = new Lexer(b, this.startXref);
|
|
729
|
+
const tok = x.readToken();
|
|
730
|
+
if (isKw(tok, "xref")) this.readXrefTable(x);
|
|
731
|
+
else if (tok instanceof PInt) {
|
|
732
|
+
x.unreadToken(tok);
|
|
733
|
+
this.readXrefStream(x);
|
|
734
|
+
} else throw new PdfError("malformed PDF: cross-reference table not found");
|
|
735
|
+
}
|
|
736
|
+
bytes;
|
|
737
|
+
xref = [];
|
|
738
|
+
trailer = /* @__PURE__ */ new Map();
|
|
739
|
+
trailerPtr = NO_PTR;
|
|
740
|
+
/** "table" | "stream" */
|
|
741
|
+
xrefType = "";
|
|
742
|
+
/** Сүүлийн startxref (шинэ revision-ий /Prev). */
|
|
743
|
+
startXref = 0;
|
|
744
|
+
/** Header-ийн "1.x". */
|
|
745
|
+
pdfVersion = "";
|
|
746
|
+
cache = /* @__PURE__ */ new Map();
|
|
747
|
+
trailerValue() {
|
|
748
|
+
return new Value(this, this.trailerPtr, this.trailer);
|
|
749
|
+
}
|
|
750
|
+
readXrefStream(b) {
|
|
751
|
+
const def = b.readObject();
|
|
752
|
+
if (!(def instanceof ObjDef) || !(def.obj instanceof PStream)) throw new PdfError("malformed PDF: cross-reference table not found");
|
|
753
|
+
const strm = def.obj;
|
|
754
|
+
const type = strm.hdr.get("Type");
|
|
755
|
+
if (!(type instanceof PName) || type.n !== "XRef") throw new PdfError("malformed PDF: xref stream does not have type XRef");
|
|
756
|
+
const sizeObj = strm.hdr.get("Size");
|
|
757
|
+
if (!(sizeObj instanceof PInt)) throw new PdfError("malformed PDF: xref stream missing Size");
|
|
758
|
+
const size = Number(sizeObj.v);
|
|
759
|
+
if (size < 0 || size > 1 << 24) throw new PdfError("xref Size \u0431\u0443\u0440\u0443\u0443");
|
|
760
|
+
const table = Array.from({ length: size }, ZERO_XREF);
|
|
761
|
+
this.readXrefStreamData(strm, table, size);
|
|
762
|
+
const seen = /* @__PURE__ */ new Set();
|
|
763
|
+
let prevoff = strm.hdr.get("Prev");
|
|
764
|
+
while (prevoff !== void 0 && prevoff !== null) {
|
|
765
|
+
if (!(prevoff instanceof PInt)) throw new PdfError("malformed PDF: xref Prev is not integer");
|
|
766
|
+
const off = Number(prevoff.v);
|
|
767
|
+
if (seen.has(off)) throw new PdfError("malformed PDF: xref Prev loop detected");
|
|
768
|
+
seen.add(off);
|
|
769
|
+
if (off < 0 || off >= this.bytes.length) throw new PdfError("xref Prev \u0431\u0443\u0440\u0443\u0443");
|
|
770
|
+
const pd = new Lexer(this.bytes, off).readObject();
|
|
771
|
+
if (!(pd instanceof ObjDef) || !(pd.obj instanceof PStream)) throw new PdfError("malformed PDF: xref prev stream not found");
|
|
772
|
+
const ps = pd.obj;
|
|
773
|
+
prevoff = ps.hdr.get("Prev");
|
|
774
|
+
const prev = new Value(this, NO_PTR, ps);
|
|
775
|
+
if (prev.key("Type").name() !== "XRef") throw new PdfError("malformed PDF: xref prev stream does not have type XRef");
|
|
776
|
+
const psize = prev.key("Size").int64();
|
|
777
|
+
if (psize > size) throw new PdfError("malformed PDF: xref prev stream larger than last stream");
|
|
778
|
+
this.readXrefStreamData(ps, table, psize);
|
|
779
|
+
}
|
|
780
|
+
this.xrefType = "stream";
|
|
781
|
+
this.xref = table;
|
|
782
|
+
this.trailerPtr = def.ptr;
|
|
783
|
+
this.trailer = strm.hdr;
|
|
784
|
+
}
|
|
785
|
+
readXrefStreamData(strm, table, size) {
|
|
786
|
+
let index = strm.hdr.get("Index");
|
|
787
|
+
if (!Array.isArray(index)) index = [new PInt(0n), new PInt(BigInt(size))];
|
|
788
|
+
if (index.length % 2 !== 0) throw new PdfError("invalid Index array");
|
|
789
|
+
const ww = strm.hdr.get("W");
|
|
790
|
+
if (!Array.isArray(ww)) throw new PdfError("xref stream missing W array");
|
|
791
|
+
const w = ww.map((x) => {
|
|
792
|
+
if (!(x instanceof PInt) || x.v < 0n || x.v > 8n) throw new PdfError("invalid W array");
|
|
793
|
+
return Number(x.v);
|
|
794
|
+
});
|
|
795
|
+
if (w.length < 3) throw new PdfError("invalid W array");
|
|
796
|
+
const wtotal = w.reduce((a, c) => a + c, 0);
|
|
797
|
+
const data = new Value(this, NO_PTR, strm).streamData();
|
|
798
|
+
let p = 0;
|
|
799
|
+
const dec = (n) => {
|
|
800
|
+
let x = 0;
|
|
801
|
+
for (let i = 0; i < n; i++) x = x * 256 + data[p + i];
|
|
802
|
+
p += n;
|
|
803
|
+
return x;
|
|
804
|
+
};
|
|
805
|
+
for (let k = 0; k + 1 < index.length; k += 2) {
|
|
806
|
+
const s = index[k];
|
|
807
|
+
const n = index[k + 1];
|
|
808
|
+
if (!(s instanceof PInt) || !(n instanceof PInt)) throw new PdfError("malformed Index pair");
|
|
809
|
+
const start2 = Number(s.v);
|
|
810
|
+
for (let i = 0; i < Number(n.v); i++) {
|
|
811
|
+
if (p + wtotal > data.length) throw new PdfError("error reading xref stream: EOF");
|
|
812
|
+
const base2 = p;
|
|
813
|
+
let v1 = dec(w[0]);
|
|
814
|
+
if (w[0] === 0) v1 = 1;
|
|
815
|
+
const v2 = dec(w[1]);
|
|
816
|
+
const v3 = dec(w[2]);
|
|
817
|
+
p = base2 + wtotal;
|
|
818
|
+
const x = start2 + i;
|
|
819
|
+
if (x >= table.length) {
|
|
820
|
+
if (x !== table.length) throw new PdfError("xref stream: index out of range");
|
|
821
|
+
table.push(ZERO_XREF());
|
|
822
|
+
}
|
|
823
|
+
if (!ptrEq(table[x].ptr, NO_PTR)) continue;
|
|
824
|
+
if (v1 === 0) table[x] = { ptr: { id: 0, gen: 65535 }, inStream: false, stream: NO_PTR, offset: 0 };
|
|
825
|
+
else if (v1 === 1) table[x] = { ptr: { id: x, gen: v3 & 65535 }, inStream: false, stream: NO_PTR, offset: v2 };
|
|
826
|
+
else if (v1 === 2) table[x] = { ptr: { id: x, gen: 0 }, inStream: true, stream: { id: v2, gen: 0 }, offset: v3 };
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
readXrefTable(b) {
|
|
831
|
+
let table = [];
|
|
832
|
+
this.readXrefTableData(b, table);
|
|
833
|
+
const trailer = b.readObject();
|
|
834
|
+
if (!(trailer instanceof Map)) throw new PdfError("malformed PDF: xref table not followed by trailer dictionary");
|
|
835
|
+
const seen = /* @__PURE__ */ new Set();
|
|
836
|
+
let prevoff = trailer.get("Prev");
|
|
837
|
+
while (prevoff !== void 0 && prevoff !== null) {
|
|
838
|
+
if (!(prevoff instanceof PInt)) throw new PdfError("malformed PDF: xref Prev is not integer");
|
|
839
|
+
const off = Number(prevoff.v);
|
|
840
|
+
if (seen.has(off)) throw new PdfError("malformed PDF: xref Prev loop detected");
|
|
841
|
+
seen.add(off);
|
|
842
|
+
if (off < 0 || off >= this.bytes.length) throw new PdfError("xref Prev \u0431\u0443\u0440\u0443\u0443");
|
|
843
|
+
const pb = new Lexer(this.bytes, off);
|
|
844
|
+
if (!isKw(pb.readToken(), "xref")) throw new PdfError("malformed PDF: xref Prev does not point to xref");
|
|
845
|
+
this.readXrefTableData(pb, table);
|
|
846
|
+
const t2 = pb.readObject();
|
|
847
|
+
if (!(t2 instanceof Map)) throw new PdfError("malformed PDF: xref Prev table not followed by trailer dictionary");
|
|
848
|
+
prevoff = t2.get("Prev");
|
|
849
|
+
}
|
|
850
|
+
const size = trailer.get("Size");
|
|
851
|
+
if (!(size instanceof PInt)) throw new PdfError("malformed PDF: trailer missing /Size entry");
|
|
852
|
+
if (Number(size.v) < table.length) table = table.slice(0, Math.max(0, Number(size.v)));
|
|
853
|
+
this.xrefType = "table";
|
|
854
|
+
this.xref = table;
|
|
855
|
+
this.trailer = trailer;
|
|
856
|
+
this.trailerPtr = NO_PTR;
|
|
857
|
+
}
|
|
858
|
+
readXrefTableData(b, table) {
|
|
859
|
+
for (; ; ) {
|
|
860
|
+
const tok = b.readToken();
|
|
861
|
+
if (isKw(tok, "trailer")) break;
|
|
862
|
+
const n = b.readToken();
|
|
863
|
+
if (!(tok instanceof PInt) || !(n instanceof PInt)) throw new PdfError("malformed xref table");
|
|
864
|
+
const start2 = Number(tok.v);
|
|
865
|
+
const count = Number(n.v);
|
|
866
|
+
if (start2 < 0 || count < 0 || start2 + count > 1 << 24) throw new PdfError("malformed xref table");
|
|
867
|
+
for (let i = 0; i < count; i++) {
|
|
868
|
+
const off = b.readToken();
|
|
869
|
+
const gen = b.readToken();
|
|
870
|
+
const alloc = b.readToken();
|
|
871
|
+
if (!(off instanceof PInt) || !(gen instanceof PInt) || !(alloc instanceof PKeyword) || alloc.k !== "f" && alloc.k !== "n") {
|
|
872
|
+
throw new PdfError("malformed xref table");
|
|
873
|
+
}
|
|
874
|
+
const x = start2 + i;
|
|
875
|
+
while (table.length <= x) table.push(ZERO_XREF());
|
|
876
|
+
if (alloc.k === "n" && table[x].offset === 0) {
|
|
877
|
+
table[x] = { ptr: { id: x, gen: Number(gen.v) & 65535 }, inStream: false, stream: NO_PTR, offset: Number(off.v) };
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
/** digitorus resolve(parent, x). */
|
|
883
|
+
resolve(parent, x) {
|
|
884
|
+
if (x instanceof PRef) {
|
|
885
|
+
const key = `${x.id} ${x.gen}`;
|
|
886
|
+
const hit = this.cache.get(key);
|
|
887
|
+
if (hit) return hit;
|
|
888
|
+
const v = this.load(x);
|
|
889
|
+
this.cache.set(key, v);
|
|
890
|
+
return v;
|
|
891
|
+
}
|
|
892
|
+
return new Value(this, parent, x);
|
|
893
|
+
}
|
|
894
|
+
load(ptr) {
|
|
895
|
+
if (ptr.id >= this.xref.length) return new Value(this, NO_PTR, void 0);
|
|
896
|
+
const xr = this.xref[ptr.id];
|
|
897
|
+
if (xr.ptr.id !== ptr.id || xr.ptr.gen !== ptr.gen || !xr.inStream && xr.offset === 0) return new Value(this, NO_PTR, void 0);
|
|
898
|
+
const p = { id: ptr.id, gen: ptr.gen };
|
|
899
|
+
let x;
|
|
900
|
+
if (xr.inStream) {
|
|
901
|
+
let strm = this.resolve(p, new PRef(xr.stream.id, xr.stream.gen));
|
|
902
|
+
for (let guard = 0; ; guard++) {
|
|
903
|
+
if (guard > 64) throw new PdfError("object stream /Extends \u0433\u0438\u043D\u0436 \u0445\u044D\u0442 \u0443\u0440\u0442");
|
|
904
|
+
if (strm.kind() !== "stream") throw new PdfError("not a stream");
|
|
905
|
+
if (strm.key("Type").name() !== "ObjStm") throw new PdfError("not an object stream");
|
|
906
|
+
const n = strm.key("N").int64();
|
|
907
|
+
const first = strm.key("First").int64();
|
|
908
|
+
if (first === 0) throw new PdfError("missing First");
|
|
909
|
+
const data = strm.streamData();
|
|
910
|
+
const b = new Lexer(data, 0, true);
|
|
911
|
+
let found;
|
|
912
|
+
let ok = false;
|
|
913
|
+
for (let i = 0; i < n; i++) {
|
|
914
|
+
const id = b.readToken();
|
|
915
|
+
const off = b.readToken();
|
|
916
|
+
const idn = id instanceof PInt ? Number(BigInt.asUintN(32, id.v)) : 0;
|
|
917
|
+
if (idn === ptr.id) {
|
|
918
|
+
const o = off instanceof PInt ? Number(off.v) : 0;
|
|
919
|
+
b.seek(first + o);
|
|
920
|
+
found = asObj(b.readObject());
|
|
921
|
+
ok = true;
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
if (ok) {
|
|
926
|
+
x = found ?? null;
|
|
927
|
+
break;
|
|
928
|
+
}
|
|
929
|
+
const ext = strm.key("Extends");
|
|
930
|
+
if (ext.kind() !== "stream") throw new PdfError("cannot find object in stream");
|
|
931
|
+
strm = ext;
|
|
932
|
+
}
|
|
933
|
+
} else {
|
|
934
|
+
const b = new Lexer(this.bytes, xr.offset);
|
|
935
|
+
const def = b.readObject();
|
|
936
|
+
if (!(def instanceof ObjDef)) throw new PdfError(`loading ${ptr.id} ${ptr.gen}: not an objdef`);
|
|
937
|
+
if (!ptrEq(def.ptr, p)) throw new PdfError(`loading ${ptr.id} ${ptr.gen}: found ${def.ptr.id} ${def.ptr.gen}`);
|
|
938
|
+
x = def.obj;
|
|
939
|
+
}
|
|
940
|
+
return new Value(this, p, x);
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
function findStartXref(b) {
|
|
944
|
+
const end = b.length;
|
|
945
|
+
let searchSize = 200;
|
|
946
|
+
let eofEnd = -1;
|
|
947
|
+
for (; ; ) {
|
|
948
|
+
const from = Math.max(0, end - searchSize);
|
|
949
|
+
if (end - searchSize >= 0) {
|
|
950
|
+
let hi = end;
|
|
951
|
+
while (hi > from && (b[hi - 1] === 10 || b[hi - 1] === 13)) hi--;
|
|
952
|
+
while (hi > from && (b[hi - 1] === 10 || b[hi - 1] === 13 || b[hi - 1] === 9 || b[hi - 1] === 32)) hi--;
|
|
953
|
+
for (; ; ) {
|
|
954
|
+
if (hi - from === 5) break;
|
|
955
|
+
if (hi - from >= 5 && fromLatin1(b, hi - 5, hi) === "%%EOF") {
|
|
956
|
+
eofEnd = hi;
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
if (hi - from < 5) break;
|
|
960
|
+
hi--;
|
|
961
|
+
}
|
|
962
|
+
if (eofEnd >= 0) break;
|
|
963
|
+
}
|
|
964
|
+
searchSize += 200;
|
|
965
|
+
if (searchSize > end) throw new PdfError("not a PDF file: missing %%EOF");
|
|
966
|
+
}
|
|
967
|
+
const winStart = eofEnd - 200;
|
|
968
|
+
if (winStart < 0) throw new PdfError("malformed PDF file: missing final startxref");
|
|
969
|
+
const buf = b.subarray(winStart, eofEnd);
|
|
970
|
+
const i = findLastLine(buf, "startxref");
|
|
971
|
+
if (i < 0) throw new PdfError("malformed PDF file: missing final startxref");
|
|
972
|
+
return winStart + i;
|
|
973
|
+
}
|
|
974
|
+
function findLastLine(buf, s) {
|
|
975
|
+
const str = fromLatin1(buf);
|
|
976
|
+
let max = buf.length;
|
|
977
|
+
for (; ; ) {
|
|
978
|
+
const i = str.lastIndexOf(s, max - s.length);
|
|
979
|
+
if (i <= 0 || i + s.length >= buf.length) return -1;
|
|
980
|
+
const before = buf[i - 1];
|
|
981
|
+
const after = buf[i + s.length];
|
|
982
|
+
if ((before === 10 || before === 13) && (after === 10 || after === 13)) return i;
|
|
983
|
+
max = i;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
// src/pades/pdf/serialize.ts
|
|
988
|
+
var MAX_DEPTH = 32;
|
|
989
|
+
function serialize(v, container, depth = 0) {
|
|
990
|
+
if (depth > MAX_DEPTH) throw new PdfError("pades: \u0443\u0442\u0433\u0430 \u0445\u044D\u0442 \u0433\u04AF\u043D \u0434\u0430\u0432\u0445\u0430\u0440\u043B\u0430\u0441\u0430\u043D");
|
|
991
|
+
if (v.isNull()) return "null";
|
|
992
|
+
if (v.ptr.id !== container) return `${v.ptr.id} ${v.ptr.gen} R`;
|
|
993
|
+
const d = v.data;
|
|
994
|
+
if (d instanceof PStr) return hexString(d.b);
|
|
995
|
+
if (typeof d === "boolean") return d ? "true" : "false";
|
|
996
|
+
if (d instanceof PInt) return d.v.toString();
|
|
997
|
+
if (d instanceof PReal) return formatFloat(d.v);
|
|
998
|
+
if (d instanceof PName) return "/" + escapeName(d.n);
|
|
999
|
+
if (d instanceof Map) {
|
|
1000
|
+
let s = "<<";
|
|
1001
|
+
for (const k of v.keys()) s += " /" + escapeName(k) + " " + serialize(v.key(k), container, depth + 1);
|
|
1002
|
+
return s + " >>";
|
|
1003
|
+
}
|
|
1004
|
+
if (Array.isArray(d)) {
|
|
1005
|
+
const parts = [];
|
|
1006
|
+
for (let i = 0; i < v.len(); i++) parts.push(serialize(v.index(i), container, depth + 1));
|
|
1007
|
+
return "[" + parts.join(" ") + "]";
|
|
1008
|
+
}
|
|
1009
|
+
throw new PdfError("pades: \u0448\u0443\u0443\u0434 stream (\u044D\u0441\u0432\u044D\u043B \u0442\u0430\u043D\u0438\u0445\u0433\u04AF\u0439 \u0443\u0442\u0433\u0430) \u0445\u0443\u0443\u043B\u0430\u0445 \u0431\u043E\u043B\u043E\u043C\u0436\u0433\u04AF\u0439");
|
|
1010
|
+
}
|
|
1011
|
+
function hexString(raw) {
|
|
1012
|
+
return "<" + toHex(raw) + ">";
|
|
1013
|
+
}
|
|
1014
|
+
function escapeName(n) {
|
|
1015
|
+
let s = "";
|
|
1016
|
+
for (let i = 0; i < n.length; i++) {
|
|
1017
|
+
const c = n.charCodeAt(i) & 255;
|
|
1018
|
+
if (c < 33 || c > 126 || "#()<>[]{}/%".includes(String.fromCharCode(c))) {
|
|
1019
|
+
s += "#" + c.toString(16).toUpperCase().padStart(2, "0");
|
|
1020
|
+
} else s += String.fromCharCode(c);
|
|
1021
|
+
}
|
|
1022
|
+
return s;
|
|
1023
|
+
}
|
|
1024
|
+
function formatFloat(f) {
|
|
1025
|
+
if (Number.isNaN(f)) return "NaN";
|
|
1026
|
+
if (!Number.isFinite(f)) return f > 0 ? "+Inf" : "-Inf";
|
|
1027
|
+
if (Object.is(f, -0)) return "-0";
|
|
1028
|
+
const s = String(f);
|
|
1029
|
+
const m = /^(-?)(\d+)(?:\.(\d+))?e([+-]\d+)$/.exec(s);
|
|
1030
|
+
if (!m) return s;
|
|
1031
|
+
const [, sign, ip, fp = "", es] = m;
|
|
1032
|
+
const digits = ip + fp;
|
|
1033
|
+
const point = ip.length + Number(es);
|
|
1034
|
+
if (point <= 0) return `${sign}0.${"0".repeat(-point)}${digits}`.replace(/0+$/, "");
|
|
1035
|
+
if (point >= digits.length) return sign + digits + "0".repeat(point - digits.length);
|
|
1036
|
+
return `${sign}${digits.slice(0, point)}.${digits.slice(point)}`;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// src/pades/pdf/incremental.ts
|
|
1040
|
+
var Incremental = class {
|
|
1041
|
+
rdr;
|
|
1042
|
+
out = new ByteWriter();
|
|
1043
|
+
nextID;
|
|
1044
|
+
entries = [];
|
|
1045
|
+
stream;
|
|
1046
|
+
constructor(base2) {
|
|
1047
|
+
const rdr = new Reader(base2);
|
|
1048
|
+
const tr = rdr.trailerValue();
|
|
1049
|
+
if (!tr.key("Encrypt").isNull()) throw new PdfError("\u0448\u0438\u0444\u0440\u043B\u044D\u0433\u0434\u0441\u044D\u043D PDF");
|
|
1050
|
+
if (tr.key("Root").kind() !== "dict") throw new PdfError("Catalog \u0430\u043B\u0433\u0430");
|
|
1051
|
+
if (rdr.xrefType !== "table" && rdr.xrefType !== "stream") throw new PdfError(`xref \u0442\u04E9\u0440\u04E9\u043B ${rdr.xrefType}`);
|
|
1052
|
+
this.rdr = rdr;
|
|
1053
|
+
this.stream = rdr.xrefType === "stream";
|
|
1054
|
+
let maxID = 0;
|
|
1055
|
+
for (const x of rdr.xref) if (x.ptr.id > maxID) maxID = x.ptr.id;
|
|
1056
|
+
const size = tr.key("Size").int64();
|
|
1057
|
+
if (size <= 0 || size > 1 << 23) throw new PdfError("/Size \u0431\u0443\u0440\u0443\u0443");
|
|
1058
|
+
this.nextID = Math.max(size, maxID + 1);
|
|
1059
|
+
this.out.bytes(base2);
|
|
1060
|
+
this.out.byte(10);
|
|
1061
|
+
}
|
|
1062
|
+
root() {
|
|
1063
|
+
return this.rdr.trailerValue().key("Root");
|
|
1064
|
+
}
|
|
1065
|
+
reserve() {
|
|
1066
|
+
return this.nextID++;
|
|
1067
|
+
}
|
|
1068
|
+
/** Объект бичиж xref-д бүртгэнэ; body-ийн эхлэх байрлал. `body` — latin1 мөр эсвэл байт. */
|
|
1069
|
+
writeObject(id, body, gen = 0) {
|
|
1070
|
+
this.out.byte(10);
|
|
1071
|
+
this.entries.push({ id, gen, offset: this.out.len });
|
|
1072
|
+
this.out.str(`${id} ${gen} obj
|
|
1073
|
+
`);
|
|
1074
|
+
const start2 = this.out.len;
|
|
1075
|
+
this.out.bytes(trimSpace(typeof body === "string" ? latin1(body) : body));
|
|
1076
|
+
this.out.str("\nendobj\n");
|
|
1077
|
+
return start2;
|
|
1078
|
+
}
|
|
1079
|
+
addObject(body) {
|
|
1080
|
+
const id = this.reserve();
|
|
1081
|
+
return [id, this.writeObject(id, body)];
|
|
1082
|
+
}
|
|
1083
|
+
finish(root) {
|
|
1084
|
+
if (this.entries.length === 0) throw new PdfError("pades: \u0448\u0438\u043D\u044D \u043E\u0431\u044A\u0435\u043A\u0442 \u0430\u043B\u0433\u0430");
|
|
1085
|
+
this.out.byte(10);
|
|
1086
|
+
const xrefStart = this.out.len;
|
|
1087
|
+
let xid = 0;
|
|
1088
|
+
if (this.stream) {
|
|
1089
|
+
xid = this.reserve();
|
|
1090
|
+
this.entries.push({ id: xid, gen: 0, offset: xrefStart });
|
|
1091
|
+
}
|
|
1092
|
+
this.entries.sort((a, b) => a.id - b.id);
|
|
1093
|
+
const runs = [];
|
|
1094
|
+
this.entries.forEach((e, i) => {
|
|
1095
|
+
const prev2 = this.entries[i - 1];
|
|
1096
|
+
if (prev2 && e.id === prev2.id) throw new PdfError("pades: \u043E\u0431\u044A\u0435\u043A\u0442\u044B\u043D \u0434\u0443\u0433\u0430\u0430\u0440 \u0434\u0430\u0432\u0445\u0430\u0440\u0434\u0441\u0430\u043D");
|
|
1097
|
+
if (prev2 && e.id === prev2.id + 1) {
|
|
1098
|
+
runs[runs.length - 1][1]++;
|
|
1099
|
+
return;
|
|
1100
|
+
}
|
|
1101
|
+
runs.push([i, 1]);
|
|
1102
|
+
});
|
|
1103
|
+
const prev = this.rdr.startXref;
|
|
1104
|
+
const tr = this.rdr.trailerValue();
|
|
1105
|
+
let extra = "";
|
|
1106
|
+
const info = tr.key("Info");
|
|
1107
|
+
if (!info.isNull()) {
|
|
1108
|
+
const p = info.ptr;
|
|
1109
|
+
const tp = tr.ptr;
|
|
1110
|
+
if (p.id === tp.id && p.gen === tp.gen) extra += " /Info " + serialize(info, tp.id, 0);
|
|
1111
|
+
else extra += ` /Info ${p.id} ${p.gen} R`;
|
|
1112
|
+
}
|
|
1113
|
+
const idv = tr.key("ID");
|
|
1114
|
+
if (idv.kind() === "array" && idv.len() === 2) {
|
|
1115
|
+
extra += ` /ID [${hexString(idv.index(0).rawString())} ${hexString(idv.index(1).rawString())}]`;
|
|
1116
|
+
}
|
|
1117
|
+
if (!this.stream) {
|
|
1118
|
+
this.out.str("xref\n");
|
|
1119
|
+
for (const [s, n] of runs) {
|
|
1120
|
+
this.out.str(`${this.entries[s].id} ${n}
|
|
1121
|
+
`);
|
|
1122
|
+
for (const e of this.entries.slice(s, s + n)) {
|
|
1123
|
+
this.out.str(`${String(e.offset).padStart(10, "0")} ${String(e.gen).padStart(5, "0")} n\r
|
|
1124
|
+
`);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
this.out.str(`trailer
|
|
1128
|
+
<< /Size ${this.nextID} /Root ${root} 0 R${extra} /Prev ${prev} >>
|
|
1129
|
+
startxref
|
|
1130
|
+
${xrefStart}
|
|
1131
|
+
%%EOF
|
|
1132
|
+
`);
|
|
1133
|
+
return this.out.result();
|
|
1134
|
+
}
|
|
1135
|
+
const rows = new Uint8Array(this.entries.length * 6);
|
|
1136
|
+
this.entries.forEach((e, i) => {
|
|
1137
|
+
if (e.offset > 4294967295) throw new PdfError("pades: offset 4 \u0431\u0430\u0439\u0442\u0430\u0434 \u0431\u0430\u0433\u0442\u0430\u0445\u0433\u04AF\u0439");
|
|
1138
|
+
if (e.gen > 255) throw new PdfError("pades: \u04AF\u0435\u0438\u0439\u043D \u0434\u0443\u0433\u0430\u0430\u0440 1 \u0431\u0430\u0439\u0442\u0430\u0434 \u0431\u0430\u0433\u0442\u0430\u0445\u0433\u04AF\u0439");
|
|
1139
|
+
const o = i * 6;
|
|
1140
|
+
rows[o] = 1;
|
|
1141
|
+
rows[o + 1] = e.offset >>> 24 & 255;
|
|
1142
|
+
rows[o + 2] = e.offset >>> 16 & 255;
|
|
1143
|
+
rows[o + 3] = e.offset >>> 8 & 255;
|
|
1144
|
+
rows[o + 4] = e.offset & 255;
|
|
1145
|
+
rows[o + 5] = e.gen;
|
|
1146
|
+
});
|
|
1147
|
+
const index = runs.map(([s, n]) => `${this.entries[s].id} ${n}`).join(" ");
|
|
1148
|
+
this.out.str(
|
|
1149
|
+
`${xid} 0 obj
|
|
1150
|
+
<< /Type /XRef /Length ${rows.length} /W [1 4 1] /Prev ${prev} /Size ${this.nextID} /Index [${index}] /Root ${root} 0 R${extra} >>
|
|
1151
|
+
stream
|
|
1152
|
+
`
|
|
1153
|
+
);
|
|
1154
|
+
this.out.bytes(rows);
|
|
1155
|
+
this.out.str(`
|
|
1156
|
+
endstream
|
|
1157
|
+
endobj
|
|
1158
|
+
startxref
|
|
1159
|
+
${xrefStart}
|
|
1160
|
+
%%EOF
|
|
1161
|
+
`);
|
|
1162
|
+
return this.out.result();
|
|
1163
|
+
}
|
|
1164
|
+
/** Catalog-ийн skip-ээс бусад түлхүүрийг " /Key value\n" хэлбэрээр (digitorus createCatalog). */
|
|
1165
|
+
catalogCopy(...skip) {
|
|
1166
|
+
const root = this.root();
|
|
1167
|
+
const rp = root.ptr;
|
|
1168
|
+
let s = "";
|
|
1169
|
+
for (const key of root.keys()) {
|
|
1170
|
+
if (key === "Type" || skip.includes(key)) continue;
|
|
1171
|
+
s += ` /${escapeName(key)} ` + serialize(root.key(key), rp.id, 0) + "\n";
|
|
1172
|
+
}
|
|
1173
|
+
return s;
|
|
1174
|
+
}
|
|
1175
|
+
};
|
|
1176
|
+
function trimSpace(b) {
|
|
1177
|
+
const ws = (c) => c === 9 || c === 10 || c === 11 || c === 12 || c === 13 || c === 32;
|
|
1178
|
+
let lo = 0;
|
|
1179
|
+
let hi = b.length;
|
|
1180
|
+
while (lo < hi && ws(b[lo])) lo++;
|
|
1181
|
+
while (hi > lo && ws(b[hi - 1])) hi--;
|
|
1182
|
+
return b.subarray(lo, hi);
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
// src/convert.ts
|
|
1186
|
+
var CONVERTER_PACKAGE = "@matbee/libreoffice-converter";
|
|
1187
|
+
var MAX_WORD_BYTES = 50 << 20;
|
|
1188
|
+
var NEAR_LIMIT = 0.9;
|
|
1189
|
+
var ConvertError = class extends Error {
|
|
1190
|
+
constructor(code, message, options) {
|
|
1191
|
+
super(`${code}: ${message}`);
|
|
1192
|
+
this.code = code;
|
|
1193
|
+
this.name = "ConvertError";
|
|
1194
|
+
if (options?.cause !== void 0) this.cause = options.cause;
|
|
1195
|
+
}
|
|
1196
|
+
code;
|
|
1197
|
+
/** Сүлжээ/asset-ийн түр алдаа — дахин оролдох утгатай. */
|
|
1198
|
+
get retryable() {
|
|
1199
|
+
return this.code === "ASSETS_UNAVAILABLE";
|
|
1200
|
+
}
|
|
1201
|
+
};
|
|
1202
|
+
var ASSETS = ["soffice.js", "soffice.wasm", "soffice.data", "soffice.worker.js", "browser.worker.global.js", "eid-convert.worker.js"];
|
|
1203
|
+
var BIG = ["soffice.wasm", "soffice.data"];
|
|
1204
|
+
var CACHE_PREFIX = "eid-convert-";
|
|
1205
|
+
function parseManifest(v) {
|
|
1206
|
+
const m = v;
|
|
1207
|
+
if (!m || m.format !== 1 || m.converter !== CONVERTER_PACKAGE || typeof m.version !== "string" || typeof m.dir !== "string" || !/^(?!\.{1,2}\/$)[A-Za-z0-9._-]+\/$/.test(m.dir) || !m.files) {
|
|
1208
|
+
throw new ConvertError("ASSETS_UNAVAILABLE", "manifest.json \u0431\u0443\u0440\u0443\u0443 \u2014 `eid-mongolia-convert-assets`-\u044D\u044D\u0440 \u0434\u0430\u0445\u0438\u043D \u04AF\u04AF\u0441\u0433\u044D");
|
|
1209
|
+
}
|
|
1210
|
+
for (const a of ASSETS) {
|
|
1211
|
+
const f = m.files[a];
|
|
1212
|
+
if (!f || !Number.isSafeInteger(f.bytes) || f.bytes <= 0) throw new ConvertError("ASSETS_UNAVAILABLE", `manifest.json-\u0434 ${a} \u0430\u043B\u0433\u0430`);
|
|
1213
|
+
}
|
|
1214
|
+
return m;
|
|
1215
|
+
}
|
|
1216
|
+
function base(assetsBase) {
|
|
1217
|
+
if (typeof assetsBase !== "string" || assetsBase === "") throw new TypeError("assetsBase \u0437\u0430\u0430\u0432\u0430\u043B");
|
|
1218
|
+
return new URL(assetsBase.endsWith("/") ? assetsBase : assetsBase + "/", globalThis.location?.href).href;
|
|
1219
|
+
}
|
|
1220
|
+
async function loadManifest(root, signal) {
|
|
1221
|
+
let res;
|
|
1222
|
+
try {
|
|
1223
|
+
res = await fetch(root + "manifest.json", { cache: "no-cache", signal });
|
|
1224
|
+
} catch (e) {
|
|
1225
|
+
if (signal?.aborted) throw abortError(signal);
|
|
1226
|
+
throw new ConvertError("ASSETS_UNAVAILABLE", "manifest.json \u0442\u0430\u0442\u0430\u0433\u0434\u0441\u0430\u043D\u0433\u04AF\u0439", { cause: e });
|
|
1227
|
+
}
|
|
1228
|
+
if (!res.ok) throw new ConvertError("ASSETS_UNAVAILABLE", `manifest.json HTTP ${res.status}`);
|
|
1229
|
+
let json;
|
|
1230
|
+
try {
|
|
1231
|
+
json = await res.json();
|
|
1232
|
+
} catch (e) {
|
|
1233
|
+
throw new ConvertError("ASSETS_UNAVAILABLE", "manifest.json JSON \u0431\u0438\u0448", { cause: e });
|
|
1234
|
+
}
|
|
1235
|
+
return parseManifest(json);
|
|
1236
|
+
}
|
|
1237
|
+
function cacheName(m) {
|
|
1238
|
+
return CACHE_PREFIX + m.version;
|
|
1239
|
+
}
|
|
1240
|
+
async function openCache(m) {
|
|
1241
|
+
try {
|
|
1242
|
+
return globalThis.caches ? await globalThis.caches.open(cacheName(m)) : null;
|
|
1243
|
+
} catch {
|
|
1244
|
+
return null;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
async function has(cache, url) {
|
|
1248
|
+
try {
|
|
1249
|
+
return !!cache && !!await cache.match(url);
|
|
1250
|
+
} catch {
|
|
1251
|
+
return false;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
async function converterStatus(opts) {
|
|
1255
|
+
const root = base(opts.assetsBase);
|
|
1256
|
+
const m = await loadManifest(root, opts.signal);
|
|
1257
|
+
const cache = await openCache(m);
|
|
1258
|
+
let missing = 0;
|
|
1259
|
+
for (const a of BIG) {
|
|
1260
|
+
const f = m.files[a];
|
|
1261
|
+
if (!await has(cache, root + m.dir + a)) missing += f.gzip ?? f.bytes;
|
|
1262
|
+
}
|
|
1263
|
+
return { cached: missing === 0, downloadBytes: missing };
|
|
1264
|
+
}
|
|
1265
|
+
async function prefetch(root, m, onProgress, signal) {
|
|
1266
|
+
const cache = await openCache(m);
|
|
1267
|
+
if (!cache) return null;
|
|
1268
|
+
const total = BIG.reduce((s, a) => s + m.files[a].bytes, 0);
|
|
1269
|
+
const need = [];
|
|
1270
|
+
let loaded = 0;
|
|
1271
|
+
for (const a of BIG) {
|
|
1272
|
+
if (await has(cache, root + m.dir + a)) loaded += m.files[a].bytes;
|
|
1273
|
+
else need.push(a);
|
|
1274
|
+
}
|
|
1275
|
+
if (need.length === 0) return cacheName(m);
|
|
1276
|
+
const report = () => onProgress?.({ phase: "download", percent: Math.min(100, Math.floor(loaded * 100 / total)), loaded, total });
|
|
1277
|
+
report();
|
|
1278
|
+
for (const a of need) {
|
|
1279
|
+
const url = root + m.dir + a;
|
|
1280
|
+
let res;
|
|
1281
|
+
try {
|
|
1282
|
+
res = await fetch(url, { signal });
|
|
1283
|
+
} catch (e) {
|
|
1284
|
+
if (signal?.aborted) throw abortError(signal);
|
|
1285
|
+
throw new ConvertError("ASSETS_UNAVAILABLE", `${a} \u0442\u0430\u0442\u0430\u0433\u0434\u0441\u0430\u043D\u0433\u04AF\u0439`, { cause: e });
|
|
1286
|
+
}
|
|
1287
|
+
if (!res.ok || !res.body) throw new ConvertError("ASSETS_UNAVAILABLE", `${a} HTTP ${res.status}`);
|
|
1288
|
+
let last = 0;
|
|
1289
|
+
const counted = res.body.pipeThrough(
|
|
1290
|
+
new TransformStream({
|
|
1291
|
+
transform(chunk, ctl) {
|
|
1292
|
+
loaded += chunk.byteLength;
|
|
1293
|
+
const now = Date.now();
|
|
1294
|
+
if (now - last > 100) {
|
|
1295
|
+
last = now;
|
|
1296
|
+
report();
|
|
1297
|
+
}
|
|
1298
|
+
ctl.enqueue(chunk);
|
|
1299
|
+
}
|
|
1300
|
+
})
|
|
1301
|
+
);
|
|
1302
|
+
const type = a.endsWith(".wasm") ? "application/wasm" : "application/octet-stream";
|
|
1303
|
+
try {
|
|
1304
|
+
await cache.put(url, new Response(counted, { headers: { "Content-Type": type } }));
|
|
1305
|
+
const size = (await (await cache.match(url))?.blob())?.size;
|
|
1306
|
+
if (size !== m.files[a].bytes) {
|
|
1307
|
+
await cache.delete(url);
|
|
1308
|
+
throw new ConvertError("ASSETS_UNAVAILABLE", `${a}: \u0445\u044D\u043C\u0436\u044D\u044D manifest-\u0442\u044D\u0439 \u0442\u0430\u0430\u0440\u0441\u0430\u043D\u0433\u04AF\u0439 (${size ?? 0} \u2260 ${m.files[a].bytes})`);
|
|
1309
|
+
}
|
|
1310
|
+
} catch (e) {
|
|
1311
|
+
if (signal?.aborted) throw abortError(signal);
|
|
1312
|
+
if (e instanceof ConvertError) throw e;
|
|
1313
|
+
throw new ConvertError("ASSETS_UNAVAILABLE", `${a} \u0442\u0430\u0442\u0430\u0445 \u044F\u0432\u0446 \u0442\u0430\u0441\u0430\u0440\u043B\u0430\u0430`, { cause: e });
|
|
1314
|
+
}
|
|
1315
|
+
report();
|
|
1316
|
+
}
|
|
1317
|
+
void dropOldCaches(m);
|
|
1318
|
+
return cacheName(m);
|
|
1319
|
+
}
|
|
1320
|
+
async function dropOldCaches(m) {
|
|
1321
|
+
try {
|
|
1322
|
+
for (const k of await globalThis.caches.keys()) if (k.startsWith(CACHE_PREFIX) && k !== cacheName(m)) await globalThis.caches.delete(k);
|
|
1323
|
+
} catch {
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
var live = null;
|
|
1327
|
+
var starting = null;
|
|
1328
|
+
var queue = Promise.resolve();
|
|
1329
|
+
function kill(l) {
|
|
1330
|
+
const w = l.conv.worker;
|
|
1331
|
+
try {
|
|
1332
|
+
w?.terminate();
|
|
1333
|
+
} catch {
|
|
1334
|
+
}
|
|
1335
|
+
if (live === l) live = null;
|
|
1336
|
+
}
|
|
1337
|
+
function disposeConverter() {
|
|
1338
|
+
if (live) kill(live);
|
|
1339
|
+
starting = null;
|
|
1340
|
+
}
|
|
1341
|
+
async function start(opts, signal) {
|
|
1342
|
+
const root = base(opts.assetsBase);
|
|
1343
|
+
const m = await loadManifest(root, signal);
|
|
1344
|
+
const key = root + m.dir;
|
|
1345
|
+
if (live && live.key === key) return live;
|
|
1346
|
+
if (live) kill(live);
|
|
1347
|
+
let mod;
|
|
1348
|
+
try {
|
|
1349
|
+
mod = await import("@matbee/libreoffice-converter/browser");
|
|
1350
|
+
} catch (e) {
|
|
1351
|
+
throw new ConvertError("ASSETS_UNAVAILABLE", `${CONVERTER_PACKAGE} \u0441\u0443\u0443\u0433\u0430\u0430\u0433\u04AF\u0439 (optional peerDependency)`, { cause: e });
|
|
1352
|
+
}
|
|
1353
|
+
const cache = await prefetch(root, m, opts.onProgress, signal);
|
|
1354
|
+
const l = { key, conv: null, sink: null };
|
|
1355
|
+
l.conv = new mod.WorkerBrowserConverter({
|
|
1356
|
+
sofficeJs: key + "soffice.js",
|
|
1357
|
+
sofficeWasm: key + "soffice.wasm",
|
|
1358
|
+
sofficeData: key + "soffice.data",
|
|
1359
|
+
sofficeWorkerJs: key + "soffice.worker.js",
|
|
1360
|
+
browserWorkerJs: key + "eid-convert.worker.js" + (cache ? "?cache=" + encodeURIComponent(cache) : ""),
|
|
1361
|
+
onProgress: (p) => l.sink?.(p)
|
|
1362
|
+
});
|
|
1363
|
+
live = l;
|
|
1364
|
+
return l;
|
|
1365
|
+
}
|
|
1366
|
+
function abortError(signal) {
|
|
1367
|
+
return signal.reason ?? new DOMException("\u0426\u0443\u0446\u043B\u0430\u0433\u0434\u0441\u0430\u043D", "AbortError");
|
|
1368
|
+
}
|
|
1369
|
+
function abortable(work, signal, onAbort) {
|
|
1370
|
+
if (!signal) return work;
|
|
1371
|
+
if (signal.aborted) {
|
|
1372
|
+
onAbort();
|
|
1373
|
+
return Promise.reject(abortError(signal));
|
|
1374
|
+
}
|
|
1375
|
+
return new Promise((resolve, reject) => {
|
|
1376
|
+
const stop = () => {
|
|
1377
|
+
onAbort();
|
|
1378
|
+
reject(abortError(signal));
|
|
1379
|
+
};
|
|
1380
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
1381
|
+
work.then(
|
|
1382
|
+
(v) => {
|
|
1383
|
+
signal.removeEventListener("abort", stop);
|
|
1384
|
+
resolve(v);
|
|
1385
|
+
},
|
|
1386
|
+
(e) => {
|
|
1387
|
+
signal.removeEventListener("abort", stop);
|
|
1388
|
+
reject(e);
|
|
1389
|
+
}
|
|
1390
|
+
);
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
var WORD_MIME = /* @__PURE__ */ new Set([
|
|
1394
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1395
|
+
"application/msword"
|
|
1396
|
+
]);
|
|
1397
|
+
function isWordFile(file) {
|
|
1398
|
+
if (typeof file === "string") return /\.(docx|doc)$/i.test(file.trim());
|
|
1399
|
+
const name = file.name;
|
|
1400
|
+
if (typeof name === "string" && name !== "") return /\.(docx|doc)$/i.test(name.trim());
|
|
1401
|
+
return WORD_MIME.has(file.type);
|
|
1402
|
+
}
|
|
1403
|
+
function sniff(b) {
|
|
1404
|
+
if (b.length >= 4 && b[0] === 80 && b[1] === 75 && b[2] === 3 && b[3] === 4) return "docx";
|
|
1405
|
+
if (b.length >= 8 && [208, 207, 17, 224, 161, 177, 26, 225].every((x, i) => b[i] === x)) return "doc";
|
|
1406
|
+
return null;
|
|
1407
|
+
}
|
|
1408
|
+
function pageCount(pdf) {
|
|
1409
|
+
const inc = new Incremental(pdf);
|
|
1410
|
+
let n = 0;
|
|
1411
|
+
const walk = (v, depth) => {
|
|
1412
|
+
if (depth > 32 || n > 1 << 16) return;
|
|
1413
|
+
const t = v.key("Type").name();
|
|
1414
|
+
if (t === "Page") n++;
|
|
1415
|
+
else if (t === "Pages") {
|
|
1416
|
+
const kids = v.key("Kids");
|
|
1417
|
+
for (let i = 0; i < kids.len(); i++) walk(kids.index(i), depth + 1);
|
|
1418
|
+
}
|
|
1419
|
+
};
|
|
1420
|
+
walk(inc.root().key("Pages"), 0);
|
|
1421
|
+
return n;
|
|
1422
|
+
}
|
|
1423
|
+
function latin12(b) {
|
|
1424
|
+
let s = "";
|
|
1425
|
+
for (let i = 0; i < b.length; i += 32768) s += String.fromCharCode(...b.subarray(i, i + 32768));
|
|
1426
|
+
return s;
|
|
1427
|
+
}
|
|
1428
|
+
function embeddedFonts(pdf) {
|
|
1429
|
+
const out = /* @__PURE__ */ new Set();
|
|
1430
|
+
for (const m of latin12(pdf).matchAll(/\/BaseFont\s*\/([^\s/<>[\]()%]+)/g)) {
|
|
1431
|
+
const name = m[1].replace(/#([0-9A-Fa-f]{2})/g, (_, h) => String.fromCharCode(parseInt(h, 16))).replace(/^[A-Z]{6}\+/, "");
|
|
1432
|
+
out.add(name);
|
|
1433
|
+
}
|
|
1434
|
+
return [...out];
|
|
1435
|
+
}
|
|
1436
|
+
var METRIC_ALIASES = {
|
|
1437
|
+
timesnewroman: "liberationserif",
|
|
1438
|
+
arial: "liberationsans",
|
|
1439
|
+
helvetica: "liberationsans",
|
|
1440
|
+
couriernew: "liberationmono",
|
|
1441
|
+
calibri: "carlito",
|
|
1442
|
+
cambria: "caladea"
|
|
1443
|
+
};
|
|
1444
|
+
var SYMBOL_FONTS = ["opensymbol", "symbol", "wingdings"];
|
|
1445
|
+
function norm(name) {
|
|
1446
|
+
return name.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
1447
|
+
}
|
|
1448
|
+
function fontWarnings(requested, embedded) {
|
|
1449
|
+
const emb = embedded.map(norm);
|
|
1450
|
+
const explained = /* @__PURE__ */ new Set();
|
|
1451
|
+
const missing = [];
|
|
1452
|
+
for (const r of requested) {
|
|
1453
|
+
const n = norm(r);
|
|
1454
|
+
if (n === "") continue;
|
|
1455
|
+
const alias = METRIC_ALIASES[n];
|
|
1456
|
+
let ok = false;
|
|
1457
|
+
emb.forEach((e, i) => {
|
|
1458
|
+
if (e.startsWith(n) || alias && e.startsWith(alias)) {
|
|
1459
|
+
ok = true;
|
|
1460
|
+
explained.add(i);
|
|
1461
|
+
}
|
|
1462
|
+
});
|
|
1463
|
+
if (!ok) missing.push(r);
|
|
1464
|
+
}
|
|
1465
|
+
const fallback = embedded.filter((_, i) => !explained.has(i) && !SYMBOL_FONTS.some((s) => emb[i].startsWith(s)));
|
|
1466
|
+
const out = [];
|
|
1467
|
+
if (missing.length) {
|
|
1468
|
+
out.push({ code: "FONT_SUBSTITUTED", fonts: missing, message: `\u0425\u0443\u0432\u0438\u0440\u0433\u0430\u0433\u0447\u0438\u0434 \u0431\u0430\u0439\u0445\u0433\u04AF\u0439 \u0444\u043E\u043D\u0442: ${missing.join(", ")} \u2014 \u04E9\u04E9\u0440 \u0444\u043E\u043D\u0442\u043E\u043E\u0440, \u043C\u04E9\u0440/\u0445\u0443\u0443\u0434\u0430\u0441 \u0448\u0438\u043B\u0436\u0441\u044D\u043D \u0431\u0430\u0439\u0436 \u0431\u043E\u043B\u043D\u043E` });
|
|
1469
|
+
}
|
|
1470
|
+
if (fallback.length && requested.length) {
|
|
1471
|
+
out.push({ code: "FONT_FALLBACK", fonts: fallback, message: `\u0417\u0430\u0440\u0438\u043C \u0442\u044D\u043C\u0434\u044D\u0433\u0442\u0438\u0439\u0433 \u04E9\u04E9\u0440 \u0444\u043E\u043D\u0442\u043E\u043E\u0440 \u0437\u0443\u0440\u0441\u0430\u043D: ${fallback.join(", ")}` });
|
|
1472
|
+
}
|
|
1473
|
+
return out;
|
|
1474
|
+
}
|
|
1475
|
+
function zipEntries(b, want) {
|
|
1476
|
+
const out = /* @__PURE__ */ new Map();
|
|
1477
|
+
const dv = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
1478
|
+
let eocd = -1;
|
|
1479
|
+
for (let i = b.length - 22; i >= Math.max(0, b.length - 22 - 65535); i--) {
|
|
1480
|
+
if (dv.getUint32(i, true) === 101010256) {
|
|
1481
|
+
eocd = i;
|
|
1482
|
+
break;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
if (eocd < 0) return out;
|
|
1486
|
+
const count = dv.getUint16(eocd + 10, true);
|
|
1487
|
+
let p = dv.getUint32(eocd + 16, true);
|
|
1488
|
+
const dec = new TextDecoder();
|
|
1489
|
+
for (let k = 0; k < count && p + 46 <= b.length; k++) {
|
|
1490
|
+
if (dv.getUint32(p, true) !== 33639248) break;
|
|
1491
|
+
const method = dv.getUint16(p + 10, true);
|
|
1492
|
+
const csize = dv.getUint32(p + 20, true);
|
|
1493
|
+
const usize = dv.getUint32(p + 24, true);
|
|
1494
|
+
const nlen = dv.getUint16(p + 28, true);
|
|
1495
|
+
const xlen = dv.getUint16(p + 30, true);
|
|
1496
|
+
const clen = dv.getUint16(p + 32, true);
|
|
1497
|
+
const local = dv.getUint32(p + 42, true);
|
|
1498
|
+
const name = dec.decode(b.subarray(p + 46, p + 46 + nlen));
|
|
1499
|
+
p += 46 + nlen + xlen + clen;
|
|
1500
|
+
if (!want(name) || usize > 16 << 20 || local + 30 > b.length || dv.getUint32(local, true) !== 67324752) continue;
|
|
1501
|
+
const start2 = local + 30 + dv.getUint16(local + 26, true) + dv.getUint16(local + 28, true);
|
|
1502
|
+
const data = b.subarray(start2, start2 + csize);
|
|
1503
|
+
try {
|
|
1504
|
+
if (method === 0) out.set(name, dec.decode(data));
|
|
1505
|
+
else if (method === 8) out.set(name, dec.decode(inflateRaw(data, 16 << 20)));
|
|
1506
|
+
} catch {
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
return out;
|
|
1510
|
+
}
|
|
1511
|
+
var RFONT_ATTR = /\bw:(ascii|hAnsi|cs|eastAsia)(Theme)?="([^"]+)"/g;
|
|
1512
|
+
function rFonts(xml, theme, into) {
|
|
1513
|
+
for (const tag of xml.matchAll(/<w:rFonts\b([^>]*)\/?>/g)) {
|
|
1514
|
+
for (const a of tag[1].matchAll(RFONT_ATTR)) {
|
|
1515
|
+
if (a[2]) {
|
|
1516
|
+
const t = a[3].startsWith("major") ? theme.major : theme.minor;
|
|
1517
|
+
if (t) into.add(t);
|
|
1518
|
+
} else into.add(a[3]);
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
function docxFonts(docx) {
|
|
1523
|
+
const files = zipEntries(docx, (n) => /^word\/(document|styles|header\d*|footer\d*)\.xml$|^word\/theme\/theme1\.xml$/.test(n));
|
|
1524
|
+
const themeXml = files.get("word/theme/theme1.xml") ?? "";
|
|
1525
|
+
const face = (tag) => new RegExp(`<a:${tag}>[\\s\\S]*?<a:latin\\s+typeface="([^"]*)"`).exec(themeXml)?.[1] || void 0;
|
|
1526
|
+
const theme = { major: face("majorFont"), minor: face("minorFont") };
|
|
1527
|
+
const fonts = /* @__PURE__ */ new Set();
|
|
1528
|
+
const body = [...files].filter(([n]) => /^word\/(document|header\d*|footer\d*)\.xml$/.test(n)).map(([, x]) => x).join("\n");
|
|
1529
|
+
rFonts(body, theme, fonts);
|
|
1530
|
+
const styles = files.get("word/styles.xml") ?? "";
|
|
1531
|
+
const defaults = /<w:docDefaults>[\s\S]*?<\/w:docDefaults>/.exec(styles)?.[0] ?? "";
|
|
1532
|
+
rFonts(defaults, theme, fonts);
|
|
1533
|
+
const byId = /* @__PURE__ */ new Map();
|
|
1534
|
+
for (const s of styles.matchAll(/<w:style\b[^>]*\bw:styleId="([^"]+)"[^>]*>[\s\S]*?<\/w:style>/g)) byId.set(s[1], s[0]);
|
|
1535
|
+
const used = /* @__PURE__ */ new Set();
|
|
1536
|
+
for (const u of body.matchAll(/<w:(?:pStyle|rStyle|tblStyle)\s+w:val="([^"]+)"/g)) used.add(u[1]);
|
|
1537
|
+
for (const s of styles.matchAll(/<w:style\b[^>]*\bw:type="paragraph"[^>]*\bw:default="1"[^>]*\bw:styleId="([^"]+)"|<w:style\b[^>]*\bw:default="1"[^>]*\bw:type="paragraph"[^>]*\bw:styleId="([^"]+)"/g)) {
|
|
1538
|
+
used.add(s[1] ?? s[2]);
|
|
1539
|
+
}
|
|
1540
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1541
|
+
const visit = (id, depth) => {
|
|
1542
|
+
if (seen.has(id) || depth > 16) return;
|
|
1543
|
+
seen.add(id);
|
|
1544
|
+
const xml = byId.get(id);
|
|
1545
|
+
if (!xml) return;
|
|
1546
|
+
rFonts(xml, theme, fonts);
|
|
1547
|
+
const based = /<w:basedOn\s+w:val="([^"]+)"/.exec(xml)?.[1];
|
|
1548
|
+
if (based) visit(based, depth + 1);
|
|
1549
|
+
};
|
|
1550
|
+
for (const id of used) visit(id, 0);
|
|
1551
|
+
return [...fonts];
|
|
1552
|
+
}
|
|
1553
|
+
function sizeWarnings(bytes) {
|
|
1554
|
+
const limit = MAX_INPUT_BYTES;
|
|
1555
|
+
const mb = (n) => (n / (1 << 20)).toFixed(2);
|
|
1556
|
+
if (bytes > limit) return [{ code: "PDF_TOO_LARGE", bytes, limit, message: `PDF ${mb(bytes)} MB \u2014 \u0433\u0430\u0440\u044B\u043D \u04AF\u0441\u0433\u0438\u0439\u043D \u0445\u044F\u0437\u0433\u0430\u0430\u0440 ${mb(limit)} MB` }];
|
|
1557
|
+
if (bytes > limit * NEAR_LIMIT) return [{ code: "PDF_NEAR_LIMIT", bytes, limit, message: `PDF ${mb(bytes)} MB \u2014 \u0433\u0430\u0440\u044B\u043D \u04AF\u0441\u0433\u0438\u0439\u043D \u0445\u044F\u0437\u0433\u0430\u0430\u0440\u0442 (${mb(limit)} MB) \u043E\u0439\u0440\u0445\u043E\u043D` }];
|
|
1558
|
+
return [];
|
|
1559
|
+
}
|
|
1560
|
+
async function wordToPdf(file, opts) {
|
|
1561
|
+
const signal = opts.signal;
|
|
1562
|
+
if (signal?.aborted) throw abortError(signal);
|
|
1563
|
+
if (typeof Worker === "undefined" || typeof WebAssembly === "undefined" || typeof window === "undefined") {
|
|
1564
|
+
throw new ConvertError("UNSUPPORTED_ENVIRONMENT", "\u0437\u04E9\u0432\u0445\u04E9\u043D \u0445\u04E9\u0442\u04E9\u0447 (Worker + WebAssembly)");
|
|
1565
|
+
}
|
|
1566
|
+
if (globalThis.crossOriginIsolated !== true || typeof SharedArrayBuffer === "undefined") {
|
|
1567
|
+
throw new ConvertError("NOT_ISOLATED", "\u0445\u0443\u0443\u0434\u0430\u0441 crossOriginIsolated \u0431\u0438\u0448 \u2014 COOP: same-origin, COEP: require-corp \u0442\u043E\u043B\u0433\u043E\u0439 \u0445\u044D\u0440\u044D\u0433\u0442\u044D\u0439 (hard navigation)");
|
|
1568
|
+
}
|
|
1569
|
+
const name = opts.fileName ?? (file instanceof Blob ? file.name : void 0) ?? "";
|
|
1570
|
+
if (name !== "" && !isWordFile(name)) throw new ConvertError("UNSUPPORTED_FILE", ".docx \u044D\u0441\u0432\u044D\u043B .doc \u0431\u0438\u0448");
|
|
1571
|
+
const size = file instanceof Blob ? file.size : file.byteLength;
|
|
1572
|
+
if (size === 0 || size > MAX_WORD_BYTES) throw new ConvertError("INPUT_TOO_LARGE", `Word \u0444\u0430\u0439\u043B 1 \u0431\u0430\u0439\u0442 \u2013 ${MAX_WORD_BYTES >> 20} MB \u0431\u0430\u0439\u0445 \u0451\u0441\u0442\u043E\u0439`);
|
|
1573
|
+
const bytes = file instanceof Blob ? new Uint8Array(await file.arrayBuffer()) : new Uint8Array(file);
|
|
1574
|
+
const kind = sniff(bytes);
|
|
1575
|
+
if (!kind) throw new ConvertError("UNSUPPORTED_FILE", ".docx \u044D\u0441\u0432\u044D\u043B .doc \u0431\u0438\u0448 (\u0430\u0433\u0443\u0443\u043B\u0433\u0430)");
|
|
1576
|
+
const run = async () => {
|
|
1577
|
+
let l = null;
|
|
1578
|
+
const onAbort = () => {
|
|
1579
|
+
if (l) kill(l);
|
|
1580
|
+
else disposeConverter();
|
|
1581
|
+
};
|
|
1582
|
+
try {
|
|
1583
|
+
starting ??= start(opts, signal);
|
|
1584
|
+
l = await abortable(starting, signal, () => {
|
|
1585
|
+
});
|
|
1586
|
+
} catch (e) {
|
|
1587
|
+
starting = null;
|
|
1588
|
+
throw e;
|
|
1589
|
+
}
|
|
1590
|
+
starting = null;
|
|
1591
|
+
const cur = l;
|
|
1592
|
+
try {
|
|
1593
|
+
cur.sink = (p) => opts.onProgress?.({ phase: "init", percent: clamp(p.percent) });
|
|
1594
|
+
opts.onProgress?.({ phase: "init", percent: 0 });
|
|
1595
|
+
await abortable(cur.conv.initialize(), signal, onAbort);
|
|
1596
|
+
opts.onProgress?.({ phase: "init", percent: 100 });
|
|
1597
|
+
cur.sink = (p) => opts.onProgress?.({ phase: "convert", percent: clamp(p.percent) });
|
|
1598
|
+
opts.onProgress?.({ phase: "convert", percent: 0 });
|
|
1599
|
+
const out = await abortable(cur.conv.convert(bytes.slice(), { outputFormat: "pdf" }, `document.${kind}`), signal, onAbort);
|
|
1600
|
+
const pdf = out.data instanceof Uint8Array ? out.data : new Uint8Array(out.data);
|
|
1601
|
+
let pages;
|
|
1602
|
+
try {
|
|
1603
|
+
pages = pageCount(pdf);
|
|
1604
|
+
} catch (e) {
|
|
1605
|
+
throw new ConvertError("CONVERT_FAILED", "\u0445\u0443\u0432\u0438\u0440\u0433\u0430\u0441\u0430\u043D PDF \u0443\u043D\u0448\u0438\u0433\u0434\u0441\u0430\u043D\u0433\u04AF\u0439", { cause: e });
|
|
1606
|
+
}
|
|
1607
|
+
if (pages === 0) throw new ConvertError("CONVERT_FAILED", "\u0445\u0443\u0432\u0438\u0440\u0433\u0430\u0441\u0430\u043D PDF \u0445\u043E\u043E\u0441\u043E\u043D");
|
|
1608
|
+
const warnings = [...sizeWarnings(pdf.length), ...kind === "docx" ? fontWarnings(docxFonts(bytes), embeddedFonts(pdf)) : []];
|
|
1609
|
+
opts.onProgress?.({ phase: "convert", percent: 100 });
|
|
1610
|
+
return { pdf, pages, warnings };
|
|
1611
|
+
} catch (e) {
|
|
1612
|
+
if (signal?.aborted) throw abortError(signal);
|
|
1613
|
+
if (e instanceof ConvertError) throw e;
|
|
1614
|
+
throw new ConvertError("CONVERT_FAILED", "LibreOffice \u0431\u0430\u0440\u0438\u043C\u0442\u044B\u0433 \u0445\u0443\u0432\u0438\u0440\u0433\u0430\u0441\u0430\u043D\u0433\u04AF\u0439", { cause: e });
|
|
1615
|
+
} finally {
|
|
1616
|
+
cur.sink = null;
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
const next = queue.then(run, run);
|
|
1620
|
+
queue = next.catch(() => {
|
|
1621
|
+
});
|
|
1622
|
+
return next;
|
|
1623
|
+
}
|
|
1624
|
+
function clamp(n) {
|
|
1625
|
+
return Number.isFinite(n) ? Math.max(0, Math.min(100, Math.round(n))) : 0;
|
|
1626
|
+
}
|
|
1627
|
+
export {
|
|
1628
|
+
CONVERTER_PACKAGE,
|
|
1629
|
+
ConvertError,
|
|
1630
|
+
MAX_WORD_BYTES,
|
|
1631
|
+
converterStatus,
|
|
1632
|
+
disposeConverter,
|
|
1633
|
+
docxFonts,
|
|
1634
|
+
embeddedFonts,
|
|
1635
|
+
fontWarnings,
|
|
1636
|
+
isWordFile,
|
|
1637
|
+
wordToPdf
|
|
1638
|
+
};
|