label-printer 0.4.6 → 0.4.7

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.
package/dist/index.mjs CHANGED
@@ -224,9 +224,9 @@ function pointsToDots(points, dpi) {
224
224
  }
225
225
 
226
226
  // src/helpers/ImageDataParser.ts
227
- function parsePNG(buffer) {
227
+ function parsePNG(buffer2) {
228
228
  const pngSignature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
229
- if (!buffer.subarray(0, 8).equals(pngSignature)) {
229
+ if (!buffer2.subarray(0, 8).equals(pngSignature)) {
230
230
  throw new Error("Invalid PNG file");
231
231
  }
232
232
  let width = 0, height = 0, bitDepth = 0, colorType = 0;
@@ -235,10 +235,10 @@ function parsePNG(buffer) {
235
235
  let transparency = null;
236
236
  let idatChunks = [];
237
237
  let offset = 8;
238
- while (offset < buffer.length) {
239
- const chunkLength = buffer.readUInt32BE(offset);
240
- const chunkType = buffer.subarray(offset + 4, offset + 8).toString("ascii");
241
- const chunkData = buffer.subarray(offset + 8, offset + 8 + chunkLength);
238
+ while (offset < buffer2.length) {
239
+ const chunkLength = buffer2.readUInt32BE(offset);
240
+ const chunkType = buffer2.subarray(offset + 4, offset + 8).toString("ascii");
241
+ const chunkData = buffer2.subarray(offset + 8, offset + 8 + chunkLength);
242
242
  if (chunkType === "IHDR") {
243
243
  width = chunkData.readUInt32BE(0);
244
244
  height = chunkData.readUInt32BE(4);
@@ -401,12 +401,12 @@ var ImageProcessor = class {
401
401
  * @param image Image to process (local file path, remote URL, data URL, or Blob)
402
402
  * @returns Promise with image data including width, height, pixel data, and bits per pixel
403
403
  */
404
- static getImageData(image) {
404
+ static getImageData(image2) {
405
405
  return __async(this, null, function* () {
406
406
  if (typeof window !== "undefined") {
407
- return this.getImageDataBrowser(image);
407
+ return this.getImageDataBrowser(image2);
408
408
  } else {
409
- return this.getImageDataNode(image);
409
+ return this.getImageDataNode(image2);
410
410
  }
411
411
  });
412
412
  }
@@ -416,7 +416,7 @@ var ImageProcessor = class {
416
416
  * @param image Image to process
417
417
  * @returns Promise with image data
418
418
  */
419
- static getImageDataBrowser(image) {
419
+ static getImageDataBrowser(image2) {
420
420
  return __async(this, null, function* () {
421
421
  console.log("Processing image in browser environment");
422
422
  return new Promise((resolve, reject) => {
@@ -446,12 +446,12 @@ var ImageProcessor = class {
446
446
  }
447
447
  };
448
448
  img.onerror = () => reject(new Error("Failed to load image"));
449
- if (typeof image === "string") {
450
- img.src = image;
449
+ if (typeof image2 === "string") {
450
+ img.src = image2;
451
451
  } else {
452
- const url = URL.createObjectURL(image);
452
+ const url2 = URL.createObjectURL(image2);
453
453
  img.onload = () => {
454
- URL.revokeObjectURL(url);
454
+ URL.revokeObjectURL(url2);
455
455
  resolve({
456
456
  data: new Uint8Array(0),
457
457
  // Will be set by the actual onload
@@ -460,7 +460,7 @@ var ImageProcessor = class {
460
460
  bitsPerPixel: 4
461
461
  });
462
462
  };
463
- img.src = url;
463
+ img.src = url2;
464
464
  }
465
465
  });
466
466
  });
@@ -471,18 +471,18 @@ var ImageProcessor = class {
471
471
  * @param image Image to process
472
472
  * @returns Promise with image data
473
473
  */
474
- static getImageDataNode(image) {
474
+ static getImageDataNode(image2) {
475
475
  return __async(this, null, function* () {
476
476
  console.log("Processing image in Node.js environment");
477
- if (image instanceof Blob) {
477
+ if (image2 instanceof Blob) {
478
478
  throw new Error("Blob input not supported in Node.js environment. Use file path or data URL instead.");
479
479
  }
480
- if (image.startsWith("data:")) {
481
- return this.getImageFromData(image);
482
- } else if (image.startsWith("http://") || image.startsWith("https://")) {
483
- return this.getImageFromUrl(image);
480
+ if (image2.startsWith("data:")) {
481
+ return this.getImageFromData(image2);
482
+ } else if (image2.startsWith("http://") || image2.startsWith("https://")) {
483
+ return this.getImageFromUrl(image2);
484
484
  } else {
485
- return this.getImageFromFile(image);
485
+ return this.getImageFromFile(image2);
486
486
  }
487
487
  });
488
488
  }
@@ -499,9 +499,9 @@ var ImageProcessor = class {
499
499
  if (!(mimeType == null ? void 0 : mimeType.startsWith("image/"))) {
500
500
  throw new Error("Invalid image data URL");
501
501
  }
502
- const buffer = Buffer.from(data, "base64");
502
+ const buffer2 = Buffer.from(data, "base64");
503
503
  const extension = mimeType.split("/")[1].toLowerCase();
504
- return this.parse(buffer, extension);
504
+ return this.parse(buffer2, extension);
505
505
  });
506
506
  }
507
507
  /**
@@ -511,8 +511,8 @@ var ImageProcessor = class {
511
511
  */
512
512
  static getImageFromFile(image) {
513
513
  return __async(this, null, function* () {
514
- const fs = yield import("fs");
515
- const path = yield import("path");
514
+ const fs = yield eval("require")("fs");
515
+ const path = yield eval("require")("path");
516
516
  if (!fs.existsSync(image)) {
517
517
  throw new Error(`Image file not found: ${image}`);
518
518
  }
@@ -526,26 +526,26 @@ var ImageProcessor = class {
526
526
  * @param url Remote image URL
527
527
  * @returns Promise with image data
528
528
  */
529
- static getImageFromUrl(url) {
529
+ static getImageFromUrl(url2) {
530
530
  return __async(this, null, function* () {
531
531
  let fetch;
532
532
  try {
533
533
  fetch = globalThis.fetch;
534
534
  } catch (e) {
535
- return this.fetchWithHttps(url);
535
+ return this.fetchWithHttps(url2);
536
536
  }
537
537
  if (!fetch) {
538
- return this.fetchWithHttps(url);
538
+ return this.fetchWithHttps(url2);
539
539
  }
540
- const response = yield fetch(url);
540
+ const response = yield fetch(url2);
541
541
  if (!response.ok) {
542
542
  throw new Error(`Failed to fetch image: ${response.status} ${response.statusText}`);
543
543
  }
544
544
  const arrayBuffer = yield response.arrayBuffer();
545
- const buffer = Buffer.from(arrayBuffer);
545
+ const buffer2 = Buffer.from(arrayBuffer);
546
546
  const contentType = response.headers.get("content-type");
547
- const imageType = this.getImageType(contentType || "", url);
548
- return this.parse(buffer, imageType);
547
+ const imageType = this.getImageType(contentType || "", url2);
548
+ return this.parse(buffer2, imageType);
549
549
  });
550
550
  }
551
551
  /**
@@ -555,8 +555,8 @@ var ImageProcessor = class {
555
555
  */
556
556
  static fetchWithHttps(url) {
557
557
  return __async(this, null, function* () {
558
- const https = yield import("https");
559
- const http = yield import("http");
558
+ const https = yield eval("require")("https");
559
+ const http = yield eval("require")("http");
560
560
  return new Promise((resolve, reject) => {
561
561
  const client = url.startsWith("https:") ? https : http;
562
562
  const request = client.get(url, (response) => {
@@ -570,10 +570,10 @@ var ImageProcessor = class {
570
570
  });
571
571
  response.on("end", () => {
572
572
  try {
573
- const buffer = Buffer.concat(chunks);
573
+ const buffer2 = Buffer.concat(chunks);
574
574
  const contentType = response.headers["content-type"] || "";
575
575
  const imageType = this.getImageType(contentType || "", url);
576
- const data = this.parse(buffer, imageType);
576
+ const data = this.parse(buffer2, imageType);
577
577
  resolve(data);
578
578
  } catch (error) {
579
579
  reject(error);
@@ -596,7 +596,7 @@ var ImageProcessor = class {
596
596
  /**
597
597
  * Decide content type
598
598
  */
599
- static getImageType(contentType, url) {
599
+ static getImageType(contentType, url2) {
600
600
  if (contentType) {
601
601
  if (contentType.includes("png")) {
602
602
  return "png";
@@ -604,7 +604,7 @@ var ImageProcessor = class {
604
604
  return "jpeg";
605
605
  }
606
606
  }
607
- const urlLower = url.toLowerCase();
607
+ const urlLower = url2.toLowerCase();
608
608
  if (urlLower.includes(".png")) {
609
609
  return "png";
610
610
  } else if (urlLower.includes(".jpg") || urlLower.includes(".jpeg")) {
@@ -618,12 +618,12 @@ var ImageProcessor = class {
618
618
  * @param extension
619
619
  * @returns
620
620
  */
621
- static parse(buffer, extension) {
621
+ static parse(buffer2, extension) {
622
622
  console.log(`Parsing image with extension: ${extension}`);
623
623
  if (extension === "png") {
624
- return parsePNG(buffer);
624
+ return parsePNG(buffer2);
625
625
  } else if (extension === "jpeg" || extension === "jpg") {
626
- return this.parseJPEG(buffer);
626
+ return this.parseJPEG(buffer2);
627
627
  } else {
628
628
  throw new Error(`Unsupported image format: ${extension}. Supported formats: PNG, JPEG`);
629
629
  }
@@ -635,23 +635,23 @@ var ImageProcessor = class {
635
635
  * @param buffer JPEG file buffer
636
636
  * @returns Image data
637
637
  */
638
- static parseJPEG(buffer) {
639
- if (buffer[0] !== 255 || buffer[1] !== 216) {
638
+ static parseJPEG(buffer2) {
639
+ if (buffer2[0] !== 255 || buffer2[1] !== 216) {
640
640
  throw new Error("Invalid JPEG file");
641
641
  }
642
642
  let offset = 2;
643
643
  let width = 0;
644
644
  let height = 0;
645
- while (offset < buffer.length - 1) {
646
- if (buffer[offset] === 255) {
647
- const marker = buffer[offset + 1];
645
+ while (offset < buffer2.length - 1) {
646
+ if (buffer2[offset] === 255) {
647
+ const marker = buffer2[offset + 1];
648
648
  if (marker >= 192 && marker <= 194) {
649
- height = buffer.readUInt16BE(offset + 5);
650
- width = buffer.readUInt16BE(offset + 7);
649
+ height = buffer2.readUInt16BE(offset + 5);
650
+ width = buffer2.readUInt16BE(offset + 7);
651
651
  break;
652
652
  }
653
- if (offset + 2 < buffer.length) {
654
- const segmentLength = buffer.readUInt16BE(offset + 2);
653
+ if (offset + 2 < buffer2.length) {
654
+ const segmentLength = buffer2.readUInt16BE(offset + 2);
655
655
  offset += 2 + segmentLength;
656
656
  } else {
657
657
  break;
@@ -666,11 +666,11 @@ var ImageProcessor = class {
666
666
  const pixelCount = width * height;
667
667
  const data = new Uint8Array(pixelCount * 4);
668
668
  let avgR = 0, avgG = 0, avgB = 0;
669
- const sampleSize = Math.min(1e3, buffer.length);
669
+ const sampleSize = Math.min(1e3, buffer2.length);
670
670
  for (let i = 0; i < sampleSize; i += 3) {
671
- avgR += buffer[i] || 0;
672
- avgG += buffer[i + 1] || 0;
673
- avgB += buffer[i + 2] || 0;
671
+ avgR += buffer2[i] || 0;
672
+ avgG += buffer2[i + 1] || 0;
673
+ avgB += buffer2[i + 2] || 0;
674
674
  }
675
675
  avgR = Math.floor(avgR / (sampleSize / 3));
676
676
  avgG = Math.floor(avgG / (sampleSize / 3));
@@ -764,9 +764,9 @@ var ImageUtils = class {
764
764
  * @param image Image to process
765
765
  * @returns
766
766
  */
767
- static getPixels(image) {
767
+ static getPixels(image2) {
768
768
  return __async(this, null, function* () {
769
- return yield ImageProcessor_default.getImageData(image);
769
+ return yield ImageProcessor_default.getImageData(image2);
770
770
  });
771
771
  }
772
772
  /**
@@ -779,14 +779,14 @@ var ImageUtils = class {
779
779
  * @param destinationHeight Height of the output bitmap
780
780
  * @returns
781
781
  */
782
- static getBWBitmap(image, destinationWidth, destinationHeight) {
782
+ static getBWBitmap(image2, destinationWidth, destinationHeight) {
783
783
  return __async(this, null, function* () {
784
784
  const {
785
785
  data,
786
786
  width,
787
787
  height,
788
788
  bitsPerPixel
789
- } = yield this.getPixels(image);
789
+ } = yield this.getPixels(image2);
790
790
  const dim = getSizePreserveAspect(width, height, destinationWidth, destinationHeight);
791
791
  const dWidth = dim.width;
792
792
  const dHeight = dim.height;
@@ -909,9 +909,9 @@ var TSPLBitmapCommand = class _TSPLBitmapCommand extends TSPLVisualCommand {
909
909
  * @param mode Graphics mode
910
910
  * @returns
911
911
  */
912
- static forImageUrl(image, x, y, imageWidth, imageHeight, mode) {
912
+ static forImageUrl(image2, x, y, imageWidth, imageHeight, mode) {
913
913
  return __async(this, null, function* () {
914
- const bitmap = yield ImageUtils.getBWBitmap(image, imageWidth, imageHeight);
914
+ const bitmap = yield ImageUtils.getBWBitmap(image2, imageWidth, imageHeight);
915
915
  return new _TSPLBitmapCommand(bitmap, x, y, mode);
916
916
  });
917
917
  }
@@ -1224,8 +1224,8 @@ var TSPLCommandGenerator = class {
1224
1224
  line(start, end, thickness) {
1225
1225
  return new TSPLDiagonal(start, end, thickness);
1226
1226
  }
1227
- image(image, x, y, mode) {
1228
- return new TSPLBitmapCommand(image, x, y, mode);
1227
+ image(image2, x, y, mode) {
1228
+ return new TSPLBitmapCommand(image2, x, y, mode);
1229
1229
  }
1230
1230
  qrCode(content, width, x, y) {
1231
1231
  const cellCount = this.cellCount(content);
@@ -1560,7 +1560,7 @@ var Printable = class {
1560
1560
  };
1561
1561
 
1562
1562
  // src/labels/Label.ts
1563
- import fontkit from "fontkit";
1563
+ import * as fontkit from "fontkit";
1564
1564
  var DEFAULT_FONT_WEIGHT = 400;
1565
1565
  var DEFAULT_FONT_STYLE = "normal";
1566
1566
  var FONT_PREFIX = "f";
@@ -2059,11 +2059,11 @@ var BarCode = class extends LabelField {
2059
2059
 
2060
2060
  // src/labels/fields/Image.ts
2061
2061
  var Image2 = class _Image extends LabelField {
2062
- constructor(x, y, image) {
2062
+ constructor(x, y, image2) {
2063
2063
  super();
2064
2064
  this.x = x;
2065
2065
  this.y = y;
2066
- this.image = image;
2066
+ this.image = image2;
2067
2067
  }
2068
2068
  commandForLanguage(language, _config) {
2069
2069
  return __async(this, null, function* () {
@@ -2079,9 +2079,9 @@ var Image2 = class _Image extends LabelField {
2079
2079
  * @param height
2080
2080
  * @returns
2081
2081
  */
2082
- static create(image, x, y, width, height) {
2082
+ static create(image2, x, y, width, height) {
2083
2083
  return __async(this, null, function* () {
2084
- const bitmap = yield ImageUtils.getBWBitmap(image, width, height);
2084
+ const bitmap = yield ImageUtils.getBWBitmap(image2, width, height);
2085
2085
  return new _Image(x, y, bitmap);
2086
2086
  });
2087
2087
  }