@embedpdf/engine-services 3.0.0-next.12 → 3.0.0-next.13

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/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AbortError, EMPTY_TRANSFER, EngineError, EngineErrorCode, NO_ANNOTATION_FLAGS, PAGE_INSERT_BLANK_MAX_COUNT, PDF_SUBTYPE_TO_CODE, PdfAnnotationSubtypeCode, UNKNOWN_WEAK_ANNOTATION_STATE, appearanceImpactOf, buildPageTextLayout, buildSnippet, charMapViolation, charRangeForTextOffsets, decodeSubmitFormFlags, foldOptionsFor, foldText, invalidatesWeakIndexRefs, isValidPageObjectNumber, knownWeakAnnotationState, matchLiteral, matchRegex, normalizePdfRect, pdfQuadBounds, revisionTokensEqual, serializeError, shiftsExistingAnnotationIndices, sniffBinaryMetadata, subtypeFromCode, textSegmentsForRange, validateSearchQuery, wirePack } from "@embedpdf/engine-core/runtime";
1
+ import { AbortError, EMPTY_TRANSFER, EdgeResolver, EngineError, EngineErrorCode, NO_ANNOTATION_FLAGS, PAGE_INSERT_BLANK_MAX_COUNT, PDF_SUBTYPE_TO_CODE, PdfAnnotationSubtypeCode, SIGNATURE_POLICY_VERSION, UNKNOWN_WEAK_ANNOTATION_STATE, USAGE_INCOMPLETE, appearanceImpactOf, assessmentOf, buildPageTextLayout, buildSnippet, charMapViolation, charRangeForTextOffsets, combine, decodeSubmitFormFlags, deriveProtection, evaluateStep, fieldLockFor, foldOptionsFor, foldText, invalidatesWeakIndexRefs, isValidPageObjectNumber, knownWeakAnnotationState, matchLiteral, matchRegex, normalizePdfRect, parsePdfValue, pdfQuadBounds, restrictionsFor, revisionTokensEqual, richTextParagraphsFromPlainText, richTextPlainText, sameEffectiveValue, serializeError, shiftsExistingAnnotationIndices, sniffBinaryMetadata, subtypeFromCode, textSegmentsForRange, validateSearchQuery, wirePack } from "@embedpdf/engine-core/runtime";
2
2
  import { NULL_PTR } from "@embedpdf/engine-runtime";
3
3
  //#region src/runtime/lifecycle/bootstrap.ts
4
4
  const initialized = /* @__PURE__ */ new WeakSet();
@@ -270,8 +270,108 @@ var SessionEventPublisher = class {
270
270
  }
271
271
  };
272
272
  //#endregion
273
+ //#region src/runtime/memory/scratch.ts
274
+ /**
275
+ * Allocate a scratch buffer, run `body` with it, and always free it.
276
+ *
277
+ * Every PDFium out-parameter call follows the same shape: malloc a
278
+ * buffer, hand its pointer to the native function, read the result back,
279
+ * free the buffer. Hand-rolling that `try/finally` at each call site is
280
+ * how leaks and copy-paste bugs creep in, so it lives here once.
281
+ */
282
+ function withScratch(mem, bytes, body) {
283
+ const ptr = mem.alloc(bytes);
284
+ try {
285
+ return body(ptr);
286
+ } finally {
287
+ mem.free(ptr);
288
+ }
289
+ }
290
+ /**
291
+ * Like {@link withScratch} but for native calls that take several
292
+ * out-parameters at once (e.g. `FPDFAnnot_GetColor` with one pointer per
293
+ * channel). Buffers are freed in reverse allocation order.
294
+ */
295
+ function withScratchN(mem, sizes, body) {
296
+ const ptrs = sizes.map((size) => mem.alloc(size));
297
+ try {
298
+ return body(ptrs);
299
+ } finally {
300
+ for (let i = ptrs.length - 1; i >= 0; i--) mem.free(ptrs[i]);
301
+ }
302
+ }
303
+ //#endregion
304
+ //#region src/runtime/memory/strings.ts
305
+ /**
306
+ * Read a PDFium UTF-16LE string getter that uses the standard
307
+ * (probe length, allocate, read) ABI. `call(buf, capacity)` must invoke
308
+ * the native function and return the length in BYTES including the
309
+ * trailing NUL (the value PDFium reports when `capacity` is 0).
310
+ *
311
+ * `emptyAs` decides what a present-but-empty value (`len === 2`, a lone
312
+ * UTF-16 NUL) decodes to. This is the one knob different callers actually
313
+ * disagree on: document Info strings treat `''` as a real value, whereas
314
+ * a page label of `''` is indistinguishable from "absent" and reads as
315
+ * `null`. Making it a parameter keeps that decision explicit instead of
316
+ * forking the buffer dance.
317
+ *
318
+ * Returns `null` when the value is absent (`len <= 0`).
319
+ */
320
+ function readUtf16String(mem, call, emptyAs = "") {
321
+ const len = call(NULL_PTR, 0);
322
+ if (len <= 0) return null;
323
+ if (len === 2) return emptyAs;
324
+ return withScratch(mem, len, (buf) => {
325
+ if (call(buf, len) <= 0) return null;
326
+ return mem.readU16String(buf);
327
+ });
328
+ }
329
+ /**
330
+ * UTF-8 sibling of {@link readUtf16String} for the handful of native
331
+ * getters that return UTF-8 (e.g. `EPDF_GetMetaKeyName`). UTF-8 has no
332
+ * fixed empty-NUL width to special-case, so an empty value decodes to
333
+ * `''`; absent (`len <= 0`) is `null`.
334
+ */
335
+ function readUtf8String(mem, call) {
336
+ const len = call(NULL_PTR, 0);
337
+ if (len <= 0) return null;
338
+ return withScratch(mem, len, (buf) => {
339
+ if (call(buf, len) <= 0) return null;
340
+ return mem.readU8String(buf);
341
+ });
342
+ }
343
+ /**
344
+ * Write side of the UTF-16 convention: encode `value` into a scratch
345
+ * buffer, hand the pointer to `set`, and always free it. `set` returns
346
+ * the native call's success flag, which is propagated.
347
+ */
348
+ function writeUtf16String(mem, value, set) {
349
+ const ptr = mem.writeU16String(value);
350
+ try {
351
+ return set(ptr);
352
+ } finally {
353
+ mem.free(ptr);
354
+ }
355
+ }
356
+ /**
357
+ * A NUL-terminated UTF-8 copy of `value` in runtime memory for the duration
358
+ * of `fn` (a `FPDF_STRING` parameter: a file system path, say).
359
+ */
360
+ function withUtf8CString(mem, value, fn) {
361
+ const encoded = new TextEncoder().encode(value);
362
+ const bytes = new Uint8Array(encoded.byteLength + 1);
363
+ bytes.set(encoded);
364
+ const ptr = mem.alloc(bytes.byteLength);
365
+ try {
366
+ mem.writeBytes(ptr, bytes);
367
+ return fn(ptr);
368
+ } finally {
369
+ mem.free(ptr);
370
+ }
371
+ }
372
+ //#endregion
273
373
  //#region src/document-session/lifecycle/PdfDocumentOpener.ts
