@chialab/pdfjs-lib 1.0.0-alpha.7 → 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,319 +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 currentFillColor = null;
24771
- let currentLineWidth = null;
24772
- for (const { fn, args } of opList) {
24773
- const finalFn = fn === OPS.constructPath ? args[0] : fn;
24774
- if (finalFn === OPS.fill) {
24775
- if (currentFillColor) {
24776
- annotation.interiorColor = currentFillColor;
24777
- currentFillColor = null;
24778
- }
24779
- firstFill = false;
24780
- continue;
24781
- }
24782
- if (fn === OPS.beginText) {
24783
- firstFill = false;
24784
- continue;
24785
- }
24786
- if (finalFn === OPS.stroke || finalFn === OPS.closeStroke) {
24787
- if (currentLineWidth === null) {
24788
- annotation.borderStyle.width = 1;
24789
- }
24790
- firstStroke = false;
24791
- currentLineWidth = null;
24792
- continue;
24793
- }
24794
- if (finalFn === OPS.fillStroke || finalFn === OPS.closeFillStroke) {
24795
- if (currentFillColor) {
24796
- annotation.interiorColor = currentFillColor;
24797
- currentFillColor = null;
24798
- }
24799
- if (currentLineWidth === null) {
24800
- annotation.borderStyle.width = 1;
24801
- }
24802
- firstStroke = false;
24803
- currentLineWidth = null;
24804
- break;
24805
- }
24806
- if (!firstFill && !firstStroke) {
24807
- break;
24808
- }
24809
- if (fn === OPS.setLineWidth && firstStroke) {
24810
- currentLineWidth = args[0];
24811
- annotation.borderStyle.width = currentLineWidth;
24812
- }
24813
- if (fn === OPS.setStrokeRGBColor && firstStroke) {
24814
- annotation.color = args;
24815
- }
24816
- if (fn === OPS.setFillRGBColor && firstFill) {
24817
- currentFillColor = args;
24818
- }
24819
- if (fn === OPS.setGState) {
24820
- for (const entry of args[0]) {
24821
- if (!Array.isArray(entry)) {
24822
- continue;
24823
- }
24824
- if (entry[0] === "ca" && firstFill) {
24825
- annotation.interiorColorOpacity = entry[1];
24826
- }
24827
- if (entry[0] === "CA" && firstStroke) {
24828
- annotation.borderColorOpacity = entry[1];
24829
- }
24830
- }
24831
- }
24832
- }
24833
- }
24834
- }
24835
- }
24836
- return annotations;
24837
- };
24838
-
24839
- // src/lib/StandardFontDataFactory.ts
24840
- var StandardFontDataFactory = class extends BaseStandardFontDataFactory {
24841
- constructor() {
24842
- super({
24843
- baseUrl: null
24844
- });
24845
- }
24846
- /**
24847
- * Fetch the corresponding standard font data.
24848
- * We need to use specific dynamic imports for each font file for the bundler to include them.
24849
- */
24850
- async fetch({ filename }) {
24851
- switch (filename) {
24852
- case "FoxitDingbats.pfb":
24853
- return import("./FoxitDingbats-65AZ2Z2V.js").then((module) => module.default);
24854
- case "FoxitFixed.pfb":
24855
- return import("./FoxitFixed-OZGLVVOQ.js").then(
24856
- (module) => module.default
24857
- );
24858
- case "FoxitFixedBold.pfb":
24859
- return import("./FoxitFixedBold-37OOYMV7.js").then((module) => module.default);
24860
- case "FoxitFixedBoldItalic.pfb":
24861
- return import("./FoxitFixedBoldItalic-XYFHEG2V.js").then((module) => module.default);
24862
- case "FoxitFixedItalic.pfb":
24863
- return import("./FoxitFixedItalic-MM7OSGS6.js").then((module) => module.default);
24864
- case "FoxitSerif.pfb":
24865
- return import("./FoxitSerif-MLMJQPRP.js").then(
24866
- (module) => module.default
24867
- );
24868
- case "FoxitSerifBold.pfb":
24869
- return import("./FoxitSerifBold-AC7RJQWJ.js").then((module) => module.default);
24870
- case "FoxitSerifBoldItalic.pfb":
24871
- return import("./FoxitSerifBoldItalic-YCQ4CLKE.js").then((module) => module.default);
24872
- case "FoxitSerifItalic.pfb":
24873
- return import("./FoxitSerifItalic-CXVTCST4.js").then((module) => module.default);
24874
- case "FoxitSymbol.pfb":
24875
- return import("./FoxitSymbol-VUGMZN5C.js").then(
24876
- (module) => module.default
24877
- );
24878
- case "LiberationSans-Bold.ttf":
24879
- return import("./LiberationSans-Bold-XSHQQBWB.js").then((module) => module.default);
24880
- case "LiberationSans-BoldItalic.ttf":
24881
- return import("./LiberationSans-BoldItalic-CTAZRFRL.js").then((module) => module.default);
24882
- case "LiberationSans-Italic.ttf":
24883
- return import("./LiberationSans-Italic-WIOTUKLC.js").then((module) => module.default);
24884
- case "LiberationSans-Regular.ttf":
24885
- return import("./LiberationSans-Regular-CDMMZL5S.js").then((module) => module.default);
24886
- }
24887
- return Uint8Array.from([]);
24888
- }
24889
- };
24890
-
24891
- // src/lib/AnnotationData.ts
24892
- function isTextAnnotation(annotation) {
24893
- return annotation.subtype === "Text";
24894
- }
24895
- function isLinkAnnotation(annotation) {
24896
- return annotation.subtype === "Link";
24897
- }
24898
- function isFreeTextAnnotation(annotation) {
24899
- return annotation.subtype === "FreeText";
24900
- }
24901
- function isLineAnnotation(annotation) {
24902
- return annotation.subtype === "Line";
24903
- }
24904
- function isSquareAnnotation(annotation) {
24905
- return annotation.subtype === "Square";
24906
- }
24907
- function isCircleAnnotation(annotation) {
24908
- return annotation.subtype === "Circle";
24909
- }
24910
- function isPolygonAnnotation(annotation) {
24911
- return annotation.subtype === "Polygon";
24912
- }
24913
- function isPolylineAnnotation(annotation) {
24914
- return annotation.subtype === "PolyLine";
24915
- }
24916
- function isHighlightAnnotation(annotation) {
24917
- return annotation.subtype === "Highlight";
24918
- }
24919
- function isUnderlineAnnotation(annotation) {
24920
- return annotation.subtype === "Underline";
24921
- }
24922
- function isSquigglyAnnotation(annotation) {
24923
- return annotation.subtype === "Squiggly";
24924
- }
24925
- function isStrikeOutAnnotation(annotation) {
24926
- return annotation.subtype === "StrikeOut";
24927
- }
24928
- function isStampAnnotation(annotation) {
24929
- return annotation.subtype === "Stamp";
24930
- }
24931
- function isCaretAnnotation(annotation) {
24932
- return annotation.subtype === "Caret";
24933
- }
24934
- function isInkAnnotation(annotation) {
24935
- return annotation.subtype === "Ink";
24936
- }
24937
- function isPopupAnnotation(annotation) {
24938
- return annotation.subtype === "Popup";
24939
- }
24940
- function isFileAttachmentAnnotation(annotation) {
24941
- return annotation.subtype === "FileAttachment";
24942
- }
24943
- function isSoundAnnotation(annotation) {
24944
- return annotation.subtype === "Sound";
24945
- }
24946
- function isMovieAnnotation(annotation) {
24947
- return annotation.subtype === "Movie";
24948
- }
24949
- function isWidgetAnnotation(annotation) {
24950
- return annotation.subtype === "Widget";
24951
- }
24952
- function isTextWidgetAnnotation(annotation) {
24953
- return annotation.subtype === "Widget" && annotation.fieldType === "Tx";
24954
- }
24955
- function isChoiceWidgetAnnotation(annotation) {
24956
- return annotation.subtype === "Widget" && annotation.fieldType === "Ch";
24957
- }
24958
- function isButtonWidgetAnnotation(annotation) {
24959
- return annotation.subtype === "Widget" && annotation.fieldType === "Btn";
24960
- }
24961
- function isScreenAnnotation(annotation) {
24962
- return annotation.subtype === "Screen";
24963
- }
24964
- function isPrinterMarkAnnotation(annotation) {
24965
- return annotation.subtype === "PrinterMark";
24966
- }
24967
- function isTrapNetAnnotation(annotation) {
24968
- return annotation.subtype === "TrapNet";
24969
- }
24970
- function isWatermarkAnnotation(annotation) {
24971
- return annotation.subtype === "Watermark";
24972
- }
24973
- function isThreeDAnnotation(annotation) {
24974
- return annotation.subtype === "3D";
24975
- }
24976
- function isRedactAnnotation(annotation) {
24977
- return annotation.subtype === "Redact";
24978
- }
24979
-
24980
24675
  // src/lib/SvgCanvasContext.ts
