@gerege-systems/eid-mongolia-sdk 0.24.0 → 0.25.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 +12 -3
- package/dist/{chunk-T3QLOY6J.js → chunk-CRXEUCMP.js} +60 -0
- package/dist/convert.js +23 -0
- package/dist/pades.cjs +991 -8
- package/dist/pades.d.cts +26 -1
- package/dist/pades.d.ts +26 -1
- package/dist/pades.js +936 -8
- package/dist/validate.cjs +37 -14
- package/dist/validate.js +2 -15
- package/package.json +2 -2
package/dist/pades.js
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
DEFAULT_TSA_URL,
|
|
4
4
|
Fetcher,
|
|
5
5
|
Incremental,
|
|
6
|
+
PInt,
|
|
6
7
|
PRef,
|
|
7
8
|
PdfError,
|
|
8
9
|
Reader,
|
|
@@ -18,8 +19,9 @@ import {
|
|
|
18
19
|
round100,
|
|
19
20
|
serialize,
|
|
20
21
|
visibleRect,
|
|
21
|
-
visibleWidget
|
|
22
|
-
|
|
22
|
+
visibleWidget,
|
|
23
|
+
zlibInflateStrict
|
|
24
|
+
} from "./chunk-CRXEUCMP.js";
|
|
23
25
|
import {
|
|
24
26
|
CLAIMED_ROLE_MAX_RUNES,
|
|
25
27
|
CertificateRevokedError,
|
|
@@ -146,13 +148,915 @@ stream
|
|
|
146
148
|
return inc.finish(catID);
|
|
147
149
|
}
|
|
148
150
|
|
|
151
|
+
// src/pades/page/occupancy.ts
|
|
152
|
+
var MAX_BYTES = 4 << 20;
|
|
153
|
+
var MAX_OPS = 1e5;
|
|
154
|
+
var MAX_DEPTH = 8;
|
|
155
|
+
var MAX_SAVE = 64;
|
|
156
|
+
var MAX_OPERANDS = 65536;
|
|
157
|
+
var MAX_NESTING = 32;
|
|
158
|
+
var MAX_ANNOTS = 1e4;
|
|
159
|
+
var MAX_W_ENTRIES = 65536;
|
|
160
|
+
var MAX_W_ASSIGN = 262144;
|
|
161
|
+
var MAX_NUM_LEN = 32;
|
|
162
|
+
var CLAMP = 1e7;
|
|
163
|
+
var MAX_INHERIT = 32;
|
|
164
|
+
var UnscannableError = class extends Error {
|
|
165
|
+
};
|
|
166
|
+
var unscannable = (why) => new UnscannableError(`pades: page content not scannable: ${why}`);
|
|
167
|
+
var IDENTITY = [1, 0, 0, 1, 0, 0];
|
|
168
|
+
function mul(m, n) {
|
|
169
|
+
return [
|
|
170
|
+
m[0] * n[0] + m[1] * n[2],
|
|
171
|
+
m[0] * n[1] + m[1] * n[3],
|
|
172
|
+
m[2] * n[0] + m[3] * n[2],
|
|
173
|
+
m[2] * n[1] + m[3] * n[3],
|
|
174
|
+
m[4] * n[0] + m[5] * n[2] + n[4],
|
|
175
|
+
m[4] * n[1] + m[5] * n[3] + n[5]
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
function apply(m, x, y) {
|
|
179
|
+
return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
180
|
+
}
|
|
181
|
+
var finite = (m) => m.every((v) => Number.isFinite(v));
|
|
182
|
+
var pointRect = (x, y) => ({ x0: x, y0: y, x1: x, y1: y });
|
|
183
|
+
function addPoint(r, x, y) {
|
|
184
|
+
r.x0 = Math.min(r.x0, x);
|
|
185
|
+
r.y0 = Math.min(r.y0, y);
|
|
186
|
+
r.x1 = Math.max(r.x1, x);
|
|
187
|
+
r.y1 = Math.max(r.y1, y);
|
|
188
|
+
}
|
|
189
|
+
function quantise(r) {
|
|
190
|
+
const c = (v) => {
|
|
191
|
+
if (Number.isNaN(v)) throw unscannable("NaN");
|
|
192
|
+
return Math.min(Math.max(v, -CLAMP), CLAMP);
|
|
193
|
+
};
|
|
194
|
+
const x0 = c(r.x0);
|
|
195
|
+
const y0 = c(r.y0);
|
|
196
|
+
const x1 = c(r.x1);
|
|
197
|
+
const y1 = c(r.y1);
|
|
198
|
+
return [Math.floor(x0 * 100) + 0, Math.floor(y0 * 100) + 0, Math.ceil(x1 * 100) + 0, Math.ceil(y1 * 100) + 0];
|
|
199
|
+
}
|
|
200
|
+
var newFont = (kind) => ({ kind, first: 0, widths: [], missing: 1e3, dw: 1e3, w: /* @__PURE__ */ new Map() });
|
|
201
|
+
var DEFAULT_FONT = newFont(3 /* Default */);
|
|
202
|
+
var toInt = (v) => Math.min(Math.max(Math.trunc(v), -(2 ** 63)), 2 ** 63);
|
|
203
|
+
function number(v) {
|
|
204
|
+
const k = v.kind();
|
|
205
|
+
return k === "int" || k === "real" ? v.float64() : void 0;
|
|
206
|
+
}
|
|
207
|
+
function loadFont(v) {
|
|
208
|
+
switch (v.key("Subtype").name()) {
|
|
209
|
+
case "Type3":
|
|
210
|
+
return DEFAULT_FONT;
|
|
211
|
+
case "Type0": {
|
|
212
|
+
const enc2 = v.key("Encoding");
|
|
213
|
+
if (enc2.kind() !== "name" || enc2.name() !== "Identity-H" && enc2.name() !== "Identity-V") return newFont(2 /* Composite */);
|
|
214
|
+
const f2 = newFont(1 /* Identity */);
|
|
215
|
+
const d = v.key("DescendantFonts").index(0);
|
|
216
|
+
const dw = number(d.key("DW"));
|
|
217
|
+
if (dw !== void 0) f2.dw = dw;
|
|
218
|
+
const w = d.key("W");
|
|
219
|
+
const n = Math.min(w.len(), MAX_W_ENTRIES);
|
|
220
|
+
let budget = MAX_W_ASSIGN;
|
|
221
|
+
const set = (cid, val) => {
|
|
222
|
+
if (--budget < 0) return false;
|
|
223
|
+
if (!f2.w.has(cid) && cid >= 0 && cid <= 65535) f2.w.set(cid, val);
|
|
224
|
+
return true;
|
|
225
|
+
};
|
|
226
|
+
read: for (let i = 0; i < n; ) {
|
|
227
|
+
const c0f = number(w.index(i));
|
|
228
|
+
if (c0f === void 0 || i + 1 >= n) break;
|
|
229
|
+
const c0 = toInt(c0f);
|
|
230
|
+
const arr = w.index(i + 1);
|
|
231
|
+
if (arr.kind() === "array") {
|
|
232
|
+
const m2 = Math.min(arr.len(), MAX_W_ENTRIES);
|
|
233
|
+
for (let j = 0; j < m2; j++) {
|
|
234
|
+
const x2 = number(arr.index(j));
|
|
235
|
+
if (x2 !== void 0 && !set(c0 + j, x2)) break read;
|
|
236
|
+
}
|
|
237
|
+
i += 2;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const c1 = number(w.index(i + 1));
|
|
241
|
+
const x = number(w.index(i + 2));
|
|
242
|
+
if (c1 === void 0 || x === void 0 || i + 2 >= n) break;
|
|
243
|
+
const hi = toInt(c1);
|
|
244
|
+
for (let c = Math.max(c0, 0); c <= hi && c <= 65535; c++) {
|
|
245
|
+
if (!set(c, x)) break read;
|
|
246
|
+
}
|
|
247
|
+
i += 3;
|
|
248
|
+
}
|
|
249
|
+
return f2;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const f = newFont(0 /* Simple */);
|
|
253
|
+
const fc = number(v.key("FirstChar"));
|
|
254
|
+
if (fc !== void 0) f.first = toInt(fc);
|
|
255
|
+
const ws = v.key("Widths");
|
|
256
|
+
const m = Math.min(ws.len(), 256);
|
|
257
|
+
for (let i = 0; i < m; i++) f.widths.push(number(ws.index(i)) ?? NaN);
|
|
258
|
+
const mw = number(v.key("FontDescriptor").key("MissingWidth"));
|
|
259
|
+
if (mw !== void 0 && mw > 0) f.missing = mw;
|
|
260
|
+
return f;
|
|
261
|
+
}
|
|
262
|
+
function glyphs(f, b, fn) {
|
|
263
|
+
if (f.kind === 1 /* Identity */) {
|
|
264
|
+
for (let i = 0; i + 1 < b.length; i += 2) {
|
|
265
|
+
const w = f.w.get(b[i] << 8 | b[i + 1]);
|
|
266
|
+
fn(w ?? f.dw, false);
|
|
267
|
+
}
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
for (const c of b) {
|
|
271
|
+
let w = 1e3;
|
|
272
|
+
if (f.kind === 0 /* Simple */) {
|
|
273
|
+
w = f.missing;
|
|
274
|
+
const i = c - f.first;
|
|
275
|
+
if (i >= 0 && i < f.widths.length && !Number.isNaN(f.widths[i])) w = f.widths[i];
|
|
276
|
+
}
|
|
277
|
+
fn(w, f.kind !== 2 /* Composite */ && c === 32);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
var EMPTY = new Uint8Array(0);
|
|
281
|
+
var opnd = (kind, num = 0, str = EMPTY, name = "") => ({ kind, num, str, name, arr: [] });
|
|
282
|
+
var isSpace = (c) => c === 0 || c === 9 || c === 10 || c === 12 || c === 13 || c === 32;
|
|
283
|
+
var isDelim = (c) => c === 40 || c === 41 || c === 60 || c === 62 || c === 91 || c === 93 || c === 123 || c === 125 || c === 47 || c === 37;
|
|
284
|
+
function unhex(c) {
|
|
285
|
+
if (c >= 48 && c <= 57) return c - 48;
|
|
286
|
+
if (c >= 97 && c <= 102) return c - 97 + 10;
|
|
287
|
+
if (c >= 65 && c <= 70) return c - 65 + 10;
|
|
288
|
+
return -1;
|
|
289
|
+
}
|
|
290
|
+
function isNumber(b, s, e) {
|
|
291
|
+
if (e === s || e - s > MAX_NUM_LEN) return false;
|
|
292
|
+
let i = s;
|
|
293
|
+
if (b[i] === 43 || b[i] === 45) i++;
|
|
294
|
+
let digits = 0;
|
|
295
|
+
let dot = false;
|
|
296
|
+
for (; i < e; i++) {
|
|
297
|
+
const c = b[i];
|
|
298
|
+
if (c >= 48 && c <= 57) digits++;
|
|
299
|
+
else if (c === 46 && !dot) dot = true;
|
|
300
|
+
else return false;
|
|
301
|
+
}
|
|
302
|
+
return digits > 0;
|
|
303
|
+
}
|
|
304
|
+
var latin12 = (b, s, e) => {
|
|
305
|
+
let out = "";
|
|
306
|
+
for (let i = s; i < e; i++) out += String.fromCharCode(b[i]);
|
|
307
|
+
return out;
|
|
308
|
+
};
|
|
309
|
+
var EOF_TOKEN = { k: 0 /* EOF */, o: opnd(5 /* Other */), kw: "" };
|
|
310
|
+
var Lexer = class {
|
|
311
|
+
constructor(b) {
|
|
312
|
+
this.b = b;
|
|
313
|
+
}
|
|
314
|
+
b;
|
|
315
|
+
i = 0;
|
|
316
|
+
skipSpace() {
|
|
317
|
+
const b = this.b;
|
|
318
|
+
while (this.i < b.length) {
|
|
319
|
+
const c = b[this.i];
|
|
320
|
+
if (isSpace(c)) this.i++;
|
|
321
|
+
else if (c === 37) {
|
|
322
|
+
while (this.i < b.length && b[this.i] !== 13 && b[this.i] !== 10) this.i++;
|
|
323
|
+
} else return;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/** Regular bytes from the current position; returns the end index (this.i is advanced to it). */
|
|
327
|
+
regular() {
|
|
328
|
+
const b = this.b;
|
|
329
|
+
while (this.i < b.length && !isSpace(b[this.i]) && !isDelim(b[this.i])) this.i++;
|
|
330
|
+
return this.i;
|
|
331
|
+
}
|
|
332
|
+
/** After '(' : balanced parentheses, escapes; EOF ends the string. */
|
|
333
|
+
literal() {
|
|
334
|
+
const b = this.b;
|
|
335
|
+
const out = [];
|
|
336
|
+
let depth = 1;
|
|
337
|
+
while (this.i < b.length) {
|
|
338
|
+
let c = b[this.i++];
|
|
339
|
+
switch (c) {
|
|
340
|
+
case 40:
|
|
341
|
+
depth++;
|
|
342
|
+
break;
|
|
343
|
+
case 41:
|
|
344
|
+
if (--depth === 0) return Uint8Array.from(out);
|
|
345
|
+
break;
|
|
346
|
+
case 92: {
|
|
347
|
+
if (this.i >= b.length) return Uint8Array.from(out);
|
|
348
|
+
const e = b[this.i++];
|
|
349
|
+
switch (e) {
|
|
350
|
+
case 110:
|
|
351
|
+
c = 10;
|
|
352
|
+
break;
|
|
353
|
+
case 114:
|
|
354
|
+
c = 13;
|
|
355
|
+
break;
|
|
356
|
+
case 116:
|
|
357
|
+
c = 9;
|
|
358
|
+
break;
|
|
359
|
+
case 98:
|
|
360
|
+
c = 8;
|
|
361
|
+
break;
|
|
362
|
+
case 102:
|
|
363
|
+
c = 12;
|
|
364
|
+
break;
|
|
365
|
+
case 13:
|
|
366
|
+
if (this.i < b.length && b[this.i] === 10) this.i++;
|
|
367
|
+
continue;
|
|
368
|
+
case 10:
|
|
369
|
+
continue;
|
|
370
|
+
default:
|
|
371
|
+
if (e >= 48 && e <= 55) {
|
|
372
|
+
let v = e - 48;
|
|
373
|
+
for (let k = 0; k < 2 && this.i < b.length && b[this.i] >= 48 && b[this.i] <= 55; k++) v = v * 8 + (b[this.i++] - 48);
|
|
374
|
+
c = v & 255;
|
|
375
|
+
} else c = e;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
out.push(c);
|
|
380
|
+
}
|
|
381
|
+
return Uint8Array.from(out);
|
|
382
|
+
}
|
|
383
|
+
/** After '<' : hex digits, other bytes skipped, odd final nibble padded with 0; '>' or EOF ends it. */
|
|
384
|
+
hexString() {
|
|
385
|
+
const b = this.b;
|
|
386
|
+
const out = [];
|
|
387
|
+
let hi = 0;
|
|
388
|
+
let half = false;
|
|
389
|
+
while (this.i < b.length) {
|
|
390
|
+
const c = b[this.i++];
|
|
391
|
+
if (c === 62) break;
|
|
392
|
+
const v = unhex(c);
|
|
393
|
+
if (v < 0) continue;
|
|
394
|
+
if (half) out.push(hi << 4 | v);
|
|
395
|
+
else hi = v;
|
|
396
|
+
half = !half;
|
|
397
|
+
}
|
|
398
|
+
if (half) out.push(hi << 4);
|
|
399
|
+
return Uint8Array.from(out);
|
|
400
|
+
}
|
|
401
|
+
/** After '/' : regular bytes, #xx decoded (latin1, like the PDF object reader). */
|
|
402
|
+
name() {
|
|
403
|
+
const s = this.i;
|
|
404
|
+
const e = this.regular();
|
|
405
|
+
const t = this.b;
|
|
406
|
+
let out = "";
|
|
407
|
+
for (let i = s; i < e; i++) {
|
|
408
|
+
if (t[i] === 35 && i + 2 < e) {
|
|
409
|
+
const h = unhex(t[i + 1]);
|
|
410
|
+
const lo = unhex(t[i + 2]);
|
|
411
|
+
if (h >= 0 && lo >= 0) {
|
|
412
|
+
out += String.fromCharCode(h << 4 | lo);
|
|
413
|
+
i += 2;
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
out += String.fromCharCode(t[i]);
|
|
418
|
+
}
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
/** One token. Keywords come back as Lx.Keyword; '{' '}', stray ')' and a lone '>' are skipped. */
|
|
422
|
+
next() {
|
|
423
|
+
const b = this.b;
|
|
424
|
+
for (; ; ) {
|
|
425
|
+
this.skipSpace();
|
|
426
|
+
if (this.i >= b.length) return EOF_TOKEN;
|
|
427
|
+
switch (b[this.i]) {
|
|
428
|
+
case 40:
|
|
429
|
+
this.i++;
|
|
430
|
+
return { k: 1 /* Obj */, o: opnd(1 /* Str */, 0, this.literal()), kw: "" };
|
|
431
|
+
case 60:
|
|
432
|
+
if (this.i + 1 < b.length && b[this.i + 1] === 60) {
|
|
433
|
+
this.i += 2;
|
|
434
|
+
return { k: 1 /* Obj */, o: opnd(4 /* Dict */), kw: "<<" };
|
|
435
|
+
}
|
|
436
|
+
this.i++;
|
|
437
|
+
return { k: 1 /* Obj */, o: opnd(1 /* Str */, 0, this.hexString()), kw: "" };
|
|
438
|
+
case 62:
|
|
439
|
+
if (this.i + 1 < b.length && b[this.i + 1] === 62) {
|
|
440
|
+
this.i += 2;
|
|
441
|
+
return { k: 4 /* DictEnd */, o: EOF_TOKEN.o, kw: "" };
|
|
442
|
+
}
|
|
443
|
+
this.i++;
|
|
444
|
+
continue;
|
|
445
|
+
case 91:
|
|
446
|
+
this.i++;
|
|
447
|
+
return { k: 1 /* Obj */, o: opnd(3 /* Arr */), kw: "[" };
|
|
448
|
+
case 93:
|
|
449
|
+
this.i++;
|
|
450
|
+
return { k: 3 /* ArrEnd */, o: EOF_TOKEN.o, kw: "" };
|
|
451
|
+
case 47:
|
|
452
|
+
this.i++;
|
|
453
|
+
return { k: 1 /* Obj */, o: opnd(2 /* Name */, 0, EMPTY, this.name()), kw: "" };
|
|
454
|
+
case 41:
|
|
455
|
+
case 123:
|
|
456
|
+
case 125:
|
|
457
|
+
this.i++;
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
const s = this.i;
|
|
461
|
+
const e = this.regular();
|
|
462
|
+
if (isNumber(b, s, e)) return { k: 1 /* Obj */, o: opnd(0 /* Num */, Number(latin12(b, s, e))), kw: "" };
|
|
463
|
+
const t = latin12(b, s, e);
|
|
464
|
+
if (t === "true" || t === "false" || t === "null") return { k: 1 /* Obj */, o: opnd(5 /* Other */), kw: "" };
|
|
465
|
+
return { k: 2 /* Keyword */, o: EOF_TOKEN.o, kw: t };
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Completes an array or dictionary opened by next (open = "[" or "<<"). Inside, every token is an element; EOF ends
|
|
470
|
+
* the content (returns eof = true). Nesting > MAX_NESTING or too many elements → UnscannableError.
|
|
471
|
+
*/
|
|
472
|
+
object(o, open, depth, count) {
|
|
473
|
+
if (open === "") return false;
|
|
474
|
+
if (depth >= MAX_NESTING) throw unscannable("nesting");
|
|
475
|
+
for (; ; ) {
|
|
476
|
+
const t = this.next();
|
|
477
|
+
let e = t.o;
|
|
478
|
+
switch (t.k) {
|
|
479
|
+
case 0 /* EOF */:
|
|
480
|
+
return true;
|
|
481
|
+
case 3 /* ArrEnd */:
|
|
482
|
+
if (open === "[") return false;
|
|
483
|
+
continue;
|
|
484
|
+
case 4 /* DictEnd */:
|
|
485
|
+
if (open === "<<") return false;
|
|
486
|
+
continue;
|
|
487
|
+
case 2 /* Keyword */:
|
|
488
|
+
e = opnd(5 /* Other */);
|
|
489
|
+
break;
|
|
490
|
+
case 1 /* Obj */:
|
|
491
|
+
if (this.object(e, t.kw, depth + 1, count)) return true;
|
|
492
|
+
}
|
|
493
|
+
if (++count.n > MAX_OPERANDS) throw unscannable("operands");
|
|
494
|
+
if (open === "[") o.arr.push(e);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
function nums(ops, n) {
|
|
499
|
+
if (ops.length !== n) return void 0;
|
|
500
|
+
const out = [];
|
|
501
|
+
for (const o of ops) {
|
|
502
|
+
if (o.kind !== 0 /* Num */) return void 0;
|
|
503
|
+
out.push(o.num);
|
|
504
|
+
}
|
|
505
|
+
return out;
|
|
506
|
+
}
|
|
507
|
+
function closePath(sp) {
|
|
508
|
+
const seg = pointRect(sp.cx, sp.cy);
|
|
509
|
+
addPoint(seg, sp.sx, sp.sy);
|
|
510
|
+
sp.segs.push(seg);
|
|
511
|
+
sp.cx = sp.sx;
|
|
512
|
+
sp.cy = sp.sy;
|
|
513
|
+
}
|
|
514
|
+
var PATH_POINTS = { l: 2, c: 6, v: 4, y: 4 };
|
|
515
|
+
function inherited(page, key) {
|
|
516
|
+
let v = page;
|
|
517
|
+
for (let i = 0; i < MAX_INHERIT; i++) {
|
|
518
|
+
const x = v.key(key);
|
|
519
|
+
if (!x.isNull()) return x;
|
|
520
|
+
v = v.key("Parent");
|
|
521
|
+
if (v.kind() !== "dict") break;
|
|
522
|
+
}
|
|
523
|
+
return void 0;
|
|
524
|
+
}
|
|
525
|
+
var Scanner = class {
|
|
526
|
+
constructor(page) {
|
|
527
|
+
this.page = page;
|
|
528
|
+
}
|
|
529
|
+
page;
|
|
530
|
+
bytes = 0;
|
|
531
|
+
ops = 0;
|
|
532
|
+
boxes = [];
|
|
533
|
+
fonts = /* @__PURE__ */ new Map();
|
|
534
|
+
emit(r) {
|
|
535
|
+
this.boxes.push(quantise(r));
|
|
536
|
+
}
|
|
537
|
+
/** Font cache keyed by the containing object's number, as the Go reference (digitorus Value.GetPtr) does. */
|
|
538
|
+
font(v) {
|
|
539
|
+
if (v.kind() !== "dict") return DEFAULT_FONT;
|
|
540
|
+
const id = v.ptr.id;
|
|
541
|
+
const hit = this.fonts.get(id);
|
|
542
|
+
if (hit && id !== 0) return hit;
|
|
543
|
+
const f = loadFont(v);
|
|
544
|
+
if (id !== 0) this.fonts.set(id, f);
|
|
545
|
+
return f;
|
|
546
|
+
}
|
|
547
|
+
/** Decoded bytes of a content/form stream: no filter, or exactly FlateDecode without parameters. */
|
|
548
|
+
stream(v) {
|
|
549
|
+
if (v.kind() !== "stream") throw unscannable("not a stream");
|
|
550
|
+
const f = v.key("Filter");
|
|
551
|
+
const p = v.key("DecodeParms");
|
|
552
|
+
let flate;
|
|
553
|
+
if (f.isNull() && p.isNull()) flate = false;
|
|
554
|
+
else if (f.kind() === "name" && f.name() === "FlateDecode" && p.isNull()) flate = true;
|
|
555
|
+
else if (f.kind() === "array" && f.len() === 1 && f.index(0).kind() === "name" && f.index(0).name() === "FlateDecode" && (p.isNull() || p.kind() === "array" && p.len() === 1 && p.index(0).isNull())) {
|
|
556
|
+
flate = true;
|
|
557
|
+
} else throw unscannable("filter");
|
|
558
|
+
const x = v.data;
|
|
559
|
+
const n = v.key("Length").int64();
|
|
560
|
+
const raw = n > 0 ? v.r.bytes.subarray(x.offset, x.offset + n) : EMPTY;
|
|
561
|
+
const left = MAX_BYTES - this.bytes;
|
|
562
|
+
let b;
|
|
563
|
+
if (flate) {
|
|
564
|
+
try {
|
|
565
|
+
b = zlibInflateStrict(raw, left + 1);
|
|
566
|
+
} catch (e) {
|
|
567
|
+
throw unscannable(e instanceof Error ? e.message : "inflate");
|
|
568
|
+
}
|
|
569
|
+
} else b = raw.subarray(0, left + 1);
|
|
570
|
+
if (b.length > left) throw unscannable("content bytes");
|
|
571
|
+
this.bytes += b.length;
|
|
572
|
+
return b;
|
|
573
|
+
}
|
|
574
|
+
/** /Contents: absent → empty; a stream; or an array of streams joined with LF. */
|
|
575
|
+
pageContent(c) {
|
|
576
|
+
switch (c.kind()) {
|
|
577
|
+
case "null":
|
|
578
|
+
return EMPTY;
|
|
579
|
+
case "stream":
|
|
580
|
+
return this.stream(c);
|
|
581
|
+
case "array": {
|
|
582
|
+
const parts = [];
|
|
583
|
+
let total = 0;
|
|
584
|
+
for (let i = 0; i < c.len(); i++) {
|
|
585
|
+
const b = this.stream(c.index(i));
|
|
586
|
+
parts.push(b);
|
|
587
|
+
total += b.length + (i > 0 ? 1 : 0);
|
|
588
|
+
}
|
|
589
|
+
const out = new Uint8Array(total);
|
|
590
|
+
let at = 0;
|
|
591
|
+
parts.forEach((b, i) => {
|
|
592
|
+
if (i > 0) out[at++] = 10;
|
|
593
|
+
out.set(b, at);
|
|
594
|
+
at += b.length;
|
|
595
|
+
});
|
|
596
|
+
return out;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
throw unscannable("/Contents");
|
|
600
|
+
}
|
|
601
|
+
/** Executes one content stream (page content or a form) from graphics state g0, with its own operand stack, q stack,
|
|
602
|
+
* path and text matrices. */
|
|
603
|
+
run(content, res, g0, depth) {
|
|
604
|
+
const l = new Lexer(content);
|
|
605
|
+
let g = { ...g0 };
|
|
606
|
+
const stack = [];
|
|
607
|
+
let path = [];
|
|
608
|
+
let tm = IDENTITY;
|
|
609
|
+
let tlm = IDENTITY;
|
|
610
|
+
let ops = [];
|
|
611
|
+
const count = { n: 0 };
|
|
612
|
+
const key = (v, k) => v?.key(k);
|
|
613
|
+
const textLine = (ty) => {
|
|
614
|
+
tlm = mul([1, 0, 0, 1, 0, ty], tlm);
|
|
615
|
+
if (!finite(tlm)) throw unscannable("text matrix");
|
|
616
|
+
tm = tlm;
|
|
617
|
+
};
|
|
618
|
+
for (; ; ) {
|
|
619
|
+
const t = l.next();
|
|
620
|
+
if (t.k === 0 /* EOF */) return;
|
|
621
|
+
if (t.k === 3 /* ArrEnd */ || t.k === 4 /* DictEnd */) continue;
|
|
622
|
+
if (t.k === 1 /* Obj */) {
|
|
623
|
+
if (l.object(t.o, t.kw, 0, count)) return;
|
|
624
|
+
if (++count.n > MAX_OPERANDS) throw unscannable("operands");
|
|
625
|
+
ops.push(t.o);
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
if (++this.ops > MAX_OPS) throw unscannable("operators");
|
|
629
|
+
const args = ops;
|
|
630
|
+
ops = [];
|
|
631
|
+
count.n = 0;
|
|
632
|
+
const kw = t.kw;
|
|
633
|
+
switch (kw) {
|
|
634
|
+
case "q":
|
|
635
|
+
case "Q":
|
|
636
|
+
case "BT":
|
|
637
|
+
if (args.length !== 0) break;
|
|
638
|
+
if (kw === "BT") {
|
|
639
|
+
tm = IDENTITY;
|
|
640
|
+
tlm = IDENTITY;
|
|
641
|
+
} else if (kw === "Q") {
|
|
642
|
+
const s = stack.pop();
|
|
643
|
+
if (s) g = s;
|
|
644
|
+
} else {
|
|
645
|
+
if (stack.length >= MAX_SAVE) throw unscannable("q nesting");
|
|
646
|
+
stack.push({ ...g });
|
|
647
|
+
}
|
|
648
|
+
break;
|
|
649
|
+
case "cm": {
|
|
650
|
+
const v = nums(args, 6);
|
|
651
|
+
if (v) {
|
|
652
|
+
g.ctm = mul(v, g.ctm);
|
|
653
|
+
if (!finite(g.ctm)) throw unscannable("CTM");
|
|
654
|
+
}
|
|
655
|
+
break;
|
|
656
|
+
}
|
|
657
|
+
case "w": {
|
|
658
|
+
const v = nums(args, 1);
|
|
659
|
+
if (v) g.lw = v[0];
|
|
660
|
+
break;
|
|
661
|
+
}
|
|
662
|
+
case "g":
|
|
663
|
+
case "G": {
|
|
664
|
+
const v = nums(args, 1);
|
|
665
|
+
if (v) {
|
|
666
|
+
if (kw === "g") g.fillWhite = v[0] >= 1;
|
|
667
|
+
else g.strokeWhite = v[0] >= 1;
|
|
668
|
+
}
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
case "rg":
|
|
672
|
+
case "RG": {
|
|
673
|
+
const v = nums(args, 3);
|
|
674
|
+
if (v) {
|
|
675
|
+
const white = v[0] >= 1 && v[1] >= 1 && v[2] >= 1;
|
|
676
|
+
if (kw === "rg") g.fillWhite = white;
|
|
677
|
+
else g.strokeWhite = white;
|
|
678
|
+
}
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
case "k":
|
|
682
|
+
case "K": {
|
|
683
|
+
const v = nums(args, 4);
|
|
684
|
+
if (v) {
|
|
685
|
+
const white = v[0] <= 0 && v[1] <= 0 && v[2] <= 0 && v[3] <= 0;
|
|
686
|
+
if (kw === "k") g.fillWhite = white;
|
|
687
|
+
else g.strokeWhite = white;
|
|
688
|
+
}
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
case "cs":
|
|
692
|
+
case "sc":
|
|
693
|
+
case "scn":
|
|
694
|
+
g.fillWhite = false;
|
|
695
|
+
break;
|
|
696
|
+
case "CS":
|
|
697
|
+
case "SC":
|
|
698
|
+
case "SCN":
|
|
699
|
+
g.strokeWhite = false;
|
|
700
|
+
break;
|
|
701
|
+
// ── Path construction (points transformed by the CTM when given) ──
|
|
702
|
+
case "m": {
|
|
703
|
+
const v = nums(args, 2);
|
|
704
|
+
if (v) {
|
|
705
|
+
const [x, y] = apply(g.ctm, v[0], v[1]);
|
|
706
|
+
path.push({ sx: x, sy: y, cx: x, cy: y, bbox: pointRect(x, y), segs: [] });
|
|
707
|
+
}
|
|
708
|
+
break;
|
|
709
|
+
}
|
|
710
|
+
case "l":
|
|
711
|
+
case "c":
|
|
712
|
+
case "v":
|
|
713
|
+
case "y": {
|
|
714
|
+
const n = PATH_POINTS[kw];
|
|
715
|
+
const v = nums(args, n);
|
|
716
|
+
if (!v || path.length === 0) break;
|
|
717
|
+
const sp = path[path.length - 1];
|
|
718
|
+
const seg = pointRect(sp.cx, sp.cy);
|
|
719
|
+
for (let i = 0; i < n; i += 2) {
|
|
720
|
+
const [x, y] = apply(g.ctm, v[i], v[i + 1]);
|
|
721
|
+
addPoint(seg, x, y);
|
|
722
|
+
addPoint(sp.bbox, x, y);
|
|
723
|
+
sp.cx = x;
|
|
724
|
+
sp.cy = y;
|
|
725
|
+
}
|
|
726
|
+
sp.segs.push(seg);
|
|
727
|
+
break;
|
|
728
|
+
}
|
|
729
|
+
case "h":
|
|
730
|
+
if (args.length === 0 && path.length > 0) closePath(path[path.length - 1]);
|
|
731
|
+
break;
|
|
732
|
+
case "re": {
|
|
733
|
+
const v = nums(args, 4);
|
|
734
|
+
if (v) {
|
|
735
|
+
const [x0, y0] = apply(g.ctm, v[0], v[1]);
|
|
736
|
+
const p1 = apply(g.ctm, v[0] + v[2], v[1]);
|
|
737
|
+
const p2 = apply(g.ctm, v[0] + v[2], v[1] + v[3]);
|
|
738
|
+
const p3 = apply(g.ctm, v[0], v[1] + v[3]);
|
|
739
|
+
const sp = { sx: x0, sy: y0, cx: x0, cy: y0, bbox: pointRect(x0, y0), segs: [] };
|
|
740
|
+
for (const [px, py] of [p1, p2, p3]) {
|
|
741
|
+
const seg = pointRect(sp.cx, sp.cy);
|
|
742
|
+
addPoint(seg, px, py);
|
|
743
|
+
sp.segs.push(seg);
|
|
744
|
+
addPoint(sp.bbox, px, py);
|
|
745
|
+
sp.cx = px;
|
|
746
|
+
sp.cy = py;
|
|
747
|
+
}
|
|
748
|
+
closePath(sp);
|
|
749
|
+
path.push(sp);
|
|
750
|
+
}
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
case "S":
|
|
754
|
+
case "s":
|
|
755
|
+
case "f":
|
|
756
|
+
case "F":
|
|
757
|
+
case "f*":
|
|
758
|
+
case "B":
|
|
759
|
+
case "B*":
|
|
760
|
+
case "b":
|
|
761
|
+
case "b*":
|
|
762
|
+
case "n": {
|
|
763
|
+
if (args.length !== 0) break;
|
|
764
|
+
if ((kw === "s" || kw === "b" || kw === "b*") && path.length > 0) closePath(path[path.length - 1]);
|
|
765
|
+
const fill = kw !== "S" && kw !== "s" && kw !== "n";
|
|
766
|
+
const stroke = kw === "S" || kw === "s" || kw === "B" || kw === "B*" || kw === "b" || kw === "b*";
|
|
767
|
+
if (fill && !g.fillWhite) this.fill(path);
|
|
768
|
+
if (stroke && !g.strokeWhite) this.stroke(path, g);
|
|
769
|
+
path = [];
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
// ── Text ──
|
|
773
|
+
case "Tc":
|
|
774
|
+
case "Tw":
|
|
775
|
+
case "Tz":
|
|
776
|
+
case "TL":
|
|
777
|
+
case "Ts": {
|
|
778
|
+
const v = nums(args, 1);
|
|
779
|
+
if (!v) break;
|
|
780
|
+
if (kw === "Tc") g.tc = v[0];
|
|
781
|
+
else if (kw === "Tw") g.tw = v[0];
|
|
782
|
+
else if (kw === "Tz") g.th = v[0] / 100;
|
|
783
|
+
else if (kw === "TL") g.tl = v[0];
|
|
784
|
+
else g.trise = v[0];
|
|
785
|
+
break;
|
|
786
|
+
}
|
|
787
|
+
case "Tf":
|
|
788
|
+
if (args.length === 2 && args[0].kind === 2 /* Name */ && args[1].kind === 0 /* Num */) {
|
|
789
|
+
const fv = key(key(res, "Font"), args[0].name);
|
|
790
|
+
g.font = fv ? this.font(fv) : DEFAULT_FONT;
|
|
791
|
+
g.tfs = args[1].num;
|
|
792
|
+
}
|
|
793
|
+
break;
|
|
794
|
+
case "Td":
|
|
795
|
+
case "TD": {
|
|
796
|
+
const v = nums(args, 2);
|
|
797
|
+
if (v) {
|
|
798
|
+
if (kw === "TD") g.tl = -v[1];
|
|
799
|
+
tlm = mul([1, 0, 0, 1, v[0], v[1]], tlm);
|
|
800
|
+
if (!finite(tlm)) throw unscannable("text matrix");
|
|
801
|
+
tm = tlm;
|
|
802
|
+
}
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
case "T*":
|
|
806
|
+
if (args.length === 0) textLine(-g.tl);
|
|
807
|
+
break;
|
|
808
|
+
case "Tm": {
|
|
809
|
+
const v = nums(args, 6);
|
|
810
|
+
if (v) {
|
|
811
|
+
tm = v;
|
|
812
|
+
tlm = tm;
|
|
813
|
+
}
|
|
814
|
+
break;
|
|
815
|
+
}
|
|
816
|
+
case "Tj":
|
|
817
|
+
case "'":
|
|
818
|
+
case '"':
|
|
819
|
+
case "TJ": {
|
|
820
|
+
let items;
|
|
821
|
+
if ((kw === "Tj" || kw === "'") && args.length === 1 && args[0].kind === 1 /* Str */) items = args;
|
|
822
|
+
else if (kw === '"' && args.length === 3 && args[0].kind === 0 /* Num */ && args[1].kind === 0 /* Num */ && args[2].kind === 1 /* Str */) {
|
|
823
|
+
g.tw = args[0].num;
|
|
824
|
+
g.tc = args[1].num;
|
|
825
|
+
items = args.slice(2);
|
|
826
|
+
} else if (kw === "TJ" && args.length === 1 && args[0].kind === 3 /* Arr */) items = args[0].arr;
|
|
827
|
+
else break;
|
|
828
|
+
if (kw === "'" || kw === '"') textLine(-g.tl);
|
|
829
|
+
tm = this.show(items, g, tm);
|
|
830
|
+
break;
|
|
831
|
+
}
|
|
832
|
+
case "Do": {
|
|
833
|
+
if (args.length !== 1 || args[0].kind !== 2 /* Name */) break;
|
|
834
|
+
const x = key(key(res, "XObject"), args[0].name);
|
|
835
|
+
if (!x) break;
|
|
836
|
+
switch (x.key("Subtype").name()) {
|
|
837
|
+
case "Image":
|
|
838
|
+
this.unitSquare(g.ctm);
|
|
839
|
+
break;
|
|
840
|
+
case "Form": {
|
|
841
|
+
if (x.kind() !== "stream") break;
|
|
842
|
+
if (depth + 1 > MAX_DEPTH) throw unscannable("form depth");
|
|
843
|
+
let fm = IDENTITY;
|
|
844
|
+
const m = x.key("Matrix");
|
|
845
|
+
if (m.kind() === "array" && m.len() === 6) {
|
|
846
|
+
const v = [];
|
|
847
|
+
for (let i = 0; i < 6; i++) {
|
|
848
|
+
const e = number(m.index(i));
|
|
849
|
+
if (e === void 0) break;
|
|
850
|
+
v.push(e);
|
|
851
|
+
}
|
|
852
|
+
if (v.length === 6) fm = v;
|
|
853
|
+
}
|
|
854
|
+
const ctm = mul(fm, g.ctm);
|
|
855
|
+
if (!finite(ctm)) throw unscannable("form matrix");
|
|
856
|
+
let fres = x.key("Resources");
|
|
857
|
+
if (fres.kind() !== "dict") fres = res;
|
|
858
|
+
const b = this.stream(x);
|
|
859
|
+
this.run(b, fres, { ...g, ctm }, depth + 1);
|
|
860
|
+
break;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
case "BI":
|
|
866
|
+
this.inlineImage(l, g.ctm);
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
/** One box per subpath; a subpath box covering ≥ half of the page box is a background and is skipped. */
|
|
872
|
+
fill(path) {
|
|
873
|
+
const p = this.page;
|
|
874
|
+
for (const sp of path) {
|
|
875
|
+
const q = quantise(sp.bbox);
|
|
876
|
+
const w = Math.min(q[2], p[2]) - Math.max(q[0], p[0]);
|
|
877
|
+
const h = Math.min(q[3], p[3]) - Math.max(q[1], p[1]);
|
|
878
|
+
if (w > 0 && h > 0) {
|
|
879
|
+
const pw = p[2] - p[0];
|
|
880
|
+
const ph = p[3] - p[1];
|
|
881
|
+
const lhs = 2 * w * h;
|
|
882
|
+
const rhs = pw * ph;
|
|
883
|
+
const bg = lhs < 2 ** 53 && rhs < 2 ** 53 ? lhs >= rhs : 2n * BigInt(w) * BigInt(h) >= BigInt(pw) * BigInt(ph);
|
|
884
|
+
if (bg) continue;
|
|
885
|
+
}
|
|
886
|
+
this.boxes.push(q);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
/** One box per segment, widened by half the line width mapped through the CTM (|a|+|c|, |b|+|d|). */
|
|
890
|
+
stroke(path, g) {
|
|
891
|
+
const hw = Math.abs(g.lw) / 2;
|
|
892
|
+
const ex = hw * Math.abs(g.ctm[0]) + hw * Math.abs(g.ctm[2]);
|
|
893
|
+
const ey = hw * Math.abs(g.ctm[1]) + hw * Math.abs(g.ctm[3]);
|
|
894
|
+
for (const sp of path) {
|
|
895
|
+
for (const r of sp.segs) this.emit({ x0: r.x0 - ex, y0: r.y0 - ey, x1: r.x1 + ex, y1: r.y1 + ey });
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
unitSquare(ctm) {
|
|
899
|
+
const [x, y] = apply(ctm, 0, 0);
|
|
900
|
+
const r = pointRect(x, y);
|
|
901
|
+
for (const [px, py] of [
|
|
902
|
+
[1, 0],
|
|
903
|
+
[0, 1],
|
|
904
|
+
[1, 1]
|
|
905
|
+
]) {
|
|
906
|
+
const [qx, qy] = apply(ctm, px, py);
|
|
907
|
+
addPoint(r, qx, qy);
|
|
908
|
+
}
|
|
909
|
+
this.emit(r);
|
|
910
|
+
}
|
|
911
|
+
/** One box per show operator: [min, max] of glyph extents in text space × [rise − 0.3·Tfs, rise + Tfs], through Tm
|
|
912
|
+
* then the CTM. Returns the advanced Tm. */
|
|
913
|
+
show(items, g, tm) {
|
|
914
|
+
let x = 0;
|
|
915
|
+
let lo = 0;
|
|
916
|
+
let hi = 0;
|
|
917
|
+
let ext = false;
|
|
918
|
+
for (const it of items) {
|
|
919
|
+
if (it.kind === 0 /* Num */) {
|
|
920
|
+
x = x - it.num / 1e3 * g.tfs * g.th;
|
|
921
|
+
} else if (it.kind === 1 /* Str */) {
|
|
922
|
+
glyphs(g.font, it.str, (w0, space) => {
|
|
923
|
+
const gw = w0 / 1e3 * g.tfs * g.th;
|
|
924
|
+
const a = Math.min(x, x + gw);
|
|
925
|
+
const b = Math.max(x, x + gw);
|
|
926
|
+
if (!ext) {
|
|
927
|
+
lo = a;
|
|
928
|
+
hi = b;
|
|
929
|
+
ext = true;
|
|
930
|
+
} else {
|
|
931
|
+
lo = Math.min(lo, a);
|
|
932
|
+
hi = Math.max(hi, b);
|
|
933
|
+
}
|
|
934
|
+
let adv = w0 / 1e3 * g.tfs + g.tc;
|
|
935
|
+
if (space) adv = adv + g.tw;
|
|
936
|
+
x = x + adv * g.th;
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
if (ext) {
|
|
941
|
+
const ya = g.trise + g.tfs * -0.3;
|
|
942
|
+
const yb = g.trise + g.tfs;
|
|
943
|
+
let r;
|
|
944
|
+
for (const [px, py] of [
|
|
945
|
+
[lo, ya],
|
|
946
|
+
[hi, ya],
|
|
947
|
+
[lo, yb],
|
|
948
|
+
[hi, yb]
|
|
949
|
+
]) {
|
|
950
|
+
const [tx, ty] = apply(tm, px, py);
|
|
951
|
+
const [dx, dy] = apply(g.ctm, tx, ty);
|
|
952
|
+
if (!r) r = pointRect(dx, dy);
|
|
953
|
+
else addPoint(r, dx, dy);
|
|
954
|
+
}
|
|
955
|
+
this.emit(r);
|
|
956
|
+
}
|
|
957
|
+
const out = mul([1, 0, 0, 1, x, 0], tm);
|
|
958
|
+
if (!finite(out)) throw unscannable("text matrix");
|
|
959
|
+
return out;
|
|
960
|
+
}
|
|
961
|
+
/** BI <key value>… ID <data> EI: data ends at the first "EI" preceded by a whitespace byte and followed by a
|
|
962
|
+
* whitespace byte or the end of the content. No EI → the content ends (no box). */
|
|
963
|
+
inlineImage(l, ctm) {
|
|
964
|
+
const count = { n: 0 };
|
|
965
|
+
for (; ; ) {
|
|
966
|
+
const t = l.next();
|
|
967
|
+
if (t.k === 0 /* EOF */) return;
|
|
968
|
+
if (t.k === 1 /* Obj */) {
|
|
969
|
+
if (l.object(t.o, t.kw, 0, count)) return;
|
|
970
|
+
continue;
|
|
971
|
+
}
|
|
972
|
+
if (t.k === 2 /* Keyword */ && t.kw === "ID") break;
|
|
973
|
+
}
|
|
974
|
+
const b = l.b;
|
|
975
|
+
if (l.i < b.length && isSpace(b[l.i])) l.i++;
|
|
976
|
+
for (let p = l.i; p + 1 < b.length; p++) {
|
|
977
|
+
if (b[p] === 69 && b[p + 1] === 73 && p > 0 && isSpace(b[p - 1]) && (p + 2 === b.length || isSpace(b[p + 2]))) {
|
|
978
|
+
l.i = p + 2;
|
|
979
|
+
this.unitSquare(ctm);
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
l.i = b.length;
|
|
984
|
+
}
|
|
985
|
+
/** /Rect of every annotation except /Popup, hidden (/F bit 2) or NoView (/F bit 6) and zero-area rects. */
|
|
986
|
+
annots(a) {
|
|
987
|
+
if (a.kind() !== "array") return;
|
|
988
|
+
if (a.len() > MAX_ANNOTS) throw unscannable("annotations");
|
|
989
|
+
for (let i = 0; i < a.len(); i++) {
|
|
990
|
+
const an = a.index(i);
|
|
991
|
+
if (an.kind() !== "dict" || an.key("Subtype").name() === "Popup") continue;
|
|
992
|
+
const f = an.key("F");
|
|
993
|
+
if (f.data instanceof PInt && (f.data.v & 34n) !== 0n) continue;
|
|
994
|
+
const r = an.key("Rect");
|
|
995
|
+
if (r.kind() !== "array" || r.len() !== 4) continue;
|
|
996
|
+
const v = [];
|
|
997
|
+
for (let j = 0; j < 4; j++) {
|
|
998
|
+
const e = number(r.index(j));
|
|
999
|
+
if (e === void 0) break;
|
|
1000
|
+
v.push(e);
|
|
1001
|
+
}
|
|
1002
|
+
if (v.length !== 4 || v[0] === v[2] || v[1] === v[3]) continue;
|
|
1003
|
+
this.emit({ x0: Math.min(v[0], v[2]), y0: Math.min(v[1], v[3]), x1: Math.max(v[0], v[2]), y1: Math.max(v[1], v[3]) });
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
function occupancy(page, box) {
|
|
1008
|
+
try {
|
|
1009
|
+
const s = new Scanner(box);
|
|
1010
|
+
const content = s.pageContent(page.key("Contents"));
|
|
1011
|
+
const res = inherited(page, "Resources");
|
|
1012
|
+
s.run(content, res, { ctm: IDENTITY, lw: 1, fillWhite: false, strokeWhite: false, tc: 0, tw: 0, th: 1, tl: 0, tfs: 0, trise: 0, font: DEFAULT_FONT }, 0);
|
|
1013
|
+
s.annots(page.key("Annots"));
|
|
1014
|
+
return s.boxes;
|
|
1015
|
+
} catch (e) {
|
|
1016
|
+
if (e instanceof UnscannableError) throw e;
|
|
1017
|
+
throw new UnscannableError(`pades: page content not scannable: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
var PLACE_PAD = 400;
|
|
1021
|
+
var PLACE_STEP = 1200;
|
|
1022
|
+
var PLACE_STEPS = 100;
|
|
1023
|
+
function placeCandidates(vw, vh, x, y, w, h) {
|
|
1024
|
+
const xl = vw - x - w;
|
|
1025
|
+
const out = [[x, y]];
|
|
1026
|
+
if (xl !== x) out.push([xl, y]);
|
|
1027
|
+
const cols = xl !== x ? [x, xl] : [x];
|
|
1028
|
+
for (const cx of cols) {
|
|
1029
|
+
for (let k = 1; k <= PLACE_STEPS && y + k * PLACE_STEP + h <= vh - y; k++) out.push([cx, y + k * PLACE_STEP]);
|
|
1030
|
+
}
|
|
1031
|
+
return out;
|
|
1032
|
+
}
|
|
1033
|
+
function placeVisible(box, rot, x, y, w, h, occ) {
|
|
1034
|
+
let vw = box[2] - box[0];
|
|
1035
|
+
let vh = box[3] - box[1];
|
|
1036
|
+
if (rot === 90 || rot === 270) [vw, vh] = [vh, vw];
|
|
1037
|
+
for (const [cx, cy] of placeCandidates(vw, vh, x, y, w, h)) {
|
|
1038
|
+
let r;
|
|
1039
|
+
try {
|
|
1040
|
+
r = visibleRect(box, rot, cx, cy, w, h);
|
|
1041
|
+
} catch {
|
|
1042
|
+
continue;
|
|
1043
|
+
}
|
|
1044
|
+
const e0 = r[0] - PLACE_PAD;
|
|
1045
|
+
const e1 = r[1] - PLACE_PAD;
|
|
1046
|
+
const e2 = r[2] + PLACE_PAD;
|
|
1047
|
+
const e3 = r[3] + PLACE_PAD;
|
|
1048
|
+
if (!occ.some((b) => b[0] < e2 && e0 < b[2] && b[1] < e3 && e1 < b[3])) return [cx, cy];
|
|
1049
|
+
}
|
|
1050
|
+
return void 0;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
149
1053
|
// src/pades/pdf/signature.ts
|
|
150
1054
|
var MEASURED_MAX_CMS = 14 << 10;
|
|
151
1055
|
var PLACEHOLDER_BYTES = 2 * MEASURED_MAX_CMS;
|
|
152
1056
|
var SUB_FILTER = "ETSI.CAdES.detached";
|
|
153
1057
|
var REASON_MAX_UNITS = 127;
|
|
154
1058
|
var BYTE_RANGE_WIDTH = 40;
|
|
155
|
-
var
|
|
1059
|
+
var MAX_DEPTH2 = 32;
|
|
156
1060
|
var PlaceholderOverflowError = class extends Error {
|
|
157
1061
|
};
|
|
158
1062
|
function pdfDate(t) {
|
|
@@ -197,6 +1101,7 @@ async function prepareSignature(base, signingTime, signerName, reason, vis) {
|
|
|
197
1101
|
sig += "\n>>";
|
|
198
1102
|
const sigStart = inc.writeObject(sigID, sig);
|
|
199
1103
|
let page;
|
|
1104
|
+
let placement;
|
|
200
1105
|
if (!vis) {
|
|
201
1106
|
let fld = "<<\n /Type /Annot\n /Subtype /Widget\n /Rect [0 0 0 0]\n";
|
|
202
1107
|
page = firstPage(root.key("Pages"), 0);
|
|
@@ -213,7 +1118,24 @@ async function prepareSignature(base, signingTime, signerName, reason, vis) {
|
|
|
213
1118
|
page = nthPage(root.key("Pages"), vis.page);
|
|
214
1119
|
if (!page) throw new AppearanceError(`\u0445\u0443\u0443\u0434\u0430\u0441 ${vis.page} \u0430\u043B\u0433\u0430`);
|
|
215
1120
|
const { box, rot } = pageGeometry(page);
|
|
216
|
-
|
|
1121
|
+
let rect = visibleRect(box, rot, vis.x, vis.y, vis.w, vis.h);
|
|
1122
|
+
placement = { x: vis.x, y: vis.y, status: "requested" };
|
|
1123
|
+
if (vis.avoid) {
|
|
1124
|
+
let occ;
|
|
1125
|
+
try {
|
|
1126
|
+
occ = occupancy(page, box);
|
|
1127
|
+
} catch {
|
|
1128
|
+
placement.status = "unscanned";
|
|
1129
|
+
}
|
|
1130
|
+
if (occ) {
|
|
1131
|
+
const at = placeVisible(box, rot, vis.x, vis.y, vis.w, vis.h, occ);
|
|
1132
|
+
if (!at) placement.status = "overlap";
|
|
1133
|
+
else if (at[0] !== vis.x || at[1] !== vis.y) {
|
|
1134
|
+
placement = { x: at[0], y: at[1], status: "moved" };
|
|
1135
|
+
rect = visibleRect(box, rot, at[0], at[1], vis.w, vis.h);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
217
1139
|
const bodies = appearanceObjects({ w: vis.w, h: vis.h, rotate: rot, name: vis.name, role: vis.role, lang: vis.lang, date: vis.date, url: vis.url }, apIDs);
|
|
218
1140
|
inc.writeObject(fieldID, visibleWidget(fieldName, rect, page.ptr.id, page.ptr.gen, sigID, apIDs[0]));
|
|
219
1141
|
bodies.forEach((b2, i) => inc.writeObject(apIDs[i], b2));
|
|
@@ -247,7 +1169,7 @@ async function prepareSignature(base, signingTime, signerName, reason, vis) {
|
|
|
247
1169
|
const field = `[${br[0]} ${br[1]} ${br[2]} ${br[3]}]`;
|
|
248
1170
|
if (field.length > BYTE_RANGE_WIDTH) throw new PdfError("pades: ByteRange \u0442\u0430\u043B\u0431\u0430\u0440\u0442 \u0431\u0430\u0433\u0442\u0441\u0430\u043D\u0433\u04AF\u0439");
|
|
249
1171
|
doc.set(latin1(field), sigStart + brAt);
|
|
250
|
-
return { pdf: doc, byteRange: br, digest: await rangeDigest(doc, br), fieldName };
|
|
1172
|
+
return { pdf: doc, byteRange: br, digest: await rangeDigest(doc, br), fieldName, placement };
|
|
251
1173
|
}
|
|
252
1174
|
async function rangeDigest(doc, br) {
|
|
253
1175
|
const buf = new Uint8Array(br[1] + br[3]);
|
|
@@ -298,7 +1220,7 @@ function addWidgetToPage(inc, page, widget) {
|
|
|
298
1220
|
inc.writeObject(pgp.id, b, pgp.gen);
|
|
299
1221
|
}
|
|
300
1222
|
function firstPage(v, depth) {
|
|
301
|
-
if (depth >
|
|
1223
|
+
if (depth > MAX_DEPTH2) return void 0;
|
|
302
1224
|
switch (v.key("Type").name()) {
|
|
303
1225
|
case "Page":
|
|
304
1226
|
return v;
|
|
@@ -774,7 +1696,12 @@ async function prepare(pdf, opts) {
|
|
|
774
1696
|
preparedSha256: toHex(await sha256(prep.pdf)),
|
|
775
1697
|
pdf: prep.pdf
|
|
776
1698
|
});
|
|
777
|
-
|
|
1699
|
+
const out = { dtbs: await sha256(attrs), digest: prep.digest, docId, state };
|
|
1700
|
+
if (vis && prep.placement) {
|
|
1701
|
+
const pl = prep.placement;
|
|
1702
|
+
out.placement = { page: vis.page, x: pl.x / 100, y: pl.y / 100, width: vis.w / 100, height: vis.h / 100, status: pl.status };
|
|
1703
|
+
}
|
|
1704
|
+
return out;
|
|
778
1705
|
}
|
|
779
1706
|
function checkAppearance(o) {
|
|
780
1707
|
const a = o.appearance;
|
|
@@ -812,7 +1739,8 @@ function visibleSpec(doc, o, name, now, docId, revIndex) {
|
|
|
812
1739
|
role: o.claimedRole ?? "",
|
|
813
1740
|
lang: a.lang || o.lang || "mn",
|
|
814
1741
|
date: `${String(z.getUTCFullYear()).padStart(4, "0")}-${p2(z.getUTCMonth() + 1)}-${p2(z.getUTCDate())} ${p2(z.getUTCHours())}:${p2(z.getUTCMinutes())} (+08)`,
|
|
815
|
-
url: `${VERIFY_URL_PREFIX}${docId}/${revIndex}
|
|
1742
|
+
url: `${VERIFY_URL_PREFIX}${docId}/${revIndex}`,
|
|
1743
|
+
avoid: true
|
|
816
1744
|
};
|
|
817
1745
|
}
|
|
818
1746
|
var DEFAULT_LTV_RETRIES = 2;
|