274
- const FPDF_ERR_PASSWORD$1 = 4;
374
+ const FPDF_ERR_PASSWORD$2 = 4;
275
375
  var CloseStack = class {
276
376
  constructor() {
277
377
  _defineProperty(this, "actions", []);
@@ -306,7 +406,7 @@ function openFatMemoryDocument(runtime, bytes, password = null) {
306
406
  mem.writeBytes(dataPtr, bytes);
307
407
  const docPtr = fn.FPDF_LoadMemDocument64(dataPtr, bytes.byteLength, password ?? "");
308
408
  if (!docPtr) {
309
- if (fn.FPDF_GetLastError() === FPDF_ERR_PASSWORD$1) throw password ? new EngineError(EngineErrorCode.DocPasswordIncorrect, "incorrect document password") : new EngineError(EngineErrorCode.DocPasswordRequired, "document requires a password");
409
+ if (fn.FPDF_GetLastError() === FPDF_ERR_PASSWORD$2) throw password ? new EngineError(EngineErrorCode.DocPasswordIncorrect, "incorrect document password") : new EngineError(EngineErrorCode.DocPasswordRequired, "document requires a password");
310
410
  throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to open document");
311
411
  }
312
412
  setRuntimeOwnerPermissionsIfEncrypted(runtime, docPtr);
@@ -314,6 +414,11 @@ function openFatMemoryDocument(runtime, bytes, password = null) {
314
414
  return {
315
415
  kind: "fat-memory",
316
416
  docPtr,
417
+ source: {
418
+ base: null,
419
+ layer: null,
420
+ password
421
+ },
317
422
  close: () => stack.close()
318
423
  };
319
424
  } catch (error) {
@@ -331,6 +436,7 @@ function openLayerDocument(runtime, base, layer = { kind: "fresh" }, password =
331
436
  mem.poke(statusPtr, "i32", -1);
332
437
  let docPtr;
333
438
  if (layer.kind === "fresh") docPtr = fn.EPDFLayer_OpenLayer(base.basePtr, NULL_PTR, password ?? "", statusPtr);
439
+ else if (layer.kind === "artifact-file" && runtime.kind === "native") docPtr = withUtf8CString(mem, layer.path, (pathPtr) => fn.EPDFLayer_OpenLayerArtifactFromPath(base.basePtr, pathPtr, password ?? "", statusPtr));
334
440
  else {
335
441
  layerAccess = layer.kind === "artifact-file" ? runtime.fileAccess.fromNodeFile(layer.path) : runtime.fileAccess.fromMemory(layer.bytes);
336
442
  stack.push(() => layerAccess?.close());
@@ -340,9 +446,22 @@ function openLayerDocument(runtime, base, layer = { kind: "fresh" }, password =
340
446
  if (!docPtr || status !== 0) throw layerOpenError(status);
341
447
  setRuntimeOwnerPermissionsIfEncrypted(runtime, docPtr);
342
448
  stack.push(() => fn.FPDF_CloseDocument(docPtr));
449
+ const baseSource = base.kind === "file" && base.path !== void 0 ? {
450
+ kind: "file",
451
+ key: base.key,
452
+ path: base.path
453
+ } : {
454
+ kind: "memory",
455
+ key: base.key
456
+ };
343
457
  return {
344
458
  kind: "layer",
345
459
  docPtr,
460
+ source: {
461
+ base: baseSource,
462
+ layer,
463
+ password
464
+ },
346
465
  close: () => stack.close()
347
466
  };
348
467
  } catch (error) {
@@ -495,22 +614,13 @@ var LocalRevisionAuthority = class {
495
614
  };
496
615
  //#endregion
497
616
  //#region src/document-session/DocumentSession.ts
498
- /**
499
- * Owns the lifecycle of a single open PDFium document and the v3
500
- * identity machinery: page registry (pageObjectNumber <-> pageIndex),
501
- * `RevisionAuthority` (per-page generation counters), and `PagePtrPool`
502
- * (refcounted pagePtr access).
503
- *
504
- * Both the local browser Worker and the server worker_thread instantiate
505
- * this exactly the same way; the only thing that differs is the
506
- * underlying PdfRuntimeModule (WASM vs native).
507
- */
508
617
  var DocumentSession = class {
509
618
  constructor(runtime, sessionId) {
510
619
  this.runtime = runtime;
511
620
  _defineProperty(this, "docPtr", null);
512
621
  _defineProperty(this, "closeDocument", null);
513
622
  _defineProperty(this, "_kind", null);
623
+ _defineProperty(this, "_source", null);
514
624
  _defineProperty(this, "_sessionId", void 0);
515
625
  _defineProperty(this, "recordsByObjectNumber", /* @__PURE__ */ new Map());
516
626
  _defineProperty(this, "recordsByIndex", /* @__PURE__ */ new Map());
@@ -518,6 +628,59 @@ var DocumentSession = class {
518
628
  _defineProperty(this, "revisions", null);
519
629
  _defineProperty(this, "mutationSeqCounter", 0);
520
630
  _defineProperty(this, "pages", null);
631
+ _defineProperty(
632
+ this,
633
+ /**
634
+ * Whether writers honour what the document's signatures forbid (locked
635
+ * fields, structural edits). Set at open from the engine option; the
636
+ * main-thread guard subtracts the matching capabilities.
637
+ */
638
+ "signedDocumentPolicy",
639
+ "protect"
640
+ );
641
+ _defineProperty(
642
+ this,
643
+ /**
644
+ * The shape the caller asked for when opening plain bytes. `layer` (the
645
+ * default) opens them as an immutable base with a fresh layer; `plain`
646
+ * keeps one in-memory document unless the bytes already carry a
647
+ * signature, which always opens as a layer.
648
+ */
649
+ "sessionKind",
650
+ "layer"
651
+ );
652
+ _defineProperty(
653
+ this,
654
+ /** The password the document was opened with; signing candidates open with the same one. */
655
+ "password",
656
+ null
657
+ );
658
+ _defineProperty(
659
+ this,
660
+ /**
661
+ * Whether mutations on a layer session also serialize the layer artifact
662
+ * into their response (what a server persists). False for a session that
663
+ * became a layer only because its document is signed (see
664
+ * `WorkerHost.openSignedAware`): the caller opened plain bytes and never
665
+ * asked for artifacts.
666
+ */
667
+ "persistLayerArtifact",
668
+ true
669
+ );
670
+ _defineProperty(this, "loadedSeq", 0);
671
+ _defineProperty(this, "plainSha256", null);
672
+ _defineProperty(
673
+ this,
674
+ /** The signing candidate parked by `signatures.prepare`, if any. */
675
+ "pendingSigning",
676
+ null
677
+ );
678
+ _defineProperty(
679
+ this,
680
+ /** The last completed signing, so a replayed `complete` answers `already-completed`. */
681
+ "lastCompletion",
682
+ null
683
+ );
521
684
  _defineProperty(this, "parkedBytes", null);
522
685
  _defineProperty(this, "retained", []);
523
686
  this._sessionId = sessionId ?? generateSessionId();
@@ -528,6 +691,15 @@ var DocumentSession = class {
528
691
  get kind() {
529
692
  return this._kind;
530
693
  }
694
+ /**
695
+ * Where the current bytes come from: the immutable base (registry key,
696
+ * file path for file bases) and the layer the session was opened with.
697
+ * One interpretation for signing candidates, overlays and verbatim reads.
698
+ */
699
+ get source() {
700
+ if (!this._source) throw new EngineError(EngineErrorCode.DocNotOpen, "document is not open");
701
+ return this._source;
702
+ }
531
703
  isOpen() {
532
704
  return this.docPtr !== null;
533
705
  }
@@ -542,9 +714,76 @@ var DocumentSession = class {
542
714
  this.docPtr = handle.docPtr;
543
715
  this.closeDocument = () => handle.close();
544
716
  this._kind = handle.kind;
717
+ this._source = handle.source;
545
718
  this.revisions = new LocalRevisionAuthority(this._sessionId);
546
719
  this.pages = new PagePtrPool(this.runtime, handle.docPtr);
547
720
  this.parkedBytes = null;
721
+ this.loadedSeq = this.mutationSeqCounter;
722
+ }
723
+ /**
724
+ * Whether anything was mutated since the current bytes were loaded. When
725
+ * false, the loaded bytes ARE the document: a save returns them verbatim
726
+ * and a signing candidate is built straight on them.
727
+ */
728
+ hasUnsavedEdits() {
729
+ return this.mutationSeqCounter !== this.loadedSeq;
730
+ }
731
+ /** Cached hash of the current loaded bytes, or `null` when not computed since the last load. */
732
+ cachedPlainSha256() {
733
+ return this.plainSha256 && this.plainSha256.loadedSeq === this.loadedSeq ? this.plainSha256.sha256 : null;
734
+ }
735
+ rememberPlainSha256(sha256) {
736
+ this.plainSha256 = {
737
+ loadedSeq: this.loadedSeq,
738
+ sha256
739
+ };
740
+ }
741
+ /**
742
+ * Re-point this session at new bytes — the one operation that changes
743
+ * what an open session is backed by, used when a completed signature
744
+ * installs its sealed file. The session id, its page object numbers
745
+ * (an incremental save never renumbers) and its retained resources
746
+ * survive; the old document is closed, every page is re-pinned (its
747
+ * revision bumped, its cached pointer dropped), and the mutation
748
+ * sequence advances so every version-keyed cache rebuilds.
749
+ */
750
+ install(handle) {
751
+ if (!this.docPtr) {
752
+ handle.close();
753
+ throw new EngineError(EngineErrorCode.DocNotOpen, "no document to replace");
754
+ }
755
+ const previousPages = Array.from(this.recordsByObjectNumber.keys());
756
+ let firstError = null;
757
+ try {
758
+ this.pages?.closeAll();
759
+ } catch (error) {
760
+ firstError = error;
761
+ }
762
+ try {
763
+ this.closeDocument?.();
764
+ } catch (error) {
765
+ firstError ?? (firstError = error);
766
+ }
767
+ this.docPtr = handle.docPtr;
768
+ this.closeDocument = () => handle.close();
769
+ this._kind = handle.kind;
770
+ this._source = handle.source;
771
+ this.pages = new PagePtrPool(this.runtime, handle.docPtr);
772
+ this.recordsByIndex.clear();
773
+ this.recordsByObjectNumber.clear();
774
+ this.fullyEnumerated = false;
775
+ for (const pon of previousPages) this.requireRevisions().bump(pon);
776
+ this.mutationSeqCounter++;
777
+ this.loadedSeq = this.mutationSeqCounter;
778
+ this.pendingSigning = null;
779
+ if (firstError) throw firstError;
780
+ }
781
+ /** The two publish fences a candidate is built on. */
782
+ versionRef(baseSha256) {
783
+ return {
784
+ baseSha256,
785
+ editsVersion: this.mutationSeqCounter
786
+ };
548
787
  }
549
788
  /**
550
789
  * Park this session in the password-locked state: the document could not
@@ -704,6 +943,11 @@ var DocumentSession = class {
704
943
  } finally {
705
944
  this.pages = null;
706
945
  }
946
+ if (this.pendingSigning?.saved.kind === "file") try {
947
+ this.runtime.fileWrite.removeFile(this.pendingSigning.saved.path);
948
+ } catch (error) {
949
+ firstError ?? (firstError = error);
950
+ }
707
951
  try {
708
952
  this.closeDocument?.();
709
953
  } catch (error) {
@@ -712,7 +956,10 @@ var DocumentSession = class {
712
956
  this.closeDocument = null;
713
957
  this.docPtr = null;
714
958
  this._kind = null;
959
+ this._source = null;
715
960
  this.parkedBytes = null;
961
+ this.pendingSigning = null;
962
+ this.lastCompletion = null;
716
963
  this.revisions?.clear();
717
964
  this.revisions = null;
718
965
  this.recordsByIndex.clear();
@@ -738,6 +985,17 @@ function generateSessionId() {
738
985
  }
739
986
  //#endregion
740
987
  //#region src/document-session/lifecycle/BaseDocumentRegistry.ts
988
+ const FPDF_ERR_PASSWORD$1 = 4;
989
+ /**
990
+ * A password failure is a recoverable STATE the caller can act on (prompt,
991
+ * unlock), never a generic open failure — the same distinction
992
+ * `openFatMemoryDocument` draws, so a plain-bytes open that becomes a base
993
+ * parks and unlocks exactly like it always did.
994
+ */
995
+ function baseOpenError(runtime, password) {
996
+ if (runtime.fn.FPDF_GetLastError() === FPDF_ERR_PASSWORD$1) return password ? new EngineError(EngineErrorCode.DocPasswordIncorrect, "incorrect document password") : new EngineError(EngineErrorCode.DocPasswordRequired, "document requires a password");
997
+ return new EngineError(EngineErrorCode.DocOpenFailed, "failed to open base document");
998
+ }
741
999
  var BaseDocumentRegistry = class {
742
1000
  constructor(runtime) {
743
1001
  this.runtime = runtime;
@@ -753,10 +1011,17 @@ var BaseDocumentRegistry = class {
753
1011
  try {
754
1012
  mem.writeBytes(dataPtr, opts.bytes);
755
1013
  const basePtr = fn.EPDF_LoadMemBaseDocument64(dataPtr, opts.bytes.byteLength, opts.password ?? "");
756
- if (!basePtr) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to open base document");
1014
+ if (!basePtr) throw baseOpenError(this.runtime, opts.password);
757
1015
  setRuntimeOwnerPermissionsIfEncrypted(this.runtime, basePtr);
758
1016
  stack.push(() => fn.EPDF_ReleaseBaseDocument(basePtr));
759
- return this.insert(opts.key, basePtr, () => stack.close());
1017
+ this.supplyKnownSha(basePtr, opts.knownSha256);
1018
+ return this.insert({
1019
+ key: opts.key,
1020
+ kind: "memory",
1021
+ basePtr,
1022
+ refs: 1,
1023
+ close: () => stack.close()
1024
+ });
760
1025
  } catch (error) {
761
1026
  stack.close();
762
1027
  throw error;
@@ -772,15 +1037,50 @@ var BaseDocumentRegistry = class {
772
1037
  access = this.runtime.fileAccess.fromNodeFile(opts.path);
773
1038
  stack.push(() => access?.close());
774
1039
  const basePtr = fn.EPDF_LoadBaseDocument(access.ptr, opts.password ?? "");
775
- if (!basePtr) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to open base document");
1040
+ if (!basePtr) throw baseOpenError(this.runtime, opts.password);
776
1041
  setRuntimeOwnerPermissionsIfEncrypted(this.runtime, basePtr);
777
1042
  stack.push(() => fn.EPDF_ReleaseBaseDocument(basePtr));
778
- return this.insert(opts.key, basePtr, () => stack.close());
1043
+ this.supplyKnownSha(basePtr, opts.knownSha256);
1044
+ return this.insert({
1045
+ key: opts.key,
1046
+ kind: "file",
1047
+ path: opts.path,
1048
+ basePtr,
1049
+ refs: 1,
1050
+ close: () => stack.close()
1051
+ });
779
1052
  } catch (error) {
780
1053
  stack.close();
781
1054
  throw error;
782
1055
  }
783
1056
  }
1057
+ /**
1058
+ * One more retain on a base already in the registry (a session holds one
1059
+ * for its lifetime, so its key is live while it is open), or null. The
1060
+ * way a signing candidate reopens over its session's own base without
1061
+ * copying a byte.
1062
+ */
1063
+ retainByKey(key) {
1064
+ return this.retain(key);
1065
+ }
1066
+ /**
1067
+ * Hand the runtime a hash the host already verified for these exact
1068
+ * bytes, so it never hashes them itself. Malformed values are ignored:
1069
+ * the runtime then hashes lazily on first use, which is always correct.
1070
+ */
1071
+ supplyKnownSha(basePtr, sha256Hex) {
1072
+ if (!sha256Hex || !/^[0-9a-fA-F]{64}$/.test(sha256Hex)) return;
1073
+ const { mem, fn } = this.runtime;
1074
+ const bytes = /* @__PURE__ */ new Uint8Array(32);
1075
+ for (let i = 0; i < 32; i++) bytes[i] = parseInt(sha256Hex.slice(i * 2, i * 2 + 2), 16);
1076
+ const ptr = mem.alloc(32);
1077
+ try {
1078
+ mem.writeBytes(ptr, bytes);
1079
+ fn.EPDF_SetBaseDocumentSha256(basePtr, ptr);
1080
+ } finally {
1081
+ mem.free(ptr);
1082
+ }
1083
+ }
784
1084
  releaseAll() {
785
1085
  const entries = Array.from(this.entries.values());
786
1086
  this.entries.clear();
@@ -802,20 +1102,16 @@ var BaseDocumentRegistry = class {
802
1102
  entry.refs++;
803
1103
  return this.handleFor(entry);
804
1104
  }
805
- insert(key, basePtr, close) {
806
- const entry = {
807
- key,
808
- basePtr,
809
- refs: 1,
810
- close
811
- };
812
- this.entries.set(key, entry);
1105
+ insert(entry) {
1106
+ this.entries.set(entry.key, entry);
813
1107
  return this.handleFor(entry);
814
1108
  }
815
1109
  handleFor(entry) {
816
1110
  let released = false;
817
1111
  return {
818
1112
  key: entry.key,
1113
+ kind: entry.kind,
1114
+ ...entry.path !== void 0 ? { path: entry.path } : {},
819
1115
  basePtr: entry.basePtr,
820
1116
  release: () => {
821
1117
  if (released) return;
@@ -831,90 +1127,6 @@ var BaseDocumentRegistry = class {
831
1127
  }
832
1128
  };
833
1129
  //#endregion
834
- //#region src/runtime/memory/scratch.ts
835
- /**
836
- * Allocate a scratch buffer, run `body` with it, and always free it.
837
- *
838
- * Every PDFium out-parameter call follows the same shape: malloc a
839
- * buffer, hand its pointer to the native function, read the result back,
840
- * free the buffer. Hand-rolling that `try/finally` at each call site is
841
- * how leaks and copy-paste bugs creep in, so it lives here once.
842
- */
843
- function withScratch(mem, bytes, body) {
844
- const ptr = mem.alloc(bytes);
845
- try {
846
- return body(ptr);
847
- } finally {
848
- mem.free(ptr);
849
- }
850
- }
851
- /**
852
- * Like {@link withScratch} but for native calls that take several
853
- * out-parameters at once (e.g. `FPDFAnnot_GetColor` with one pointer per
854
- * channel). Buffers are freed in reverse allocation order.
855
- */
856
- function withScratchN(mem, sizes, body) {
857
- const ptrs = sizes.map((size) => mem.alloc(size));
858
- try {
859
- return body(ptrs);
860
- } finally {
861
- for (let i = ptrs.length - 1; i >= 0; i--) mem.free(ptrs[i]);
862
- }
863
- }
864
- //#endregion
865
- //#region src/runtime/memory/strings.ts
866
- /**
867
- * Read a PDFium UTF-16LE string getter that uses the standard
868
- * (probe length, allocate, read) ABI. `call(buf, capacity)` must invoke
869
- * the native function and return the length in BYTES including the
870
- * trailing NUL (the value PDFium reports when `capacity` is 0).
871
- *
872
- * `emptyAs` decides what a present-but-empty value (`len === 2`, a lone
873
- * UTF-16 NUL) decodes to. This is the one knob different callers actually
874
- * disagree on: document Info strings treat `''` as a real value, whereas
875
- * a page label of `''` is indistinguishable from "absent" and reads as
876
- * `null`. Making it a parameter keeps that decision explicit instead of
877
- * forking the buffer dance.
878
- *
879
- * Returns `null` when the value is absent (`len <= 0`).
880
- */
881
- function readUtf16String(mem, call, emptyAs = "") {
882
- const len = call(NULL_PTR, 0);
883
- if (len <= 0) return null;
884
- if (len === 2) return emptyAs;
885
- return withScratch(mem, len, (buf) => {
886
- if (call(buf, len) <= 0) return null;
887
- return mem.readU16String(buf);
888
- });
889
- }
890
- /**
891
- * UTF-8 sibling of {@link readUtf16String} for the handful of native
892
- * getters that return UTF-8 (e.g. `EPDF_GetMetaKeyName`). UTF-8 has no
893
- * fixed empty-NUL width to special-case, so an empty value decodes to
894
- * `''`; absent (`len <= 0`) is `null`.
895
- */
896
- function readUtf8String(mem, call) {
897
- const len = call(NULL_PTR, 0);
898
- if (len <= 0) return null;
899
- return withScratch(mem, len, (buf) => {
900
- if (call(buf, len) <= 0) return null;
901
- return mem.readU8String(buf);
902
- });
903
- }
904
- /**
905
- * Write side of the UTF-16 convention: encode `value` into a scratch
906
- * buffer, hand the pointer to `set`, and always free it. `set` returns
907
- * the native call's success flag, which is propagated.
908
- */
909
- function writeUtf16String(mem, value, set) {
910
- const ptr = mem.writeU16String(value);
911
- try {
912
- return set(ptr);
913
- } finally {
914
- mem.free(ptr);
915
- }
916
- }
917
- //#endregion
918
1130
  //#region src/features/metadata/internal/readMetadataText.ts
919
1131
  /**
920
1132
  * Read a key from the document's Info dictionary. Returns null if the key
@@ -2171,8 +2383,11 @@ var PagesInserter = class {
2171
2383
  //#endregion
2172
2384
  //#region src/features/annotations/internal/read/annotationReadContext.ts
2173
2385
  /** Build the read context both read paths (list loop, mutator read-back) use. */
2174
- function readContextFor(session) {
2175
- return { docPtr: session.requireDocPtr() };
2386
+ function readContextFor(session, fonts) {
2387
+ return {
2388
+ docPtr: session.requireDocPtr(),
2389
+ ...fonts ? { fonts } : {}
2390
+ };
2176
2391
  }
2177
2392
  //#endregion
2178
2393
  //#region src/features/annotations/internal/annotationFlagBits.ts
@@ -3013,18 +3228,218 @@ function standardFontFromCode(code) {
3013
3228
  return CODE_TO_FONT[code] ?? "helvetica";
3014
3229
  }
3015
3230
  //#endregion
3016
- //#region src/features/annotations/internal/textAlignment.ts
3017
- /**
3018
- * `/Q` quadding codes (ISO 32000 §12.7.3.3) mapped to the wire-stable
3019
- * `TextAlignment` strings. Kept in engine-services so engine-core stays
3020
- * PDFium-free (mirrors `lineEnding.ts`).
3021
- *
3022
- * Left=0, Center=1, Right=2
3023
- */
3024
- const Q_LEFT = 0;
3025
- const Q_CENTER = 1;
3026
- const Q_RIGHT = 2;
3027
- function textAlignmentToCode(align) {
3231
+ //#region src/features/annotations/internal/richTextWire.ts
3232
+ const STANDARD_FACES = {
3233
+ courier: {
3234
+ family: "Courier",
3235
+ weight: 400,
3236
+ italic: false
3237
+ },
3238
+ "courier-bold": {
3239
+ family: "Courier",
3240
+ weight: 700,
3241
+ italic: false
3242
+ },
3243
+ "courier-bold-oblique": {
3244
+ family: "Courier",
3245
+ weight: 700,
3246
+ italic: true
3247
+ },
3248
+ "courier-oblique": {
3249
+ family: "Courier",
3250
+ weight: 400,
3251
+ italic: true
3252
+ },
3253
+ helvetica: {
3254
+ family: "Helvetica",
3255
+ weight: 400,
3256
+ italic: false
3257
+ },
3258
+ "helvetica-bold": {
3259
+ family: "Helvetica",
3260
+ weight: 700,
3261
+ italic: false
3262
+ },
3263
+ "helvetica-bold-oblique": {
3264
+ family: "Helvetica",
3265
+ weight: 700,
3266
+ italic: true
3267
+ },
3268
+ "helvetica-oblique": {
3269
+ family: "Helvetica",
3270
+ weight: 400,
3271
+ italic: true
3272
+ },
3273
+ "times-roman": {
3274
+ family: "Times",
3275
+ weight: 400,
3276
+ italic: false
3277
+ },
3278
+ "times-bold": {
3279
+ family: "Times",
3280
+ weight: 700,
3281
+ italic: false
3282
+ },
3283
+ "times-bold-italic": {
3284
+ family: "Times",
3285
+ weight: 700,
3286
+ italic: true
3287
+ },
3288
+ "times-italic": {
3289
+ family: "Times",
3290
+ weight: 400,
3291
+ italic: true
3292
+ },
3293
+ symbol: {
3294
+ family: "Symbol",
3295
+ weight: 400,
3296
+ italic: false
3297
+ },
3298
+ "zapf-dingbats": {
3299
+ family: "ZapfDingbats",
3300
+ weight: 400,
3301
+ italic: false
3302
+ }
3303
+ };
3304
+ /** Family names compare without case, spaces, hyphens and underscores
3305
+ * (the engine's own rule): "Noto Sans" == "NotoSans" == "noto-sans". */
3306
+ function familyKey$1(family) {
3307
+ return family.toLowerCase().replace(/[\s\-_'"]/g, "");
3308
+ }
3309
+ const STANDARD_FAMILY_KEYS = {
3310
+ helvetica: "Helvetica",
3311
+ arial: "Helvetica",
3312
+ arialmt: "Helvetica",
3313
+ sansserif: "Helvetica",
3314
+ times: "Times",
3315
+ timesroman: "Times",
3316
+ timesnewroman: "Times",
3317
+ timesnewromanpsmt: "Times",
3318
+ serif: "Times",
3319
+ courier: "Courier",
3320
+ couriernew: "Courier",
3321
+ couriernewpsmt: "Courier",
3322
+ monospace: "Courier",
3323
+ symbol: "Symbol",
3324
+ zapfdingbats: "ZapfDingbats",
3325
+ dingbats: "ZapfDingbats"
3326
+ };
3327
+ /**
3328
+ * The face a DTO `fontFamily` (or a rich run's `family`) asks for: a
3329
+ * standard font's family/weight/italic, a registered key's resolved
3330
+ * identity, or — for anything else — the string itself as a family name
3331
+ * (a face the document embeds, or one the engine will substitute).
3332
+ */
3333
+ function faceForFreeTextFont(font, describe) {
3334
+ if (isStandardFont(font)) {
3335
+ const face = STANDARD_FACES[font];
3336
+ return {
3337
+ family: face.family,
3338
+ weight: face.weight,
3339
+ italic: face.italic
3340
+ };
3341
+ }
3342
+ const registered = describe?.(font);
3343
+ if (registered) return {
3344
+ family: registered.familyName,
3345
+ weight: registered.weight,
3346
+ italic: registered.italic
3347
+ };
3348
+ return { family: font };
3349
+ }
3350
+ /**
3351
+ * The DTO `fontFamily` for a body face: the registered `key` whose identity
3352
+ * matches, else the standard font's kebab name, else the family itself.
3353
+ */
3354
+ function freeTextFontForFace(face, keyForFace) {
3355
+ const key = keyForFace?.(face.family, face.weight, face.italic);
3356
+ if (key !== void 0) return key;
3357
+ const standard = STANDARD_FAMILY_KEYS[familyKey$1(face.family)];
3358
+ if (standard) {
3359
+ const bold = face.weight >= 600;
3360
+ for (const [name, spec] of Object.entries(STANDARD_FACES)) if (spec.family === standard && spec.weight >= 600 === bold && spec.italic === face.italic) return name;
3361
+ for (const [name, spec] of Object.entries(STANDARD_FACES)) if (spec.family === standard) return name;
3362
+ }
3363
+ return face.family;
3364
+ }
3365
+ /** A run style or body with its `family` (and the weight/italic a key or
3366
+ * standard name implies) rewritten to what the engine resolves by. */
3367
+ function withEngineFace(style, describe) {
3368
+ if (style.family === void 0) return style;
3369
+ const face = faceForFreeTextFont(style.family, describe);
3370
+ const out = {
3371
+ ...style,
3372
+ family: face.family
3373
+ };
3374
+ if (face.weight !== void 0 && style.weight === void 0) out.weight = face.weight;
3375
+ if (face.italic !== void 0 && style.italic === void 0) out.italic = face.italic;
3376
+ return out;
3377
+ }
3378
+ /** The engine's JSON for a write: the input shape with faces resolved. */
3379
+ function engineRichTextJson(input, describe) {
3380
+ const body = input.body ? withEngineFace(input.body, describe) : void 0;
3381
+ const paragraphs = input.paragraphs.map((p) => ({
3382
+ ...p,
3383
+ runs: p.runs.map((r) => r.style ? {
3384
+ ...r,
3385
+ style: withEngineFace(r.style, describe)
3386
+ } : r)
3387
+ }));
3388
+ return JSON.stringify(body ? {
3389
+ body,
3390
+ paragraphs
3391
+ } : { paragraphs });
3392
+ }
3393
+ /** The engine's JSON, parsed into the DTO shape (its `source` and
3394
+ * `diagnostics` dropped). `null` when it is not what the engine writes. */
3395
+ function parseEngineRichText(json) {
3396
+ let raw;
3397
+ try {
3398
+ raw = JSON.parse(json);
3399
+ } catch {
3400
+ return null;
3401
+ }
3402
+ if (!raw || typeof raw !== "object" || !raw.body || !Array.isArray(raw.paragraphs)) return null;
3403
+ const { source: _source, ...bodyRest } = raw.body;
3404
+ return {
3405
+ body: bodyRest,
3406
+ paragraphs: raw.paragraphs.map((p) => {
3407
+ const { runs, ...props } = p;
3408
+ return {
3409
+ ...props,
3410
+ runs: (runs ?? []).map((r) => r)
3411
+ };
3412
+ })
3413
+ };
3414
+ }
3415
+ /** Read an annotation's rich text through `EPDFAnnot_GetRichTextJSON`. */
3416
+ function readEngineRichText(fn, mem, annotPtr) {
3417
+ const json = readUtf8String(mem, (buf, cap) => fn.EPDFAnnot_GetRichTextJSON(annotPtr, buf, cap));
3418
+ return json ? parseEngineRichText(json) : null;
3419
+ }
3420
+ /**
3421
+ * A draft or patch that carries both `contents` and `richText` must agree:
3422
+ * `contents` is the plain projection of the rich text (plan §4.7). A stale
3423
+ * DTO echo fails loud here instead of quietly restoring old text.
3424
+ */
3425
+ function assertRichTextAgreement(input) {
3426
+ if (input.subtype !== "free-text" || input.richText === void 0) return;
3427
+ if (input.contents === void 0 || input.contents === null) return;
3428
+ if (input.contents !== richTextPlainText(input.richText)) throw new EngineError(EngineErrorCode.InvalidArg, "free-text: `contents` must equal the plain projection of `richText` when both are given");
3429
+ }
3430
+ //#endregion
3431
+ //#region src/features/annotations/internal/textAlignment.ts
3432
+ /**
3433
+ * `/Q` quadding codes (ISO 32000 §12.7.3.3) mapped to the wire-stable
3434
+ * `TextAlignment` strings. Kept in engine-services so engine-core stays
3435
+ * PDFium-free (mirrors `lineEnding.ts`).
3436
+ *
3437
+ * Left=0, Center=1, Right=2
3438
+ */
3439
+ const Q_LEFT = 0;
3440
+ const Q_CENTER = 1;
3441
+ const Q_RIGHT = 2;
3442
+ function textAlignmentToCode(align) {
3028
3443
  switch (align) {
3029
3444
  case "center": return Q_CENTER;
3030
3445
  case "right": return Q_RIGHT;
@@ -3139,17 +3554,36 @@ const DEFAULT_FONT_SIZE$2 = 12;
3139
3554
  function colorsEqual(a, b) {
3140
3555
  return a.r === b.r && a.g === b.g && a.b === b.b;
3141
3556
  }
3142
- function readFreeText(fn, mem, annotPtr, base) {
3557
+ function readFreeText(fn, mem, annotPtr, base, _subtypeCode, ctx) {
3143
3558
  const da = readDefaultAppearance(fn, mem, annotPtr);
3144
3559
  const color = da?.color ?? { ...DEFAULT_FREETEXT_COLOR$1 };
3145
- const fontFamily = da ? standardFontFromCode(da.fontCode) : DEFAULT_STANDARD_FONT;
3146
- const fontSize = da && da.fontSize > 0 ? da.fontSize : DEFAULT_FONT_SIZE$2;
3560
+ const rich = readEngineRichText(fn, mem, annotPtr);
3561
+ const fonts = ctx?.fonts;
3562
+ const fontFamily = rich ? freeTextFontForFace(rich.body, fonts ? (family, weight, italic) => fonts.keyForFace(family, weight, italic) : void 0) : da ? standardFontFromCode(da.fontCode) : DEFAULT_STANDARD_FONT;
3563
+ const fontSize = rich && rich.body.size > 0 ? rich.body.size : da && da.fontSize > 0 ? da.fontSize : DEFAULT_FONT_SIZE$2;
3564
+ const richText = rich ?? {
3565
+ body: {
3566
+ family: "Helvetica",
3567
+ weight: 400,
3568
+ italic: false,
3569
+ size: fontSize,
3570
+ color: "#000000",
3571
+ decoration: [],
3572
+ script: "normal",
3573
+ letterSpacing: 0,
3574
+ horizontalScale: 1,
3575
+ align: "left",
3576
+ dir: "ltr"
3577
+ },
3578
+ paragraphs: [{ runs: [{ text: base.contents ?? "" }] }]
3579
+ };
3147
3580
  const textColor = readAnnotColor(fn, mem, annotPtr, FPDFANNOT_COLORTYPE.TextColor);
3148
3581
  const fontColor = textColor && !colorsEqual(textColor, color) ? textColor : void 0;
3149
3582
  const background = readAnnotColor(fn, mem, annotPtr, FPDFANNOT_COLORTYPE.Color);
3150
3583
  const ca = readAnnotOpacity(fn, mem, annotPtr);
3151
3584
  const opacity = ca == null ? 1 : Math.max(0, Math.min(1, ca));
3152
- const textAlign = textAlignmentFromCode(readTextAlignment(fn, annotPtr));
3585
+ const quadding = textAlignmentFromCode(readTextAlignment(fn, annotPtr));
3586
+ const textAlign = rich && rich.body.align !== "justify" ? rich.body.align : quadding;
3153
3587
  const intent = freeTextIntentFromName(readIntent(fn, mem, annotPtr));
3154
3588
  const points = readCalloutLine(fn, mem, annotPtr);
3155
3589
  const calloutLine = points.length === 2 ? [points[0], points[1]] : points.length === 3 ? [
@@ -3168,6 +3602,7 @@ function readFreeText(fn, mem, annotPtr, base) {
3168
3602
  fontFamily,
3169
3603
  fontSize,
3170
3604
  textAlign,
3605
+ richText,
3171
3606
  color,
3172
3607
  ...fontColor !== void 0 ? { fontColor } : {},
3173
3608
  interiorColor: background ?? null,
@@ -3680,18 +4115,18 @@ function pickReader(rawSubtypeCode) {
3680
4115
  * per session, closed before replacement and on session teardown via
3681
4116
  * {@link disposeFormModel}.
3682
4117
  */
3683
- const cache$1 = /* @__PURE__ */ new WeakMap();
4118
+ const cache$2 = /* @__PURE__ */ new WeakMap();
3684
4119
  function acquireFormModel(runtime, session) {
3685
4120
  const seq = session.mutationSeq();
3686
- const hit = cache$1.get(session);
4121
+ const hit = cache$2.get(session);
3687
4122
  if (hit && hit.seq === seq) return hit.ptr;
3688
4123
  if (hit) {
3689
4124
  runtime.fn.EPDFForm_CloseModel(hit.ptr);
3690
- cache$1.delete(session);
4125
+ cache$2.delete(session);
3691
4126
  }
3692
4127
  const ptr = runtime.fn.EPDFForm_LoadModel(session.requireDocPtr());
3693
4128
  if (ptr === NULL_PTR) throw new EngineError(EngineErrorCode.Unknown, "failed to build form model");
3694
- cache$1.set(session, {
4129
+ cache$2.set(session, {
3695
4130
  seq,
3696
4131
  ptr
3697
4132
  });
@@ -3699,10 +4134,10 @@ function acquireFormModel(runtime, session) {
3699
4134
  }
3700
4135
  /** Close and forget the session's cached model, if any. */
3701
4136
  function disposeFormModel(runtime, session) {
3702
- const hit = cache$1.get(session);
4137
+ const hit = cache$2.get(session);
3703
4138
  if (!hit) return;
3704
4139
  runtime.fn.EPDFForm_CloseModel(hit.ptr);
3705
- cache$1.delete(session);
4140
+ cache$2.delete(session);
3706
4141
  }
3707
4142
  //#endregion
3708
4143
  //#region src/features/forms/internal/readFormSnapshot.ts
@@ -3731,7 +4166,7 @@ const FF_EDIT = 1 << 18;
3731
4166
  const FF_MULTI_SELECT = 1 << 21;
3732
4167
  const FF_COMB = 1 << 24;
3733
4168
  const FF_RADIOS_IN_UNISON = 1 << 25;
3734
- function readWide(runtime, call) {
4169
+ function readWide$1(runtime, call) {
3735
4170
  return readUtf16String(runtime.mem, call) ?? "";
3736
4171
  }
3737
4172
  function readWideOrNull(runtime, call) {
@@ -3745,7 +4180,7 @@ function readToggleWidgets(runtime, model, fieldIndex) {
3745
4180
  annotObjectNumber: fn.EPDFForm_GetFieldWidgetObjNum(model, fieldIndex, w),
3746
4181
  pageObjectNumber: fn.EPDFForm_GetFieldWidgetPageObjNum(model, fieldIndex, w),
3747
4182
  onState: readUtf8String(runtime.mem, (buf, cap) => fn.EPDFForm_GetFieldWidgetOnState(model, fieldIndex, w, buf, cap)) ?? "",
3748
- exportValue: readWide(runtime, (buf, cap) => fn.EPDFForm_GetFieldWidgetExportValue(model, fieldIndex, w, buf, cap)),
4183
+ exportValue: readWide$1(runtime, (buf, cap) => fn.EPDFForm_GetFieldWidgetExportValue(model, fieldIndex, w, buf, cap)),
3749
4184
  checked: fn.EPDFForm_IsFieldWidgetChecked(model, fieldIndex, w)
3750
4185
  });
3751
4186
  return widgets;
@@ -3765,8 +4200,8 @@ function readOptions(runtime, model, fieldIndex) {
3765
4200
  const count = fn.EPDFForm_CountFieldOptions(model, fieldIndex);
3766
4201
  const options = [];
3767
4202
  for (let o = 0; o < count; o++) options.push({
3768
- label: readWide(runtime, (buf, cap) => fn.EPDFForm_GetFieldOptionLabel(model, fieldIndex, o, buf, cap)),
3769
- value: readWide(runtime, (buf, cap) => fn.EPDFForm_GetFieldOptionValue(model, fieldIndex, o, buf, cap)),
4203
+ label: readWide$1(runtime, (buf, cap) => fn.EPDFForm_GetFieldOptionLabel(model, fieldIndex, o, buf, cap)),
4204
+ value: readWide$1(runtime, (buf, cap) => fn.EPDFForm_GetFieldOptionValue(model, fieldIndex, o, buf, cap)),
3770
4205
  selected: fn.EPDFForm_IsFieldOptionSelected(model, fieldIndex, o)
3771
4206
  });
3772
4207
  return options;
@@ -3775,7 +4210,7 @@ function readOptions(runtime, model, fieldIndex) {
3775
4210
  function readFieldAt(runtime, model, fieldIndex, docPtr, actionBudget = new ActionReadBudgetTracker()) {
3776
4211
  const { fn } = runtime;
3777
4212
  const fieldObjectNumber = fn.EPDFForm_GetFieldObjNum(model, fieldIndex);
3778
- const name = readWide(runtime, (buf, cap) => fn.EPDFForm_GetFieldName(model, fieldIndex, buf, cap));
4213
+ const name = readWide$1(runtime, (buf, cap) => fn.EPDFForm_GetFieldName(model, fieldIndex, buf, cap));
3779
4214
  const rawFlags = fn.EPDFForm_GetFieldFlags(model, fieldIndex);
3780
4215
  const family = FAMILY_BY_CODE[fn.EPDFForm_GetFieldFamily(model, fieldIndex)] ?? "unknown";
3781
4216
  const valueEntry = readValueEntry(runtime, model, fieldIndex, false);
@@ -3906,7 +4341,7 @@ function readValueEntry(runtime, model, fieldIndex, isDefault) {
3906
4341
  if (kind !== 1 && kind !== 2) return { kind: "unsupported" };
3907
4342
  const count = isDefault ? fn.EPDFForm_CountFieldDefaultValues(model, fieldIndex) : fn.EPDFForm_CountFieldValues(model, fieldIndex);
3908
4343
  const values = [];
3909
- for (let index = 0; index < count; index++) values.push(readWide(runtime, (buf, capacity) => isDefault ? fn.EPDFForm_GetFieldDefaultValueAt(model, fieldIndex, index, buf, capacity) : fn.EPDFForm_GetFieldValueAt(model, fieldIndex, index, buf, capacity)));
4344
+ for (let index = 0; index < count; index++) values.push(readWide$1(runtime, (buf, capacity) => isDefault ? fn.EPDFForm_GetFieldDefaultValueAt(model, fieldIndex, index, buf, capacity) : fn.EPDFForm_GetFieldValueAt(model, fieldIndex, index, buf, capacity)));
3910
4345
  return kind === 1 ? {
3911
4346
  kind: "scalar",
3912
4347
  value: values[0] ?? ""
@@ -4247,13 +4682,13 @@ function readAnnotationActions(fn, mem, docPtr, annotPtr, budget) {
4247
4682
  * releasing any enclosing `pagePtr`.
4248
4683
  */
4249
4684
  function collectPageAnnotations(input) {
4250
- const { runtime, session, pageObjectNumber, count, getAnnotPtrAt, signal } = input;
4685
+ const { runtime, session, pageObjectNumber, count, getAnnotPtrAt, signal, fonts } = input;
4251
4686
  const { fn, mem } = runtime;
4252
4687
  const annotations = [];
4253
4688
  let hasWeak = false;
4254
4689
  const revision = session.pageState(pageObjectNumber).revision;
4255
4690
  const actionBudget = new ActionReadBudgetTracker();
4256
- const readCtx = readContextFor(session);
4691
+ const readCtx = readContextFor(session, fonts);
4257
4692
  for (let i = 0; i < count; i++) {
4258
4693
  throwIfAborted(signal);
4259
4694
  const annotPtr = getAnnotPtrAt(i);
@@ -4289,9 +4724,10 @@ function collectPageAnnotations(input) {
4289
4724
  * as they land - the public DTO surface stays the same.
4290
4725
  */
4291
4726
  var AnnotationReader = class {
4292
- constructor(runtime, session) {
4727
+ constructor(runtime, session, fonts) {
4293
4728
  this.runtime = runtime;
4294
4729
  this.session = session;
4730
+ this.fonts = fonts;
4295
4731
  }
4296
4732
  list(pageObjectNumber, signal) {
4297
4733
  throwIfAborted(signal);
@@ -4306,7 +4742,8 @@ var AnnotationReader = class {
4306
4742
  pageObjectNumber,
4307
4743
  count,
4308
4744
  getAnnotPtrAt: (i) => fn.FPDFPage_GetAnnot(pagePtr, i),
4309
- signal
4745
+ signal,
4746
+ ...this.fonts ? { fonts: this.fonts } : {}
4310
4747
  });
4311
4748
  } finally {
4312
4749
  pool.release(pageObjectNumber);
@@ -4635,9 +5072,10 @@ function normalizeScale(scale) {
4635
5072
  * actually need a pagePtr to materialise their fields.
4636
5073
  */
4637
5074
  var RawAnnotationReader = class {
4638
- constructor(runtime, session) {
5075
+ constructor(runtime, session, fonts) {
4639
5076
  this.runtime = runtime;
4640
5077
  this.session = session;
5078
+ this.fonts = fonts;
4641
5079
  }
4642
5080
  listAll(signal) {
4643
5081
  throwIfAborted(signal);
@@ -4663,7 +5101,8 @@ var RawAnnotationReader = class {
4663
5101
  pageObjectNumber,
4664
5102
  count,
4665
5103
  getAnnotPtrAt: (i) => fn.EPDFPage_GetAnnotRaw(docPtr, record.pageIndex, i),
4666
- signal
5104
+ signal,
5105
+ ...this.fonts ? { fonts: this.fonts } : {}
4667
5106
  });
4668
5107
  }
4669
5108
  };
@@ -5604,6 +6043,20 @@ const DEFAULT_FREETEXT_COLOR = {
5604
6043
  b: 0
5605
6044
  };
5606
6045
  /**
6046
+ * Write rich text through the engine's rich writer: `/RC`, `/DS`, `/DA`,
6047
+ * `/Contents` and the appearance, all or nothing. Faces in the input are
6048
+ * resolved from keys / standard names to the identities the engine names.
6049
+ */
6050
+ function writeRichText(fn, annotPtr, input, ctx) {
6051
+ const json = engineRichTextJson(input, ctx?.describeRegisteredFont);
6052
+ if (!fn.EPDFAnnot_SetRichTextJSON(annotPtr, json)) throw new EngineError(EngineErrorCode.InvalidArg, "rich text could not be written (empty rect, or a font resource could not be built)");
6053
+ }
6054
+ /** `#RRGGBB` for a rich body colour. */
6055
+ function hexColor(color) {
6056
+ const hex = (n) => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, "0");
6057
+ return `#${hex(color.r)}${hex(color.g)}${hex(color.b)}`.toUpperCase();
6058
+ }
6059
+ /**
5607
6060
  * Apply a free-text draft to a freshly-created annotation. Colour model:
5608
6061
  * - `color` -> `/DA` colour = border + default text colour.
5609
6062
  * - `fontColor` (optional) -> `TextColor` channel, overriding text only;
@@ -5645,6 +6098,7 @@ function applyFreeTextDraft(fn, mem, annotPtr, draft, ctx) {
5645
6098
  rotation: draft.rotation,
5646
6099
  unrotatedRect: draft.unrotatedRect
5647
6100
  });
6101
+ writeRichText(fn, annotPtr, draft.richText ?? { paragraphs: richTextParagraphsFromPlainText(draft.contents ?? "") }, ctx);
5648
6102
  }
5649
6103
  /**
5650
6104
  * Apply a free-text patch to an existing annotation. Only present fields are
@@ -5671,11 +6125,30 @@ function applyFreeTextPatch(fn, mem, annotPtr, patch, ctx) {
5671
6125
  else setAnnotColor(fn, annotPtr, patch.interiorColor, FPDFANNOT_COLORTYPE.Color);
5672
6126
  if (patch.opacity !== void 0) setAnnotOpacity(fn, annotPtr, patch.opacity);
5673
6127
  applyBorderPatch(fn, mem, annotPtr, patch);
5674
- if (patch.fontFamily !== void 0 || patch.fontSize !== void 0 || patch.color !== void 0) {
6128
+ const current = readEngineRichText(fn, mem, annotPtr);
6129
+ if (patch.color !== void 0) {
5675
6130
  const cur = readDefaultAppearance(fn, mem, annotPtr);
5676
- applyDefaultAppearance(fn, annotPtr, patch.fontFamily ?? (cur ? standardFontFromCode(cur.fontCode) : "helvetica"), patch.fontSize ?? (cur && cur.fontSize > 0 ? cur.fontSize : 12), patch.color ?? cur?.color ?? DEFAULT_FREETEXT_COLOR, ctx);
6131
+ applyDefaultAppearance(fn, annotPtr, cur ? standardFontFromCode(cur.fontCode) : DEFAULT_STANDARD_FONT, cur && cur.fontSize > 0 ? cur.fontSize : 12, patch.color, ctx);
5677
6132
  }
5678
6133
  if (patch.fontColor !== void 0) setAnnotColor(fn, annotPtr, patch.fontColor, FPDFANNOT_COLORTYPE.TextColor);
6134
+ if (patch.richText !== void 0) writeRichText(fn, annotPtr, patch.richText, ctx);
6135
+ else if (patch.contents !== void 0) writeRichText(fn, annotPtr, { paragraphs: richTextParagraphsFromPlainText(patch.contents ?? "") }, ctx);
6136
+ else if (current && (patch.fontFamily !== void 0 || patch.fontSize !== void 0 || patch.fontColor !== void 0 || patch.textAlign !== void 0)) {
6137
+ const body = { ...current.body };
6138
+ if (patch.fontFamily !== void 0) {
6139
+ const face = faceForFreeTextFont(patch.fontFamily, ctx?.describeRegisteredFont);
6140
+ body.family = face.family;
6141
+ if (face.weight !== void 0) body.weight = face.weight;
6142
+ if (face.italic !== void 0) body.italic = face.italic;
6143
+ }
6144
+ if (patch.fontSize !== void 0) body.size = patch.fontSize;
6145
+ if (patch.fontColor !== void 0) body.color = hexColor(patch.fontColor);
6146
+ if (patch.textAlign !== void 0) body.align = patch.textAlign;
6147
+ writeRichText(fn, annotPtr, {
6148
+ body,
6149
+ paragraphs: current.paragraphs
6150
+ }, ctx);
6151
+ }
5679
6152
  if (patch.textAlign !== void 0) setTextAlignment(fn, annotPtr, textAlignmentToCode(patch.textAlign));
5680
6153
  if (patch.intent !== void 0) setIntent(fn, annotPtr, freeTextIntentToName(patch.intent));
5681
6154
  if (patch.rectDifferences === null) clearRectangleDifferences(fn, annotPtr);
@@ -6725,7 +7198,10 @@ var AnnotationMutator = class {
6725
7198
  writeContext(pagePtr, resources) {
6726
7199
  const fonts = this.fonts;
6727
7200
  return {
6728
- ...fonts ? { resolveRegisteredFontId: (key) => fonts.idFor(key) } : {},
7201
+ ...fonts ? {
7202
+ resolveRegisteredFontId: (key) => fonts.idFor(key),
7203
+ describeRegisteredFont: (key) => fonts.describeOrUndefined(key)
7204
+ } : {},
6729
7205
  docPtr: this.session.requireDocPtr(),
6730
7206
  pagePtr,
6731
7207
  ...resources ? { resources } : {}
@@ -6741,6 +7217,7 @@ var AnnotationMutator = class {
6741
7217
  this.ensureKnownWeakStateFromPage(pageObjectNumber, pagePtr);
6742
7218
  const writeCtx = this.writeContext(pagePtr, resources);
6743
7219
  preflightDraft(draft, writeCtx);
7220
+ assertRichTextAgreement(draft);
6744
7221
  const pageStateBefore = this.session.pageState(pageObjectNumber);
6745
7222
  throwIfAborted(signal);
6746
7223
  const annotPtr = fn.EPDFPage_CreateAnnot(pagePtr, subtypeCode);
@@ -6763,7 +7240,7 @@ var AnnotationMutator = class {
6763
7240
  if (newObjNum <= 0) throw new EngineError(EngineErrorCode.Unknown, `EPDFPage_CreateAnnot produced a direct object (no objectNumber); fork helper invariant broken`);
6764
7241
  newIndex = fn.FPDFPage_GetAnnotIndex(pagePtr, annotPtr);
6765
7242
  if (newIndex < 0) throw new EngineError(EngineErrorCode.Unknown, `FPDFPage_GetAnnotIndex returned ${newIndex} for freshly created annotation`);
6766
- dto = readAnnotationFromPtr(fn, mem, annotPtr, pageObjectNumber, newIndex, pageStateBefore.revision, readContextFor(this.session));
7243
+ dto = readAnnotationFromPtr(fn, mem, annotPtr, pageObjectNumber, newIndex, pageStateBefore.revision, readContextFor(this.session, this.fonts));
6767
7244
  joinWidgetFieldNumbers(this.runtime, this.session, [dto]);
6768
7245
  } finally {
6769
7246
  fn.FPDFPage_CloseAnnot(annotPtr);
@@ -6798,6 +7275,7 @@ var AnnotationMutator = class {
6798
7275
  throwIfAborted(signal);
6799
7276
  const writeCtx = this.writeContext(pagePtr, resources);
6800
7277
  preflightPatch(patch, writeCtx);
7278
+ assertRichTextAgreement(patch);
6801
7279
  this.ensureKnownWeakStateFromPage(ref.pageObjectNumber, pagePtr);
6802
7280
  const pageStateBefore = this.session.pageState(ref.pageObjectNumber);
6803
7281
  throwIfAborted(signal);
@@ -6805,7 +7283,7 @@ var AnnotationMutator = class {
6805
7283
  const previousBlendMode = blendModeFromCode(fn.EPDFAnnot_GetBlendMode(annotPtr));
6806
7284
  const preIndex = fn.FPDFPage_GetAnnotIndex(pagePtr, annotPtr);
6807
7285
  if (preIndex < 0) throw new EngineError(EngineErrorCode.Unknown, `FPDFPage_GetAnnotIndex returned ${preIndex} before update`);
6808
- const currentDto = readAnnotationFromPtr(fn, mem, annotPtr, ref.pageObjectNumber, preIndex, pageStateBefore.revision, readContextFor(this.session));
7286
+ const currentDto = readAnnotationFromPtr(fn, mem, annotPtr, ref.pageObjectNumber, preIndex, pageStateBefore.revision, readContextFor(this.session, this.fonts));
6809
7287
  applyPatch(fn, mem, annotPtr, patch, writeCtx);
6810
7288
  let linkedParentId = null;
6811
7289
  if (patch.inReplyTo !== void 0 || patch.replyType !== void 0) linkedParentId = writeAnnotationRelationship(this.runtime, this.session, pagePtr, annotPtr, ref.pageObjectNumber, {
@@ -6832,7 +7310,7 @@ var AnnotationMutator = class {
6832
7310
  annotPtr = resolveAnnotPtr(this.runtime, this.session, pagePtr, ref);
6833
7311
  const newIndex = fn.FPDFPage_GetAnnotIndex(pagePtr, annotPtr);
6834
7312
  if (newIndex < 0) throw new EngineError(EngineErrorCode.Unknown, `FPDFPage_GetAnnotIndex returned ${newIndex} after update`);
6835
- const dto = readAnnotationFromPtr(fn, mem, annotPtr, ref.pageObjectNumber, newIndex, pageStateBefore.revision, readContextFor(this.session));
7313
+ const dto = readAnnotationFromPtr(fn, mem, annotPtr, ref.pageObjectNumber, newIndex, pageStateBefore.revision, readContextFor(this.session, this.fonts));
6836
7314
  joinWidgetFieldNumbers(this.runtime, this.session, [dto]);
6837
7315
  this.recordWeakStateFromPage(ref.pageObjectNumber, pagePtr);
6838
7316
  const meta = computeMutationImpact({
@@ -7045,7 +7523,7 @@ var AnnotationMutator = class {
7045
7523
  const annotPtr = fn.FPDFPage_GetAnnot(pagePtr, newIdx);
7046
7524
  if (!annotPtr) throw new EngineError(EngineErrorCode.Unknown, `failed to re-read moved annotation at index ${newIdx}`);
7047
7525
  try {
7048
- moved[i] = readAnnotationFromPtr(fn, mem, annotPtr, pageObjectNumber, newIdx, bumpedRev, readContextFor(this.session));
7526
+ moved[i] = readAnnotationFromPtr(fn, mem, annotPtr, pageObjectNumber, newIdx, bumpedRev, readContextFor(this.session, this.fonts));
7049
7527
  } finally {
7050
7528
  fn.FPDFPage_CloseAnnot(annotPtr);
7051
7529
  }
@@ -8307,7 +8785,480 @@ function layerSaveError(status) {
8307
8785
  return new EngineError(EngineErrorCode.DocOpenFailed, "failed to save layer artifact");
8308
8786
  }
8309
8787
  //#endregion
8788
+ //#region src/runtime/memory/u64.ts
8789
+ /**
8790
+ * 64-bit out-params as two little-endian 32-bit halves: the wasm memory
8791
+ * helpers have no 64-bit accessors (Emscripten's `setValue(i64)` needs
8792
+ * WASM_BIGINT), and the native runtime reads the same layout.
8793
+ */
8794
+ function pokeU64(mem, ptr, value, byteOffset = 0) {
8795
+ const big = BigInt(value);
8796
+ mem.poke(ptr, "i32", Number(big & 4294967295n) | 0, byteOffset);
8797
+ mem.poke(ptr, "i32", Number(big >> 32n & 4294967295n) | 0, byteOffset + 4);
8798
+ }
8799
+ function peekU64(mem, ptr, byteOffset = 0) {
8800
+ const lo = Number(mem.peek(ptr, "i32", byteOffset)) >>> 0;
8801
+ return (Number(mem.peek(ptr, "i32", byteOffset + 4)) >>> 0) * 4294967296 + lo;
8802
+ }
8803
+ //#endregion
8804
+ //#region src/features/signature/internal/readSignatureModel.ts
8805
+ const KIND_DOC_TIMESTAMP = 1;
8806
+ const COVERAGE_BY_CODE = {
8807
+ 0: "whole-revision",
8808
+ 1: "partial",
8809
+ 2: "malformed"
8810
+ };
8811
+ const STRING_FILTER = 0;
8812
+ const STRING_SUBFILTER = 1;
8813
+ const STRING_NAME = 2;
8814
+ const STRING_REASON = 3;
8815
+ const STRING_LOCATION = 4;
8816
+ const STRING_CONTACT_INFO = 5;
8817
+ const STRING_M = 6;
8818
+ const FIELD_ACTION_BY_CODE = {
8819
+ 1: "all",
8820
+ 2: "include",
8821
+ 3: "exclude"
8822
+ };
8823
+ const FIELDS_FIELDMDP = 0;
8824
+ const FIELDS_LOCK = 1;
8825
+ const SV_V = 4;
8826
+ const SV_MDP = 1 << 17;
8827
+ const SV_LIST_SUBFILTER = 0;
8828
+ const SV_LIST_DIGEST_METHOD = 1;
8829
+ const SV_LIST_REASONS = 2;
8830
+ /** The PDF 1.7 seed-value entry set this engine implements. */
8831
+ const SUPPORTED_SEED_VALUE_VERSION = 2;
8832
+ const SV_UNSUPPORTED_REQUIRED = 432;
8833
+ function readWide(runtime, call) {
8834
+ return readUtf16String(runtime.mem, call, "");
8835
+ }
8836
+ function isPermission(value) {
8837
+ return value === 1 || value === 2 || value === 3;
8838
+ }
8839
+ function readByteRange(runtime, model, index) {
8840
+ const { fn, mem } = runtime;
8841
+ return withScratch(mem, 32, (ptr) => {
8842
+ for (let k = 0; k < 4; k++) pokeU64(mem, ptr, 0, k * 8);
8843
+ if (!fn.EPDFSig_GetByteRange(model, index, ptr)) return null;
8844
+ return [
8845
+ peekU64(mem, ptr, 0),
8846
+ peekU64(mem, ptr, 8),
8847
+ peekU64(mem, ptr, 16),
8848
+ peekU64(mem, ptr, 24)
8849
+ ];
8850
+ });
8851
+ }
8852
+ function readLock(runtime, model, index, which) {
8853
+ const { fn } = runtime;
8854
+ const code = which === FIELDS_LOCK ? fn.EPDFSig_GetLockAction(model, index) : fn.EPDFSig_GetFieldMDPAction(model, index);
8855
+ const action = FIELD_ACTION_BY_CODE[code];
8856
+ if (!action) return null;
8857
+ const fields = [];
8858
+ const count = fn.EPDFSig_GetFieldNameCount(model, index, which);
8859
+ for (let n = 0; n < count; n++) fields.push(readWide(runtime, (buf, cap) => fn.EPDFSig_GetFieldNameAt(model, index, which, n, buf, cap)) ?? "");
8860
+ const spec = {
8861
+ action,
8862
+ fields
8863
+ };
8864
+ if (which === FIELDS_LOCK) {
8865
+ const permission = fn.EPDFSig_GetLockPermission(model, index);
8866
+ if (isPermission(permission)) spec.permission = permission;
8867
+ }
8868
+ return spec;
8869
+ }
8870
+ function readSeedValue(runtime, model, index) {
8871
+ const { fn } = runtime;
8872
+ if (!fn.EPDFSig_HasSeedValue(model, index)) return null;
8873
+ const requiredFlags = fn.EPDFSig_GetSeedValueRequiredFlags(model, index) >>> 0;
8874
+ const presentFlags = fn.EPDFSig_GetSeedValuePresentFlags(model, index) >>> 0;
8875
+ const version = fn.EPDFSig_GetSeedValueVersion(model, index);
8876
+ const mdp = fn.EPDFSig_GetSeedValueMDP(model, index);
8877
+ const list = (which) => {
8878
+ const count = fn.EPDFSig_GetSeedValueListCount(model, index, which);
8879
+ const items = [];
8880
+ for (let n = 0; n < count; n++) items.push(readWide(runtime, (buf, cap) => fn.EPDFSig_GetSeedValueListAt(model, index, which, n, buf, cap)) ?? "");
8881
+ return items;
8882
+ };
8883
+ return {
8884
+ requiredFlags,
8885
+ presentFlags,
8886
+ version: version > 0 ? version : null,
8887
+ mdp: (presentFlags & SV_MDP) !== 0 && mdp >= 0 && mdp <= 3 ? mdp : null,
8888
+ filter: readWide(runtime, (buf, cap) => fn.EPDFSig_GetSeedValueFilter(model, index, buf, cap)),
8889
+ subFilters: list(SV_LIST_SUBFILTER),
8890
+ digestMethods: list(SV_LIST_DIGEST_METHOD),
8891
+ reasons: list(SV_LIST_REASONS),
8892
+ unsupportedRequired: (requiredFlags & SV_UNSUPPORTED_REQUIRED) !== 0 || (requiredFlags & SV_V) !== 0 && version > SUPPORTED_SEED_VALUE_VERSION
8893
+ };
8894
+ }
8895
+ /** Every signature field of a native signature model, in model order. */
8896
+ function readSignaturesFromModel(runtime, model) {
8897
+ const { fn } = runtime;
8898
+ const count = fn.EPDFSig_Count(model);
8899
+ const out = [];
8900
+ const str = (i, key) => readWide(runtime, (buf, cap) => fn.EPDFSig_GetString(model, i, key, buf, cap));
8901
+ for (let i = 0; i < count; i++) {
8902
+ const signed = fn.EPDFSig_IsSigned(model, i);
8903
+ const widgetObjNum = fn.EPDFSig_GetWidgetObjNum(model, i);
8904
+ const revisionIndex = fn.EPDFSig_GetRevisionIndex(model, i);
8905
+ const docMdp = fn.EPDFSig_GetDocMDPPermission(model, i);
8906
+ out.push({
8907
+ index: i,
8908
+ field: {
8909
+ kind: "objectNumber",
8910
+ fieldObjectNumber: fn.EPDFSig_GetFieldObjNum(model, i)
8911
+ },
8912
+ fieldName: readWide(runtime, (buf, cap) => fn.EPDFSig_GetFieldName(model, i, buf, cap)) ?? "",
8913
+ widget: widgetObjNum > 0 ? {
8914
+ annotObjectNumber: widgetObjNum,
8915
+ pageObjectNumber: fn.EPDFSig_GetWidgetPageObjNum(model, i)
8916
+ } : null,
8917
+ signed,
8918
+ kind: fn.EPDFSig_GetKind(model, i) === KIND_DOC_TIMESTAMP ? "timestamp" : "signature",
8919
+ filter: str(i, STRING_FILTER),
8920
+ subFilter: str(i, STRING_SUBFILTER),
8921
+ byteRange: signed ? readByteRange(runtime, model, i) : null,
8922
+ contentsSize: signed ? Math.max(0, fn.EPDFSig_GetContents(model, i, NULL_PTR, 0)) : 0,
8923
+ coverage: signed ? COVERAGE_BY_CODE[fn.EPDFSig_GetCoverage(model, i)] ?? "malformed" : null,
8924
+ revisionIndex: signed && revisionIndex >= 0 ? revisionIndex : null,
8925
+ signer: {
8926
+ name: str(i, STRING_NAME),
8927
+ reason: str(i, STRING_REASON),
8928
+ location: str(i, STRING_LOCATION),
8929
+ contactInfo: str(i, STRING_CONTACT_INFO),
8930
+ claimedTime: str(i, STRING_M)
8931
+ },
8932
+ docMdp: isPermission(docMdp) ? docMdp : null,
8933
+ catalogCertification: fn.EPDFSig_IsCatalogCertification(model, i),
8934
+ fieldMdp: readLock(runtime, model, i, FIELDS_FIELDMDP),
8935
+ lock: readLock(runtime, model, i, FIELDS_LOCK),
8936
+ seedValue: readSeedValue(runtime, model, i)
8937
+ });
8938
+ }
8939
+ return out;
8940
+ }
8941
+ /** The DER `/Contents` of signature `index`, copied out; `NotFound` when unsigned or malformed. */
8942
+ function readContentsAt(runtime, model, index) {
8943
+ const { fn, mem } = runtime;
8944
+ const length = fn.EPDFSig_GetContents(model, index, NULL_PTR, 0);
8945
+ if (length <= 0) throw new EngineError(EngineErrorCode.NotFound, "signature has no usable /Contents (malformed encoding)");
8946
+ return withScratch(mem, length, (buf) => {
8947
+ if (fn.EPDFSig_GetContents(model, index, buf, length) !== length) throw new EngineError(EngineErrorCode.Unknown, "failed to read signature contents");
8948
+ const out = new Uint8Array(length);
8949
+ out.set(mem.readBytes(buf, length));
8950
+ return out;
8951
+ });
8952
+ }
8953
+ /** The chained revisions of a document, oldest first; empty when the chain is not valid. */
8954
+ function readRevisions(runtime, docPtr, signatures) {
8955
+ const { fn, mem } = runtime;
8956
+ const count = fn.EPDFDoc_GetRevisionCount(docPtr);
8957
+ const revisions = [];
8958
+ if (count <= 0) return revisions;
8959
+ withScratchN(mem, [8, 8], ([endPtr, xrefPtr]) => {
8960
+ for (let i = 0; i < count; i++) {
8961
+ pokeU64(mem, endPtr, 0);
8962
+ pokeU64(mem, xrefPtr, 0);
8963
+ if (!fn.EPDFDoc_GetRevision(docPtr, i, endPtr, xrefPtr)) throw new EngineError(EngineErrorCode.Unknown, `failed to read revision ${i}`);
8964
+ const sealedBy = signatures.find((s) => s.revisionIndex === i);
8965
+ revisions.push({
8966
+ index: i,
8967
+ end: peekU64(mem, endPtr),
8968
+ xrefOffset: peekU64(mem, xrefPtr),
8969
+ signatureIndex: sealedBy ? sealedBy.index : null
8970
+ });
8971
+ }
8972
+ });
8973
+ return revisions;
8974
+ }
8975
+ /** Whether any signature field of the document carries a signed value (fields alone do not count). */
8976
+ function hasSignedSignature(runtime, docPtr) {
8977
+ const { fn } = runtime;
8978
+ return withSignatureModel(runtime, docPtr, (model) => {
8979
+ const count = fn.EPDFSig_Count(model);
8980
+ for (let i = 0; i < count; i++) if (fn.EPDFSig_IsSigned(model, i)) return true;
8981
+ return false;
8982
+ });
8983
+ }
8984
+ /** Run `body` with a fresh signature model of `docPtr`, closing it afterwards. */
8985
+ function withSignatureModel(runtime, docPtr, body) {
8986
+ const model = runtime.fn.EPDFSig_LoadModel(docPtr);
8987
+ if (model === NULL_PTR) throw new EngineError(EngineErrorCode.Unknown, "failed to build signature model");
8988
+ try {
8989
+ return body(model);
8990
+ } finally {
8991
+ runtime.fn.EPDFSig_CloseModel(model);
8992
+ }
8993
+ }
8994
+ /**
8995
+ * The structure of one document (a revision prefix, usually): catalog,
8996
+ * AcroForm and page-tree object numbers, the pages, the terminal fields
8997
+ * with their widgets, and the signatures. What the rule engine needs to
8998
+ * give every changed object a role.
8999
+ */
9000
+ function readStructure(runtime, docPtr) {
9001
+ const { fn, mem } = runtime;
9002
+ const [root, acroForm, pagesRoot] = withScratchN(mem, [
9003
+ 4,
9004
+ 4,
9005
+ 4
9006
+ ], ([r, a, p]) => {
9007
+ mem.poke(r, "i32", 0);
9008
+ mem.poke(a, "i32", 0);
9009
+ mem.poke(p, "i32", 0);
9010
+ fn.EPDFDoc_GetStructureObjectNumbers(docPtr, r, a, p);
9011
+ return [
9012
+ Number(mem.peek(r, "i32")) >>> 0,
9013
+ Number(mem.peek(a, "i32")) >>> 0,
9014
+ Number(mem.peek(p, "i32")) >>> 0
9015
+ ];
9016
+ });
9017
+ const pages = [];
9018
+ const pageCount = fn.FPDF_GetPageCount(docPtr);
9019
+ for (let i = 0; i < pageCount; i++) pages.push(fn.EPDFDoc_GetPageObjectNumberByIndex(docPtr, i));
9020
+ const fields = [];
9021
+ const formModel = fn.EPDFForm_LoadModel(docPtr);
9022
+ if (formModel !== NULL_PTR) try {
9023
+ const snapshot = readFormSnapshot(runtime, formModel, docPtr);
9024
+ for (const f of snapshot.fields) fields.push({
9025
+ objectNumber: f.fieldObjectNumber,
9026
+ name: f.name,
9027
+ family: f.family,
9028
+ widgets: f.widgets.map((w) => w.annotObjectNumber),
9029
+ flags: f.flags.raw
9030
+ });
9031
+ } finally {
9032
+ fn.EPDFForm_CloseModel(formModel);
9033
+ }
9034
+ return {
9035
+ root,
9036
+ acroForm,
9037
+ pagesRoot,
9038
+ pages,
9039
+ fields,
9040
+ signatures: withSignatureModel(runtime, docPtr, (model) => readSignaturesFromModel(runtime, model))
9041
+ };
9042
+ }
9043
+ //#endregion
9044
+ //#region src/features/signature/internal/signatureModelCache.ts
9045
+ /**
9046
+ * Per-session cache of the native `EPDFSig_LoadModel` snapshot, keyed on
9047
+ * the session's mutation sequence exactly like the form model: the model
9048
+ * walks the revision chain, the field tree and every widget, so it is
9049
+ * built once per document state and rebuilt after any mutation.
9050
+ */
9051
+ const cache$1 = /* @__PURE__ */ new WeakMap();
9052
+ function acquireSignatureModel(runtime, session) {
9053
+ const seq = session.mutationSeq();
9054
+ const hit = cache$1.get(session);
9055
+ if (hit && hit.seq === seq) return hit.ptr;
9056
+ if (hit) {
9057
+ runtime.fn.EPDFSig_CloseModel(hit.ptr);
9058
+ cache$1.delete(session);
9059
+ }
9060
+ const ptr = runtime.fn.EPDFSig_LoadModel(session.requireDocPtr());
9061
+ if (ptr === NULL_PTR) throw new EngineError(EngineErrorCode.Unknown, "failed to build signature model");
9062
+ cache$1.set(session, {
9063
+ seq,
9064
+ ptr
9065
+ });
9066
+ return ptr;
9067
+ }
9068
+ function disposeSignatureModel(runtime, session) {
9069
+ const hit = cache$1.get(session);
9070
+ if (!hit) return;
9071
+ runtime.fn.EPDFSig_CloseModel(hit.ptr);
9072
+ cache$1.delete(session);
9073
+ }
9074
+ //#endregion
9075
+ //#region src/features/signature/SignatureReader.ts
9076
+ const DIGEST_CODE = {
9077
+ sha1: 0,
9078
+ sha256: 1,
9079
+ sha384: 2,
9080
+ sha512: 3
9081
+ };
9082
+ const DIGEST_LENGTH = {
9083
+ sha1: 20,
9084
+ sha256: 32,
9085
+ sha384: 48,
9086
+ sha512: 64
9087
+ };
9088
+ /**
9089
+ * Reads the signature model of a session: revisions, every signature
9090
+ * field with its signed state, and the protection those signatures
9091
+ * impose. Byte facts (revisions, coverage, digests, loaded bytes) come
9092
+ * from the bytes the document was loaded from — for a layer session the
9093
+ * base plus the delta it was opened with — never from unsaved edits.
9094
+ */
9095
+ var SignatureReader = class {
9096
+ constructor(runtime, session) {
9097
+ this.runtime = runtime;
9098
+ this.session = session;
9099
+ }
9100
+ readSnapshot() {
9101
+ const model = acquireSignatureModel(this.runtime, this.session);
9102
+ const chainValid = this.runtime.fn.EPDFSig_IsRevisionChainValid(model);
9103
+ const signatures = readSignaturesFromModel(this.runtime, model);
9104
+ return {
9105
+ chainValid,
9106
+ revisions: chainValid ? readRevisions(this.runtime, this.session.requireDocPtr(), signatures) : [],
9107
+ signatures,
9108
+ protection: deriveProtection(signatures)
9109
+ };
9110
+ }
9111
+ /** The protection alone, for the open probe and the form write guard. */
9112
+ readProtection() {
9113
+ const model = acquireSignatureModel(this.runtime, this.session);
9114
+ return deriveProtection(readSignaturesFromModel(this.runtime, model));
9115
+ }
9116
+ /** The DER `/Contents` of a signed field, padding stripped. */
9117
+ readContents(ref) {
9118
+ const model = acquireSignatureModel(this.runtime, this.session);
9119
+ const index = this.requireSignedIndex(model, ref);
9120
+ return readContentsAt(this.runtime, model, index).buffer;
9121
+ }
9122
+ /** Hash a signed field's `/ByteRange` straight from the loaded bytes. */
9123
+ digest(ref, algorithm) {
9124
+ const model = acquireSignatureModel(this.runtime, this.session);
9125
+ const index = this.requireSignedIndex(model, ref);
9126
+ const range = readByteRange(this.runtime, model, index);
9127
+ if (!range) throw new EngineError(EngineErrorCode.NotFound, "signature has no usable /ByteRange");
9128
+ return this.digestRange(range, algorithm);
9129
+ }
9130
+ /** The exact bytes of one revision. */
9131
+ revisionBytes(revisionIndex) {
9132
+ const snapshot = this.readSnapshot();
9133
+ if (!snapshot.chainValid) throw new EngineError(EngineErrorCode.MalformedPdf, "revision chain is not valid");
9134
+ const revision = snapshot.revisions[revisionIndex];
9135
+ if (!revision) throw new EngineError(EngineErrorCode.NotFound, `no revision ${revisionIndex}`);
9136
+ return this.readLoadedBytes(0, revision.end);
9137
+ }
9138
+ /** Resolve a signature-field ref to its identity in the current model. */
9139
+ resolveField(ref) {
9140
+ const { fn } = this.runtime;
9141
+ const model = acquireSignatureModel(this.runtime, this.session);
9142
+ const index = this.indexOf(model, ref);
9143
+ if (index < 0) throw new EngineError(EngineErrorCode.NotFound, ref.kind === "objectNumber" ? `signature field not found: object ${ref.fieldObjectNumber}` : `signature field not found: "${ref.name}"`);
9144
+ const widgetObjNum = fn.EPDFSig_GetWidgetObjNum(model, index);
9145
+ return {
9146
+ index,
9147
+ fieldObjectNumber: fn.EPDFSig_GetFieldObjNum(model, index),
9148
+ signed: fn.EPDFSig_IsSigned(model, index),
9149
+ widget: widgetObjNum > 0 ? {
9150
+ annotObjectNumber: widgetObjNum,
9151
+ pageObjectNumber: fn.EPDFSig_GetWidgetPageObjNum(model, index)
9152
+ } : null
9153
+ };
9154
+ }
9155
+ /** One signature by field object number, from the current model. */
9156
+ readSignatureByObjectNumber(fieldObjectNumber) {
9157
+ const found = this.readSnapshot().signatures.find((s) => s.field.kind === "objectNumber" && s.field.fieldObjectNumber === fieldObjectNumber);
9158
+ if (!found) throw new EngineError(EngineErrorCode.NotFound, `signature field not found: object ${fieldObjectNumber}`);
9159
+ return found;
9160
+ }
9161
+ /** The complete loaded bytes (a plain file, or a layer's base + delta). */
9162
+ loadedBytes() {
9163
+ const size = Number(this.runtime.fn.EPDFDoc_GetLoadedBytesSize(this.session.requireDocPtr()));
9164
+ return this.readLoadedBytes(0, size);
9165
+ }
9166
+ /**
9167
+ * SHA-256 and length of the base the session is on. A layer session
9168
+ * reports its base's hash (supplied by the host or computed once by the
9169
+ * runtime); a plain session hashes its loaded bytes once per load.
9170
+ */
9171
+ version() {
9172
+ const { fn, mem } = this.runtime;
9173
+ const docPtr = this.session.requireDocPtr();
9174
+ const byteLength = Number(fn.EPDFDoc_GetBaseBytesSize(docPtr));
9175
+ if (byteLength <= 0) throw new EngineError(EngineErrorCode.DocNotOpen, "document has no loaded bytes");
9176
+ if (this.session.kind === "layer") return {
9177
+ sha256: withScratch(mem, 32, (out) => {
9178
+ if (!fn.EPDFLayer_GetBaseSha256(docPtr, out)) throw new EngineError(EngineErrorCode.Unknown, "failed to read the base hash");
9179
+ return toHex(mem.readBytes(out, 32));
9180
+ }),
9181
+ byteLength
9182
+ };
9183
+ const cached = this.session.cachedPlainSha256();
9184
+ if (cached) return {
9185
+ sha256: cached,
9186
+ byteLength
9187
+ };
9188
+ const sha256 = toHex(new Uint8Array(this.digestRange([
9189
+ 0,
9190
+ byteLength,
9191
+ byteLength,
9192
+ 0
9193
+ ], "sha256")));
9194
+ this.session.rememberPlainSha256(sha256);
9195
+ return {
9196
+ sha256,
9197
+ byteLength
9198
+ };
9199
+ }
9200
+ digestRange(range, algorithm) {
9201
+ const { fn, mem } = this.runtime;
9202
+ const docPtr = this.session.requireDocPtr();
9203
+ const outLength = DIGEST_LENGTH[algorithm];
9204
+ return withScratchN(mem, [
9205
+ 32,
9206
+ outLength,
9207
+ 8
9208
+ ], ([rangePtr, outPtr, lenPtr]) => {
9209
+ for (let k = 0; k < 4; k++) pokeU64(mem, rangePtr, range[k], k * 8);
9210
+ pokeU64(mem, lenPtr, outLength);
9211
+ if (!fn.EPDFSig_DigestByteRange(docPtr, rangePtr, DIGEST_CODE[algorithm], outPtr, lenPtr)) throw new EngineError(EngineErrorCode.InvalidArg, "byte range is not within the loaded bytes");
9212
+ const written = Number(mem.peek(lenPtr, "i32"));
9213
+ return copyOut$1(mem.readBytes(outPtr, written));
9214
+ });
9215
+ }
9216
+ /** `[offset, offset + length)` of the loaded bytes (base + loaded delta for a layer), copied out. */
9217
+ readLoadedBytes(offset, length) {
9218
+ const { fn, mem } = this.runtime;
9219
+ const docPtr = this.session.requireDocPtr();
9220
+ if (length === 0) return /* @__PURE__ */ new ArrayBuffer(0);
9221
+ return withScratch(mem, length, (ptr) => {
9222
+ if (fn.EPDFDoc_ReadLoadedBytes(docPtr, BigInt(offset), ptr, length) !== length) throw new EngineError(EngineErrorCode.Unknown, "failed to read the loaded bytes");
9223
+ return copyOut$1(mem.readBytes(ptr, length));
9224
+ });
9225
+ }
9226
+ indexOf(model, ref) {
9227
+ const { fn } = this.runtime;
9228
+ if (ref.kind === "objectNumber") return fn.EPDFSig_GetIndexByFieldObjNum(model, ref.fieldObjectNumber);
9229
+ const count = fn.EPDFSig_Count(model);
9230
+ for (let i = 0; i < count; i++) if (this.readWide((buf, cap) => fn.EPDFSig_GetFieldName(model, i, buf, cap)) === ref.name) return i;
9231
+ return -1;
9232
+ }
9233
+ requireSignedIndex(model, ref) {
9234
+ const { fn } = this.runtime;
9235
+ const index = this.indexOf(model, ref);
9236
+ if (index < 0) throw new EngineError(EngineErrorCode.NotFound, ref.kind === "objectNumber" ? `signature field not found: object ${ref.fieldObjectNumber}` : `signature field not found: "${ref.name}"`);
9237
+ if (!fn.EPDFSig_IsSigned(model, index)) throw new EngineError(EngineErrorCode.NotFound, "signature field is not signed");
9238
+ return index;
9239
+ }
9240
+ readString(model, index, key) {
9241
+ return this.readWide((buf, cap) => this.runtime.fn.EPDFSig_GetString(model, index, key, buf, cap));
9242
+ }
9243
+ readWide(call) {
9244
+ return readUtf16String(this.runtime.mem, call, "");
9245
+ }
9246
+ };
9247
+ function copyOut$1(view) {
9248
+ const buffer = new ArrayBuffer(view.byteLength);
9249
+ new Uint8Array(buffer).set(view);
9250
+ return buffer;
9251
+ }
9252
+ function toHex(bytes) {
9253
+ let out = "";
9254
+ for (const b of bytes) out += b.toString(16).padStart(2, "0");
9255
+ return out;
9256
+ }
9257
+ //#endregion
8310
9258
  //#region src/features/save/DocumentSaver.ts
9259
+ /** The fork's `EPDFSaveStatus`. */
9260
+ const EPDF_SAVE_WRITTEN = 1;
9261
+ const EPDF_SAVE_UNCHANGED_SINCE_LOAD = 2;
8311
9262
  /**
8312
9263
  * Owns every "serialize the open document" path for a `DocumentSession`.
8313
9264
  * Two distinct outputs:
@@ -8327,84 +9278,1681 @@ var DocumentSaver = class {
8327
9278
  this.runtime = runtime;
8328
9279
  this.session = session;
8329
9280
  }
8330
- saveLayerArtifact() {
8331
- this.requireLayer();
8332
- const { mem, fn } = this.runtime;
8333
- const sizePtr = mem.alloc(4);
8334
- const statusPtr = mem.alloc(4);
8335
- let artifactPtr = null;
8336
- try {
8337
- mem.poke(sizePtr, "i32", 0);
8338
- mem.poke(statusPtr, "i32", -1);
8339
- artifactPtr = fn.EPDFLayer_SaveLayerArtifactToOwnedBuffer(this.session.requireDocPtr(), sizePtr, statusPtr);
8340
- const status = Number(mem.peek(statusPtr, "i32"));
8341
- const size = Number(mem.peek(sizePtr, "i32"));
8342
- if (!artifactPtr || status !== 0 || size <= 0) throw layerSaveError(status);
8343
- const bytes = mem.readBytes(artifactPtr, size);
8344
- const buffer = new ArrayBuffer(bytes.byteLength);
8345
- new Uint8Array(buffer).set(bytes);
9281
+ /**
9282
+ * The bytes a save would produce, and whether they are the loaded bytes.
9283
+ * Three answers, cheapest first: nothing was mutated since load (the
9284
+ * session's counter); something was mutated but the document is still
9285
+ * the one it was opened with — the fork's save pass found no reachable
9286
+ * object that differs from its loaded version, an annotation added and
9287
+ * removed again being the common case; or a real delta. The one pass
9288
+ * answers the second and produces the third, comparing only the objects
9289
+ * that were touched. A signed file must come back exactly as sealed, so
9290
+ * the loaded bytes are returned verbatim in the first two cases.
9291
+ */
9292
+ snapshot() {
9293
+ if (!this.session.hasUnsavedEdits()) return {
9294
+ bytes: this.loadedBytes(),
9295
+ source: "loaded"
9296
+ };
9297
+ const saved = this.saveStandaloneToBufferEx("incremental");
9298
+ if (saved.unchangedSinceLoad) return {
9299
+ bytes: this.loadedBytes(),
9300
+ source: "loaded"
9301
+ };
9302
+ return {
9303
+ bytes: saved.bytes,
9304
+ source: "working-copy"
9305
+ };
9306
+ }
9307
+ /** The complete loaded bytes: a plain file, or a layer's base plus the delta it was opened with. */
9308
+ loadedBytes() {
9309
+ return new SignatureReader(this.runtime, this.session).loadedBytes();
9310
+ }
9311
+ /**
9312
+ * The loaded bytes streamed to `path` in chunks: a plain file, or a
9313
+ * layer's base plus the delta it was opened with, never the base alone.
9314
+ * Native only. Replaces whatever `path` held.
9315
+ */
9316
+ copyLoadedBytesToFile(path) {
9317
+ const reader = new SignatureReader(this.runtime, this.session);
9318
+ const size = Number(this.runtime.fn.EPDFDoc_GetLoadedBytesSize(this.session.requireDocPtr()));
9319
+ const chunk = 4 * 1024 * 1024;
9320
+ this.runtime.fileWrite.removeFile(path);
9321
+ for (let offset = 0; offset < size; offset += chunk) {
9322
+ const length = Math.min(chunk, size - offset);
9323
+ this.runtime.fileWrite.appendBytes(path, new Uint8Array(reader.readLoadedBytes(offset, length)));
9324
+ }
9325
+ if (size === 0) this.runtime.fileWrite.appendBytes(path, /* @__PURE__ */ new Uint8Array(0));
9326
+ }
9327
+ /**
9328
+ * `snapshot()` onto a file: `path` ends up holding the document a save
9329
+ * would write — the loaded bytes when nothing changed, else the standalone
9330
+ * save — without the document ever being an in-memory buffer. Native only.
9331
+ */
9332
+ snapshotToFile(path) {
9333
+ if (!this.session.hasUnsavedEdits()) {
9334
+ this.copyLoadedBytesToFile(path);
8346
9335
  return {
8347
- bytes: buffer,
8348
- size
9336
+ path,
9337
+ source: "loaded"
9338
+ };
9339
+ }
9340
+ if (this.saveStandaloneToFileEx(path, "incremental").unchangedSinceLoad) {
9341
+ this.copyLoadedBytesToFile(path);
9342
+ return {
9343
+ path,
9344
+ source: "loaded"
8349
9345
  };
8350
- } finally {
8351
- if (artifactPtr) fn.EPDF_FreeBuffer(artifactPtr);
8352
- mem.free(statusPtr);
8353
- mem.free(sizePtr);
9346
+ }
9347
+ return {
9348
+ path,
9349
+ source: "working-copy"
9350
+ };
9351
+ }
9352
+ /** Whether this runtime and session can put scratch output beside the base file. */
9353
+ canWriteScratchFiles() {
9354
+ return this.runtime.kind === "native" && this.session.source.base?.kind === "file";
9355
+ }
9356
+ saveLayerArtifact() {
9357
+ const saved = this.saveLayerArtifactImpl(false);
9358
+ if (saved.size <= 0) throw layerSaveError(-1);
9359
+ return {
9360
+ bytes: saved.bytes,
9361
+ size: saved.size
9362
+ };
9363
+ }
9364
+ /**
9365
+ * `saveLayerArtifact` that says whether anything reachable changed since
9366
+ * the layer was opened. When nothing did, `bytes` is empty and the
9367
+ * artifact the session was opened with stands. The artifact written
9368
+ * otherwise is cumulative against the base (empty delta when the layer
9369
+ * came to equal its base).
9370
+ */
9371
+ saveLayerArtifactEx() {
9372
+ return this.saveLayerArtifactImpl(true);
9373
+ }
9374
+ /**
9375
+ * `saveLayerArtifactEx` onto a file: nothing reaches `path` when nothing
9376
+ * changed since load. The delta inside is streamed, never held whole.
9377
+ * Native only.
9378
+ */
9379
+ saveLayerArtifactToFileEx(path) {
9380
+ this.requireLayer();
9381
+ return this.saveLayerToFileImpl(path, "artifact");
9382
+ }
9383
+ /** `saveLayerDeltaEx` onto a file: the cumulative delta, or nothing when nothing changed since load. Native only. */
9384
+ saveLayerDeltaToFileEx(path) {
9385
+ this.requireLayer();
9386
+ return this.saveLayerToFileImpl(path, "delta");
9387
+ }
9388
+ saveLayerToFileImpl(path, what) {
9389
+ const { mem, fn } = this.runtime;
9390
+ const statusPtr = mem.alloc(4);
9391
+ const changedPtr = mem.alloc(4);
9392
+ this.runtime.fileWrite.removeFile(path);
9393
+ const writer = this.runtime.fileWrite.toNodeFile(path);
9394
+ try {
9395
+ mem.poke(statusPtr, "i32", -1);
9396
+ mem.poke(changedPtr, "i32", 1);
9397
+ const ok = what === "artifact" ? fn.EPDFLayer_SaveLayerArtifactEx(this.session.requireDocPtr(), writer.ptr, statusPtr, changedPtr) : fn.EPDFLayer_SaveDeltaEx(this.session.requireDocPtr(), writer.ptr, statusPtr, changedPtr);
9398
+ const status = Number(mem.peek(statusPtr, "i32"));
9399
+ if (!ok || status !== 0) throw layerSaveError(status);
9400
+ return {
9401
+ path,
9402
+ changedSinceLoad: Number(mem.peek(changedPtr, "i32")) !== 0
9403
+ };
9404
+ } finally {
9405
+ writer.close();
9406
+ mem.free(changedPtr);
9407
+ mem.free(statusPtr);
9408
+ }
9409
+ }
9410
+ saveLayerArtifactImpl(reporting) {
9411
+ this.requireLayer();
9412
+ const { mem, fn } = this.runtime;
9413
+ const sizePtr = mem.alloc(4);
9414
+ const statusPtr = mem.alloc(4);
9415
+ const changedPtr = mem.alloc(4);
9416
+ let artifactPtr = null;
9417
+ try {
9418
+ mem.poke(sizePtr, "i32", 0);
9419
+ mem.poke(statusPtr, "i32", -1);
9420
+ mem.poke(changedPtr, "i32", 1);
9421
+ artifactPtr = reporting ? fn.EPDFLayer_SaveLayerArtifactToOwnedBufferEx(this.session.requireDocPtr(), sizePtr, statusPtr, changedPtr) : fn.EPDFLayer_SaveLayerArtifactToOwnedBuffer(this.session.requireDocPtr(), sizePtr, statusPtr);
9422
+ const status = Number(mem.peek(statusPtr, "i32"));
9423
+ const size = Number(mem.peek(sizePtr, "i32"));
9424
+ const changedSinceLoad = reporting ? Number(mem.peek(changedPtr, "i32")) !== 0 : true;
9425
+ if (status !== 0) throw layerSaveError(status);
9426
+ if (reporting && !changedSinceLoad) return {
9427
+ bytes: /* @__PURE__ */ new ArrayBuffer(0),
9428
+ size: 0,
9429
+ changedSinceLoad: false
9430
+ };
9431
+ if (!artifactPtr || size <= 0) throw layerSaveError(status);
9432
+ const bytes = mem.readBytes(artifactPtr, size);
9433
+ const buffer = new ArrayBuffer(bytes.byteLength);
9434
+ new Uint8Array(buffer).set(bytes);
9435
+ return {
9436
+ bytes: buffer,
9437
+ size,
9438
+ changedSinceLoad
9439
+ };
9440
+ } finally {
9441
+ if (artifactPtr) fn.EPDF_FreeBuffer(artifactPtr);
9442
+ mem.free(changedPtr);
9443
+ mem.free(statusPtr);
9444
+ mem.free(sizePtr);
9445
+ }
9446
+ }
9447
+ /**
9448
+ * The raw cumulative delta a layer save writes (every promoted object,
9449
+ * offsets notional from the base's append offset), without the artifact
9450
+ * header: what `EPDFDoc_OpenBaseOverlay` composes with the base. Empty
9451
+ * when nothing was promoted.
9452
+ */
9453
+ saveLayerDelta() {
9454
+ const saved = this.saveLayerDeltaImpl(false);
9455
+ return {
9456
+ bytes: saved.bytes,
9457
+ size: saved.size
9458
+ };
9459
+ }
9460
+ /**
9461
+ * `saveLayerDelta` that says whether anything reachable changed since the
9462
+ * layer was opened; empty bytes when nothing did (keep what you have), or
9463
+ * when the layer came to equal its base (then `changedSinceLoad` is true
9464
+ * and the document IS the base).
9465
+ */
9466
+ saveLayerDeltaEx() {
9467
+ return this.saveLayerDeltaImpl(true);
9468
+ }
9469
+ saveLayerDeltaImpl(reporting) {
9470
+ this.requireLayer();
9471
+ const { mem, fn } = this.runtime;
9472
+ const sizePtr = mem.alloc(4);
9473
+ const statusPtr = mem.alloc(4);
9474
+ const changedPtr = mem.alloc(4);
9475
+ let deltaPtr = null;
9476
+ try {
9477
+ mem.poke(sizePtr, "i32", 0);
9478
+ mem.poke(statusPtr, "i32", -1);
9479
+ mem.poke(changedPtr, "i32", 1);
9480
+ deltaPtr = reporting ? fn.EPDFLayer_SaveDeltaToOwnedBufferEx(this.session.requireDocPtr(), sizePtr, statusPtr, changedPtr) : fn.EPDFLayer_SaveDeltaToOwnedBuffer(this.session.requireDocPtr(), sizePtr, statusPtr);
9481
+ const status = Number(mem.peek(statusPtr, "i32"));
9482
+ const size = Number(mem.peek(sizePtr, "i32"));
9483
+ const changedSinceLoad = reporting ? Number(mem.peek(changedPtr, "i32")) !== 0 : true;
9484
+ if (status !== 0) throw layerSaveError(status);
9485
+ if (!deltaPtr || size <= 0) return {
9486
+ bytes: /* @__PURE__ */ new ArrayBuffer(0),
9487
+ size: 0,
9488
+ changedSinceLoad
9489
+ };
9490
+ const bytes = mem.readBytes(deltaPtr, size);
9491
+ const buffer = new ArrayBuffer(bytes.byteLength);
9492
+ new Uint8Array(buffer).set(bytes);
9493
+ return {
9494
+ bytes: buffer,
9495
+ size,
9496
+ changedSinceLoad
9497
+ };
9498
+ } finally {
9499
+ if (deltaPtr) fn.EPDF_FreeBuffer(deltaPtr);
9500
+ mem.free(changedPtr);
9501
+ mem.free(statusPtr);
9502
+ mem.free(sizePtr);
8354
9503
  }
8355
9504
  }
8356
9505
  saveLayerArtifactToFile(path) {
8357
9506
  this.requireLayer();
8358
9507
  const { mem, fn } = this.runtime;
8359
9508
  const statusPtr = mem.alloc(4);
8360
- const writer = this.runtime.fileWrite.toNodeFile(path);
9509
+ const writer = this.runtime.fileWrite.toNodeFile(path);
9510
+ try {
9511
+ mem.poke(statusPtr, "i32", -1);
9512
+ const ok = fn.EPDFLayer_SaveLayerArtifact(this.session.requireDocPtr(), writer.ptr, statusPtr);
9513
+ const status = Number(mem.peek(statusPtr, "i32"));
9514
+ if (!ok || status !== 0) throw layerSaveError(status);
9515
+ return { path };
9516
+ } finally {
9517
+ writer.close();
9518
+ mem.free(statusPtr);
9519
+ }
9520
+ }
9521
+ saveStandaloneToBuffer(mode) {
9522
+ const { mem, fn } = this.runtime;
9523
+ const sizePtr = mem.alloc(4);
9524
+ let pdfPtr = null;
9525
+ try {
9526
+ mem.poke(sizePtr, "i32", 0);
9527
+ pdfPtr = fn.EPDF_SaveDocumentToOwnedBuffer(this.session.requireDocPtr(), pdfSaveModeFlags(mode), sizePtr);
9528
+ const size = Number(mem.peek(sizePtr, "i32"));
9529
+ if (!pdfPtr || size <= 0) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to save document");
9530
+ const bytes = mem.readBytes(pdfPtr, size);
9531
+ const buffer = new ArrayBuffer(bytes.byteLength);
9532
+ new Uint8Array(buffer).set(bytes);
9533
+ return {
9534
+ bytes: buffer,
9535
+ size
9536
+ };
9537
+ } finally {
9538
+ if (pdfPtr) fn.EPDF_FreeBuffer(pdfPtr);
9539
+ mem.free(sizePtr);
9540
+ }
9541
+ }
9542
+ /**
9543
+ * `saveStandaloneToBuffer` that says whether the save wrote anything. An
9544
+ * incremental save of a layer never rewrites an object equal to its base
9545
+ * twin, and when no reachable object differs from the document the layer
9546
+ * was opened with it writes nothing at all: `unchangedSinceLoad`, empty
9547
+ * bytes — the loaded bytes are the document.
9548
+ */
9549
+ saveStandaloneToBufferEx(mode) {
9550
+ const { mem, fn } = this.runtime;
9551
+ const sizePtr = mem.alloc(4);
9552
+ const statusPtr = mem.alloc(4);
9553
+ let pdfPtr = null;
9554
+ try {
9555
+ mem.poke(sizePtr, "i32", 0);
9556
+ mem.poke(statusPtr, "i32", 0);
9557
+ pdfPtr = fn.EPDF_SaveDocumentToOwnedBufferEx(this.session.requireDocPtr(), pdfSaveModeFlags(mode), 0, sizePtr, statusPtr);
9558
+ const status = Number(mem.peek(statusPtr, "i32"));
9559
+ if (status === EPDF_SAVE_UNCHANGED_SINCE_LOAD) return {
9560
+ bytes: /* @__PURE__ */ new ArrayBuffer(0),
9561
+ size: 0,
9562
+ unchangedSinceLoad: true
9563
+ };
9564
+ const size = Number(mem.peek(sizePtr, "i32"));
9565
+ if (!pdfPtr || size <= 0 || status !== EPDF_SAVE_WRITTEN) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to save document");
9566
+ const bytes = mem.readBytes(pdfPtr, size);
9567
+ const buffer = new ArrayBuffer(bytes.byteLength);
9568
+ new Uint8Array(buffer).set(bytes);
9569
+ return {
9570
+ bytes: buffer,
9571
+ size,
9572
+ unchangedSinceLoad: false
9573
+ };
9574
+ } finally {
9575
+ if (pdfPtr) fn.EPDF_FreeBuffer(pdfPtr);
9576
+ mem.free(statusPtr);
9577
+ mem.free(sizePtr);
9578
+ }
9579
+ }
9580
+ saveStandaloneToFile(path, mode) {
9581
+ const writer = this.runtime.fileWrite.toNodeFile(path);
9582
+ try {
9583
+ if (!this.runtime.fn.FPDF_SaveAsCopy(this.session.requireDocPtr(), writer.ptr, pdfSaveModeFlags(mode))) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to save document");
9584
+ return { path };
9585
+ } finally {
9586
+ writer.close();
9587
+ }
9588
+ }
9589
+ /**
9590
+ * `saveStandaloneToFile` with the report of `saveStandaloneToBufferEx`.
9591
+ * On `unchangedSinceLoad` no byte reached the file: the caller streams
9592
+ * the loaded bytes instead.
9593
+ */
9594
+ saveStandaloneToFileEx(path, mode) {
9595
+ const { mem, fn } = this.runtime;
9596
+ const writer = this.runtime.fileWrite.toNodeFile(path);
9597
+ const statusPtr = mem.alloc(4);
9598
+ try {
9599
+ mem.poke(statusPtr, "i32", 0);
9600
+ const ok = fn.EPDF_SaveAsCopyEx(this.session.requireDocPtr(), writer.ptr, pdfSaveModeFlags(mode), statusPtr);
9601
+ const status = Number(mem.peek(statusPtr, "i32"));
9602
+ if (!ok || status === 0) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to save document");
9603
+ return {
9604
+ path,
9605
+ unchangedSinceLoad: status === EPDF_SAVE_UNCHANGED_SINCE_LOAD
9606
+ };
9607
+ } finally {
9608
+ mem.free(statusPtr);
9609
+ writer.close();
9610
+ }
9611
+ }
9612
+ requireLayer() {
9613
+ if (this.session.kind !== "layer") throw new EngineError(EngineErrorCode.InvalidArg, "document session is not a layer");
9614
+ }
9615
+ };
9616
+ //#endregion
9617
+ //#region src/features/signature/internal/candidateStore.ts
9618
+ function copyOut(view) {
9619
+ const out = new Uint8Array(view.byteLength);
9620
+ out.set(view);
9621
+ return out;
9622
+ }
9623
+ /** Marshal `EPDFSig_SealSpan` over a span held in the runtime heap. */
9624
+ function sealSpanInHeap(runtime, spanPtr, spanLength, objectOffset, fileLength) {
9625
+ const { fn, mem } = runtime;
9626
+ return withScratchN(mem, [
9627
+ 32,
9628
+ 8,
9629
+ 8
9630
+ ], ([rangePtr, coPtr, chPtr]) => {
9631
+ for (let k = 0; k < 4; k++) pokeU64(mem, rangePtr, 0, k * 8);
9632
+ pokeU64(mem, coPtr, 0);
9633
+ pokeU64(mem, chPtr, 0);
9634
+ if (!fn.EPDFSig_SealSpan(spanPtr, BigInt(spanLength), BigInt(objectOffset), BigInt(fileLength), rangePtr, coPtr, chPtr)) throw new EngineError(EngineErrorCode.Unknown, "failed to seal the signing candidate");
9635
+ return {
9636
+ byteRange: [
9637
+ peekU64(mem, rangePtr, 0),
9638
+ peekU64(mem, rangePtr, 8),
9639
+ peekU64(mem, rangePtr, 16),
9640
+ peekU64(mem, rangePtr, 24)
9641
+ ],
9642
+ contentsOffset: peekU64(mem, coPtr),
9643
+ contentsHexLength: peekU64(mem, chPtr)
9644
+ };
9645
+ });
9646
+ }
9647
+ /** Marshal `EPDFSig_WriteContents` over a buffer in the heap whose first byte is file offset `bufferOffset`. */
9648
+ function writeContentsInHeap(runtime, bufPtr, bufferLength, bufferOffset, sealed, cms) {
9649
+ const { fn, mem } = runtime;
9650
+ const cmsPtr = mem.alloc(cms.byteLength);
9651
+ try {
9652
+ mem.writeBytes(cmsPtr, cms);
9653
+ if (!fn.EPDFSig_WriteContents(bufPtr, BigInt(bufferLength), BigInt(sealed.contentsOffset - bufferOffset), BigInt(sealed.contentsHexLength), cmsPtr, cms.byteLength)) throw new EngineError(EngineErrorCode.SignatureRefused, "the CMS is not one DER object that fits the reserved /Contents");
9654
+ } finally {
9655
+ mem.free(cmsPtr);
9656
+ }
9657
+ }
9658
+ /**
9659
+ * Hex-encode `cms` into the /Contents hole of a candidate FILE, in place.
9660
+ * Only the hole (`<`, the hex digits, `>`) is read and written back; the
9661
+ * delimiters are checked so a wrong offset patches nothing.
9662
+ */
9663
+ function writeContentsIntoFile(runtime, path, hole, cms) {
9664
+ const { mem } = runtime;
9665
+ const span = runtime.fileAccess.readRange(path, hole.holeOffset, hole.hexLength + 2);
9666
+ if (span.byteLength !== hole.hexLength + 2 || span[0] !== 60 || span[span.byteLength - 1] !== 62) throw new EngineError(EngineErrorCode.InvalidArg, "the byteRange gap is not a <…> /Contents hole of the candidate");
9667
+ const spanPtr = mem.alloc(span.byteLength);
9668
+ try {
9669
+ mem.writeBytes(spanPtr, span);
9670
+ writeContentsInHeap(runtime, spanPtr, span.byteLength, hole.holeOffset, {
9671
+ byteRange: [
9672
+ 0,
9673
+ 0,
9674
+ 0,
9675
+ 0
9676
+ ],
9677
+ contentsOffset: hole.holeOffset + 1,
9678
+ contentsHexLength: hole.hexLength,
9679
+ digest: /* @__PURE__ */ new Uint8Array()
9680
+ }, cms);
9681
+ runtime.fileWrite.writeRange(path, hole.holeOffset, mem.readBytes(spanPtr, span.byteLength));
9682
+ } finally {
9683
+ mem.free(spanPtr);
9684
+ }
9685
+ }
9686
+ var MemoryCandidateStore = class {
9687
+ constructor(runtime, baseDocuments) {
9688
+ this.runtime = runtime;
9689
+ this.baseDocuments = baseDocuments;
9690
+ }
9691
+ save(candidateDocPtr, sigObjNum) {
9692
+ const { mem, fn } = this.runtime;
9693
+ return withScratchN(mem, [
9694
+ 8,
9695
+ 8,
9696
+ 8
9697
+ ], ([sizePtr, offPtr, lenPtr]) => {
9698
+ pokeU64(mem, sizePtr, 0);
9699
+ pokeU64(mem, offPtr, 0);
9700
+ pokeU64(mem, lenPtr, 0);
9701
+ const bufPtr = fn.EPDFSig_SaveCandidateToOwnedBuffer(candidateDocPtr, sigObjNum, sizePtr, offPtr, lenPtr);
9702
+ if (!bufPtr) throw new EngineError(EngineErrorCode.Unknown, "failed to save the signing candidate");
9703
+ try {
9704
+ const size = peekU64(mem, sizePtr);
9705
+ return {
9706
+ kind: "memory",
9707
+ bytes: copyOut(mem.readBytes(bufPtr, size)),
9708
+ size,
9709
+ objectOffset: peekU64(mem, offPtr),
9710
+ objectLength: peekU64(mem, lenPtr)
9711
+ };
9712
+ } finally {
9713
+ fn.EPDF_FreeBuffer(bufPtr);
9714
+ }
9715
+ });
9716
+ }
9717
+ seal(saved, algorithm) {
9718
+ if (saved.kind !== "memory") throw new EngineError(EngineErrorCode.InvalidArg, "not a memory candidate");
9719
+ const { mem, fn } = this.runtime;
9720
+ const bufPtr = mem.alloc(saved.bytes.byteLength);
9721
+ try {
9722
+ mem.writeBytes(bufPtr, saved.bytes);
9723
+ return withScratchN(mem, [
9724
+ 32,
9725
+ 8,
9726
+ 8,
9727
+ 64,
9728
+ 8
9729
+ ], ([rangePtr, coPtr, chPtr, digestPtr, lenPtr]) => {
9730
+ for (let k = 0; k < 4; k++) pokeU64(mem, rangePtr, 0, k * 8);
9731
+ pokeU64(mem, coPtr, 0);
9732
+ pokeU64(mem, chPtr, 0);
9733
+ pokeU64(mem, lenPtr, 64);
9734
+ if (!fn.EPDFSig_Seal(bufPtr, BigInt(saved.bytes.byteLength), BigInt(saved.objectOffset), BigInt(saved.objectLength), DIGEST_CODE[algorithm], rangePtr, coPtr, chPtr, digestPtr, lenPtr)) throw new EngineError(EngineErrorCode.Unknown, "failed to seal the signing candidate");
9735
+ saved.bytes = copyOut(mem.readBytes(bufPtr, saved.bytes.byteLength));
9736
+ const digestLength = Number(mem.peek(lenPtr, "i32"));
9737
+ return {
9738
+ byteRange: [
9739
+ peekU64(mem, rangePtr, 0),
9740
+ peekU64(mem, rangePtr, 8),
9741
+ peekU64(mem, rangePtr, 16),
9742
+ peekU64(mem, rangePtr, 24)
9743
+ ],
9744
+ contentsOffset: peekU64(mem, coPtr),
9745
+ contentsHexLength: peekU64(mem, chPtr),
9746
+ digest: copyOut(mem.readBytes(digestPtr, digestLength))
9747
+ };
9748
+ });
9749
+ } finally {
9750
+ mem.free(bufPtr);
9751
+ }
9752
+ }
9753
+ writeContents(saved, sealed, cms) {
9754
+ if (saved.kind !== "memory") throw new EngineError(EngineErrorCode.InvalidArg, "not a memory candidate");
9755
+ const { mem } = this.runtime;
9756
+ const bufPtr = mem.alloc(saved.bytes.byteLength);
9757
+ try {
9758
+ mem.writeBytes(bufPtr, saved.bytes);
9759
+ writeContentsInHeap(this.runtime, bufPtr, saved.bytes.byteLength, 0, sealed, cms);
9760
+ saved.bytes = copyOut(mem.readBytes(bufPtr, saved.bytes.byteLength));
9761
+ } finally {
9762
+ mem.free(bufPtr);
9763
+ }
9764
+ }
9765
+ openSealed(saved, password) {
9766
+ if (saved.kind !== "memory") throw new EngineError(EngineErrorCode.InvalidArg, "not a memory candidate");
9767
+ const base = this.baseDocuments.acquireMemoryBase({
9768
+ key: `signed:${generateUuid()}`,
9769
+ bytes: saved.bytes,
9770
+ password
9771
+ });
9772
+ return openLayerDocument(this.runtime, base, { kind: "fresh" }, password);
9773
+ }
9774
+ discard() {}
9775
+ };
9776
+ /**
9777
+ * A candidate written next to the session's base file, through the
9778
+ * runtime's local-file surface: the base streams through the writer, the
9779
+ * seal patches the object span in place, the digest streams the file, and
9780
+ * the sealed file becomes a file base. Only the object span and the CMS
9781
+ * are ever held in memory. Node runtimes only.
9782
+ */
9783
+ var FileCandidateStore = class {
9784
+ constructor(runtime, baseDocuments, basePath, candidatePath = defaultCandidatePath) {
9785
+ this.runtime = runtime;
9786
+ this.baseDocuments = baseDocuments;
9787
+ this.basePath = basePath;
9788
+ this.candidatePath = candidatePath;
9789
+ }
9790
+ save(candidateDocPtr, sigObjNum, signingId) {
9791
+ const { mem, fn } = this.runtime;
9792
+ const path = this.candidatePath(this.basePath, signingId);
9793
+ this.runtime.fileWrite.removeFile(path);
9794
+ const writer = this.runtime.fileWrite.toNodeFile(path);
9795
+ try {
9796
+ return withScratchN(mem, [
9797
+ 8,
9798
+ 8,
9799
+ 8
9800
+ ], ([sizePtr, offPtr, lenPtr]) => {
9801
+ pokeU64(mem, sizePtr, 0);
9802
+ pokeU64(mem, offPtr, 0);
9803
+ pokeU64(mem, lenPtr, 0);
9804
+ if (!fn.EPDFSig_SaveCandidate(candidateDocPtr, sigObjNum, writer.ptr, sizePtr, offPtr, lenPtr)) throw new EngineError(EngineErrorCode.Unknown, "failed to save the signing candidate");
9805
+ return {
9806
+ kind: "file",
9807
+ path,
9808
+ size: peekU64(mem, sizePtr),
9809
+ objectOffset: peekU64(mem, offPtr),
9810
+ objectLength: peekU64(mem, lenPtr)
9811
+ };
9812
+ });
9813
+ } catch (error) {
9814
+ writer.close();
9815
+ this.runtime.fileWrite.removeFile(path);
9816
+ throw error;
9817
+ } finally {
9818
+ writer.close();
9819
+ }
9820
+ }
9821
+ seal(saved, algorithm) {
9822
+ if (saved.kind !== "file") throw new EngineError(EngineErrorCode.InvalidArg, "not a file candidate");
9823
+ const { mem, fn } = this.runtime;
9824
+ const span = this.runtime.fileAccess.readRange(saved.path, saved.objectOffset, saved.objectLength);
9825
+ if (span.byteLength !== saved.objectLength) throw new EngineError(EngineErrorCode.Unknown, "the signing candidate file is shorter than its object span");
9826
+ const spanPtr = mem.alloc(span.byteLength);
9827
+ let plan;
9828
+ try {
9829
+ mem.writeBytes(spanPtr, span);
9830
+ plan = sealSpanInHeap(this.runtime, spanPtr, span.byteLength, saved.objectOffset, saved.size);
9831
+ this.runtime.fileWrite.writeRange(saved.path, saved.objectOffset, mem.readBytes(spanPtr, span.byteLength));
9832
+ } finally {
9833
+ mem.free(spanPtr);
9834
+ }
9835
+ const access = this.runtime.fileAccess.fromNodeFile(saved.path);
9836
+ try {
9837
+ const digest = withScratchN(mem, [
9838
+ 32,
9839
+ 64,
9840
+ 8
9841
+ ], ([rangePtr, digestPtr, lenPtr]) => {
9842
+ for (let k = 0; k < 4; k++) pokeU64(mem, rangePtr, plan.byteRange[k], k * 8);
9843
+ pokeU64(mem, lenPtr, 64);
9844
+ if (!fn.EPDFSig_DigestFileRange(access.ptr, rangePtr, DIGEST_CODE[algorithm], digestPtr, lenPtr)) throw new EngineError(EngineErrorCode.Unknown, "failed to digest the signing candidate file");
9845
+ return copyOut(mem.readBytes(digestPtr, Number(mem.peek(lenPtr, "i32"))));
9846
+ });
9847
+ return {
9848
+ ...plan,
9849
+ digest
9850
+ };
9851
+ } finally {
9852
+ access.close();
9853
+ }
9854
+ }
9855
+ writeContents(saved, sealed, cms) {
9856
+ if (saved.kind !== "file") throw new EngineError(EngineErrorCode.InvalidArg, "not a file candidate");
9857
+ const { mem } = this.runtime;
9858
+ const span = this.runtime.fileAccess.readRange(saved.path, saved.objectOffset, saved.objectLength);
9859
+ const spanPtr = mem.alloc(span.byteLength);
9860
+ try {
9861
+ mem.writeBytes(spanPtr, span);
9862
+ writeContentsInHeap(this.runtime, spanPtr, span.byteLength, saved.objectOffset, sealed, cms);
9863
+ this.runtime.fileWrite.writeRange(saved.path, saved.objectOffset, mem.readBytes(spanPtr, span.byteLength));
9864
+ } finally {
9865
+ mem.free(spanPtr);
9866
+ }
9867
+ }
9868
+ openSealed(saved, password) {
9869
+ if (saved.kind !== "file") throw new EngineError(EngineErrorCode.InvalidArg, "not a file candidate");
9870
+ const base = this.baseDocuments.acquireFileBase({
9871
+ key: `signed:${saved.path}`,
9872
+ path: saved.path,
9873
+ password
9874
+ });
9875
+ return openLayerDocument(this.runtime, base, { kind: "fresh" }, password);
9876
+ }
9877
+ discard(saved) {
9878
+ if (saved.kind === "file") this.runtime.fileWrite.removeFile(saved.path);
9879
+ }
9880
+ };
9881
+ /** Beside the base file, named by the signing; the host may override this (server storage layout). */
9882
+ function defaultCandidatePath(basePath, signingId) {
9883
+ return `${basePath}.signing-${signingId}.pdf`;
9884
+ }
9885
+ /**
9886
+ * A scratch file beside the base: `<base>.<tag>-<id>.<ext>`. Unique per
9887
+ * id, so a file a document holds open is never rewritten; the caller that
9888
+ * names it registers its removal before anything writes it.
9889
+ */
9890
+ function scratchPath(basePath, tag, id, ext) {
9891
+ return `${basePath}.${tag}-${id}.${ext}`;
9892
+ }
9893
+ //#endregion
9894
+ //#region src/features/signature/internal/appearance.ts
9895
+ /** `EPDF_STAMP_FIT_CONTAIN`: uniform scale, letterboxed and centred in the rect. */
9896
+ const FIT_CONTAIN = 0;
9897
+ /**
9898
+ * Draw one page of a PDF into a widget annotation's normal appearance —
9899
+ * what a signature's mark is, whether the field is being signed
9900
+ * (`SignatureMutator`) or only filled visually (`FormMutator`).
9901
+ *
9902
+ * Two fork calls, the same pair the stamp path makes: `SetAppearanceFromPage`
9903
+ * clones the page into the document as a wrapped Form XObject (page content
9904
+ * and resources in a child form, the outer stream a placement matrix), then
9905
+ * `UpdateAppearanceToRect` writes that placement — the page fitted uniformly
9906
+ * into the widget's rect. Without the second call the page sits in its own
9907
+ * box and the viewer's BBox→/Rect mapping stretches it to the rect's aspect.
9908
+ */
9909
+ function bakeWidgetAppearance(runtime, docPtr, widget, pdf, pageIndex) {
9910
+ const { mem, fn } = runtime;
9911
+ const stack = new CloseStack();
9912
+ try {
9913
+ const pagePtr = fn.EPDFDoc_LoadPageByObjectNumber(docPtr, widget.pageObjectNumber);
9914
+ if (!pagePtr) throw new EngineError(EngineErrorCode.NotFound, "the widget page could not be loaded");
9915
+ stack.push(() => fn.FPDF_ClosePage(pagePtr));
9916
+ const annotPtr = fn.EPDFPage_GetAnnotByObjectNumber(pagePtr, widget.annotObjectNumber);
9917
+ if (!annotPtr) throw new EngineError(EngineErrorCode.NotFound, "the signature widget could not be loaded");
9918
+ stack.push(() => fn.FPDFPage_CloseAnnot(annotPtr));
9919
+ const dataPtr = mem.alloc(pdf.byteLength);
9920
+ stack.push(() => mem.free(dataPtr));
9921
+ mem.writeBytes(dataPtr, pdf);
9922
+ const artworkPtr = fn.FPDF_LoadMemDocument64(dataPtr, pdf.byteLength, "");
9923
+ if (!artworkPtr) throw new EngineError(EngineErrorCode.MalformedPdf, "the appearance PDF could not be opened");
9924
+ stack.push(() => fn.FPDF_CloseDocument(artworkPtr));
9925
+ if (!fn.EPDFAnnot_SetAppearanceFromPage(annotPtr, artworkPtr, pageIndex)) throw new EngineError(EngineErrorCode.InvalidArg, "the appearance page could not be drawn into the widget");
9926
+ if (!fn.EPDFAnnot_UpdateAppearanceToRect(annotPtr, FIT_CONTAIN)) throw new EngineError(EngineErrorCode.Unknown, "the appearance page could not be fitted into the widget");
9927
+ } finally {
9928
+ stack.close();
9929
+ }
9930
+ }
9931
+ //#endregion
9932
+ //#region src/features/signature/internal/sealCheck.ts
9933
+ /**
9934
+ * The one check both signing paths make before sealed bytes become a
9935
+ * version — the in-session complete and the server's session-less
9936
+ * finalize. Reading the result back through the ordinary signature model
9937
+ * proves the bytes, not the writer: the field is signed, its signature
9938
+ * covers the whole LAST revision (the candidate's own, never an earlier
9939
+ * one), its /ByteRange is the one the digest was computed over, and the
9940
+ * /Contents decode to exactly the CMS installed. Anything else is refused
9941
+ * and the bytes are never installed.
9942
+ */
9943
+ function assertSealedSignature(runtime, docPtr, model, expected) {
9944
+ const refuse = (why) => new EngineError(EngineErrorCode.SignatureRefused, `the sealed bytes ${why}`);
9945
+ const signatures = readSignaturesFromModel(runtime, model);
9946
+ const signature = signatures.find((s) => s.field.kind === "objectNumber" && s.field.fieldObjectNumber === expected.fieldObjectNumber);
9947
+ if (!signature) throw refuse("lost the signature field");
9948
+ if (!signature.signed || signature.coverage !== "whole-revision" || !signature.byteRange) throw refuse("do not carry a whole-revision signature on the field");
9949
+ if (!sameRange(signature.byteRange, expected.byteRange)) throw refuse(`carry /ByteRange [${signature.byteRange.join(" ")}] where [${expected.byteRange.join(" ")}] was sealed`);
9950
+ if (!runtime.fn.EPDFSig_IsRevisionChainValid(model)) throw refuse("have a broken cross-reference chain");
9951
+ const revisions = readRevisions(runtime, docPtr, signatures);
9952
+ if (signature.revisionIndex !== revisions.length - 1) throw refuse("seal an earlier revision than the candidate");
9953
+ if (!bytesEqual(readContentsAt(runtime, model, signature.index), expected.cms)) throw refuse("carry /Contents that are not the CMS installed");
9954
+ return signature;
9955
+ }
9956
+ function sameRange(a, b) {
9957
+ return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
9958
+ }
9959
+ function bytesEqual(a, b) {
9960
+ if (a.byteLength !== b.byteLength) return false;
9961
+ for (let i = 0; i < a.byteLength; i++) if (a[i] !== b[i]) return false;
9962
+ return true;
9963
+ }
9964
+ //#endregion
9965
+ //#region src/features/forms/internal/wideStringArray.ts
9966
+ /**
9967
+ * Marshal a `const FPDF_WIDESTRING*` array: encode every string into the
9968
+ * runtime heap, write the pointer table with the runtime's pointer width
9969
+ * (bigint pointers cannot be poked into the wasm 32-bit heap), run |body|,
9970
+ * free everything.
9971
+ */
9972
+ function withWideStringArray(runtime, values, body) {
9973
+ const { mem } = runtime;
9974
+ const stringPtrs = values.map((value) => mem.writeU16String(value));
9975
+ const isWasm = runtime.kind === "wasm";
9976
+ const ptrSize = isWasm ? 4 : 8;
9977
+ try {
9978
+ return withScratch(mem, Math.max(1, stringPtrs.length * ptrSize), (arrayPtr) => {
9979
+ stringPtrs.forEach((ptr, i) => isWasm ? mem.poke(arrayPtr, "i32", Number(ptr), i * 4) : mem.poke(arrayPtr, "i64", ptr, i * 8));
9980
+ return body(arrayPtr, stringPtrs.length);
9981
+ });
9982
+ } finally {
9983
+ for (let i = stringPtrs.length - 1; i >= 0; i--) mem.free(stringPtrs[i]);
9984
+ }
9985
+ }
9986
+ //#endregion
9987
+ //#region src/features/signature/SignatureMutator.ts
9988
+ const SUBFILTER_CODE = {
9989
+ "adbe.pkcs7.detached": 0,
9990
+ "ETSI.CAdES.detached": 1,
9991
+ "ETSI.RFC3161": 2
9992
+ };
9993
+ const FIELD_ACTION_CODE = {
9994
+ all: 1,
9995
+ include: 2,
9996
+ exclude: 3
9997
+ };
9998
+ const DEFAULT_CONTENTS_SIZE = 8192;
9999
+ const MIN_CONTENTS_SIZE = 256;
10000
+ const MAX_CONTENTS_SIZE = 4 * 1024 * 1024;
10001
+ const PREPARE_ILP32 = {
10002
+ bytes: 52,
10003
+ ptrBytes: 4,
10004
+ subfilter: 0,
10005
+ digest: 4,
10006
+ contentsSize: 8,
10007
+ name: 12,
10008
+ reason: 16,
10009
+ location: 20,
10010
+ contactInfo: 24,
10011
+ signingTime: 28,
10012
+ docmdpPermission: 32,
10013
+ fieldmdpAction: 36,
10014
+ fieldmdpFields: 40,
10015
+ fieldmdpFieldCount: 44,
10016
+ lockPermission: 48
10017
+ };
10018
+ const PREPARE_LP64 = {
10019
+ bytes: 80,
10020
+ ptrBytes: 8,
10021
+ subfilter: 0,
10022
+ digest: 4,
10023
+ contentsSize: 8,
10024
+ name: 16,
10025
+ reason: 24,
10026
+ location: 32,
10027
+ contactInfo: 40,
10028
+ signingTime: 48,
10029
+ docmdpPermission: 56,
10030
+ fieldmdpAction: 60,
10031
+ fieldmdpFields: 64,
10032
+ fieldmdpFieldCount: 72,
10033
+ lockPermission: 76
10034
+ };
10035
+ /**
10036
+ * The two-phase signing protocol on a session, local and native alike.
10037
+ *
10038
+ * `prepare` never touches the live document: it builds a CANDIDATE — a
10039
+ * fresh layer over the session's own immutable base (see `openCandidate`)
10040
+ * — writes the signature there, saves it through a `CandidateStore`
10041
+ * (memory locally, a file beside the base on file-backed sessions), seals
10042
+ * it, and parks the saved candidate on the session. `complete` writes the
10043
+ * CMS into it, opens it as a new base with a fresh layer, proves the new
10044
+ * signature seals a whole revision, and installs it as the session's
10045
+ * document. `abort` discards the candidate.
10046
+ */
10047
+ var SignatureMutator = class {
10048
+ constructor(runtime, session, baseDocuments, candidatePath) {
10049
+ this.runtime = runtime;
10050
+ this.session = session;
10051
+ this.baseDocuments = baseDocuments;
10052
+ this.candidatePath = candidatePath;
10053
+ }
10054
+ prepare(input) {
10055
+ if (this.session.pendingSigning) throw new EngineError(EngineErrorCode.SigningPending, `a signing is pending (${this.session.pendingSigning.prepared.signingId}); complete or abort it first`);
10056
+ const reader = new SignatureReader(this.runtime, this.session);
10057
+ const field = reader.resolveField(input.field);
10058
+ if (field.signed) throw new EngineError(EngineErrorCode.SignatureRefused, "the signature field is already signed");
10059
+ if (input.appearance && !field.widget) throw new EngineError(EngineErrorCode.SignatureRefused, "the signature field has no widget to carry an appearance");
10060
+ const algorithm = input.digest ?? "sha256";
10061
+ if (!(algorithm in DIGEST_CODE) || algorithm === "sha1") throw new EngineError(EngineErrorCode.SignatureRefused, `unsupported digest '${algorithm}'`);
10062
+ const subFilter = (input.kind ?? "signature") === "timestamp" ? "ETSI.RFC3161" : input.subFilter ?? "ETSI.CAdES.detached";
10063
+ if (!(subFilter in SUBFILTER_CODE)) throw new EngineError(EngineErrorCode.SignatureRefused, `unsupported subFilter '${subFilter}'`);
10064
+ const contentsSize = input.contentsSize ?? DEFAULT_CONTENTS_SIZE;
10065
+ if (!Number.isInteger(contentsSize) || contentsSize < MIN_CONTENTS_SIZE || contentsSize > MAX_CONTENTS_SIZE) throw new EngineError(EngineErrorCode.SignatureRefused, `contentsSize must be an integer in [${MIN_CONTENTS_SIZE}, ${MAX_CONTENTS_SIZE}]`);
10066
+ const expectedVersion = this.session.versionRef(reader.version().sha256);
10067
+ const signingId = generateUuid();
10068
+ const stack = new CloseStack();
10069
+ try {
10070
+ const candidate = this.openCandidate(signingId, stack);
10071
+ stack.push(() => candidate.close());
10072
+ const attribution = input.attribution ?? input.signer;
10073
+ const valueObjNum = this.callPrepare(candidate.docPtr, field.fieldObjectNumber, {
10074
+ subfilter: SUBFILTER_CODE[subFilter],
10075
+ digest: DIGEST_CODE[algorithm],
10076
+ contentsSize,
10077
+ name: attribution?.name ?? null,
10078
+ reason: attribution?.reason ?? null,
10079
+ location: attribution?.location ?? null,
10080
+ contactInfo: attribution?.contactInfo ?? null,
10081
+ signingTime: input.signingTime ?? null,
10082
+ docmdpPermission: input.certify?.permission ?? 0,
10083
+ fieldmdpAction: input.lock ? FIELD_ACTION_CODE[input.lock.action] : 0,
10084
+ fieldmdpFields: input.lock?.action === "all" ? [] : input.lock?.fields ?? [],
10085
+ lockPermission: input.lock?.permission ?? 0
10086
+ });
10087
+ if (valueObjNum === 0) throw new EngineError(EngineErrorCode.SignatureRefused, "the engine refused to author the signature (the field is signed, read-only or locked, a certification is not allowed here, the seed value requires what is not implemented, or the request is inconsistent)");
10088
+ if (input.appearance && field.widget) bakeWidgetAppearance(this.runtime, candidate.docPtr, field.widget, input.appearance.pdf, input.appearance.pageIndex ?? 0);
10089
+ const store = this.storeFor();
10090
+ const saved = store.save(candidate.docPtr, valueObjNum, signingId);
10091
+ let sealed;
10092
+ try {
10093
+ sealed = store.seal(saved, algorithm);
10094
+ } catch (error) {
10095
+ store.discard(saved);
10096
+ throw error;
10097
+ }
10098
+ const prepared = {
10099
+ signingId,
10100
+ digest: sealed.digest,
10101
+ algorithm,
10102
+ byteRange: sealed.byteRange,
10103
+ contentsSize,
10104
+ subFilter,
10105
+ expectedVersion,
10106
+ expiresAt: null
10107
+ };
10108
+ this.session.pendingSigning = {
10109
+ prepared,
10110
+ fieldObjectNumber: field.fieldObjectNumber,
10111
+ saved,
10112
+ contentsOffset: sealed.contentsOffset,
10113
+ contentsHexLength: sealed.contentsHexLength
10114
+ };
10115
+ return prepared;
10116
+ } finally {
10117
+ stack.close();
10118
+ }
10119
+ }
10120
+ complete(input) {
10121
+ const pending = this.session.pendingSigning;
10122
+ if (!pending || pending.prepared.signingId !== input.signingId) {
10123
+ const last = this.session.lastCompletion;
10124
+ if (last && last.signingId === input.signingId) {
10125
+ if (!bytesEqual(last.cms, input.cms)) throw new EngineError(EngineErrorCode.SignatureRefused, "this signing already completed with a different CMS");
10126
+ return {
10127
+ ...last.result,
10128
+ status: "already-completed"
10129
+ };
10130
+ }
10131
+ throw new EngineError(EngineErrorCode.NotFound, `no pending signing '${input.signingId}'`);
10132
+ }
10133
+ if (!sameVersion(pending.prepared.expectedVersion, input.expectedVersion)) throw new EngineError(EngineErrorCode.SigningVersionMismatch, "expectedVersion is not the version the candidate was prepared on");
10134
+ if (input.cms.byteLength === 0 || input.cms[0] !== 48) throw new EngineError(EngineErrorCode.SignatureRefused, "the CMS is not a DER SEQUENCE");
10135
+ if (input.cms.byteLength > pending.prepared.contentsSize) throw new EngineError(EngineErrorCode.SignatureRefused, `the CMS (${input.cms.byteLength} bytes) does not fit the reserved ${pending.prepared.contentsSize} bytes`);
10136
+ const store = this.storeFor();
10137
+ const sealed = {
10138
+ byteRange: pending.prepared.byteRange,
10139
+ contentsOffset: pending.contentsOffset,
10140
+ contentsHexLength: pending.contentsHexLength,
10141
+ digest: pending.prepared.digest
10142
+ };
10143
+ store.writeContents(pending.saved, sealed, input.cms);
10144
+ const handle = store.openSealed(pending.saved, this.session.password);
10145
+ this.verifySealed(handle, {
10146
+ fieldObjectNumber: pending.fieldObjectNumber,
10147
+ byteRange: pending.prepared.byteRange,
10148
+ cms: input.cms
10149
+ });
10150
+ this.session.install(handle);
10151
+ disposeFormModel(this.runtime, this.session);
10152
+ disposeSignatureModel(this.runtime, this.session);
10153
+ const reader = new SignatureReader(this.runtime, this.session);
10154
+ const snapshot = reader.readSnapshot();
10155
+ const signature = snapshot.signatures.find((s) => s.field.kind === "objectNumber" && s.field.fieldObjectNumber === pending.fieldObjectNumber);
10156
+ if (!signature) throw new EngineError(EngineErrorCode.Unknown, "the installed document lost the signature field");
10157
+ const result = {
10158
+ status: "completed",
10159
+ signature,
10160
+ version: reader.version(),
10161
+ previous: pending.prepared.expectedVersion,
10162
+ protection: snapshot.protection,
10163
+ meta: {
10164
+ affectedPages: this.session.allRecords().map((r) => this.session.pageState(r.pageObjectNumber)),
10165
+ cacheDelta: null
10166
+ }
10167
+ };
10168
+ this.session.lastCompletion = {
10169
+ signingId: input.signingId,
10170
+ cms: input.cms.slice(),
10171
+ result
10172
+ };
10173
+ return result;
10174
+ }
10175
+ abort(signingId) {
10176
+ const pending = this.session.pendingSigning;
10177
+ if (pending && pending.prepared.signingId === signingId) {
10178
+ this.session.pendingSigning = null;
10179
+ this.storeFor().discard(pending.saved);
10180
+ return { status: "aborted" };
10181
+ }
10182
+ if (this.session.lastCompletion?.signingId === signingId) return { status: "already-completed" };
10183
+ return { status: "unknown" };
10184
+ }
10185
+ callPrepare(docPtr, fieldObjectNumber, opts) {
10186
+ const { mem, fn } = this.runtime;
10187
+ const layout = this.runtime.kind === "wasm" ? PREPARE_ILP32 : PREPARE_LP64;
10188
+ const owned = [];
10189
+ const wide = (value) => {
10190
+ if (value === null) return NULL_PTR;
10191
+ const ptr = mem.writeU16String(value);
10192
+ owned.push(ptr);
10193
+ return ptr;
10194
+ };
10195
+ const utf8 = (value) => {
10196
+ if (value === null) return NULL_PTR;
10197
+ const ptr = mem.writeU8String(value);
10198
+ owned.push(ptr);
10199
+ return ptr;
10200
+ };
10201
+ try {
10202
+ return withWideStringArray(this.runtime, opts.fieldmdpFields, (fieldsPtr, fieldCount) => withScratch(mem, layout.bytes, (structPtr) => {
10203
+ for (let off = 0; off < layout.bytes; off += 4) mem.poke(structPtr, "i32", 0, off);
10204
+ const pokePtr = (offset, ptr) => {
10205
+ if (layout.ptrBytes === 4) mem.poke(structPtr, "i32", Number(ptr), offset);
10206
+ else mem.poke(structPtr, "i64", ptr, offset);
10207
+ };
10208
+ mem.poke(structPtr, "i32", opts.subfilter, layout.subfilter);
10209
+ mem.poke(structPtr, "i32", opts.digest, layout.digest);
10210
+ mem.poke(structPtr, "i32", opts.contentsSize, layout.contentsSize);
10211
+ pokePtr(layout.name, wide(opts.name));
10212
+ pokePtr(layout.reason, wide(opts.reason));
10213
+ pokePtr(layout.location, wide(opts.location));
10214
+ pokePtr(layout.contactInfo, wide(opts.contactInfo));
10215
+ pokePtr(layout.signingTime, utf8(opts.signingTime));
10216
+ mem.poke(structPtr, "i32", opts.docmdpPermission, layout.docmdpPermission);
10217
+ mem.poke(structPtr, "i32", opts.fieldmdpAction, layout.fieldmdpAction);
10218
+ pokePtr(layout.fieldmdpFields, fieldCount > 0 ? fieldsPtr : NULL_PTR);
10219
+ mem.poke(structPtr, "i32", fieldCount, layout.fieldmdpFieldCount);
10220
+ mem.poke(structPtr, "i32", opts.lockPermission, layout.lockPermission);
10221
+ return fn.EPDFSig_Prepare(docPtr, fieldObjectNumber, structPtr);
10222
+ }));
10223
+ } finally {
10224
+ for (let i = owned.length - 1; i >= 0; i--) mem.free(owned[i]);
10225
+ }
10226
+ }
10227
+ /** Draw a page of `pdf` into the widget's appearance stream on the candidate. */
10228
+ /**
10229
+ * File-backed sessions (a file base in the registry: the server) persist
10230
+ * the candidate as a file beside the base; everything else keeps it in
10231
+ * memory. The choice follows the SESSION's base, not the candidate's.
10232
+ */
10233
+ storeFor() {
10234
+ const base = this.session.source.base;
10235
+ if (base?.kind === "file") return new FileCandidateStore(this.runtime, this.baseDocuments, base.path, this.candidatePath);
10236
+ return new MemoryCandidateStore(this.runtime, this.baseDocuments);
10237
+ }
10238
+ /**
10239
+ * Prove the sealed bytes carry a whole-revision signature on the field
10240
+ * before anything is installed; closes the handle on failure.
10241
+ */
10242
+ /**
10243
+ * Read the sealed candidate back before it is installed: the checks in
10244
+ * `assertSealedSignature` are made on a model loaded from the sealed
10245
+ * bytes themselves, so a candidate the writer got wrong never becomes
10246
+ * the session's bytes.
10247
+ */
10248
+ verifySealed(handle, expected) {
10249
+ const { fn } = this.runtime;
10250
+ try {
10251
+ const model = fn.EPDFSig_LoadModel(handle.docPtr);
10252
+ if (!model) throw new EngineError(EngineErrorCode.Unknown, "the sealed bytes have no signature model");
10253
+ try {
10254
+ assertSealedSignature(this.runtime, handle.docPtr, model, expected);
10255
+ } finally {
10256
+ fn.EPDFSig_CloseModel(model);
10257
+ }
10258
+ if (this.session.kind !== "layer") this.session.persistLayerArtifact = false;
10259
+ } catch (error) {
10260
+ handle.close();
10261
+ throw error;
10262
+ }
10263
+ }
10264
+ /**
10265
+ * The candidate the signature is authored on. A layer session reopens a
10266
+ * fresh layer over ITS OWN immutable base (a registry retain, no copy of
10267
+ * the file), fed the layer it was opened with — or, with unsaved edits,
10268
+ * the artifact a save would write, which is cumulative: the edits and
10269
+ * the signature then share one revision, as Acrobat saves a
10270
+ * fill-and-sign. The fallback freezes the complete loaded bytes as a new
10271
+ * memory base: for a plain session, and for a layer whose loaded delta
10272
+ * already holds signed bytes (a layer save rewrites the delta and would
10273
+ * drop them; the fork refuses to prepare such a candidate).
10274
+ */
10275
+ openCandidate(signingId, stack) {
10276
+ const password = this.session.password;
10277
+ const source = this.session.source;
10278
+ const saver = new DocumentSaver(this.runtime, this.session);
10279
+ const scratch = saver.canWriteScratchFiles() && source.base?.kind === "file" ? source.base.path : null;
10280
+ if (this.session.kind === "layer" && source.base && !this.loadedDeltaHoldsSignedBytes()) {
10281
+ const base = this.baseDocuments.retainByKey(source.base.key);
10282
+ if (base) {
10283
+ let layer = source.layer ?? { kind: "fresh" };
10284
+ if (this.session.hasUnsavedEdits()) if (scratch) {
10285
+ const path = scratchPath(scratch, "signing", signingId, "layer");
10286
+ stack.push(() => this.runtime.fileWrite.removeFile(path));
10287
+ if (saver.saveLayerArtifactToFileEx(path).changedSinceLoad) layer = {
10288
+ kind: "artifact-file",
10289
+ path
10290
+ };
10291
+ } else {
10292
+ const artifact = saver.saveLayerArtifactEx();
10293
+ if (artifact.changedSinceLoad) layer = {
10294
+ kind: "artifact",
10295
+ bytes: artifact.bytes
10296
+ };
10297
+ }
10298
+ return openLayerDocument(this.runtime, base, layer, password);
10299
+ }
10300
+ }
10301
+ if (scratch) {
10302
+ const path = scratchPath(scratch, "signing", signingId, "base.pdf");
10303
+ stack.push(() => this.runtime.fileWrite.removeFile(path));
10304
+ saver.snapshotToFile(path);
10305
+ const base = this.baseDocuments.acquireFileBase({
10306
+ key: `candidate:${signingId}`,
10307
+ path,
10308
+ password
10309
+ });
10310
+ return openLayerDocument(this.runtime, base, { kind: "fresh" }, password);
10311
+ }
10312
+ const candidateBytes = saver.snapshot().bytes;
10313
+ const base = this.baseDocuments.acquireMemoryBase({
10314
+ key: `candidate:${signingId}`,
10315
+ bytes: new Uint8Array(candidateBytes),
10316
+ password
10317
+ });
10318
+ return openLayerDocument(this.runtime, base, { kind: "fresh" }, password);
10319
+ }
10320
+ /** Whether a signature's byte range reaches into the layer's loaded delta (past the base). */
10321
+ loadedDeltaHoldsSignedBytes() {
10322
+ const layer = this.session.source.layer;
10323
+ if (!layer || layer.kind === "fresh") return false;
10324
+ const baseSize = Number(this.runtime.fn.EPDFDoc_GetBaseBytesSize(this.session.requireDocPtr()));
10325
+ return new SignatureReader(this.runtime, this.session).readSnapshot().signatures.some((s) => s.signed && s.byteRange !== null && s.byteRange[2] + s.byteRange[3] > baseSize);
10326
+ }
10327
+ };
10328
+ function sameVersion(a, b) {
10329
+ return a.baseSha256 === b.baseSha256 && a.editsVersion === b.editsVersion;
10330
+ }
10331
+ //#endregion
10332
+ //#region src/features/signature/SignatureAnalyzer.ts
10333
+ const CHANGE_BY_CODE = {
10334
+ 0: "added",
10335
+ 1: "modified",
10336
+ 2: "freed"
10337
+ };
10338
+ const KIND_BY_CODE = {
10339
+ 0: "dictionary",
10340
+ 1: "stream",
10341
+ 2: "array",
10342
+ 3: "scalar",
10343
+ 4: "xref",
10344
+ 5: "objstm",
10345
+ 6: "trailer"
10346
+ };
10347
+ const DIFF_OLD = 0;
10348
+ const DIFF_NEW = 1;
10349
+ /** EPDF_DIFF_READ_OK / FAILED / ABSENT. */
10350
+ const READ_BY_CODE = {
10351
+ 0: "ok",
10352
+ 1: "failed",
10353
+ 2: "absent"
10354
+ };
10355
+ /** The objects an edge may be anchored at: the trailer, the structure of the revision, and every changed object. */
10356
+ function anchorSet(s, changed) {
10357
+ const out = /* @__PURE__ */ new Set([
10358
+ 0,
10359
+ s.root,
10360
+ s.pagesRoot,
10361
+ ...s.pages,
10362
+ ...changed
10363
+ ]);
10364
+ if (s.acroForm) out.add(s.acroForm);
10365
+ for (const f of s.fields) {
10366
+ out.add(f.objectNumber);
10367
+ for (const w of f.widgets) out.add(w);
10368
+ }
10369
+ return out;
10370
+ }
10371
+ /**
10372
+ * Modification analysis over a session: which revisions to compare, the
10373
+ * fork's exact object diff for each adjacent pair, each revision's
10374
+ * structure, and the pure rule engine's verdict per step. Every revision
10375
+ * is opened as its own prefix document from the target's loaded bytes;
10376
+ * `working-copy` first snapshots the session the way a save would.
10377
+ */
10378
+ var SignatureAnalyzer = class {
10379
+ constructor(runtime, session) {
10380
+ this.runtime = runtime;
10381
+ this.session = session;
10382
+ }
10383
+ /**
10384
+ * The signature snapshot of the session's working copy: the unsaved
10385
+ * state snapshotted the way a save would write it, as one more revision
10386
+ * over the loaded bytes. Identical to the loaded-bytes snapshot when
10387
+ * nothing is unsaved.
10388
+ */
10389
+ readWorkingCopySnapshot() {
10390
+ const stack = new CloseStack();
10391
+ try {
10392
+ const copy = this.openWorkingCopy(stack);
10393
+ if (copy.source === "loaded") return new SignatureReader(this.runtime, this.session).readSnapshot();
10394
+ const target = copy.target;
10395
+ return withSignatureModel(this.runtime, target, (model) => {
10396
+ const chainValid = this.runtime.fn.EPDFSig_IsRevisionChainValid(model);
10397
+ const signatures = readSignaturesFromModel(this.runtime, model);
10398
+ return {
10399
+ chainValid,
10400
+ revisions: chainValid ? readRevisions(this.runtime, target, signatures) : [],
10401
+ signatures,
10402
+ protection: deriveProtection(signatures)
10403
+ };
10404
+ });
10405
+ } finally {
10406
+ stack.close();
10407
+ }
10408
+ }
10409
+ analyze(input) {
10410
+ const reader = new SignatureReader(this.runtime, this.session);
10411
+ const snapshot = reader.readSnapshot();
10412
+ const version = reader.version();
10413
+ const mode = input.exploratoryLevel ? "exploratory" : "authoritative";
10414
+ const detail = input.detail ?? "summary";
10415
+ let basisSource = "persisted";
10416
+ const editsVersion = this.session.mutationSeq();
10417
+ let sinceRevision;
10418
+ let sinceSignature = null;
10419
+ if ("signatureIndex" in input.since) {
10420
+ const sig = snapshot.signatures[input.since.signatureIndex];
10421
+ if (!sig) throw new EngineError(EngineErrorCode.NotFound, `no signature ${input.since.signatureIndex}`);
10422
+ if (!sig.signed || sig.revisionIndex === null) throw new EngineError(EngineErrorCode.InvalidArg, `signature ${input.since.signatureIndex} seals no whole revision; nothing to analyze from`);
10423
+ sinceRevision = sig.revisionIndex;
10424
+ sinceSignature = sig.index;
10425
+ } else sinceRevision = input.since.revisionIndex;
10426
+ if (!snapshot.chainValid) {
10427
+ const current = {
10428
+ verdict: "indeterminate",
10429
+ complete: false,
10430
+ findings: [{
10431
+ rule: "revision-chain",
10432
+ verdict: "incomplete",
10433
+ objectNumber: 0,
10434
+ detail: "the cross-reference chain is broken or was rebuilt: no revision can be told from another"
10435
+ }],
10436
+ method: "net-state"
10437
+ };
10438
+ return {
10439
+ mode,
10440
+ policyVersion: SIGNATURE_POLICY_VERSION,
10441
+ basis: {
10442
+ version,
10443
+ editsVersion,
10444
+ source: "persisted"
10445
+ },
10446
+ since: {
10447
+ revisionIndex: sinceRevision,
10448
+ signatureIndex: sinceSignature
10449
+ },
10450
+ until: { revisionIndex: sinceRevision },
10451
+ restrictions: [],
10452
+ current: {
10453
+ ...current,
10454
+ primary: current.findings[0]
10455
+ },
10456
+ later: {
10457
+ revisionCount: 0,
10458
+ undoneObjectNumbers: []
10459
+ },
10460
+ verdict: "indeterminate",
10461
+ steps: []
10462
+ };
10463
+ }
10464
+ if (!Number.isInteger(sinceRevision) || sinceRevision < 0 || sinceRevision >= snapshot.revisions.length) throw new EngineError(EngineErrorCode.InvalidArg, `no revision ${sinceRevision}`);
10465
+ const stack = new CloseStack();
10466
+ try {
10467
+ let target = this.session.requireDocPtr();
10468
+ let revisions = snapshot.revisions;
10469
+ const copy = input.until === "working-copy" ? this.openWorkingCopy(stack) : null;
10470
+ if (copy && copy.source === "working-copy") {
10471
+ basisSource = "working-copy";
10472
+ target = copy.target;
10473
+ const signatures = withSignatureModel(this.runtime, target, (m) => readSignaturesFromModel(this.runtime, m));
10474
+ revisions = readRevisions(this.runtime, target, signatures);
10475
+ if (revisions.length === 0) throw new EngineError(EngineErrorCode.MalformedPdf, "the working copy has no valid revision chain");
10476
+ }
10477
+ let untilRevision = revisions.length - 1;
10478
+ if (typeof input.until === "object") {
10479
+ untilRevision = input.until.revisionIndex;
10480
+ if (!Number.isInteger(untilRevision) || untilRevision < sinceRevision || untilRevision >= revisions.length) throw new EngineError(EngineErrorCode.InvalidArg, `no revision ${untilRevision} at or after ${sinceRevision}`);
10481
+ }
10482
+ const pair = new CloseStack();
10483
+ let before;
10484
+ let after;
10485
+ let changes = [];
10486
+ let health;
10487
+ try {
10488
+ const sealed = this.openPrefix(target, revisions, sinceRevision, pair);
10489
+ const judged = untilRevision === revisions.length - 1 ? target : this.openPrefix(target, revisions, untilRevision, pair);
10490
+ before = readStructure(this.runtime, sealed);
10491
+ after = untilRevision === sinceRevision ? before : readStructure(this.runtime, judged);
10492
+ if (untilRevision > sinceRevision) ({changes, health} = this.compare(sealed, judged, before, after));
10493
+ } finally {
10494
+ pair.close();
10495
+ }
10496
+ const judgedSignature = sinceSignature === null ? null : before.signatures[sinceSignature] ?? null;
10497
+ const restrictions = restrictionsFor(before, judgedSignature);
10498
+ const levelOverride = input.exploratoryLevel;
10499
+ const net = evaluateStep({
10500
+ older: sinceRevision,
10501
+ newer: untilRevision,
10502
+ changes,
10503
+ before,
10504
+ after,
10505
+ health,
10506
+ restrictions: {
10507
+ level: restrictions.level,
10508
+ locks: restrictions.locks
10509
+ },
10510
+ levelOverride
10511
+ });
10512
+ const certifies = restrictions.anchors.some((r) => r.source === "docmdp" && r.own);
10513
+ const intermediate = untilRevision - sinceRevision;
10514
+ let steps;
10515
+ if (intermediate <= 1) steps = intermediate === 1 ? [net] : [];
10516
+ else if (certifies || detail === "full") steps = this.walkSteps(target, revisions, sinceRevision, untilRevision, before, {
10517
+ level: restrictions.level,
10518
+ locks: restrictions.locks,
10519
+ levelOverride
10520
+ });
10521
+ else steps = [];
10522
+ const current = certifies ? {
10523
+ ...combine(net, steps),
10524
+ method: "net-state+replay"
10525
+ } : {
10526
+ ...assessmentOf(net),
10527
+ method: "net-state"
10528
+ };
10529
+ return {
10530
+ mode,
10531
+ policyVersion: SIGNATURE_POLICY_VERSION,
10532
+ basis: {
10533
+ version,
10534
+ editsVersion,
10535
+ source: basisSource
10536
+ },
10537
+ since: {
10538
+ revisionIndex: sinceRevision,
10539
+ signatureIndex: sinceSignature
10540
+ },
10541
+ until: { revisionIndex: untilRevision },
10542
+ restrictions: restrictions.anchors,
10543
+ current,
10544
+ later: {
10545
+ revisionCount: intermediate,
10546
+ undoneObjectNumbers: changes.filter(sameEffectiveValue).map((c) => c.objectNumber)
10547
+ },
10548
+ verdict: current.verdict,
10549
+ steps
10550
+ };
10551
+ } finally {
10552
+ stack.close();
10553
+ }
10554
+ }
10555
+ /** Revision `index` of `target`'s bytes as its own document, owned by `stack`. */
10556
+ openPrefix(target, revisions, index, stack) {
10557
+ const prefix = this.runtime.fn.EPDFDoc_OpenRevision(target, BigInt(revisions[index].end));
10558
+ if (prefix === NULL_PTR) throw new EngineError(EngineErrorCode.MalformedPdf, `revision ${index} does not open as a document`);
10559
+ stack.push(() => this.runtime.fn.FPDF_CloseDocument(prefix));
10560
+ return prefix;
10561
+ }
10562
+ /**
10563
+ * Every pairwise step between `since` and `until`, two prefix documents
10564
+ * open at a time whatever the revision count. `sealedStructure` is the
10565
+ * structure of `since` (already read); every later structure is read once
10566
+ * and dropped with its document.
10567
+ */
10568
+ walkSteps(target, revisions, since, until, sealedStructure, restrictions) {
10569
+ const { fn } = this.runtime;
10570
+ const steps = [];
10571
+ let older = this.openPrefix(target, revisions, since, new CloseStack());
10572
+ let olderStructure = sealedStructure;
10573
+ try {
10574
+ for (let r = since + 1; r <= until; r++) {
10575
+ const newer = r === revisions.length - 1 ? target : this.openPrefix(target, revisions, r, new CloseStack());
10576
+ try {
10577
+ const newerStructure = readStructure(this.runtime, newer);
10578
+ const { changes, health } = this.compare(older, newer, olderStructure, newerStructure);
10579
+ steps.push(evaluateStep({
10580
+ older: r - 1,
10581
+ newer: r,
10582
+ changes,
10583
+ before: olderStructure,
10584
+ after: newerStructure,
10585
+ health,
10586
+ restrictions: {
10587
+ level: restrictions.level,
10588
+ locks: restrictions.locks
10589
+ },
10590
+ levelOverride: restrictions.levelOverride
10591
+ }));
10592
+ olderStructure = newerStructure;
10593
+ } finally {
10594
+ fn.FPDF_CloseDocument(older);
10595
+ older = newer;
10596
+ }
10597
+ }
10598
+ } finally {
10599
+ if (older !== target) fn.FPDF_CloseDocument(older);
10600
+ }
10601
+ return steps;
10602
+ }
10603
+ /**
10604
+ * The document as a save would write it, opened read-only, and whose
10605
+ * bytes that is. `loaded`: nothing was mutated since load, or the saver's
10606
+ * pass found no reachable object that differs from its loaded version —
10607
+ * the session's own document is the working copy. `working-copy`: a
10608
+ * layer session composes its immutable base with the cumulative delta
10609
+ * the pass emits (`EPDFDoc_OpenBaseOverlay`: no copy of the base, one
10610
+ * cross-reference parse, the loaded delta's revision REPLACED, as an
10611
+ * artifact is), or — when every edit brought the layer back to its base
10612
+ * — a fresh layer over that base; a plain session (unsigned by law 9, so
10613
+ * this is exploratory) materialises a standalone incremental save and
10614
+ * opens it whole.
10615
+ */
10616
+ openWorkingCopy(stack) {
10617
+ const { fn, mem } = this.runtime;
10618
+ const docPtr = this.session.requireDocPtr();
10619
+ if (!this.session.hasUnsavedEdits()) return {
10620
+ target: docPtr,
10621
+ source: "loaded"
10622
+ };
10623
+ const saver = new DocumentSaver(this.runtime, this.session);
10624
+ if (this.session.kind === "layer") {
10625
+ const base = this.session.source.base;
10626
+ if (saver.canWriteScratchFiles() && base?.kind === "file") {
10627
+ const path = scratchPath(base.path, "working-copy", generateUuid(), "delta");
10628
+ stack.push(() => this.runtime.fileWrite.removeFile(path));
10629
+ if (!saver.saveLayerDeltaToFileEx(path).changedSinceLoad) return {
10630
+ target: docPtr,
10631
+ source: "loaded"
10632
+ };
10633
+ if (this.runtime.fileAccess.sizeOf(path) === 0) return this.freshLayerOverBase(stack);
10634
+ const overlay = withUtf8CString(mem, path, (pathPtr) => fn.EPDFDoc_OpenBaseOverlayFromPath(docPtr, pathPtr));
10635
+ if (overlay === NULL_PTR) throw new EngineError(EngineErrorCode.MalformedPdf, "the working copy does not compose with its base");
10636
+ stack.push(() => fn.FPDF_CloseDocument(overlay));
10637
+ return {
10638
+ target: overlay,
10639
+ source: "working-copy"
10640
+ };
10641
+ }
10642
+ const delta = saver.saveLayerDeltaEx();
10643
+ if (!delta.changedSinceLoad) return {
10644
+ target: docPtr,
10645
+ source: "loaded"
10646
+ };
10647
+ if (delta.size === 0) return this.freshLayerOverBase(stack);
10648
+ const bytes = new Uint8Array(delta.bytes);
10649
+ const ptr = mem.alloc(bytes.byteLength);
10650
+ try {
10651
+ mem.writeBytes(ptr, bytes);
10652
+ const overlay = fn.EPDFDoc_OpenBaseOverlay(docPtr, ptr, bytes.byteLength);
10653
+ if (overlay === NULL_PTR) throw new EngineError(EngineErrorCode.MalformedPdf, "the working copy does not compose with its base");
10654
+ stack.push(() => fn.FPDF_CloseDocument(overlay));
10655
+ return {
10656
+ target: overlay,
10657
+ source: "working-copy"
10658
+ };
10659
+ } finally {
10660
+ mem.free(ptr);
10661
+ }
10662
+ }
10663
+ const saved = saver.saveStandaloneToBufferEx("incremental");
10664
+ if (saved.unchangedSinceLoad) return {
10665
+ target: docPtr,
10666
+ source: "loaded"
10667
+ };
10668
+ const opened = openFatMemoryDocument(this.runtime, new Uint8Array(saved.bytes), this.session.password);
10669
+ stack.push(() => opened.close());
10670
+ return {
10671
+ target: opened.docPtr,
10672
+ source: "working-copy"
10673
+ };
10674
+ }
10675
+ /** Changed since load, nothing to write: the layer equals its base, and a fresh layer over it IS that document. */
10676
+ freshLayerOverBase(stack) {
10677
+ const { fn, mem } = this.runtime;
10678
+ const base = fn.EPDFLayer_GetBaseDocument(this.session.requireDocPtr());
10679
+ const statusPtr = mem.alloc(4);
8361
10680
  try {
8362
10681
  mem.poke(statusPtr, "i32", -1);
8363
- const ok = fn.EPDFLayer_SaveLayerArtifact(this.session.requireDocPtr(), writer.ptr, statusPtr);
8364
- const status = Number(mem.peek(statusPtr, "i32"));
8365
- if (!ok || status !== 0) throw layerSaveError(status);
8366
- return { path };
10682
+ const fresh = fn.EPDFLayer_OpenLayer(base, NULL_PTR, this.session.password ?? "", statusPtr);
10683
+ if (fresh === NULL_PTR || Number(mem.peek(statusPtr, "i32")) !== 0) throw new EngineError(EngineErrorCode.MalformedPdf, "the working copy could not be opened over its base");
10684
+ stack.push(() => fn.FPDF_CloseDocument(fresh));
10685
+ return {
10686
+ target: fresh,
10687
+ source: "working-copy"
10688
+ };
8367
10689
  } finally {
8368
- writer.close();
8369
10690
  mem.free(statusPtr);
8370
10691
  }
8371
10692
  }
8372
- saveStandaloneToBuffer(mode) {
8373
- const { mem, fn } = this.runtime;
8374
- const sizePtr = mem.alloc(4);
8375
- let pdfPtr = null;
10693
+ /**
10694
+ * The fork's exact object diff between two revision documents,
10695
+ * marshalled: values in pass one, anchored edges in pass two (the anchors
10696
+ * are the changed objects plus each side's structural objects).
10697
+ */
10698
+ compare(older, newer, before, after) {
10699
+ const { fn, mem } = this.runtime;
10700
+ const diff = fn.EPDFDoc_CompareRevisions(older, newer);
10701
+ if (diff === NULL_PTR) throw new EngineError(EngineErrorCode.MalformedPdf, "the revisions do not share a byte history");
8376
10702
  try {
8377
- mem.poke(sizePtr, "i32", 0);
8378
- pdfPtr = fn.EPDF_SaveDocumentToOwnedBuffer(this.session.requireDocPtr(), pdfSaveModeFlags(mode), sizePtr);
8379
- const size = Number(mem.peek(sizePtr, "i32"));
8380
- if (!pdfPtr || size <= 0) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to save document");
8381
- const bytes = mem.readBytes(pdfPtr, size);
8382
- const buffer = new ArrayBuffer(bytes.byteLength);
8383
- new Uint8Array(buffer).set(bytes);
10703
+ const count = fn.EPDFObjectDiff_GetCount(diff);
10704
+ const changes = [];
10705
+ withScratchN(mem, [
10706
+ 4,
10707
+ 4,
10708
+ 4,
10709
+ 4,
10710
+ 4,
10711
+ 4
10712
+ ], ([numPtr, changePtr, kindPtr, oldGenPtr, newGenPtr, streamPtr]) => {
10713
+ for (let i = 0; i < count; i++) {
10714
+ for (const p of [
10715
+ numPtr,
10716
+ changePtr,
10717
+ kindPtr,
10718
+ oldGenPtr,
10719
+ newGenPtr,
10720
+ streamPtr
10721
+ ]) mem.poke(p, "i32", 0);
10722
+ if (!fn.EPDFObjectDiff_GetEntry(diff, i, numPtr, changePtr, kindPtr, oldGenPtr, newGenPtr, streamPtr)) throw new EngineError(EngineErrorCode.Unknown, `failed to read diff entry ${i}`);
10723
+ const objectNumber = Number(mem.peek(numPtr, "i32")) >>> 0;
10724
+ const oldGen = Number(mem.peek(oldGenPtr, "i32"));
10725
+ const newGen = Number(mem.peek(newGenPtr, "i32"));
10726
+ const oldValue = this.readValue(diff, i, DIFF_OLD);
10727
+ const newValue = this.readValue(diff, i, DIFF_NEW);
10728
+ let truncated = oldValue.truncated || newValue.truncated;
10729
+ let oldParsed = null;
10730
+ let newParsed = null;
10731
+ try {
10732
+ oldParsed = oldValue.text === null || oldValue.truncated ? null : parsePdfValue(oldValue.text);
10733
+ newParsed = newValue.text === null || newValue.truncated ? null : parsePdfValue(newValue.text);
10734
+ } catch {
10735
+ truncated = true;
10736
+ }
10737
+ const change = CHANGE_BY_CODE[Number(mem.peek(changePtr, "i32"))] ?? "modified";
10738
+ changes.push({
10739
+ objectNumber,
10740
+ change,
10741
+ kind: KIND_BY_CODE[Number(mem.peek(kindPtr, "i32"))] ?? "scalar",
10742
+ present: {
10743
+ old: change !== "added",
10744
+ new: change !== "freed"
10745
+ },
10746
+ generation: {
10747
+ old: oldGen < 0 ? null : oldGen,
10748
+ new: newGen < 0 ? null : newGen
10749
+ },
10750
+ value: {
10751
+ old: oldParsed,
10752
+ new: newParsed,
10753
+ truncated
10754
+ },
10755
+ raw: {
10756
+ old: oldValue.text,
10757
+ new: newValue.text
10758
+ },
10759
+ read: {
10760
+ old: READ_BY_CODE[fn.EPDFObjectDiff_GetReadStatus(diff, i, DIFF_OLD)] ?? "failed",
10761
+ new: READ_BY_CODE[fn.EPDFObjectDiff_GetReadStatus(diff, i, DIFF_NEW)] ?? "failed"
10762
+ },
10763
+ streamDataChanged: Number(mem.peek(streamPtr, "i32")) !== 0,
10764
+ usage: {
10765
+ old: [],
10766
+ new: []
10767
+ }
10768
+ });
10769
+ }
10770
+ });
8384
10771
  return {
8385
- bytes: buffer,
8386
- size
10772
+ changes: this.withAnchoredUsage(diff, changes, before, after),
10773
+ health: {
10774
+ old: this.readHealth(diff, DIFF_OLD),
10775
+ new: this.readHealth(diff, DIFF_NEW)
10776
+ }
8387
10777
  };
8388
10778
  } finally {
8389
- if (pdfPtr) fn.EPDF_FreeBuffer(pdfPtr);
8390
- mem.free(sizePtr);
10779
+ fn.EPDFObjectDiff_Close(diff);
8391
10780
  }
8392
10781
  }
8393
- saveStandaloneToFile(path, mode) {
8394
- const writer = this.runtime.fileWrite.toNodeFile(path);
10782
+ /** What the fork learned about one revision as a whole (see `RevisionHealth`). */
10783
+ readHealth(diff, which) {
10784
+ const { fn, mem } = this.runtime;
10785
+ return withScratchN(mem, [
10786
+ 4,
10787
+ 4,
10788
+ 4
10789
+ ], ([sparsePtr, barePtr, completePtr]) => {
10790
+ for (const p of [
10791
+ sparsePtr,
10792
+ barePtr,
10793
+ completePtr
10794
+ ]) mem.poke(p, "i32", 0);
10795
+ if (!fn.EPDFObjectDiff_GetRevisionHealth(diff, which, sparsePtr, barePtr, completePtr)) throw new EngineError(EngineErrorCode.Unknown, "failed to read revision health");
10796
+ return {
10797
+ sparseXref: Number(mem.peek(sparsePtr, "i32")) !== 0,
10798
+ bareReferences: Number(mem.peek(barePtr, "i32")) >>> 0,
10799
+ referrersComplete: Number(mem.peek(completePtr, "i32")) !== 0
10800
+ };
10801
+ });
10802
+ }
10803
+ /**
10804
+ * Pass two: every changed object's inbound edges, resolved through
10805
+ * unchanged non-structural objects to the nearest anchor (the fork's
10806
+ * referrer index covers every reachable object, so the walk never leaves
10807
+ * the diff). An object whose edges exceed the resolver's budget is
10808
+ * marked `usageIncomplete` and the evaluator refuses to permit it.
10809
+ */
10810
+ withAnchoredUsage(diff, changes, before, after) {
10811
+ const changed = new Set(changes.map((c) => c.objectNumber));
10812
+ const resolvers = {
10813
+ old: new EdgeResolver((n) => this.readReferrers(diff, DIFF_OLD, n), anchorSet(before, changed)),
10814
+ new: new EdgeResolver((n) => this.readReferrers(diff, DIFF_NEW, n), anchorSet(after, changed))
10815
+ };
10816
+ return changes.map((c) => {
10817
+ const old = c.present.old ? resolvers.old.resolve(c.objectNumber) : [];
10818
+ const now = c.present.new ? resolvers.new.resolve(c.objectNumber) : [];
10819
+ const incomplete = old === USAGE_INCOMPLETE || now === USAGE_INCOMPLETE;
10820
+ return {
10821
+ ...c,
10822
+ usage: {
10823
+ old: old === USAGE_INCOMPLETE ? [] : old,
10824
+ new: now === USAGE_INCOMPLETE ? [] : now
10825
+ },
10826
+ ...incomplete ? { usageIncomplete: true } : {}
10827
+ };
10828
+ });
10829
+ }
10830
+ readValue(diff, index, which) {
10831
+ const { fn, mem } = this.runtime;
10832
+ return withScratch(mem, 4, (truncPtr) => {
10833
+ mem.poke(truncPtr, "i32", 0);
10834
+ const length = fn.EPDFObjectDiff_GetValue(diff, index, which, NULL_PTR, 0, truncPtr);
10835
+ const truncatedProbe = Number(mem.peek(truncPtr, "i32")) !== 0;
10836
+ if (length <= 0) return {
10837
+ text: null,
10838
+ truncated: truncatedProbe
10839
+ };
10840
+ return withScratch(mem, length, (buf) => {
10841
+ mem.poke(truncPtr, "i32", 0);
10842
+ const written = fn.EPDFObjectDiff_GetValue(diff, index, which, buf, length, truncPtr);
10843
+ const truncated = Number(mem.peek(truncPtr, "i32")) !== 0;
10844
+ if (written <= 0) return {
10845
+ text: null,
10846
+ truncated
10847
+ };
10848
+ const bytes = mem.readBytes(buf, written - 1);
10849
+ let text = "";
10850
+ for (const b of bytes) text += String.fromCharCode(b);
10851
+ return {
10852
+ text,
10853
+ truncated
10854
+ };
10855
+ });
10856
+ });
10857
+ }
10858
+ readReferrers(diff, which, objectNumber) {
10859
+ const { fn, mem } = this.runtime;
10860
+ const count = fn.EPDFObjectDiff_GetReferrerCount(diff, which, objectNumber);
10861
+ const out = [];
10862
+ if (count <= 0) return out;
10863
+ withScratch(mem, 4, (parentPtr) => {
10864
+ for (let k = 0; k < count; k++) {
10865
+ mem.poke(parentPtr, "i32", 0);
10866
+ const length = fn.EPDFObjectDiff_GetReferrer(diff, which, objectNumber, k, parentPtr, NULL_PTR, 0);
10867
+ const label = length <= 0 ? "" : withScratch(mem, length, (buf) => {
10868
+ const written = fn.EPDFObjectDiff_GetReferrer(diff, which, objectNumber, k, parentPtr, buf, length);
10869
+ if (written <= 0) return "";
10870
+ const bytes = mem.readBytes(buf, written - 1);
10871
+ let text = "";
10872
+ for (const b of bytes) text += String.fromCharCode(b);
10873
+ return text;
10874
+ });
10875
+ out.push({
10876
+ parent: Number(mem.peek(parentPtr, "i32")) >>> 0,
10877
+ label
10878
+ });
10879
+ }
10880
+ });
10881
+ return out;
10882
+ }
10883
+ };
10884
+ //#endregion
10885
+ //#region src/features/signature/CandidateFinalizer.ts
10886
+ /**
10887
+ * The session-less half of a durable signing. A server prepares a
10888
+ * candidate on one replica, keeps only its tail, and may complete it on
10889
+ * another: the completing replica rebuilds base ⊕ tail into a private
10890
+ * file and hands it here with what the prepare reported. The CMS is
10891
+ * hex-encoded into the /Contents hole in place — never more than the
10892
+ * hole itself is held in memory — and the file is then opened like any
10893
+ * other base and read back through the signature model, so the version
10894
+ * returned is the file's own hash and the signature is what the file
10895
+ * says, not what the writer meant. Nothing here touches a live session.
10896
+ */
10897
+ var CandidateFinalizer = class {
10898
+ constructor(runtime, baseDocuments) {
10899
+ this.runtime = runtime;
10900
+ this.baseDocuments = baseDocuments;
10901
+ }
10902
+ finalize(input) {
10903
+ const [r0, r1, r2, r3] = input.byteRange;
10904
+ const size = this.runtime.fileAccess.sizeOf(input.path);
10905
+ const isOffset = (n) => Number.isSafeInteger(n) && n >= 0;
10906
+ if (!input.byteRange.every(isOffset) || r0 !== 0 || r1 >= r2 || r2 + r3 !== size) throw new EngineError(EngineErrorCode.InvalidArg, `byteRange [${input.byteRange.join(" ")}] does not span a ${size}-byte candidate`);
10907
+ const hexLength = r2 - r1 - 2;
10908
+ if (!Number.isSafeInteger(input.contentsSize) || hexLength !== 2 * input.contentsSize) throw new EngineError(EngineErrorCode.InvalidArg, `the /Contents hole holds ${hexLength} hex digits, not the ${2 * input.contentsSize} of contentsSize`);
10909
+ if (input.cms.byteLength === 0 || input.cms[0] !== 48) throw new EngineError(EngineErrorCode.SignatureRefused, "the CMS is not a DER SEQUENCE");
10910
+ if (input.cms.byteLength > input.contentsSize) throw new EngineError(EngineErrorCode.SignatureRefused, `the CMS (${input.cms.byteLength} bytes) does not fit the reserved ${input.contentsSize} bytes`);
10911
+ writeContentsIntoFile(this.runtime, input.path, {
10912
+ holeOffset: r1,
10913
+ hexLength
10914
+ }, input.cms);
10915
+ const session = new DocumentSession(this.runtime);
10916
+ const base = this.baseDocuments.acquireFileBase({
10917
+ key: `finalize:${generateUuid()}`,
10918
+ path: input.path,
10919
+ password: input.password
10920
+ });
8395
10921
  try {
8396
- if (!this.runtime.fn.FPDF_SaveAsCopy(this.session.requireDocPtr(), writer.ptr, pdfSaveModeFlags(mode))) throw new EngineError(EngineErrorCode.DocOpenFailed, "failed to save document");
8397
- return { path };
10922
+ session.openFromHandle(openLayerDocument(this.runtime, base, { kind: "fresh" }, input.password));
10923
+ const model = acquireSignatureModel(this.runtime, session);
10924
+ assertSealedSignature(this.runtime, session.requireDocPtr(), model, {
10925
+ fieldObjectNumber: input.fieldObjectNumber,
10926
+ byteRange: input.byteRange,
10927
+ cms: input.cms
10928
+ });
10929
+ const reader = new SignatureReader(this.runtime, session);
10930
+ const snapshot = reader.readSnapshot();
10931
+ return {
10932
+ signature: snapshot.signatures.find((s) => s.field.kind === "objectNumber" && s.field.fieldObjectNumber === input.fieldObjectNumber),
10933
+ protection: snapshot.protection,
10934
+ version: reader.version()
10935
+ };
8398
10936
  } finally {
8399
- writer.close();
10937
+ session.close();
8400
10938
  }
8401
10939
  }
8402
- requireLayer() {
8403
- if (this.session.kind !== "layer") throw new EngineError(EngineErrorCode.InvalidArg, "document session is not a layer");
8404
- }
8405
10940
  };
8406
10941
  //#endregion
8407
10942
  //#region src/features/fonts/FontRegistrar.ts
10943
+ /** `EPDF_FONT_EMBEDDING_*` codes. Restricted (3) and bitmap-only (4) are
10944
+ * refused at registration, so they never reach a handle. */
10945
+ function permissionFromCode(code) {
10946
+ switch (code) {
10947
+ case 1: return "editable";
10948
+ case 2: return "preview-and-print";
10949
+ default: return "installable";
10950
+ }
10951
+ }
10952
+ /** The engine's family comparison: no case, spaces, hyphens, underscores. */
10953
+ function familyKey(family) {
10954
+ return family.toLowerCase().replace(/[\s\-_'"]/g, "");
10955
+ }
8408
10956
  /**
8409
10957
  * Thread-confined runtime font registry binding.
8410
10958
  *
@@ -8428,6 +10976,7 @@ var FontRegistrar = class {
8428
10976
  this.runtime = runtime;
8429
10977
  this.ids = ids;
8430
10978
  _defineProperty(this, "fileHandles", []);
10979
+ _defineProperty(this, "identities", /* @__PURE__ */ new Map());
8431
10980
  }
8432
10981
  /**
8433
10982
  * Register a font from its bytes. Copies into the WASM heap, hands the
@@ -8446,6 +10995,11 @@ var FontRegistrar = class {
8446
10995
  const id = fn.EPDFFont_RegisterMemFont64(familyName, weight, italic, ptr, bytes.byteLength);
8447
10996
  if (id === 0) throw new EngineError(EngineErrorCode.InvalidArg, `font registration failed (unloadable or no glyphs): ${fontKey}`);
8448
10997
  this.ids.set(fontKey, id);
10998
+ this.identities.set(fontKey, this.readIdentity(id, {
10999
+ familyName,
11000
+ weight,
11001
+ italic
11002
+ }));
8449
11003
  } finally {
8450
11004
  mem.free(ptr);
8451
11005
  }
@@ -8471,8 +11025,72 @@ var FontRegistrar = class {
8471
11025
  throw new EngineError(EngineErrorCode.InvalidArg, `font registration failed (unloadable or no glyphs): ${fontKey} (${path})`);
8472
11026
  }
8473
11027
  this.ids.set(fontKey, id);
11028
+ this.identities.set(fontKey, this.readIdentity(id, {
11029
+ familyName,
11030
+ weight,
11031
+ italic
11032
+ }));
8474
11033
  this.fileHandles.push(access);
8475
11034
  }
11035
+ /** The identity and licence the runtime resolved for a key. Throws if unknown. */
11036
+ describe(fontKey) {
11037
+ const identity = this.identities.get(fontKey);
11038
+ if (!identity) throw new EngineError(EngineErrorCode.InvalidArg, `font not registered on this thread: ${fontKey}`);
11039
+ return identity;
11040
+ }
11041
+ /** {@link describe} for callers that treat an unknown key as "not a key". */
11042
+ describeOrUndefined(fontKey) {
11043
+ return this.identities.get(fontKey);
11044
+ }
11045
+ /**
11046
+ * The key of the registered font whose identity matches a face (family
11047
+ * compared the engine's way; the closest weight, italic first), or
11048
+ * undefined when no registered family matches.
11049
+ */
11050
+ keyForFace(family, weight, italic) {
11051
+ const wanted = familyKey(family);
11052
+ let best;
11053
+ for (const [key, identity] of this.identities) {
11054
+ if (familyKey(identity.familyName) !== wanted) continue;
11055
+ const score = Math.abs(identity.weight - weight) + (identity.italic === italic ? 0 : 1e3);
11056
+ if (!best || score < best.score) best = {
11057
+ key,
11058
+ score
11059
+ };
11060
+ }
11061
+ return best?.key;
11062
+ }
11063
+ /** The application asserts a licence permitting editing with the font. */
11064
+ authorizeEditing(fontKey) {
11065
+ const id = this.requireId(fontKey);
11066
+ if (!this.runtime.fn.EPDFFont_AuthorizeEditing(id)) throw new EngineError(EngineErrorCode.InvalidArg, `authorizeEditing failed: ${fontKey}`);
11067
+ const previous = this.describe(fontKey);
11068
+ const identity = this.readIdentity(id, {
11069
+ familyName: previous.familyName,
11070
+ weight: previous.weight,
11071
+ italic: previous.italic ? 1 : 0
11072
+ });
11073
+ this.identities.set(fontKey, identity);
11074
+ return identity;
11075
+ }
11076
+ readIdentity(id, given) {
11077
+ const { fn, mem } = this.runtime;
11078
+ const attempt = (read, fallback) => {
11079
+ try {
11080
+ return read();
11081
+ } catch {
11082
+ return fallback;
11083
+ }
11084
+ };
11085
+ return {
11086
+ familyName: attempt(() => readUtf8String(mem, (buf, cap) => fn.EPDFFont_GetFamilyName(id, buf, cap)) ?? given.familyName, given.familyName),
11087
+ weight: attempt(() => fn.EPDFFont_GetWeight(id), given.weight || 400),
11088
+ italic: attempt(() => fn.EPDFFont_IsItalic(id), given.italic === 1),
11089
+ embeddingPermission: attempt(() => permissionFromCode(fn.EPDFFont_GetEmbeddingPermission(id)), "installable"),
11090
+ editingAuthorized: attempt(() => fn.EPDFFont_IsEditingAuthorized(id), true),
11091
+ instanced: attempt(() => fn.EPDFFont_IsInstanced(id), false)
11092
+ };
11093
+ }
8476
11094
  /**
8477
11095
  * Register host-owned startup fonts on this thread, in order, optionally
8478
11096
  * adding each to the fallback chain. Order matters: it fixes both the native
@@ -8498,6 +11116,7 @@ var FontRegistrar = class {
8498
11116
  clear() {
8499
11117
  this.runtime.fn.EPDFFont_ClearRegisteredFonts();
8500
11118
  this.ids.clear();
11119
+ this.identities.clear();
8501
11120
  for (const access of this.fileHandles) access.close();
8502
11121
  this.fileHandles.length = 0;
8503
11122
  }
@@ -8650,35 +11269,14 @@ function flagMasks(flags) {
8650
11269
  };
8651
11270
  }
8652
11271
  //#endregion
8653
- //#region src/features/forms/internal/wideStringArray.ts
8654
- /**
8655
- * Marshal a `const FPDF_WIDESTRING*` array: encode every string into the
8656
- * runtime heap, write the pointer table with the runtime's pointer width
8657
- * (bigint pointers cannot be poked into the wasm 32-bit heap), run |body|,
8658
- * free everything.
8659
- */
8660
- function withWideStringArray(runtime, values, body) {
8661
- const { mem } = runtime;
8662
- const stringPtrs = values.map((value) => mem.writeU16String(value));
8663
- const isWasm = runtime.kind === "wasm";
8664
- const ptrSize = isWasm ? 4 : 8;
8665
- try {
8666
- return withScratch(mem, Math.max(1, stringPtrs.length * ptrSize), (arrayPtr) => {
8667
- stringPtrs.forEach((ptr, i) => isWasm ? mem.poke(arrayPtr, "i32", Number(ptr), i * 4) : mem.poke(arrayPtr, "i64", ptr, i * 8));
8668
- return body(arrayPtr, stringPtrs.length);
8669
- });
8670
- } finally {
8671
- for (let i = stringPtrs.length - 1; i >= 0; i--) mem.free(stringPtrs[i]);
8672
- }
8673
- }
8674
- //#endregion
8675
11272
  //#region src/features/forms/FormMutator.ts
8676
11273
  const FAMILY_CODE = {
8677
11274
  checkbox: 2,
8678
11275
  radio: 3,
8679
11276
  text: 4,
8680
11277
  combobox: 5,
8681
- listbox: 6
11278
+ listbox: 6,
11279
+ signature: 7
8682
11280
  };
8683
11281
  /** Widgets a single value write can touch; far above any real form. */
8684
11282
  const CHANGED_WIDGETS_CAPACITY$1 = 256;
@@ -8711,7 +11309,7 @@ var FormMutator = class {
8711
11309
  throwIfAborted(signal);
8712
11310
  const model = acquireFormModel(this.runtime, this.session);
8713
11311
  const resolved = resolveFieldRef(this.runtime, model, ref);
8714
- this.assertWritable(resolved.fieldObjectNumber);
11312
+ this.assertWritable(resolved);
8715
11313
  const before = readFieldAt(this.runtime, model, resolved.fieldIndex, this.session.requireDocPtr());
8716
11314
  if (!FAMILY_BY_VALUE_TYPE[value.type].includes(before.family)) throw new EngineError(EngineErrorCode.InvalidArg, `value type '${value.type}' does not apply to a '${before.family}' field`);
8717
11315
  const changed = this.applyWrite(resolved.fieldObjectNumber, value);
@@ -8721,7 +11319,7 @@ var FormMutator = class {
8721
11319
  throwIfAborted(signal);
8722
11320
  const model = acquireFormModel(this.runtime, this.session);
8723
11321
  const resolved = resolveFieldRef(this.runtime, model, ref);
8724
- this.assertWritable(resolved.fieldObjectNumber);
11322
+ this.assertWritable(resolved);
8725
11323
  const changed = this.withChangedWidgets((buf, cap, countPtr) => this.runtime.fn.EPDFForm_ResetField(this.session.requireDocPtr(), resolved.fieldObjectNumber, buf, cap, countPtr));
8726
11324
  if (changed === null) throw new EngineError(EngineErrorCode.InvalidArg, "form field cannot be reset");
8727
11325
  return this.readBack(resolved.fieldObjectNumber, changed);
@@ -8814,13 +11412,39 @@ var FormMutator = class {
8814
11412
  this.session.noteMutation();
8815
11413
  return { field: this.readBackField(fieldObjectNumber) };
8816
11414
  }
11415
+ /**
11416
+ * Draw a PDF page into every widget of an UNSIGNED signature field: the
11417
+ * visual "sign" of a viewer without a signer. The field's value stays
11418
+ * empty and nothing is sealed; a signed field is refused (its appearance
11419
+ * is part of what the signature covers). Pages whose widgets changed are
11420
+ * reported so their renders re-pin.
11421
+ */
11422
+ setSignatureAppearance(ref, pdf, pageIndex, signal) {
11423
+ throwIfAborted(signal);
11424
+ const docPtr = this.session.requireDocPtr();
11425
+ const model = acquireFormModel(this.runtime, this.session);
11426
+ const resolved = resolveFieldRef(this.runtime, model, ref);
11427
+ this.assertWritable(resolved);
11428
+ const before = readFieldAt(this.runtime, model, resolved.fieldIndex, docPtr);
11429
+ if (before.family !== "signature") throw new EngineError(EngineErrorCode.InvalidArg, `'${before.name}' is a ${before.family} field, not a signature field`);
11430
+ if (withSignatureModel(this.runtime, docPtr, (signatures) => readSignaturesFromModel(this.runtime, signatures).some((s) => s.signed && s.field.kind === "objectNumber" && s.field.fieldObjectNumber === resolved.fieldObjectNumber))) throw new EngineError(EngineErrorCode.InvalidArg, `'${before.name}' is signed; its appearance is sealed with the signature`);
11431
+ if (before.widgets.length === 0) throw new EngineError(EngineErrorCode.InvalidArg, `'${before.name}' has no widget to draw into`);
11432
+ for (const widget of before.widgets) bakeWidgetAppearance(this.runtime, docPtr, widget, pdf, pageIndex);
11433
+ this.session.noteMutation();
11434
+ const pages = [...new Set(before.widgets.map((w) => w.pageObjectNumber))];
11435
+ for (const pon of pages) this.session.bumpRevision(pon);
11436
+ return {
11437
+ field: this.readBackField(resolved.fieldObjectNumber),
11438
+ pages
11439
+ };
11440
+ }
8817
11441
  updateField(ref, patch, signal) {
8818
11442
  throwIfAborted(signal);
8819
11443
  const { fn } = this.runtime;
8820
11444
  const docPtr = this.session.requireDocPtr();
8821
11445
  const model = acquireFormModel(this.runtime, this.session);
8822
11446
  const resolved = resolveFieldRef(this.runtime, model, ref);
8823
- this.assertWritable(resolved.fieldObjectNumber);
11447
+ this.assertWritable(resolved);
8824
11448
  const before = readFieldAt(this.runtime, model, resolved.fieldIndex, this.session.requireDocPtr());
8825
11449
  if (before.family !== patch.family) throw new EngineError(EngineErrorCode.InvalidArg, `patch family '${patch.family}' does not match field family '${before.family}'`);
8826
11450
  const fieldObjectNumber = resolved.fieldObjectNumber;
@@ -8851,7 +11475,7 @@ var FormMutator = class {
8851
11475
  const { fn, mem } = this.runtime;
8852
11476
  const model = acquireFormModel(this.runtime, this.session);
8853
11477
  const resolved = resolveFieldRef(this.runtime, model, ref);
8854
- this.assertWritable(resolved.fieldObjectNumber);
11478
+ this.assertWritable(resolved);
8855
11479
  const before = readFieldAt(this.runtime, model, resolved.fieldIndex, this.session.requireDocPtr());
8856
11480
  if (!withScratchN(mem, [256 * 4, 4], ([buf, countPtr]) => {
8857
11481
  mem.poke(countPtr, "i32", 0);
@@ -8871,7 +11495,7 @@ var FormMutator = class {
8871
11495
  const { fn } = this.runtime;
8872
11496
  const model = acquireFormModel(this.runtime, this.session);
8873
11497
  const resolved = resolveFieldRef(this.runtime, model, ref);
8874
- this.assertWritable(resolved.fieldObjectNumber);
11498
+ this.assertWritable(resolved);
8875
11499
  const before = readFieldAt(this.runtime, model, resolved.fieldIndex, this.session.requireDocPtr());
8876
11500
  const toggle = before.family === "checkbox" || before.family === "radio";
8877
11501
  const state = toggle ? onState ?? (before.family === "checkbox" ? "Yes" : "") : "";
@@ -8885,7 +11509,7 @@ var FormMutator = class {
8885
11509
  const { fn } = this.runtime;
8886
11510
  const model = acquireFormModel(this.runtime, this.session);
8887
11511
  const resolved = resolveFieldRef(this.runtime, model, ref);
8888
- this.assertWritable(resolved.fieldObjectNumber);
11512
+ this.assertWritable(resolved);
8889
11513
  if (!fn.EPDFForm_DetachWidget(this.session.requireDocPtr(), resolved.fieldObjectNumber, widget.annotObjectNumber)) throw new EngineError(EngineErrorCode.InvalidArg, "widget is not attached to this field");
8890
11514
  this.session.noteMutation();
8891
11515
  return { field: this.readBackField(resolved.fieldObjectNumber) };
@@ -8920,8 +11544,30 @@ var FormMutator = class {
8920
11544
  if (fieldIndex < 0) throw new EngineError(EngineErrorCode.Unknown, "form field vanished after write");
8921
11545
  return readFieldAt(this.runtime, fresh, fieldIndex, this.session.requireDocPtr());
8922
11546
  }
8923
- assertWritable(fieldObjectNumber) {
8924
- if (fieldObjectNumber === 0) throw new EngineError(EngineErrorCode.InvalidArg, "form field is stored as a direct object and cannot be written");
11547
+ assertWritable(resolved) {
11548
+ if (resolved.fieldObjectNumber === 0) throw new EngineError(EngineErrorCode.InvalidArg, "form field is stored as a direct object and cannot be written");
11549
+ this.assertNotLockedBySignature(resolved);
11550
+ }
11551
+ /**
11552
+ * A field an earlier signature froze (its FieldMDP, or the /Lock of a
11553
+ * signed field) refuses every write: document-derived authority, the
11554
+ * same way encryption bits are. Off under `signedDocumentPolicy:
11555
+ * 'permit'`. A document whose signature model cannot be built is not
11556
+ * known to be locked.
11557
+ */
11558
+ assertNotLockedBySignature(resolved) {
11559
+ if (this.session.signedDocumentPolicy !== "protect") return;
11560
+ let protection;
11561
+ try {
11562
+ protection = new SignatureReader(this.runtime, this.session).readProtection();
11563
+ } catch {
11564
+ return;
11565
+ }
11566
+ if (protection.fieldLocks.length === 0) return;
11567
+ const model = acquireFormModel(this.runtime, this.session);
11568
+ const name = readUtf16String(this.runtime.mem, (buf, cap) => this.runtime.fn.EPDFForm_GetFieldName(model, resolved.fieldIndex, buf, cap)) ?? "";
11569
+ const lock = fieldLockFor(protection, name);
11570
+ if (lock) throw new EngineError(EngineErrorCode.ProtectedDocument, `form field "${name}" is locked by signature ${lock.signatureIndex} (${lock.source === "fieldmdp" ? "FieldMDP" : "/Lock"})`);
8925
11571
  }
8926
11572
  /** Dispatch the typed native write. Returns the changed widget objnums. */
8927
11573
  applyWrite(fieldObjectNumber, value) {
@@ -9768,6 +12414,7 @@ var WorkerHost = class {
9768
12414
  this.aborts.set(msg.jobId, ctrl);
9769
12415
  let resultPack;
9770
12416
  try {
12417
+ this.assertNoPendingSigning(msg);
9771
12418
  switch (msg.kind) {
9772
12419
  case "open.fatMem":
9773
12420
  case "open.layerMemBase":
@@ -9807,6 +12454,36 @@ var WorkerHost = class {
9807
12454
  case "annotations.move":
9808
12455
  resultPack = this.handleAnnotationsMove(msg, ctrl.signal);
9809
12456
  break;
12457
+ case "signatures.list":
12458
+ resultPack = this.handleSignaturesList(msg);
12459
+ break;
12460
+ case "signatures.contents":
12461
+ resultPack = this.handleSignaturesContents(msg);
12462
+ break;
12463
+ case "signatures.digest":
12464
+ resultPack = this.handleSignaturesDigest(msg);
12465
+ break;
12466
+ case "signatures.revisionBytes":
12467
+ resultPack = this.handleSignaturesRevisionBytes(msg);
12468
+ break;
12469
+ case "document.version":
12470
+ resultPack = this.handleDocumentVersion(msg);
12471
+ break;
12472
+ case "signatures.prepare":
12473
+ resultPack = this.handleSignaturesPrepare(msg);
12474
+ break;
12475
+ case "signatures.complete":
12476
+ resultPack = this.handleSignaturesComplete(msg);
12477
+ break;
12478
+ case "signatures.abort":
12479
+ resultPack = this.handleSignaturesAbort(msg);
12480
+ break;
12481
+ case "signatures.analyze":
12482
+ resultPack = this.handleSignaturesAnalyze(msg);
12483
+ break;
12484
+ case "signatures.finalizeCandidate":
12485
+ resultPack = this.handleSignaturesFinalizeCandidate(msg);
12486
+ break;
9810
12487
  case "forms.list":
9811
12488
  resultPack = this.handleFormsList(msg, ctrl.signal);
9812
12489
  break;
@@ -9834,6 +12511,9 @@ var WorkerHost = class {
9834
12511
  case "forms.updateField":
9835
12512
  resultPack = this.handleFormsUpdateField(msg, ctrl.signal);
9836
12513
  break;
12514
+ case "forms.setSignatureAppearance":
12515
+ resultPack = this.handleFormsSetSignatureAppearance(msg, ctrl.signal);
12516
+ break;
9837
12517
  case "forms.deleteField":
9838
12518
  resultPack = this.handleFormsDeleteField(msg, ctrl.signal);
9839
12519
  break;
@@ -9951,6 +12631,12 @@ var WorkerHost = class {
9951
12631
  case "fonts.clear":
9952
12632
  resultPack = this.handleFontsClear(msg);
9953
12633
  break;
12634
+ case "fonts.authorizeEditing":
12635
+ resultPack = this.handleFontsAuthorizeEditing(msg);
12636
+ break;
12637
+ case "document.setFontSettings":
12638
+ resultPack = this.handleDocumentSetFontSettings(msg);
12639
+ break;
9954
12640
  case "layer.close":
9955
12641
  resultPack = this.handleLayerClose(msg);
9956
12642
  break;
@@ -9982,10 +12668,13 @@ var WorkerHost = class {
9982
12668
  const key = sessionKey(req.docId, req.kind === "open.fatMem" ? void 0 : req.layerName);
9983
12669
  if (this.sessions.has(key)) throw new EngineError(EngineErrorCode.InvalidArg, `document session already open: ${key}`);
9984
12670
  const session = new DocumentSession(this.runtime);
12671
+ session.signedDocumentPolicy = req.signedDocumentPolicy ?? "protect";
12672
+ session.password = req.password;
9985
12673
  if (req.kind === "open.fatMem") {
12674
+ session.sessionKind = req.sessionKind ?? "layer";
9986
12675
  const bytes = new Uint8Array(req.bytes);
9987
12676
  try {
9988
- session.openFromHandle(openFatMemoryDocument(this.runtime, bytes, req.password));
12677
+ this.openSignedAware(session, bytes, req.password);
9989
12678
  } catch (error) {
9990
12679
  if (!isPasswordOpenError(error)) throw error;
9991
12680
  session.parkLocked(bytes);
@@ -10000,14 +12689,16 @@ var WorkerHost = class {
10000
12689
  const base = this.baseDocuments.acquireMemoryBase({
10001
12690
  key: req.baseKey,
10002
12691
  bytes: new Uint8Array(req.baseBytes),
10003
- password: req.password
12692
+ password: req.password,
12693
+ knownSha256: req.baseSha256
10004
12694
  });
10005
12695
  session.openFromHandle(openLayerDocument(this.runtime, base, req.layer, req.password));
10006
12696
  } else {
10007
12697
  const base = this.baseDocuments.acquireFileBase({
10008
12698
  key: req.baseKey,
10009
12699
  path: req.basePath,
10010
- password: req.password
12700
+ password: req.password,
12701
+ knownSha256: req.baseSha256
10011
12702
  });
10012
12703
  session.openFromHandle(openLayerDocument(this.runtime, base, req.layer, req.password));
10013
12704
  }
@@ -10015,9 +12706,160 @@ var WorkerHost = class {
10015
12706
  return wirePack({
10016
12707
  tag: "open",
10017
12708
  docId: req.docId,
10018
- security: new SecurityReader(this.runtime).checkPasswordPermissions(session, req.password ?? "")
12709
+ security: new SecurityReader(this.runtime).checkPasswordPermissions(session, req.password ?? ""),
12710
+ protection: this.probeProtection(session)
12711
+ });
12712
+ }
12713
+ /**
12714
+ * Open plain bytes into `session` in the shape it asked for. The default,
12715
+ * `layer`, makes the bytes an immutable base with a fresh layer on top:
12716
+ * reads fall through to the base, a write promotes only what it touches,
12717
+ * a save appends exactly that, and a signature keeps its validity across
12718
+ * later edits because the signature dictionary is never rewritten. The
12719
+ * `plain` shape keeps one in-memory document — unless the bytes already
12720
+ * carry a signature value, which always opens as a layer, because a plain
12721
+ * save rewrites every loaded object into the new revision and strict
12722
+ * validators reject that. Auto-layered sessions serialize no artifact per
12723
+ * mutation: the caller never asked for one.
12724
+ */
12725
+ openSignedAware(session, bytes, password) {
12726
+ if (session.sessionKind === "plain") {
12727
+ const plain = openFatMemoryDocument(this.runtime, bytes, password);
12728
+ let signed = false;
12729
+ try {
12730
+ signed = hasSignedSignature(this.runtime, plain.docPtr);
12731
+ } catch {
12732
+ signed = false;
12733
+ }
12734
+ if (!signed) {
12735
+ session.openFromHandle(plain);
12736
+ return;
12737
+ }
12738
+ plain.close();
12739
+ }
12740
+ const base = this.baseDocuments.acquireMemoryBase({
12741
+ key: `signed-open:${generateUuid()}`,
12742
+ bytes,
12743
+ password
12744
+ });
12745
+ session.openFromHandle(openLayerDocument(this.runtime, base, { kind: "fresh" }, password));
12746
+ session.persistLayerArtifact = false;
12747
+ }
12748
+ /**
12749
+ * What the document's signatures forbid, read at open and after an
12750
+ * unlock so the main-thread guard can subtract capabilities before the
12751
+ * first mutation. A document whose signature model cannot be built
12752
+ * still opens: protection is then `null` (nothing is known to be
12753
+ * signed), and the reads report the failure on their own.
12754
+ */
12755
+ probeProtection(session) {
12756
+ try {
12757
+ return new SignatureReader(this.runtime, session).readProtection();
12758
+ } catch {
12759
+ return null;
12760
+ }
12761
+ }
12762
+ handleSignaturesList(req) {
12763
+ const session = this.requireSession(req);
12764
+ return wirePack({
12765
+ tag: "signatures.list",
12766
+ snapshot: req.workingCopy ? new SignatureAnalyzer(this.runtime, session).readWorkingCopySnapshot() : new SignatureReader(this.runtime, session).readSnapshot()
12767
+ });
12768
+ }
12769
+ handleSignaturesContents(req) {
12770
+ const session = this.requireSession(req);
12771
+ const bytes = new SignatureReader(this.runtime, session).readContents(req.ref);
12772
+ return wirePack({
12773
+ tag: "signatures.contents",
12774
+ bytes
12775
+ }, [bytes]);
12776
+ }
12777
+ handleSignaturesDigest(req) {
12778
+ const session = this.requireSession(req);
12779
+ const digest = new SignatureReader(this.runtime, session).digest(req.ref, req.algorithm);
12780
+ return wirePack({
12781
+ tag: "signatures.digest",
12782
+ digest
12783
+ }, [digest]);
12784
+ }
12785
+ handleSignaturesRevisionBytes(req) {
12786
+ const session = this.requireSession(req);
12787
+ const bytes = new SignatureReader(this.runtime, session).revisionBytes(req.revisionIndex);
12788
+ return wirePack({
12789
+ tag: "signatures.revisionBytes",
12790
+ bytes,
12791
+ size: bytes.byteLength
12792
+ }, [bytes]);
12793
+ }
12794
+ handleDocumentVersion(req) {
12795
+ const session = this.requireSession(req);
12796
+ return wirePack({
12797
+ tag: "document.version",
12798
+ version: new SignatureReader(this.runtime, session).version()
12799
+ });
12800
+ }
12801
+ handleSignaturesPrepare(req) {
12802
+ const session = this.requireSession(req);
12803
+ return wirePack({
12804
+ tag: "signatures.prepare",
12805
+ result: new SignatureMutator(this.runtime, session, this.baseDocuments, this.options.signingCandidatePath).prepare(req.input)
12806
+ });
12807
+ }
12808
+ handleSignaturesComplete(req) {
12809
+ const session = this.requireSession(req);
12810
+ const result = new SignatureMutator(this.runtime, session, this.baseDocuments, this.options.signingCandidatePath).complete(req.input);
12811
+ if (result.status === "already-completed") return wirePack({
12812
+ tag: "signatures.complete",
12813
+ result
12814
+ });
12815
+ return this.finishMutation(session, {
12816
+ tag: "signatures.complete",
12817
+ result
12818
+ }, req.artifactPath);
12819
+ }
12820
+ handleSignaturesAnalyze(req) {
12821
+ const session = this.requireSession(req);
12822
+ return wirePack({
12823
+ tag: "signatures.analyze",
12824
+ analysis: new SignatureAnalyzer(this.runtime, session).analyze(req.input)
12825
+ });
12826
+ }
12827
+ /**
12828
+ * Session-less, like `document.renderPageFile`: the candidate file is
12829
+ * the caller's, patched in place and read back through a transient
12830
+ * session that is never stored in `this.sessions`.
12831
+ */
12832
+ handleSignaturesFinalizeCandidate(req) {
12833
+ return wirePack({
12834
+ tag: "signatures.finalizeCandidate",
12835
+ ...new CandidateFinalizer(this.runtime, this.baseDocuments).finalize({
12836
+ path: req.path,
12837
+ byteRange: req.byteRange,
12838
+ contentsSize: req.contentsSize,
12839
+ fieldObjectNumber: req.fieldObjectNumber,
12840
+ cms: new Uint8Array(req.cms),
12841
+ password: req.password ?? null
12842
+ })
12843
+ });
12844
+ }
12845
+ handleSignaturesAbort(req) {
12846
+ const session = this.requireSession(req);
12847
+ return wirePack({
12848
+ tag: "signatures.abort",
12849
+ result: new SignatureMutator(this.runtime, session, this.baseDocuments, this.options.signingCandidatePath).abort(req.signingId)
10019
12850
  });
10020
12851
  }
12852
+ /**
12853
+ * The dispatch fence for a parked signing candidate. Only mutating kinds
12854
+ * are refused; a request for a session that is not open falls through
12855
+ * to its handler's own `DocNotOpen`.
12856
+ */
12857
+ assertNoPendingSigning(msg) {
12858
+ if (!MUTATING_KINDS.has(msg.kind) || !("docId" in msg)) return;
12859
+ const layerName = "layerName" in msg ? msg.layerName : void 0;
12860
+ const session = this.sessions.get(sessionKey(msg.docId, layerName));
12861
+ if (session?.pendingSigning) throw new EngineError(EngineErrorCode.SigningPending, `a signing is pending (${session.pendingSigning.prepared.signingId}); complete or abort it before mutating the document`);
12862
+ }
10021
12863
  handleMetadataRead(req, signal) {
10022
12864
  const session = this.requireSession(req);
10023
12865
  return wirePack({
@@ -10044,21 +12886,21 @@ var WorkerHost = class {
10044
12886
  const session = this.requireSession(req);
10045
12887
  return wirePack({
10046
12888
  tag: "annotations.listRawAll",
10047
- snapshot: new RawAnnotationReader(this.runtime, session).listAll(signal)
12889
+ snapshot: new RawAnnotationReader(this.runtime, session, this.fonts).listAll(signal)
10048
12890
  });
10049
12891
  }
10050
12892
  handleAnnotationsListRawPage(req, signal) {
10051
12893
  const session = this.requireSession(req);
10052
12894
  return wirePack({
10053
12895
  tag: "annotations.listRawPage",
10054
- snapshot: new RawAnnotationReader(this.runtime, session).listOne(req.pageObjectNumber, signal)
12896
+ snapshot: new RawAnnotationReader(this.runtime, session, this.fonts).listOne(req.pageObjectNumber, signal)
10055
12897
  });
10056
12898
  }
10057
12899
  handleAnnotationsListFullPage(req, signal) {
10058
12900
  const session = this.requireSession(req);
10059
12901
  return wirePack({
10060
12902
  tag: "annotations.listFullPage",
10061
- snapshot: new AnnotationReader(this.runtime, session).list(req.pageObjectNumber, signal)
12903
+ snapshot: new AnnotationReader(this.runtime, session, this.fonts).list(req.pageObjectNumber, signal)
10062
12904
  });
10063
12905
  }
10064
12906
  handleAnnotationsRenderAppearances(req, signal) {
@@ -10419,6 +13261,14 @@ var WorkerHost = class {
10419
13261
  }
10420
13262
  handleDocumentSaveBuffer(req) {
10421
13263
  const session = this.requireSession(req);
13264
+ if (req.mode === "incremental") {
13265
+ const snap = new DocumentSaver(this.runtime, session).snapshot();
13266
+ return wirePack({
13267
+ tag: "document.saveBuffer",
13268
+ bytes: snap.bytes,
13269
+ size: snap.bytes.byteLength
13270
+ }, [snap.bytes]);
13271
+ }
10422
13272
  const saved = new DocumentSaver(this.runtime, session).saveStandaloneToBuffer(req.mode);
10423
13273
  return wirePack({
10424
13274
  tag: "document.saveBuffer",
@@ -10428,6 +13278,17 @@ var WorkerHost = class {
10428
13278
  }
10429
13279
  handleDocumentSaveFile(req) {
10430
13280
  const session = this.requireSession(req);
13281
+ if (req.mode === "incremental" && (!session.hasUnsavedEdits() || new DocumentSaver(this.runtime, session).saveStandaloneToFileEx(req.path, req.mode).unchangedSinceLoad)) {
13282
+ new DocumentSaver(this.runtime, session).copyLoadedBytesToFile(req.path);
13283
+ return wirePack({
13284
+ tag: "document.saveFile",
13285
+ path: req.path
13286
+ });
13287
+ }
13288
+ if (req.mode === "incremental") return wirePack({
13289
+ tag: "document.saveFile",
13290
+ path: req.path
13291
+ });
10431
13292
  return wirePack({
10432
13293
  tag: "document.saveFile",
10433
13294
  path: new DocumentSaver(this.runtime, session).saveStandaloneToFile(req.path, req.mode).path
@@ -10484,21 +13345,47 @@ var WorkerHost = class {
10484
13345
  const parked = this.sessions.get(sessionKey(req.docId));
10485
13346
  let session;
10486
13347
  if (parked?.isLocked()) {
10487
- parked.openFromHandle(openFatMemoryDocument(this.runtime, parked.lockedBytes(), req.password));
13348
+ this.openSignedAware(parked, parked.lockedBytes(), req.password);
13349
+ parked.password = req.password;
10488
13350
  session = parked;
10489
13351
  } else session = this.requireSession(req);
10490
13352
  return wirePack({
10491
13353
  tag: "document.checkPasswordPermissions",
10492
- security: new SecurityReader(this.runtime).checkPasswordPermissions(session, req.password, req.mode ?? "any")
13354
+ security: new SecurityReader(this.runtime).checkPasswordPermissions(session, req.password, req.mode ?? "any"),
13355
+ protection: this.probeProtection(session)
10493
13356
  });
10494
13357
  }
10495
13358
  handleFontsRegister(req) {
10496
13359
  this.fonts.register(req.fontKey, req.familyName, req.weight, req.italic, new Uint8Array(req.data));
10497
13360
  return wirePack({
10498
13361
  tag: "fonts.register",
10499
- fontKey: req.fontKey
13362
+ fontKey: req.fontKey,
13363
+ identity: this.fonts.describe(req.fontKey)
13364
+ });
13365
+ }
13366
+ handleFontsAuthorizeEditing(req) {
13367
+ return wirePack({
13368
+ tag: "fonts.authorizeEditing",
13369
+ identity: this.fonts.authorizeEditing(req.fontKey)
10500
13370
  });
10501
13371
  }
13372
+ /** Per-document font and text-layout settings (session state). */
13373
+ handleDocumentSetFontSettings(req) {
13374
+ const docPtr = this.requireSession(req).requireDocPtr();
13375
+ const { fn } = this.runtime;
13376
+ if (req.embeddingPolicy !== void 0) {
13377
+ const code = {
13378
+ default: 0,
13379
+ subset: 1,
13380
+ full: 2
13381
+ }[req.embeddingPolicy];
13382
+ if (!fn.EPDFDoc_SetFontEmbeddingPolicy(docPtr, code)) throw new EngineError(EngineErrorCode.InvalidArg, "EPDFDoc_SetFontEmbeddingPolicy failed");
13383
+ }
13384
+ if (req.typographicFeatures !== void 0) {
13385
+ if (!fn.EPDFDoc_SetTypographicFeatures(docPtr, req.typographicFeatures)) throw new EngineError(EngineErrorCode.InvalidArg, "EPDFDoc_SetTypographicFeatures failed");
13386
+ }
13387
+ return wirePack({ tag: "document.setFontSettings" });
13388
+ }
10502
13389
  handleFontsAddFallback(req) {
10503
13390
  this.fonts.addFallback(req.fontKey);
10504
13391
  return wirePack({ tag: "fonts.addFallback" });
@@ -10524,6 +13411,7 @@ var WorkerHost = class {
10524
13411
  const session = this.sessions.get(key);
10525
13412
  if (session) {
10526
13413
  disposeFormModel(this.runtime, session);
13414
+ disposeSignatureModel(this.runtime, session);
10527
13415
  session.close();
10528
13416
  this.sessions.delete(key);
10529
13417
  }
@@ -10533,6 +13421,7 @@ var WorkerHost = class {
10533
13421
  for (const [key, session] of Array.from(this.sessions.entries())) {
10534
13422
  if (!sessionKeyBelongsToDoc(key, req.docId)) continue;
10535
13423
  disposeFormModel(this.runtime, session);
13424
+ disposeSignatureModel(this.runtime, session);
10536
13425
  session.close();
10537
13426
  this.sessions.delete(key);
10538
13427
  }
@@ -10543,6 +13432,7 @@ var WorkerHost = class {
10543
13432
  this.destroyed = true;
10544
13433
  for (const session of this.sessions.values()) {
10545
13434
  disposeFormModel(this.runtime, session);
13435
+ disposeSignatureModel(this.runtime, session);
10546
13436
  session.close();
10547
13437
  }
10548
13438
  this.sessions.clear();
@@ -10648,6 +13538,21 @@ var WorkerHost = class {
10648
13538
  }
10649
13539
  }, req.artifactPath);
10650
13540
  }
13541
+ handleFormsSetSignatureAppearance(req, signal) {
13542
+ const session = this.requireSession(req);
13543
+ const { field, pages } = new FormMutator(this.runtime, session).setSignatureAppearance(req.ref, new Uint8Array(req.pdf), req.pageIndex, signal);
13544
+ const meta = {
13545
+ affectedPages: pages.map((pon) => session.pageState(pon)),
13546
+ cacheDelta: null
13547
+ };
13548
+ return this.finishMutation(session, {
13549
+ tag: "forms.setSignatureAppearance",
13550
+ result: {
13551
+ field,
13552
+ meta
13553
+ }
13554
+ }, req.artifactPath);
13555
+ }
10651
13556
  handleFormsDeleteField(req, signal) {
10652
13557
  const session = this.requireSession(req);
10653
13558
  const { deletedFieldObjectNumber, detachedWidgets } = new FormMutator(this.runtime, session).deleteField(req.ref, signal);
@@ -10693,8 +13598,8 @@ var WorkerHost = class {
10693
13598
  }, req.artifactPath);
10694
13599
  }
10695
13600
  finishMutation(session, payload, artifactPath) {
10696
- if (!payload.tag.startsWith("forms.") && payload.tag !== "pages.flatten" && payload.tag !== "redaction.apply") session.noteMutation();
10697
- if (session.kind !== "layer") return wirePack(payload);
13601
+ if (!payload.tag.startsWith("forms.") && payload.tag !== "pages.flatten" && payload.tag !== "redaction.apply" && payload.tag !== "signatures.complete") session.noteMutation();
13602
+ if (session.kind !== "layer" || !session.persistLayerArtifact) return wirePack(payload);
10698
13603
  const saved = this.saveLayerArtifact(session, artifactPath);
10699
13604
  return wirePack({
10700
13605
  ...payload,
@@ -10720,6 +13625,39 @@ const EMPTY_FORM_META = {
10720
13625
  cacheDelta: null
10721
13626
  };
10722
13627
  const BASE_SESSION_SUFFIX = "__base__";
13628
+ /** Every request kind that writes to a session's document. */
13629
+ const MUTATING_KINDS = /* @__PURE__ */ new Set([
13630
+ "metadata.update",
13631
+ "annotations.create",
13632
+ "annotations.update",
13633
+ "annotations.delete",
13634
+ "annotations.move",
13635
+ "annotations.flatten",
13636
+ "forms.setValue",
13637
+ "forms.reset",
13638
+ "forms.applyEffects",
13639
+ "forms.import",
13640
+ "forms.repair",
13641
+ "forms.createField",
13642
+ "forms.updateField",
13643
+ "forms.setSignatureAppearance",
13644
+ "forms.deleteField",
13645
+ "forms.attachWidget",
13646
+ "forms.detachWidget",
13647
+ "pages.move",
13648
+ "pages.rotate",
13649
+ "pages.delete",
13650
+ "pages.setName",
13651
+ "pages.removeName",
13652
+ "pages.flatten",
13653
+ "pages.insert",
13654
+ "pages.insertBlank",
13655
+ "redaction.apply",
13656
+ "attachments.create",
13657
+ "attachments.delete",
13658
+ "pieceInfo.update",
13659
+ "pieceInfo.clear"
13660
+ ]);
10723
13661
  function sessionKey(docId, layerName) {
10724
13662
  return `${docId}::${layerName ? `layer:${layerName}` : BASE_SESSION_SUFFIX}`;
10725
13663
  }
@@ -10748,6 +13686,6 @@ function passwordRequiredProbe() {
10748
13686
  };
10749
13687
  }
10750
13688
  //#endregion
10751
- export { ALL_STANDARD_SECURITY_PERMISSIONS, ActionReadBudgetTracker, AnnotationAppearanceReader, AnnotationFlattener, AnnotationMutator, AnnotationReader, AttachmentMutator, AttachmentReader, BaseDocumentRegistry, CloseStack, DocumentActionsReader, PagesMutator as DocumentPagesMutator, PagesMutator, DocumentSaver, DocumentSession, EPDF_CHAR_GEOMETRY_LAYOUT, EventHub, FontRegistrar, FormMutator, FormReader, FormsEffectsApplier, LocalRevisionAuthority, MetadataMutator, MetadataReader, PageGeometryReader, PagesReader as PageLayoutReader, PagesReader, PagePtrPool, PageRenderReader as PageRasterReader, PageRenderReader, PageTextReader, PagesExtractor, PagesFlattener, PagesInserter, RawAnnotationReader, RedactionApplier, SearchReader, SecurityReader, SessionEventPublisher, WorkerHost, buildRunsFromRawGlyphs, computeMutationImpact, destroyLibrary, disposeFormModel, ensureInitialized, formatPdfDate, generateUuid, hasAllStandardSecurityPermissions, normalizeU32, openFatMemoryDocument, openLayerDocument, pdfDateToIso, readActionModel, readAttachmentFileInfo, throwIfAborted, writeAttachmentFilePayload };
13689
+ export { ALL_STANDARD_SECURITY_PERMISSIONS, ActionReadBudgetTracker, AnnotationAppearanceReader, AnnotationFlattener, AnnotationMutator, AnnotationReader, AttachmentMutator, AttachmentReader, BaseDocumentRegistry, CandidateFinalizer, CloseStack, DIGEST_CODE, DocumentActionsReader, PagesMutator as DocumentPagesMutator, PagesMutator, DocumentSaver, DocumentSession, EPDF_CHAR_GEOMETRY_LAYOUT, EventHub, FontRegistrar, FormMutator, FormReader, FormsEffectsApplier, LocalRevisionAuthority, MetadataMutator, MetadataReader, PageGeometryReader, PagesReader as PageLayoutReader, PagesReader, PagePtrPool, PageRenderReader as PageRasterReader, PageRenderReader, PageTextReader, PagesExtractor, PagesFlattener, PagesInserter, RawAnnotationReader, RedactionApplier, SearchReader, SecurityReader, SessionEventPublisher, SignatureAnalyzer, SignatureMutator, SignatureReader, WorkerHost, acquireSignatureModel, buildRunsFromRawGlyphs, computeMutationImpact, destroyLibrary, disposeFormModel, disposeSignatureModel, ensureInitialized, formatPdfDate, generateUuid, hasAllStandardSecurityPermissions, hasSignedSignature, normalizeU32, openFatMemoryDocument, openLayerDocument, pdfDateToIso, readActionModel, readAttachmentFileInfo, readRevisions, readSignaturesFromModel, readStructure, throwIfAborted, writeAttachmentFilePayload };
10752
13690
 
10753
13691
  //# sourceMappingURL=index.js.map