@embedpdf/engines 2.6.0 → 2.6.2

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.
Files changed (43) hide show
  1. package/dist/direct-engine-CCVjfywm.cjs +2 -0
  2. package/dist/direct-engine-CCVjfywm.cjs.map +1 -0
  3. package/dist/{direct-engine-CvfzIn2D.js → direct-engine-CmsmBgWP.js} +253 -9
  4. package/dist/direct-engine-CmsmBgWP.js.map +1 -0
  5. package/dist/index.cjs +1 -1
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.js +11 -2
  8. package/dist/index.js.map +1 -1
  9. package/dist/lib/orchestrator/pdf-engine.d.ts +4 -1
  10. package/dist/lib/orchestrator/remote-executor.d.ts +2 -1
  11. package/dist/lib/pdfium/engine.d.ts +13 -1
  12. package/dist/lib/pdfium/index.cjs +1 -1
  13. package/dist/lib/pdfium/index.js +3 -3
  14. package/dist/lib/pdfium/web/direct-engine.cjs +1 -1
  15. package/dist/lib/pdfium/web/direct-engine.js +2 -2
  16. package/dist/lib/pdfium/web/worker-engine.cjs +1 -1
  17. package/dist/lib/pdfium/web/worker-engine.cjs.map +1 -1
  18. package/dist/lib/pdfium/web/worker-engine.js +5 -2
  19. package/dist/lib/pdfium/web/worker-engine.js.map +1 -1
  20. package/dist/lib/webworker/engine.cjs +1 -1
  21. package/dist/lib/webworker/engine.cjs.map +1 -1
  22. package/dist/lib/webworker/engine.d.ts +19 -1
  23. package/dist/lib/webworker/engine.js +44 -0
  24. package/dist/lib/webworker/engine.js.map +1 -1
  25. package/dist/{pdf-engine-aJNoNMbt.js → pdf-engine-C07l7iNd.js} +29 -1
  26. package/dist/pdf-engine-C07l7iNd.js.map +1 -0
  27. package/dist/pdf-engine-DUjCt3HC.cjs +2 -0
  28. package/dist/pdf-engine-DUjCt3HC.cjs.map +1 -0
  29. package/dist/preact/index.cjs +1 -1
  30. package/dist/preact/index.js +1 -1
  31. package/dist/react/index.cjs +1 -1
  32. package/dist/react/index.js +1 -1
  33. package/dist/svelte/index.cjs +1 -1
  34. package/dist/svelte/index.js +1 -1
  35. package/dist/vue/index.cjs +1 -1
  36. package/dist/vue/index.js +1 -1
  37. package/package.json +5 -5
  38. package/dist/direct-engine-CvfzIn2D.js.map +0 -1
  39. package/dist/direct-engine-DITdlET4.cjs +0 -2
  40. package/dist/direct-engine-DITdlET4.cjs.map +0 -1
  41. package/dist/pdf-engine-BmrecQLq.cjs +0 -2
  42. package/dist/pdf-engine-BmrecQLq.cjs.map +0 -1
  43. package/dist/pdf-engine-aJNoNMbt.js.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import { init } from "@embedpdf/pdfium";
2
2
  import { Rotation, NoopLogger, PdfTaskHelper, PdfErrorCode, pdfDateToDate, isUuidV4, uuidV4, PdfAnnotationSubtype, PdfPageFlattenFlag, stripPdfUnwantedMarkers, PdfAnnotationIcon, PdfAnnotationBorderStyle, PdfAnnotationColorType, PdfAnnotationLineEnding, PdfStandardFont, PdfStampFit, PdfTrappedStatus, pdfColorToWebColor, webColorToPdfColor, pdfAlphaToWebOpacity, webOpacityToPdfAlpha, PdfAnnotationReplyType, dateToPdfDate, quadToRect, rectToQuad, PdfPageObjectType, flagsToNames, namesToFlags, PDF_FORM_FIELD_TYPE, AppearanceMode, Task, toIntRect, transformRect, buildUserToDeviceMatrix, PdfZoomMode, PdfActionType } from "@embedpdf/models";
