@embedpdf/engines 1.3.3 → 1.3.4

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.
@@ -2795,23 +2795,6 @@ class PdfiumEngine {
2795
2795
  }
2796
2796
  return true;
2797
2797
  }
2798
- /** Build page-space image placement matrix from a device-space rect. */
2799
- imageMatrixFromDeviceRect(page, rect) {
2800
- const x0 = rect.origin.x;
2801
- const y0 = rect.origin.y;
2802
- const x1 = x0 + rect.size.width;
2803
- const y1 = y0 + rect.size.height;
2804
- const BL = this.convertDevicePointToPagePoint(page, { x: x0, y: y1 });
2805
- const BR = this.convertDevicePointToPagePoint(page, { x: x1, y: y1 });
2806
- const TL = this.convertDevicePointToPagePoint(page, { x: x0, y: y0 });
2807
- const a = BR.x - BL.x;
2808
- const b = BR.y - BL.y;
2809
- const c = TL.x - BL.x;
2810
- const d = TL.y - BL.y;
2811
- const e = BL.x;
2812
- const f = BL.y;
2813
- return { a, b, c, d, e, f };
2814
- }
2815
2798
  /**
2816
2799
  * Add image object to annotation
2817
2800
  * @param docPtr - pointer to pdf document object
@@ -2825,63 +2808,76 @@ class PdfiumEngine {
2825
2808
  * @private
2826
2809
  */
2827
2810
  addImageObject(docPtr, page, pagePtr, annotationPtr, rect, imageData) {
2828
- const BYTES_PER_PIXEL = 4;
2811
+ const bytesPerPixel = 4;
2829
2812
  const pixelCount = imageData.width * imageData.height;
2830
- const bufPtr = this.memoryManager.malloc(BYTES_PER_PIXEL * pixelCount);
2831
- if (!bufPtr) return false;
2832
- for (let i = 0; i < pixelCount; i++) {
2833
- const r = imageData.data[i * 4 + 0];
2834
- const g = imageData.data[i * 4 + 1];
2835
- const b = imageData.data[i * 4 + 2];
2836
- const a = imageData.data[i * 4 + 3];
2837
- this.pdfiumModule.pdfium.setValue(bufPtr + i * 4 + 0, b, "i8");
2838
- this.pdfiumModule.pdfium.setValue(bufPtr + i * 4 + 1, g, "i8");
2839
- this.pdfiumModule.pdfium.setValue(bufPtr + i * 4 + 2, r, "i8");
2840
- this.pdfiumModule.pdfium.setValue(bufPtr + i * 4 + 3, a, "i8");
2813
+ const bitmapBufferPtr = this.memoryManager.malloc(bytesPerPixel * pixelCount);
2814
+ if (!bitmapBufferPtr) {
2815
+ return false;
2841
2816
  }
2817
+ for (let i = 0; i < pixelCount; i++) {
2818
+ const red = imageData.data[i * bytesPerPixel];
2819
+ const green = imageData.data[i * bytesPerPixel + 1];
2820
+ const blue = imageData.data[i * bytesPerPixel + 2];
2821
+ const alpha = imageData.data[i * bytesPerPixel + 3];
2822
+ this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel, blue, "i8");
2823
+ this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel + 1, green, "i8");
2824
+ this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel + 2, red, "i8");
2825
+ this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel + 3, alpha, "i8");
2826
+ }
2827
+ const format = 4;
2842
2828
  const bitmapPtr = this.pdfiumModule.FPDFBitmap_CreateEx(
2843
2829
  imageData.width,
2844
2830
  imageData.height,
2845
- 4,
2846
- bufPtr,
2831
+ format,
2832
+ bitmapBufferPtr,
2847
2833
  0
2848
2834
  );
2849
2835
  if (!bitmapPtr) {
2850
- this.memoryManager.free(bufPtr);
2836
+ this.memoryManager.free(bitmapBufferPtr);
2851
2837
  return false;
2852
2838
  }
