label-printer 0.11.0 → 0.12.0

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.js CHANGED
@@ -271,6 +271,19 @@ function dotToPoint(dots, dpi) {
271
271
  const inch = dots / dpi;
272
272
  return Math.round(inch * pointsPerInch);
273
273
  }
274
+ function inToDot(inches, dpi, round = false) {
275
+ const res = inches * dpi;
276
+ if (round) return Math.round(res);
277
+ else return res;
278
+ }
279
+ function mmToDot(mm, dpi, round = false) {
280
+ const res = mm / 25.4 * dpi;
281
+ if (round) return Math.round(res);
282
+ else return res;
283
+ }
284
+ function unitToDot(value, dpi, unitSystem, round = false) {
285
+ return unitSystem === "imperial" ? inToDot(value, dpi, round) : mmToDot(value, dpi, round);
286
+ }
274
287
 
275
288
  // src/helpers/ImageDataParser.ts
276
289
  function parsePNG(buffer2) {
@@ -1251,20 +1264,6 @@ var TSPLBitmapCommand = class _TSPLBitmapCommand extends TSPLVisualCommand {
1251
1264
  }
1252
1265
  };
1253
1266
 
1254
- // src/commands/tspl/types.ts
1255
- var alignmentToNumber = (alignment) => {
1256
- switch (alignment) {
1257
- case void 0:
1258
- return 0;
1259
- case "left":
1260
- return 1;
1261
- case "center":
1262
- return 2;
1263
- case "right":
1264
- return 3;
1265
- }
1266
- };
1267
-
1268
1267
  // src/commands/tspl/commands/basic/TSPLTextCommand.ts