3
- import { P as PdfEngine } from "./pdf-engine-aJNoNMbt.js";
3
+ import { P as PdfEngine } from "./pdf-engine-C07l7iNd.js";
4
4
  import { b as browserImageDataToBlobConverter } from "./browser-BISJ9naB.js";
5
5
  function readString(wasmModule, readChars, parseChars, defaultLength = 100) {
6
6
  let buffer = wasmModule.wasmExports.malloc(defaultLength);
@@ -3732,7 +3732,8 @@ class PdfiumNative {
3732
3732
  height: g.size.height
3733
3733
  },
3734
3734
  charStart: i,
3735
- glyphs: []
3735
+ glyphs: [],
3736
+ fontSize: this.pdfiumModule.FPDFText_GetFontSize(textPagePtr, i)
3736
3737
  };
3737
3738
  bounds = {
3738
3739
  minX: g.origin.x,
@@ -3747,7 +3748,9 @@ class PdfiumNative {
3747
3748
  y: g.origin.y,
3748
3749
  width: g.size.width,
3749
3750
  height: g.size.height,
3750
- flags: g.isEmpty ? 2 : g.isSpace ? 1 : 0
3751
+ flags: g.isEmpty ? 2 : g.isSpace ? 1 : 0,
3752
+ ...g.tightOrigin && { tightX: g.tightOrigin.x, tightY: g.tightOrigin.y },
3753
+ ...g.tightSize && { tightWidth: g.tightSize.width, tightHeight: g.tightSize.height }
3751
3754
  });
3752
3755
  if (g.isEmpty) {
3753
3756
  continue;
@@ -3765,6 +3768,184 @@ class PdfiumNative {
3765
3768
  }
3766
3769
  return runs;
3767
3770
  }