24981
24676
  function parseTransformMatrix(transformString) {
24982
24677
  const values = transformString.match(/matrix\(([^)]+)\)/)?.[1].split(",").map(Number);
@@ -25270,19 +24965,33 @@ var SvgCanvasContext = class {
25270
24965
  this._styleStack.push(this._getStyleState());
25271
24966
  this._transformMatrixStack.push(this.getTransform());
25272
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
+ }
25273
24974
  restore() {
25274
- const group = this._groupStack.pop();
25275
- if (!this._isTransformationGroup(this._currentGroup)) {
25276
- group.children = group.children.filter(
25277
- (child) => child !== this._currentGroup
25278
- );
25279
- 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);
25280
24994
  }
25281
- this._currentGroup = group;
25282
- const state = this._styleStack.pop();
25283
- this._applyStyleState(state);
25284
- const { a, b, c, d, e, f } = this._transformMatrixStack.pop();
25285
- this.setTransform(a, b, c, d, e, f);
25286
24995
  }
25287
24996
  beginPath() {
25288
24997
  this._currentPosition = {};
@@ -25620,6 +25329,9 @@ var SvgCanvasContext = class {
25620
25329
  const fillRuleValue = fillRule ?? path;
25621
25330
  this._currentStyle.fillRule = fillRuleValue;
25622
25331
  }
25332
+ if (path instanceof SvgPath2D && path.commands.length === 0) {
25333
+ return;
25334
+ }
25623
25335
  const id = `clip_${crypto.randomUUID()}`;
25624
25336
  const clipPath = {
25625
25337
  tag: "clipPath",
@@ -25825,64 +25537,405 @@ var SvgCanvasContext = class {
25825
25537
  if (command === "Z" && this._currentElement.attrs.d.endsWith("Z")) {
25826
25538
  return;
25827
25539
  }
25828
- this._currentElement.attrs.d += ` ${command}`;
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
+ }
25829
25717
  } else {
25830
- this._currentElement.attrs.d = command;
25831
- this._currentGroup.children.push(this._currentElement);
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
+ }
25832
25794
  }
