label-printer 0.9.1 → 0.11.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.d.mts CHANGED
@@ -97,12 +97,12 @@ type BWBitmap = BitmapLike;
97
97
  interface CommandGenerator<T extends Command> {
98
98
  commandGroup: (commands: T[]) => CommandGroup<T>;
99
99
  print: (sets: number, copiesPerSet: number) => T;
100
- text: (content: string, x: number, y: number, font: string | "default", size: number) => T;
100
+ text: (content: string, x: number, y: number, font: string | "default", size: number, rotation?: Rotation) => T;
101
101
  upload: (name: string, data: ArrayBuffer | Uint8Array) => T;
102
102
  line: (start: Point, end: Point, thickness: number) => T;
103
103
  image: (image: BitmapLike, x: number, y: number, mode?: GraphicMode) => T;
104
- qrCode: (content: string, width: number, x: number, y: number) => T;
105
- barCode: (content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment) => T;
104
+ qrCode: (content: string, width: number, x: number, y: number, rotation?: Rotation) => T;
105
+ barCode: (content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment, barWidth?: number) => T;
106
106
  /**
107
107
  * Should instruct the printer to display the image of the label on its screen instead of printing it
108
108
  */
@@ -162,7 +162,7 @@ type LabelDirection = "normal" | "inverse";
162
162
  type ECCLevel = "L" | "M" | "Q" | "H";
163
163
  type AutoManual = "A" | "M";
164
164
  type QRModel = "M1" | "M2";
165
- type BarcodeType = "CODE128" | "EAN13" | "EAN8" | "EAN5" | "EAN2" | "UPC" | "CODE39" | "ITF14" | "MSI10" | "MSI11" | "MSI1010" | "MSI1110" | "pharmacode" | "codabar";
165
+ type BarcodeType = "128" | "EAN128" | "25" | "25C" | "39" | "39C" | "93" | "EAN13" | "EAN13+2" | "EAN13+5" | "EAN8" | "EAN8+2" | "EAN8+5" | "CODA" | "POST" | "UPCA" | "UPCA+2" | "UPCA+5" | "UPCE" | "UPCE+2" | "UPCE+5" | "CPOST" | "MSI" | "MSIC" | "PLESSEY" | "ITF14" | "EAN14" | "11" | "TELEPEN" | "TELEPENN" | "PLANET" | "CODE49" | "DPI" | "DPL";
166
166
  type BarcodeHumanReable = "none" | "left" | "right" | "center";
167
167
 
168
168
  /**
@@ -413,14 +413,23 @@ declare class TSPLDensityCommand extends TSPLCommand {
413
413
  declare class TSPLCommandGenerator implements CommandGenerator<TSPLCommand> {
414
414
  commandGroup(commands: TSPLCommand[]): TSPLCommandGroup;
415
415
  print(sets: number, copiesPerSet: number): TSPLCommand;
416
- text(content: string, x: number, y: number, font: string | "default", size: number): TSPLCommand;
416
+ text(content: string, x: number, y: number, font: string | "default", size: number, rotation?: Rotation): TSPLCommand;
417
417
  upload(name: string, data: ArrayBuffer | Uint8Array): TSPLCommand;
418
418
  setUp(width: number, height: number, gap: number, offset: number, direction: LabelDirection, mirror: boolean | undefined, unitSystem: UnitSystem, density: number): TSPLCommand;
419
419
  display(): TSPLCommandGroup;
420
420
  line(start: Point, end: Point, thickness: number): TSPLCommand;
421
421
  image(image: BitmapLike, x: number, y: number, mode?: GraphicMode | undefined): TSPLCommand;
422
- qrCode(content: string, width: number, x: number, y: number): TSPLCommand;
423
- barCode(content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment): TSPLCommand;
422
+ qrCode(content: string, width: number, x: number, y: number, rotation?: Rotation): TSPLCommand;
423
+ barCode(content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment, barWidth?: number): TSPLCommand;
424
+ /**
425
+ * Calculates the narrow and wide element widths for a barcode type based on the
426
+ * TSPL documentation's standard narrow:wide ratios.
427
+ *
428
+ * Types that only support 1:1 (continuous barcodes like CODE128, EAN, UPC) get
429
+ * narrow === wide. All other types use the 1:3 ratio, which is the industry
430
+ * standard for variable-ratio symbologies (CODE39, ITF14, codabar, MSI…).
431
+ */
432
+ private static narrowWideFor;
424
433
  private cellCount;
425
434
  }
