label-printer 0.7.7 → 0.8.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 +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +67 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +67 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -26,6 +26,12 @@ declare abstract class Command {
|
|
|
26
26
|
* Returns a string representation of the command
|
|
27
27
|
*/
|
|
28
28
|
abstract get commandString(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Returns a byte array representation of the command
|
|
31
|
+
* By default, converts commandString to bytes, but can be overridden for commands
|
|
32
|
+
* that include binary data (e.g., images, fonts)
|
|
33
|
+
*/
|
|
34
|
+
get commandBytes(): Uint8Array;
|
|
29
35
|
print(fn: (command: string) => void): void;
|
|
30
36
|
/**
|
|
31
37
|
* Write the command data to a device
|
|
@@ -66,6 +72,7 @@ declare abstract class CommandGroup<T extends Command> extends Command {
|
|
|
66
72
|
print(fn: (command: string) => void): void;
|
|
67
73
|
writeTo(device: Device): Promise<void>;
|
|
68
74
|
get commandString(): string;
|
|
75
|
+
get commandBytes(): Uint8Array;
|
|
69
76
|
}
|
|
70
77
|
|
|
71
78
|
/**
|
|
@@ -181,6 +188,7 @@ declare class TSPLBitmapCommand extends TSPLVisualCommand {
|
|
|
181
188
|
get commandString(): string;
|
|
182
189
|
private get commandWithoutBytes();
|
|
183
190
|
private get modeValue();
|
|
191
|
+
get commandBytes(): Uint8Array;
|
|
184
192
|
writeTo(device: Device): Promise<void>;
|
|
185
193
|
/**
|
|
186
194
|
* Create a new bitmap command for the given image url
|
|
@@ -366,6 +374,7 @@ declare class TSPLDownload extends TSPLCommand {
|
|
|
366
374
|
*/
|
|
367
375
|
constructor(fileName: string, data: Data);
|
|
368
376
|
get commandString(): string;
|
|
377
|
+
get commandBytes(): Uint8Array;
|
|
369
378
|
writeTo(device: Device): Promise<void>;
|
|
370
379
|
}
|
|
371
380
|
|
|
@@ -631,6 +640,7 @@ declare class Text extends LabelField {
|
|
|
631
640
|
private type;
|
|
632
641
|
private context;
|
|
633
642
|
private readonly lineSpacing;
|
|
643
|
+
private endsWithBreak;
|
|
634
644
|
/**
|
|
635
645
|
* Width of the text.
|
|
636
646
|
* If set, the text will be clipped to this size
|
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,12 @@ declare abstract class Command {
|
|
|
26
26
|
* Returns a string representation of the command
|
|
27
27
|
*/
|
|
28
28
|
abstract get commandString(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Returns a byte array representation of the command
|
|
31
|
+
* By default, converts commandString to bytes, but can be overridden for commands
|
|
32
|
+
* that include binary data (e.g., images, fonts)
|
|
33
|
+
*/
|
|
34
|
+
get commandBytes(): Uint8Array;
|
|
29
35
|
print(fn: (command: string) => void): void;
|
|
30
36
|
/**
|
|
31
37
|
* Write the command data to a device
|
|
@@ -66,6 +72,7 @@ declare abstract class CommandGroup<T extends Command> extends Command {
|
|
|
66
72
|
print(fn: (command: string) => void): void;
|
|
67
73
|
writeTo(device: Device): Promise<void>;
|
|
68
74
|
get commandString(): string;
|
|
75
|
+
get commandBytes(): Uint8Array;
|
|
69
76
|
}
|
|
70
77
|
|
|
71
78
|
/**
|
|
@@ -181,6 +188,7 @@ declare class TSPLBitmapCommand extends TSPLVisualCommand {
|
|
|
181
188
|
get commandString(): string;
|
|
182
189
|
private get commandWithoutBytes();
|
|
183
190
|
private get modeValue();
|
|
191
|
+
get commandBytes(): Uint8Array;
|
|
184
192
|
writeTo(device: Device): Promise<void>;
|
|
185
193
|
/**
|
|
186
194
|
* Create a new bitmap command for the given image url
|
|
@@ -366,6 +374,7 @@ declare class TSPLDownload extends TSPLCommand {
|
|
|
366
374
|
*/
|
|
367
375
|
constructor(fileName: string, data: Data);
|
|
368
376
|
get commandString(): string;
|
|
377
|
+
get commandBytes(): Uint8Array;
|
|
369
378
|
writeTo(device: Device): Promise<void>;
|
|
370
379
|
}
|
|
371
380
|
|
|
@@ -631,6 +640,7 @@ declare class Text extends LabelField {
|
|
|
631
640
|
private type;
|
|
632
641
|
private context;
|
|
633
642
|
private readonly lineSpacing;
|
|
643
|
+
private endsWithBreak;
|
|
634
644
|
/**
|
|
635
645
|
* Width of the text.
|
|
636
646
|
* If set, the text will be clipped to this size
|
package/dist/index.js
CHANGED
|
@@ -83,6 +83,15 @@ __export(commands_exports, {
|
|
|
83
83
|
|
|
84
84
|
// src/commands/Command.ts
|
|
85
85
|
var Command = class {
|
|
86
|
+
/**
|
|
87
|
+
* Returns a byte array representation of the command
|
|
88
|
+
* By default, converts commandString to bytes, but can be overridden for commands
|
|
89
|
+
* that include binary data (e.g., images, fonts)
|
|
90
|
+
*/
|
|
91
|
+
get commandBytes() {
|
|
92
|
+
const encoder = new TextEncoder();
|
|
93
|
+
return encoder.encode(this.commandString);
|
|
94
|
+
}
|
|
86
95
|
print(fn) {
|
|
87
96
|
fn(this.commandString);
|
|
88
97
|
}
|
|
@@ -154,6 +163,27 @@ var CommandGroup = class extends Command {
|
|
|
154
163
|
get commandString() {
|
|
155
164
|
return this.commands.map((c) => c.commandString).join("\n");
|
|
156
165
|
}
|
|
166
|
+
get commandBytes() {
|
|
167
|
+
if (this.commands.length === 0) {
|
|
168
|
+
return new Uint8Array(0);
|
|
169
|
+
}
|
|
170
|
+
const newlineBytes = new Uint8Array([10]);
|
|
171
|
+
const byteArrays = [];
|
|
172
|
+
for (let i = 0; i < this.commands.length; i++) {
|
|
173
|
+
byteArrays.push(this.commands[i].commandBytes);
|
|
174
|
+
if (i < this.commands.length - 1) {
|
|
175
|
+
byteArrays.push(newlineBytes);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const totalLength = byteArrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
179
|
+
const result = new Uint8Array(totalLength);
|
|
180
|
+
let offset = 0;
|
|
181
|
+
for (const arr of byteArrays) {
|
|
182
|
+
result.set(arr, offset);
|
|
183
|
+
offset += arr.length;
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
157
187
|
};
|
|
158
188
|
|
|
159
189
|
// src/commands/tspl/index.ts
|
|
@@ -1146,6 +1176,14 @@ var TSPLBitmapCommand = class _TSPLBitmapCommand extends TSPLVisualCommand {
|
|
|
1146
1176
|
return 2;
|
|
1147
1177
|
}
|
|
1148
1178
|
}
|
|
1179
|
+
get commandBytes() {
|
|
1180
|
+
const encoder = new TextEncoder();
|
|
1181
|
+
const commandBytes = encoder.encode(this.commandWithoutBytes);
|
|
1182
|
+
const result = new Uint8Array(commandBytes.length + this.bitmap.bytes.length);
|
|
1183
|
+
result.set(commandBytes, 0);
|
|
1184
|
+
result.set(this.bitmap.bytes, commandBytes.length);
|
|
1185
|
+
return result;
|
|
1186
|
+
}
|
|
1149
1187
|
writeTo(device) {
|
|
1150
1188
|
return __async(this, null, function* () {
|
|
1151
1189
|
yield this.writeString(this.commandWithoutBytes, device);
|
|
@@ -1327,6 +1365,15 @@ var TSPLDownload = class extends TSPLCommand {
|
|
|
1327
1365
|
get commandString() {
|
|
1328
1366
|
return `DOWNLOAD "${this.fileName}", ${this.data.byteLength},`;
|
|
1329
1367
|
}
|
|
1368
|
+
get commandBytes() {
|
|
1369
|
+
const encoder = new TextEncoder();
|
|
1370
|
+
const commandBytes = encoder.encode(this.commandString);
|
|
1371
|
+
const dataBytes = this.data instanceof Uint8Array ? this.data : new Uint8Array(this.data);
|
|
1372
|
+
const result = new Uint8Array(commandBytes.length + dataBytes.length);
|
|
1373
|
+
result.set(commandBytes, 0);
|
|
1374
|
+
result.set(dataBytes, commandBytes.length);
|
|
1375
|
+
return result;
|
|
1376
|
+
}
|
|
1330
1377
|
writeTo(device) {
|
|
1331
1378
|
return __async(this, null, function* () {
|
|
1332
1379
|
yield this.writeString(this.commandString, device);
|
|
@@ -2562,6 +2609,24 @@ var Text = class extends LabelField {
|
|
|
2562
2609
|
this.y = y;
|
|
2563
2610
|
this.formatted = formatted;
|
|
2564
2611
|
}
|
|
2612
|
+
endsWithBreak(node) {
|
|
2613
|
+
if (node.nodeType == import_node_html_parser.NodeType.TEXT_NODE) {
|
|
2614
|
+
return node.innerText.trim() == "";
|
|
2615
|
+
}
|
|
2616
|
+
const elementNode = node;
|
|
2617
|
+
if (elementNode.rawTagName == BREAK_TAG) return true;
|
|
2618
|
+
const children = elementNode.childNodes;
|
|
2619
|
+
for (let i = children.length - 1; i >= 0; i--) {
|
|
2620
|
+
const child = children[i];
|
|
2621
|
+
if (child.nodeType == import_node_html_parser.NodeType.TEXT_NODE) {
|
|
2622
|
+
if (child.innerText.trim() == "") continue;
|
|
2623
|
+
return false;
|
|
2624
|
+
}
|
|
2625
|
+
if (this.endsWithBreak(child)) return true;
|
|
2626
|
+
return false;
|
|
2627
|
+
}
|
|
2628
|
+
return false;
|
|
2629
|
+
}
|
|
2565
2630
|
/**
|
|
2566
2631
|
* Sets the field to single line
|
|
2567
2632
|
* @param width Max width of the text. Leave it undefined to allow the field to grow
|
|
@@ -2656,8 +2721,7 @@ var Text = class extends LabelField {
|
|
|
2656
2721
|
baseFont.style = "italic";
|
|
2657
2722
|
}
|
|
2658
2723
|
if (tag == PARAGRAPH_TAG) {
|
|
2659
|
-
|
|
2660
|
-
if (!isAtFieldOrigin) {
|
|
2724
|
+
if (initialX != this.x) {
|
|
2661
2725
|
currentX = this.x;
|
|
2662
2726
|
currentY = initialY + baseFont.size + this.lineSpacing;
|
|
2663
2727
|
}
|
|
@@ -2669,20 +2733,7 @@ var Text = class extends LabelField {
|
|
|
2669
2733
|
commands.push(command);
|
|
2670
2734
|
});
|
|
2671
2735
|
if (tag == PARAGRAPH_TAG) {
|
|
2672
|
-
|
|
2673
|
-
for (let i = elementNode.childNodes.length - 1; i >= 0; i--) {
|
|
2674
|
-
const node = elementNode.childNodes[i];
|
|
2675
|
-
if (node.nodeType == import_node_html_parser.NodeType.TEXT_NODE) {
|
|
2676
|
-
if (node.innerText.trim() == "") continue;
|
|
2677
|
-
break;
|
|
2678
|
-
}
|
|
2679
|
-
const childElement = node;
|
|
2680
|
-
if (childElement.rawTagName == BREAK_TAG) {
|
|
2681
|
-
paragraphEndsWithBreak = true;
|
|
2682
|
-
}
|
|
2683
|
-
break;
|
|
2684
|
-
}
|
|
2685
|
-
if (!paragraphEndsWithBreak) {
|
|
2736
|
+
if (!this.endsWithBreak(elementNode)) {
|
|
2686
2737
|
currentX = this.x;
|
|
2687
2738
|
currentY += baseFont.size + this.lineSpacing;
|
|
2688
2739
|
}
|