@pisell/utils 1.0.16 → 1.0.18

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.
@@ -0,0 +1,538 @@
1
+ // Maximum columns for printing in columns function
2
+ export var MAX_COLUMNS = 6;
3
+
4
+ // Left alignment
5
+ export var ALIGN_LEFT = 0;
6
+ // Center alignment
7
+ export var ALIGN_CENTER = 1;
8
+ // Right alignment
9
+ export var ALIGN_RIGHT = 2;
10
+
11
+ // HRI above the barcode
12
+ export var HRI_POS_ABOVE = 1;
13
+ // HRI below the barcode
14
+ export var HRI_POS_BELOW = 2;
15
+ var EscPosPrinter = function EscPosPrinter(dotsPerLine) {
16
+ if (dotsPerLine != 384 && dotsPerLine != 576)
17
+ // Print width in dots. 384 for 58mm and 576 for 80mm
18
+ dotsPerLine = 384;
19
+ this.widthOfColumns = new Array(MAX_COLUMNS);
20
+ this.dotsPerLine = dotsPerLine;
21
+ this.charHSize = 1;
22
+ this.orderData = "";
23
+ };
24
+ function _numToHexStr(n, bytes) {
25
+ var str = "";
26
+ var v;
27
+ for (var i = 0; i < bytes; i++) {
28
+ v = n & 0xff;
29
+ if (v < 0x10) str += "0" + v.toString(16);else str += v.toString(16);
30
+ n >>= 8;
31
+ }
32
+ return str;
33
+ }
34
+ function _unicodeToUtf8(unicode) {
35
+ var c1, c2, c3, c4;
36
+ if (unicode < 0) return "";
37
+ if (unicode <= 0x7f) {
38
+ c1 = unicode & 0x7f;
39
+ return _numToHexStr(c1, 1);
40
+ }
41
+ if (unicode <= 0x7ff) {
42
+ c1 = unicode >> 6 & 0x1f | 0xc0;
43
+ c2 = unicode & 0x3f | 0x80;
44
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1);
45
+ }
46
+ if (unicode <= 0xffff) {
47
+ c1 = unicode >> 12 & 0x0f | 0xe0;
48
+ c2 = unicode >> 6 & 0x3f | 0x80;
49
+ c3 = unicode & 0x3f | 0x80;
50
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1);
51
+ }
52
+ if (unicode <= 0x10ffff) {
53
+ c1 = unicode >> 18 & 0x07 | 0xf0;
54
+ c2 = unicode >> 12 & 0x3f | 0x80;
55
+ c3 = unicode >> 6 & 0x3f | 0x80;
56
+ c4 = unicode & 0x3f | 0x80;
57
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1) + _numToHexStr(c4, 1);
58
+ }
59
+ return "";
60
+ }
61
+
62
+ // Clear the generated order data.
63
+ EscPosPrinter.prototype.clear = function () {
64
+ this.orderData = "";
65
+ };
66
+
67
+ // Print the order data.
68
+ EscPosPrinter.prototype.print = function (host, sn, copies) {
69
+ var that = this;
70
+ return new Promise(function (resolve, reject) {
71
+ var xmlobj = new XMLHttpRequest();
72
+ xmlobj.timeout = 10000;
73
+ var url = "https://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
74
+ xmlobj.open("POST", url, true);
75
+ xmlobj.onreadystatechange = function () {
76
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
77
+ if (xmlobj.status.toString(10) === "200") {
78
+ var re = /task_id: (\d+)/;
79
+ var matches = re.exec(xmlobj.responseText);
80
+ resolve(matches[1] || "");
81
+ } else {
82
+ reject(xmlobj.responseText);
83
+ }
84
+ }
85
+ };
86
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
87
+ xmlobj.send(that.orderData);
88
+ });
89
+ };
90
+
91
+ // Query print status.
92
+ EscPosPrinter.prototype.queryStatus = function (host, sn, task_id) {
93
+ return new Promise(function (resolve, reject) {
94
+ var xmlobj = new XMLHttpRequest();
95
+ xmlobj.timeout = 10000;
96
+ var url = "https://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
97
+ xmlobj.open("POST", url, true);
98
+ xmlobj.onreadystatechange = function () {
99
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
100
+ if (xmlobj.status.toString(10) === "200") {
101
+ resolve(xmlobj.responseText);
102
+ } else {
103
+ reject(xmlobj.responseText);
104
+ }
105
+ }
106
+ };
107
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
108
+ xmlobj.send();
109
+ });
110
+ };
111
+
112
+ //////////////////////////////////////////////////
113
+ // Basic ESC/POS Commands
114
+ //////////////////////////////////////////////////
115
+
116
+ // Append text in the order.
117
+ EscPosPrinter.prototype.appendText = function (str) {
118
+ for (var i = 0; i < str.length; i++) this.orderData += _unicodeToUtf8(str.charCodeAt(i));
119
+ };
120
+
121
+ // [LF] Print the contents in the buffer and feed n lines.
122
+ EscPosPrinter.prototype.lineFeed = function (n) {
123
+ for (var i = 0; i < n; i++) this.orderData += "0a";
124
+ };
125
+
126
+ // [ESC @] Restore default settings (line spacing, print modes, etc).
127
+ EscPosPrinter.prototype.restoreDefaultSettings = function () {
128
+ this.charHSize = 1;
129
+ this.orderData += "1b40";
130
+ };
131
+
132
+ // [ESC 2] Restore default line spacing.
133
+ EscPosPrinter.prototype.restoreDefaultLineSpacing = function () {
134
+ this.orderData += "1b32";
135
+ };
136
+
137
+ // [ESC 3] Set line spacing.
138
+ EscPosPrinter.prototype.setLineSpacing = function (n) {
139
+ if (n >= 0 && n <= 255) this.orderData += "1b33" + _numToHexStr(n, 1);
140
+ };
141
+
142
+ // [ESC !] Select print modes (double width/height or not, bold or not).
143
+ EscPosPrinter.prototype.setPrintModes = function (bold, double_h, double_w) {
144
+ var n = 0;
145
+ if (bold) n |= 8;
146
+ if (double_h) n |= 16;
147
+ if (double_w) n |= 32;
148
+ this.charHSize = double_w ? 2 : 1;
149
+ this.orderData += "1b21" + _numToHexStr(n, 1);
150
+ };
151
+
152
+ // [GS !] Set character size (1~8 times of normal width or height).
153
+ EscPosPrinter.prototype.setCharacterSize = function (h, w) {
154
+ var n = 0;
155
+ if (h >= 1 && h <= 8) n |= h - 1;
156
+ if (w >= 1 && w <= 8) {
157
+ n |= w - 1 << 4;
158
+ this.charHSize = w;
159
+ }
160
+ this.orderData += "1d21" + _numToHexStr(n, 1);
161
+ };
162
+
163
+ // [HT] Jump to the next n TAB positions.
164
+ EscPosPrinter.prototype.horizontalTab = function (n) {
165
+ for (var i = 0; i < n; i++) this.orderData += "09";
166
+ };
167
+
168
+ // [ESC $] Move to horizontal absolute position.
169
+ EscPosPrinter.prototype.setAbsolutePrintPosition = function (n) {
170
+ if (n >= 0 && n <= 65535) this.orderData += "1b24" + _numToHexStr(n, 2);
171
+ };
172
+
173
+ // [ESC \] Move to horizontal relative position.
174
+ EscPosPrinter.prototype.setRelativePrintPosition = function (n) {
175
+ if (n >= -32768 && n <= 32767) this.orderData += "1b5c" + _numToHexStr(n, 2);
176
+ };
177
+
178
+ // [ESC a] Set alignment.
179
+ EscPosPrinter.prototype.setAlignment = function (n) {
180
+ if (n >= 0 && n <= 2) this.orderData += "1b61" + _numToHexStr(n, 1);
181
+ };
182
+
183
+ // [GS V m] Cut paper.
184
+ EscPosPrinter.prototype.cutPaper = function (fullCut) {
185
+ this.orderData += "1d56" + (fullCut ? "30" : "31");
186
+ };
187
+
188
+ // [GS V m n] Postponed cut paper.
189
+ // After sending this command, the cutter will not cut until (76+n) dot lines are fed.
190
+ EscPosPrinter.prototype.postponedCutPaper = function (fullCut, n) {
191
+ if (n >= 0 && n <= 255) this.orderData += "1d56" + (fullCut ? "61" : "62") + _numToHexStr(n, 1);
192
+ };
193
+
194
+ //////////////////////////////////////////////////
195
+ // Print in Columns
196
+ //////////////////////////////////////////////////
197
+
198
+ // Return the width of a character.
199
+ function _widthOfChar(c) {
200
+ if (c >= 0x00020 && c <= 0x0036f || c >= 0x0ff61 && c <= 0x0ff9f) return 12;
201
+ if (c == 0x02010 || c >= 0x02013 && c <= 0x02016 || c >= 0x02018 && c <= 0x02019 || c >= 0x0201c && c <= 0x0201d || c >= 0x02025 && c <= 0x02026 || c >= 0x02030 && c <= 0x02033 || c == 0x02035 || c == 0x0203b) return 24;
202
+ if (c >= 0x01100 && c <= 0x011ff || c >= 0x02460 && c <= 0x024ff || c >= 0x025a0 && c <= 0x027bf || c >= 0x02e80 && c <= 0x02fdf || c >= 0x03000 && c <= 0x0318f || c >= 0x031a0 && c <= 0x031ef || c >= 0x03200 && c <= 0x09fff || c >= 0x0ac00 && c <= 0x0d7ff || c >= 0x0f900 && c <= 0x0faff || c >= 0x0fe30 && c <= 0x0fe4f || c >= 0x1f000 && c <= 0x1f9ff) return 24;
203
+ if (c >= 0x0ff01 && c <= 0x0ff5e || c >= 0x0ffe0 && c <= 0x0ffe5) return 24;
204
+ return 0;
205
+ }
206
+ export function columnWidthWithAlignment(width, alignment) {
207
+ return width & 0xffff | (alignment & 3) << 16;
208
+ }
209
+ function _columnWidth(v) {
210
+ return v & 0xffff;
211
+ }
212
+ function _columnAlignment(v) {
213
+ return v >> 16 & 3;
214
+ }
215
+
216
+ // Set the width and alignment mode for each columns.
217
+ EscPosPrinter.prototype.setColumnWidths = function /*...*/
218
+ () {
219
+ var i, remain, width, alignment;
220
+ if (arguments.length == 0) return;
221
+ for (i = 0; i < MAX_COLUMNS; i++) this.widthOfColumns[i] = 0;
222
+ remain = this.dotsPerLine; // Dots not used
223
+ for (i = 0; i < arguments.length; i++) {
224
+ if (i == MAX_COLUMNS)
225
+ // Maximum columns exceeded
226
+ return;
227
+ width = _columnWidth(arguments[i]);
228
+ alignment = _columnAlignment(arguments[i]);
229
+ if (width == 0 || width > remain) {
230
+ // Use all free dots for the last column
231
+ this.widthOfColumns[i] = columnWidthWithAlignment(remain, alignment);
232
+ return;
233
+ }
234
+ this.widthOfColumns[i] = arguments[i];
235
+ remain -= width;
236
+ }
237
+ };
238
+
239
+ // Print in columns with the current column settings.
240
+ EscPosPrinter.prototype.printInColumns = function /*...*/
241
+ () {
242
+ var strcurr = new Array(MAX_COLUMNS);
243
+ var strrem = new Array(MAX_COLUMNS);
244
+ var strwidth = new Array(MAX_COLUMNS);
245
+ var i, j, c, w, columns, width, alignment, pos;
246
+ var done;
247
+ if (arguments.length == 0) return;
248
+ columns = 0;
249
+ for (i = 0; i < arguments.length; i++) {
250
+ if (i == MAX_COLUMNS || this.widthOfColumns[i] == 0) break;
251
+ strcurr[i] = "";
252
+ strrem[i] = arguments[i];
253
+ columns++;
254
+ }
255
+ do {
256
+ done = true;
257
+ pos = 0;
258
+ for (i = 0; i < columns; i++) {
259
+ width = _columnWidth(this.widthOfColumns[i]);
260
+ if (strrem[i].length == 0) {
261
+ pos += width;
262
+ continue;
263
+ }
264
+ done = false;
265
+ strcurr[i] = "";
266
+ strwidth[i] = 0;
267
+ for (j = 0; j < strrem[i].length; j++) {
268
+ c = strrem[i].charCodeAt(j);
269
+ if (c == 0x0a) {
270
+ j++; // Drop the '\n'
271
+ break;
272
+ } else {
273
+ w = _widthOfChar(c);
274
+ if (w == 0) {
275
+ c = "?";
276
+ w = 12;
277
+ }
278
+ w *= this.charHSize;
279
+ if (strwidth[i] + w > width) {
280
+ break;
281
+ } else {
282
+ strcurr[i] += String.fromCharCode(c);
283
+ strwidth[i] += w;
284
+ }
285
+ }
286
+ }
287
+ if (j < strrem[i].length) strrem[i] = strrem[i].substring(j);else strrem[i] = "";
288
+ alignment = _columnAlignment(this.widthOfColumns[i]);
289
+ switch (alignment) {
290
+ case ALIGN_CENTER:
291
+ this.setAbsolutePrintPosition(pos + (width - strwidth[i]) / 2);
292
+ break;
293
+ case ALIGN_RIGHT:
294
+ this.setAbsolutePrintPosition(pos + (width - strwidth[i]));
295
+ break;
296
+ default:
297
+ this.setAbsolutePrintPosition(pos);
298
+ break;
299
+ }
300
+ this.appendText(strcurr[i]);
301
+ pos += width;
302
+ }
303
+ if (!done) this.lineFeed(1);
304
+ } while (!done);
305
+ };
306
+
307
+ //////////////////////////////////////////////////
308
+ // Barcode / QR Code Printing
309
+ //////////////////////////////////////////////////
310
+
311
+ // Append barcode in the order.
312
+ EscPosPrinter.prototype.appendBarcode = function (hri_pos, height, module_size, barcode_type, text) {
313
+ var text_length = text.length;
314
+ if (text_length == 0) return;
315
+ if (text_length > 255) text_length = 255;
316
+ if (height < 1) height = 1;else if (height > 255) height = 255;
317
+ if (module_size < 1) module_size = 1;else if (module_size > 6) module_size = 6;
318
+ this.orderData += "1d48" + _numToHexStr(hri_pos & 3, 1);
319
+ this.orderData += "1d6600";
320
+ this.orderData += "1d68" + _numToHexStr(height, 1);
321
+ this.orderData += "1d77" + _numToHexStr(module_size, 1);
322
+ this.orderData += "1d6b" + _numToHexStr(barcode_type, 1) + _numToHexStr(text_length, 1);
323
+ for (var i = 0; i < text_length; i++) this.orderData += _numToHexStr(text.charCodeAt(i), 1);
324
+ };
325
+
326
+ // Append QR code in the order.
327
+ EscPosPrinter.prototype.appendQRcode = function (module_size, ec_level, text) {
328
+ var text_length = text.length;
329
+ if (text_length == 0) return;
330
+ if (text_length > 65535) text_length = 65535;
331
+ if (module_size < 1) module_size = 1;else if (module_size > 16) module_size = 16;
332
+ if (ec_level < 0) ec_level = 0;else if (ec_level > 3) ec_level = 3;
333
+ this.orderData += "1d286b040031410000";
334
+ this.orderData += "1d286b03003143" + _numToHexStr(module_size, 1);
335
+ this.orderData += "1d286b03003145" + _numToHexStr(ec_level + 48, 1);
336
+ this.orderData += "1d286b" + _numToHexStr(text_length + 3, 2) + "315030";
337
+ for (var i = 0; i < text_length; i++) this.orderData += _numToHexStr(text.charCodeAt(i), 1);
338
+ this.orderData += "1d286b0300315130";
339
+ };
340
+
341
+ //////////////////////////////////////////////////
342
+ // Image Printing
343
+ //////////////////////////////////////////////////
344
+
345
+ // Grayscale to mono - diffuse dithering.
346
+ function _diffuseDither(src_data, width, height) {
347
+ if (width <= 0 || height <= 0) return null;
348
+ if (src_data.length < width * height) return null;
349
+ var bmwidth = width + 7 >> 3;
350
+ var dst_data = new Array(bmwidth * height);
351
+ var line_buffer = new Array(2 * width);
352
+ var i, p, q, x, y, mask;
353
+ var line1, line2, b1, b2, tmp;
354
+ var err, e1, e3, e5, e7;
355
+ var not_last_line;
356
+ line1 = 0;
357
+ line2 = 1;
358
+ for (i = 0; i < width; i++) {
359
+ line_buffer[i] = 0;
360
+ line_buffer[width + i] = src_data[i];
361
+ }
362
+ for (y = 0; y < height; y++) {
363
+ tmp = line1;
364
+ line1 = line2;
365
+ line2 = tmp;
366
+ not_last_line = y < height - 1;
367
+ if (not_last_line) {
368
+ p = (y + 1) * width;
369
+ for (i = 0; i < width; i++) {
370
+ line_buffer[line2 * width + i] = src_data[p];
371
+ p++;
372
+ }
373
+ }
374
+ q = y * bmwidth;
375
+ for (i = 0; i < bmwidth; i++) {
376
+ dst_data[q] = 0;
377
+ q++;
378
+ }
379
+ b1 = 0;
380
+ b2 = 0;
381
+ q = y * bmwidth;
382
+ mask = 0x80;
383
+ for (x = 1; x <= width; x++) {
384
+ var idx = line1 * width + b1;
385
+ if (line_buffer[idx] < 128) {
386
+ // Black pixel
387
+ err = line_buffer[idx];
388
+ dst_data[q] |= mask;
389
+ } else {
390
+ err = line_buffer[idx] - 255;
391
+ }
392
+ b1++;
393
+ if (mask == 1) {
394
+ q++;
395
+ mask = 0x80;
396
+ } else {
397
+ mask >>= 1;
398
+ }
399
+ e7 = err * 7 + 8 >> 4;
400
+ e5 = err * 5 + 8 >> 4;
401
+ e3 = err * 3 + 8 >> 4;
402
+ e1 = err - (e7 + e5 + e3);
403
+ if (x < width) line_buffer[line1 * width + b1] += e7;
404
+ if (not_last_line) {
405
+ line_buffer[line2 * width + b2] += e5;
406
+ if (x > 1) line_buffer[line2 * width + b2 - 1] += e3;
407
+ if (x < width) line_buffer[line2 * width + b2 + 1] += e1;
408
+ }
409
+ b2++;
410
+ }
411
+ }
412
+ return dst_data;
413
+ }
414
+
415
+ // Grayscale to mono - threshold dithering.
416
+ function _thresholdDither(src_data, width, height) {
417
+ if (width <= 0 || height <= 0) return null;
418
+ if (src_data.length < width * height) return null;
419
+ var bmwidth = width + 7 >> 3;
420
+ var dst_data = new Array(bmwidth * height);
421
+ var p, q, k, x, y, mask;
422
+ p = 0;
423
+ q = 0;
424
+ for (y = 0; y < height; y++) {
425
+ k = q;
426
+ mask = 0x80;
427
+ for (x = 0; x < width; x++) {
428
+ if (src_data[p] < 128) {
429
+ // Black pixel
430
+ dst_data[k] |= mask;
431
+ }
432
+ p++;
433
+ if (mask == 1) {
434
+ k++;
435
+ mask = 0x80;
436
+ } else {
437
+ mask >>= 1;
438
+ }
439
+ }
440
+ q += bmwidth;
441
+ }
442
+ return dst_data;
443
+ }
444
+
445
+ // RGB to grayscale.
446
+ function _convertToGray(imgdata, width, height) {
447
+ var gray_data = new Array(width * height);
448
+ var i = 0,
449
+ j = 0,
450
+ x,
451
+ y,
452
+ r,
453
+ g,
454
+ b;
455
+ for (y = 0; y < height; y++) {
456
+ for (x = 0; x < width; x++) {
457
+ r = imgdata.data[j++] & 0xff;
458
+ g = imgdata.data[j++] & 0xff;
459
+ b = imgdata.data[j++] & 0xff;
460
+ j++; // Skip the Alpha channel
461
+ gray_data[i++] = r * 11 + g * 16 + b * 5 >> 5 & 0xff;
462
+ }
463
+ }
464
+ return gray_data;
465
+ }
466
+
467
+ // Append image in the order.
468
+ EscPosPrinter.prototype.appendImage = function (imgdata, dither) {
469
+ var gray_data, mono_data;
470
+ var w = imgdata.width;
471
+ var h = imgdata.height;
472
+ gray_data = _convertToGray(imgdata, w, h);
473
+ if (dither == "diffuse") mono_data = _diffuseDither(gray_data, w, h);
474
+ /* if (dither == "threshold") */else mono_data = _thresholdDither(gray_data, w, h);
475
+ w = w + 7 >> 3;
476
+ this.orderData += "1d763000";
477
+ this.orderData += _numToHexStr(w, 2);
478
+ this.orderData += _numToHexStr(h, 2);
479
+ for (var i = 0; i < mono_data.length; i++) this.orderData += _numToHexStr(mono_data[i] & 0xff, 1);
480
+ };
481
+
482
+ //////////////////////////////////////////////////
483
+ // Page Mode Commands
484
+ //////////////////////////////////////////////////
485
+
486
+ // Enter page mode.
487
+ EscPosPrinter.prototype.enterPageMode = function () {
488
+ this.orderData += "1b4c";
489
+ };
490
+
491
+ // Set print area in page mode.
492
+ // x, y: origin of the print area (the left-top corner of the print area)
493
+ // w, h: width and height of the print area
494
+ EscPosPrinter.prototype.setPrintAreaInPageMode = function (x, y, w, h) {
495
+ this.orderData += "1b57";
496
+ this.orderData += _numToHexStr(x, 2);
497
+ this.orderData += _numToHexStr(y, 2);
498
+ this.orderData += _numToHexStr(w, 2);
499
+ this.orderData += _numToHexStr(h, 2);
500
+ };
501
+
502
+ // Set print direction in page mode.
503
+ // dir: 0:not rotated; 1:90-degree clockwise rotated;
504
+ // 2:180-degree clockwise rotated; 3:270-degree clockwise rotated
505
+ EscPosPrinter.prototype.setPrintDirectionInPageMode = function (dir) {
506
+ if (dir >= 0 && dir <= 3) this.orderData += "1b54" + _numToHexStr(dir, 1);
507
+ };
508
+
509
+ // Set absolute print position in page mode.
510
+ EscPosPrinter.prototype.setAbsolutePrintPositionInPageMode = function (n) {
511
+ if (n >= 0 && n <= 65535) this.orderData += "1d24" + _numToHexStr(n, 2);
512
+ };
513
+
514
+ // Set relative print position in page mode.
515
+ EscPosPrinter.prototype.setRelativePrintPositionInPageMode = function (n) {
516
+ if (n >= -32768 && n <= 32767) this.orderData += "1d5c" + _numToHexStr(n, 2);
517
+ };
518
+
519
+ // Print contents in the buffer and exit page mode.
520
+ EscPosPrinter.prototype.printAndExitPageMode = function () {
521
+ this.orderData += "0c";
522
+ };
523
+
524
+ // Print contents in the buffer and keep in page mode.
525
+ EscPosPrinter.prototype.printInPageMode = function () {
526
+ this.orderData += "1b0c";
527
+ };
528
+
529
+ // Clear contents in the buffer and keep in page mode.
530
+ EscPosPrinter.prototype.clearInPageMode = function () {
531
+ this.orderData += "18";
532
+ };
533
+
534
+ // Discard contents in the buffer and exit page mode.
535
+ EscPosPrinter.prototype.exitPageMode = function () {
536
+ this.orderData += "1b53";
537
+ };
538
+ export default EscPosPrinter;
@@ -0,0 +1,9 @@
1
+ export declare const EscPosPrinter = any;
2
+ export declare const ALIGN_LEFT = number;
3
+ export declare const ALIGN_CENTER = number;
4
+ export declare const ALIGN_RIGHT = number;
5
+ export declare const columnWidthWithAlignment = (
6
+ width: number,
7
+ alignment: number
8
+ ) => any;
9
+ export declare const preparing = any;
@@ -0,0 +1,54 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
+ import EscPosPrinter, { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, columnWidthWithAlignment } from "./EscPosPrinter";
6
+ var dividing = function dividing(printer) {
7
+ printer.setAlignment(ALIGN_LEFT);
8
+ printer.appendText("------------------------------------------------\n");
9
+ };
10
+ var preparing = /*#__PURE__*/function () {
11
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ip, sn, params) {
12
+ var printer, title, result;
13
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
14
+ while (1) switch (_context.prev = _context.next) {
15
+ case 0:
16
+ printer = new EscPosPrinter(576);
17
+ title = params.title;
18
+ printer.clear();
19
+ printer.lineFeed(1);
20
+ printer.setPrintModes(false, false, false);
21
+ printer.setAlignment(ALIGN_CENTER);
22
+ printer.appendText("打印测试\n");
23
+ dividing(printer);
24
+ printer.setAlignment(ALIGN_LEFT);
25
+ printer.appendText("Order time: 2023-10-19 14:15:50\n");
26
+ printer.appendText("Order number\n");
27
+ printer.appendText("D3012");
28
+ dividing(printer);
29
+ printer.setColumnWidths(columnWidthWithAlignment(150, ALIGN_LEFT), columnWidthWithAlignment(426, ALIGN_LEFT));
30
+ printer.printInColumns("Number", "Name");
31
+ printer.setPrintModes(true, true, true);
32
+ printer.printInColumns("1 x", "我是很长的商品名称我是很长的商品名称我是很长的商品名称我是很长的商品名称");
33
+ printer.setPrintModes(false, false, false);
34
+ dividing(printer);
35
+ printer.lineFeed(4);
36
+ printer.cutPaper(false);
37
+ // Print and exit page mode
38
+ printer.printAndExitPageMode();
39
+ _context.next = 24;
40
+ return printer.print(ip, sn, 1);
41
+ case 24:
42
+ result = _context.sent;
43
+ return _context.abrupt("return", result);
44
+ case 26:
45
+ case "end":
46
+ return _context.stop();
47
+ }
48
+ }, _callee);
49
+ }));
50
+ return function preparing(_x, _x2, _x3) {
51
+ return _ref.apply(this, arguments);
52
+ };
53
+ }();
54
+ export { EscPosPrinter, ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, columnWidthWithAlignment, preparing };
package/es/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- export * from './otherUtils';
2
- export * from './typeUtils';
3
- export * from './document';
4
- export * from './date';
5
- export * from './platform';
6
- export * from './log';
7
- export * from './format';
1
+ export * from "./otherUtils";
2
+ export * from "./typeUtils";
3
+ export * from "./document";
4
+ export * from "./date";
5
+ export * from "./platform";
6
+ export * from "./log";
7
+ export * from "./format";
8
+ export * from "./escPosPrinter";
package/es/index.js CHANGED
@@ -5,4 +5,5 @@ export * from "./date";
5
5
  export * from "./platform";
