label-printer 0.2.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/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Label Print
2
+
3
+ This package provides a js based implementation for variouse languages used for label printers
4
+
5
+ ## Layers
6
+
7
+ The packages is logacally divided into multiple sub layers. These are not separate modules per say, but separated parts of the code that serve different purposes
8
+
9
+ ### 1. Command Layer
10
+
11
+ This layer provides a low lever wrapper for the different languages commands. Using this module, you can create commands that suite your needs the best or you can integrate this pacakge in your codebase
12
+
13
+ #### TODO
14
+
15
+ - Finish implementing basic commands
16
+ - Add example code
17
+
18
+ ### 2. Label layer
19
+
20
+ This layer provides a language independent API to construct a label (object representation) without any knowledge of the fine details of the printer the label will be printed on.
21
+
22
+ #### TODO
23
+
24
+ - Add example code
25
+ - Implement layer
26
+
27
+ ### 3. Printer layer
28
+
29
+ This layer contains code to interact with printers
30
+
31
+ #### TODO
32
+
33
+ - Add example code
34
+ - Implement layer
35
+
36
+ ## Documentation of supported languages
37
+
38
+ - [TSPL](documentations/TSPL.pdf)
@@ -0,0 +1,457 @@
1
+ /**
2
+ * Convenience wrapper for a web usb device
3
+ * Its main purpose is to hide the details of the usb library from client code so in case
4
+ * it needs to be switched, compatibility can be retained
5
+ */
6
+ declare class UsbDevice {
7
+ private readonly device;
8
+ get opened(): boolean;
9
+ /**
10
+ * All available endpoints
11
+ */
12
+ private get endpoints();
13
+ /**
14
+ * Endpoint for writing
15
+ */
16
+ private get outEndpoint();
17
+ /**
18
+ * Endpoint for reading
19
+ */
20
+ private get inEndpoint();
21
+ constructor(device: USBDevice);
22
+ /**
23
+ * Open the device and claim its interface
24
+ */
25
+ openAndConfigure(): Promise<void>;
26
+ /**
27
+ * Closes the device
28
+ */
29
+ close(): Promise<void>;
30
+ /**
31
+ * Write data to an USB device
32
+ * @param data Data to write
33
+ */
34
+ writeData(data: Uint8Array): Promise<void>;
35
+ /**
36
+ * Writes a text to a device
37
+ * @param text Text to write
38
+ */
39
+ writeString(text: string): Promise<void>;
40
+ /**
41
+ * Reads bytes from the usb device
42
+ * @param length The max length of the incoming data.
43
+ * @returns Bytes received as a DataView or undefined. If data is longer then `length`, undefined will be returned
44
+ */
45
+ readData(length: number): Promise<DataView | undefined>;
46
+ /**
47
+ * Reads data from the usb device and converts it to string
48
+ * {@see readData}
49
+ */
50
+ readString(length: number): Promise<string | undefined>;
51
+ }
52
+
53
+ /**
54
+ * Base implementation for all types of command
55
+ * Represents a command to be send to the printer to execute, such as print
56
+ */
57
+ declare abstract class Command {
58
+ /**
59
+ * Returns a string representation of the command
60
+ */
61
+ abstract get commandString(): string;
62
+ /**
63
+ * Write the command data to a USB device
64
+ * @param device Device to write to
65
+ */
66
+ write(device: UsbDevice): Promise<void>;
67
+ /**
68
+ * Byte representation of a newline
69
+ */
70
+ protected get commandTerminatorBytes(): Uint8Array;
71
+ /**
72
+ * Writes a string to a device
73
+ * @param data String representation of data
74
+ * @param device Device to write to
75
+ */
76
+ protected writeString(data: string, device: UsbDevice): Promise<void>;
77
+ /**
78
+ * Writes bytes to a device. It will automatically end a command with @see{commandTerminatorBytes}
79
+ * @param data Byte array to send
80
+ * @param device Device to write to
81
+ */
82
+ protected writeBytes(data: Uint8Array, device: UsbDevice): Promise<void>;
83
+ /**
84
+ * Write the command terminator to the device
85
+ * @param device
86
+ */
87
+ protected terminateCommand(device: UsbDevice): Promise<void>;
88
+ }
89
+
90
+ /**
91
+ * A utility class that helps groupping commands together
92
+ * Should be implemnted with a specific command type to ensure only commands for the same lagnuage are
93
+ * groupped together
94
+ */
95
+ declare abstract class CommandGroup<T extends Command> extends Command {
96
+ private commands;
97
+ constructor(commands: T[]);
98
+ write(device: UsbDevice): Promise<void>;
99
+ get commandString(): string;
100
+ }
101
+
102
+ /**
103
+ * Base for TSPL specific commands
104
+ */
105
+ declare abstract class TSPLCommand extends Command {
106
+ }
107
+
108
+ /**
109
+ * A raw TSPL command. Can be used to use a command that is not yet supported
110
+ */
111
+ declare class TSPLRawCommand extends TSPLCommand {
112
+ /**
113
+ * Raw command string
114
+ */
115
+ private readonly commandBody;
116
+ /**
117
+ * Initialize a command with a raw body
118
+ * @param body
119
+ */
120
+ constructor(body: string);
121
+ get commandString(): string;
122
+ }
123
+
124
+ /**
125
+ * Command group for tspl commands
126
+ */
127
+ declare class TSPLCommandGroup extends CommandGroup<TSPLCommand> {
128
+ }
129
+
130
+ /**
131
+ * Base for all TSPL commands that have a visual effect on the final label
132
+ */
133
+ declare abstract class TSPLVisualCommand extends TSPLCommand {
134
+ protected readonly x: number;
135
+ protected readonly y: number;
136
+ constructor(x: number, y: number);
137
+ }
138
+
139
+ /**
140
+ * Helper type to transmit black and white bitmap data
141
+ */
142
+ type BWBitmap = {
143
+ width: number;
144
+ height: number;
145
+ bytes: Uint8Array;
146
+ };
147
+
148
+ type Rotation = 0 | 90 | 180 | 270;
149
+ type Alignment = undefined | "left" | "center" | "right";
150
+ /**
151
+ * Convert an alignemnt string to its number value
152
+ * @param alignment
153
+ * @returns
154
+ */
155
+ declare const alignmentToNumber: (alignment: Alignment) => 0 | 1 | 2 | 3;
156
+ /**
157
+ * Represents the strategy to use when two bitmaps overlap. The final value will be determined by
158
+ * either overwriting the first bitmap's value with the second one or performing an 'or' or 'xor' operation
159
+ * on the values
160
+ */
161
+ type GraphicMode = "overwrite" | "or" | "xor";
162
+ type UnitSystem = "imperial" | "metric" | "dot";
163
+ type ECCLevel = "L" | "M" | "Q" | "H";
164
+ type AutoManual = "A" | "M";
165
+ type QRModel = "M1" | "M2";
166
+ type BarcodeType = "CODE128" | "EAN13" | "EAN8" | "EAN5" | "EAN2" | "UPC" | "CODE39" | "ITF14" | "MSI10" | "MSI11" | "MSI1010" | "MSI1110" | "pharmacode" | "codabar";
167
+ type BarcodeHumanReable = "none" | "left" | "right" | "center";
168
+
169
+ /**
170
+ * Represents a bitmap command. Can be used to draw an image to the label
171
+ * {@link /documentsions/TSPL.pdf}
172
+ */
173
+ declare class TSPLBitmapCommand extends TSPLVisualCommand {
174
+ /**
175
+ * Bitmap to present.
176
+ * TSPL only supports black and write printing so this bitmap contains a matrix of 1 (White pixel)
177
+ * and 0 (Black pixel) values
178
+ */
179
+ private readonly bitmap;
180
+ private readonly mode;
181
+ /**
182
+ *
183
+ * @param bitmap Bitmap to present.
184
+ * @param x X coordinates in dots
185
+ * @param y Y Coordinates in dots
186
+ * @param mode Represents the strategy to use when two bitmaps overlap. The final value will be determined by
187
+ * either overwriting the first bitmap's value with the second one or performing an 'or' or 'xor' operation on the values
188
+ */
189
+ constructor(bitmap: BWBitmap, x: number, y: number, mode?: GraphicMode);
190
+ get commandString(): string;
191
+ private get commandWithoutBytes();
192
+ private get modeValue();
193
+ write(device: UsbDevice): Promise<void>;
194
+ }
195
+
196
+ /**
197
+ * Prints a single line text on the label
198
+ * {@link /documentsions/TSPL.pdf}
199
+ */
200
+ declare class TSPLTextCommand extends TSPLVisualCommand {
201
+ /**
202
+ * Name of the font to use. Consult documentation for more info
203
+ */
204
+ protected readonly font: string;
205
+ /**
206
+ * Angle of rotation of the text. 0, 90, 180 or 270
207
+ */
208
+ protected readonly rotatation: Rotation;
209
+ /**
210
+ * Multiplication of the font size in x axis
211
+ */
212
+ protected readonly xMultiplication: number;
213
+ /**
214
+ * Multiplication of the font size in y axis
215
+ */
216
+ protected readonly yMultiplication: number;
217
+ /**
218
+ * Text alignment. Left, Center or Right. The default alignmnet is left
219
+ */
220
+ protected readonly alignment: number;
221
+ /**
222
+ * Text to print
223
+ */
224
+ protected readonly content: string;
225
+ constructor(content: string, x: number, y: number, font: string, rotation?: Rotation, xMultiplication?: number, yMultiplication?: number, alignment?: Alignment);
226
+ get commandString(): string;
227
+ }
228
+
229
+ /**
230
+ * Defines the gap between two labels
231
+ * {@link /documentsions/TSPL.pdf}
232
+ */
233
+ declare class TSPLGapCommand extends TSPLCommand {
234
+ private readonly gap;
235
+ private readonly offset;
236
+ /**
237
+ * This controls what unit the {@link width} and {@link height} will be
238
+ * - For imperial, the unit is inches
239
+ * - For metric, the unit is milimeters
240
+ * - For dots, the unit is dots
241
+ */
242
+ private readonly unitSystem;
243
+ constructor(gap: number, offset: number, unitSystem: UnitSystem);
244
+ get commandString(): string;
245
+ private valueWithUnit;
246
+ }
247
+
248
+ /**
249
+ * Defines the size of the label to rpint
250
+ * {@link /documentsions/TSPL.pdf}
251
+ */
252
+ declare class TSPLSizeCommand extends TSPLCommand {
253
+ private readonly width;
254
+ private readonly height;
255
+ /**
256
+ * This controls what unit the {@link width} and {@link height} will be
257
+ * - For imperial, the unit is inches
258
+ * - For metric, the unit is milimeters
259
+ * - For dots, the unit is dots
260
+ */
261
+ private readonly unitSystem;
262
+ constructor(width: number, height: number, unitSystem: UnitSystem);
263
+ get commandString(): string;
264
+ private valueWithUnit;
265
+ }
266
+
267
+ /**
268
+ * Clear the image buffer
269
+ * {@link /documentsions/TSPL.pdf}
270
+ */
271
+ declare class TSPLCLSCommand extends TSPLCommand {
272
+ get commandString(): string;
273
+ }
274
+
275
+ /**
276
+ * This command defines in which direction will the label be printed and wether or not to mirror the image
277
+ * {@link /documentsions/TSPL.pdf}
278
+ */
279
+ declare class TSPLDirectionCommand extends TSPLCommand {
280
+ private readonly direction;
281
+ private readonly mirror;
282
+ /**
283
+ * @param direction Controls the orientation of the resulting label compared to the printer
284
+ * @param mirror Controls mirroring relative to the center line of the label perpendicular to the printhead. See the documentsion for examples
285
+ */
286
+ constructor(direction: "normal" | "inverse", mirror?: boolean);
287
+ get commandString(): string;
288
+ }
289
+
290
+ /**
291
+ * Prints the current image buffer
292
+ * {@link /documentsions/TSPL.pdf}
293
+ */
294
+ declare class TSPLPrintCommand extends TSPLCommand {
295
+ /**
296
+ * The number of set to print.
297
+ */
298
+ private readonly sets;
299
+ /**
300
+ * The number of copies to print of each set.
301
+ * The difference between a set and a copy is that if you have a counter for example,
302
+ * the counter will be incremented for each set but not for each copy
303
+ */
304
+ private readonly copies;
305
+ constructor(sets: number, copies?: number);
306
+ get commandString(): string;
307
+ }
308
+
309
+ /**
310
+ * Draws a black bar
311
+ * {@link /documentsions/TSPL.pdf}
312
+ */
313
+ declare class TSPLBarCommand extends TSPLVisualCommand {
314
+ private readonly width;
315
+ private readonly height;
316
+ /**
317
+ * @param x X coordinates in dots
318
+ * @param y Y coordinates in dots
319
+ * @param width Width of tha bar in dots
320
+ * @param height Height of the bar in dots
321
+ */
322
+ constructor(x: number, y: number, width: number, height: number);
323
+ get commandString(): string;
324
+ }
325
+
326
+ /**
327
+ * Prints a QR code
328
+ * {@link /documentsions/TSPL.pdf}
329
+ */
330
+ declare class TSPLQRCommand extends TSPLVisualCommand {
331
+ private readonly ecc;
332
+ private readonly cellWidth;
333
+ private readonly mode;
334
+ private readonly rotation;
335
+ private readonly model;
336
+ /**
337
+ * Should be between 0 and 7
338
+ */
339
+ private readonly mask;
340
+ private readonly content;
341
+ constructor(content: string, x: number, y: number, cellWidth: number, ecc?: ECCLevel, mode?: AutoManual, rotation?: Rotation, model?: QRModel, mask?: number);
342
+ get commandString(): string;
343
+ }
344
+
345
+ declare class TSPLBlockCommand extends TSPLTextCommand {
346
+ private readonly width;
347
+ private readonly height;
348
+ private readonly lineSpacing;
349
+ constructor(content: string, x: number, y: number, width: number, height: number, font: string, rotation?: Rotation, xMultiplication?: number, yMultiplication?: number, lineSpacing?: number, alignment?: Alignment);
350
+ get commandString(): string;
351
+ }
352
+
353
+ type index$2_Alignment = Alignment;
354
+ type index$2_AutoManual = AutoManual;
355
+ type index$2_BarcodeHumanReable = BarcodeHumanReable;
356
+ type index$2_BarcodeType = BarcodeType;
357
+ type index$2_ECCLevel = ECCLevel;
358
+ type index$2_GraphicMode = GraphicMode;
359
+ type index$2_QRModel = QRModel;
360
+ type index$2_Rotation = Rotation;
361
+ type index$2_TSPLBarCommand = TSPLBarCommand;
362
+ declare const index$2_TSPLBarCommand: typeof TSPLBarCommand;
363
+ type index$2_TSPLBitmapCommand = TSPLBitmapCommand;
364
+ declare const index$2_TSPLBitmapCommand: typeof TSPLBitmapCommand;
365
+ type index$2_TSPLBlockCommand = TSPLBlockCommand;
366
+ declare const index$2_TSPLBlockCommand: typeof TSPLBlockCommand;
367
+ type index$2_TSPLCLSCommand = TSPLCLSCommand;
368
+ declare const index$2_TSPLCLSCommand: typeof TSPLCLSCommand;
369
+ type index$2_TSPLCommand = TSPLCommand;
370
+ declare const index$2_TSPLCommand: typeof TSPLCommand;
371
+ type index$2_TSPLCommandGroup = TSPLCommandGroup;
372
+ declare const index$2_TSPLCommandGroup: typeof TSPLCommandGroup;
373
+ type index$2_TSPLDirectionCommand = TSPLDirectionCommand;
374
+ declare const index$2_TSPLDirectionCommand: typeof TSPLDirectionCommand;
375
+ type index$2_TSPLGapCommand = TSPLGapCommand;
376
+ declare const index$2_TSPLGapCommand: typeof TSPLGapCommand;
377
+ type index$2_TSPLPrintCommand = TSPLPrintCommand;
378
+ declare const index$2_TSPLPrintCommand: typeof TSPLPrintCommand;
379
+ type index$2_TSPLQRCommand = TSPLQRCommand;
380
+ declare const index$2_TSPLQRCommand: typeof TSPLQRCommand;
381
+ type index$2_TSPLRawCommand = TSPLRawCommand;
382
+ declare const index$2_TSPLRawCommand: typeof TSPLRawCommand;
383
+ type index$2_TSPLSizeCommand = TSPLSizeCommand;
384
+ declare const index$2_TSPLSizeCommand: typeof TSPLSizeCommand;
385
+ type index$2_TSPLTextCommand = TSPLTextCommand;
386
+ declare const index$2_TSPLTextCommand: typeof TSPLTextCommand;
387
+ type index$2_TSPLVisualCommand = TSPLVisualCommand;
388
+ declare const index$2_TSPLVisualCommand: typeof TSPLVisualCommand;
389
+ type index$2_UnitSystem = UnitSystem;
390
+ declare const index$2_alignmentToNumber: typeof alignmentToNumber;
391
+ declare namespace index$2 {
392
+ export { type index$2_Alignment as Alignment, type index$2_AutoManual as AutoManual, type index$2_BarcodeHumanReable as BarcodeHumanReable, type index$2_BarcodeType as BarcodeType, type index$2_ECCLevel as ECCLevel, type index$2_GraphicMode as GraphicMode, type index$2_QRModel as QRModel, type index$2_Rotation as Rotation, index$2_TSPLBarCommand as TSPLBarCommand, index$2_TSPLBitmapCommand as TSPLBitmapCommand, index$2_TSPLBlockCommand as TSPLBlockCommand, index$2_TSPLCLSCommand as TSPLCLSCommand, index$2_TSPLCommand as TSPLCommand, index$2_TSPLCommandGroup as TSPLCommandGroup, index$2_TSPLDirectionCommand as TSPLDirectionCommand, index$2_TSPLGapCommand as TSPLGapCommand, index$2_TSPLPrintCommand as TSPLPrintCommand, index$2_TSPLQRCommand as TSPLQRCommand, index$2_TSPLRawCommand as TSPLRawCommand, index$2_TSPLSizeCommand as TSPLSizeCommand, index$2_TSPLTextCommand as TSPLTextCommand, index$2_TSPLVisualCommand as TSPLVisualCommand, type index$2_UnitSystem as UnitSystem, index$2_alignmentToNumber as alignmentToNumber };
393
+ }
394
+
395
+ type PrinterLanguage = "tspl";
396
+
397
+ type index$1_Command = Command;
398
+ declare const index$1_Command: typeof Command;
399
+ type index$1_CommandGroup<T extends Command> = CommandGroup<T>;
400
+ declare const index$1_CommandGroup: typeof CommandGroup;
401
+ type index$1_PrinterLanguage = PrinterLanguage;
402
+ declare namespace index$1 {
403
+ export { index$1_Command as Command, index$1_CommandGroup as CommandGroup, type index$1_PrinterLanguage as PrinterLanguage, index$2 as tspl };
404
+ }
405
+
406
+ /**
407
+ * Base class that encapsulates functionality of all printers
408
+ */
409
+ declare abstract class Printer {
410
+ private readonly usbDevice;
411
+ /**
412
+ * Printer language used by the type of printer the subclass represents
413
+ */
414
+ abstract get language(): PrinterLanguage;
415
+ /**
416
+ * When called, it will feed the labels to the beginig of the next label
417
+ */
418
+ abstract feedLabel(): Promise<void>;
419
+ constructor(device: UsbDevice);
420
+ /**
421
+ * Close the printer USB
422
+ */
423
+ close(): Promise<void>;
424
+ /**
425
+ * Writes a command to the printers usb
426
+ * @param command Command to send to the usb
427
+ */
428
+ protected writeCommand(command: Command): Promise<void>;
429
+ /**
430
+ * Check if the device is indeed a printer
431
+ * @param device
432
+ */
433
+ static try(_device: UsbDevice): Promise<boolean>;
434
+ }
435
+
436
+ declare class PrinterService {
437
+ /**
438
+ * Try each type of printer and return the one that mathces the usb device
439
+ * @param device
440
+ * @returns
441
+ */
442
+ static printerForDevice(device: UsbDevice): Promise<Printer | undefined>;
443
+ /**
444
+ * @returns List of available printers
445
+ */
446
+ static getPrinters(): Promise<Printer[]>;
447
+ }
448
+
449
+ type index_Printer = Printer;
450
+ declare const index_Printer: typeof Printer;
451
+ type index_PrinterService = PrinterService;
452
+ declare const index_PrinterService: typeof PrinterService;
453
+ declare namespace index {
454
+ export { index_Printer as Printer, index_PrinterService as PrinterService };
455
+ }
456
+
457
+ export { index$1 as commands, index as printers };