1269
1268
  var TSPLTextCommand = class extends TSPLVisualCommand {
1270
1269
  constructor(content, x, y, font, rotation, xMultiplication, yMultiplication, alignment) {
@@ -1637,6 +1636,20 @@ var TSPLCommandGenerator = class _TSPLCommandGenerator {
1637
1636
  };
1638
1637
  var TSPLCommandGenerator_default = new TSPLCommandGenerator();
1639
1638
 
1639
+ // src/commands/tspl/types.ts
1640
+ var alignmentToNumber = (alignment) => {
1641
+ switch (alignment) {
1642
+ case void 0:
1643
+ return 0;
1644
+ case "left":
1645
+ return 1;
1646
+ case "center":
1647
+ return 2;
1648
+ case "right":
1649
+ return 3;
1650
+ }
1651
+ };
1652
+
1640
1653
  // src/printers/index.ts
1641
1654
  var printers_exports = {};
1642
1655
  __export(printers_exports, {
@@ -2464,7 +2477,7 @@ var Printable = class {
2464
2477
  /**
2465
2478
  * Generates printable command for the given printer. Can be used to obtain a command for fields supported by the package then customizing it before printing
2466
2479
  * @param printer Printer to generate the command. Important because the command is printer language specific
2467
- * @returns A promise for a command. Most commands are syncronouse but some may require to access async resources
2480
+ * @returns A promise for a command. Most commands are synchronous but some may require to access async resources
2468
2481
  */
2469
2482
  commandForPrinter(printer, config) {
2470
2483
  return __async(this, null, function* () {
@@ -2483,8 +2496,105 @@ var Printable = class {
2483
2496
  }
2484
2497
  };
2485
2498
 
2499
+ // src/labels/types.ts
2500
+ var rotationForOrientation = (orientation) => {
2501
+ switch (orientation) {
2502
+ case "normal":
2503
+ return 0;
2504
+ case "left":
2505
+ return 90;
2506
+ case "upside-down":
2507
+ return 180;
2508
+ case "right":
2509
+ return 270;
2510
+ }
2511
+ };
2512
+
2486
2513
  // src/labels/Label.ts
2487
2514
  var fontkit = __toESM(require("fontkit"));
2515
+
2516
+ // src/labels/fields/superClasses/LabelField.ts
2517
+ var LabelField = class extends Printable {
2518
+ };
2519
+
2520
+ // src/labels/fields/superClasses/RotatableLabelField.ts
2521
+ var RotatableLabelField = class extends LabelField {
2522
+ constructor() {
2523
+ super(...arguments);
2524
+ this.rotation = 0;
2525
+ }
2526
+ setRotation(rotation) {
2527
+ this.rotation = rotation;
2528
+ }
2529
+ getRotation() {
2530
+ return this.rotation;
2531
+ }
2532
+ };
2533
+
2534
+ // src/labels/RotatableContainer.ts
2535
+ var RotatableContainer = class extends RotatableLabelField {
2536
+ /**
2537
+ * @param size Size in dots
2538
+ */
2539
+ constructor(size) {
2540
+ super();
2541
+ this.fields = [];
2542
+ this.size = size;
2543
+ }
2544
+ /**
2545
+ * Place fields in the container
2546
+ * @param fields
2547
+ */
2548
+ add(...fields) {
2549
+ this.fields.push(...fields);
2550
+ }
2551
+ commandForLanguage(language, config) {
2552
+ return __async(this, null, function* () {
2553
+ const commandList = yield Promise.all(this.fields.map((field) => this.rotationAdjustedCommand(field, language, config)));
2554
+ return this.commandGeneratorFor(language).commandGroup(commandList);
2555
+ });
2556
+ }
2557
+ rotationAdjustedCommand(field, language, config) {
2558
+ return __async(this, null, function* () {
2559
+ let originalRotation = void 0;
2560
+ let originalPosition = void 0;
2561
+ if (field.hasOwnProperty("rotation")) {
2562
+ const rotatableField = field;
2563
+ originalRotation = rotatableField.getRotation();
2564
+ rotatableField.setRotation((originalRotation + this.rotation) % 360);
2565
+ }
2566
+ if (typeof field.getPosition === "function" && typeof field.setPosition === "function") {
2567
+ const positionedField = field;
2568
+ originalPosition = positionedField.getPosition();
2569
+ positionedField.setPosition(this.adjustPosition(originalPosition));
2570
+ }
2571
+ const command = yield field.commandForLanguage(language, config);
2572
+ if (originalRotation !== void 0) {
2573
+ const rotatableField = field;
2574
+ rotatableField.setRotation(originalRotation);
2575
+ }
2576
+ if (originalPosition !== void 0) {
2577
+ const positionedField = field;
2578
+ positionedField.setPosition(originalPosition);
2579
+ }
2580
+ return command;
2581
+ });
2582
+ }
2583
+ adjustPosition(position) {
2584
+ switch (this.rotation) {
2585
+ case 90:
2586
+ return { x: this.size.height - position.y, y: position.x };
2587
+ case 180:
2588
+ return { x: this.size.width - position.x, y: this.size.height - position.y };
2589
+ case 270:
2590
+ return { x: position.y, y: this.size.width - position.x };
2591
+ default:
2592
+ return position;
2593
+ }
2594
+ }
2595
+ };
2596
+
2597
+ // src/labels/Label.ts
2488
2598
  var DEFAULT_FONT_WEIGHT = 400;
2489
2599
  var DEFAULT_FONT_STYLE = "normal";
2490
2600
  var FONT_PREFIX = "f";
@@ -2493,10 +2603,7 @@ var Label = class extends Printable {
2493
2603
  super();
2494
2604
  this.fonts = {};
2495
2605
  this.density = 8;
2496
- /**
2497
- * List of fields on the label
2498
- */
2499
- this.fields = [];
2606
+ this.orientation = "normal";
2500
2607
  this.fontCounter = 0;
2501
2608
  this._textWidthCorrectionFactor = 0.935;
2502
2609
  this.width = width;
@@ -2504,6 +2611,9 @@ var Label = class extends Printable {
2504
2611
  this.unitSystem = dimensionUnit;
2505
2612
  this.dpi = dpi;
2506
2613
  this.density = density;
2614
+ const widthInDots = unitToDot(width, dpi, dimensionUnit);
2615
+ const heightInDots = unitToDot(height, dpi, dimensionUnit);
2616
+ this.container = new RotatableContainer({ width: widthInDots, height: heightInDots });
2507
2617
  }
2508
2618
  /**
2509
2619
  * Configuration used when generating commands
@@ -2534,11 +2644,17 @@ var Label = class extends Printable {
2534
2644
  setTextWidthCorrectionFactor(factor) {
2535
2645
  this._textWidthCorrectionFactor = factor;
2536
2646
  }
2647
+ /**
2648
+ * Change the orientation the label is printed in. This will rotate all fields in the container
2649
+ * @param orientation
2650
+ */
2651
+ setOrientation(orientation) {
2652
+ this.orientation = orientation;
2653
+ this.container.setRotation(rotationForOrientation(orientation));
2654
+ }
2537
2655
  commandForLanguage(language, config) {
2538
2656
  return __async(this, null, function* () {
2539
- const configuration = config != null ? config : this.printConfig;
2540
- const commandList = yield Promise.all(this.fields.map((field) => field.commandForLanguage(language, configuration)));
2541
- return this.commandGeneratorFor(language).commandGroup(commandList);
2657
+ return yield this.container.commandForLanguage(language, config);
2542
2658
  });
2543
2659
  }
2544
2660
  /**
@@ -2546,7 +2662,7 @@ var Label = class extends Printable {
2546
2662
  * @param fields
2547
2663
  */
2548
2664
  add(...fields) {
2549
- this.fields.push(...fields);
2665
+ this.container.add(...fields);
2550
2666
  }
2551
2667
  /**
2552
2668
  * Register a font to be used. Use the name provided in components to use the font.
@@ -2612,11 +2728,17 @@ var Label = class extends Printable {
2612
2728
  */
2613
2729
  fullCommand(language, gap, direction, mirror = false, gapOffset = 0, generator) {
2614
2730
  return __async(this, null, function* () {
2731
+ let finalDimations;
2732
+ if (this.orientation == "normal" || this.orientation == "upside-down") {
2733
+ finalDimations = { width: this.width, height: this.height };
2734
+ } else {
2735
+ finalDimations = { width: this.height, height: this.width };
2736
+ }
2615
2737
  const commands = [
2616
2738
  this.fontUploadCommands(generator),
2617
2739
  generator.setUp(
2618
- this.width,
2619
- this.height,
2740
+ finalDimations.width,
2741
+ finalDimations.height,
2620
2742
  gap,
2621
2743
  gapOffset,
2622
2744
  direction,
@@ -2647,10 +2769,10 @@ var Label = class extends Printable {
2647
2769
  const family = this.fonts[font.name];
2648
2770
  if (!family) return null;
2649
2771
  const style = (_a = font.style) != null ? _a : DEFAULT_FONT_STYLE;
2650
- const weigth = (_b = font.weight) != null ? _b : DEFAULT_FONT_WEIGHT;
2772
+ const weight = (_b = font.weight) != null ? _b : DEFAULT_FONT_WEIGHT;
2651
2773
  const fontKeys = Object.keys(family.fonts);
2652
2774
  const exactMatch = fontKeys.find(
2653
- (key) => family.fonts[key].style == style && family.fonts[key].weight == weigth
2775
+ (key) => family.fonts[key].style == style && family.fonts[key].weight == weight
2654
2776
  );
2655
2777
  if (exactMatch) {
2656
2778
  return family.fonts[exactMatch];
@@ -2660,7 +2782,7 @@ var Label = class extends Printable {
2660
2782
  let weigthDiff = 99999999;
2661
2783
  let selectedKey = "";
2662
2784
  sameStyleKeys.forEach((key) => {
2663
- const diff = Math.abs(weigth - family.fonts[key].weight);
2785
+ const diff = Math.abs(weight - family.fonts[key].weight);
2664
2786
  if (diff < weigthDiff) {
2665
2787
  weigthDiff = diff;
2666
2788
  selectedKey = key;
@@ -2688,10 +2810,6 @@ var Label = class extends Printable {
2688
2810
  }
2689
2811
  };
2690
2812
 
2691
- // src/labels/fields/LabelField.ts
2692
- var LabelField = class extends Printable {
2693
- };
2694
-
2695
2813
  // src/labels/fields/Line.ts
2696
2814
  var Line = class extends LabelField {
2697
2815
  /**
@@ -2720,12 +2838,11 @@ var UNDERLINE_TAG = "u";
2720
2838
  var STRIKE_TAG = ["s", "del", "strike"];
2721
2839
  var PARAGRAPH_TAG = "p";
2722
2840
  var BREAK_TAG = "br";
2723
- var Text = class extends LabelField {
2841
+ var Text = class extends RotatableLabelField {
2724
2842
  constructor(content, x, y, formatted = true) {
2725
2843
  super();
2726
2844
  this.font = { name: "default", size: 10 };
2727
2845
  this.type = "singleline";
2728
- this.rotation = 0;
2729
2846
  this.context = void 0;
2730
2847
  this.lineSpacing = 1;
2731
2848
  this.content = content.replace("\n", "").replace('"', '\\"');
@@ -2751,6 +2868,13 @@ var Text = class extends LabelField {
2751
2868
  }
2752
2869
  return false;
2753
2870
  }
2871
+ setPosition(position) {
2872
+ this.x = position.x;
2873
+ this.y = position.y;
2874
+ }
2875
+ getPosition() {
2876
+ return { x: this.x, y: this.y };
2877
+ }
2754
2878
  /**
2755
2879
  * Sets the field to single line
2756
2880
  * @param width Max width of the text. Leave it undefined to allow the field to grow
@@ -2778,13 +2902,6 @@ var Text = class extends LabelField {
2778
2902
  setFont(font) {
2779
2903
  this.font = font;
2780
2904
  }
2781
- /**
2782
- * Set the rotation of the text field. All text commands in this field will be rotated.
2783
- * For multiline text, lines advance perpendicular to the character direction.
2784
- */
2785
- setRotation(rotation) {
2786
- this.rotation = rotation;
2787
- }
2788
2905
  // --- Rotation-aware position helpers ---
2789
2906
  /** Advance cursor by `amount` along the character direction */
2790
2907
  advanceChar(x, y, amount) {
@@ -3197,7 +3314,7 @@ var Text = class extends LabelField {
3197
3314
  };
3198
3315
 
3199
3316
  // src/labels/fields/BarCode.ts
3200
- var BarCode = class extends LabelField {
3317
+ var BarCode = class extends RotatableLabelField {
3201
3318
  /**
3202
3319
  * @param content Content to encode
3203
3320
  * @param x X coordinate in dots
@@ -3215,16 +3332,19 @@ var BarCode = class extends LabelField {
3215
3332
  this.type = type;
3216
3333
  this.height = height;
3217
3334
  this.barWidth = barWidth;
3218
- this.rotation = 0;
3219
3335
  this.humanReadable = "none";
3220
3336
  this.alignment = "left";
3221
3337
  }
3222
- setRotation(rotation) {
3223
- this.rotation = rotation;
3224
- }
3225
3338
  setHumanReadable(humanReadable) {
3226
3339
  this.humanReadable = humanReadable;
3227
3340
  }
3341
+ setPosition(position) {
3342
+ this.x = position.x;
3343
+ this.y = position.y;
3344
+ }
3345
+ getPosition() {
3346
+ return { x: this.x, y: this.y };
3347
+ }
3228
3348
  commandForLanguage(language, _config) {
3229
3349
  return __async(this, null, function* () {
3230
3350
  return yield this.commandGeneratorFor(language).barCode(this.content, this.x, this.y, this.type, this.height, this.rotation, this.humanReadable, this.alignment, this.barWidth);
@@ -3233,16 +3353,19 @@ var BarCode = class extends LabelField {
3233
3353
  };
3234
3354
 
3235
3355
  // src/labels/fields/Image.ts
3236
- var Image2 = class _Image extends LabelField {
3356
+ var Image2 = class _Image extends RotatableLabelField {
3237
3357
  constructor(x, y, image2) {
3238
3358
  super();
3239
- this.rotation = 0;
3240
3359
  this.x = x;
3241
3360
  this.y = y;
3242
3361
  this.image = image2;
3243
3362
  }
3244
- setRotation(rotation) {
3245
- this.rotation = rotation;
3363
+ setPosition(position) {
3364
+ this.x = position.x;
3365
+ this.y = position.y;
3366
+ }
3367
+ getPosition() {
3368
+ return { x: this.x, y: this.y };
3246
3369
  }
3247
3370
  commandForLanguage(language, _config) {
3248
3371
  return __async(this, null, function* () {
@@ -3287,17 +3410,20 @@ var Image2 = class _Image extends LabelField {
3287
3410
  };
3288
3411
 
3289
3412
  // src/labels/fields/QRCode.ts
3290
- var QRCode = class extends LabelField {
3413
+ var QRCode = class extends RotatableLabelField {
3291
3414
  constructor(content, x, y, width) {
3292
3415
  super();
3293
- this.rotation = 0;
3294
3416
  this.content = content;
3295
3417
  this.x = x;
3296
3418
  this.y = y;
3297
3419
  this.width = width;
3298
3420
  }
3299
- setRotation(rotation) {
3300
- this.rotation = rotation;
3421
+ setPosition(position) {
3422
+ this.x = position.x;
3423
+ this.y = position.y;
3424
+ }
3425
+ getPosition() {
3426
+ return { x: this.x, y: this.y };
3301
3427
  }
3302
3428
  commandForLanguage(language, config) {
3303
3429
  return __async(this, null, function* () {