diff --git a/lib/types/epson.js b/lib/types/epson.js index 062510a..2f10718 100644 --- a/lib/types/epson.js +++ b/lib/types/epson.js @@ -47,44 +47,29 @@ class Epson extends PrinterType { // ------------------------------ CODE 128 ------------------------------ code128(data, settings) { this.buffer = null; - settings = settings || { + settings = { hriPos: 0, hriFont: 0, width: 3, - height: 162 + height: 162, + ...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 characters Position, 0-3 (none, top, bottom, top/bottom), default 0 none + this.append(Buffer.from([0x1d, 0x48])); // GS H + this.append(Buffer.from([settings.hriPos])); // 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])); - } + this.append(Buffer.from([0x1d, 0x66])); // GS f + this.append(Buffer.from([settings.hriFont])); // 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])); - } + this.append(Buffer.from([0x1d, 0x77])); // GS W + this.append(Buffer.from([settings.width])); // 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])); - } + this.append(Buffer.from([0x1d, 0x68])); // GS h + this.append(Buffer.from([settings.height])); // Print Barcode this.append(this.config.BARCODE_CODE128); @@ -99,7 +84,12 @@ class Epson extends PrinterType { // ------------------------------ QR ------------------------------ printQR(str, settings) { this.buffer = null; - settings = settings || {}; + settings = { + model: 2, + cellSize: 3, + correction: 'M', + ...settings + }; // [Name] Select the QR code model // [Code] 1D 28 6B 04 00 31 41 n1 n2 @@ -109,24 +99,16 @@ class Epson extends PrinterType { // [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); - } + 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); // [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); - } + const size = 'QRCODE_CELLSIZE_'.concat(settings.cellSize.toString()); + this.append(this.config[size]); // [Name] Select the error correction level // 1D 28 6B 03 00 31 45 n @@ -136,12 +118,8 @@ class Epson extends PrinterType { // [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); - } + const correction = 'QRCODE_CORRECTION_'.concat(settings.correction.toUpperCase()); + this.append(this.config[correction]); // [Name] Store the data in the symbol storage area // 1D 28 6B pL pH 31 50 30 d1...dk @@ -163,44 +141,30 @@ class Epson extends PrinterType { // ------------------------------ PDF417 ------------------------------ pdf417(data, settings) { this.buffer = null; - settings = settings || {}; + settings = { + correction: 1, + rowHeight: 3, + width: 3, + columns: 0, + truncated: false, + ...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 error correction ratio 1 - 40, default 1 + this.append(this.config.PDF417_CORRECTION); + this.append(Buffer.from([settings.correction])); - // 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 row height 2 - 8, default 3 + this.append(this.config.PDF417_ROW_HEIGHT); + this.append(Buffer.from([settings.rowHeight])); - // 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])); - } + // Set width of module 2 - 8, default 3 + this.append(this.config.PDF417_WIDTH); + this.append(Buffer.from([settings.width])); - // 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])); - } + // Manually set columns 1 - 30, default auto + this.append(this.config.PDF417_COLUMNS); + this.append(Buffer.from([settings.columns])); // Standard or truncated option if (settings.truncated) this.append(this.config.PDF417_OPTION_TRUNCATED); @@ -223,7 +187,10 @@ class Epson extends PrinterType { // ------------------------------ MAXI CODE ------------------------------ maxiCode(data, settings) { this.buffer = null; - settings = settings || {}; + settings = { + mode: 4, + ...settings + }; // Maxi Mode // 2 - Formatted data/structured Carrier Message with a numeric postal code. (US) @@ -231,16 +198,11 @@ class Epson extends PrinterType { // 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); - } + 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); // Setup size of MaxiCode data const s = data.length + 3; @@ -260,50 +222,41 @@ class Epson extends PrinterType { // ------------------------------ BARCODE ------------------------------ printBarcode(data, type, settings) { this.buffer = null; - settings = settings || {}; + settings = { + hriPos: 0, + hriFont: 0, + width: 3, + height: 162, + ...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])); - } + this.append(Buffer.from([0x1d, 0x48])); // GS H + this.append(Buffer.from([settings.hriPos])); // 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])); - } + this.append(Buffer.from([0x1d, 0x66])); // GS f + this.append(Buffer.from([settings.hriFont])); // 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])); - } + this.append(Buffer.from([0x1d, 0x77])); // GS W + this.append(Buffer.from([settings.width])); // 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])); - } + this.append(Buffer.from([0x1d, 0x68])); // GS h + this.append(Buffer.from([settings.height])); // 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 { + } else { this.append(Buffer.from([type, data.length])); } + // Data this.append(Buffer.from(data));