3771
+ /**
3772
+ * Rich text runs: groups consecutive characters sharing the same
3773
+ * text object, font, size, and fill color into structured segments
3774
+ * with full font metadata and bounding boxes in PDF page coordinates.
3775
+ *
3776
+ * @public
3777
+ */
3778
+ getPageTextRuns(doc, page) {
3779
+ const label = "getPageTextRuns";
3780
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, label, "Begin", doc.id);
3781
+ const ctx = this.cache.getContext(doc.id);
3782
+ if (!ctx) {
3783
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, label, "End", doc.id);
3784
+ return PdfTaskHelper.reject({
3785
+ code: PdfErrorCode.DocNotOpen,
3786
+ message: "document does not open"
3787
+ });
3788
+ }
3789
+ const pageCtx = ctx.acquirePage(page.index);
3790
+ const textPagePtr = pageCtx.getTextPage();
3791
+ const charCount = this.pdfiumModule.FPDFText_CountChars(textPagePtr);
3792
+ const runs = [];
3793
+ let runStart = 0;
3794
+ let curObjPtr = null;
3795
+ let curFont = null;
3796
+ let curFontSize = 0;
3797
+ let curColor = null;
3798
+ let bounds = null;
3799
+ const flushRun = (end) => {
3800
+ if (curObjPtr === null || curFont === null || curColor === null || bounds === null) return;
3801
+ const count = end - runStart;
3802
+ if (count <= 0) return;
3803
+ const bufPtr = this.memoryManager.malloc(2 * (count + 1));
3804
+ this.pdfiumModule.FPDFText_GetText(textPagePtr, runStart, count, bufPtr);
3805
+ const text = stripPdfUnwantedMarkers(this.pdfiumModule.pdfium.UTF16ToString(bufPtr));
3806
+ this.memoryManager.free(bufPtr);
3807
+ runs.push({
3808
+ text,
3809
+ rect: {
3810
+ origin: { x: bounds.minX, y: bounds.minY },
3811
+ size: {
3812
+ width: Math.max(1, bounds.maxX - bounds.minX),
3813
+ height: Math.max(1, bounds.maxY - bounds.minY)
3814
+ }
3815
+ },
3816
+ font: curFont,
3817
+ fontSize: curFontSize,
3818
+ color: curColor,
3819
+ charIndex: runStart,
3820
+ charCount: count
3821
+ });
3822
+ };
3823
+ const rPtr = this.memoryManager.malloc(4);
3824
+ const gPtr = this.memoryManager.malloc(4);
3825
+ const bPtr = this.memoryManager.malloc(4);
3826
+ const aPtr = this.memoryManager.malloc(4);
3827
+ const rectPtr = this.memoryManager.malloc(16);
3828
+ const dx1Ptr = this.memoryManager.malloc(4);
3829
+ const dy1Ptr = this.memoryManager.malloc(4);
3830
+ const dx2Ptr = this.memoryManager.malloc(4);
3831
+ const dy2Ptr = this.memoryManager.malloc(4);
3832
+ const italicAnglePtr = this.memoryManager.malloc(4);
3833
+ for (let i = 0; i < charCount; i++) {
3834
+ const uc = this.pdfiumModule.FPDFText_GetUnicode(textPagePtr, i);
3835
+ if (uc === 65534 || uc === 65533) continue;
3836
+ const objPtr = this.pdfiumModule.FPDFText_GetTextObject(textPagePtr, i);
3837
+ if (objPtr === 0) continue;
3838
+ const fontSize = this.pdfiumModule.FPDFText_GetFontSize(textPagePtr, i);
3839
+ this.pdfiumModule.FPDFText_GetFillColor(textPagePtr, i, rPtr, gPtr, bPtr, aPtr);
3840
+ const red = this.pdfiumModule.pdfium.getValue(rPtr, "i32") & 255;
3841
+ const green = this.pdfiumModule.pdfium.getValue(gPtr, "i32") & 255;
3842
+ const blue = this.pdfiumModule.pdfium.getValue(bPtr, "i32") & 255;
3843
+ const alpha = this.pdfiumModule.pdfium.getValue(aPtr, "i32") & 255;
3844
+ const fontInfo = this.readFontInfoFromTextObject(objPtr, italicAnglePtr);
3845
+ const needNewRun = curObjPtr === null || objPtr !== curObjPtr || fontInfo.name !== curFont.name || Math.abs(fontSize - curFontSize) > 0.01 || red !== curColor.red || green !== curColor.green || blue !== curColor.blue;
3846
+ if (needNewRun) {
3847
+ flushRun(i);
3848
+ curObjPtr = objPtr;
3849
+ curFont = fontInfo;
3850
+ curFontSize = fontSize;
3851
+ curColor = { red, green, blue, alpha };
3852
+ runStart = i;
3853
+ bounds = null;
3854
+ }
3855
+ if (this.pdfiumModule.FPDFText_GetLooseCharBox(textPagePtr, i, rectPtr)) {
3856
+ const left = this.pdfiumModule.pdfium.getValue(rectPtr, "float");
3857
+ const top = this.pdfiumModule.pdfium.getValue(rectPtr + 4, "float");
3858
+ const right = this.pdfiumModule.pdfium.getValue(rectPtr + 8, "float");
3859
+ const bottom = this.pdfiumModule.pdfium.getValue(rectPtr + 12, "float");
3860
+ if (left !== right && top !== bottom) {
3861
+ this.pdfiumModule.FPDF_PageToDevice(
3862
+ pageCtx.pagePtr,
3863
+ 0,
3864
+ 0,
3865
+ page.size.width,
3866
+ page.size.height,
3867
+ 0,
3868
+ left,
3869
+ top,
3870
+ dx1Ptr,
3871
+ dy1Ptr
3872
+ );
3873
+ this.pdfiumModule.FPDF_PageToDevice(
3874
+ pageCtx.pagePtr,
3875
+ 0,
3876
+ 0,
3877
+ page.size.width,
3878
+ page.size.height,
3879
+ 0,
3880
+ right,
3881
+ bottom,
3882
+ dx2Ptr,
3883
+ dy2Ptr
3884
+ );
3885
+ const x1 = this.pdfiumModule.pdfium.getValue(dx1Ptr, "i32");
3886
+ const y1 = this.pdfiumModule.pdfium.getValue(dy1Ptr, "i32");
3887
+ const x2 = this.pdfiumModule.pdfium.getValue(dx2Ptr, "i32");
3888
+ const y2 = this.pdfiumModule.pdfium.getValue(dy2Ptr, "i32");
3889
+ const cx = Math.min(x1, x2);
3890
+ const cy = Math.min(y1, y2);
3891
+ const cw = Math.abs(x2 - x1);
3892
+ const ch = Math.abs(y2 - y1);
3893
+ if (bounds === null) {
3894
+ bounds = { minX: cx, minY: cy, maxX: cx + cw, maxY: cy + ch };
3895
+ } else {
3896
+ bounds.minX = Math.min(bounds.minX, cx);
3897
+ bounds.minY = Math.min(bounds.minY, cy);
3898
+ bounds.maxX = Math.max(bounds.maxX, cx + cw);
3899
+ bounds.maxY = Math.max(bounds.maxY, cy + ch);
3900
+ }
3901
+ }
3902
+ }
3903
+ }
3904
+ flushRun(charCount);
3905
+ [rPtr, gPtr, bPtr, aPtr, rectPtr, dx1Ptr, dy1Ptr, dx2Ptr, dy2Ptr, italicAnglePtr].forEach(
3906
+ (p) => this.memoryManager.free(p)
3907
+ );
3908
+ pageCtx.release();
3909
+ this.logger.perf(LOG_SOURCE, LOG_CATEGORY, label, "End", doc.id);
3910
+ return PdfTaskHelper.resolve({ runs });
3911
+ }
3912
+ /**
3913
+ * Read font metadata from a text object handle via FPDFFont_* APIs.
3914
+ */
3915
+ readFontInfoFromTextObject(textObjPtr, italicAnglePtr) {
3916
+ const fontPtr = this.pdfiumModule.FPDFTextObj_GetFont(textObjPtr);
3917
+ let name = "";
3918
+ let familyName = "";
3919
+ let weight = 400;
3920
+ let italic = false;
3921
+ let monospaced = false;
3922
+ let embedded = false;
3923
+ if (fontPtr) {
3924
+ const nameLen = this.pdfiumModule.FPDFFont_GetBaseFontName(fontPtr, 0, 0);
3925
+ if (nameLen > 0) {
3926
+ const nameBuf = this.memoryManager.malloc(nameLen + 1);
3927
+ this.pdfiumModule.FPDFFont_GetBaseFontName(fontPtr, nameBuf, nameLen + 1);
3928
+ name = this.pdfiumModule.pdfium.UTF8ToString(nameBuf);
3929
+ this.memoryManager.free(nameBuf);
3930
+ }
3931
+ const famLen = this.pdfiumModule.FPDFFont_GetFamilyName(fontPtr, 0, 0);
3932
+ if (famLen > 0) {
3933
+ const famBuf = this.memoryManager.malloc(famLen + 1);
3934
+ this.pdfiumModule.FPDFFont_GetFamilyName(fontPtr, famBuf, famLen + 1);
3935
+ familyName = this.pdfiumModule.pdfium.UTF8ToString(famBuf);
3936
+ this.memoryManager.free(famBuf);
3937
+ }
3938
+ weight = this.pdfiumModule.FPDFFont_GetWeight(fontPtr);
3939
+ embedded = this.pdfiumModule.FPDFFont_GetIsEmbedded(fontPtr) !== 0;
3940
+ if (this.pdfiumModule.FPDFFont_GetItalicAngle(fontPtr, italicAnglePtr)) {
3941
+ const angle = this.pdfiumModule.pdfium.getValue(italicAnglePtr, "i32");
3942
+ italic = angle !== 0;
3943
+ }
3944
+ const flags = this.pdfiumModule.FPDFFont_GetFlags(fontPtr);
3945
+ monospaced = (flags & 1) !== 0;
3946
+ }
3947
+ return { name, familyName, weight, italic, monospaced, embedded };
3948
+ }
3768
3949
  /**
3769
3950
  * Extract glyph geometry + metadata for `charIndex`
3770
3951
  *
@@ -3781,14 +3962,31 @@ class PdfiumNative {
3781
3962
  const dx2Ptr = this.memoryManager.malloc(4);
3782
3963
  const dy2Ptr = this.memoryManager.malloc(4);
3783
3964
  const rectPtr = this.memoryManager.malloc(16);
3965
+ const tLeftPtr = this.memoryManager.malloc(8);
3966
+ const tRightPtr = this.memoryManager.malloc(8);
3967
+ const tBottomPtr = this.memoryManager.malloc(8);
3968
+ const tTopPtr = this.memoryManager.malloc(8);
3969
+ const allPtrs = [
3970
+ rectPtr,
3971
+ dx1Ptr,
3972
+ dy1Ptr,
3973
+ dx2Ptr,
3974
+ dy2Ptr,
3975
+ tLeftPtr,
3976
+ tRightPtr,
3977
+ tBottomPtr,
3978
+ tTopPtr
3979
+ ];
3784
3980
  let x = 0, y = 0, width = 0, height = 0, isSpace = false;
3981
+ let tightOrigin;
3982
+ let tightSize;
3785
3983
  if (this.pdfiumModule.FPDFText_GetLooseCharBox(textPagePtr, charIndex, rectPtr)) {
3786
3984
  const left = this.pdfiumModule.pdfium.getValue(rectPtr, "float");
3787
3985
  const top = this.pdfiumModule.pdfium.getValue(rectPtr + 4, "float");
3788
3986
  const right = this.pdfiumModule.pdfium.getValue(rectPtr + 8, "float");
3789
3987
  const bottom = this.pdfiumModule.pdfium.getValue(rectPtr + 12, "float");
3790
3988
  if (left === right || top === bottom) {
3791
- [rectPtr, dx1Ptr, dy1Ptr, dx2Ptr, dy2Ptr].forEach((p) => this.memoryManager.free(p));
3989
+ allPtrs.forEach((p) => this.memoryManager.free(p));
3792
3990
  return {
3793
3991
  origin: { x: 0, y: 0 },
3794
3992
  size: { width: 0, height: 0 },
@@ -3801,7 +3999,6 @@ class PdfiumNative {
3801
3999
  0,
3802
4000
  page.size.width,
3803
4001
  page.size.height,
3804
- /*rotate=*/
3805
4002
  0,
