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/README.md CHANGED
@@ -52,6 +52,6 @@ This layer contains code to interact with printers
52
52
 
53
53
  # Update package
54
54
 
55
- - Run `pnpm changeset` to create change
56
- - Run `pnpm changeset version` to create an update with all the versions
55
+ - Run `pnpm changeset` to create change. This change has to be merged into main first
56
+ - Run `pnpm changeset version` to create an update with all the versions.
57
57
  - PR is automatically created on push, merge it to trigger publish
package/dist/index.js CHANGED
@@ -249,9 +249,9 @@ function pointsToDots(points, dpi) {
249
249
  }
250
250
 
251
251
  // src/helpers/ImageDataParser.ts
252
- function parsePNG(buffer) {
252
+ function parsePNG(buffer2) {
253
253
  const pngSignature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
254
- if (!buffer.subarray(0, 8).equals(pngSignature)) {
254
+ if (!buffer2.subarray(0, 8).equals(pngSignature)) {
255
255
  throw new Error("Invalid PNG file");
256
256
  }
257
257
  let width = 0, height = 0, bitDepth = 0, colorType = 0;
@@ -260,10 +260,10 @@ function parsePNG(buffer) {
260
260
  let transparency = null;
261
261
  let idatChunks = [];
262
262
  let offset = 8;
263
- while (offset < buffer.length) {
264
- const chunkLength = buffer.readUInt32BE(offset);
265
- const chunkType = buffer.subarray(offset + 4, offset + 8).toString("ascii");
266
- const chunkData = buffer.subarray(offset + 8, offset + 8 + chunkLength);
263
+ while (offset < buffer2.length) {
264
+ const chunkLength = buffer2.readUInt32BE(offset);
265
+ const chunkType = buffer2.subarray(offset + 4, offset + 8).toString("ascii");
266
+ const chunkData = buffer2.subarray(offset + 8, offset + 8 + chunkLength);
267
267
  if (chunkType === "IHDR") {
268
268
  width = chunkData.readUInt32BE(0);
269
269
  height = chunkData.readUInt32BE(4);
@@ -426,12 +426,12 @@ var ImageProcessor = class {
426
426
  * @param image Image to process (local file path, remote URL, data URL, or Blob)
427
427
  * @returns Promise with image data including width, height, pixel data, and bits per pixel
428
428
  */
429
- static getImageData(image) {
429
+ static getImageData(image2) {
430
430
  return __async(this, null, function* () {
431
431
  if (typeof window !== "undefined") {
432
- return this.getImageDataBrowser(image);
432
+ return this.getImageDataBrowser(image2);
433
433
  } else {
434
- return this.getImageDataNode(image);
434
+ return this.getImageDataNode(image2);
435
435
  }
436
436
  });
437
437
  }
@@ -441,7 +441,7 @@ var ImageProcessor = class {
441
441
  * @param image Image to process
442
442
  * @returns Promise with image data
443
443
  */
444
- static getImageDataBrowser(image) {
444
+ static getImageDataBrowser(image2) {
445
445
  return __async(this, null, function* () {
446
446
  console.log("Processing image in browser environment");
447
447
  return new Promise((resolve, reject) => {
@@ -471,12 +471,12 @@ var ImageProcessor = class {
471
471
  }
472
472
  };
473
473
  img.onerror = () => reject(new Error("Failed to load image"));
474
- if (typeof image === "string") {
475
- img.src = image;
474
+ if (typeof image2 === "string") {
475
+ img.src = image2;
476
476
  } else {
477
- const url = URL.createObjectURL(image);
477
+ const url2 = URL.createObjectURL(image2);
478
478
  img.onload = () => {
479
- URL.revokeObjectURL(url);
479
+ URL.revokeObjectURL(url2);
480
480
  resolve({
481
481
  data: new Uint8Array(0),
482
482
  // Will be set by the actual onload
@@ -485,7 +485,7 @@ var ImageProcessor = class {
485
485
  bitsPerPixel: 4
486
486
  });
487
487
  };
488
- img.src = url;
488
+ img.src = url2;
489
489
  }
490
490
  });
491
491
  });
@@ -496,18 +496,18 @@ var ImageProcessor = class {
496
496
  * @param image Image to process
497
497
  * @returns Promise with image data
498
498
  */
499
- static getImageDataNode(image) {
499
+ static getImageDataNode(image2) {
500
500
  return __async(this, null, function* () {
501
501
  console.log("Processing image in Node.js environment");
502
- if (image instanceof Blob) {
502
+ if (image2 instanceof Blob) {
503
503
  throw new Error("Blob input not supported in Node.js environment. Use file path or data URL instead.");
504
504
  }
505
- if (image.startsWith("data:")) {
506
- return this.getImageFromData(image);
507
- } else if (image.startsWith("http://") || image.startsWith("https://")) {
508
- return this.getImageFromUrl(image);
505
+ if (image2.startsWith("data:")) {
506
+ return this.getImageFromData(image2);
507
+ } else if (image2.startsWith("http://") || image2.startsWith("https://")) {
508
+ return this.getImageFromUrl(image2);
509
509
  } else {
510
- return this.getImageFromFile(image);
510
+ return this.getImageFromFile(image2);
511
511
  }
512
512
  });
513
513
  }
@@ -524,9 +524,9 @@ var ImageProcessor = class {
524
524
  if (!(mimeType == null ? void 0 : mimeType.startsWith("image/"))) {
525
525
  throw new Error("Invalid image data URL");
526
526
  }
527
- const buffer = Buffer.from(data, "base64");
527
+ const buffer2 = Buffer.from(data, "base64");
528
528
  const extension = mimeType.split("/")[1].toLowerCase();
529
- return this.parse(buffer, extension);
529
+ return this.parse(buffer2, extension);
530
530
  });
531
531
  }
532
532
  /**
@@ -536,8 +536,8 @@ var ImageProcessor = class {
536
536
  */
537
537
  static getImageFromFile(image) {
538
538
  return __async(this, null, function* () {
539
- const fs = yield import("fs");
540
- const path = yield import("path");
539
+ const fs = yield eval("require")("fs");
540
+ const path = yield eval("require")("path");
541
541
  if (!fs.existsSync(image)) {
542
542
  throw new Error(`Image file not found: ${image}`);
543
543
  }
@@ -551,26 +551,26 @@ var ImageProcessor = class {
551
551
  * @param url Remote image URL
552
552
  * @returns Promise with image data
553
553
  */
554
- static getImageFromUrl(url) {
554
+ static getImageFromUrl(url2) {
555
555
  return __async(this, null, function* () {
556
556
  let fetch;
557
557
  try {
558
558
  fetch = globalThis.fetch;
559
559
  } catch (e) {
560
- return this.fetchWithHttps(url);
560
+ return this.fetchWithHttps(url2);
561
561
  }
562
562
  if (!fetch) {
563
- return this.fetchWithHttps(url);
563
+ return this.fetchWithHttps(url2);
564
564
  }
565
- const response = yield fetch(url);
565
+ const response = yield fetch(url2);
566
566
  if (!response.ok) {
567
567
  throw new Error(`Failed to fetch image: ${response.status} ${response.statusText}`);
568
568
  }
569
569
  const arrayBuffer = yield response.arrayBuffer();
570
- const buffer = Buffer.from(arrayBuffer);
570
+ const buffer2 = Buffer.from(arrayBuffer);
571
571
  const contentType = response.headers.get("content-type");
572
- const imageType = this.getImageType(contentType || "", url);
573
- return this.parse(buffer, imageType);
572
+ const imageType = this.getImageType(contentType || "", url2);
573
+ return this.parse(buffer2, imageType);
574
574
  });
575
575
  }
576
576
  /**
@@ -580,8 +580,8 @@ var ImageProcessor = class {
580
580
  */
581
581
  static fetchWithHttps(url) {
582
582
  return __async(this, null, function* () {
583
- const https = yield import("https");
584
- const http = yield import("http");
583
+ const https = yield eval("require")("https");
584
+ const http = yield eval("require")("http");
585
585
  return new Promise((resolve, reject) => {
586
586
  const client = url.startsWith("https:") ? https : http;
587
587
  const request = client.get(url, (response) => {
@@ -595,10 +595,10 @@ var ImageProcessor = class {
595
595
  });
596
596
  response.on("end", () => {
597
597
  try {
598
- const buffer = Buffer.concat(chunks);
598
+ const buffer2 = Buffer.concat(chunks);
599
599
  const contentType = response.headers["content-type"] || "";
600
600
  const imageType = this.getImageType(contentType || "", url);
601
- const data = this.parse(buffer, imageType);
601
+ const data = this.parse(buffer2, imageType);
602
602
  resolve(data);
603
603
  } catch (error) {
604
604
  reject(error);
@@ -621,7 +621,7 @@ var ImageProcessor = class {
621
621
  /**
622
622
  * Decide content type
623
623
  */
624
- static getImageType(contentType, url) {
624
+ static getImageType(contentType, url2) {
625
625
  if (contentType) {
626
626
  if (contentType.includes("png")) {
627
627
  return "png";
@@ -629,7 +629,7 @@ var ImageProcessor = class {
629
629
  return "jpeg";
630
630
  }
631
631
  }
632
- const urlLower = url.toLowerCase();
632
+ const urlLower = url2.toLowerCase();
633
633
  if (urlLower.includes(".png")) {
634
634
  return "png";
635
635
  } else if (urlLower.includes(".jpg") || urlLower.includes(".jpeg")) {
@@ -643,12 +643,12 @@ var ImageProcessor = class {
643
643
  * @param extension
644
644
  * @returns
645
645
  */
646
- static parse(buffer, extension) {
646
+ static parse(buffer2, extension) {
647
647
  console.log(`Parsing image with extension: ${extension}`);
648
648
  if (extension === "png") {
649
- return parsePNG(buffer);
649
+ return parsePNG(buffer2);
650
650
  } else if (extension === "jpeg" || extension === "jpg") {
651
- return this.parseJPEG(buffer);
651
+ return this.parseJPEG(buffer2);
652
652
  } else {
653
653
  throw new Error(`Unsupported image format: ${extension}. Supported formats: PNG, JPEG`);
654
654
  }
@@ -660,23 +660,23 @@ var ImageProcessor = class {
660
660
  * @param buffer JPEG file buffer
661
661
  * @returns Image data
662
662
  */
663
- static parseJPEG(buffer) {
664
- if (buffer[0] !== 255 || buffer[1] !== 216) {
663
+ static parseJPEG(buffer2) {
664
+ if (buffer2[0] !== 255 || buffer2[1] !== 216) {
665
665
  throw new Error("Invalid JPEG file");
666
666
  }
667
667
  let offset = 2;
668
668
  let width = 0;
669
669
  let height = 0;
670
- while (offset < buffer.length - 1) {
671
- if (buffer[offset] === 255) {
672
- const marker = buffer[offset + 1];
670
+ while (offset < buffer2.length - 1) {
671
+ if (buffer2[offset] === 255) {
672
+ const marker = buffer2[offset + 1];
673
673
  if (marker >= 192 && marker <= 194) {
674
- height = buffer.readUInt16BE(offset + 5);
675
- width = buffer.readUInt16BE(offset + 7);
674
+ height = buffer2.readUInt16BE(offset + 5);
675
+ width = buffer2.readUInt16BE(offset + 7);
676
676
  break;
677
677
  }
678
- if (offset + 2 < buffer.length) {
679
- const segmentLength = buffer.readUInt16BE(offset + 2);
678
+ if (offset + 2 < buffer2.length) {
679
+ const segmentLength = buffer2.readUInt16BE(offset + 2);
680
680
  offset += 2 + segmentLength;
681
681
  } else {
682
682
  break;
@@ -691,11 +691,11 @@ var ImageProcessor = class {
691
691
  const pixelCount = width * height;
692
692
  const data = new Uint8Array(pixelCount * 4);
693
693
  let avgR = 0, avgG = 0, avgB = 0;
694
- const sampleSize = Math.min(1e3, buffer.length);
694
+ const sampleSize = Math.min(1e3, buffer2.length);
695
695
  for (let i = 0; i < sampleSize; i += 3) {
696
- avgR += buffer[i] || 0;
697
- avgG += buffer[i + 1] || 0;
698
- avgB += buffer[i + 2] || 0;
696
+ avgR += buffer2[i] || 0;
697
+ avgG += buffer2[i + 1] || 0;
698
+ avgB += buffer2[i + 2] || 0;
699
699
  }
700
700
  avgR = Math.floor(avgR / (sampleSize / 3));
701
701
  avgG = Math.floor(avgG / (sampleSize / 3));
@@ -789,9 +789,9 @@ var ImageUtils = class {
789
789
  * @param image Image to process
790
790
  * @returns
791
791
  */
792
- static getPixels(image) {
792
+ static getPixels(image2) {
793
793
  return __async(this, null, function* () {
794
- return yield ImageProcessor_default.getImageData(image);
794
+ return yield ImageProcessor_default.getImageData(image2);
795
795
  });
796
796
  }
797
797
  /**
@@ -804,14 +804,14 @@ var ImageUtils = class {
804
804
  * @param destinationHeight Height of the output bitmap
805
805
  * @returns
806
806
  */
807
- static getBWBitmap(image, destinationWidth, destinationHeight) {
807
+ static getBWBitmap(image2, destinationWidth, destinationHeight) {
808
808
  return __async(this, null, function* () {
809
809
  const {
810
810
  data,
811
811
  width,
812
812
  height,
813
813
  bitsPerPixel
814
- } = yield this.getPixels(image);
814
+ } = yield this.getPixels(image2);
815
815
  const dim = getSizePreserveAspect(width, height, destinationWidth, destinationHeight);
816
816
  const dWidth = dim.width;
817
817
  const dHeight = dim.height;
@@ -934,9 +934,9 @@ var TSPLBitmapCommand = class _TSPLBitmapCommand extends TSPLVisualCommand {
934
934
  * @param mode Graphics mode
935
935
  * @returns
936
936
  */
937
- static forImageUrl(image, x, y, imageWidth, imageHeight, mode) {
937
+ static forImageUrl(image2, x, y, imageWidth, imageHeight, mode) {
938
938
  return __async(this, null, function* () {
939
- const bitmap = yield ImageUtils.getBWBitmap(image, imageWidth, imageHeight);
939
+ const bitmap = yield ImageUtils.getBWBitmap(image2, imageWidth, imageHeight);
940
940
  return new _TSPLBitmapCommand(bitmap, x, y, mode);
941
941
  });
942
942
  }
@@ -1249,8 +1249,8 @@ var TSPLCommandGenerator = class {
1249
1249
  line(start, end, thickness) {
1250
1250
  return new TSPLDiagonal(start, end, thickness);
1251
1251
  }
1252
- image(image, x, y, mode) {
1253
- return new TSPLBitmapCommand(image, x, y, mode);
1252
+ image(image2, x, y, mode) {
1253
+ return new TSPLBitmapCommand(image2, x, y, mode);
1254
1254
  }
1255
1255
  qrCode(content, width, x, y) {
1256
1256
  const cellCount = this.cellCount(content);
@@ -1585,7 +1585,7 @@ var Printable = class {
1585
1585
  };
1586
1586
 
1587
1587
  // src/labels/Label.ts
1588
- var import_fontkit = __toESM(require("fontkit"));
1588
+ var fontkit = __toESM(require("fontkit"));
1589
1589
  var DEFAULT_FONT_WEIGHT = 400;
1590
1590
  var DEFAULT_FONT_STYLE = "normal";
1591
1591
  var FONT_PREFIX = "f";
@@ -1647,7 +1647,7 @@ var Label = class extends Printable {
1647
1647
  };
1648
1648
  }
1649
1649
  const fontBuffer = Buffer.from(font.data);
1650
- const builtFont = import_fontkit.default.create(fontBuffer);
1650
+ const builtFont = fontkit.create(fontBuffer);
1651
1651
  const finalFont = builtFont.fonts ? builtFont.fonts[0] : builtFont;
1652
1652
  this.fonts[font.name].fonts[key] = __spreadProps(__spreadValues({}, font), {
1653
1653
  font: finalFont,
@@ -2084,11 +2084,11 @@ var BarCode = class extends LabelField {
2084
2084
 
2085
2085
  // src/labels/fields/Image.ts
2086
2086
  var Image2 = class _Image extends LabelField {
2087
- constructor(x, y, image) {
2087
+ constructor(x, y, image2) {
2088
2088
  super();
2089
2089
  this.x = x;
2090
2090
  this.y = y;
2091
- this.image = image;
2091
+ this.image = image2;
2092
2092
  }
2093
2093
  commandForLanguage(language, _config) {
2094
2094
  return __async(this, null, function* () {
@@ -2104,9 +2104,9 @@ var Image2 = class _Image extends LabelField {
2104
2104
  * @param height
2105
2105
  * @returns
2106
2106
  */
2107
- static create(image, x, y, width, height) {
2107
+ static create(image2, x, y, width, height) {
2108
2108
  return __async(this, null, function* () {
2109
- const bitmap = yield ImageUtils.getBWBitmap(image, width, height);
2109
+ const bitmap = yield ImageUtils.getBWBitmap(image2, width, height);
2110
2110
  return new _Image(x, y, bitmap);
2111
2111
  });
2112
2112
  }