-
Notifications
You must be signed in to change notification settings - Fork 233
/
epson.js
349 lines (298 loc) · 10.7 KB
/
epson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const PrinterType = require('./printer-type');
class Epson extends PrinterType {
constructor() {
super();
this.config = require('./epson-config');
}
// ------------------------------ Get paper status ------------------------------
getStatus() {
// https://www.epson-biz.com/modules/ref_escpos/index.php?content_id=124#gs_lr
return this.config.TRANSMIT_PAPER_STATUS;
}
// ------------------------------ Append ------------------------------
append(appendBuffer) {
if (this.buffer) {
this.buffer = Buffer.concat([this.buffer, appendBuffer]);
} else {
this.buffer = appendBuffer;
}
}
// ------------------------------ Beep ------------------------------
beep() {
return this.config.BEEP;
}
// ------------------------------ Set text size ------------------------------
setTextSize(height, width) {
this.buffer = null;
if (height > 7 || height < 0) throw new Error('setTextSize: Height must be between 0 and 7');
if (width > 7 || width < 0) throw new Error('setTextSize: Width must be between 0 and 7');
const x = Buffer.from(`${height}${width}`, 'hex');
this.append(Buffer.from([0x1D, 0x21]));
this.append(x);
return this.buffer;
}
// ------------------------------ QR ------------------------------
printQR(str, settings) {
this.buffer = null;
settings = settings || {};
// [Name] Select the QR code model
// [Code] 1D 28 6B 04 00 31 41 n1 n2
// n1
// [49 x31, model 1]
// [50 x32, model 2]
// [51 x33, micro qr code]
// n2 = 0
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=140
if (settings.model) {
if (settings.model === 1) this.append(this.config.QRCODE_MODEL1);
else if (settings.model === 3) this.append(this.config.QRCODE_MODEL3);
else this.append(this.config.QRCODE_MODEL2);
} else {
this.append(this.config.QRCODE_MODEL2);
}
// [Name]: Set the size of module
// 1D 28 6B 03 00 31 43 n
// n depends on the printer
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=141
if (settings.cellSize) {
const i = 'QRCODE_CELLSIZE_'.concat(settings.cellSize.toString());
this.append(this.config[i]);
} else {
this.append(this.config.QRCODE_CELLSIZE_3);
}
// [Name] Select the error correction level
// 1D 28 6B 03 00 31 45 n
// n
// [48 x30 -> 7%]
// [49 x31-> 15%]
// [50 x32 -> 25%]
// [51 x33 -> 30%]
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=142
if (settings.correction) {
const i = 'QRCODE_CORRECTION_'.concat(settings.correction.toUpperCase());
this.append(this.config[i]);
} else {
this.append(this.config.QRCODE_CORRECTION_M);
}
// [Name] Store the data in the symbol storage area
// 1D 28 6B pL pH 31 50 30 d1...dk
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143
const s = str.length + 3;
const lsb = parseInt(s % 256);
const msb = parseInt(s / 256);
this.append(Buffer.from([0x1d, 0x28, 0x6b, lsb, msb, 0x31, 0x50, 0x30]));
this.append(Buffer.from(str));
// [Name] Print the symbol data in the symbol storage area
// 1D 28 6B 03 00 31 51 m
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=144
this.append(this.config.QRCODE_PRINT);
return this.buffer;
}
// ------------------------------ PDF417 ------------------------------
pdf417(data, settings) {
this.buffer = null;
settings = settings || {};
// Set error correction ratio 1 - 40
if (settings.correction) {
this.append(this.config.PDF417_CORRECTION);
this.append(Buffer.from([settings.correction]));
} else {
this.append(this.config.PDF417_CORRECTION);
this.append(Buffer.from([0x01]));
}
// Set row height 2 - 8
if (settings.rowHeight) {
this.append(this.config.PDF417_ROW_HEIGHT);
this.append(Buffer.from([settings.rowHeight]));
} else {
this.append(this.config.PDF417_ROW_HEIGHT);
this.append(Buffer.from([0x03]));
}
// Set width of module 2 - 8
if (settings.width) {
this.append(this.config.PDF417_WIDTH);
this.append(Buffer.from([settings.width]));
} else {
this.append(this.config.PDF417_WIDTH);
this.append(Buffer.from([0x03]));
}
// Manually set columns 1 - 30
if (settings.columns) {
this.append(this.config.PDF417_COLUMNS);
this.append(Buffer.from([settings.columns]));
} else {
// Default to auto
this.append(this.config.PDF417_COLUMNS);
this.append(Buffer.from([0x00]));
}
// Standard or truncated option
if (settings.truncated) this.append(this.config.PDF417_OPTION_TRUNCATED);
else this.append(this.config.PDF417_OPTION_STANDARD);
// Set PDF417 bar code data
const s = data.length + 3;
const lsb = parseInt(s % 256);
const msb = parseInt(s / 256);
this.append(Buffer.from([0x1d, 0x28, 0x6b, lsb, msb, 0x30, 0x50, 0x30]));
this.append(Buffer.from(data.toString()));
// Print barcode
this.append(Buffer.from(this.config.PDF417_PRINT));
return this.buffer;
}
// ------------------------------ MAXI CODE ------------------------------
maxiCode(data, settings) {
this.buffer = null;
settings = settings || {};
// Maxi Mode
// 2 - Formatted data/structured Carrier Message with a numeric postal code. (US)
// 3 - Formatted data/structured Carrier Message with a numeric postal code. (International)
// 4 - Unformatted data/Standard Error Correction.
// 5 - Unformatted data/Enhanced Error Correction.
// 6 - Used for programming hardware devices.
if (settings.mode) {
if (settings.mode == 2) this.append(this.config.MAXI_MODE2);
else if (settings.mode == 3) this.append(this.config.MAXI_MODE3);
else if (settings.mode == 5) this.append(this.config.MAXI_MODE5);
else if (settings.mode == 6) this.append(this.config.MAXI_MODE6);
else this.append(this.config.MAXI_MODE4);
} else {
this.append(this.config.MAXI_MODE4);
}
// Setup size of MaxiCode data
const s = data.length + 3;
const lsb = parseInt(s % 256);
const msb = parseInt(s / 256);
// Send Data
this.append(Buffer.from([0x1d, 0x28, 0x6b, lsb, msb, 0x32, 0x50, 0x30]));
this.append(Buffer.from(data.toString()));
// Print barcode
this.append(this.config.MAXI_PRINT);
return this.buffer;
}
// ------------------------------ BARCODE ------------------------------
printBarcode(data, type, settings) {
this.buffer = null;
settings = settings || {};
// Set HRI characters Position, 0-3 (none, top, bottom, top/bottom)
if (settings.hriPos) {
this.append(Buffer.from([0x1d, 0x48])); // GS H
this.append(Buffer.from([settings.hriPos]));
} else {
this.append(Buffer.from([0x1d, 0x48, 0x00]));
}
// Set HRI character font. 0-4, 48-52, 97, 98 (depending on printer, 0 and 1 available on all), default 0
if (settings.hriFont) {
this.append(Buffer.from([0x1d, 0x66])); // GS f
this.append(Buffer.from([settings.hriFont]));
} else {
this.append(Buffer.from([0x1d, 0x66, 0x00]));
}
// Set width 2-6, default 3
if (settings.width) {
this.append(Buffer.from([0x1d, 0x77])); // GS W
this.append(Buffer.from([settings.width]));
} else {
this.append(Buffer.from([0x1d, 0x77, 0x03]));
}
// Set height 1 - 255 default 162
if (settings.height) {
this.append(Buffer.from([0x1d, 0x68])); // GS h
this.append(Buffer.from([settings.height]));
} else {
this.append(Buffer.from([0x1d, 0x68, 0xA2]));
}
// Print Barcode
this.append(Buffer.from([0x1d, 0x6b])); // GS k
// Select type and bit length of data
if (type == 73) {
this.append(Buffer.from([type, data.length + 2]));
this.append(Buffer.from([0x7b, 0x42]));
}
else {
this.append(Buffer.from([type, data.length]));
}
// Data
this.append(Buffer.from(data));
return this.buffer;
}
// ----------------------------------------------------- PRINT IMAGE -----------------------------------------------------
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=88
async printImage(image) {
const fs = require('fs');
const { PNG } = require('pngjs');
try {
const data = fs.readFileSync(image);
const png = PNG.sync.read(data);
const buff = this.printImageBuffer(png.width, png.height, png.data);
return buff;
} catch (error) {
throw error;
}
}
printImageBuffer(width, height, data) {
this.buffer = null;
// Get pixel rgba in 2D array
const pixels = [];
for (let i = 0; i < height; i++) {
const line = [];
for (let j = 0; j < width; j++) {
const idx = (width * i + j) << 2;
line.push({
r: data[idx],
g: data[idx + 1],
b: data[idx + 2],
a: data[idx + 3],
});
}
pixels.push(line);
}
const imageBufferArray = [];
for (let i = 0; i < height; i++) {
for (let j = 0; j < Math.ceil(width / 8); j++) {
let byte = 0x0;
for (let k = 0; k < 8; k++) {
let pixel = pixels[i][j * 8 + k];
// Image overflow
if (pixel === undefined) {
pixel = {
a: 0,
r: 0,
g: 0,
b: 0,
};
}
if (pixel.a > 126) { // checking transparency
const grayscale = parseInt(0.2126 * pixel.r + 0.7152 * pixel.g + 0.0722 * pixel.b);
if (grayscale < 128) { // checking color
const mask = 1 << 7 - k; // setting bitwise mask
byte |= mask; // setting the correct bit to 1
}
}
}
imageBufferArray.push(byte);
// imageBuffer = Buffer.concat([imageBuffer, Buffer.from([byte])]);
}
}
const imageBuffer = Buffer.from(imageBufferArray);
// Print raster bit image
// GS v 0
// 1D 76 30 m xL xH yL yH d1...dk
// xL = (this.width >> 3) & 0xff;
// xH = 0x00;
// yL = this.height & 0xff;
// yH = (this.height >> 8) & 0xff;
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=94
// Check if width/8 is decimal
if (width % 8 != 0) {
width += 8;
}
this.append(Buffer.from([0x1d, 0x76, 0x30, 48]));
this.append(Buffer.from([(width >> 3) & 0xff]));
this.append(Buffer.from([0x00]));
this.append(Buffer.from([height & 0xff]));
this.append(Buffer.from([(height >> 8) & 0xff]));
// append data
this.append(imageBuffer);
return this.buffer;
}
}
module.exports = Epson;