426
435
  declare const _default: TSPLCommandGenerator;
@@ -514,6 +523,8 @@ type PrintConfig = {
514
523
  /** Width of the text in dots */
515
524
  textWidth: (text: string, font: FontOption) => number;
516
525
  getFontName: (font: FontOption) => string;
526
+ /** Factor to correct text width measurement vs printer rendering (default 1.0, typical 0.9-1.0) */
527
+ textWidthCorrectionFactor: number;
517
528
  };
518
529
  /**
519
530
  * A component that can be directly printed to label printer
@@ -565,10 +576,18 @@ declare class Label extends Printable {
565
576
  */
566
577
  private fields;
567
578
  private fontCounter;
579
+ private _textWidthCorrectionFactor;
568
580
  /**
569
581
  * Configuration used when generating commands
570
582
  */
571
583
  get printConfig(): PrintConfig;
584
+ /**
585
+ * Sets a correction factor for text width measurement.
586
+ * Values < 1.0 make text wrap less aggressively (more chars per line).
587
+ * Useful when the printer renders text narrower than fontkit measures.
588
+ * @param factor Correction factor (default 1.0, typical range 0.9-1.0)
589
+ */
590
+ setTextWidthCorrectionFactor(factor: number): void;
572
591
  constructor(width: number, height: number, dimensionUnit?: UnitSystem, dpi?: number, density?: number);
573
592
  commandForLanguage(language: PrinterLanguage, config?: PrintConfig): Promise<Command>;
574
593
  /**
@@ -651,9 +670,9 @@ declare class Text extends LabelField {
651
670
  private readonly formatted;
652
671
  private font;
653
672
  private type;
673
+ private rotation;
654
674
  private context;
655
675
  private readonly lineSpacing;
656
- private endsWithBreak;
657
676
  /**
658
677
  * Width of the text.
659
678
  * If set, the text will be clipped to this size
@@ -664,6 +683,7 @@ declare class Text extends LabelField {
664
683
  * Height of the text box, if empty and the type is multiline, the box can grow infinitely
665
684
  */
666
685
  private height;
686
+ private endsWithBreak;
667
687
  constructor(content: string, x: number, y: number, formatted?: boolean);
668
688
  /**
669
689
  * Sets the field to single line
@@ -682,6 +702,21 @@ declare class Text extends LabelField {
682
702
  * that is registered on the label using 'registerFont'.
683
703
  */
684
704
  setFont(font: FontOption): void;
705
+ /**
706
+ * Set the rotation of the text field. All text commands in this field will be rotated.
707
+ * For multiline text, lines advance perpendicular to the character direction.
708
+ */
709
+ setRotation(rotation: Rotation): void;
710
+ /** Advance cursor by `amount` along the character direction */
711
+ private advanceChar;
712
+ /** Advance to the next line (reset char axis, advance in line direction) */
713
+ private advanceLine;
714
+ /** How far into the current line the cursor is (for width constraint calc) */
715
+ private charOffset;
716
+ /** How many lines deep we are (for height constraint calc) */
717
+ private lineOffset;
718
+ /** Whether the cursor is at the start of its line (char axis at origin) */
719
+ private atLineStart;
685
720
  commandForLanguage(language: PrinterLanguage, config?: PrintConfig): Promise<Command>;
686
721
  /**
687
722
  * Generate commands for formatted text
@@ -708,8 +743,9 @@ declare class Text extends LabelField {
708
743
  private getFontName;
709
744
  private get textWidthFunction();
710
745
  /**
711
- * This function is used to calculate the font size if no
712
- * print config is provided. This will asume that the font has square characters
746
+ * Fallback width estimate when no font metrics are available.
747
+ * Uses the TSPL x-multiplication value (dotToPoint of the dot size) as
748
+ * the per-character width, which matches the rendered width of font "0".
713
749
  */
714
750
  private defaultTextWidth;
715
751
  }
