@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.
@@ -6847,19 +6847,27 @@ var CanvasExtraState = class {
6847
6847
  clone.clipBox = this.clipBox.slice();
6848
6848
  return clone;
6849
6849
  }
6850
- updateRectMinMax(transform, rect) {
6851
- const p1 = [rect[0], rect[1]];
6852
- Util.applyTransform(p1, transform);
6853
- const p2 = [rect[2], rect[3]];
6854
- Util.applyTransform(p2, transform);
6855
- const p3 = [rect[0], rect[3]];
6856
- Util.applyTransform(p3, transform);
6857
- const p4 = [rect[2], rect[1]];
6858
- Util.applyTransform(p4, transform);
6859
- this.minX = Math.min(this.minX, p1[0], p2[0], p3[0], p4[0]);
6860
- this.minY = Math.min(this.minY, p1[1], p2[1], p3[1], p4[1]);
6861
- this.maxX = Math.max(this.maxX, p1[0], p2[0], p3[0], p4[0]);
6862
- this.maxY = Math.max(this.maxY, p1[1], p2[1], p3[1], p4[1]);
6850
+ updateRectMinMax([m0, m1, m2, m3, m4, m5], [r0, r1, r2, r3]) {
6851
+ const m0r0m4 = m0 * r0 + m4;
6852
+ const m0r2m4 = m0 * r2 + m4;
6853
+ const m1r0m5 = m1 * r0 + m5;
6854
+ const m1r2m5 = m1 * r2 + m5;
6855
+ const m2r1 = m2 * r1;
6856
+ const m2r3 = m2 * r3;
6857
+ const m3r1 = m3 * r1;
6858
+ const m3r3 = m3 * r3;
6859
+ const a0 = m0r0m4 + m2r1;
6860
+ const a1 = m0r2m4 + m2r3;
6861
+ const a2 = m0r0m4 + m2r3;
6862
+ const a3 = m0r2m4 + m2r1;
6863
+ const b0 = m1r0m5 + m3r1;
6864
+ const b1 = m1r2m5 + m3r3;
6865
+ const b2 = m1r0m5 + m3r3;
6866
+ const b3 = m1r2m5 + m3r1;
6867
+ this.minX = Math.min(this.minX, a0, a1, a2, a3);
6868
+ this.maxX = Math.max(this.maxX, a0, a1, a2, a3);
6869
+ this.minY = Math.min(this.minY, b0, b1, b2, b3);
6870
+ this.maxY = Math.max(this.maxY, b0, b1, b2, b3);
6863
6871
  }
6864
6872
  getPathBoundingBox(pathType = PathType.FILL, transform = null) {
6865
6873
  const box = [this.minX, this.minY, this.maxX, this.maxY];
@@ -8323,7 +8331,7 @@ var _CanvasGraphics = class _CanvasGraphics {
8323
8331
  }
8324
8332
  this.baseTransform = getCurrentTransform(this.ctx);
8325
8333
  if (bbox) {
8326
- this.current.updateRectMinMax(getCurrentTransform(this.ctx), bbox);
8334
+ this.current.updateRectMinMax(this.baseTransform, bbox);
8327
8335
  const [x0, y0, x1, y1] = bbox;
8328
8336
  const clip = new SvgPath2D();
8329
8337
  clip.rect(x0, y0, x1 - x0, y1 - y0);
@@ -24354,319 +24362,6 @@ if (false) {
24354
24362
  };
24355
24363
  }
24356
24364
 
24357
- // src/lib/utils.ts
24358
- async function canvasToData(canvas) {
24359
- if ("toBlob" in canvas) {
24360
- const blob = await new Promise(
24361
- (resolve) => canvas.toBlob((data) => resolve(data))
24362
- );
24363
- if (!blob) {
24364
- throw new Error("Failed to generate graphics");
24365
- }
24366
- return new Uint8Array(await blob.arrayBuffer());
24367
- }
24368
- const buffer = await canvas.encode("png");
24369
- return new Uint8Array(buffer);
24370
- }
24371
- async function toDataUrl(data) {
24372
- if (typeof FileReader !== "undefined") {
24373
- return new Promise((resolve) => {
24374
- const reader = new FileReader();
24375
- reader.onload = () => {
24376
- resolve(reader.result);
24377
- };
24378
- reader.readAsDataURL(new Blob([data], { type: "image/png" }));
24379
- });
24380
- }
24381
- return `data:image/png;base64,${Buffer.from(data).toString("base64")}`;
24382
- }
24383
-
24384
- // src/lib/PDFPageProxy.ts
24385
- var getAnnotations = PDFPageProxy.prototype.getAnnotations;
24386
- PDFPageProxy.prototype.getAnnotations = async function(params) {
24387
- const annotations = await getAnnotations.call(this, params);
24388
- const operatorList = await this.getOperatorList({
24389
- annotationMode: AnnotationMode.ENABLE_FORMS
24390
- });
24391
- const annotationsOperatorList = /* @__PURE__ */ new Map();
24392
- let currentId = null;
24393
- let currentList = [];
24394
- for (const [index, op] of operatorList.fnArray.entries()) {
24395
- const args = operatorList.argsArray[index];
24396
- if (op === OPS.beginAnnotation) {
24397
- currentId = args[0];
24398
- currentList.push({ fn: op, args });
24399
- } else if (op === OPS.endAnnotation && currentId) {
24400
- currentList.push({ fn: op, args });
24401
- annotationsOperatorList.set(currentId, currentList);
24402
- currentList = [];
24403
- } else if (currentId) {
24404
- currentList.push({ fn: op, args });
24405
- }
24406
- }
24407
- for (const annotation of annotations) {
24408
- const opList = annotationsOperatorList.get(annotation.id);
24409
- if (opList) {
24410
- if (annotation.subtype === "Stamp") {
24411
- try {
24412
- const canvasFactory = isNodeJS ? new NodeCanvasFactory({}) : new DOMCanvasFactory({});
24413
- const scale = 2;
24414
- const viewport = this.getViewport({ scale });
24415
- const { context: canvasContext } = canvasFactory.create(
24416
- viewport.width,
24417
- viewport.height
24418
- );
24419
- const gsx = new CanvasGraphics(
24420
- canvasContext,
24421
- this.commonObjs,
24422
- this.objs,
24423
- canvasFactory,
24424
- this.filterFactory,
24425
- { optionalContentConfig: null }
24426
- );
24427
- gsx.beginDrawing({
24428
- transform: null,
24429
- viewport
24430
- });
24431
- canvasContext.clearRect(0, 0, viewport.width, viewport.height);
24432
- gsx.executeOperatorList({
24433
- fnArray: opList.map((op) => op.fn),
24434
- argsArray: opList.map((op) => op.args)
24435
- });
24436
- const width = annotation.rect[2] - annotation.rect[0];
24437
- const height = annotation.rect[3] - annotation.rect[1];
24438
- const { context: croppedCanvasContext } = canvasFactory.create(
24439
- width * scale,
24440
- height * scale
24441
- );
24442
- croppedCanvasContext.drawImage(
24443
- canvasContext.canvas,
24444
- annotation.rect[0] * scale,
24445
- (this.view[3] - annotation.rect[1] - height) * scale,
24446
- width * scale,
24447
- height * scale,
24448
- 0,
24449
- 0,
24450
- width * scale,
24451
- height * scale
24452
- );
24453
- annotation.graphics = await canvasToData(croppedCanvasContext.canvas);
24454
- } catch (e) {
24455
- console.error(e);
24456
- }
24457
- } else {
24458
- let firstStroke = true;
24459
- let firstFill = true;
24460
- let currentFillColor = null;
24461
- let currentLineWidth = null;
24462
- for (const { fn, args } of opList) {
24463
- const finalFn = fn === OPS.constructPath ? args[0] : fn;
24464
- if (finalFn === OPS.fill) {
24465
- if (currentFillColor) {
24466
- annotation.interiorColor = currentFillColor;
24467
- currentFillColor = null;
24468
- }
24469
- firstFill = false;
24470
- continue;
24471
- }
24472
- if (fn === OPS.beginText) {
24473
- firstFill = false;
24474
- continue;
24475
- }
24476
- if (finalFn === OPS.stroke || finalFn === OPS.closeStroke) {
24477
- if (currentLineWidth === null) {
24478
- annotation.borderStyle.width = 1;
24479
- }
24480
- firstStroke = false;
24481
- currentLineWidth = null;
24482
- continue;
24483
- }
24484
- if (finalFn === OPS.fillStroke || finalFn === OPS.closeFillStroke) {
24485
- if (currentFillColor) {
24486
- annotation.interiorColor = currentFillColor;
24487
- currentFillColor = null;
24488
- }
24489
- if (currentLineWidth === null) {
24490
- annotation.borderStyle.width = 1;
24491
- }
24492
- firstStroke = false;
24493
- currentLineWidth = null;
24494
- break;
24495
- }
24496
- if (!firstFill && !firstStroke) {
24497
- break;
24498
- }
24499
- if (fn === OPS.setLineWidth && firstStroke) {
24500
- currentLineWidth = args[0];
24501
- annotation.borderStyle.width = currentLineWidth;
24502
- }
24503
- if (fn === OPS.setStrokeRGBColor && firstStroke) {
24504
- annotation.color = args;
24505
- }
24506
- if (fn === OPS.setFillRGBColor && firstFill) {
24507
- currentFillColor = args;
24508
- }
24509
- if (fn === OPS.setGState) {
24510
- for (const entry of args[0]) {
24511
- if (!Array.isArray(entry)) {
24512
- continue;
24513
- }
24514
- if (entry[0] === "ca" && firstFill) {
24515
- annotation.interiorColorOpacity = entry[1];
24516
- }
24517
- if (entry[0] === "CA" && firstStroke) {
24518
- annotation.borderColorOpacity = entry[1];
24519
- }
24520
- }
24521
- }
24522
- }
24523
- }
24524
- }
24525
- }
24526
- return annotations;
24527
- };
24528
-
24529
- // src/lib/StandardFontDataFactory.ts
24530
- var StandardFontDataFactory = class extends BaseStandardFontDataFactory {
24531
- constructor() {
24532
- super({
24533
- baseUrl: null
24534
- });
24535
- }
24536
- /**
24537
- * Fetch the corresponding standard font data.
24538
- * We need to use specific dynamic imports for each font file for the bundler to include them.
24539
- */
24540
- async fetch({ filename }) {
24541
- switch (filename) {
24542
- case "FoxitDingbats.pfb":
24543
- return import("./FoxitDingbats-SB6TO3S5.js").then((module) => module.default);
24544
- case "FoxitFixed.pfb":
24545
- return import("./FoxitFixed-UIGSMBQB.js").then(
24546
- (module) => module.default
24547
- );
24548
- case "FoxitFixedBold.pfb":
24549
- return import("./FoxitFixedBold-2PAEIZAT.js").then((module) => module.default);
24550
- case "FoxitFixedBoldItalic.pfb":
24551
- return import("./FoxitFixedBoldItalic-OSQUQDEE.js").then((module) => module.default);
24552
- case "FoxitFixedItalic.pfb":
24553
- return import("./FoxitFixedItalic-W73RDK22.js").then((module) => module.default);
24554
- case "FoxitSerif.pfb":
24555
- return import("./FoxitSerif-3HH3SOZF.js").then(
24556
- (module) => module.default
24557
- );
24558
- case "FoxitSerifBold.pfb":
24559
- return import("./FoxitSerifBold-HXP2QOO7.js").then((module) => module.default);
24560
- case "FoxitSerifBoldItalic.pfb":
24561
- return import("./FoxitSerifBoldItalic-FZXLNWD7.js").then((module) => module.default);
24562
- case "FoxitSerifItalic.pfb":
24563
- return import("./FoxitSerifItalic-WQFHUBI2.js").then((module) => module.default);
24564
- case "FoxitSymbol.pfb":
24565
- return import("./FoxitSymbol-OVWU7LKS.js").then(
24566
- (module) => module.default
24567
- );
24568
- case "LiberationSans-Bold.ttf":
24569
- return import("./LiberationSans-Bold-GSJN42N5.js").then((module) => module.default);
24570
- case "LiberationSans-BoldItalic.ttf":
24571
- return import("./LiberationSans-BoldItalic-UCPQJ3L2.js").then((module) => module.default);
24572
- case "LiberationSans-Italic.ttf":
24573
- return import("./LiberationSans-Italic-6CIHEALY.js").then((module) => module.default);
24574
- case "LiberationSans-Regular.ttf":
24575
- return import("./LiberationSans-Regular-KIF3IRJY.js").then((module) => module.default);
24576
- }
24577
- return Uint8Array.from([]);
24578
- }
24579
- };
24580
-
24581
- // src/lib/AnnotationData.ts
24582
- function isTextAnnotation(annotation) {
24583
- return annotation.subtype === "Text";
24584
- }
24585
- function isLinkAnnotation(annotation) {
24586
- return annotation.subtype === "Link";
24587
- }
24588
- function isFreeTextAnnotation(annotation) {
24589
- return annotation.subtype === "FreeText";
24590
- }
24591
- function isLineAnnotation(annotation) {
24592
- return annotation.subtype === "Line";
24593
- }
24594
- function isSquareAnnotation(annotation) {
24595
- return annotation.subtype === "Square";
24596
- }
24597
- function isCircleAnnotation(annotation) {
24598
- return annotation.subtype === "Circle";
24599
- }
24600
- function isPolygonAnnotation(annotation) {
24601
- return annotation.subtype === "Polygon";
24602
- }
24603
- function isPolylineAnnotation(annotation) {
24604
- return annotation.subtype === "PolyLine";
24605
- }
24606
- function isHighlightAnnotation(annotation) {
24607
- return annotation.subtype === "Highlight";
24608
- }
24609
- function isUnderlineAnnotation(annotation) {
24610
- return annotation.subtype === "Underline";
24611
- }
24612
- function isSquigglyAnnotation(annotation) {
24613
- return annotation.subtype === "Squiggly";
24614
- }
24615
- function isStrikeOutAnnotation(annotation) {
24616
- return annotation.subtype === "StrikeOut";
24617
- }
24618
- function isStampAnnotation(annotation) {
24619
- return annotation.subtype === "Stamp";
24620
- }
24621
- function isCaretAnnotation(annotation) {
24622
- return annotation.subtype === "Caret";
24623
- }
24624
- function isInkAnnotation(annotation) {
24625
- return annotation.subtype === "Ink";
24626
- }
24627
- function isPopupAnnotation(annotation) {
24628
- return annotation.subtype === "Popup";
24629
- }
24630
- function isFileAttachmentAnnotation(annotation) {
24631
- return annotation.subtype === "FileAttachment";
24632
- }
24633
- function isSoundAnnotation(annotation) {
24634
- return annotation.subtype === "Sound";
24635
- }
24636
- function isMovieAnnotation(annotation) {
24637
- return annotation.subtype === "Movie";
24638
- }
24639
- function isWidgetAnnotation(annotation) {
24640
- return annotation.subtype === "Widget";
24641
- }
24642
- function isTextWidgetAnnotation(annotation) {
24643
- return annotation.subtype === "Widget" && annotation.fieldType === "Tx";
24644
- }
24645
- function isChoiceWidgetAnnotation(annotation) {
24646
- return annotation.subtype === "Widget" && annotation.fieldType === "Ch";
24647
- }
24648
- function isButtonWidgetAnnotation(annotation) {
24649
- return annotation.subtype === "Widget" && annotation.fieldType === "Btn";
24650
- }
24651
- function isScreenAnnotation(annotation) {
24652
- return annotation.subtype === "Screen";
24653
- }
24654
- function isPrinterMarkAnnotation(annotation) {
24655
- return annotation.subtype === "PrinterMark";
24656
- }
24657
- function isTrapNetAnnotation(annotation) {
24658
- return annotation.subtype === "TrapNet";
24659
- }
24660
- function isWatermarkAnnotation(annotation) {
24661
- return annotation.subtype === "Watermark";
24662
- }
24663
- function isThreeDAnnotation(annotation) {
24664
- return annotation.subtype === "3D";
24665
- }
24666
- function isRedactAnnotation(annotation) {
24667
- return annotation.subtype === "Redact";
24668
- }
24669
-
24670
24365
  // src/lib/SvgCanvasContext.ts
24671
24366
  function parseTransformMatrix(transformString) {
24672
24367
  const values = transformString.match(/matrix\(([^)]+)\)/)?.[1].split(",").map(Number);
@@ -24960,19 +24655,33 @@ var SvgCanvasContext = class {
24960
24655
  this._styleStack.push(this._getStyleState());
24961
24656
  this._transformMatrixStack.push(this.getTransform());
24962
24657
  }
24658
+ clearRect(x, y, width, height) {
24659
+ if (x !== 0 || y !== 0 || this._width !== width || this._height !== height) {
24660
+ throw new Error("clearRect is not supported in SVG");
24661
+ }
24662
+ this._clearCanvas();
24663
+ }
24963
24664
  restore() {
24964
- const group = this._groupStack.pop();
24965
- if (!this._isTransformationGroup(this._currentGroup)) {
24966
- group.children = group.children.filter(
24967
- (child) => child !== this._currentGroup
24968
- );
24969
- group.children.push(...this._currentGroup.children);
24665
+ if (this._groupStack.length) {
24666
+ const group = this._groupStack.pop();
24667
+ if (!this._isTransformationGroup(this._currentGroup)) {
24668
+ group.children = group.children.filter(
24669
+ (child) => child !== this._currentGroup
24670
+ );
24671
+ group.children.push(...this._currentGroup.children);
24672
+ }
24673
+ this._currentGroup = group;
24674
+ } else {
24675
+ this._currentGroup = this._root;
24676
+ }
24677
+ if (this._styleStack.length) {
24678
+ const state = this._styleStack.pop();
24679
+ this._applyStyleState(state);
24680
+ }
24681
+ if (this._transformMatrixStack.length) {
24682
+ const { a, b, c, d, e, f } = this._transformMatrixStack.pop();
24683
+ this.setTransform(a, b, c, d, e, f);
24970
24684
  }
24971
- this._currentGroup = group;
24972
- const state = this._styleStack.pop();
24973
- this._applyStyleState(state);
24974
- const { a, b, c, d, e, f } = this._transformMatrixStack.pop();
24975
- this.setTransform(a, b, c, d, e, f);
24976
24685
  }
24977
24686
  beginPath() {
24978
24687
  this._currentPosition = {};
@@ -25310,6 +25019,9 @@ var SvgCanvasContext = class {
25310
25019
  const fillRuleValue = fillRule ?? path;
25311
25020
  this._currentStyle.fillRule = fillRuleValue;
25312
25021
  }
25022
+ if (path instanceof SvgPath2D && path.commands.length === 0) {
25023
+ return;
25024
+ }
25313
25025
  const id = `clip_${crypto.randomUUID()}`;
25314
25026
  const clipPath = {
25315
25027
  tag: "clipPath",
@@ -25515,64 +25227,405 @@ var SvgCanvasContext = class {
25515
25227
  if (command === "Z" && this._currentElement.attrs.d.endsWith("Z")) {
25516
25228
  return;
25517
25229
  }
25518
- this._currentElement.attrs.d += ` ${command}`;
25230
+ this._currentElement.attrs.d += ` ${command}`;
25231
+ } else {
25232
+ this._currentElement.attrs.d = command;
25233
+ this._currentGroup.children.push(this._currentElement);
25234
+ }
25235
+ }
25236
+ }
25237
+ _applyText(text, x, y, action) {
25238
+ const parent = this._currentGroup;
25239
+ const style = parseFontStyle(this._currentStyle.font);
25240
+ const textElement = {
25241
+ tag: "text",
25242
+ attrs: {
25243
+ x: `${x}`,
25244
+ y: `${y}`,
25245
+ "font-family": style.fontFamilies.join(", "),
25246
+ "font-size": style.fontSize,
25247
+ "font-style": style.fontStyle,
25248
+ "font-weight": style.fontWeight,
25249
+ "text-anchor": getTextAnchor(this._currentStyle.textAlign),
25250
+ "dominant-baseline": getDominantBaseline(
25251
+ this._currentStyle.textBaseline
25252
+ )
25253
+ },
25254
+ text
25255
+ };
25256
+ this._applyTransformation(textElement);
25257
+ this._applyStyle(textElement, action);
25258
+ parent.children.push(textElement);
25259
+ }
25260
+ _clearCanvas() {
25261
+ this._defs.children = [];
25262
+ this._root.children = [this._defs];
25263
+ this._currentGroup = this._root;
25264
+ this._groupStack = [];
25265
+ }
25266
+ };
25267
+ async function createSvgContext(width, height) {
25268
+ const canvas = await createCanvas(width, height);
25269
+ return new SvgCanvasContext(
25270
+ width,
25271
+ height,
25272
+ canvas
25273
+ );
25274
+ }
25275
+ async function toSvgNode(ctx) {
25276
+ if (!(ctx instanceof SvgCanvasContext)) {
25277
+ throw new Error("Invalid context");
25278
+ }
25279
+ return ctx.getNode();
25280
+ }
25281
+
25282
+ // src/lib/utils.ts
25283
+ async function canvasToData(canvas) {
25284
+ if ("toBlob" in canvas) {
25285
+ const blob = await new Promise(
25286
+ (resolve) => canvas.toBlob((data) => resolve(data))
25287
+ );
25288
+ if (!blob) {
25289
+ throw new Error("Failed to generate graphics");
25290
+ }
25291
+ return new Uint8Array(await blob.arrayBuffer());
25292
+ }
25293
+ const buffer = await canvas.encode("png");
25294
+ return new Uint8Array(buffer);
25295
+ }
25296
+ async function toDataUrl(data, type = "image/png") {
25297
+ if (typeof FileReader !== "undefined") {
25298
+ return new Promise((resolve) => {
25299
+ const reader = new FileReader();
25300
+ reader.onload = () => {
25301
+ resolve(reader.result);
25302
+ };
25303
+ reader.readAsDataURL(new Blob([data], { type }));
25304
+ });
25305
+ }
25306
+ return `data:${type};base64,${Buffer.from(data).toString("base64")}`;
25307
+ }
25308
+ function makeSerializable(object) {
25309
+ if (typeof object !== "object" || object === null) {
25310
+ return object;
25311
+ }
25312
+ 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) {
25313
+ return makeSerializable(Array.from(object));
25314
+ }
25315
+ if (object instanceof BigInt64Array || object instanceof BigUint64Array) {
25316
+ return makeSerializable(Array.from(object));
25317
+ }
25318
+ if (Array.isArray(object)) {
25319
+ return object.map(makeSerializable);
25320
+ }
25321
+ return Object.fromEntries(
25322
+ Object.entries(object).map(([key, value]) => [
25323
+ key,
25324
+ makeSerializable(value)
25325
+ ])
25326
+ );
25327
+ }
25328
+
25329
+ // src/lib/PDFPageProxy.ts
25330
+ var getAnnotations = PDFPageProxy.prototype.getAnnotations;
25331
+ PDFPageProxy.prototype.getAnnotations = async function(params) {
25332
+ const annotations = await getAnnotations.call(this, params);
25333
+ const operatorList = await this.getOperatorList({
25334
+ annotationMode: AnnotationMode.ENABLE_FORMS
25335
+ });
25336
+ const annotationsOperatorList = /* @__PURE__ */ new Map();
25337
+ let currentId = null;
25338
+ let currentList = [];
25339
+ for (const [index, op] of operatorList.fnArray.entries()) {
25340
+ const args = operatorList.argsArray[index];
25341
+ if (op === OPS.beginAnnotation) {
25342
+ currentId = args[0];
25343
+ currentList.push({ fn: op, args });
25344
+ } else if (op === OPS.endAnnotation && currentId) {
25345
+ currentList.push({ fn: op, args });
25346
+ annotationsOperatorList.set(currentId, currentList);
25347
+ currentList = [];
25348
+ } else if (currentId) {
25349
+ currentList.push({ fn: op, args });
25350
+ }
25351
+ }
25352
+ for (const annotation of annotations) {
25353
+ const opList = annotationsOperatorList.get(annotation.id);
25354
+ if (opList) {
25355
+ if (annotation.subtype === "Stamp") {
25356
+ try {
25357
+ const canvasFactory = isNodeJS ? new NodeCanvasFactory({}) : new DOMCanvasFactory({});
25358
+ const scale = 2;
25359
+ const viewport = this.getViewport({ scale });
25360
+ const svgContext = await createSvgContext(
25361
+ viewport.width,
25362
+ viewport.height
25363
+ );
25364
+ const gsx = new CanvasGraphics(
25365
+ svgContext,
25366
+ this.commonObjs,
25367
+ this.objs,
25368
+ canvasFactory,
25369
+ this.filterFactory,
25370
+ { optionalContentConfig: null }
25371
+ );
25372
+ gsx.beginDrawing({
25373
+ transform: null,
25374
+ viewport
25375
+ });
25376
+ svgContext.clearRect(0, 0, viewport.width, viewport.height);
25377
+ gsx.executeOperatorList({
25378
+ fnArray: opList.map((op) => op.fn),
25379
+ argsArray: opList.map((op) => op.args)
25380
+ });
25381
+ const width = annotation.rect[2] - annotation.rect[0];
25382
+ const height = annotation.rect[3] - annotation.rect[1];
25383
+ annotation.graphics = {
25384
+ tag: "svg",
25385
+ attrs: {
25386
+ version: "1.1",
25387
+ xmlns: "http://www.w3.org/2000/svg",
25388
+ width: `${width}`,
25389
+ height: `${height}`,
25390
+ viewBox: `0 0 ${width * scale} ${height * scale}`,
25391
+ preserveAspectRatio: "none",
25392
+ "xml:space": "preserve"
25393
+ },
25394
+ children: [
25395
+ {
25396
+ tag: "g",
25397
+ attrs: {
25398
+ transform: `translate(-${annotation.rect[0] * scale}, -${(this.view[3] - annotation.rect[1] - height) * scale})`
25399
+ },
25400
+ children: [await toSvgNode(svgContext)]
25401
+ }
25402
+ ]
25403
+ };
25404
+ } catch (e) {
25405
+ console.error(e);
25406
+ }
25519
25407
  } else {
25520
- this._currentElement.attrs.d = command;
25521
- this._currentGroup.children.push(this._currentElement);
25408
+ let firstStroke = true;
25409
+ let firstFill = true;
25410
+ let firstText = true;
25411
+ let currentFillColor = null;
25412
+ let currentLineWidth = null;
25413
+ for (const { fn, args } of opList) {
25414
+ const finalFn = fn === OPS.constructPath ? args[0] : fn;
25415
+ if (finalFn === OPS.fill) {
25416
+ if (currentFillColor && firstFill) {
25417
+ annotation.interiorColor = currentFillColor;
25418
+ currentFillColor = null;
25419
+ }
25420
+ firstFill = false;
25421
+ continue;
25422
+ }
25423
+ if (fn === OPS.beginText) {
25424
+ firstFill = false;
25425
+ continue;
25426
+ }
25427
+ if (finalFn === OPS.stroke || finalFn === OPS.closeStroke) {
25428
+ if (currentLineWidth === null && firstStroke) {
25429
+ annotation.borderStyle.width = 1;
25430
+ }
25431
+ firstStroke = false;
25432
+ currentLineWidth = null;
25433
+ continue;
25434
+ }
25435
+ if (finalFn === OPS.fillStroke || finalFn === OPS.closeFillStroke) {
25436
+ if (currentFillColor && firstFill) {
25437
+ annotation.interiorColor = currentFillColor;
25438
+ currentFillColor = null;
25439
+ }
25440
+ if (currentLineWidth === null && firstStroke) {
25441
+ annotation.borderStyle.width = 1;
25442
+ }
25443
+ firstStroke = false;
25444
+ currentLineWidth = null;
25445
+ break;
25446
+ }
25447
+ if (!firstFill && !firstStroke) {
25448
+ break;
25449
+ }
25450
+ if (fn === OPS.setLineWidth && firstStroke) {
25451
+ currentLineWidth = args[0];
25452
+ annotation.borderStyle.width = currentLineWidth;
25453
+ }
25454
+ if (fn === OPS.setStrokeRGBColor && firstStroke) {
25455
+ annotation.color = args;
25456
+ }
25457
+ if (fn === OPS.setFillRGBColor && firstFill) {
25458
+ currentFillColor = args;
25459
+ }
25460
+ if (fn === OPS.setGState) {
25461
+ for (const entry of args[0]) {
25462
+ if (!Array.isArray(entry)) {
25463
+ continue;
25464
+ }
25465
+ if (entry[0] === "ca" && firstFill) {
25466
+ annotation.interiorColorOpacity = entry[1];
25467
+ }
25468
+ if (entry[0] === "CA" && firstStroke) {
25469
+ annotation.borderColorOpacity = entry[1];
25470
+ }
25471
+ }
25472
+ }
25473
+ if (fn === OPS.setFont && firstText) {
25474
+ const fontName = args[0];
25475
+ const font = fontName.startsWith("g_") ? this.commonObjs.get(fontName) : this.objs.get(fontName);
25476
+ if (font) {
25477
+ annotation.defaultAppearanceData.fontName = font.fallbackName;
25478
+ }
25479
+ }
25480
+ if (fn === OPS.endText) {
25481
+ firstText = false;
25482
+ }
25483
+ }
25522
25484
  }
25523
25485
  }
25524
25486
  }
25525
- _applyText(text, x, y, action) {
25526
- const parent = this._currentGroup;
25527
- const style = parseFontStyle(this._currentStyle.font);
25528
- const textElement = {
25529
- tag: "text",
25530
- attrs: {
25531
- x: `${x}`,
25532
- y: `${y}`,
25533
- "font-family": style.fontFamilies.join(", "),
25534
- "font-size": style.fontSize,
25535
- "font-style": style.fontStyle,
25536
- "font-weight": style.fontWeight,
25537
- "text-anchor": getTextAnchor(this._currentStyle.textAlign),
25538
- "dominant-baseline": getDominantBaseline(
25539
- this._currentStyle.textBaseline
25540
- )
25541
- },
25542
- text
25543
- };
25544
- this._applyTransformation(textElement);
25545
- this._applyStyle(textElement, action);
25546
- parent.children.push(textElement);
25487
+ return makeSerializable(annotations);
25488
+ };
25489
+
25490
+ // src/lib/StandardFontDataFactory.ts
25491
+ var StandardFontDataFactory = class extends BaseStandardFontDataFactory {
25492
+ constructor() {
25493
+ super({
25494
+ baseUrl: null
25495
+ });
25547
25496
  }
25548
- _clearCanvas() {
25549
- const rootGroup = this._root.children[1];
25550
- this._defs.children = [];
25551
- this._root.children = this._root.children.filter(
25552
- (child) => child === rootGroup
25553
- );
25554
- this._currentGroup = {
25555
- tag: "g",
25556
- attrs: {},
25557
- children: []
25558
- };
25559
- this._root.children.push(this._currentGroup);
25560
- this._groupStack = [];
25497
+ /**
25498
+ * Fetch the corresponding standard font data.
25499
+ * We need to use specific dynamic imports for each font file for the bundler to include them.
25500
+ */
25501
+ async fetch({ filename }) {
25502
+ switch (filename) {
25503
+ case "FoxitDingbats.pfb":
25504
+ return import("./FoxitDingbats-SB6TO3S5.js").then((module) => module.default);
25505
+ case "FoxitFixed.pfb":
25506
+ return import("./FoxitFixed-UIGSMBQB.js").then(
25507
+ (module) => module.default
25508
+ );
25509
+ case "FoxitFixedBold.pfb":
25510
+ return import("./FoxitFixedBold-2PAEIZAT.js").then((module) => module.default);
25511
+ case "FoxitFixedBoldItalic.pfb":
25512
+ return import("./FoxitFixedBoldItalic-OSQUQDEE.js").then((module) => module.default);
25513
+ case "FoxitFixedItalic.pfb":
25514
+ return import("./FoxitFixedItalic-W73RDK22.js").then((module) => module.default);
25515
+ case "FoxitSerif.pfb":
25516
+ return import("./FoxitSerif-3HH3SOZF.js").then(
25517
+ (module) => module.default
25518
+ );
25519
+ case "FoxitSerifBold.pfb":
25520
+ return import("./FoxitSerifBold-HXP2QOO7.js").then((module) => module.default);
25521
+ case "FoxitSerifBoldItalic.pfb":
25522
+ return import("./FoxitSerifBoldItalic-FZXLNWD7.js").then((module) => module.default);
25523
+ case "FoxitSerifItalic.pfb":
25524
+ return import("./FoxitSerifItalic-WQFHUBI2.js").then((module) => module.default);
25525
+ case "FoxitSymbol.pfb":
25526
+ return import("./FoxitSymbol-OVWU7LKS.js").then(
25527
+ (module) => module.default
25528
+ );
25529
+ case "LiberationSans-Bold.ttf":
25530
+ return import("./LiberationSans-Bold-GSJN42N5.js").then((module) => module.default);
25531
+ case "LiberationSans-BoldItalic.ttf":
25532
+ return import("./LiberationSans-BoldItalic-UCPQJ3L2.js").then((module) => module.default);
25533
+ case "LiberationSans-Italic.ttf":
25534
+ return import("./LiberationSans-Italic-6CIHEALY.js").then((module) => module.default);
25535
+ case "LiberationSans-Regular.ttf":
25536
+ return import("./LiberationSans-Regular-KIF3IRJY.js").then((module) => module.default);
25537
+ }
25538
+ return Uint8Array.from([]);
25561
25539
  }
25562
25540
  };
25563
- async function createSvgContext(width, height) {
25564
- const canvas = await createCanvas(width, height);
25565
- return new SvgCanvasContext(
25566
- width,
25567
- height,
25568
- canvas
25569
- );
25541
+
25542
+ // src/lib/AnnotationData.ts
25543
+ function isTextAnnotation(annotation) {
25544
+ return annotation.subtype === "Text";
25570
25545
  }
25571
- async function toSvgNode(ctx) {
25572
- if (!(ctx instanceof SvgCanvasContext)) {
25573
- throw new Error("Invalid context");
25574
- }
25575
- return ctx.getNode();
25546
+ function isLinkAnnotation(annotation) {
25547
+ return annotation.subtype === "Link";
25548
+ }
25549
+ function isFreeTextAnnotation(annotation) {
25550
+ return annotation.subtype === "FreeText";
25551
+ }
25552
+ function isLineAnnotation(annotation) {
25553
+ return annotation.subtype === "Line";
25554
+ }
25555
+ function isSquareAnnotation(annotation) {
25556
+ return annotation.subtype === "Square";
25557
+ }
25558
+ function isCircleAnnotation(annotation) {
25559
+ return annotation.subtype === "Circle";
25560
+ }
25561
+ function isPolygonAnnotation(annotation) {
25562
+ return annotation.subtype === "Polygon";
25563
+ }
25564
+ function isPolylineAnnotation(annotation) {
25565
+ return annotation.subtype === "PolyLine";
25566
+ }
25567
+ function isHighlightAnnotation(annotation) {
25568
+ return annotation.subtype === "Highlight";
25569
+ }
25570
+ function isUnderlineAnnotation(annotation) {
25571
+ return annotation.subtype === "Underline";
25572
+ }
25573
+ function isSquigglyAnnotation(annotation) {
25574
+ return annotation.subtype === "Squiggly";
25575
+ }
25576
+ function isStrikeOutAnnotation(annotation) {
25577
+ return annotation.subtype === "StrikeOut";
25578
+ }
25579
+ function isStampAnnotation(annotation) {
25580
+ return annotation.subtype === "Stamp";
25581
+ }
25582
+ function isCaretAnnotation(annotation) {
25583
+ return annotation.subtype === "Caret";
25584
+ }
25585
+ function isInkAnnotation(annotation) {
25586
+ return annotation.subtype === "Ink";
25587
+ }
25588
+ function isPopupAnnotation(annotation) {
25589
+ return annotation.subtype === "Popup";
25590
+ }
25591
+ function isFileAttachmentAnnotation(annotation) {
25592
+ return annotation.subtype === "FileAttachment";
25593
+ }
25594
+ function isSoundAnnotation(annotation) {
25595
+ return annotation.subtype === "Sound";
25596
+ }
25597
+ function isMovieAnnotation(annotation) {
25598
+ return annotation.subtype === "Movie";
25599
+ }
25600
+ function isWidgetAnnotation(annotation) {
25601
+ return annotation.subtype === "Widget";
25602
+ }
25603
+ function isTextWidgetAnnotation(annotation) {
25604
+ return annotation.subtype === "Widget" && annotation.fieldType === "Tx";
25605
+ }
25606
+ function isChoiceWidgetAnnotation(annotation) {
25607
+ return annotation.subtype === "Widget" && annotation.fieldType === "Ch";
25608
+ }
25609
+ function isButtonWidgetAnnotation(annotation) {
25610
+ return annotation.subtype === "Widget" && annotation.fieldType === "Btn";
25611
+ }
25612
+ function isScreenAnnotation(annotation) {
25613
+ return annotation.subtype === "Screen";
25614
+ }
25615
+ function isPrinterMarkAnnotation(annotation) {
25616
+ return annotation.subtype === "PrinterMark";
25617
+ }
25618
+ function isTrapNetAnnotation(annotation) {
25619
+ return annotation.subtype === "TrapNet";
25620
+ }
25621
+ function isWatermarkAnnotation(annotation) {
25622
+ return annotation.subtype === "Watermark";
25623
+ }
25624
+ function isThreeDAnnotation(annotation) {
25625
+ return annotation.subtype === "3D";
25626
+ }
25627
+ function isRedactAnnotation(annotation) {
25628
+ return annotation.subtype === "Redact";
25576
25629
  }
25577
25630
 
25578
25631
  // src/lib/TextLayer.ts
@@ -26135,6 +26188,7 @@ export {
26135
26188
  isWatermarkAnnotation,
26136
26189
  isWidgetAnnotation,
26137
26190
  loadDefaultFonts,
26191
+ makeSerializable,
26138
26192
  noContextMenu,
26139
26193
  normalizeUnicode,
26140
26194
  setLayerDimensions,