2853
- const imgPtr = this.pdfiumModule.FPDFPageObj_NewImageObj(docPtr);
2854
- if (!imgPtr) {
2839
+ const imageObjectPtr = this.pdfiumModule.FPDFPageObj_NewImageObj(docPtr);
2840
+ if (!imageObjectPtr) {
2855
2841
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2856
- this.memoryManager.free(bufPtr);
2842
+ this.memoryManager.free(bitmapBufferPtr);
2857
2843
  return false;
2858
2844
  }
2859
- if (!this.pdfiumModule.FPDFImageObj_SetBitmap(pagePtr, 0, imgPtr, bitmapPtr)) {
2860
- this.pdfiumModule.FPDFPageObj_Destroy(imgPtr);
2845
+ if (!this.pdfiumModule.FPDFImageObj_SetBitmap(pagePtr, 0, imageObjectPtr, bitmapPtr)) {
2861
2846
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2862
- this.memoryManager.free(bufPtr);
2847
+ this.pdfiumModule.FPDFPageObj_Destroy(imageObjectPtr);
2848
+ this.memoryManager.free(bitmapBufferPtr);
2863
2849
  return false;
2864
2850
  }
2865
- const M = this.imageMatrixFromDeviceRect(page, rect);
2866
- const mPtr = this.memoryManager.malloc(6 * 4);
2867
- this.pdfiumModule.pdfium.setValue(mPtr + 0, M.a, "float");
2868
- this.pdfiumModule.pdfium.setValue(mPtr + 4, M.b, "float");
2869
- this.pdfiumModule.pdfium.setValue(mPtr + 8, M.c, "float");
2870
- this.pdfiumModule.pdfium.setValue(mPtr + 12, M.d, "float");
2871
- this.pdfiumModule.pdfium.setValue(mPtr + 16, M.e, "float");
2872
- this.pdfiumModule.pdfium.setValue(mPtr + 20, M.f, "float");
2873
- const setOk = this.pdfiumModule.FPDFPageObj_SetMatrix(imgPtr, mPtr);
2874
- this.memoryManager.free(mPtr);
2875
- if (!setOk) {
2876
- this.pdfiumModule.FPDFPageObj_Destroy(imgPtr);
2851
+ const matrixPtr = this.memoryManager.malloc(6 * 4);
2852
+ this.pdfiumModule.pdfium.setValue(matrixPtr, imageData.width, "float");
2853
+ this.pdfiumModule.pdfium.setValue(matrixPtr + 4, 0, "float");
2854
+ this.pdfiumModule.pdfium.setValue(matrixPtr + 8, 0, "float");
2855
+ this.pdfiumModule.pdfium.setValue(matrixPtr + 12, imageData.height, "float");
2856
+ this.pdfiumModule.pdfium.setValue(matrixPtr + 16, 0, "float");
2857
+ this.pdfiumModule.pdfium.setValue(matrixPtr + 20, 0, "float");
2858
+ if (!this.pdfiumModule.FPDFPageObj_SetMatrix(imageObjectPtr, matrixPtr)) {
2859
+ this.memoryManager.free(matrixPtr);
2877
2860
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2878
- this.memoryManager.free(bufPtr);
2861
+ this.pdfiumModule.FPDFPageObj_Destroy(imageObjectPtr);
2862
+ this.memoryManager.free(bitmapBufferPtr);
2863
+ return false;
2864
+ }
2865
+ this.memoryManager.free(matrixPtr);
2866
+ const pagePos = this.convertDevicePointToPagePoint(page, {
2867
+ x: rect.origin.x,
2868
+ y: rect.origin.y + imageData.height
2869
+ // shift down by the image height
2870
+ });
2871
+ this.pdfiumModule.FPDFPageObj_Transform(imageObjectPtr, 1, 0, 0, 1, pagePos.x, pagePos.y);
2872
+ if (!this.pdfiumModule.FPDFAnnot_AppendObject(annotationPtr, imageObjectPtr)) {
2873
+ this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2874
+ this.pdfiumModule.FPDFPageObj_Destroy(imageObjectPtr);
2875
+ this.memoryManager.free(bitmapBufferPtr);
2879
2876
  return false;
2880
2877
  }
2881
- const ok = this.pdfiumModule.FPDFAnnot_AppendObject(annotationPtr, imgPtr);
2882
2878
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2883
- this.memoryManager.free(bufPtr);
2884
- return !!ok;
2879
+ this.memoryManager.free(bitmapBufferPtr);
2880
+ return true;
2885
2881
  }
2886
2882
  /**
2887
2883
  * Save document to array buffer
@@ -6497,36 +6493,35 @@ class PdfiumEngine {
6497
6493
  /**
6498
6494
  * Set the rect of specified annotation
6499
6495
  * @param page - page info that the annotation is belonged to
6500
- * @param pagePtr - pointer of page object
6501
6496
  * @param annotationPtr - pointer to annotation object
6502
6497
  * @param rect - target rectangle
6503
6498
  * @returns whether the rect is setted
6504
6499
  *
6505
6500
  * @private
6506
6501
  */
6507
- setPageAnnoRect(page, annotationPtr, rect) {
6508
- const x0 = rect.origin.x;
6509
- const y0 = rect.origin.y;
6510
- const x1 = x0 + rect.size.width;
6511
- const y1 = y0 + rect.size.height;
6512
- const p1 = this.convertDevicePointToPagePoint(page, { x: x0, y: y0 });
6513
- const p2 = this.convertDevicePointToPagePoint(page, { x: x1, y: y0 });
6514
- const p3 = this.convertDevicePointToPagePoint(page, { x: x1, y: y1 });
6515
- const p4 = this.convertDevicePointToPagePoint(page, { x: x0, y: y1 });
6516
- if (!p1 || !p2 || !p3 || !p4) return false;
6517
- const xs = [p1.x, p2.x, p3.x, p4.x];
6518
- const ys = [p1.y, p2.y, p3.y, p4.y];
6519
- const left = Math.min(...xs);
6520
- const right = Math.max(...xs);
6521
- const bottom = Math.min(...ys);
6522
- const top = Math.max(...ys);
6523
- const rectPtr = this.memoryManager.malloc(16);
6524
- this.pdfiumModule.pdfium.setValue(rectPtr + 0, left, "float");
6525
- this.pdfiumModule.pdfium.setValue(rectPtr + 4, bottom, "float");
6526
- this.pdfiumModule.pdfium.setValue(rectPtr + 8, right, "float");
6527
- this.pdfiumModule.pdfium.setValue(rectPtr + 12, top, "float");
6528
- const ok = this.pdfiumModule.FPDFAnnot_SetRect(annotationPtr, rectPtr);
6529
- this.memoryManager.free(rectPtr);
6502
+ setPageAnnoRect(page, annotPtr, rect) {
6503
+ const x0d = Math.floor(rect.origin.x);
6504
+ const y0d = Math.floor(rect.origin.y);
6505
+ const x1d = Math.floor(rect.origin.x + rect.size.width);
6506
+ const y1d = Math.floor(rect.origin.y + rect.size.height);
6507
+ const TL = this.convertDevicePointToPagePoint(page, { x: x0d, y: y0d });
6508
+ const TR = this.convertDevicePointToPagePoint(page, { x: x1d, y: y0d });
6509
+ const BR = this.convertDevicePointToPagePoint(page, { x: x1d, y: y1d });
6510
+ const BL = this.convertDevicePointToPagePoint(page, { x: x0d, y: y1d });
6511
+ let left = Math.min(TL.x, TR.x, BR.x, BL.x);
6512
+ let right = Math.max(TL.x, TR.x, BR.x, BL.x);
6513
+ let bottom = Math.min(TL.y, TR.y, BR.y, BL.y);
6514
+ let top = Math.max(TL.y, TR.y, BR.y, BL.y);
6515
+ if (left > right) [left, right] = [right, left];
6516
+ if (bottom > top) [bottom, top] = [top, bottom];
6517
+ const ptr = this.memoryManager.malloc(16);
6518
+ const pdf = this.pdfiumModule.pdfium;
6519
+ pdf.setValue(ptr + 0, left, "float");
6520
+ pdf.setValue(ptr + 4, top, "float");
6521
+ pdf.setValue(ptr + 8, right, "float");
6522
+ pdf.setValue(ptr + 12, bottom, "float");
6523
+ const ok = this.pdfiumModule.FPDFAnnot_SetRect(annotPtr, ptr);
6524
+ this.memoryManager.free(ptr);
6530
6525
  return !!ok;
6531
6526
  }
6532
6527
  /**
@@ -7025,4 +7020,4 @@ export {
7025
7020
  isValidCustomKey as i,
7026
7021
  readString as r
7027
7022
  };
7028
- //# sourceMappingURL=engine-B2N46cyz.js.map
7023
+ //# sourceMappingURL=engine-N_GwaBxA.js.map