25833
25795
  }
25834
25796
  }
25835
- _applyText(text, x, y, action) {
25836
- const parent = this._currentGroup;
25837
- const style = parseFontStyle(this._currentStyle.font);
25838
- const textElement = {
25839
- tag: "text",
25840
- attrs: {
25841
- x: `${x}`,
25842
- y: `${y}`,
25843
- "font-family": style.fontFamilies.join(", "),
25844
- "font-size": style.fontSize,
25845
- "font-style": style.fontStyle,
25846
- "font-weight": style.fontWeight,
25847
- "text-anchor": getTextAnchor(this._currentStyle.textAlign),
25848
- "dominant-baseline": getDominantBaseline(
25849
- this._currentStyle.textBaseline
25850
- )
25851
- },
25852
- text
25853
- };
25854
- this._applyTransformation(textElement);
25855
- this._applyStyle(textElement, action);
25856
- 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
+ });
25857
25806
  }
25858
- _clearCanvas() {
25859
- const rootGroup = this._root.children[1];
25860
- this._defs.children = [];
25861
- this._root.children = this._root.children.filter(
25862
- (child) => child === rootGroup
25863
- );
25864
- this._currentGroup = {
25865
- tag: "g",
25866
- attrs: {},
25867
- children: []
25868
- };
25869
- this._root.children.push(this._currentGroup);
25870
- 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([]);
25871
25849
  }
25872
25850
  };
25873
- async function createSvgContext(width, height) {
25874
- const canvas = await createCanvas(width, height);
25875
- return new SvgCanvasContext(
25876
- width,
25877
- height,
25878
- canvas
25879
- );
25851
+
25852
+ // src/lib/AnnotationData.ts
25853
+ function isTextAnnotation(annotation) {
25854
+ return annotation.subtype === "Text";
25880
25855
  }
25881
- async function toSvgNode(ctx) {
25882
- if (!(ctx instanceof SvgCanvasContext)) {
25883
- throw new Error("Invalid context");
25884
- }
25885
- 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";
25886
25939
  }
25887
25940
 
25888
25941
  // src/lib/TextLayer.ts
@@ -26445,6 +26498,7 @@ export {
26445
26498
  isWatermarkAnnotation,
26446
26499
  isWidgetAnnotation,
26447
26500
  loadDefaultFonts,
26501
+ makeSerializable,
26448
26502
  noContextMenu,
26449
26503
  normalizeUnicode,
26450
26504
  setLayerDimensions,