3806
4003
  left,
3807
4004
  top,
@@ -3814,7 +4011,6 @@ class PdfiumNative {
3814
4011
  0,
3815
4012
  page.size.width,
3816
4013
  page.size.height,
3817
- /*rotate=*/
3818
4014
  0,
3819
4015
  right,
3820
4016
  bottom,
@@ -3829,13 +4025,61 @@ class PdfiumNative {
3829
4025
  y = Math.min(y1, y2);
3830
4026
  width = Math.max(1, Math.abs(x2 - x1));
3831
4027
  height = Math.max(1, Math.abs(y2 - y1));
4028
+ if (this.pdfiumModule.FPDFText_GetCharBox(
4029
+ textPagePtr,
4030
+ charIndex,
4031
+ tLeftPtr,
4032
+ tRightPtr,
4033
+ tBottomPtr,
4034
+ tTopPtr
4035
+ )) {
4036
+ const tLeft = this.pdfiumModule.pdfium.getValue(tLeftPtr, "double");
4037
+ const tRight = this.pdfiumModule.pdfium.getValue(tRightPtr, "double");
4038
+ const tBottom = this.pdfiumModule.pdfium.getValue(tBottomPtr, "double");
4039
+ const tTop = this.pdfiumModule.pdfium.getValue(tTopPtr, "double");
4040
+ this.pdfiumModule.FPDF_PageToDevice(
4041
+ pagePtr,
4042
+ 0,
4043
+ 0,
4044
+ page.size.width,
4045
+ page.size.height,
4046
+ 0,
4047
+ tLeft,
4048
+ tTop,
4049
+ dx1Ptr,
4050
+ dy1Ptr
4051
+ );
4052
+ this.pdfiumModule.FPDF_PageToDevice(
4053
+ pagePtr,
4054
+ 0,
4055
+ 0,
4056
+ page.size.width,
4057
+ page.size.height,
4058
+ 0,
4059
+ tRight,
4060
+ tBottom,
4061
+ dx2Ptr,
4062
+ dy2Ptr
4063
+ );
4064
+ const tx1 = this.pdfiumModule.pdfium.getValue(dx1Ptr, "i32");
4065
+ const ty1 = this.pdfiumModule.pdfium.getValue(dy1Ptr, "i32");
4066
+ const tx2 = this.pdfiumModule.pdfium.getValue(dx2Ptr, "i32");
4067
+ const ty2 = this.pdfiumModule.pdfium.getValue(dy2Ptr, "i32");
4068
+ tightOrigin = { x: Math.min(tx1, tx2), y: Math.min(ty1, ty2) };
4069
+ tightSize = {
4070
+ width: Math.max(1, Math.abs(tx2 - tx1)),
4071
+ height: Math.max(1, Math.abs(ty2 - ty1))
4072
+ };
4073
+ }
3832
4074
  const uc = this.pdfiumModule.FPDFText_GetUnicode(textPagePtr, charIndex);
3833
4075
  isSpace = uc === 32;
3834
4076
  }
3835
- [rectPtr, dx1Ptr, dy1Ptr, dx2Ptr, dy2Ptr].forEach((p) => this.memoryManager.free(p));
4077
+ allPtrs.forEach((p) => this.memoryManager.free(p));
3836
4078
  return {
3837
4079
  origin: { x, y },
3838
4080
  size: { width, height },
4081
+ ...tightOrigin && { tightOrigin },
4082
+ ...tightSize && { tightSize },
3839
4083
  ...isSpace && { isSpace }
3840
4084
  };
3841
4085
  }
@@ -7488,7 +7732,7 @@ class PdfiumNative {
7488
7732
  right: 0,
7489
7733
  bottom: 0
7490
7734
  };
7491
- if (this.pdfiumModule.FPDFAnnot_GetRect(annotationPtr, pageRectPtr)) {
7735
+ if (this.pdfiumModule.EPDFAnnot_GetRect(annotationPtr, pageRectPtr)) {
7492
7736
  pageRect.left = this.pdfiumModule.pdfium.getValue(pageRectPtr, "float");
7493
7737
  pageRect.top = this.pdfiumModule.pdfium.getValue(pageRectPtr + 4, "float");
7494
7738
  pageRect.right = this.pdfiumModule.pdfium.getValue(pageRectPtr + 8, "float");
@@ -8020,4 +8264,4 @@ export {
8020
8264
  isValidCustomKey as i,
8021
8265
  readArrayBuffer as r
8022
8266
  };
8023
- //# sourceMappingURL=direct-engine-CvfzIn2D.js.map
8267
+ //# sourceMappingURL=direct-engine-CmsmBgWP.js.map