6
6
  export * from "./log";
7
7
  export * from "./format";
8
+ export * from "./escPosPrinter";
8
9
  // export { default as firebase } from './firebase';
@@ -0,0 +1,524 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/escPosPrinter/EscPosPrinter.js
20
+ var EscPosPrinter_exports = {};
21
+ __export(EscPosPrinter_exports, {
22
+ ALIGN_CENTER: () => ALIGN_CENTER,
23
+ ALIGN_LEFT: () => ALIGN_LEFT,
24
+ ALIGN_RIGHT: () => ALIGN_RIGHT,
25
+ HRI_POS_ABOVE: () => HRI_POS_ABOVE,
26
+ HRI_POS_BELOW: () => HRI_POS_BELOW,
27
+ MAX_COLUMNS: () => MAX_COLUMNS,
28
+ columnWidthWithAlignment: () => columnWidthWithAlignment,
29
+ default: () => EscPosPrinter_default
30
+ });
31
+ module.exports = __toCommonJS(EscPosPrinter_exports);
32
+ var MAX_COLUMNS = 6;
33
+ var ALIGN_LEFT = 0;
34
+ var ALIGN_CENTER = 1;
35
+ var ALIGN_RIGHT = 2;
36
+ var HRI_POS_ABOVE = 1;
37
+ var HRI_POS_BELOW = 2;
38
+ var EscPosPrinter = function(dotsPerLine) {
39
+ if (dotsPerLine != 384 && dotsPerLine != 576)
40
+ dotsPerLine = 384;
41
+ this.widthOfColumns = new Array(MAX_COLUMNS);
42
+ this.dotsPerLine = dotsPerLine;
43
+ this.charHSize = 1;
44
+ this.orderData = "";
45
+ };
46
+ function _numToHexStr(n, bytes) {
47
+ var str = "";
48
+ var v;
49
+ for (var i = 0; i < bytes; i++) {
50
+ v = n & 255;
51
+ if (v < 16)
52
+ str += "0" + v.toString(16);
53
+ else
54
+ str += v.toString(16);
55
+ n >>= 8;
56
+ }
57
+ return str;
58
+ }
59
+ function _unicodeToUtf8(unicode) {
60
+ var c1, c2, c3, c4;
61
+ if (unicode < 0)
62
+ return "";
63
+ if (unicode <= 127) {
64
+ c1 = unicode & 127;
65
+ return _numToHexStr(c1, 1);
66
+ }
67
+ if (unicode <= 2047) {
68
+ c1 = unicode >> 6 & 31 | 192;
69
+ c2 = unicode & 63 | 128;
70
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1);
71
+ }
72
+ if (unicode <= 65535) {
73
+ c1 = unicode >> 12 & 15 | 224;
74
+ c2 = unicode >> 6 & 63 | 128;
75
+ c3 = unicode & 63 | 128;
76
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1);
77
+ }
78
+ if (unicode <= 1114111) {
79
+ c1 = unicode >> 18 & 7 | 240;
80
+ c2 = unicode >> 12 & 63 | 128;
81
+ c3 = unicode >> 6 & 63 | 128;
82
+ c4 = unicode & 63 | 128;
83
+ return _numToHexStr(c1, 1) + _numToHexStr(c2, 1) + _numToHexStr(c3, 1) + _numToHexStr(c4, 1);
84
+ }
85
+ return "";
86
+ }
87
+ EscPosPrinter.prototype.clear = function() {
88
+ this.orderData = "";
89
+ };
90
+ EscPosPrinter.prototype.print = function(host, sn, copies) {
91
+ var that = this;
92
+ return new Promise(function(resolve, reject) {
93
+ var xmlobj = new XMLHttpRequest();
94
+ xmlobj.timeout = 1e4;
95
+ var url = "https://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
96
+ xmlobj.open("POST", url, true);
97
+ xmlobj.onreadystatechange = function() {
98
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
99
+ if (xmlobj.status.toString(10) === "200") {
100
+ var re = /task_id: (\d+)/;
101
+ var matches = re.exec(xmlobj.responseText);
102
+ resolve(matches[1] || "");
103
+ } else {
104
+ reject(xmlobj.responseText);
105
+ }
106
+ }
107
+ };
108
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
109
+ xmlobj.send(that.orderData);
110
+ });
111
+ };
112
+ EscPosPrinter.prototype.queryStatus = function(host, sn, task_id) {
113
+ return new Promise(function(resolve, reject) {
114
+ var xmlobj = new XMLHttpRequest();
115
+ xmlobj.timeout = 1e4;
116
+ var url = "https://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
117
+ xmlobj.open("POST", url, true);
118
+ xmlobj.onreadystatechange = function() {
119
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
120
+ if (xmlobj.status.toString(10) === "200") {
121
+ resolve(xmlobj.responseText);
122
+ } else {
123
+ reject(xmlobj.responseText);
124
+ }
125
+ }
126
+ };
127
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
128
+ xmlobj.send();
129
+ });
130
+ };
131
+ EscPosPrinter.prototype.appendText = function(str) {
132
+ for (var i = 0; i < str.length; i++)
133
+ this.orderData += _unicodeToUtf8(str.charCodeAt(i));
134
+ };
135
+ EscPosPrinter.prototype.lineFeed = function(n) {
136
+ for (var i = 0; i < n; i++)
137
+ this.orderData += "0a";
138
+ };
139
+ EscPosPrinter.prototype.restoreDefaultSettings = function() {
140
+ this.charHSize = 1;
141
+ this.orderData += "1b40";
142
+ };
143
+ EscPosPrinter.prototype.restoreDefaultLineSpacing = function() {
144
+ this.orderData += "1b32";
145
+ };
146
+ EscPosPrinter.prototype.setLineSpacing = function(n) {
147
+ if (n >= 0 && n <= 255)
148
+ this.orderData += "1b33" + _numToHexStr(n, 1);
149
+ };
150
+ EscPosPrinter.prototype.setPrintModes = function(bold, double_h, double_w) {
151
+ var n = 0;
152
+ if (bold)
153
+ n |= 8;
154
+ if (double_h)
155
+ n |= 16;
156
+ if (double_w)
157
+ n |= 32;
158
+ this.charHSize = double_w ? 2 : 1;
159
+ this.orderData += "1b21" + _numToHexStr(n, 1);
160
+ };
161
+ EscPosPrinter.prototype.setCharacterSize = function(h, w) {
162
+ var n = 0;
163
+ if (h >= 1 && h <= 8)
164
+ n |= h - 1;
165
+ if (w >= 1 && w <= 8) {
166
+ n |= w - 1 << 4;
167
+ this.charHSize = w;
168
+ }
169
+ this.orderData += "1d21" + _numToHexStr(n, 1);
170
+ };
171
+ EscPosPrinter.prototype.horizontalTab = function(n) {
172
+ for (var i = 0; i < n; i++)
173
+ this.orderData += "09";
174
+ };
175
+ EscPosPrinter.prototype.setAbsolutePrintPosition = function(n) {
176
+ if (n >= 0 && n <= 65535)
177
+ this.orderData += "1b24" + _numToHexStr(n, 2);
178
+ };
179
+ EscPosPrinter.prototype.setRelativePrintPosition = function(n) {
180
+ if (n >= -32768 && n <= 32767)
181
+ this.orderData += "1b5c" + _numToHexStr(n, 2);
182
+ };
183
+ EscPosPrinter.prototype.setAlignment = function(n) {
184
+ if (n >= 0 && n <= 2)
185
+ this.orderData += "1b61" + _numToHexStr(n, 1);
186
+ };
187
+ EscPosPrinter.prototype.cutPaper = function(fullCut) {
188
+ this.orderData += "1d56" + (fullCut ? "30" : "31");
189
+ };
190
+ EscPosPrinter.prototype.postponedCutPaper = function(fullCut, n) {
191
+ if (n >= 0 && n <= 255)
192
+ this.orderData += "1d56" + (fullCut ? "61" : "62") + _numToHexStr(n, 1);
193
+ };
194
+ function _widthOfChar(c) {
195
+ if (c >= 32 && c <= 879 || c >= 65377 && c <= 65439)
196
+ return 12;
197
+ if (c == 8208 || c >= 8211 && c <= 8214 || c >= 8216 && c <= 8217 || c >= 8220 && c <= 8221 || c >= 8229 && c <= 8230 || c >= 8240 && c <= 8243 || c == 8245 || c == 8251)
198
+ return 24;
199
+ if (c >= 4352 && c <= 4607 || c >= 9312 && c <= 9471 || c >= 9632 && c <= 10175 || c >= 11904 && c <= 12255 || c >= 12288 && c <= 12687 || c >= 12704 && c <= 12783 || c >= 12800 && c <= 40959 || c >= 44032 && c <= 55295 || c >= 63744 && c <= 64255 || c >= 65072 && c <= 65103 || c >= 126976 && c <= 129535)
200
+ return 24;
201
+ if (c >= 65281 && c <= 65374 || c >= 65504 && c <= 65509)
202
+ return 24;
203
+ return 0;
204
+ }
205
+ function columnWidthWithAlignment(width, alignment) {
206
+ return width & 65535 | (alignment & 3) << 16;
207
+ }
208
+ function _columnWidth(v) {
209
+ return v & 65535;
210
+ }
211
+ function _columnAlignment(v) {
212
+ return v >> 16 & 3;
213
+ }
214
+ EscPosPrinter.prototype.setColumnWidths = function() {
215
+ var i, remain, width, alignment;
216
+ if (arguments.length == 0)
217
+ return;
218
+ for (i = 0; i < MAX_COLUMNS; i++)
219
+ this.widthOfColumns[i] = 0;
220
+ remain = this.dotsPerLine;
221
+ for (i = 0; i < arguments.length; i++) {
222
+ if (i == MAX_COLUMNS)
223
+ return;
224
+ width = _columnWidth(arguments[i]);
225
+ alignment = _columnAlignment(arguments[i]);
226
+ if (width == 0 || width > remain) {
227
+ this.widthOfColumns[i] = columnWidthWithAlignment(remain, alignment);
228
+ return;
229
+ }
230
+ this.widthOfColumns[i] = arguments[i];
231
+ remain -= width;
232
+ }
233
+ };
234
+ EscPosPrinter.prototype.printInColumns = function() {
235
+ var strcurr = new Array(MAX_COLUMNS);
236
+ var strrem = new Array(MAX_COLUMNS);
237
+ var strwidth = new Array(MAX_COLUMNS);
238
+ var i, j, c, w, columns, width, alignment, pos;
239
+ var done;
240
+ if (arguments.length == 0)
241
+ return;
242
+ columns = 0;
243
+ for (i = 0; i < arguments.length; i++) {
244
+ if (i == MAX_COLUMNS || this.widthOfColumns[i] == 0)
245
+ break;
246
+ strcurr[i] = "";
247
+ strrem[i] = arguments[i];
248
+ columns++;
249
+ }
250
+ do {
251
+ done = true;
252
+ pos = 0;
253
+ for (i = 0; i < columns; i++) {
254
+ width = _columnWidth(this.widthOfColumns[i]);
255
+ if (strrem[i].length == 0) {
256
+ pos += width;
257
+ continue;
258
+ }
259
+ done = false;
260
+ strcurr[i] = "";
261
+ strwidth[i] = 0;
262
+ for (j = 0; j < strrem[i].length; j++) {
263
+ c = strrem[i].charCodeAt(j);
264
+ if (c == 10) {
265
+ j++;
266
+ break;
267
+ } else {
268
+ w = _widthOfChar(c);
269
+ if (w == 0) {
270
+ c = "?";
271
+ w = 12;
272
+ }
273
+ w *= this.charHSize;
274
+ if (strwidth[i] + w > width) {
275
+ break;
276
+ } else {
277
+ strcurr[i] += String.fromCharCode(c);
278
+ strwidth[i] += w;
279
+ }
280
+ }
281
+ }
282
+ if (j < strrem[i].length)
283
+ strrem[i] = strrem[i].substring(j);
284
+ else
285
+ strrem[i] = "";
286
+ alignment = _columnAlignment(this.widthOfColumns[i]);
287
+ switch (alignment) {
288
+ case ALIGN_CENTER:
289
+ this.setAbsolutePrintPosition(pos + (width - strwidth[i]) / 2);
290
+ break;
291
+ case ALIGN_RIGHT:
292
+ this.setAbsolutePrintPosition(pos + (width - strwidth[i]));
293
+ break;
294
+ default:
295
+ this.setAbsolutePrintPosition(pos);
296
+ break;
297
+ }
298
+ this.appendText(strcurr[i]);
299
+ pos += width;
300
+ }
301
+ if (!done)
302
+ this.lineFeed(1);
303
+ } while (!done);
304
+ };
305
+ EscPosPrinter.prototype.appendBarcode = function(hri_pos, height, module_size, barcode_type, text) {
306
+ var text_length = text.length;
307
+ if (text_length == 0)
308
+ return;
309
+ if (text_length > 255)
310
+ text_length = 255;
311
+ if (height < 1)
312
+ height = 1;
313
+ else if (height > 255)
314
+ height = 255;
315
+ if (module_size < 1)
316
+ module_size = 1;
317
+ else if (module_size > 6)
318
+ module_size = 6;
319
+ this.orderData += "1d48" + _numToHexStr(hri_pos & 3, 1);
320
+ this.orderData += "1d6600";
321
+ this.orderData += "1d68" + _numToHexStr(height, 1);
322
+ this.orderData += "1d77" + _numToHexStr(module_size, 1);
323
+ this.orderData += "1d6b" + _numToHexStr(barcode_type, 1) + _numToHexStr(text_length, 1);
324
+ for (var i = 0; i < text_length; i++)
325
+ this.orderData += _numToHexStr(text.charCodeAt(i), 1);
326
+ };
327
+ EscPosPrinter.prototype.appendQRcode = function(module_size, ec_level, text) {
328
+ var text_length = text.length;
329
+ if (text_length == 0)
330
+ return;
331
+ if (text_length > 65535)
332
+ text_length = 65535;
333
+ if (module_size < 1)
334
+ module_size = 1;
335
+ else if (module_size > 16)
336
+ module_size = 16;
337
+ if (ec_level < 0)
338
+ ec_level = 0;
339
+ else if (ec_level > 3)
340
+ ec_level = 3;
341
+ this.orderData += "1d286b040031410000";
342
+ this.orderData += "1d286b03003143" + _numToHexStr(module_size, 1);
343
+ this.orderData += "1d286b03003145" + _numToHexStr(ec_level + 48, 1);
344
+ this.orderData += "1d286b" + _numToHexStr(text_length + 3, 2) + "315030";
345
+ for (var i = 0; i < text_length; i++)
346
+ this.orderData += _numToHexStr(text.charCodeAt(i), 1);
347
+ this.orderData += "1d286b0300315130";
348
+ };
349
+ function _diffuseDither(src_data, width, height) {
350
+ if (width <= 0 || height <= 0)
351
+ return null;
352
+ if (src_data.length < width * height)
353
+ return null;
354
+ var bmwidth = width + 7 >> 3;
355
+ var dst_data = new Array(bmwidth * height);
356
+ var line_buffer = new Array(2 * width);
357
+ var i, p, q, x, y, mask;
358
+ var line1, line2, b1, b2, tmp;
359
+ var err, e1, e3, e5, e7;
360
+ var not_last_line;
361
+ line1 = 0;
362
+ line2 = 1;
363
+ for (i = 0; i < width; i++) {
364
+ line_buffer[i] = 0;
365
+ line_buffer[width + i] = src_data[i];
366
+ }
367
+ for (y = 0; y < height; y++) {
368
+ tmp = line1;
369
+ line1 = line2;
370
+ line2 = tmp;
371
+ not_last_line = y < height - 1;
372
+ if (not_last_line) {
373
+ p = (y + 1) * width;
374
+ for (i = 0; i < width; i++) {
375
+ line_buffer[line2 * width + i] = src_data[p];
376
+ p++;
377
+ }
378
+ }
379
+ q = y * bmwidth;
380
+ for (i = 0; i < bmwidth; i++) {
381
+ dst_data[q] = 0;
382
+ q++;
383
+ }
384
+ b1 = 0;
385
+ b2 = 0;
386
+ q = y * bmwidth;
387
+ mask = 128;
388
+ for (x = 1; x <= width; x++) {
389
+ var idx = line1 * width + b1;
390
+ if (line_buffer[idx] < 128) {
391
+ err = line_buffer[idx];
392
+ dst_data[q] |= mask;
393
+ } else {
394
+ err = line_buffer[idx] - 255;
395
+ }
396
+ b1++;
397
+ if (mask == 1) {
398
+ q++;
399
+ mask = 128;
400
+ } else {
401
+ mask >>= 1;
402
+ }
403
+ e7 = err * 7 + 8 >> 4;
404
+ e5 = err * 5 + 8 >> 4;
405
+ e3 = err * 3 + 8 >> 4;
406
+ e1 = err - (e7 + e5 + e3);
407
+ if (x < width)
408
+ line_buffer[line1 * width + b1] += e7;
409
+ if (not_last_line) {
410
+ line_buffer[line2 * width + b2] += e5;
411
+ if (x > 1)
412
+ line_buffer[line2 * width + b2 - 1] += e3;
413
+ if (x < width)
414
+ line_buffer[line2 * width + b2 + 1] += e1;
415
+ }
416
+ b2++;
417
+ }
418
+ }
419
+ return dst_data;
420
+ }
421
+ function _thresholdDither(src_data, width, height) {
422
+ if (width <= 0 || height <= 0)
423
+ return null;
424
+ if (src_data.length < width * height)
425
+ return null;
426
+ var bmwidth = width + 7 >> 3;
427
+ var dst_data = new Array(bmwidth * height);
428
+ var p, q, k, x, y, mask;
429
+ p = 0;
430
+ q = 0;
431
+ for (y = 0; y < height; y++) {
432
+ k = q;
433
+ mask = 128;
434
+ for (x = 0; x < width; x++) {
435
+ if (src_data[p] < 128) {
436
+ dst_data[k] |= mask;
437
+ }
438
+ p++;
439
+ if (mask == 1) {
440
+ k++;
441
+ mask = 128;
442
+ } else {
443
+ mask >>= 1;
444
+ }
445
+ }
446
+ q += bmwidth;
447
+ }
448
+ return dst_data;
449
+ }
450
+ function _convertToGray(imgdata, width, height) {
451
+ var gray_data = new Array(width * height);
452
+ var i = 0, j = 0, x, y, r, g, b;
453
+ for (y = 0; y < height; y++) {
454
+ for (x = 0; x < width; x++) {
455
+ r = imgdata.data[j++] & 255;
456
+ g = imgdata.data[j++] & 255;
457
+ b = imgdata.data[j++] & 255;
458
+ j++;
459
+ gray_data[i++] = r * 11 + g * 16 + b * 5 >> 5 & 255;
460
+ }
461
+ }
462
+ return gray_data;
463
+ }
464
+ EscPosPrinter.prototype.appendImage = function(imgdata, dither) {
465
+ var gray_data, mono_data;
466
+ var w = imgdata.width;
467
+ var h = imgdata.height;
468
+ gray_data = _convertToGray(imgdata, w, h);
469
+ if (dither == "diffuse")
470
+ mono_data = _diffuseDither(gray_data, w, h);
471
+ else
472
+ mono_data = _thresholdDither(gray_data, w, h);
473
+ w = w + 7 >> 3;
474
+ this.orderData += "1d763000";
475
+ this.orderData += _numToHexStr(w, 2);
476
+ this.orderData += _numToHexStr(h, 2);
477
+ for (var i = 0; i < mono_data.length; i++)
478
+ this.orderData += _numToHexStr(mono_data[i] & 255, 1);
479
+ };
480
+ EscPosPrinter.prototype.enterPageMode = function() {
481
+ this.orderData += "1b4c";
482
+ };
483
+ EscPosPrinter.prototype.setPrintAreaInPageMode = function(x, y, w, h) {
484
+ this.orderData += "1b57";
485
+ this.orderData += _numToHexStr(x, 2);
486
+ this.orderData += _numToHexStr(y, 2);
487
+ this.orderData += _numToHexStr(w, 2);
488
+ this.orderData += _numToHexStr(h, 2);
489
+ };
490
+ EscPosPrinter.prototype.setPrintDirectionInPageMode = function(dir) {
491
+ if (dir >= 0 && dir <= 3)
492
+ this.orderData += "1b54" + _numToHexStr(dir, 1);
493
+ };
494
+ EscPosPrinter.prototype.setAbsolutePrintPositionInPageMode = function(n) {
495
+ if (n >= 0 && n <= 65535)
496
+ this.orderData += "1d24" + _numToHexStr(n, 2);
497
+ };
498
+ EscPosPrinter.prototype.setRelativePrintPositionInPageMode = function(n) {
499
+ if (n >= -32768 && n <= 32767)
500
+ this.orderData += "1d5c" + _numToHexStr(n, 2);
501
+ };
502
+ EscPosPrinter.prototype.printAndExitPageMode = function() {
503
+ this.orderData += "0c";
504
+ };
505
+ EscPosPrinter.prototype.printInPageMode = function() {
506
+ this.orderData += "1b0c";
507
+ };
508
+ EscPosPrinter.prototype.clearInPageMode = function() {
509
+ this.orderData += "18";
510
+ };
511
+ EscPosPrinter.prototype.exitPageMode = function() {
512
+ this.orderData += "1b53";
513
+ };
514
+ var EscPosPrinter_default = EscPosPrinter;
515
+ // Annotate the CommonJS export names for ESM import in node:
516
+ 0 && (module.exports = {
517
+ ALIGN_CENTER,
518
+ ALIGN_LEFT,
519
+ ALIGN_RIGHT,
520
+ HRI_POS_ABOVE,
521
+ HRI_POS_BELOW,
522
+ MAX_COLUMNS,
523
+ columnWidthWithAlignment
524
+ });
@@ -0,0 +1,9 @@
1
+ export declare const EscPosPrinter = any;
2
+ export declare const ALIGN_LEFT = number;
3
+ export declare const ALIGN_CENTER = number;
4
+ export declare const ALIGN_RIGHT = number;
5
+ export declare const columnWidthWithAlignment = (
6
+ width: number,
7
+ alignment: number
8
+ ) => any;
9
+ export declare const preparing = any;
@@ -0,0 +1,85 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/escPosPrinter/index.js
30
+ var escPosPrinter_exports = {};
31
+ __export(escPosPrinter_exports, {
32
+ ALIGN_CENTER: () => import_EscPosPrinter.ALIGN_CENTER,
33
+ ALIGN_LEFT: () => import_EscPosPrinter.ALIGN_LEFT,
34
+ ALIGN_RIGHT: () => import_EscPosPrinter.ALIGN_RIGHT,
35
+ EscPosPrinter: () => import_EscPosPrinter.default,
36
+ columnWidthWithAlignment: () => import_EscPosPrinter.columnWidthWithAlignment,
37
+ preparing: () => preparing
38
+ });
39
+ module.exports = __toCommonJS(escPosPrinter_exports);
40
+ var import_EscPosPrinter = __toESM(require("./EscPosPrinter"));
41
+ var dividing = (printer) => {
42
+ printer.setAlignment(import_EscPosPrinter.ALIGN_LEFT);
43
+ printer.appendText("------------------------------------------------\n");
44
+ };
45
+ var preparing = async (ip, sn, params) => {
46
+ const printer = new import_EscPosPrinter.default(576);
47
+ const { title } = params;
48
+ printer.clear();
49
+ printer.lineFeed(1);
50
+ printer.setPrintModes(false, false, false);
51
+ printer.setAlignment(import_EscPosPrinter.ALIGN_CENTER);
52
+ printer.appendText("\u6253\u5370\u6D4B\u8BD5\n");
53
+ dividing(printer);
54
+ printer.setAlignment(import_EscPosPrinter.ALIGN_LEFT);
55
+ printer.appendText("Order time: 2023-10-19 14:15:50\n");
56
+ printer.appendText("Order number\n");
57
+ printer.appendText("D3012");
58
+ dividing(printer);
59
+ printer.setColumnWidths(
60
+ (0, import_EscPosPrinter.columnWidthWithAlignment)(150, import_EscPosPrinter.ALIGN_LEFT),
61
+ (0, import_EscPosPrinter.columnWidthWithAlignment)(426, import_EscPosPrinter.ALIGN_LEFT)
62
+ );
63
+ printer.printInColumns("Number", "Name");
64
+ printer.setPrintModes(true, true, true);
65
+ printer.printInColumns(
66
+ "1 x",
67
+ "\u6211\u662F\u5F88\u957F\u7684\u5546\u54C1\u540D\u79F0\u6211\u662F\u5F88\u957F\u7684\u5546\u54C1\u540D\u79F0\u6211\u662F\u5F88\u957F\u7684\u5546\u54C1\u540D\u79F0\u6211\u662F\u5F88\u957F\u7684\u5546\u54C1\u540D\u79F0"
68
+ );
69
+ printer.setPrintModes(false, false, false);
70
+ dividing(printer);
71
+ printer.lineFeed(4);
72
+ printer.cutPaper(false);
73
+ printer.printAndExitPageMode();
74
+ const result = await printer.print(ip, sn, 1);
75
+ return result;
76
+ };
77
+ // Annotate the CommonJS export names for ESM import in node:
78
+ 0 && (module.exports = {
79
+ ALIGN_CENTER,
80
+ ALIGN_LEFT,
81
+ ALIGN_RIGHT,
82
+ EscPosPrinter,
83
+ columnWidthWithAlignment,
84
+ preparing
85
+ });
package/lib/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- export * from './otherUtils';
2
- export * from './typeUtils';
3
- export * from './document';
4
- export * from './date';
5
- export * from './platform';
6
- export * from './log';
7
- export * from './format';
1
+ export * from "./otherUtils";
2
+ export * from "./typeUtils";
3
+ export * from "./document";
4
+ export * from "./date";
5
+ export * from "./platform";
6
+ export * from "./log";
7
+ export * from "./format";
8
+ export * from "./escPosPrinter";
package/lib/index.js CHANGED
@@ -23,6 +23,7 @@ __reExport(src_exports, require("./date"), module.exports);
23
23
  __reExport(src_exports, require("./platform"), module.exports);
24
24
  __reExport(src_exports, require("./log"), module.exports);
25
25
  __reExport(src_exports, require("./format"), module.exports);
26
+ __reExport(src_exports, require("./escPosPrinter"), module.exports);
26
27
  // Annotate the CommonJS export names for ESM import in node:
27
28
  0 && (module.exports = {
28
29
  ...require("./otherUtils"),
@@ -31,5 +32,6 @@ __reExport(src_exports, require("./format"), module.exports);
31
32
  ...require("./date"),
32
33
  ...require("./platform"),
33
34
  ...require("./log"),
34
- ...require("./format")
35
+ ...require("./format"),
36
+ ...require("./escPosPrinter")
35
37
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pisell/utils",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "sideEffects": false,
5
5
  "main": "./lib/index.js",
6
6
  "module": "./es/index.js",