@risleylima/escpos 0.2.1 → 1.0.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.
Files changed (107) hide show
  1. package/README.md +94 -766
  2. package/dist/adapter/index.d.ts +29 -0
  3. package/dist/adapter/index.d.ts.map +1 -0
  4. package/dist/adapter/index.js +25 -0
  5. package/dist/adapter/index.js.map +1 -0
  6. package/dist/index.d.ts +14 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +25 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/network-adapter/index.d.ts +24 -0
  11. package/dist/network-adapter/index.d.ts.map +1 -0
  12. package/dist/network-adapter/index.js +263 -0
  13. package/dist/network-adapter/index.js.map +1 -0
  14. package/dist/printer/commands-types.d.ts +37 -0
  15. package/dist/printer/commands-types.d.ts.map +1 -0
  16. package/dist/printer/commands-types.js +63 -0
  17. package/dist/printer/commands-types.js.map +1 -0
  18. package/dist/printer/commands.d.ts +169 -0
  19. package/dist/printer/commands.d.ts.map +1 -0
  20. package/dist/printer/commands.js +192 -0
  21. package/dist/printer/commands.js.map +1 -0
  22. package/dist/printer/image-loader.d.ts +17 -0
  23. package/dist/printer/image-loader.d.ts.map +1 -0
  24. package/dist/printer/image-loader.js +462 -0
  25. package/dist/printer/image-loader.js.map +1 -0
  26. package/dist/printer/image.d.ts +43 -0
  27. package/dist/printer/image.d.ts.map +1 -0
  28. package/dist/printer/image.js +132 -0
  29. package/dist/printer/image.js.map +1 -0
  30. package/dist/printer/index.d.ts +158 -0
  31. package/dist/printer/index.d.ts.map +1 -0
  32. package/dist/printer/index.js +703 -0
  33. package/dist/printer/index.js.map +1 -0
  34. package/dist/printer/profiles/bematech/mp4200th.d.ts +13 -0
  35. package/dist/printer/profiles/bematech/mp4200th.d.ts.map +1 -0
  36. package/dist/printer/profiles/bematech/mp4200th.js +29 -0
  37. package/dist/printer/profiles/bematech/mp4200th.js.map +1 -0
  38. package/dist/printer/profiles/custom/bematech-mp4200th.d.ts +13 -0
  39. package/dist/printer/profiles/custom/bematech-mp4200th.d.ts.map +1 -0
  40. package/dist/printer/profiles/custom/bematech-mp4200th.js +21 -0
  41. package/dist/printer/profiles/custom/bematech-mp4200th.js.map +1 -0
  42. package/dist/printer/profiles/custom/vkp80iii.d.ts +19 -0
  43. package/dist/printer/profiles/custom/vkp80iii.d.ts.map +1 -0
  44. package/dist/printer/profiles/custom/vkp80iii.js +87 -0
  45. package/dist/printer/profiles/custom/vkp80iii.js.map +1 -0
  46. package/dist/printer/profiles/default.d.ts +7 -0
  47. package/dist/printer/profiles/default.d.ts.map +1 -0
  48. package/dist/printer/profiles/default.js +15 -0
  49. package/dist/printer/profiles/default.js.map +1 -0
  50. package/dist/printer/profiles/index.d.ts +41 -0
  51. package/dist/printer/profiles/index.d.ts.map +1 -0
  52. package/dist/printer/profiles/index.js +98 -0
  53. package/dist/printer/profiles/index.js.map +1 -0
  54. package/dist/printer/profiles/merge.d.ts +7 -0
  55. package/dist/printer/profiles/merge.d.ts.map +1 -0
  56. package/dist/printer/profiles/merge.js +58 -0
  57. package/dist/printer/profiles/merge.js.map +1 -0
  58. package/dist/printer/profiles/types.d.ts +99 -0
  59. package/dist/printer/profiles/types.d.ts.map +1 -0
  60. package/dist/printer/profiles/types.js +8 -0
  61. package/dist/printer/profiles/types.js.map +1 -0
  62. package/dist/printer/utils.d.ts +9 -0
  63. package/dist/printer/utils.d.ts.map +1 -0
  64. package/dist/printer/utils.js +54 -0
  65. package/dist/printer/utils.js.map +1 -0
  66. package/dist/serial-adapter/index.d.ts +17 -0
  67. package/dist/serial-adapter/index.d.ts.map +1 -0
  68. package/dist/serial-adapter/index.js +172 -0
  69. package/dist/serial-adapter/index.js.map +1 -0
  70. package/dist/usb-adapter/index.d.ts +20 -0
  71. package/dist/usb-adapter/index.d.ts.map +1 -0
  72. package/dist/usb-adapter/index.js +264 -0
  73. package/dist/usb-adapter/index.js.map +1 -0
  74. package/package.json +42 -15
  75. package/CHANGELOG.md +0 -74
  76. package/docs/COVERAGE_ANALYSIS.md +0 -98
  77. package/docs/DEPENDENCIES_REVIEW.md +0 -127
  78. package/docs/JSDOC_REVIEW.md +0 -122
  79. package/docs/LIBRARY_OVERVIEW.md +0 -383
  80. package/docs/PRE_PUBLISH_CHECKLIST.md +0 -331
  81. package/docs/PUBLIC_API_ANALYSIS.md +0 -223
  82. package/docs/README.md +0 -37
  83. package/docs/SERIALPORT_V13_MIGRATION_COMPLETE.md +0 -127
  84. package/docs/TESTS_IMPLEMENTED.md +0 -129
  85. package/docs/USB_V2_REVIEW.md +0 -148
  86. package/docs/VERIFICATION_RESULTS.md +0 -172
  87. package/docs/VERSIONING.md +0 -102
  88. package/examples/printTest.js +0 -59
  89. package/index.js +0 -7
  90. package/jest.config.js +0 -16
  91. package/src/adapter/index.js +0 -75
  92. package/src/printer/commands.js +0 -199
  93. package/src/printer/image.js +0 -159
  94. package/src/printer/index.js +0 -621
  95. package/src/printer/utils.js +0 -58
  96. package/src/serial-adapter/index.js +0 -198
  97. package/src/usb-adapter/index.js +0 -283
  98. package/tests/README.md +0 -67
  99. package/tests/integration/printer-flow.test.js +0 -128
  100. package/tests/unit/adapters/adapter.test.js +0 -49
  101. package/tests/unit/adapters/serial-adapter.test.js +0 -238
  102. package/tests/unit/adapters/usb-adapter.test.js +0 -319
  103. package/tests/unit/image/image.test.js +0 -157
  104. package/tests/unit/printer/buffer.test.js +0 -60
  105. package/tests/unit/printer/commands.test.js +0 -109
  106. package/tests/unit/printer/printer.test.js +0 -405
  107. package/tests/unit/utils/utils.test.js +0 -96
