@chialab/pdfjs-lib 1.0.0-alpha.8 → 1.0.0-alpha.9

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.
@@ -7505,19 +7505,27 @@ var CanvasExtraState = class {
7505
7505
  clone.clipBox = this.clipBox.slice();
7506
7506
  return clone;
7507
7507
  }
7508
- updateRectMinMax(transform, rect) {
7509
- const p1 = [rect[0], rect[1]];
7510
- Util.applyTransform(p1, transform);
7511
- const p2 = [rect[2], rect[3]];
7512
- Util.applyTransform(p2, transform);
7513
- const p3 = [rect[0], rect[3]];
7514
- Util.applyTransform(p3, transform);
7515
- const p4 = [rect[2], rect[1]];
7516
- Util.applyTransform(p4, transform);
7517
- this.minX = Math.min(this.minX, p1[0], p2[0], p3[0], p4[0]);
7518
- this.minY = Math.min(this.minY, p1[1], p2[1], p3[1], p4[1]);
7519
- this.maxX = Math.max(this.maxX, p1[0], p2[0], p3[0], p4[0]);
7520
- this.maxY = Math.max(this.maxY, p1[1], p2[1], p3[1], p4[1]);
7508
+ updateRectMinMax([m0, m1, m2, m3, m4, m5], [r0, r1, r2, r3]) {
7509
+ const m0r0m4 = m0 * r0 + m4;
7510
+ const m0r2m4 = m0 * r2 + m4;
7511
+ const m1r0m5 = m1 * r0 + m5;
7512
+ const m1r2m5 = m1 * r2 + m5;
7513
+ const m2r1 = m2 * r1;
7514
+ const m2r3 = m2 * r3;
7515
+ const m3r1 = m3 * r1;
7516
+ const m3r3 = m3 * r3;
7517
+ const a0 = m0r0m4 + m2r1;
7518
+ const a1 = m0r2m4 + m2r3;
7519
+ const a2 = m0r0m4 + m2r3;
7520
+ const a3 = m0r2m4 + m2r1;
7521
+ const b0 = m1r0m5 + m3r1;
7522
+ const b1 = m1r2m5 + m3r3;
7523
+ const b2 = m1r0m5 + m3r3;
7524
+ const b3 = m1r2m5 + m3r1;
7525
+ this.minX = Math.min(this.minX, a0, a1, a2, a3);
7526
+ this.maxX = Math.max(this.maxX, a0, a1, a2, a3);
7527
+ this.minY = Math.min(this.minY, b0, b1, b2, b3);
7528
+ this.maxY = Math.max(this.maxY, b0, b1, b2, b3);
7521
7529
  }
7522
7530
  getPathBoundingBox(pathType = PathType.FILL, transform = null) {
7523
7531
  const box = [this.minX, this.minY, this.maxX, this.maxY];
@@ -8981,7 +8989,7 @@ var _CanvasGraphics = class _CanvasGraphics {
8981
8989
  }
8982
8990
  this.baseTransform = getCurrentTransform(this.ctx);
8983
8991
  if (bbox) {
8984
- this.current.updateRectMinMax(getCurrentTransform(this.ctx), bbox);
8992
+ this.current.updateRectMinMax(this.baseTransform, bbox);
8985
8993
  const [x0, y0, x1, y1] = bbox;
8986
8994
  const clip = new SvgPath2D();
8987
8995
  clip.rect(x0, y0, x1 - x0, y1 - y0);
@@ -24664,330 +24672,6 @@ if (false) {
24664
24672
  };
24665
24673
  }
24666
24674
 
24667
- // src/lib/utils.ts
24668
- async function canvasToData(canvas) {
24669
- if ("toBlob" in canvas) {
24670
- const blob = await new Promise(
24671
- (resolve) => canvas.toBlob((data) => resolve(data))
24672
- );
24673
- if (!blob) {
24674
- throw new Error("Failed to generate graphics");
24675
- }
24676
- return new Uint8Array(await blob.arrayBuffer());
24677
- }
24678
- const buffer = await canvas.encode("png");
24679
- return new Uint8Array(buffer);
24680
- }
24681
- async function toDataUrl(data) {
24682
- if (typeof FileReader !== "undefined") {
24683
- return new Promise((resolve) => {
24684
- const reader = new FileReader();
24685
- reader.onload = () => {
24686
- resolve(reader.result);
24687
- };
24688
- reader.readAsDataURL(new Blob([data], { type: "image/png" }));
24689
- });
24690
- }
24691
- return `data:image/png;base64,${Buffer.from(data).toString("base64")}`;
24692
- }
24693
-
24694
- // src/lib/PDFPageProxy.ts
24695
- var getAnnotations = PDFPageProxy.prototype.getAnnotations;
24696
- PDFPageProxy.prototype.getAnnotations = async function(params) {
24697
- const annotations = await getAnnotations.call(this, params);
24698
- const operatorList = await this.getOperatorList({
24699
- annotationMode: AnnotationMode.ENABLE_FORMS
24700
- });
24701
- const annotationsOperatorList = /* @__PURE__ */ new Map();
24702
- let currentId = null;
24703
- let currentList = [];
24704
- for (const [index, op] of operatorList.fnArray.entries()) {
24705
- const args = operatorList.argsArray[index];
24706
- if (op === OPS.beginAnnotation) {
24707
- currentId = args[0];
24708
- currentList.push({ fn: op, args });
24709
- } else if (op === OPS.endAnnotation && currentId) {
24710
- currentList.push({ fn: op, args });
24711
- annotationsOperatorList.set(currentId, currentList);
24712
- currentList = [];
24713
- } else if (currentId) {
24714
- currentList.push({ fn: op, args });
24715
- }
24716
- }
24717
- for (const annotation of annotations) {
24718
- const opList = annotationsOperatorList.get(annotation.id);
24719
- if (opList) {
24720
- if (annotation.subtype === "Stamp") {
24721
- try {
24722
- const canvasFactory = isNodeJS ? new NodeCanvasFactory({}) : new DOMCanvasFactory({});
24723
- const scale = 2;
24724
- const viewport = this.getViewport({ scale });
24725
- const { context: canvasContext } = canvasFactory.create(
24726
- viewport.width,
24727
- viewport.height
24728
- );
24729
- const gsx = new CanvasGraphics(
24730
- canvasContext,
24731
- this.commonObjs,
24732
- this.objs,
24733
- canvasFactory,
24734
- this.filterFactory,
24735
- { optionalContentConfig: null }
24736
- );
24737
- gsx.beginDrawing({
24738
- transform: null,
24739
- viewport
24740
- });
24741
- canvasContext.clearRect(0, 0, viewport.width, viewport.height);
24742
- gsx.executeOperatorList({
24743
- fnArray: opList.map((op) => op.fn),
24744
- argsArray: opList.map((op) => op.args)
24745
- });
24746
- const width = annotation.rect[2] - annotation.rect[0];
24747
- const height = annotation.rect[3] - annotation.rect[1];
24748
- const { context: croppedCanvasContext } = canvasFactory.create(
24749
- width * scale,
24750
- height * scale
24751
- );
24752
- croppedCanvasContext.drawImage(
24753
- canvasContext.canvas,
24754
- annotation.rect[0] * scale,
24755
- (this.view[3] - annotation.rect[1] - height) * scale,
24756
- width * scale,
24757
- height * scale,
24758
- 0,
24759
- 0,
24760
- width * scale,
24761
- height * scale
24762
- );
24763
- annotation.graphics = await canvasToData(croppedCanvasContext.canvas);
24764
- } catch (e) {
24765
- console.error(e);
24766
- }
24767
- } else {
24768
- let firstStroke = true;
24769
- let firstFill = true;
24770
- let firstText = true;
24771
- let currentFillColor = null;
24772
- let currentLineWidth = null;
24773
- for (const { fn, args } of opList) {
24774
- const finalFn = fn === OPS.constructPath ? args[0] : fn;
24775
- if (finalFn === OPS.fill) {
24776
- if (currentFillColor && firstFill) {
24777
- annotation.interiorColor = currentFillColor;
24778
- currentFillColor = null;
24779
- }
24780
- firstFill = false;
24781
- continue;
24782
- }
24783
- if (fn === OPS.beginText) {
24784
- firstFill = false;
24785
- continue;
24786
- }
24787
- if (finalFn === OPS.stroke || finalFn === OPS.closeStroke) {
24788
- if (currentLineWidth === null && firstStroke) {
24789
- annotation.borderStyle.width = 1;
24790
- }
24791
- firstStroke = false;
24792
- currentLineWidth = null;
24793
- continue;
24794
- }
24795
- if (finalFn === OPS.fillStroke || finalFn === OPS.closeFillStroke) {
24796
- if (currentFillColor && firstFill) {
24797
- annotation.interiorColor = currentFillColor;
24798
- currentFillColor = null;
24799
- }
24800
- if (currentLineWidth === null && firstStroke) {
24801
- annotation.borderStyle.width = 1;
24802
- }
24803
- firstStroke = false;
24804
- currentLineWidth = null;
24805
- break;
24806
- }
24807
- if (!firstFill && !firstStroke) {
24808
- break;
24809
- }
24810
- if (fn === OPS.setLineWidth && firstStroke) {
24811
- currentLineWidth = args[0];
24812
- annotation.borderStyle.width = currentLineWidth;
24813
- }
24814
- if (fn === OPS.setStrokeRGBColor && firstStroke) {
24815
- annotation.color = args;
24816
- }
24817
- if (fn === OPS.setFillRGBColor && firstFill) {
24818
- currentFillColor = args;
24819
- }
24820
- if (fn === OPS.setGState) {
24821
- for (const entry of args[0]) {
24822
- if (!Array.isArray(entry)) {
24823
- continue;
24824
- }
24825
- if (entry[0] === "ca" && firstFill) {
24826
- annotation.interiorColorOpacity = entry[1];
24827
- }
24828
- if (entry[0] === "CA" && firstStroke) {
24829
- annotation.borderColorOpacity = entry[1];
24830
- }
24831
- }
24832
- }
24833
- if (fn === OPS.setFont && firstText) {
24834
- const fontName = args[0];
24835
- const font = fontName.startsWith("g_") ? this.commonObjs.get(fontName) : this.objs.get(fontName);
24836
- if (font) {
24837
- annotation.defaultAppearanceData.fontName = font.fallbackName;
24838
- }
24839
- }
24840
- if (fn === OPS.endText) {
24841
- firstText = false;
24842
- }
24843
- }
24844
- }
24845
- }
24846
- }
24847
- return annotations;
24848
- };
24849
-
24850
- // src/lib/StandardFontDataFactory.ts
24851
- var StandardFontDataFactory = class extends BaseStandardFontDataFactory {
24852
- constructor() {
24853
- super({
24854
- baseUrl: null
24855
- });
24856
- }
24857
- /**
24858
- * Fetch the corresponding standard font data.
24859
- * We need to use specific dynamic imports for each font file for the bundler to include them.
24860
- */
24861
- async fetch({ filename }) {
24862
- switch (filename) {
24863
- case "FoxitDingbats.pfb":
24864
- return import("./FoxitDingbats-65AZ2Z2V.js").then((module) => module.default);
24865
- case "FoxitFixed.pfb":
24866
- return import("./FoxitFixed-OZGLVVOQ.js").then(
24867
- (module) => module.default
24868
- );
24869
- case "FoxitFixedBold.pfb":
24870
- return import("./FoxitFixedBold-37OOYMV7.js").then((module) => module.default);
24871
- case "FoxitFixedBoldItalic.pfb":
24872
- return import("./FoxitFixedBoldItalic-XYFHEG2V.js").then((module) => module.default);
24873
- case "FoxitFixedItalic.pfb":
24874
- return import("./FoxitFixedItalic-MM7OSGS6.js").then((module) => module.default);
24875
- case "FoxitSerif.pfb":
24876
- return import("./FoxitSerif-MLMJQPRP.js").then(
24877
- (module) => module.default
24878
- );
24879
- case "FoxitSerifBold.pfb":
24880
- return import("./FoxitSerifBold-AC7RJQWJ.js").then((module) => module.default);
24881
- case "FoxitSerifBoldItalic.pfb":
24882
- return import("./FoxitSerifBoldItalic-YCQ4CLKE.js").then((module) => module.default);
24883
- case "FoxitSerifItalic.pfb":
24884
- return import("./FoxitSerifItalic-CXVTCST4.js").then((module) => module.default);
24885
- case "FoxitSymbol.pfb":
24886
- return import("./FoxitSymbol-VUGMZN5C.js").then(
24887
- (module) => module.default
24888
- );
24889
- case "LiberationSans-Bold.ttf":
24890
- return import("./LiberationSans-Bold-XSHQQBWB.js").then((module) => module.default);
24891
- case "LiberationSans-BoldItalic.ttf":
24892
- return import("./LiberationSans-BoldItalic-CTAZRFRL.js").then((module) => module.default);
24893
- case "LiberationSans-Italic.ttf":
24894
- return import("./LiberationSans-Italic-WIOTUKLC.js").then((module) => module.default);
24895
- case "LiberationSans-Regular.ttf":
24896
- return import("./LiberationSans-Regular-CDMMZL5S.js").then((module) => module.default);
24897
- }
24898
- return Uint8Array.from([]);
24899
- }
24900
- };
24901
-
24902
- // src/lib/AnnotationData.ts
24903
- function isTextAnnotation(annotation) {
24904
- return annotation.subtype === "Text";
24905
- }
24906
- function isLinkAnnotation(annotation) {
24907
- return annotation.subtype === "Link";
24908
- }
24909
- function isFreeTextAnnotation(annotation) {
24910
- return annotation.subtype === "FreeText";
24911
- }
24912
- function isLineAnnotation(annotation) {
24913
- return annotation.subtype === "Line";
24914
- }
24915
- function isSquareAnnotation(annotation) {
24916
- return annotation.subtype === "Square";
24917
- }
24918
- function isCircleAnnotation(annotation) {
24919
- return annotation.subtype === "Circle";
24920
- }
24921
- function isPolygonAnnotation(annotation) {
24922
- return annotation.subtype === "Polygon";
24923
- }
24924
- function isPolylineAnnotation(annotation) {
24925
- return annotation.subtype === "PolyLine";
24926
- }
24927
- function isHighlightAnnotation(annotation) {
24928
- return annotation.subtype === "Highlight";
24929
- }
24930
- function isUnderlineAnnotation(annotation) {
24931
- return annotation.subtype === "Underline";
24932
- }
24933
- function isSquigglyAnnotation(annotation) {
24934
- return annotation.subtype === "Squiggly";
24935
- }
24936
- function isStrikeOutAnnotation(annotation) {
24937
- return annotation.subtype === "StrikeOut";
24938
- }
24939
- function isStampAnnotation(annotation) {
24940
- return annotation.subtype === "Stamp";
24941
- }
24942
- function isCaretAnnotation(annotation) {
24943
- return annotation.subtype === "Caret";
24944
- }
24945
- function isInkAnnotation(annotation) {
24946
- return annotation.subtype === "Ink";
24947
- }
24948
- function isPopupAnnotation(annotation) {
24949
- return annotation.subtype === "Popup";
24950
- }
24951
- function isFileAttachmentAnnotation(annotation) {
24952
- return annotation.subtype === "FileAttachment";
24953
- }
24954
- function isSoundAnnotation(annotation) {
24955
- return annotation.subtype === "Sound";
24956
- }
24957
- function isMovieAnnotation(annotation) {
24958
- return annotation.subtype === "Movie";
24959
- }
24960
- function isWidgetAnnotation(annotation) {
24961
- return annotation.subtype === "Widget";
24962
- }
24963
- function isTextWidgetAnnotation(annotation) {
24964
- return annotation.subtype === "Widget" && annotation.fieldType === "Tx";
24965
- }
24966
- function isChoiceWidgetAnnotation(annotation) {
24967
- return annotation.subtype === "Widget" && annotation.fieldType === "Ch";
24968
- }
24969
- function isButtonWidgetAnnotation(annotation) {
24970
- return annotation.subtype === "Widget" && annotation.fieldType === "Btn";
24971
- }
24972
- function isScreenAnnotation(annotation) {
24973
- return annotation.subtype === "Screen";
24974
- }
24975
- function isPrinterMarkAnnotation(annotation) {
24976
- return annotation.subtype === "PrinterMark";
24977
- }
24978
- function isTrapNetAnnotation(annotation) {
24979
- return annotation.subtype === "TrapNet";
24980
- }
24981
- function isWatermarkAnnotation(annotation) {
24982
- return annotation.subtype === "Watermark";
24983
- }
24984
- function isThreeDAnnotation(annotation) {
24985
- return annotation.subtype === "3D";
24986
- }
24987
- function isRedactAnnotation(annotation) {
24988
- return annotation.subtype === "Redact";
24989
- }
24990
-
24991
24675
  // src/lib/SvgCanvasContext.ts
24992
24676
  function parseTransformMatrix(transformString) {
24993
24677
  const values = transformString.match(/matrix\(([^)]+)\)/)?.[1].split(",").map(Number);
@@ -25281,19 +24965,33 @@ var SvgCanvasContext = class {
25281
24965
  this._styleStack.push(this._getStyleState());
25282
24966
  this._transformMatrixStack.push(this.getTransform());
25283
24967
  }
24968
+ clearRect(x, y, width, height) {
24969
+ if (x !== 0 || y !== 0 || this._width !== width || this._height !== height) {
24970
+ throw new Error("clearRect is not supported in SVG");
24971
+ }
24972
+ this._clearCanvas();
24973
+ }
25284
24974
  restore() {
25285
- const group = this._groupStack.pop();
25286
- if (!this._isTransformationGroup(this._currentGroup)) {
25287
- group.children = group.children.filter(
25288
- (child) => child !== this._currentGroup
25289
- );
25290
- group.children.push(...this._currentGroup.children);
24975
+ if (this._groupStack.length) {
24976
+ const group = this._groupStack.pop();
24977
+ if (!this._isTransformationGroup(this._currentGroup)) {
24978
+ group.children = group.children.filter(
24979
+ (child) => child !== this._currentGroup
24980
+ );
24981
+ group.children.push(...this._currentGroup.children);
24982
+ }
24983
+ this._currentGroup = group;
24984
+ } else {
24985
+ this._currentGroup = this._root;
24986
+ }
24987
+ if (this._styleStack.length) {
24988
+ const state = this._styleStack.pop();
24989
+ this._applyStyleState(state);
24990
+ }
24991
+ if (this._transformMatrixStack.length) {
24992
+ const { a, b, c, d, e, f } = this._transformMatrixStack.pop();
24993
+ this.setTransform(a, b, c, d, e, f);
25291
24994
  }
25292
- this._currentGroup = group;
25293
- const state = this._styleStack.pop();
25294
- this._applyStyleState(state);
25295
- const { a, b, c, d, e, f } = this._transformMatrixStack.pop();
25296
- this.setTransform(a, b, c, d, e, f);
25297
24995
  }
25298
24996
  beginPath() {
25299
24997
  this._currentPosition = {};
@@ -25631,6 +25329,9 @@ var SvgCanvasContext = class {
25631
25329
  const fillRuleValue = fillRule ?? path;
25632
25330
  this._currentStyle.fillRule = fillRuleValue;
25633
25331
  }
25332
+ if (path instanceof SvgPath2D && path.commands.length === 0) {
25333
+ return;
25334
+ }
25634
25335
  const id = `clip_${crypto.randomUUID()}`;
25635
25336
  const clipPath = {
25636
25337
  tag: "clipPath",
@@ -25836,64 +25537,405 @@ var SvgCanvasContext = class {
25836
25537
  if (command === "Z" && this._currentElement.attrs.d.endsWith("Z")) {
25837
25538
  return;
25838
25539
  }
25839
- this._currentElement.attrs.d += ` ${command}`;
25840
- } else {
25841
- this._currentElement.attrs.d = command;
25842
- this._currentGroup.children.push(this._currentElement);
25540
+ this._currentElement.attrs.d += ` ${command}`;
25541
+ } else {
25542
+ this._currentElement.attrs.d = command;
25543
+ this._currentGroup.children.push(this._currentElement);
25544
+ }
25545
+ }
25546
+ }
25547
+ _applyText(text, x, y, action) {
25548
+ const parent = this._currentGroup;
25549
+ const style = parseFontStyle(this._currentStyle.font);
25550
+ const textElement = {
25551
+ tag: "text",
25552
+ attrs: {
25553
+ x: `${x}`,
25554
+ y: `${y}`,
25555
+ "font-family": style.fontFamilies.join(", "),
25556
+ "font-size": style.fontSize,
25557
+ "font-style": style.fontStyle,
25558
+ "font-weight": style.fontWeight,
25559
+ "text-anchor": getTextAnchor(this._currentStyle.textAlign),
25560
+ "dominant-baseline": getDominantBaseline(
25561
+ this._currentStyle.textBaseline
25562
+ )
25563
+ },
25564
+ text
25565
+ };
25566
+ this._applyTransformation(textElement);
25567
+ this._applyStyle(textElement, action);
25568
+ parent.children.push(textElement);
25569
+ }
25570
+ _clearCanvas() {
25571
+ this._defs.children = [];
25572
+ this._root.children = [this._defs];
25573
+ this._currentGroup = this._root;
25574
+ this._groupStack = [];
25575
+ }
25576
+ };
25577
+ async function createSvgContext(width, height) {
25578
+ const canvas = await createCanvas(width, height);
25579
+ return new SvgCanvasContext(
25580
+ width,
25581
+ height,
25582
+ canvas
25583
+ );
25584
+ }
25585
+ async function toSvgNode(ctx) {
25586
+ if (!(ctx instanceof SvgCanvasContext)) {
25587
+ throw new Error("Invalid context");
25588
+ }
25589
+ return ctx.getNode();
25590
+ }
25591
+
25592
+ // src/lib/utils.ts
25593
+ async function canvasToData(canvas) {
25594
+ if ("toBlob" in canvas) {
25595
+ const blob = await new Promise(
25596
+ (resolve) => canvas.toBlob((data) => resolve(data))
25597
+ );
25598
+ if (!blob) {
25599
+ throw new Error("Failed to generate graphics");
25600
+ }
25601
+ return new Uint8Array(await blob.arrayBuffer());
25602
+ }
25603
+ const buffer = await canvas.encode("png");
25604
+ return new Uint8Array(buffer);
25605
+ }
25606
+ async function toDataUrl(data, type = "image/png") {
25607
+ if (typeof FileReader !== "undefined") {
25608
+ return new Promise((resolve) => {
25609
+ const reader = new FileReader();
25610
+ reader.onload = () => {
25611
+ resolve(reader.result);
25612
+ };
25613
+ reader.readAsDataURL(new Blob([data], { type }));
25614
+ });
25615
+ }
25616
+ return `data:${type};base64,${Buffer.from(data).toString("base64")}`;
25617
+ }
25618
+ function makeSerializable(object) {
25619
+ if (typeof object !== "object" || object === null) {
25620
+ return object;
25621
+ }
25622
+ if (object instanceof Int8Array || object instanceof Uint8Array || object instanceof Uint8ClampedArray || object instanceof Int16Array || object instanceof Uint16Array || object instanceof Int32Array || object instanceof Uint32Array || object instanceof Float32Array || object instanceof Float64Array) {
25623
+ return makeSerializable(Array.from(object));
25624
+ }
25625
+ if (object instanceof BigInt64Array || object instanceof BigUint64Array) {
25626
+ return makeSerializable(Array.from(object));
25627
+ }
25628
+ if (Array.isArray(object)) {
25629
+ return object.map(makeSerializable);
25630
+ }
25631
+ return Object.fromEntries(
25632
+ Object.entries(object).map(([key, value]) => [
25633
+ key,
25634
+ makeSerializable(value)
25635
+ ])
25636
+ );
25637
+ }
25638
+
25639
+ // src/lib/PDFPageProxy.ts
25640
+ var getAnnotations = PDFPageProxy.prototype.getAnnotations;
25641
+ PDFPageProxy.prototype.getAnnotations = async function(params) {
25642
+ const annotations = await getAnnotations.call(this, params);
25643
+ const operatorList = await this.getOperatorList({
25644
+ annotationMode: AnnotationMode.ENABLE_FORMS
25645
+ });
25646
+ const annotationsOperatorList = /* @__PURE__ */ new Map();
25647
+ let currentId = null;
25648
+ let currentList = [];
25649
+ for (const [index, op] of operatorList.fnArray.entries()) {
25650
+ const args = operatorList.argsArray[index];
25651
+ if (op === OPS.beginAnnotation) {
25652
+ currentId = args[0];
25653
+ currentList.push({ fn: op, args });
25654
+ } else if (op === OPS.endAnnotation && currentId) {
25655
+ currentList.push({ fn: op, args });
25656
+ annotationsOperatorList.set(currentId, currentList);
25657
+ currentList = [];
25658
+ } else if (currentId) {
25659
+ currentList.push({ fn: op, args });
25660
+ }
25661
+ }
25662
+ for (const annotation of annotations) {
25663
+ const opList = annotationsOperatorList.get(annotation.id);
25664
+ if (opList) {
25665
+ if (annotation.subtype === "Stamp") {
25666
+ try {
25667
+ const canvasFactory = isNodeJS ? new NodeCanvasFactory({}) : new DOMCanvasFactory({});
25668
+ const scale = 2;
25669
+ const viewport = this.getViewport({ scale });
25670
+ const svgContext = await createSvgContext(
25671
+ viewport.width,
25672
+ viewport.height
25673
+ );
25674
+ const gsx = new CanvasGraphics(
25675
+ svgContext,
25676
+ this.commonObjs,
25677
+ this.objs,
25678
+ canvasFactory,
25679
+ this.filterFactory,
25680
+ { optionalContentConfig: null }
25681
+ );
25682
+ gsx.beginDrawing({
25683
+ transform: null,
25684
+ viewport
25685
+ });
25686
+ svgContext.clearRect(0, 0, viewport.width, viewport.height);
25687
+ gsx.executeOperatorList({
25688
+ fnArray: opList.map((op) => op.fn),
25689
+ argsArray: opList.map((op) => op.args)
25690
+ });
25691
+ const width = annotation.rect[2] - annotation.rect[0];
25692
+ const height = annotation.rect[3] - annotation.rect[1];
25693
+ annotation.graphics = {
25694
+ tag: "svg",
25695
+ attrs: {
25696
+ version: "1.1",
25697
+ xmlns: "http://www.w3.org/2000/svg",
25698
+ width: `${width}`,
25699
+ height: `${height}`,
25700
+ viewBox: `0 0 ${width * scale} ${height * scale}`,
25701
+ preserveAspectRatio: "none",
25702
+ "xml:space": "preserve"
25703
+ },
25704
+ children: [
25705
+ {
25706
+ tag: "g",
25707
+ attrs: {
25708
+ transform: `translate(-${annotation.rect[0] * scale}, -${(this.view[3] - annotation.rect[1] - height) * scale})`
25709
+ },
25710
+ children: [await toSvgNode(svgContext)]
25711
+ }
25712
+ ]
25713
+ };
25714
+ } catch (e) {
25715
+ console.error(e);
25716
+ }
25717
+ } else {
25718
+ let firstStroke = true;
25719
+ let firstFill = true;
25720
+ let firstText = true;
25721
+ let currentFillColor = null;
25722
+ let currentLineWidth = null;
25723
+ for (const { fn, args } of opList) {
25724
+ const finalFn = fn === OPS.constructPath ? args[0] : fn;
25725
+ if (finalFn === OPS.fill) {
25726
+ if (currentFillColor && firstFill) {
25727
+ annotation.interiorColor = currentFillColor;
25728
+ currentFillColor = null;
25729
+ }
25730
+ firstFill = false;
25731
+ continue;
25732
+ }
25733
+ if (fn === OPS.beginText) {
25734
+ firstFill = false;
25735
+ continue;
25736
+ }
25737
+ if (finalFn === OPS.stroke || finalFn === OPS.closeStroke) {
25738
+ if (currentLineWidth === null && firstStroke) {
25739
+ annotation.borderStyle.width = 1;
25740
+ }
25741
+ firstStroke = false;
25742
+ currentLineWidth = null;
25743
+ continue;
25744
+ }
25745
+ if (finalFn === OPS.fillStroke || finalFn === OPS.closeFillStroke) {
25746
+ if (currentFillColor && firstFill) {
25747
+ annotation.interiorColor = currentFillColor;
25748
+ currentFillColor = null;
25749
+ }
25750
+ if (currentLineWidth === null && firstStroke) {
25751
+ annotation.borderStyle.width = 1;
25752
+ }
25753
+ firstStroke = false;
25754
+ currentLineWidth = null;
25755
+ break;
25756
+ }
25757
+ if (!firstFill && !firstStroke) {
25758
+ break;
25759
+ }
25760
+ if (fn === OPS.setLineWidth && firstStroke) {
25761
+ currentLineWidth = args[0];
25762
+ annotation.borderStyle.width = currentLineWidth;
25763
+ }
25764
+ if (fn === OPS.setStrokeRGBColor && firstStroke) {
25765
+ annotation.color = args;
25766
+ }
25767
+ if (fn === OPS.setFillRGBColor && firstFill) {
25768
+ currentFillColor = args;
25769
+ }
25770
+ if (fn === OPS.setGState) {
25771
+ for (const entry of args[0]) {
25772
+ if (!Array.isArray(entry)) {
25773
+ continue;
25774
+ }
25775
+ if (entry[0] === "ca" && firstFill) {
25776
+ annotation.interiorColorOpacity = entry[1];
25777
+ }
25778
+ if (entry[0] === "CA" && firstStroke) {
25779
+ annotation.borderColorOpacity = entry[1];
25780
+ }
25781
+ }
25782
+ }
25783
+ if (fn === OPS.setFont && firstText) {
25784
+ const fontName = args[0];
25785
+ const font = fontName.startsWith("g_") ? this.commonObjs.get(fontName) : this.objs.get(fontName);
25786
+ if (font) {
25787
+ annotation.defaultAppearanceData.fontName = font.fallbackName;
25788
+ }
25789
+ }
25790
+ if (fn === OPS.endText) {
25791
+ firstText = false;
25792
+ }
25793
+ }
25843
25794
  }
25844
25795
  }
25845
25796
  }
25846
- _applyText(text, x, y, action) {
25847
- const parent = this._currentGroup;
25848
- const style = parseFontStyle(this._currentStyle.font);
25849
- const textElement = {
25850
- tag: "text",
25851
- attrs: {
25852
- x: `${x}`,
25853
- y: `${y}`,
25854
- "font-family": style.fontFamilies.join(", "),
25855
- "font-size": style.fontSize,
25856
- "font-style": style.fontStyle,
25857
- "font-weight": style.fontWeight,
25858
- "text-anchor": getTextAnchor(this._currentStyle.textAlign),
25859
- "dominant-baseline": getDominantBaseline(
25860
- this._currentStyle.textBaseline
25861
- )
25862
- },
25863
- text
25864
- };
25865
- this._applyTransformation(textElement);
25866
- this._applyStyle(textElement, action);
25867
- parent.children.push(textElement);
25797
+ return makeSerializable(annotations);
25798
+ };
25799
+
25800
+ // src/lib/StandardFontDataFactory.ts
25801
+ var StandardFontDataFactory = class extends BaseStandardFontDataFactory {
25802
+ constructor() {
25803
+ super({
25804
+ baseUrl: null
25805
+ });
25868
25806
  }
25869
- _clearCanvas() {
25870
- const rootGroup = this._root.children[1];
25871
- this._defs.children = [];
25872
- this._root.children = this._root.children.filter(
25873
- (child) => child === rootGroup
25874
- );
25875
- this._currentGroup = {
25876
- tag: "g",
25877
- attrs: {},
25878
- children: []
25879
- };
25880
- this._root.children.push(this._currentGroup);
25881
- this._groupStack = [];
25807
+ /**
25808
+ * Fetch the corresponding standard font data.
25809
+ * We need to use specific dynamic imports for each font file for the bundler to include them.
25810
+ */
25811
+ async fetch({ filename }) {
25812
+ switch (filename) {
25813
+ case "FoxitDingbats.pfb":
25814
+ return import("./FoxitDingbats-65AZ2Z2V.js").then((module) => module.default);
25815
+ case "FoxitFixed.pfb":
25816
+ return import("./FoxitFixed-OZGLVVOQ.js").then(
25817
+ (module) => module.default
25818
+ );
25819
+ case "FoxitFixedBold.pfb":
25820
+ return import("./FoxitFixedBold-37OOYMV7.js").then((module) => module.default);
25821
+ case "FoxitFixedBoldItalic.pfb":
25822
+ return import("./FoxitFixedBoldItalic-XYFHEG2V.js").then((module) => module.default);
25823
+ case "FoxitFixedItalic.pfb":
25824
+ return import("./FoxitFixedItalic-MM7OSGS6.js").then((module) => module.default);
25825
+ case "FoxitSerif.pfb":
25826
+ return import("./FoxitSerif-MLMJQPRP.js").then(
25827
+ (module) => module.default
25828
+ );
25829
+ case "FoxitSerifBold.pfb":
25830
+ return import("./FoxitSerifBold-AC7RJQWJ.js").then((module) => module.default);
25831
+ case "FoxitSerifBoldItalic.pfb":
25832
+ return import("./FoxitSerifBoldItalic-YCQ4CLKE.js").then((module) => module.default);
25833
+ case "FoxitSerifItalic.pfb":
25834
+ return import("./FoxitSerifItalic-CXVTCST4.js").then((module) => module.default);
25835
+ case "FoxitSymbol.pfb":
25836
+ return import("./FoxitSymbol-VUGMZN5C.js").then(
25837
+ (module) => module.default
25838
+ );
25839
+ case "LiberationSans-Bold.ttf":
25840
+ return import("./LiberationSans-Bold-XSHQQBWB.js").then((module) => module.default);
25841
+ case "LiberationSans-BoldItalic.ttf":
25842
+ return import("./LiberationSans-BoldItalic-CTAZRFRL.js").then((module) => module.default);
25843
+ case "LiberationSans-Italic.ttf":
25844
+ return import("./LiberationSans-Italic-WIOTUKLC.js").then((module) => module.default);
25845
+ case "LiberationSans-Regular.ttf":
25846
+ return import("./LiberationSans-Regular-CDMMZL5S.js").then((module) => module.default);
25847
+ }
25848
+ return Uint8Array.from([]);
25882
25849
  }
25883
25850
  };
25884
- async function createSvgContext(width, height) {
25885
- const canvas = await createCanvas(width, height);
25886
- return new SvgCanvasContext(
25887
- width,
25888
- height,
25889
- canvas
25890
- );
25851
+
25852
+ // src/lib/AnnotationData.ts
25853
+ function isTextAnnotation(annotation) {
25854
+ return annotation.subtype === "Text";
25891
25855
  }
25892
- async function toSvgNode(ctx) {
25893
- if (!(ctx instanceof SvgCanvasContext)) {
25894
- throw new Error("Invalid context");
25895
- }
25896
- return ctx.getNode();
25856
+ function isLinkAnnotation(annotation) {
25857
+ return annotation.subtype === "Link";
25858
+ }
25859
+ function isFreeTextAnnotation(annotation) {
25860
+ return annotation.subtype === "FreeText";
25861
+ }
25862
+ function isLineAnnotation(annotation) {
25863
+ return annotation.subtype === "Line";
25864
+ }
25865
+ function isSquareAnnotation(annotation) {
25866
+ return annotation.subtype === "Square";
25867
+ }
25868
+ function isCircleAnnotation(annotation) {
25869
+ return annotation.subtype === "Circle";
25870
+ }
25871
+ function isPolygonAnnotation(annotation) {
25872
+ return annotation.subtype === "Polygon";
25873
+ }
25874
+ function isPolylineAnnotation(annotation) {
25875
+ return annotation.subtype === "PolyLine";
25876
+ }
25877
+ function isHighlightAnnotation(annotation) {
25878
+ return annotation.subtype === "Highlight";
25879
+ }
25880
+ function isUnderlineAnnotation(annotation) {
25881
+ return annotation.subtype === "Underline";
25882
+ }
25883
+ function isSquigglyAnnotation(annotation) {
25884
+ return annotation.subtype === "Squiggly";
25885
+ }
25886
+ function isStrikeOutAnnotation(annotation) {
25887
+ return annotation.subtype === "StrikeOut";
25888
+ }
25889
+ function isStampAnnotation(annotation) {
25890
+ return annotation.subtype === "Stamp";
25891
+ }
25892
+ function isCaretAnnotation(annotation) {
25893
+ return annotation.subtype === "Caret";
25894
+ }
25895
+ function isInkAnnotation(annotation) {
25896
+ return annotation.subtype === "Ink";
25897
+ }
25898
+ function isPopupAnnotation(annotation) {
25899
+ return annotation.subtype === "Popup";
25900
+ }
25901
+ function isFileAttachmentAnnotation(annotation) {
25902
+ return annotation.subtype === "FileAttachment";
25903
+ }
25904
+ function isSoundAnnotation(annotation) {
25905
+ return annotation.subtype === "Sound";
25906
+ }
25907
+ function isMovieAnnotation(annotation) {
25908
+ return annotation.subtype === "Movie";
25909
+ }
25910
+ function isWidgetAnnotation(annotation) {
25911
+ return annotation.subtype === "Widget";
25912
+ }
25913
+ function isTextWidgetAnnotation(annotation) {
25914
+ return annotation.subtype === "Widget" && annotation.fieldType === "Tx";
25915
+ }
25916
+ function isChoiceWidgetAnnotation(annotation) {
25917
+ return annotation.subtype === "Widget" && annotation.fieldType === "Ch";
25918
+ }
25919
+ function isButtonWidgetAnnotation(annotation) {
25920
+ return annotation.subtype === "Widget" && annotation.fieldType === "Btn";
25921
+ }
25922
+ function isScreenAnnotation(annotation) {
25923
+ return annotation.subtype === "Screen";
25924
+ }
25925
+ function isPrinterMarkAnnotation(annotation) {
25926
+ return annotation.subtype === "PrinterMark";
25927
+ }
25928
+ function isTrapNetAnnotation(annotation) {
25929
+ return annotation.subtype === "TrapNet";
25930
+ }
25931
+ function isWatermarkAnnotation(annotation) {
25932
+ return annotation.subtype === "Watermark";
25933
+ }
25934
+ function isThreeDAnnotation(annotation) {
25935
+ return annotation.subtype === "3D";
25936
+ }
25937
+ function isRedactAnnotation(annotation) {
25938
+ return annotation.subtype === "Redact";
25897
25939
  }
25898
25940
 
25899
25941
  // src/lib/TextLayer.ts
@@ -26456,6 +26498,7 @@ export {
26456
26498
  isWatermarkAnnotation,
26457
26499
  isWidgetAnnotation,
26458
26500
  loadDefaultFonts,
26501
+ makeSerializable,
26459
26502
  noContextMenu,
26460
26503
  normalizeUnicode,
26461
26504
  setLayerDimensions,