@embedpdf/engines 1.3.1 → 1.3.3

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.
@@ -724,12 +724,14 @@ class PdfiumEngine {
724
724
  message: `FPDF_GetPageSizeByIndexF failed`
725
725
  });
726
726
  }
727
+ const rotation = this.pdfiumModule.EPDF_GetPageRotationByIndex(docPtr, index);
727
728
  const page = {
728
729
  index,
729
730
  size: {
730
731
  width: this.pdfiumModule.pdfium.getValue(sizePtr, "float"),
731
732
  height: this.pdfiumModule.pdfium.getValue(sizePtr + 4, "float")
732
- }
733
+ },
734
+ rotation
733
735
  };
734
736
  pages.push(page);
735
737
  }
@@ -806,12 +808,14 @@ class PdfiumEngine {
806
808
  message: `FPDF_GetPageSizeByIndexF failed`
807
809
  });
808
810
  }
811
+ const rotation = this.pdfiumModule.EPDF_GetPageRotationByIndex(docPtr, index);
809
812
  const page = {
810
813
  index,
811
814
  size: {
812
815
  width: this.pdfiumModule.pdfium.getValue(sizePtr, "float"),
813
816
  height: this.pdfiumModule.pdfium.getValue(sizePtr + 4, "float")
814
- }
817
+ },
818
+ rotation
815
819
  };
816
820
  pages.push(page);
817
821
  }
@@ -1301,7 +1305,7 @@ class PdfiumEngine {
1301
1305
  message: "can not set the name of the annotation"
1302
1306
  });
1303
1307
  }