@@ -1,621 +0,0 @@
1
- 'use strict';
2
-
3
- const commands = require('./commands');
4
- const iconv = require('iconv-lite');
5
- const utils = require('./utils');
6
-
7
- const Image = require('./image');
8
-
9
- /**
10
- * This is the Special Buffer, a "Dinamyc" Buffer that will control the data that will be sent to the printer
11
- * @class
12
- * @classdesc This Class will manage a Buffer that can store the data while we send the commands
13
- */
14
- class SpecBuffer {
15
- constructor() {
16
- this.buffer = Buffer.from('', 'hex');
17
- }
18
-
19
- /**
20
- * Write Data on Buffer
21
- * @param {String} data Refers to the Data that will be written on the Buffer
22
- * @param {String} type Refers to the Type of data ('hex','ascii','etc.')
23
- */
24
- write(data, type) {
25
- this.buffer = Buffer.concat([this.buffer, Buffer.from(data, type)]);
26
- }
27
-
28
- /**
29
- * This Command Clean the Special Buffer, and returns the data contained inside the Special Buffer
30
- * @returns {Buffer} The resulting Buffer.
31
- */
32
- flush() {
33
- let data = Buffer.from(this.buffer);
34
- this.buffer = Buffer.from('');
35
- return data;
36
- }
37
- }
38
-
39
- /**
40
- * This is de EscPos Printer Class
41
- * @class
42
- * @classdesc This Class will get the Adapter, manage the buffer, and send the data through the adapter
43
- */
44
- class Printer {
45
- /**
46
- *
47
- * @param {Adapter} adapter Refers to the adapter that will be used by the class
48
- * @param {{encoding:string,width:string}} options Refers to the options that can be used to instantiate the class (Encoding, Size of Columns)
49
- */
50
- constructor(adapter, options) {
51
- // Use adapter directly to maintain consistency - adapter is already an Adapter instance (USB/Serial)
52
- this.adapter = adapter;
53
- this.options = options;
54
- this.buffer = new SpecBuffer();
55
- this.Image = Image;
56
- this.commands = commands;
57
-
58
- this.encoding = options && options.encoding || 'GB18030';
59
- this.width = options && options.width || 48;
60
- }
61
-
62
- /**
63
- * Set the Character Code Table (See the Printer manual for the table)
64
- * @param {Number} codeTable Refers to the codeTable that will be used
65
- * @return {Printer} The escpos printer instance
66
- */
67
- setCharacterCodeTable(codeTable) {
68
- this.buffer.write(Buffer.concat([
69
- commands.ESC,
70
- commands.TAB,
71
- Buffer.from(commands.numToHexString(codeTable), 'hex')
72
- ]));
73
- return this;
74
- }
75
-
76
- /**
77
- * Set the Margin of the Text
78
- * @param {'LEFT'|'RIGHT'|'BOTTOM'} type Refers to the type of the margin
79
- * @param {Number} size Refers to the size of margin that will be set
80
- * @return {Printer} The escpos printer instance
81
- */
82
- margin(type, size) {
83
- let margin = commands.MARGINS.LEFT;
84
-
85
- switch (type.toUpperCase()) {
86
- case 'RIGHT':
87
- margin = commands.MARGINS.RIGHT;
88
- break;
89
- case 'BOTTOM':
90
- margin = commands.MARGINS.BOTTOM;
91
- break;
92
- }
93
- this.buffer.write(
94
- Buffer.concat([
95
- margin,
96
- Buffer.from(commands.numToHexString(size), 'hex')
97
- ])
98
- );
99
- return this;
100
- }
101
-
102
- /**
103
- * Print the text without set the encoding
104
- * @param {String} content Refers to the text that will be printed
105
- * @return {Printer} The escpos printer instance
106
- */
107
- print(content) {
108
- this.buffer.write(content, 'ascii');
109
- return this;
110
- }
111
-
112
- /**
113
- * Print the text without set the encoding with End Of Line
114
- * @param {String} content Refers to the text that will be printed
115
- * @return {Printer} The escpos printer instance
116
- */
117
- println(content) {
118
- this.buffer.write(Buffer.concat([
119
- Buffer.from(content, 'ascii'),
120
- commands.EOL
121
- ]));
122
- return this;
123
- }
124
-
125
- /**
126
- * Send End Of Line command
127
- * @return {Printer} The escpos printer instance
128
- */
129
- newLine() {
130
- this.buffer.write(commands.EOL);
131
- return this;
132
- }
133
-
134
- /**
135
- * Print the text with the defined encoding [optional]
136
- * @param {String} content Refers to the text that will be printed
137
- * @param {String} encoding Refers to the encoding that will be used
138
- * @return {Printer} The escpos printer instance
139
- */
140
- text(content, encoding) {
141
- return this.print(iconv.encode(content, encoding || this.encoding));
142
- }
143
-
144
- /**
145
- * Print the text with the defined encoding [optional] with End Of Line
146
- * @param {String} content Refers to the text that will be printed
147
- * @param {String} encoding Refers to the encoding that will be used
148
- * @return {Printer} The escpos printer instance
149
- */
150
- textln(content, encoding = undefined) {
151
- return this.println(iconv.encode(content, encoding || this.encoding));
152
- }
153
-
154
- /**
155
- * Draw a Line with the desired character
156
- * @param {String} character Refers to the character that will be used
157
- * @return {Printer} The escpos printer instance
158
- */
159
- drawLine(character) {
160
- if (!character)
161
- character = '-';
162
-
163
- for (let i = 0; i < this.width; i++) {
164
- this.buffer.write(Buffer.from(character, 'ascii'));
165
- }
166
-
167
- return this.newLine();
168
- }
169
-
170
- /**
171
- * Set the Encode of the Text (See iconv NPM Library)
172
- * @param {String} encoding Refers to the type of encoding that will be used
173
- * @return {Printer} The escpos printer instance
174
- */
175
- /**
176
- * Set the character encoding for text printing
177
- * @param {String} encoding - Encoding name (e.g., 'GB18030', 'UTF-8', 'ASCII')
178
- * @returns {Printer} The escpos printer instance
179
- */
180
- encode(encoding) {
181
- this.encoding = encoding;
182
- return this;
183
- }
184
-
185
- /**
186
- * Feed the Paper for n times
187
- * @param {Number} n Refers to the number of lines that will be feed
188
- * @return {Printer} The escpos printer instance
189
- */
190
- feed(n) {
191
- this.buffer.write(Buffer.concat(new Array(n || 1).fill(commands.EOL)));
192
- return this;
193
- }
194
-
195
- /**
196
- * Send a Sequence of pre defined feed sequences (See commands under FEED_CONTROL_SEQUENCES)
197
- * @param {String} ctrl Refers to the type of the Alignment
198
- * @return {Printer} The escpos printer instance
199
- */
200
- control(ctrl) {
201
- this.buffer.write(commands.FEED_CONTROL_SEQUENCES['CTL_' + ctrl.toUpperCase()]);
202
- return this;
203
- }
204
-
205
- /**
206
- * Set the Text Alignment
207
- * @param {String} align Refers to the type of the Alignment
208
- * @return {Printer} The escpos printer instance
209
- */
210
- align(align) {
211
- this.buffer.write(commands.TEXT_FORMAT['TXT_ALIGN_' + align.toUpperCase()]);
212
- return this;
213
- }
214
- /**
215
- * Set the Font Family of the Printer
216
- * @param {String} family Refers to the family of the font
217
- * @return {Printer} The escpos printer instance
218
- */
219
- font(family) {
220
- this.buffer.write(commands.TEXT_FORMAT['TXT_FONT_' + family.toUpperCase()]);
221
- if (family.toUpperCase() === 'A')
222
- this.width = this.options && this.options.width || 42;
223
- else
224
- this.width = this.options && this.options.width || 56;
225
- return this;
226
- }
227
-
228
- /**
229
- * Set the Style of the font
230
- * @param {String} type Refers to the type of the font style
231
- * @return {Printer} The escpos printer instance
232
- */
233
- style(type) {
234
- let styled;
235
- switch (type.toUpperCase()) {
236
- case 'B':
237
- styled = Buffer.concat([
238
- commands.TEXT_FORMAT.TXT_BOLD_ON,
239
- commands.TEXT_FORMAT.TXT_ITALIC_OFF,
240
- commands.TEXT_FORMAT.TXT_UNDERL_OFF
241
- ]);
242
- break;
243
- case 'I':
244
- styled = Buffer.concat([
245
- commands.TEXT_FORMAT.TXT_BOLD_OFF,
246
- commands.TEXT_FORMAT.TXT_ITALIC_ON,
247
- commands.TEXT_FORMAT.TXT_UNDERL_OFF
248
- ]);
249
- break;
250
- case 'U':
251
- styled = Buffer.concat([
252
- commands.TEXT_FORMAT.TXT_BOLD_OFF,
253
- commands.TEXT_FORMAT.TXT_ITALIC_OFF,
254
- commands.TEXT_FORMAT.TXT_UNDERL_ON
255
- ]);
256
- break;
257
- case 'U2':
258
- styled = Buffer.concat([
259
- commands.TEXT_FORMAT.TXT_BOLD_OFF,
260
- commands.TEXT_FORMAT.TXT_ITALIC_OFF,
261
- commands.TEXT_FORMAT.TXT_UNDERL2_ON
262
- ]);
263
- break;
264
-
265
- case 'BI':
266
- styled = Buffer.concat([
267
- commands.TEXT_FORMAT.TXT_BOLD_ON,
268
- commands.TEXT_FORMAT.TXT_ITALIC_ON,
269
- commands.TEXT_FORMAT.TXT_UNDERL_OFF
270
- ]);
271
- break;
272
- case 'BIU':
273
- styled = Buffer.concat([
274
- commands.TEXT_FORMAT.TXT_BOLD_ON,
275
- commands.TEXT_FORMAT.TXT_ITALIC_ON,
276
- commands.TEXT_FORMAT.TXT_UNDERL_ON
277
- ]);
278
- break;
279
- case 'BIU2':
280
- styled = Buffer.concat([
281
- commands.TEXT_FORMAT.TXT_BOLD_ON,
282
- commands.TEXT_FORMAT.TXT_ITALIC_ON,
283
- commands.TEXT_FORMAT.TXT_UNDERL2_ON
284
- ]);
285
- break;
286
- case 'BU':
287
- styled = Buffer.concat([
288
- commands.TEXT_FORMAT.TXT_BOLD_ON,
289
- commands.TEXT_FORMAT.TXT_ITALIC_OFF,
290
- commands.TEXT_FORMAT.TXT_UNDERL_ON
291
- ]);
292
- break;
293
- case 'BU2':
294
- styled = Buffer.concat([
295
- commands.TEXT_FORMAT.TXT_BOLD_ON,
296
- commands.TEXT_FORMAT.TXT_ITALIC_OFF,
297
- commands.TEXT_FORMAT.TXT_UNDERL2_ON
298
- ]);
299
- break;
300
- case 'IU':
301
- styled = Buffer.concat([
302
- commands.TEXT_FORMAT.TXT_BOLD_OFF,
303
- commands.TEXT_FORMAT.TXT_ITALIC_ON,
304
- commands.TEXT_FORMAT.TXT_UNDERL_ON
305
- ]);
306
- break;
307
- case 'IU2':
308
- styled = Buffer.concat([
309
- commands.TEXT_FORMAT.TXT_BOLD_OFF,
310
- commands.TEXT_FORMAT.TXT_ITALIC_ON,
311
- commands.TEXT_FORMAT.TXT_UNDERL2_ON
312
- ]);
313
- break;
314
-
315
- case 'NORMAL':
316
- default:
317
- styled = Buffer.concat([
318
- commands.TEXT_FORMAT.TXT_BOLD_OFF,
319
- commands.TEXT_FORMAT.TXT_ITALIC_OFF,
320
- commands.TEXT_FORMAT.TXT_UNDERL_OFF,
321
- ]);
322
- break;
323
- }
324
-
325
- this.buffer.write(styled);
326
- return this;
327
- }
328
-
329
- /**
330
- * Set the Size of the Characters
331
- * @param {Number} width Refers to the width of the character
332
- * @param {Number} height Refers to the height of the character
333
- * @return {Printer} The escpos printer instance
334
- */
335
- size(width, height) {
336
- this.buffer.write(commands.TEXT_FORMAT.TXT_CUSTOM_SIZE(width, height));
337
- return this;
338
- }
339
-
340
- /**
341
- * Set the Character Spacing between them
342
- * @param {Number} n Refers to the space desired between the characters
343
- * @return {Printer} The escpos printer instance
344
- */
345
- spacing(n) {
346
- if (n === undefined || n === null) {
347
- this.buffer.write(commands.CHARACTER_SPACING.CS_DEFAULT);
348
- } else {
349
- this.buffer.write(
350
- Buffer.concat([
351
- commands.CHARACTER_SPACING.CS_SET,
352
- Buffer.from(commands.numToHexString(n), 'hex')
353
- ])
354
- );
355
- }
356
- return this;
357
- }
358
-
359
- /**
360
- * Set the Line Spacing between lines
361
- * @param {Number} n Refers to the space desired between lines
362
- * @return {Printer} The escpos printer instance
363
- */
364
- lineSpace(n) {
365
- if (n === undefined || n === null) {
366
- this.buffer.write(commands.LINE_SPACING.LS_DEFAULT);
367
- } else {
368
- this.buffer.write(
369
- Buffer.concat([
370
- commands.LINE_SPACING.LS_SET,
371
- Buffer.from(commands.numToHexString(n), 'hex')
372
- ])
373
- );
374
- }
375
- return this;
376
- }
377
-
378
- /**
379
- * Send Hardware Commands to the Printer
380
- * @param {String} hw Refers to the type of command that will be sent to printer (See commands under HW section)
381
- * @return {Printer} The escpos printer instance
382
- */
383
- hardware(hw) {
384
- this.buffer.write(commands.HARDWARE['HW_' + hw.toUpperCase()]);
385
- return this;
386
- }
387
-
388
- /**
389
- * Print barcode
390
- * @param {String} code Refers to the code that will be converted to barcode
391
- * @param {String} type Refers to the type of the barcode
392
- * @param {{width:String,height:String,position:String,font:String,includeParity:Boolean}} options Refers to the options for generating the barcode
393
- * @return {Printer} The escpos printer instance
394
- */
395
- barcode(code, type, options) {
396
- options = options || {};
397
- let width = options.width;
398
- let height = options.height;
399
- let position = options.position;
400
- let font = options.font;
401
- let includeParity = options.includeParity !== false; // true by default
402
-
403
- type = type || 'EAN13'; // default type is EAN13, may a good choice ?
404
- let convertCode = String(code);
405
- let parityBit = '';
406
- let codeLength = '';
407
-
408
- if (type === 'EAN13' && convertCode.length !== 12) {
409
- throw new Error('EAN13 Barcode type requires code length 12');
410
- } else if (type === 'EAN8' && convertCode.length !== 7) {
411
- throw new Error('EAN8 Barcode type requires code length 7');
412
- }
413
-
414
- if (width >= 1 && width <= 5) {
415
- this.buffer.write(commands.BARCODE_FORMAT.BARCODE_WIDTH[width]);
416
- } else {
417
- this.buffer.write(commands.BARCODE_FORMAT.BARCODE_WIDTH_DEFAULT);
418
- }
419
-
420
- if (height >= 1 && height <= 255) {
421
- this.buffer.write(commands.BARCODE_FORMAT.BARCODE_HEIGHT(height));
422
- } else {
423
- this.buffer.write(commands.BARCODE_FORMAT.BARCODE_HEIGHT_DEFAULT);
424
- }
425
-
426
- this.buffer.write(commands.BARCODE_FORMAT['BARCODE_FONT_' + (font || 'A').toUpperCase()]);
427
-
428
- this.buffer.write(commands.BARCODE_FORMAT['BARCODE_TXT_' + (position || 'BLW').toUpperCase()]);
429
-
430
- this.buffer.write(commands.BARCODE_FORMAT['BARCODE_' + ((type || 'EAN13').replace('-', '_').toUpperCase())]);
431
-
432
- if (includeParity) {
433
- if (type === 'EAN13' || type === 'EAN8') {
434
- parityBit = utils.getParityBit(code);
435
- }
436
- }
437
- if (type == 'CODE128' || type == 'CODE93') {
438
- codeLength = utils.codeLength(code);
439
- }
440
- this.buffer.write(Buffer.concat([
441
- Buffer.from(codeLength + code + (includeParity ? parityBit : ''), 'ascii'),
442
- Buffer.from('00', 'hex')
443
- ]));
444
-
445
- return this;
446
- }
447
-
448
- /**
449
- * Prepare the Image to be printed by bitmap mode
450
- * @param {Image} image Refers to the Image object to be printed
451
- * @param {String} density Refers to the density of the image to be printed
452
- * @return {Printer} The escpos printer instance
453
- */
454
- image(image, density) {
455
- if (!(image instanceof Image))
456
- throw new TypeError('Only Image object supported');
457
- density = density || 'd24';
458
- let n = !!~['d8', 's8'].indexOf(density) ? 1 : 3;
459
- let header = commands.BITMAP_FORMAT['BITMAP_' + density.toUpperCase()];
460
- let bitmap = image.toBitmap(n * 8);
461
-
462
- bitmap.data.forEach((line) => {
463
- let lineLength = Buffer.allocUnsafe(2);
464
- lineLength.writeUInt16LE(line.length / n, 0);
465
-
466
- this.buffer.write(Buffer.concat([
467
- header,
468
- lineLength,
469
- Buffer.from(line),
470
- commands.ESC,
471
- commands.FEED_CONTROL_SEQUENCES.CTL_GLF
472
- ]));
473
- });
474
-
475
- return this;
476
- };
477
-
478
- /**
479
- * Prepare the Image to be printed by raster mode
480
- * @param {Image} image Refers to the Image object to be printed
481
- * @param {String} mode Refers to the type of mode will be sent to raster format
482
- * @return {Printer} The escpos printer instance
483
- */
484
- raster(image, mode) {
485
- if (!(image instanceof Image))
486
- throw new TypeError('Only Image object supported');
487
-
488
- mode = mode || 'normal';
489
- if (mode === 'dhdw' || mode === 'dwh' || mode === 'dhw')
490
- mode = 'dwdh';
491
-
492
-
493
- let raster = image.toRaster();
494
- let header = commands.GSV0_FORMAT['GSV0_' + mode.toUpperCase()];
495
-
496
- let width = Buffer.allocUnsafe(2);
497
- width.writeUInt16LE(raster.width);
498
- let height = Buffer.allocUnsafe(2);
499
- height.writeUInt16LE(raster.height);
500
-
501
- this.buffer.write(Buffer.concat([
502
- header,
503
- width,
504
- height,
505
- Buffer.from(raster.data)
506
- ]));
507
-
508
- return this;
509
- }
510
- /**
511
- * Send pulse to open de cash drawer
512
- * @param {Number} pin Refers to pin where the pulse will be sent
513
- * @return {Printer} The escpos printer instance
514
- */
515
- cashdraw(pin) {
516
- this.buffer.write(commands.CASH_DRAWER['CD_KICK_' + (pin || 2)]);
517
- return this;
518
- }
519
-
520
- /**
521
- * Printer Buzzer (Beep sound)
522
- * @param {Number} n Refers to the number of buzzer times
523
- * @param {Number} t Refers to the buzzer sound length in (t * 100) milliseconds.
524
- * @return {Printer} The escpos printer instance
525
- */
526
- beep(n, t) {
527
- let nB = Buffer.allocUnsafe(1);
528
- let tB = Buffer.allocUnsafe(1);
529
-
530
- nB.writeUInt8(n, 0);
531
- tB.writeUInt8(t, 0);
532
-
533
- this.buffer.write(Buffer.concat([
534
- commands.BEEP,
535
- nB,
536
- tB
537
- ]));
538
-
539
- return this;
540
- }
541
-
542
- /**
543
- * Cut the Paper
544
- * @param {Boolean} part Refers to the type of the cut (partial or full)
545
- * @param {Number} t Refers to the number of lines to be added before the cut
546
- * @return {Printer} The escpos printer instance
547
- */
548
- cut(part, feed) {
549
- this.feed(feed || 3);
550
- this.buffer.write(commands.PAPER[part ? 'PAPER_PART_CUT' : 'PAPER_FULL_CUT']);
551
- return this;
552
- };
553
-
554
- /**
555
- * Send data to hardware using the adapter and flush buffer
556
- * @return {Promise<Printer>} Promise containing the escpos printer instance
557
- */
558
- async flush() {
559
- var buf = this.buffer.flush();
560
- if (buf.length > 0) {
561
- await this.adapter.write(buf);
562
- }
563
- return this;
564
- }
565
-
566
- /**
567
- * Close the connection to the printer using the adapter
568
- * @return {Promise<Printer>} Promise containing the escpos printer instance
569
- */
570
- async close(options) {
571
- await this.flush();
572
- await this.adapter.close(options);
573
- return this;
574
- };
575
-
576
- /**
577
- * color select between two print color modes, if your printer supports it
578
- * @param {Number} color - 0 for primary color (black) 1 for secondary color (red)
579
- * @return {Printer} The escpos printer instance
580
- */
581
- color(color) {
582
- this.buffer.write(commands.COLOR[color === 0 || color === 1 ? color : 0]);
583
- return this;
584
- };
585
-
586
- /**
587
- * reverse colors, if your printer supports it
588
- * @param {Boolean} bool - True for reverse, false otherwise
589
- * @return {Printer} The escpos printer instance
590
- */
591
- setReverseColors(bool) {
592
- this.buffer.write(bool ? commands.COLOR.REVERSE : commands.COLOR.UNREVERSE);
593
- return this;
594
- };
595
-
596
-
597
- /**
598
- * writes a low level command to the printer buffer
599
- *
600
- * @usage
601
- * 1) raw('1d:77:06:1d:6b:02:32:32:30:30:30:30:32:30:30:30:35:30:35:00:0a')
602
- * 2) raw('1d 77 06 1d 6b 02 32 32 30 30 30 30 32 30 30 30 35 30 35 00 0a')
603
- * 3) raw(Buffer.from('1d77061d6b0232323030303032303030353035000a','hex'))
604
- *
605
- * @param {Buffer|string} data Refers to the RAW data or Buffer that will be sent to the printer
606
- * @return {Printer} The escpos printer instance
607
- */
608
- raw(data) {
609
- if (Buffer.isBuffer(data)) {
610
- this.buffer.write(data);
611
- } else if (typeof data === 'string') {
612
- data = data.toLowerCase();
613
- this.buffer.write(Buffer.from(data.replace(/(\s|:)/g, ''), 'hex'));
614
- }else {
615
- throw new Error('Data is Invalid!')
616
- }
617
- return this;
618
- }
619
- }
620
-
621
- module.exports = Printer;
@@ -1,58 +0,0 @@
1
- 'use strict';
2
- /**
3
- * Get character length (1 or 2 bytes for multi-byte characters)
4
- * @param {String} char - Single character to measure
5
- * @returns {Number} 1 for single-byte, 2 for multi-byte characters
6
- */
7
- function charLength(char) {
8
- const code = char.charCodeAt(0);
9
- return code > 0x7f && code <= 0xffff ? 2 : 1; // More than 2bytes count as 2
10
- }
11
- /**
12
- * Calculate parity bit for a string (used in barcode generation)
13
- * @param {String} str - String to calculate parity for
14
- * @returns {String} Parity bit as string
15
- */
16
- exports.getParityBit = function (str) {
17
- var parity = 0, reversedCode = str.split('').reverse().join('');
18
- for (var counter = 0; counter < reversedCode.length; counter += 1) {
19
- parity += parseInt(reversedCode.charAt(counter), 10) * Math.pow(3, ((counter + 1) % 2));
20
- }
21
- return String((10 - (parity % 10)) % 10);
22
- };
23
-
24
- /**
25
- * Get code length as hex string
26
- * @param {String} str - String to get length for
27
- * @returns {String} Length as hex string
28
- */
29
- exports.codeLength = function (str) {
30
- let buff = Buffer.from((str.length).toString(16), 'hex');
31
- return buff.toString();
32
- }
33
-
34
- /**
35
- * Get text length accounting for multi-byte characters
36
- * @param {String} str - String to measure
37
- * @returns {Number} Text length (multi-byte characters count as 2)
38
- */
39
- exports.textLength = function (str) {
40
- return str.split('').reduce((accLen, char) => {
41
- return accLen + charLength(char);
42
- }, 0)
43
- }
44
-
45
- /**
46
- * Get text substring accounting for multi-byte characters
47
- * @param {String} str - String to extract from
48
- * @param {Number} start - Start position (in character units, not bytes)
49
- * @param {Number} [end] - End position (in character units, not bytes)
50
- * @returns {String} Resulting substring
51
- */
52
- exports.textSubstring = function (str, start, end) {
53
- let accLen = 0;
54
- return str.split('').reduce((accStr, char) => {
55
- accLen = accLen + charLength(char);
56
- return accStr + (accLen > start && (!end || accLen <= end) ? char : '');
57
- }, '')
58
- }