@embedpdf/engines 2.5.0 → 2.6.0

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,7 +1,7 @@
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
3
  import { P as PdfEngine } from "./pdf-engine-aJNoNMbt.js";
4
- import { b as browserImageDataToBlobConverter } from "./browser-qfUHZxQ6.js";
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);
7
7
  for (let i = 0; i < defaultLength; i++) {
@@ -1567,10 +1567,17 @@ class PdfiumNative {
1567
1567
  message: "can not set the rect of the annotation"
1568
1568
  });
1569
1569
  }
1570
+ const saveAnnotation = this.prepareAnnotationForSave(annotation);
1570
1571
  let isSucceed = false;
1571
- switch (annotation.type) {
1572
+ switch (saveAnnotation.type) {
1572
1573
  case PdfAnnotationSubtype.INK:
1573
- isSucceed = this.addInkStroke(doc, page, pageCtx.pagePtr, annotationPtr, annotation);
1574
+ isSucceed = this.addInkStroke(
1575
+ doc,
1576
+ page,
1577
+ pageCtx.pagePtr,
1578
+ annotationPtr,
1579
+ saveAnnotation
1580
+ );
1574
1581
  break;
1575
1582
  case PdfAnnotationSubtype.STAMP:
1576
1583
  isSucceed = this.addStampContent(
@@ -1579,26 +1586,50 @@ class PdfiumNative {
1579
1586
  page,
1580
1587
  pageCtx.pagePtr,
1581
1588
  annotationPtr,
1582
- annotation,
1589
+ saveAnnotation,
1583
1590
  context == null ? void 0 : context.imageData
1584
1591
  );
1585
1592
  break;
1586
1593
  case PdfAnnotationSubtype.TEXT:
1587
- isSucceed = this.addTextContent(page, pageCtx.pagePtr, annotationPtr, annotation);
1594
+ isSucceed = this.addTextContent(
1595
+ doc,
1596
+ page,
1597
+ pageCtx.pagePtr,
1598
+ annotationPtr,
1599
+ saveAnnotation
1600
+ );
1588
1601
  break;
1589
1602
  case PdfAnnotationSubtype.FREETEXT:
1590
- isSucceed = this.addFreeTextContent(page, pageCtx.pagePtr, annotationPtr, annotation);
1603
+ isSucceed = this.addFreeTextContent(
1604
+ doc,
1605
+ page,
1606
+ pageCtx.pagePtr,
1607
+ annotationPtr,
1608
+ saveAnnotation
1609
+ );
1591
1610
  break;
1592
1611
  case PdfAnnotationSubtype.LINE:
1593
- isSucceed = this.addLineContent(doc, page, pageCtx.pagePtr, annotationPtr, annotation);
1612
+ isSucceed = this.addLineContent(
1613
+ doc,
1614
+ page,
1615
+ pageCtx.pagePtr,
1616
+ annotationPtr,
1617
+ saveAnnotation
1618
+ );
1594
1619
  break;
1595
1620
  case PdfAnnotationSubtype.POLYLINE:
1596
1621
  case PdfAnnotationSubtype.POLYGON:
1597
- isSucceed = this.addPolyContent(doc, page, pageCtx.pagePtr, annotationPtr, annotation);
1622
+ isSucceed = this.addPolyContent(
1623
+ doc,
1624
+ page,
1625
+ pageCtx.pagePtr,
1626
+ annotationPtr,
1627
+ saveAnnotation
1628
+ );
1598
1629
  break;
1599
1630
  case PdfAnnotationSubtype.CIRCLE:
1600
1631
  case PdfAnnotationSubtype.SQUARE:
1601
- isSucceed = this.addShapeContent(page, pageCtx.pagePtr, annotationPtr, annotation);
1632
+ isSucceed = this.addShapeContent(doc, page, pageCtx.pagePtr, annotationPtr, saveAnnotation);
1602
1633
  break;
1603
1634
  case PdfAnnotationSubtype.UNDERLINE:
1604
1635
  case PdfAnnotationSubtype.STRIKEOUT:
@@ -1609,14 +1640,27 @@ class PdfiumNative {
1609
1640
  page,
1610
1641
  pageCtx.pagePtr,
1611
1642
  annotationPtr,
1612
- annotation
1643
+ saveAnnotation
1613
1644
  );
1614
1645
  break;
1615
1646
  case PdfAnnotationSubtype.LINK:
1616
- isSucceed = this.addLinkContent(ctx.docPtr, pageCtx.pagePtr, annotationPtr, annotation);
1647
+ isSucceed = this.addLinkContent(
1648
+ doc,
1649
+ page,
1650
+ ctx.docPtr,
1651
+ pageCtx.pagePtr,
1652
+ annotationPtr,
1653
+ saveAnnotation
1654
+ );
1617
1655
  break;
1618
1656
  case PdfAnnotationSubtype.REDACT:
1619
- isSucceed = this.addRedactContent(doc, page, pageCtx.pagePtr, annotationPtr, annotation);
1657
+ isSucceed = this.addRedactContent(
1658
+ doc,
1659
+ page,
1660
+ pageCtx.pagePtr,
1661
+ annotationPtr,
1662
+ saveAnnotation
1663
+ );
1620
1664
  break;
1621
1665
  }
1622
1666
  if (!isSucceed) {
@@ -1711,43 +1755,81 @@ class PdfiumNative {
1711
1755
  message: "failed to move annotation"
1712
1756
  });
1713
1757
  }
1758
+ const saveAnnotation = this.prepareAnnotationForSave(annotation);
1714
1759
  let ok = false;
1715
- switch (annotation.type) {
1760
+ switch (saveAnnotation.type) {
1716
1761
  /* ── Ink ─────────────────────────────────────────────────────────────── */
1717
1762
  case PdfAnnotationSubtype.INK: {
1718
1763
  if (!this.pdfiumModule.FPDFAnnot_RemoveInkList(annotPtr)) break;
1719
- ok = this.addInkStroke(doc, page, pageCtx.pagePtr, annotPtr, annotation);
1764
+ ok = this.addInkStroke(
1765
+ doc,
1766
+ page,
1767
+ pageCtx.pagePtr,
1768
+ annotPtr,
1769
+ saveAnnotation
1770
+ );
1720
1771
  break;
1721
1772
  }
1722
1773
  /* ── Stamp ───────────────────────────────────────────────────────────── */
1723
1774
  case PdfAnnotationSubtype.STAMP: {
1724
- ok = this.addStampContent(doc, ctx.docPtr, page, pageCtx.pagePtr, annotPtr, annotation);
1775
+ ok = this.addStampContent(
1776
+ doc,
1777
+ ctx.docPtr,
1778
+ page,
1779
+ pageCtx.pagePtr,
1780
+ annotPtr,
1781
+ saveAnnotation
1782
+ );
1725
1783
  break;
1726
1784
  }
1727
1785
  case PdfAnnotationSubtype.TEXT: {
1728
- ok = this.addTextContent(page, pageCtx.pagePtr, annotPtr, annotation);
1786
+ ok = this.addTextContent(
1787
+ doc,
1788
+ page,
1789
+ pageCtx.pagePtr,
1790
+ annotPtr,
1791
+ saveAnnotation
1792
+ );
1729
1793
  break;
1730
1794
  }
1731
1795
  /* ── Free text ────────────────────────────────────────────────────────── */
1732
1796
  case PdfAnnotationSubtype.FREETEXT: {
1733
- ok = this.addFreeTextContent(page, pageCtx.pagePtr, annotPtr, annotation);
1797
+ ok = this.addFreeTextContent(
1798
+ doc,
1799
+ page,
1800
+ pageCtx.pagePtr,
1801
+ annotPtr,
1802
+ saveAnnotation
1803
+ );
1734
1804
  break;
1735
1805
  }
1736
1806
  /* ── Shape ───────────────────────────────────────────────────────────── */
1737
1807
  case PdfAnnotationSubtype.CIRCLE:
1738
1808
  case PdfAnnotationSubtype.SQUARE: {
1739
- ok = this.addShapeContent(page, pageCtx.pagePtr, annotPtr, annotation);
1809
+ ok = this.addShapeContent(doc, page, pageCtx.pagePtr, annotPtr, saveAnnotation);
1740
1810
  break;
1741
1811
  }
1742
1812
  /* ── Line ─────────────────────────────────────────────────────────────── */
1743
1813
  case PdfAnnotationSubtype.LINE: {
1744
- ok = this.addLineContent(doc, page, pageCtx.pagePtr, annotPtr, annotation);
1814
+ ok = this.addLineContent(
1815
+ doc,
1816
+ page,
1817
+ pageCtx.pagePtr,
1818
+ annotPtr,
1819
+ saveAnnotation
1820
+ );
1745
1821
  break;
1746
1822
  }
1747
1823
  /* ── Polygon / Polyline ───────────────────────────────────────────────── */
1748
1824
  case PdfAnnotationSubtype.POLYGON:
1749
1825
  case PdfAnnotationSubtype.POLYLINE: {
1750
- ok = this.addPolyContent(doc, page, pageCtx.pagePtr, annotPtr, annotation);
1826
+ ok = this.addPolyContent(
1827
+ doc,
1828
+ page,
1829
+ pageCtx.pagePtr,
1830
+ annotPtr,
1831
+ saveAnnotation
1832
+ );
1751
1833
  break;
1752
1834
  }
1753
1835
  /* ── Text-markup family ──────────────────────────────────────────────── */
@@ -1755,17 +1837,36 @@ class PdfiumNative {
1755
1837
  case PdfAnnotationSubtype.UNDERLINE:
1756
1838
  case PdfAnnotationSubtype.STRIKEOUT:
1757
1839
  case PdfAnnotationSubtype.SQUIGGLY: {
1758
- ok = this.addTextMarkupContent(doc, page, pageCtx.pagePtr, annotPtr, annotation);
1840
+ ok = this.addTextMarkupContent(
1841
+ doc,
1842
+ page,
1843
+ pageCtx.pagePtr,
1844
+ annotPtr,
1845
+ saveAnnotation
1846
+ );
1759
1847
  break;
1760
1848
  }
1761
1849
  /* ── Link ─────────────────────────────────────────────────────────────── */
1762
1850
  case PdfAnnotationSubtype.LINK: {
1763
- ok = this.addLinkContent(ctx.docPtr, pageCtx.pagePtr, annotPtr, annotation);
1851
+ ok = this.addLinkContent(
1852
+ doc,
1853
+ page,
1854
+ ctx.docPtr,
1855
+ pageCtx.pagePtr,
1856
+ annotPtr,
1857
+ saveAnnotation
1858
+ );
1764
1859
  break;
1765
1860
  }
1766
1861
  /* ── Redact ───────────────────────────────────────────────────────────── */
1767
1862
  case PdfAnnotationSubtype.REDACT: {
1768
- ok = this.addRedactContent(doc, page, pageCtx.pagePtr, annotPtr, annotation);
1863
+ ok = this.addRedactContent(
1864
+ doc,
1865
+ page,
1866
+ pageCtx.pagePtr,
1867
+ annotPtr,
1868
+ saveAnnotation
1869
+ );
1769
1870
  break;
1770
1871
  }
1771
1872
  /* ── Unsupported edits – fall through to error ───────────────────────── */
@@ -2749,7 +2850,7 @@ class PdfiumNative {
2749
2850
  *
2750
2851
  * @private
2751
2852
  */
2752
- addTextContent(page, pagePtr, annotationPtr, annotation) {
2853
+ addTextContent(doc, page, pagePtr, annotationPtr, annotation) {
2753
2854
  if (!this.setAnnotationIcon(annotationPtr, annotation.icon || PdfAnnotationIcon.Comment)) {
2754
2855
  return false;
2755
2856
  }
@@ -2764,7 +2865,7 @@ class PdfiumNative {
2764
2865
  return false;
2765
2866
  }
2766
2867
  }
2767
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
2868
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
2768
2869
  }
2769
2870
  /**
2770
2871
  * Add free text content to annotation
@@ -2776,7 +2877,7 @@ class PdfiumNative {
2776
2877
  *
2777
2878
  * @private
2778
2879
  */
2779
- addFreeTextContent(page, pagePtr, annotationPtr, annotation) {
2880
+ addFreeTextContent(doc, page, pagePtr, annotationPtr, annotation) {
2780
2881
  if (!this.setBorderStyle(annotationPtr, PdfAnnotationBorderStyle.SOLID, 0)) {
2781
2882
  return false;
2782
2883
  }
@@ -2808,7 +2909,7 @@ class PdfiumNative {
2808
2909
  } else if (!this.setAnnotationColor(annotationPtr, bgColor ?? "#FFFFFF", PdfAnnotationColorType.Color)) {
2809
2910
  return false;
2810
2911
  }
2811
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
2912
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
2812
2913
  }
2813
2914
  /**
2814
2915
  * Set the rect of specified annotation
@@ -2834,7 +2935,7 @@ class PdfiumNative {
2834
2935
  if (!this.setAnnotationColor(annotationPtr, strokeColor, PdfAnnotationColorType.Color)) {
2835
2936
  return false;
2836
2937
  }
2837
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
2938
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
2838
2939
  }
2839
2940
  /**
2840
2941
  * Add line content to annotation
@@ -2894,7 +2995,7 @@ class PdfiumNative {
2894
2995
  )) {
2895
2996
  return false;
2896
2997
  }
2897
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
2998
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
2898
2999
  }
2899
3000
  /**
2900
3001
  * Add polygon or polyline content to annotation
@@ -2948,7 +3049,7 @@ class PdfiumNative {
2948
3049
  )) {
2949
3050
  return false;
2950
3051
  }
2951
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
3052
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
2952
3053
  }
2953
3054
  /**
2954
3055
  * Add link content (action or destination) to a link annotation
@@ -2960,7 +3061,7 @@ class PdfiumNative {
2960
3061
  *
2961
3062
  * @private
2962
3063
  */
2963
- addLinkContent(docPtr, pagePtr, annotationPtr, annotation) {
3064
+ addLinkContent(doc, page, docPtr, pagePtr, annotationPtr, annotation) {
2964
3065
  const style = annotation.strokeStyle ?? PdfAnnotationBorderStyle.UNDERLINE;
2965
3066
  const width = annotation.strokeWidth ?? 2;
2966
3067
  if (!this.setBorderStyle(annotationPtr, style, width)) {
@@ -2983,7 +3084,7 @@ class PdfiumNative {
2983
3084
  return false;
2984
3085
  }
2985
3086
  }
2986
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
3087
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
2987
3088
  }
2988
3089
  /**
2989
3090
  * Add shape content to annotation
@@ -2995,7 +3096,7 @@ class PdfiumNative {
2995
3096
  *
2996
3097
  * @private
2997
3098
  */
2998
- addShapeContent(page, pagePtr, annotationPtr, annotation) {
3099
+ addShapeContent(doc, page, pagePtr, annotationPtr, annotation) {
2999
3100
  if (!this.setBorderStyle(annotationPtr, annotation.strokeStyle, annotation.strokeWidth)) {
3000
3101
  return false;
3001
3102
  }
@@ -3023,7 +3124,7 @@ class PdfiumNative {
3023
3124
  )) {
3024
3125
  return false;
3025
3126
  }
3026
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
3127
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
3027
3128
  }
3028
3129
  /**
3029
3130
  * Add highlight content to annotation
@@ -3045,7 +3146,7 @@ class PdfiumNative {
3045
3146
  if (!this.setAnnotationColor(annotationPtr, strokeColor, PdfAnnotationColorType.Color)) {
3046
3147
  return false;
3047
3148
  }
3048
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
3149
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
3049
3150
  }
3050
3151
  /**
3051
3152
  * Add content to redact annotation
@@ -3112,7 +3213,7 @@ class PdfiumNative {
3112
3213
  if (annotation.textAlign !== void 0 && !this.setAnnotationTextAlignment(annotationPtr, annotation.textAlign)) {
3113
3214
  return false;
3114
3215
  }
3115
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
3216
+ return this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation);
3116
3217
  }
3117
3218
  /**
3118
3219
  * Add contents to stamp annotation
@@ -3142,10 +3243,10 @@ class PdfiumNative {
3142
3243
  return false;
3143
3244
  }
3144
3245
  }
3145
- if (!this.pdfiumModule.EPDFAnnot_UpdateAppearanceToRect(annotationPtr, PdfStampFit.Cover)) {
3246
+ if (!this.applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation)) {
3146
3247
  return false;
3147
3248
  }
3148
- return this.applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation);
3249
+ return !!this.pdfiumModule.EPDFAnnot_UpdateAppearanceToRect(annotationPtr, PdfStampFit.Cover);
3149
3250
  }
3150
3251
  /**
3151
3252
  * Add image object to annotation
@@ -4045,8 +4146,62 @@ class PdfiumNative {
4045
4146
  }
4046
4147
  break;
4047
4148
  }
4149
+ if (annotation) {
4150
+ annotation = this.reverseRotateAnnotationOnLoad(annotation);
4151
+ }
4048
4152
  return annotation;
4049
4153
  }
4154
+ /**
4155
+ * On load, if a vertex-type annotation has rotation metadata in EPDFCustom,
4156
+ * reverse-rotate the PDF's physically rotated vertices by -rotation to recover
4157
+ * the unrotated vertices for runtime editing.
4158
+ */
4159
+ reverseRotateAnnotationOnLoad(annotation) {
4160
+ const rotation = annotation.rotation;
4161
+ const unrotatedRect = annotation.unrotatedRect;
4162
+ if (!rotation || rotation === 0 || !unrotatedRect) {
4163
+ return annotation;
4164
+ }
4165
+ const center = {
4166
+ x: unrotatedRect.origin.x + unrotatedRect.size.width / 2,
4167
+ y: unrotatedRect.origin.y + unrotatedRect.size.height / 2
4168
+ };
4169
+ switch (annotation.type) {
4170
+ case PdfAnnotationSubtype.INK: {
4171
+ const ink = annotation;
4172
+ const unrotatedInkList = ink.inkList.map((stroke) => ({
4173
+ points: stroke.points.map((p) => this.rotatePointForSave(p, center, -rotation))
4174
+ }));
4175
+ return { ...ink, inkList: unrotatedInkList };
4176
+ }
4177
+ case PdfAnnotationSubtype.LINE: {
4178
+ const line = annotation;
4179
+ return {
4180
+ ...line,
4181
+ linePoints: {
4182
+ start: this.rotatePointForSave(line.linePoints.start, center, -rotation),
4183
+ end: this.rotatePointForSave(line.linePoints.end, center, -rotation)
4184
+ }
4185
+ };
4186
+ }
4187
+ case PdfAnnotationSubtype.POLYGON: {
4188
+ const poly = annotation;
4189
+ return {
4190
+ ...poly,
4191
+ vertices: poly.vertices.map((v) => this.rotatePointForSave(v, center, -rotation))
4192
+ };
4193
+ }
4194
+ case PdfAnnotationSubtype.POLYLINE: {
4195
+ const polyline = annotation;
4196
+ return {
4197
+ ...polyline,
4198
+ vertices: polyline.vertices.map((v) => this.rotatePointForSave(v, center, -rotation))
4199
+ };
4200
+ }
4201
+ default:
4202
+ return annotation;
4203
+ }
4204
+ }
4050
4205
  /**
4051
4206
  * Return the colour stored directly in the annotation dictionary's `/C` entry.
4052
4207
  *
@@ -4139,6 +4294,125 @@ class PdfiumNative {
4139
4294
  const pdfOpacity = webOpacityToPdfAlpha(opacity);
4140
4295
  return this.pdfiumModule.EPDFAnnot_SetOpacity(annotationPtr, pdfOpacity & 255);
4141
4296
  }
4297
+ /**
4298
+ * Get the rotation angle (in degrees) from the annotation's /Rotate entry.
4299
+ * Returns 0 if no rotation is set or on error.
4300
+ *
4301
+ * @param annotationPtr - pointer to the annotation
4302
+ * @returns rotation in degrees (0 if not set)
4303
+ */
4304
+ getAnnotationRotation(annotationPtr) {
4305
+ const rotationPtr = this.memoryManager.malloc(4);
4306
+ const ok = this.pdfiumModule.EPDFAnnot_GetRotate(annotationPtr, rotationPtr);
4307
+ if (!ok) {
4308
+ this.memoryManager.free(rotationPtr);
4309
+ return 0;
4310
+ }
4311
+ const rotation = this.pdfiumModule.pdfium.getValue(rotationPtr, "float");
4312
+ this.memoryManager.free(rotationPtr);
4313
+ return rotation;
4314
+ }
4315
+ /**
4316
+ * Set the rotation angle (in degrees) on the annotation's /Rotate entry.
4317
+ * A value of 0 removes the /Rotate key.
4318
+ *
4319
+ * @param annotationPtr - pointer to the annotation
4320
+ * @param rotation - rotation in degrees (clockwise)
4321
+ * @returns true on success
4322
+ */
4323
+ setAnnotationRotation(annotationPtr, rotation) {
4324
+ return !!this.pdfiumModule.EPDFAnnot_SetRotate(annotationPtr, rotation);
4325
+ }
4326
+ /**
4327
+ * Get the EmbedPDF extended rotation (in degrees) from the annotation's
4328
+ * /EPDFRotate entry. Returns 0 if not set or on error.
4329
+ *
4330
+ * @param annotationPtr - pointer to the annotation
4331
+ * @returns rotation in degrees (0 if not set)
4332
+ */
4333
+ getAnnotExtendedRotation(annotationPtr) {
4334
+ const rotationPtr = this.memoryManager.malloc(4);
4335
+ const ok = this.pdfiumModule.EPDFAnnot_GetExtendedRotation(annotationPtr, rotationPtr);
4336
+ if (!ok) {
4337
+ this.memoryManager.free(rotationPtr);
4338
+ return 0;
4339
+ }
4340
+ const rotation = this.pdfiumModule.pdfium.getValue(rotationPtr, "float");
4341
+ this.memoryManager.free(rotationPtr);
4342
+ return rotation;
4343
+ }
4344
+ /**
4345
+ * Set the EmbedPDF extended rotation (in degrees) on the annotation's
4346
+ * /EPDFRotate entry. A value of 0 removes the key.
4347
+ *
4348
+ * @param annotationPtr - pointer to the annotation
4349
+ * @param rotation - rotation in degrees
4350
+ * @returns true on success
4351
+ */
4352
+ setAnnotExtendedRotation(annotationPtr, rotation) {
4353
+ return !!this.pdfiumModule.EPDFAnnot_SetExtendedRotation(annotationPtr, rotation);
4354
+ }
4355
+ /**
4356
+ * Read the EmbedPDF unrotated rect from the annotation's /EPDFUnrotatedRect
4357
+ * entry. Returns the raw page-space rect (same format as `readPageAnnoRect`)
4358
+ * or null if not set.
4359
+ *
4360
+ * @param annotationPtr - pointer to the annotation
4361
+ * @returns raw `{ left, top, right, bottom }` in page coords, or null
4362
+ */
4363
+ readAnnotUnrotatedRect(annotationPtr) {
4364
+ const rectPtr = this.memoryManager.malloc(4 * 4);
4365
+ const ok = this.pdfiumModule.EPDFAnnot_GetUnrotatedRect(annotationPtr, rectPtr);
4366
+ if (!ok) {
4367
+ this.memoryManager.free(rectPtr);
4368
+ return null;
4369
+ }
4370
+ const left = this.pdfiumModule.pdfium.getValue(rectPtr, "float");
4371
+ const top = this.pdfiumModule.pdfium.getValue(rectPtr + 4, "float");
4372
+ const right = this.pdfiumModule.pdfium.getValue(rectPtr + 8, "float");
4373
+ const bottom = this.pdfiumModule.pdfium.getValue(rectPtr + 12, "float");
4374
+ this.memoryManager.free(rectPtr);
4375
+ if (left === 0 && top === 0 && right === 0 && bottom === 0) {
4376
+ return null;
4377
+ }
4378
+ return { left, top, right, bottom };
4379
+ }
4380
+ /**
4381
+ * Write the EmbedPDF unrotated rect (/EPDFUnrotatedRect) for an annotation.
4382
+ * Accepts a device-space `Rect` and converts to page coordinates internally,
4383
+ * following the same pattern as `setPageAnnoRect`.
4384
+ *
4385
+ * @param doc - pdf document object
4386
+ * @param page - pdf page object
4387
+ * @param annotPtr - pointer to the annotation
4388
+ * @param rect - device-space rect to store as the unrotated rect
4389
+ * @returns true on success
4390
+ */
4391
+ setAnnotUnrotatedRect(doc, page, annotPtr, rect) {
4392
+ const x0d = Math.floor(rect.origin.x);
4393
+ const y0d = Math.floor(rect.origin.y);
4394
+ const x1d = Math.floor(rect.origin.x + rect.size.width);
4395
+ const y1d = Math.floor(rect.origin.y + rect.size.height);
4396
+ const TL = this.convertDevicePointToPagePoint(doc, page, { x: x0d, y: y0d });
4397
+ const TR = this.convertDevicePointToPagePoint(doc, page, { x: x1d, y: y0d });
4398
+ const BR = this.convertDevicePointToPagePoint(doc, page, { x: x1d, y: y1d });
4399
+ const BL = this.convertDevicePointToPagePoint(doc, page, { x: x0d, y: y1d });
4400
+ let left = Math.min(TL.x, TR.x, BR.x, BL.x);
4401
+ let right = Math.max(TL.x, TR.x, BR.x, BL.x);
4402
+ let bottom = Math.min(TL.y, TR.y, BR.y, BL.y);
4403
+ let top = Math.max(TL.y, TR.y, BR.y, BL.y);
4404
+ if (left > right) [left, right] = [right, left];
4405
+ if (bottom > top) [bottom, top] = [top, bottom];
4406
+ const ptr = this.memoryManager.malloc(16);
4407
+ const pdf = this.pdfiumModule.pdfium;
4408
+ pdf.setValue(ptr + 0, left, "float");
4409
+ pdf.setValue(ptr + 4, top, "float");
4410
+ pdf.setValue(ptr + 8, right, "float");
4411
+ pdf.setValue(ptr + 12, bottom, "float");
4412
+ const ok = this.pdfiumModule.EPDFAnnot_SetUnrotatedRect(annotPtr, ptr);
4413
+ this.memoryManager.free(ptr);
4414
+ return !!ok;
4415
+ }
4142
4416
  /**
4143
4417
  * Fetch the `/Q` text-alignment value from a **FreeText** annotation.
4144
4418
  *
@@ -4288,10 +4562,11 @@ class PdfiumNative {
4288
4562
  * @returns `true` on success
4289
4563
  */
4290
4564
  setAnnotationDefaultAppearance(annotationPtr, font, fontSize, color) {
4565
+ const resolvedFont = font === PdfStandardFont.Unknown ? PdfStandardFont.Helvetica : font;
4291
4566
  const { red, green, blue } = webColorToPdfColor(color);
4292
4567
  return !!this.pdfiumModule.EPDFAnnot_SetDefaultAppearance(
4293
4568
  annotationPtr,
4294
- font,
4569
+ resolvedFont,
4295
4570
  fontSize,
4296
4571
  red & 255,
4297
4572
  green & 255,
@@ -4745,7 +5020,7 @@ class PdfiumNative {
4745
5020
  ptr,
4746
5021
  count,
4747
5022
  recurseForms ? true : false,
4748
- drawBlackBoxes ? true : false
5023
+ false
4749
5024
  );
4750
5025
  } finally {
4751
5026
  this.memoryManager.free(ptr);
@@ -4998,7 +5273,7 @@ class PdfiumNative {
4998
5273
  state,
4999
5274
  stateModel,
5000
5275
  icon,
5001
- ...this.readBaseAnnotationProperties(annotationPtr)
5276
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5002
5277
  };
5003
5278
  }
5004
5279
  /**
@@ -5037,7 +5312,7 @@ class PdfiumNative {
5037
5312
  textAlign,
5038
5313
  defaultStyle,
5039
5314
  richContent,
5040
- ...this.readBaseAnnotationProperties(annotationPtr)
5315
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5041
5316
  };
5042
5317
  }
5043
5318
  /**
@@ -5085,7 +5360,7 @@ class PdfiumNative {
5085
5360
  strokeWidth,
5086
5361
  strokeStyle,
5087
5362
  strokeDashArray,
5088
- ...this.readBaseAnnotationProperties(annotationPtr)
5363
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5089
5364
  };
5090
5365
  }
5091
5366
  /**
@@ -5108,7 +5383,7 @@ class PdfiumNative {
5108
5383
  type: PdfAnnotationSubtype.WIDGET,
5109
5384
  rect,
5110
5385
  field,
5111
- ...this.readBaseAnnotationProperties(annotationPtr)
5386
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5112
5387
  };
5113
5388
  }
5114
5389
  /**
@@ -5128,7 +5403,7 @@ class PdfiumNative {
5128
5403
  id: index,
5129
5404
  type: PdfAnnotationSubtype.FILEATTACHMENT,
5130
5405
  rect,
5131
- ...this.readBaseAnnotationProperties(annotationPtr)
5406
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5132
5407
  };
5133
5408
  }
5134
5409
  /**
@@ -5162,7 +5437,7 @@ class PdfiumNative {
5162
5437
  opacity,
5163
5438
  strokeWidth: strokeWidth === 0 ? 1 : strokeWidth,
5164
5439
  inkList,
5165
- ...this.readBaseAnnotationProperties(annotationPtr)
5440
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5166
5441
  };
5167
5442
  }
5168
5443
  /**
@@ -5211,7 +5486,7 @@ class PdfiumNative {
5211
5486
  strokeStyle,
5212
5487
  strokeDashArray,
5213
5488
  vertices,
5214
- ...this.readBaseAnnotationProperties(annotationPtr)
5489
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5215
5490
  };
5216
5491
  }
5217
5492
  /**
@@ -5255,7 +5530,7 @@ class PdfiumNative {
5255
5530
  strokeDashArray,
5256
5531
  lineEndings,
5257
5532
  vertices,
5258
- ...this.readBaseAnnotationProperties(annotationPtr)
5533
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5259
5534
  };
5260
5535
  }
5261
5536
  /**
@@ -5302,7 +5577,7 @@ class PdfiumNative {
5302
5577
  start: PdfAnnotationLineEnding.None,
5303
5578
  end: PdfAnnotationLineEnding.None
5304
5579
  },
5305
- ...this.readBaseAnnotationProperties(annotationPtr)
5580
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5306
5581
  };
5307
5582
  }
5308
5583
  /**
@@ -5332,7 +5607,7 @@ class PdfiumNative {
5332
5607
  color: strokeColor,
5333
5608
  // deprecated alias
5334
5609
  opacity,
5335
- ...this.readBaseAnnotationProperties(annotationPtr)
5610
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5336
5611
  };
5337
5612
  }
5338
5613
  /**
@@ -5362,7 +5637,7 @@ class PdfiumNative {
5362
5637
  color: strokeColor,
5363
5638
  // deprecated alias
5364
5639
  opacity,
5365
- ...this.readBaseAnnotationProperties(annotationPtr)
5640
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5366
5641
  };
5367
5642
  }
5368
5643
  /**
@@ -5392,7 +5667,7 @@ class PdfiumNative {
5392
5667
  color: strokeColor,
5393
5668
  // deprecated alias
5394
5669
  opacity,
5395
- ...this.readBaseAnnotationProperties(annotationPtr)
5670
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5396
5671
  };
5397
5672
  }
5398
5673
  /**
@@ -5422,7 +5697,7 @@ class PdfiumNative {
5422
5697
  color: strokeColor,
5423
5698
  // deprecated alias
5424
5699
  opacity,
5425
- ...this.readBaseAnnotationProperties(annotationPtr)
5700
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5426
5701
  };
5427
5702
  }
5428
5703
  /**
@@ -5442,7 +5717,7 @@ class PdfiumNative {
5442
5717
  id: index,
5443
5718
  type: PdfAnnotationSubtype.CARET,
5444
5719
  rect,
5445
- ...this.readBaseAnnotationProperties(annotationPtr)
5720
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5446
5721
  };
5447
5722
  }
5448
5723
  /**
@@ -5485,7 +5760,7 @@ class PdfiumNative {
5485
5760
  fontSize: da == null ? void 0 : da.fontSize,
5486
5761
  fontColor: da == null ? void 0 : da.fontColor,
5487
5762
  textAlign,
5488
- ...this.readBaseAnnotationProperties(annotationPtr)
5763
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5489
5764
  };
5490
5765
  }
5491
5766
  /**
@@ -5505,7 +5780,7 @@ class PdfiumNative {
5505
5780
  id: index,
5506
5781
  type: PdfAnnotationSubtype.STAMP,
5507
5782
  rect,
5508
- ...this.readBaseAnnotationProperties(annotationPtr)
5783
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5509
5784
  };
5510
5785
  }
5511
5786
  /**
@@ -5768,7 +6043,7 @@ class PdfiumNative {
5768
6043
  strokeColor: strokeColor ?? "#FF0000",
5769
6044
  strokeStyle,
5770
6045
  ...strokeDashArray !== void 0 && { strokeDashArray },
5771
- ...this.readBaseAnnotationProperties(annotationPtr)
6046
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5772
6047
  };
5773
6048
  }
5774
6049
  /**
@@ -5808,7 +6083,7 @@ class PdfiumNative {
5808
6083
  strokeWidth,
5809
6084
  strokeStyle,
5810
6085
  ...strokeDashArray !== void 0 && { strokeDashArray },
5811
- ...this.readBaseAnnotationProperties(annotationPtr)
6086
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5812
6087
  };
5813
6088
  }
5814
6089
  /**
@@ -5829,7 +6104,7 @@ class PdfiumNative {
5829
6104
  id: index,
5830
6105
  type,
5831
6106
  rect,
5832
- ...this.readBaseAnnotationProperties(annotationPtr)
6107
+ ...this.readBaseAnnotationProperties(doc, page, annotationPtr)
5833
6108
  };
5834
6109
  }
5835
6110
  /**
@@ -5861,6 +6136,73 @@ class PdfiumNative {
5861
6136
  if (!parentPtr) return false;
5862
6137
  return this.pdfiumModule.EPDFAnnot_SetLinkedAnnot(annotationPtr, "IRT", parentPtr);
5863
6138
  }
6139
+ /**
6140
+ * Rotate a point around a center by the given angle in degrees.
6141
+ * Used to rotate vertices for PDF storage.
6142
+ */
6143
+ rotatePointForSave(point, center, angleDegrees) {
6144
+ const rad = angleDegrees * Math.PI / 180;
6145
+ const cos = Math.cos(rad);
6146
+ const sin = Math.sin(rad);
6147
+ const dx = point.x - center.x;
6148
+ const dy = point.y - center.y;
6149
+ return {
6150
+ x: center.x + dx * cos - dy * sin,
6151
+ y: center.y + dx * sin + dy * cos
6152
+ };
6153
+ }
6154
+ /**
6155
+ * Prepare an annotation for saving to PDF.
6156
+ * For vertex types (ink, line, polygon, polyline) with rotation,
6157
+ * physically rotates the vertices by +rotation so that other PDF viewers
6158
+ * see the correct visual result. Our viewer reverse-rotates on load.
6159
+ */
6160
+ prepareAnnotationForSave(annotation) {
6161
+ const rotation = annotation.rotation;
6162
+ const unrotatedRect = annotation.unrotatedRect;
6163
+ if (!rotation || rotation === 0 || !unrotatedRect) {
6164
+ return annotation;
6165
+ }
6166
+ const center = {
6167
+ x: unrotatedRect.origin.x + unrotatedRect.size.width / 2,
6168
+ y: unrotatedRect.origin.y + unrotatedRect.size.height / 2
6169
+ };
6170
+ switch (annotation.type) {
6171
+ case PdfAnnotationSubtype.INK: {
6172
+ const ink = annotation;
6173
+ const rotatedInkList = ink.inkList.map((stroke) => ({
6174
+ points: stroke.points.map((p) => this.rotatePointForSave(p, center, rotation))
6175
+ }));
6176
+ return { ...ink, inkList: rotatedInkList };
6177
+ }
6178
+ case PdfAnnotationSubtype.LINE: {
6179
+ const line = annotation;
6180
+ return {
6181
+ ...line,
6182
+ linePoints: {
6183
+ start: this.rotatePointForSave(line.linePoints.start, center, rotation),
6184
+ end: this.rotatePointForSave(line.linePoints.end, center, rotation)
6185
+ }
6186
+ };
6187
+ }
6188
+ case PdfAnnotationSubtype.POLYGON: {
6189
+ const poly = annotation;
6190
+ return {
6191
+ ...poly,
6192
+ vertices: poly.vertices.map((v) => this.rotatePointForSave(v, center, rotation))
6193
+ };
6194
+ }
6195
+ case PdfAnnotationSubtype.POLYLINE: {
6196
+ const polyline = annotation;
6197
+ return {
6198
+ ...polyline,
6199
+ vertices: polyline.vertices.map((v) => this.rotatePointForSave(v, center, rotation))
6200
+ };
6201
+ }
6202
+ default:
6203
+ return annotation;
6204
+ }
6205
+ }
5864
6206
  /**
5865
6207
  * Apply all base annotation properties from PdfAnnotationObjectBase.
5866
6208
  * The setInReplyToId and setReplyType functions handle clearing when undefined.
@@ -5870,7 +6212,7 @@ class PdfiumNative {
5870
6212
  * @param annotation - the annotation object containing properties to apply
5871
6213
  * @returns `true` on success
5872
6214
  */
5873
- applyBaseAnnotationProperties(pagePtr, annotationPtr, annotation) {
6215
+ applyBaseAnnotationProperties(doc, page, pagePtr, annotationPtr, annotation) {
5874
6216
  if (!this.setAnnotString(annotationPtr, "T", annotation.author || "")) {
5875
6217
  return false;
5876
6218
  }
@@ -5892,10 +6234,31 @@ class PdfiumNative {
5892
6234
  return false;
5893
6235
  }
5894
6236
  }
5895
- if (annotation.custom) {
5896
- if (!this.setAnnotCustom(annotationPtr, annotation.custom)) {
6237
+ const existingCustom = this.getAnnotCustom(annotationPtr) ?? {};
6238
+ const customData = {
6239
+ ...existingCustom,
6240
+ ...annotation.custom ?? {}
6241
+ };
6242
+ delete customData.unrotatedRect;
6243
+ delete customData.rotation;
6244
+ const hasCustomData = Object.keys(customData).length > 0;
6245
+ if (hasCustomData) {
6246
+ if (!this.setAnnotCustom(annotationPtr, customData)) {
5897
6247
  return false;
5898
6248
  }
6249
+ } else if (Object.keys(existingCustom).length > 0) {
6250
+ if (!this.setAnnotCustom(annotationPtr, null)) {
6251
+ return false;
6252
+ }
6253
+ }
6254
+ if (annotation.rotation !== void 0) {
6255
+ const pdfRotation = annotation.rotation ? (360 - annotation.rotation) % 360 : 0;
6256
+ this.setAnnotExtendedRotation(annotationPtr, pdfRotation);
6257
+ }
6258
+ if (annotation.unrotatedRect) {
6259
+ this.setAnnotUnrotatedRect(doc, page, annotationPtr, annotation.unrotatedRect);
6260
+ } else if (annotation.rotation && annotation.rotation !== 0) {
6261
+ this.setAnnotUnrotatedRect(doc, page, annotationPtr, annotation.rect);
5899
6262
  }
5900
6263
  if (!this.setInReplyToId(pagePtr, annotationPtr, annotation.inReplyToId)) {
5901
6264
  return false;
@@ -5909,10 +6272,12 @@ class PdfiumNative {
5909
6272
  * Read all base annotation properties from PdfAnnotationObjectBase.
5910
6273
  * Returns an object that can be spread into the annotation return value.
5911
6274
  *
6275
+ * @param doc - pdf document object
6276
+ * @param page - pdf page object
5912
6277
  * @param annotationPtr - pointer to annotation object
5913
6278
  * @returns object with base annotation properties
5914
6279
  */
5915
- readBaseAnnotationProperties(annotationPtr) {
6280
+ readBaseAnnotationProperties(doc, page, annotationPtr) {
5916
6281
  const author = this.getAnnotString(annotationPtr, "T");
5917
6282
  const contents = this.getAnnotString(annotationPtr, "Contents") || "";
5918
6283
  const modified = this.getAnnotationDate(annotationPtr, "M");
@@ -5921,6 +6286,10 @@ class PdfiumNative {
5921
6286
  const custom = this.getAnnotCustom(annotationPtr);
5922
6287
  const inReplyToId = this.getInReplyToId(annotationPtr);
5923
6288
  const replyType = this.getReplyType(annotationPtr);
6289
+ const pdfRotation = this.getAnnotExtendedRotation(annotationPtr);
6290
+ const rotation = pdfRotation !== 0 ? (360 - pdfRotation) % 360 : 0;
6291
+ const rawUnrotatedRect = this.readAnnotUnrotatedRect(annotationPtr);
6292
+ const unrotatedRect = rawUnrotatedRect ? this.convertPageRectToDeviceRect(doc, page, rawUnrotatedRect) : void 0;
5924
6293
  return {
5925
6294
  author,
5926
6295
  contents,
@@ -5931,7 +6300,9 @@ class PdfiumNative {
5931
6300
  // Only include IRT if present
5932
6301
  ...inReplyToId && { inReplyToId },
5933
6302
  // Only include RT if present and not the default (Reply)
5934
- ...replyType && replyType !== PdfAnnotationReplyType.Reply && { replyType }
6303
+ ...replyType && replyType !== PdfAnnotationReplyType.Reply && { replyType },
6304
+ ...rotation !== 0 && { rotation },
6305
+ ...unrotatedRect !== void 0 && { unrotatedRect }
5935
6306
  };
5936
6307
  }
5937
6308
  /**
@@ -6359,7 +6730,9 @@ class PdfiumNative {
6359
6730
  return PdfTaskHelper.reject({ code: PdfErrorCode.NotFound, message: "annotation not found" });
6360
6731
  }
6361
6732
  const finalScale = Math.max(0.01, scaleFactor * dpr);
6362
- const rect = toIntRect(annotation.rect);
6733
+ const unrotated = !!(options == null ? void 0 : options.unrotated) && !!annotation.unrotatedRect;
6734
+ const renderRect = unrotated ? annotation.unrotatedRect : annotation.rect;
6735
+ const rect = toIntRect(renderRect);
6363
6736
  const devRect = toIntRect(transformRect(page.size, rect, rotation, finalScale));
6364
6737
  const wDev = Math.max(1, devRect.size.width);
6365
6738
  const hDev = Math.max(1, devRect.size.height);
@@ -6387,14 +6760,25 @@ class PdfiumNative {
6387
6760
  const FLAGS = 16;
6388
6761
  let ok = false;
6389
6762
  try {
6390
- ok = !!this.pdfiumModule.EPDF_RenderAnnotBitmap(
6391
- bitmapPtr,
6392
- pageCtx.pagePtr,
6393
- annotPtr,
6394
- mode,
6395
- mPtr,
6396
- FLAGS
6397
- );
6763
+ if (unrotated) {
6764
+ ok = !!this.pdfiumModule.EPDF_RenderAnnotBitmapUnrotated(
6765
+ bitmapPtr,
6766
+ pageCtx.pagePtr,
6767
+ annotPtr,
6768
+ mode,
6769
+ mPtr,
6770
+ FLAGS
6771
+ );
6772
+ } else {
6773
+ ok = !!this.pdfiumModule.EPDF_RenderAnnotBitmap(
6774
+ bitmapPtr,
6775
+ pageCtx.pagePtr,
6776
+ annotPtr,
6777
+ mode,
6778
+ mPtr,
6779
+ FLAGS
6780
+ );
6781
+ }
6398
6782
  } finally {
6399
6783
  this.memoryManager.free(mPtr);
6400
6784
  this.pdfiumModule.FPDFBitmap_Destroy(bitmapPtr);
@@ -7629,11 +8013,11 @@ export {
7629
8013
  PdfiumErrorCode as P,
7630
8014
  RenderFlag as R,
7631
8015
  PdfiumNative as a,
7632
- readArrayBuffer as b,
7633
- createPdfiumEngine as c,
7634
- computeFormDrawParams as d,
7635
- createNodeFontLoader as e,
8016
+ createNodeFontLoader as b,
8017
+ computeFormDrawParams as c,
8018
+ createPdfiumEngine as d,
8019
+ readString as e,
7636
8020
  isValidCustomKey as i,
7637
- readString as r
8021
+ readArrayBuffer as r
7638
8022
  };
7639
- //# sourceMappingURL=direct-engine-D8HGqcIt.js.map
8023
+ //# sourceMappingURL=direct-engine-CvfzIn2D.js.map