@@ -720,10 +756,20 @@ declare class BarCode extends LabelField {
720
756
  private readonly y;
721
757
  private readonly type;
722
758
  private readonly height;
759
+ private readonly barWidth;
723
760
  private rotation;
724
761
  private humanReadable;
725
762
  private readonly alignment;
726
- constructor(content: string, x: number, y: number, type: BarcodeType, height: number);
763
+ /**
764
+ * @param content Content to encode
765
+ * @param x X coordinate in dots
766
+ * @param y Y coordinate in dots
767
+ * @param type Barcode symbology
768
+ * @param height Height of the barcode in dots
769
+ * @param barWidth Width of the narrow bar element in dots. The wide element width is
770
+ * calculated automatically based on the symbology's standard ratio.
771
+ */
772
+ constructor(content: string, x: number, y: number, type: BarcodeType, height: number, barWidth?: number);
727
773
  setRotation(rotation: Rotation): void;
728
774
  setHumanReadable(humanReadable: BarcodeHumanReable): void;
729
775
  commandForLanguage(language: PrinterLanguage, _config?: PrintConfig | undefined): Promise<Command>;
@@ -739,7 +785,9 @@ declare class Image extends LabelField {
739
785
  */
740
786
  private readonly y;
741
787
  private readonly image;
788
+ private rotation;
742
789
  constructor(x: number, y: number, image: BitmapLike);
790
+ setRotation(rotation: Rotation): void;
743
791
  commandForLanguage(language: PrinterLanguage, _config?: PrintConfig | undefined): Promise<Command>;
744
792
  /**
745
793
  * Create an image field for an image
@@ -758,7 +806,9 @@ declare class QRCode extends LabelField {
758
806
  private readonly x;
759
807
  private readonly y;
760
808
  private readonly width;
809
+ private rotation;
761
810
  constructor(content: string, x: number, y: number, width: number);
811
+ setRotation(rotation: Rotation): void;
762
812
  commandForLanguage(language: PrinterLanguage, config?: PrintConfig | undefined): Promise<Command>;
763
813
  }
764
814
 
package/dist/index.d.ts CHANGED
@@ -97,12 +97,12 @@ type BWBitmap = BitmapLike;
97
97
  interface CommandGenerator<T extends Command> {
98
98
  commandGroup: (commands: T[]) => CommandGroup<T>;
99
99
  print: (sets: number, copiesPerSet: number) => T;
100
- text: (content: string, x: number, y: number, font: string | "default", size: number) => T;
100
+ text: (content: string, x: number, y: number, font: string | "default", size: number, rotation?: Rotation) => T;
101
101
  upload: (name: string, data: ArrayBuffer | Uint8Array) => T;
102
102
  line: (start: Point, end: Point, thickness: number) => T;
103
103
  image: (image: BitmapLike, x: number, y: number, mode?: GraphicMode) => T;
104
- qrCode: (content: string, width: number, x: number, y: number) => T;
105
- barCode: (content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment) => T;
104
+ qrCode: (content: string, width: number, x: number, y: number, rotation?: Rotation) => T;
105
+ barCode: (content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment, barWidth?: number) => T;
106
106
  /**
107
107
  * Should instruct the printer to display the image of the label on its screen instead of printing it
108
108
  */
@@ -162,7 +162,7 @@ type LabelDirection = "normal" | "inverse";
162
162
  type ECCLevel = "L" | "M" | "Q" | "H";
163
163
  type AutoManual = "A" | "M";
164
164
  type QRModel = "M1" | "M2";
165
- type BarcodeType = "CODE128" | "EAN13" | "EAN8" | "EAN5" | "EAN2" | "UPC" | "CODE39" | "ITF14" | "MSI10" | "MSI11" | "MSI1010" | "MSI1110" | "pharmacode" | "codabar";
165
+ type BarcodeType = "128" | "EAN128" | "25" | "25C" | "39" | "39C" | "93" | "EAN13" | "EAN13+2" | "EAN13+5" | "EAN8" | "EAN8+2" | "EAN8+5" | "CODA" | "POST" | "UPCA" | "UPCA+2" | "UPCA+5" | "UPCE" | "UPCE+2" | "UPCE+5" | "CPOST" | "MSI" | "MSIC" | "PLESSEY" | "ITF14" | "EAN14" | "11" | "TELEPEN" | "TELEPENN" | "PLANET" | "CODE49" | "DPI" | "DPL";
166
166
  type BarcodeHumanReable = "none" | "left" | "right" | "center";
167
167
 
168
168
  /**
@@ -413,14 +413,23 @@ declare class TSPLDensityCommand extends TSPLCommand {
413
413
  declare class TSPLCommandGenerator implements CommandGenerator<TSPLCommand> {
414
414
  commandGroup(commands: TSPLCommand[]): TSPLCommandGroup;
415
415
  print(sets: number, copiesPerSet: number): TSPLCommand;
416
- text(content: string, x: number, y: number, font: string | "default", size: number): TSPLCommand;
416
+ text(content: string, x: number, y: number, font: string | "default", size: number, rotation?: Rotation): TSPLCommand;
417
417
  upload(name: string, data: ArrayBuffer | Uint8Array): TSPLCommand;
418
418
  setUp(width: number, height: number, gap: number, offset: number, direction: LabelDirection, mirror: boolean | undefined, unitSystem: UnitSystem, density: number): TSPLCommand;
419
419
  display(): TSPLCommandGroup;
420
420
  line(start: Point, end: Point, thickness: number): TSPLCommand;
421
421
  image(image: BitmapLike, x: number, y: number, mode?: GraphicMode | undefined): TSPLCommand;
422
- qrCode(content: string, width: number, x: number, y: number): TSPLCommand;
423
- barCode(content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment): TSPLCommand;
422
+ qrCode(content: string, width: number, x: number, y: number, rotation?: Rotation): TSPLCommand;
423
+ barCode(content: string, x: number, y: number, type: BarcodeType, height: number, rotation: Rotation, humanReadable: BarcodeHumanReable, alignment: Alignment, barWidth?: number): TSPLCommand;
424
+ /**
425
+ * Calculates the narrow and wide element widths for a barcode type based on the
426
+ * TSPL documentation's standard narrow:wide ratios.
427
+ *
428
+ * Types that only support 1:1 (continuous barcodes like CODE128, EAN, UPC) get
429
+ * narrow === wide. All other types use the 1:3 ratio, which is the industry
430
+ * standard for variable-ratio symbologies (CODE39, ITF14, codabar, MSI…).
431
+ */
432
+ private static narrowWideFor;
424
433
  private cellCount;
425
434
  }
426
435
  declare const _default: TSPLCommandGenerator;
@@ -514,6 +523,8 @@ type PrintConfig = {
514
523
  /** Width of the text in dots */
515
524
  textWidth: (text: string, font: FontOption) => number;
516
525
  getFontName: (font: FontOption) => string;
526
+ /** Factor to correct text width measurement vs printer rendering (default 1.0, typical 0.9-1.0) */
527
+ textWidthCorrectionFactor: number;
517
528
  };
518
529
  /**
519
530
  * A component that can be directly printed to label printer
@@ -565,10 +576,18 @@ declare class Label extends Printable {
565
576
  */
566
577
  private fields;
567
578
  private fontCounter;
579
+ private _textWidthCorrectionFactor;
568
580
  /**
569
581
  * Configuration used when generating commands
570
582
  */
571
583
  get printConfig(): PrintConfig;
584
+ /**
585
+ * Sets a correction factor for text width measurement.
586
+ * Values < 1.0 make text wrap less aggressively (more chars per line).
587
+ * Useful when the printer renders text narrower than fontkit measures.
588
+ * @param factor Correction factor (default 1.0, typical range 0.9-1.0)
589
+ */
590
+ setTextWidthCorrectionFactor(factor: number): void;
572
591
  constructor(width: number, height: number, dimensionUnit?: UnitSystem, dpi?: number, density?: number);
573
592
  commandForLanguage(language: PrinterLanguage, config?: PrintConfig): Promise<Command>;
574
593
  /**
@@ -651,9 +670,9 @@ declare class Text extends LabelField {
651
670
  private readonly formatted;
652
671
  private font;
653
672
  private type;
673
+ private rotation;
654
674
  private context;
655
675
  private readonly lineSpacing;
656
- private endsWithBreak;
657
676
  /**
658
677
  * Width of the text.
659
678
  * If set, the text will be clipped to this size
@@ -664,6 +683,7 @@ declare class Text extends LabelField {
664
683
  * Height of the text box, if empty and the type is multiline, the box can grow infinitely
665
684
  */
666
685
  private height;
686
+ private endsWithBreak;
667
687
  constructor(content: string, x: number, y: number, formatted?: boolean);
668
688
  /**
669
689
  * Sets the field to single line
@@ -682,6 +702,21 @@ declare class Text extends LabelField {
682
702
  * that is registered on the label using 'registerFont'.
683
703
  */
684
704
  setFont(font: FontOption): void;
705
+ /**
706
+ * Set the rotation of the text field. All text commands in this field will be rotated.
707
+ * For multiline text, lines advance perpendicular to the character direction.
708
+ */
709
+ setRotation(rotation: Rotation): void;
710
+ /** Advance cursor by `amount` along the character direction */
711
+ private advanceChar;
712
+ /** Advance to the next line (reset char axis, advance in line direction) */
713
+ private advanceLine;
714
+ /** How far into the current line the cursor is (for width constraint calc) */
715
+ private charOffset;
716
+ /** How many lines deep we are (for height constraint calc) */
717
+ private lineOffset;
718
+ /** Whether the cursor is at the start of its line (char axis at origin) */
719
+ private atLineStart;
685
720
  commandForLanguage(language: PrinterLanguage, config?: PrintConfig): Promise<Command>;
686
721
  /**
687
722
  * Generate commands for formatted text
@@ -708,8 +743,9 @@ declare class Text extends LabelField {
708
743
  private getFontName;
709
744
  private get textWidthFunction();
710
745
  /**
711
- * This function is used to calculate the font size if no
712
- * print config is provided. This will asume that the font has square characters
746
+ * Fallback width estimate when no font metrics are available.
747
+ * Uses the TSPL x-multiplication value (dotToPoint of the dot size) as
748
+ * the per-character width, which matches the rendered width of font "0".
713
749
  */
714
750
  private defaultTextWidth;
715
751
  }
