@embedpdf/engines 1.1.1 → 1.2.1

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.
@@ -1,4 +1,4 @@
1
- import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfTrappedStatus, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, dateToPdfDate, quadToRect, rectToQuad, PdfStandardFont, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE, Rotation, AppearanceMode, Task, toIntRect, transformRect, buildUserToDeviceMatrix, PdfActionType, PdfZoomMode, MatchFlag } from "@embedpdf/models";
1
+ import { NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, ignore, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfStampFit, PdfTrappedStatus, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, dateToPdfDate, quadToRect, rectToQuad, PdfStandardFont, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE, Rotation, AppearanceMode, Task, toIntRect, transformRect, buildUserToDeviceMatrix, PdfActionType, PdfZoomMode, MatchFlag } from "@embedpdf/models";
2
2
  function readString(wasmModule, readChars, parseChars, defaultLength = 100) {
3
3
  let buffer = wasmModule.wasmExports.malloc(defaultLength);
4
4
  for (let i = 0; i < defaultLength; i++) {
@@ -2160,9 +2160,6 @@ class PdfiumEngine {
2160
2160
  * @private
2161
2161
  */
2162
2162
  addTextContent(page, pagePtr, annotationPtr, annotation) {
2163
- if (!this.setPageAnnoRect(page, pagePtr, annotationPtr, annotation.rect)) {
2164
- return false;
2165
- }
2166
2163
  if (!this.setAnnotString(annotationPtr, "Contents", annotation.contents ?? "")) {
2167
2164
  return false;
2168
2165
  }
@@ -2218,9 +2215,6 @@ class PdfiumEngine {
2218
2215
  if (!this.setAnnotString(annotationPtr, "Contents", annotation.contents ?? "")) {
2219
2216
  return false;
2220
2217
  }
2221
- if (!this.setPageAnnoRect(page, pagePtr, annotationPtr, annotation.rect)) {
2222
- return false;
2223
- }
2224
2218
  if (!this.setAnnotString(annotationPtr, "T", annotation.author || "")) {
2225
2219
  return false;
2226
2220
  }
@@ -2283,9 +2277,6 @@ class PdfiumEngine {
2283
2277
  if (!this.setBorderStyle(annotationPtr, PdfAnnotationBorderStyle.SOLID, annotation.strokeWidth)) {
2284
2278
  return false;
2285
2279
  }
2286
- if (!this.setPageAnnoRect(page, pagePtr, annotationPtr, annotation.rect)) {
2287
- return false;
2288
- }
2289
2280
  if (!this.setInkList(page, annotationPtr, annotation.inkList)) {
2290
2281
  return false;
2291
2282
  }
@@ -2325,9 +2316,6 @@ class PdfiumEngine {
2325
2316
  if (annotation.modified && !this.setAnnotationDate(annotationPtr, "M", annotation.modified)) {
2326
2317
  return false;
2327
2318
  }
2328
- if (!this.setPageAnnoRect(page, pagePtr, annotationPtr, annotation.rect)) {
2329
- return false;
2330
- }
2331
2319
  if (!this.setLinePoints(
2332
2320
  page,
2333
2321
  annotationPtr,
@@ -2402,9 +2390,6 @@ class PdfiumEngine {
2402
2390
  if (annotation.flags && !this.setAnnotationFlags(annotationPtr, annotation.flags)) {
2403
2391
  return false;
2404
2392
  }
2405
- if (!this.setPageAnnoRect(page, pagePtr, annotationPtr, annotation.rect)) {
2406
- return false;
2407
- }
2408
2393
  if (annotation.type === PdfAnnotationSubtype.POLYLINE && !this.setLineEndings(
2409
2394
  annotationPtr,
2410
2395
  ((_a = annotation.lineEndings) == null ? void 0 : _a.start) ?? PdfAnnotationLineEnding.None,
@@ -2470,9 +2455,6 @@ class PdfiumEngine {
2470
2455
  if (annotation.modified && !this.setAnnotationDate(annotationPtr, "M", annotation.modified)) {
2471
2456
  return false;
2472
2457
  }
2473
- if (!this.setPageAnnoRect(page, pagePtr, annotationPtr, annotation.rect)) {
2474
- return false;
2475
- }
2476
2458
  if (!this.setAnnotString(annotationPtr, "Contents", annotation.contents ?? "")) {
2477
2459
  return false;
2478
2460
  }
@@ -2524,13 +2506,13 @@ class PdfiumEngine {
2524
2506
  if (annotation.created && !this.setAnnotationDate(annotationPtr, "CreationDate", annotation.created)) {
2525
2507
  return false;
2526
2508
  }
2527
- if (annotation.flags && !this.setAnnotationFlags(annotationPtr, annotation.flags)) {
2509
+ if (annotation.custom && !this.setAnnotCustom(annotationPtr, annotation.custom)) {
2528
2510
  return false;
2529
2511
  }
2530
- if (annotation.modified && !this.setAnnotationDate(annotationPtr, "M", annotation.modified)) {
2512
+ if (annotation.flags && !this.setAnnotationFlags(annotationPtr, annotation.flags)) {
2531
2513
  return false;
2532
2514
  }
2533
- if (!this.setPageAnnoRect(page, pagePtr, annotationPtr, annotation.rect)) {
2515
+ if (annotation.modified && !this.setAnnotationDate(annotationPtr, "M", annotation.modified)) {
2534
2516
  return false;
2535
2517
  }
2536
2518
  if (!this.syncQuadPointsAnno(page, annotationPtr, annotation.segmentRects)) {
@@ -2576,6 +2558,12 @@ class PdfiumEngine {
2576
2558
  if (annotation.modified && !this.setAnnotationDate(annotationPtr, "M", annotation.modified)) {
2577
2559
  return false;
2578
2560
  }
2561
+ if (annotation.icon && !this.setAnnotationIcon(annotationPtr, annotation.icon)) {
2562
+ return false;
2563
+ }
2564
+ if (annotation.subject && !this.setAnnotString(annotationPtr, "Subj", annotation.subject)) {
2565
+ return false;
2566
+ }
2579
2567
  if (!this.setAnnotString(annotationPtr, "Contents", annotation.contents ?? "")) {
2580
2568
  return false;
2581
2569
  }
@@ -2583,14 +2571,12 @@ class PdfiumEngine {
2583
2571
  for (let i = this.pdfiumModule.FPDFAnnot_GetObjectCount(annotationPtr) - 1; i >= 0; i--) {
2584
2572
  this.pdfiumModule.FPDFAnnot_RemoveObject(annotationPtr, i);
2585
2573
  }
2586
- return this.addImageObject(
2587
- docPtr,
2588
- page,
2589
- pagePtr,
2590
- annotationPtr,
2591
- annotation.rect.origin,
2592
- imageData
2593
- );
2574
+ if (!this.addImageObject(docPtr, page, pagePtr, annotationPtr, annotation.rect, imageData)) {
2575
+ return false;
2576
+ }
2577
+ }
2578
+ if (!this.pdfiumModule.EPDFAnnot_UpdateAppearanceToRect(annotationPtr, PdfStampFit.Cover)) {
2579
+ return false;
2594
2580
  }
2595
2581
  return true;
2596
2582
  }
@@ -2606,7 +2592,7 @@ class PdfiumEngine {
2606
2592
  *
2607
2593
  * @private
2608
2594
  */
2609
- addImageObject(docPtr, page, pagePtr, annotationPtr, position, imageData) {
2595
+ addImageObject(docPtr, page, pagePtr, annotationPtr, rect, imageData) {
2610
2596
  const bytesPerPixel = 4;
2611
2597
  const pixelCount = imageData.width * imageData.height;
2612
2598
  const bitmapBufferPtr = this.memoryManager.malloc(bytesPerPixel * pixelCount);
@@ -2663,8 +2649,8 @@ class PdfiumEngine {
2663
2649
  }
2664
2650
  this.memoryManager.free(matrixPtr);
2665
2651
  const pagePos = this.convertDevicePointToPagePoint(page, {
2666
- x: position.x,
2667
- y: position.y + imageData.height
2652
+ x: rect.origin.x,
2653
+ y: rect.origin.y + imageData.height
2668
2654
  // shift down by the image height
2669
2655
  });
2670
2656
  this.pdfiumModule.FPDFPageObj_Transform(imageObjectPtr, 1, 0, 0, 1, pagePos.x, pagePos.y);
@@ -2700,6 +2686,28 @@ class PdfiumEngine {
2700
2686
  this.pdfiumModule.PDFiumExt_CloseFileWriter(writerPtr);
2701
2687
  return buffer;
2702
2688
  }
2689
+ /**
2690
+ * Read Catalog /Lang via EPDFCatalog_GetLanguage (UTF-16LE → JS string).
2691
+ * Returns:
2692
+ * null -> /Lang not present (getter returned 0) OR doc not open,
2693
+ * '' -> /Lang exists but is explicitly empty,
2694
+ * 'en', 'en-US', ... -> normal tag.
2695
+ *
2696
+ * Note: EPDFCatalog_GetLanguage lengths are BYTES (incl. trailing NUL).
2697
+ *
2698
+ * @private
2699
+ */
2700
+ readCatalogLanguage(docPtr) {
2701
+ const byteLen = this.pdfiumModule.EPDFCatalog_GetLanguage(docPtr, 0, 0) >>> 0;
2702
+ if (byteLen === 0) return null;
2703
+ if (byteLen === 2) return "";
2704
+ return readString(
2705
+ this.pdfiumModule.pdfium,
2706
+ (buffer, bufferLength) => this.pdfiumModule.EPDFCatalog_GetLanguage(docPtr, buffer, bufferLength),
2707
+ this.pdfiumModule.pdfium.UTF16ToString,
2708
+ byteLen
2709
+ );
2710
+ }
2703
2711
  /**
2704
2712
  * Read metadata from pdf document
2705
2713
  * @param docPtr - pointer to pdf document
@@ -5490,7 +5498,8 @@ class PdfiumEngine {
5490
5498
  return PdfTaskHelper.reject({ code: PdfErrorCode.NotFound, message: "annotation not found" });
5491
5499
  }
5492
5500
  const finalScale = Math.max(0.01, scaleFactor * dpr);
5493
- const devRect = toIntRect(transformRect(page.size, annotation.rect, rotation, finalScale));
5501
+ const rect = toIntRect(annotation.rect);
5502
+ const devRect = toIntRect(transformRect(page.size, rect, rotation, finalScale));
5494
5503
  const wDev = Math.max(1, devRect.size.width);
5495
5504
  const hDev = Math.max(1, devRect.size.height);
5496
5505
  const stride = wDev * 4;
@@ -5505,7 +5514,7 @@ class PdfiumEngine {
5505
5514
  );
5506
5515
  this.pdfiumModule.FPDFBitmap_FillRect(bitmapPtr, 0, 0, wDev, hDev, 0);
5507
5516
  const M = buildUserToDeviceMatrix(
5508
- annotation.rect,
5517
+ rect,
5509
5518
  // {origin:{L,B}, size:{W,H}}
5510
5519
  rotation,
5511
5520
  wDev,
@@ -6055,6 +6064,26 @@ class PdfiumEngine {
6055
6064
  this.memoryManager.free(bufferPtr);
6056
6065
  return ap;
6057
6066
  }
6067
+ /**
6068
+ * Set the appearance stream of annotation
6069
+ * @param annotationPtr - pointer to pdf annotation
6070
+ * @param mode - appearance mode
6071
+ * @param apContent - appearance stream content (null to remove)
6072
+ * @returns whether the appearance stream was set successfully
6073
+ *
6074
+ * @private
6075
+ */
6076
+ setPageAnnoAppearanceStream(annotationPtr, mode = AppearanceMode.Normal, apContent) {
6077
+ const bytes = 2 * (apContent.length + 1);
6078
+ const ptr = this.memoryManager.malloc(bytes);
6079
+ try {
6080
+ this.pdfiumModule.pdfium.stringToUTF16(apContent, ptr, bytes);
6081
+ const ok = this.pdfiumModule.FPDFAnnot_SetAP(annotationPtr, mode, ptr);
6082
+ return !!ok;
6083
+ } finally {
6084
+ this.memoryManager.free(ptr);
6085
+ }
6086
+ }
6058
6087
  /**
6059
6088
  * Set the rect of specified annotation
6060
6089
  * @param page - page info that the annotation is belonged to
@@ -6625,4 +6654,4 @@ export {
6625
6654
  isValidCustomKey as i,
6626
6655
  readString as r
6627
6656
  };
6628
- //# sourceMappingURL=engine-Cm-940a0.js.map
6657
+ //# sourceMappingURL=engine-4h8XG4y4.js.map