1304
- if (!this.setPageAnnoRect(page, pageCtx.pagePtr, annotationPtr, annotation.rect)) {
1308
+ if (!this.setPageAnnoRect(page, annotationPtr, annotation.rect)) {
1305
1309
  this.pdfiumModule.FPDFPage_CloseAnnot(annotationPtr);
1306
1310
  pageCtx.release();
1307
1311
  this.logger.perf(
@@ -1432,7 +1436,7 @@ class PdfiumEngine {
1432
1436
  );
1433
1437
  return PdfTaskHelper.reject({ code: PdfErrorCode.NotFound, message: "annotation not found" });
1434
1438
  }
1435
- if (!this.setPageAnnoRect(page, pageCtx.pagePtr, annotPtr, annotation.rect)) {
1439
+ if (!this.setPageAnnoRect(page, annotPtr, annotation.rect)) {
1436
1440
  this.pdfiumModule.FPDFPage_CloseAnnot(annotPtr);
1437
1441
  pageCtx.release();
1438
1442
  this.logger.perf(
@@ -2791,6 +2795,23 @@ class PdfiumEngine {
2791
2795
  }
2792
2796
  return true;
2793
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
+ }
2794
2815
  /**
2795
2816
  * Add image object to annotation
2796
2817
  * @param docPtr - pointer to pdf document object
@@ -2804,76 +2825,63 @@ class PdfiumEngine {
2804
2825
  * @private
2805
2826
  */
2806
2827
  addImageObject(docPtr, page, pagePtr, annotationPtr, rect, imageData) {
2807
- const bytesPerPixel = 4;
2828
+ const BYTES_PER_PIXEL = 4;
2808
2829
  const pixelCount = imageData.width * imageData.height;
2809
- const bitmapBufferPtr = this.memoryManager.malloc(bytesPerPixel * pixelCount);
2810
- if (!bitmapBufferPtr) {
2811
- return false;
2812
- }
2830
+ const bufPtr = this.memoryManager.malloc(BYTES_PER_PIXEL * pixelCount);
2831
+ if (!bufPtr) return false;
2813
2832
  for (let i = 0; i < pixelCount; i++) {
2814
- const red = imageData.data[i * bytesPerPixel];
2815
- const green = imageData.data[i * bytesPerPixel + 1];
2816
- const blue = imageData.data[i * bytesPerPixel + 2];
2817
- const alpha = imageData.data[i * bytesPerPixel + 3];
2818
- this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel, blue, "i8");
2819
- this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel + 1, green, "i8");
2820
- this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel + 2, red, "i8");
2821
- this.pdfiumModule.pdfium.setValue(bitmapBufferPtr + i * bytesPerPixel + 3, alpha, "i8");
2822
- }
2823
- const format = 4;
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");
2841
+ }
2824
2842
  const bitmapPtr = this.pdfiumModule.FPDFBitmap_CreateEx(
2825
2843
  imageData.width,
2826
2844
  imageData.height,
2827
- format,
2828
- bitmapBufferPtr,
2845
+ 4,
2846
+ bufPtr,
2829
2847
  0
2830
2848
  );
2831
2849
  if (!bitmapPtr) {
2832
- this.memoryManager.free(bitmapBufferPtr);
2833
- return false;
2834
- }
2835
- const imageObjectPtr = this.pdfiumModule.FPDFPageObj_NewImageObj(docPtr);
2836
- if (!imageObjectPtr) {
2837
- this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2838
- this.memoryManager.free(bitmapBufferPtr);
2850
+ this.memoryManager.free(bufPtr);
2839
2851
  return false;
2840
2852
  }
2841
- if (!this.pdfiumModule.FPDFImageObj_SetBitmap(pagePtr, 0, imageObjectPtr, bitmapPtr)) {
2853
+ const imgPtr = this.pdfiumModule.FPDFPageObj_NewImageObj(docPtr);
2854
+ if (!imgPtr) {
2842
2855
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2843
- this.pdfiumModule.FPDFPageObj_Destroy(imageObjectPtr);
2844
- this.memoryManager.free(bitmapBufferPtr);
2856
+ this.memoryManager.free(bufPtr);
2845
2857
  return false;
2846
2858
  }
2847
- const matrixPtr = this.memoryManager.malloc(6 * 4);
2848
- this.pdfiumModule.pdfium.setValue(matrixPtr, imageData.width, "float");
2849
- this.pdfiumModule.pdfium.setValue(matrixPtr + 4, 0, "float");
2850
- this.pdfiumModule.pdfium.setValue(matrixPtr + 8, 0, "float");
2851
- this.pdfiumModule.pdfium.setValue(matrixPtr + 12, imageData.height, "float");
2852
- this.pdfiumModule.pdfium.setValue(matrixPtr + 16, 0, "float");
2853
- this.pdfiumModule.pdfium.setValue(matrixPtr + 20, 0, "float");
2854
- if (!this.pdfiumModule.FPDFPageObj_SetMatrix(imageObjectPtr, matrixPtr)) {
2855
- this.memoryManager.free(matrixPtr);
2859
+ if (!this.pdfiumModule.FPDFImageObj_SetBitmap(pagePtr, 0, imgPtr, bitmapPtr)) {
2860
+ this.pdfiumModule.FPDFPageObj_Destroy(imgPtr);
2856
2861
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2857
- this.pdfiumModule.FPDFPageObj_Destroy(imageObjectPtr);
2858
- this.memoryManager.free(bitmapBufferPtr);
2862
+ this.memoryManager.free(bufPtr);
2859
2863
  return false;
2860
2864
  }
2861
- this.memoryManager.free(matrixPtr);
2862
- const pagePos = this.convertDevicePointToPagePoint(page, {
2863
- x: rect.origin.x,
2864
- y: rect.origin.y + imageData.height
2865
- // shift down by the image height
2866
- });
2867
- this.pdfiumModule.FPDFPageObj_Transform(imageObjectPtr, 1, 0, 0, 1, pagePos.x, pagePos.y);
2868
- if (!this.pdfiumModule.FPDFAnnot_AppendObject(annotationPtr, imageObjectPtr)) {
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);
2869
2877
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2870
- this.pdfiumModule.FPDFPageObj_Destroy(imageObjectPtr);
2871
- this.memoryManager.free(bitmapBufferPtr);
2878
+ this.memoryManager.free(bufPtr);
2872
2879
  return false;
2873
2880
  }
2881
+ const ok = this.pdfiumModule.FPDFAnnot_AppendObject(annotationPtr, imgPtr);
2874
2882
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
2875
- this.memoryManager.free(bitmapBufferPtr);
2876
- return true;
2883
+ this.memoryManager.free(bufPtr);
2884
+ return !!ok;
2877
2885
  }
2878
2886
  /**
2879
2887
  * Save document to array buffer
@@ -4079,24 +4087,24 @@ class PdfiumEngine {
4079
4087
  * @param page - logical page info object (`PdfPageObject`)
4080
4088
  * @returns `{ start, end }` or `undefined` when PDFium can't read them
4081
4089
  */
4082
- getLinePoints(annotationPtr, page) {
4083
- const startPointPtr = this.memoryManager.malloc(8);
4084
- const endPointPtr = this.memoryManager.malloc(8);
4085
- this.pdfiumModule.FPDFAnnot_GetLine(annotationPtr, startPointPtr, endPointPtr);
4086
- const startPointX = this.pdfiumModule.pdfium.getValue(startPointPtr, "float");
4087
- const startPointY = this.pdfiumModule.pdfium.getValue(startPointPtr + 4, "float");
4088
- const start = this.convertPagePointToDevicePoint(page, {
4089
- x: startPointX,
4090
- y: startPointY
4091
- });
4092
- const endPointX = this.pdfiumModule.pdfium.getValue(endPointPtr, "float");
4093
- const endPointY = this.pdfiumModule.pdfium.getValue(endPointPtr + 4, "float");
4094
- const end = this.convertPagePointToDevicePoint(page, {
4095
- x: endPointX,
4096
- y: endPointY
4097
- });
4098
- this.memoryManager.free(startPointPtr);
4099
- this.memoryManager.free(endPointPtr);
4090
+ getLinePoints(page, annotationPtr) {
4091
+ const startPtr = this.memoryManager.malloc(8);
4092
+ const endPtr = this.memoryManager.malloc(8);
4093
+ const ok = this.pdfiumModule.FPDFAnnot_GetLine(annotationPtr, startPtr, endPtr);
4094
+ if (!ok) {
4095
+ this.memoryManager.free(startPtr);
4096
+ this.memoryManager.free(endPtr);
4097
+ return void 0;
4098
+ }
4099
+ const pdf = this.pdfiumModule.pdfium;
4100
+ const sx = pdf.getValue(startPtr + 0, "float");
4101
+ const sy = pdf.getValue(startPtr + 4, "float");
4102
+ const ex = pdf.getValue(endPtr + 0, "float");
4103
+ const ey = pdf.getValue(endPtr + 4, "float");
4104
+ this.memoryManager.free(startPtr);
4105
+ this.memoryManager.free(endPtr);
4106
+ const start = this.convertPagePointToDevicePoint(page, { x: sx, y: sy });
4107
+ const end = this.convertPagePointToDevicePoint(page, { x: ex, y: ey });
4100
4108
  return { start, end };
4101
4109
  }
4102
4110
  /**
@@ -4109,16 +4117,18 @@ class PdfiumEngine {
4109
4117
  * @returns true on success
4110
4118
  */
4111
4119
  setLinePoints(page, annotPtr, start, end) {
4112
- const buf = this.memoryManager.malloc(16);
4113
4120
  const p1 = this.convertDevicePointToPagePoint(page, start);
4114
4121
  const p2 = this.convertDevicePointToPagePoint(page, end);
4115
- this.pdfiumModule.pdfium.setValue(buf + 0, p1.x, "float");
4116
- this.pdfiumModule.pdfium.setValue(buf + 4, p1.y, "float");
4117
- this.pdfiumModule.pdfium.setValue(buf + 8, p2.x, "float");
4118
- this.pdfiumModule.pdfium.setValue(buf + 12, p2.y, "float");
4122
+ if (!p1 || !p2) return false;
4123
+ const buf = this.memoryManager.malloc(16);
4124
+ const pdf = this.pdfiumModule.pdfium;
4125
+ pdf.setValue(buf + 0, p1.x, "float");
4126
+ pdf.setValue(buf + 4, p1.y, "float");
4127
+ pdf.setValue(buf + 8, p2.x, "float");
4128
+ pdf.setValue(buf + 12, p2.y, "float");
4119
4129
  const ok = this.pdfiumModule.EPDFAnnot_SetLine(annotPtr, buf, buf + 8);
4120
4130
  this.memoryManager.free(buf);
4121
- return ok;
4131
+ return !!ok;
4122
4132
  }
4123
4133
  /**
4124
4134
  * Read `/QuadPoints` from any annotation and convert each quadrilateral to
@@ -4292,29 +4302,30 @@ class PdfiumEngine {
4292
4302
  /**
4293
4303
  * Read ink list from annotation
4294
4304
  * @param page - logical page info object (`PdfPageObject`)
4305
+ * @param pagePtr - pointer to the page
4295
4306
  * @param annotationPtr - pointer to the annotation whose ink list is needed
4296
4307
  * @returns ink list
4297
4308
  */
4298
4309
  getInkList(page, annotationPtr) {
4299
4310
  const inkList = [];
4300
- const count = this.pdfiumModule.FPDFAnnot_GetInkListCount(annotationPtr);
4301
- for (let i = 0; i < count; i++) {
4311
+ const pathCount = this.pdfiumModule.FPDFAnnot_GetInkListCount(annotationPtr);
4312
+ if (pathCount <= 0) return inkList;
4313
+ const pdf = this.pdfiumModule.pdfium;
4314
+ const POINT_STRIDE = 8;
4315
+ for (let i = 0; i < pathCount; i++) {
4302
4316
  const points = [];
4303
- const pointsCount = this.pdfiumModule.FPDFAnnot_GetInkListPath(annotationPtr, i, 0, 0);
4304
- if (pointsCount > 0) {
4305
- const pointMemorySize = 8;
4306
- const pointsPtr = this.memoryManager.malloc(pointsCount * pointMemorySize);
4307
- this.pdfiumModule.FPDFAnnot_GetInkListPath(annotationPtr, i, pointsPtr, pointsCount);
4308
- for (let j = 0; j < pointsCount; j++) {
4309
- const pointX = this.pdfiumModule.pdfium.getValue(pointsPtr + j * 8, "float");
4310
- const pointY = this.pdfiumModule.pdfium.getValue(pointsPtr + j * 8 + 4, "float");
4311
- const { x, y } = this.convertPagePointToDevicePoint(page, {
4312
- x: pointX,
4313
- y: pointY
4314
- });
4315
- points.push({ x, y });
4317
+ const n = this.pdfiumModule.FPDFAnnot_GetInkListPath(annotationPtr, i, 0, 0);
4318
+ if (n > 0) {
4319
+ const buf = this.memoryManager.malloc(n * POINT_STRIDE);
4320
+ this.pdfiumModule.FPDFAnnot_GetInkListPath(annotationPtr, i, buf, n);
4321
+ for (let j = 0; j < n; j++) {
4322
+ const base = buf + j * POINT_STRIDE;
4323
+ const px = pdf.getValue(base + 0, "float");
4324
+ const py = pdf.getValue(base + 4, "float");
4325
+ const d = this.convertPagePointToDevicePoint(page, { x: px, y: py });
4326
+ points.push({ x: d.x, y: d.y });
4316
4327
  }
4317
- this.memoryManager.free(pointsPtr);
4328
+ this.memoryManager.free(buf);
4318
4329
  }
4319
4330
  inkList.push({ points });
4320
4331
  }
@@ -4323,25 +4334,29 @@ class PdfiumEngine {
4323
4334
  /**
4324
4335
  * Add ink list to annotation
4325
4336
  * @param page - logical page info object (`PdfPageObject`)
4337
+ * @param pagePtr - pointer to the page
4326
4338
  * @param annotationPtr - pointer to the annotation whose ink list is needed
4327
- * @param annotation - annotation object (`PdfInkAnnoObject`)
4339
+ * @param inkList - ink list array of `PdfInkListObject`
4328
4340
  * @returns `true` if the operation was successful
4329
4341
  */
4330
4342
  setInkList(page, annotationPtr, inkList) {
4331
- for (const inkStroke of inkList) {
4332
- const inkPointsCount = inkStroke.points.length;
4333
- const inkPointsPtr = this.memoryManager.malloc(inkPointsCount * 8);
4334
- for (let i = 0; i < inkPointsCount; i++) {
4335
- const point = inkStroke.points[i];
4336
- const { x, y } = this.convertDevicePointToPagePoint(page, point);
4337
- this.pdfiumModule.pdfium.setValue(inkPointsPtr + i * 8, x, "float");
4338
- this.pdfiumModule.pdfium.setValue(inkPointsPtr + i * 8 + 4, y, "float");
4343
+ const pdf = this.pdfiumModule.pdfium;
4344
+ const POINT_STRIDE = 8;
4345
+ for (const stroke of inkList) {
4346
+ const n = stroke.points.length;
4347
+ if (n === 0) continue;
4348
+ const buf = this.memoryManager.malloc(n * POINT_STRIDE);
4349
+ for (let i = 0; i < n; i++) {
4350
+ const pDev = stroke.points[i];
4351
+ const pPage = this.convertDevicePointToPagePoint(page, pDev);
4352
+ pdf.setValue(buf + i * POINT_STRIDE + 0, pPage.x, "float");
4353
+ pdf.setValue(buf + i * POINT_STRIDE + 4, pPage.y, "float");
4339
4354
  }
4340
- if (this.pdfiumModule.FPDFAnnot_AddInkStroke(annotationPtr, inkPointsPtr, inkPointsCount) === -1) {
4341
- this.memoryManager.free(inkPointsPtr);
4355
+ const idx = this.pdfiumModule.FPDFAnnot_AddInkStroke(annotationPtr, buf, n);
4356
+ this.memoryManager.free(buf);
4357
+ if (idx === -1) {
4342
4358
  return false;
4343
4359
  }
4344
- this.memoryManager.free(inkPointsPtr);
4345
4360
  }
4346
4361
  return true;
4347
4362
  }
@@ -4714,7 +4729,7 @@ class PdfiumEngine {
4714
4729
  const author = this.getAnnotString(annotationPtr, "T");
4715
4730
  const modified = this.getAnnotationDate(annotationPtr, "M");
4716
4731
  const created = this.getAnnotationDate(annotationPtr, "CreationDate");
4717
- const linePoints = this.getLinePoints(annotationPtr, page);
4732
+ const linePoints = this.getLinePoints(page, annotationPtr);
4718
4733
  const lineEndings = this.getLineEndings(annotationPtr);
4719
4734
  const contents = this.getAnnotString(annotationPtr, "Contents") || "";
4720
4735
  const strokeColor = this.getAnnotationColor(annotationPtr);
@@ -5939,6 +5954,7 @@ class PdfiumEngine {
5939
5954
  const hDev = Math.max(1, Math.round((swap ? baseW : baseH) * finalScale));
5940
5955
  const stride = wDev * 4;
5941
5956
  const bytes = stride * hDev;
5957
+ const pageCtx = ctx.acquirePage(page.index);
5942
5958
  const heapPtr = this.memoryManager.malloc(bytes);
5943
5959
  const bitmapPtr = this.pdfiumModule.FPDFBitmap_CreateEx(
5944
5960
  wDev,
@@ -5955,9 +5971,8 @@ class PdfiumEngine {
5955
5971
  const clipPtr = this.memoryManager.malloc(4 * 4);
5956
5972
  const clipView = new Float32Array(this.pdfiumModule.pdfium.HEAPF32.buffer, clipPtr, 4);
5957
5973
  clipView.set([0, 0, wDev, hDev]);
5958
- let flags = 2 | 16;
5974
+ let flags = 16;
5959
5975
  if ((options == null ? void 0 : options.withAnnotations) ?? false) flags |= 1;
5960
- const pageCtx = ctx.acquirePage(page.index);
5961
5976
  try {
5962
5977
  this.pdfiumModule.FPDF_RenderPageBitmapWithMatrix(
5963
5978
  bitmapPtr,
@@ -5977,13 +5992,12 @@ class PdfiumEngine {
5977
5992
  };
5978
5993
  this.imageDataConverter(
5979
5994
  () => {
5980
- const rgba = new Uint8ClampedArray(
5981
- this.pdfiumModule.pdfium.HEAPU8.subarray(heapPtr, heapPtr + bytes)
5982
- );
5995
+ const heapBuf = this.pdfiumModule.pdfium.HEAPU8.buffer;
5996
+ const data = new Uint8ClampedArray(heapBuf, heapPtr, bytes);
5983
5997
  return {
5984
5998
  width: wDev,
5985
5999
  height: hDev,
5986
- data: rgba
6000
+ data
5987
6001
  };
5988
6002
  },
5989
6003
  imageType,
@@ -6361,9 +6375,21 @@ class PdfiumEngine {
6361
6375
  * @private
6362
6376
  */
6363
6377
  convertDevicePointToPagePoint(page, position) {
6364
- const x = position.x;
6365
- const y = page.size.height - position.y;
6366
- return { x, y };
6378
+ const DW = page.size.width;
6379
+ const DH = page.size.height;
6380
+ const r = page.rotation & 3;
6381
+ if (r === 0) {
6382
+ return { x: position.x, y: DH - position.y };
6383
+ }
6384
+ if (r === 1) {
6385
+ return { x: position.y, y: position.x };
6386
+ }
6387
+ if (r === 2) {
6388
+ return { x: DW - position.x, y: position.y };
6389
+ }
6390
+ {
6391
+ return { x: DH - position.y, y: DW - position.x };
6392
+ }
6367
6393
  }
6368
6394
  /**
6369
6395
  * Convert coordinate of point from page coordinate to device coordinate
@@ -6374,9 +6400,21 @@ class PdfiumEngine {
6374
6400
  * @private
6375
6401
  */
6376
6402
  convertPagePointToDevicePoint(page, position) {
6377
- const x = position.x;
6378
- const y = page.size.height - position.y;
6379
- return { x, y };
6403
+ const DW = page.size.width;
6404
+ const DH = page.size.height;
6405
+ const r = page.rotation & 3;
6406
+ if (r === 0) {
6407
+ return { x: position.x, y: DH - position.y };
6408
+ }
6409
+ if (r === 1) {
6410
+ return { x: position.y, y: position.x };
6411
+ }
6412
+ if (r === 2) {
6413
+ return { x: DW - position.x, y: position.y };
6414
+ }
6415
+ {
6416
+ return { x: DW - position.y, y: DH - position.x };
6417
+ }
6380
6418
  }
6381
6419
  /**
6382
6420
  * Convert coordinate of rectangle from page coordinate to device coordinate
@@ -6466,40 +6504,30 @@ class PdfiumEngine {
6466
6504
  *
6467
6505
  * @private
6468
6506
  */
6469
- setPageAnnoRect(page, pagePtr, annotationPtr, rect) {
6470
- const pageXPtr = this.memoryManager.malloc(8);
6471
- const pageYPtr = this.memoryManager.malloc(8);
6472
- if (!this.pdfiumModule.FPDF_DeviceToPage(
6473
- pagePtr,
6474
- 0,
6475
- 0,
6476
- page.size.width,
6477
- page.size.height,
6478
- 0,
6479
- rect.origin.x,
6480
- rect.origin.y,
6481
- pageXPtr,
6482
- pageYPtr
6483
- )) {
6484
- this.memoryManager.free(pageXPtr);
6485
- this.memoryManager.free(pageYPtr);
6486
- return false;
6487
- }
6488
- const pageX = this.pdfiumModule.pdfium.getValue(pageXPtr, "double");
6489
- const pageY = this.pdfiumModule.pdfium.getValue(pageYPtr, "double");
6490
- this.memoryManager.free(pageXPtr);
6491
- this.memoryManager.free(pageYPtr);
6492
- const pageRectPtr = this.memoryManager.malloc(4 * 4);
6493
- this.pdfiumModule.pdfium.setValue(pageRectPtr, pageX, "float");
6494
- this.pdfiumModule.pdfium.setValue(pageRectPtr + 4, pageY, "float");
6495
- this.pdfiumModule.pdfium.setValue(pageRectPtr + 8, pageX + rect.size.width, "float");
6496
- this.pdfiumModule.pdfium.setValue(pageRectPtr + 12, pageY - rect.size.height, "float");
6497
- if (!this.pdfiumModule.FPDFAnnot_SetRect(annotationPtr, pageRectPtr)) {
6498
- this.memoryManager.free(pageRectPtr);
6499
- return false;
6500
- }
6501
- this.memoryManager.free(pageRectPtr);
6502
- return true;
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);
6530
+ return !!ok;
6503
6531
  }
6504
6532
  /**
6505
6533
  * Read the rectangle of annotation
@@ -6536,62 +6564,39 @@ class PdfiumEngine {
6536
6564
  *
6537
6565
  * @private
6538
6566
  */
6539
- getHighlightRects(page, pagePtr, textPagePtr, startIndex, charCount) {
6567
+ getHighlightRects(page, textPagePtr, startIndex, charCount) {
6540
6568
  const rectsCount = this.pdfiumModule.FPDFText_CountRects(textPagePtr, startIndex, charCount);
6541
6569
  const highlightRects = [];
6570
+ const l = this.memoryManager.malloc(8);
6571
+ const t = this.memoryManager.malloc(8);
6572
+ const r = this.memoryManager.malloc(8);
6573
+ const b = this.memoryManager.malloc(8);
6542
6574
  for (let i = 0; i < rectsCount; i++) {
6543
- const topPtr = this.memoryManager.malloc(8);
6544
- const leftPtr = this.memoryManager.malloc(8);
6545
- const rightPtr = this.memoryManager.malloc(8);
6546
- const bottomPtr = this.memoryManager.malloc(8);
6547
- const isSucceed = this.pdfiumModule.FPDFText_GetRect(
6548
- textPagePtr,
6549
- i,
6550
- leftPtr,
6551
- topPtr,
6552
- rightPtr,
6553
- bottomPtr
6554
- );
6555
- if (!isSucceed) {
6556
- this.memoryManager.free(leftPtr);
6557
- this.memoryManager.free(topPtr);
6558
- this.memoryManager.free(rightPtr);
6559
- this.memoryManager.free(bottomPtr);
6560
- continue;
6561
- }
6562
- const left = this.pdfiumModule.pdfium.getValue(leftPtr, "double");
6563
- const top = this.pdfiumModule.pdfium.getValue(topPtr, "double");
6564
- const right = this.pdfiumModule.pdfium.getValue(rightPtr, "double");
6565
- const bottom = this.pdfiumModule.pdfium.getValue(bottomPtr, "double");
6566
- this.memoryManager.free(leftPtr);
6567
- this.memoryManager.free(topPtr);
6568
- this.memoryManager.free(rightPtr);
6569
- this.memoryManager.free(bottomPtr);
6570
- const deviceXPtr = this.memoryManager.malloc(4);
6571
- const deviceYPtr = this.memoryManager.malloc(4);
6572
- this.pdfiumModule.FPDF_PageToDevice(
6573
- pagePtr,
6574
- 0,
6575
- 0,
6576
- page.size.width,
6577
- page.size.height,
6578
- 0,
6579
- left,
6580
- top,
6581
- deviceXPtr,
6582
- deviceYPtr
6583
- );
6584
- const x = this.pdfiumModule.pdfium.getValue(deviceXPtr, "i32");
6585
- const y = this.pdfiumModule.pdfium.getValue(deviceYPtr, "i32");
6586
- this.memoryManager.free(deviceXPtr);
6587
- this.memoryManager.free(deviceYPtr);
6588
- const width = Math.ceil(Math.abs(right - left));
6589
- const height = Math.ceil(Math.abs(top - bottom));
6575
+ const ok = this.pdfiumModule.FPDFText_GetRect(textPagePtr, i, l, t, r, b);
6576
+ if (!ok) continue;
6577
+ const left = this.pdfiumModule.pdfium.getValue(l, "double");
6578
+ const top = this.pdfiumModule.pdfium.getValue(t, "double");
6579
+ const right = this.pdfiumModule.pdfium.getValue(r, "double");
6580
+ const bottom = this.pdfiumModule.pdfium.getValue(b, "double");
6581
+ const p1 = this.convertPagePointToDevicePoint(page, { x: left, y: top });
6582
+ const p2 = this.convertPagePointToDevicePoint(page, { x: right, y: top });
6583
+ const p3 = this.convertPagePointToDevicePoint(page, { x: right, y: bottom });
6584
+ const p4 = this.convertPagePointToDevicePoint(page, { x: left, y: bottom });
6585
+ const xs = [p1.x, p2.x, p3.x, p4.x];
6586
+ const ys = [p1.y, p2.y, p3.y, p4.y];
6587
+ const x = Math.min(...xs);
6588
+ const y = Math.min(...ys);
6589
+ const width = Math.max(...xs) - x;
6590
+ const height = Math.max(...ys) - y;
6590
6591
  highlightRects.push({
6591
6592
  origin: { x, y },
6592
- size: { width, height }
6593
+ size: { width: Math.ceil(width), height: Math.ceil(height) }
6593
6594
  });
6594
6595
  }
6596
+ this.memoryManager.free(l);
6597
+ this.memoryManager.free(t);
6598
+ this.memoryManager.free(r);
6599
+ this.memoryManager.free(b);
6595
6600
  return highlightRects;
6596
6601
  }
6597
6602
  /**
@@ -6760,13 +6765,7 @@ class PdfiumEngine {
6760
6765
  while (this.pdfiumModule.FPDFText_FindNext(searchHandle)) {
6761
6766
  const charIndex = this.pdfiumModule.FPDFText_GetSchResultIndex(searchHandle);
6762
6767
  const charCount = this.pdfiumModule.FPDFText_GetSchCount(searchHandle);
6763
- const rects = this.getHighlightRects(
6764
- page,
6765
- pageCtx.pagePtr,
6766
- textPagePtr,
6767
- charIndex,
6768
- charCount
6769
- );
6768
+ const rects = this.getHighlightRects(page, textPagePtr, charIndex, charCount);
6770
6769
  const context = this.buildContext(fullText, charIndex, charCount);
6771
6770
  pageResults.push({
6772
6771
  pageIndex: page.index,
@@ -7026,4 +7025,4 @@ export {
7026
7025
  isValidCustomKey as i,
7027
7026
  readString as r
7028
7027
  };
7029
- //# sourceMappingURL=engine-BVe88qdX.js.map
7028
+ //# sourceMappingURL=engine-B2N46cyz.js.map