@@ -720,10 +756,20 @@ declare class BarCode extends LabelField {
720
756
  private readonly y;
721
757
  private readonly type;
722
758
  private readonly height;
759
+ private readonly barWidth;
723
760
  private rotation;
724
761
  private humanReadable;
725
762
  private readonly alignment;
726
- constructor(content: string, x: number, y: number, type: BarcodeType, height: number);
763
+ /**
764
+ * @param content Content to encode
765
+ * @param x X coordinate in dots
766
+ * @param y Y coordinate in dots
767
+ * @param type Barcode symbology
768
+ * @param height Height of the barcode in dots
769
+ * @param barWidth Width of the narrow bar element in dots. The wide element width is
770
+ * calculated automatically based on the symbology's standard ratio.
771
+ */
772
+ constructor(content: string, x: number, y: number, type: BarcodeType, height: number, barWidth?: number);
727
773
  setRotation(rotation: Rotation): void;
728
774
  setHumanReadable(humanReadable: BarcodeHumanReable): void;
729
775
  commandForLanguage(language: PrinterLanguage, _config?: PrintConfig | undefined): Promise<Command>;
@@ -739,7 +785,9 @@ declare class Image extends LabelField {
739
785
  */
740
786
  private readonly y;
741
787
  private readonly image;
788
+ private rotation;
742
789
  constructor(x: number, y: number, image: BitmapLike);
790
+ setRotation(rotation: Rotation): void;
743
791
  commandForLanguage(language: PrinterLanguage, _config?: PrintConfig | undefined): Promise<Command>;
744
792
  /**
745
793
  * Create an image field for an image
@@ -758,7 +806,9 @@ declare class QRCode extends LabelField {
758
806
  private readonly x;
759
807
  private readonly y;
760
808
  private readonly width;
809
+ private rotation;
761
810
  constructor(content: string, x: number, y: number, width: number);
811
+ setRotation(rotation: Rotation): void;
762
812
  commandForLanguage(language: PrinterLanguage, config?: PrintConfig | undefined): Promise<Command>;
763
813
  }
764
814