const iconv = require('iconv-lite'); const ASCII = { LF: 0x0A, // ESC: 0x1B, FS: 0x1C, GS: 0x1D, US: 0x1F, FF: 0x0C, DLE: 0x10, DC1: 0x11, DC4: 0x14, EOT: 0x04, NUL: 0x00 }; export default class Generator { private modality: string; private readonly _max_characters_per_line: number; private font_size: number; private commands: any[]; private _page_table: string; constructor(max_characters_per_line = 48) { this.modality = Generator.MODALITY_CLASS; this._max_characters_per_line = max_characters_per_line; this.font_size = Generator.FONT_NORMAL; this.commands = []; this._page_table = 'gbk'; } setModality(modality: string) { if (modality === Generator.MODALITY_CLASS) { this.modality = Generator.MODALITY_CLASS; } else if (modality === Generator.MODALITY_ARRAY) { this.modality = Generator.MODALITY_ARRAY; } else if (modality === Generator.MODALITY_DIRECT) { this.modality = Generator.MODALITY_DIRECT; } return this; } init() { this.commands = []; const command = [ASCII.ESC, 0x40]; return this._rightModality(command); } selectCharacterCodeTable(esc_pos_number = 15, page_table = this._page_table) { const command = [ASCII.ESC, 0x74, esc_pos_number]; this._page_table = page_table; return this._rightModality(command); } newLine(line = 1) { const command = []; for (let i = 0; i < line; i++) { command.push(ASCII.LF); } return this._rightModality(command); } text(text: string) { if (text) { let bytes; if (Array.isArray(text)) { bytes = text; } else { bytes = this._textToBytes(text); } // console.log('text', bytes) // return this._rightModality(bytes) return this._rightModality([text]); } else { return this._rightModality([]); } } // textColumns(values: string) { // let done = !values.length; // let command: Array = []; // // if (!done) { // let currentAvailableChar = this._max_characters_per_line; // // // Prepare everything // const toBeAutomaticallyAllocated = []; // values.forEach((item) => { // if (!item.percentage) { // item.__allocated_chars = -1; // toBeAutomaticallyAllocated.push(item); // } else { // const toAlloc = parseInt((this._max_characters_per_line / 100 * item.percentage).toString()); // currentAvailableChar -= toAlloc; // item.__allocated_chars = toAlloc; // } // // const slitted = item.text.split(' '); // item.__text_array = []; // for (let i = 0; i < slitted.length; i++) { // item.__text_array.push(slitted[i]); // // if (i !== slitted.length - 1) { // item.__text_array.push(' '); // } // } // // if (!item.alignment) { // item.alignment = Generator.LEFT; // } // }); // // // Manage the automatic chars alloc // if (toBeAutomaticallyAllocated.length) { // const spacePerItem = parseInt((currentAvailableChar / toBeAutomaticallyAllocated.length).toString()); // toBeAutomaticallyAllocated.forEach((item) => { // item.__allocated_chars = spacePerItem; // currentAvailableChar -= spacePerItem; // }); // } // // // Check if there are some words that are larger than the max chars // values.forEach((item) => { // const processed = []; // item.__text_array.forEach((singleText) => { // function recursiveCut(text, max_chars, processed) { // if (text.length > max_chars) { // const subStr = text.substring(0, max_chars); // processed.push(subStr); // // recursiveCut(text.substring(max_chars, text.length), max_chars, processed); // } else { // processed.push(text); // } // } // // recursiveCut(singleText, item.__allocated_chars, processed); // }); // // item.__text_array = processed; // }); // // while (!done) { // let allocated = []; // // let done_counter = 0; // values.forEach((item) => { // let prefix = []; // if (item.prefix) { // prefix = item.prefix; // } // allocated = allocated.concat(prefix); // // let remainingCharsToBeAllocated = item.__allocated_chars; // // // Loop until I reach the max length // let bytesToWrite = []; // for (let i = 0; i < item.__text_array.length; i++) { // // Check if it's possible to insert the item // if (item.__text_array[i].length <= remainingCharsToBeAllocated) { // bytesToWrite = bytesToWrite.concat(this._textToBytes(item.__text_array[i])); // remainingCharsToBeAllocated -= item.__text_array[i].length; // // item.__text_array.splice(i, 1); // i--; // } // } // // const bytesSpaces = []; // for (let i = 0; i < remainingCharsToBeAllocated; i++) { // bytesSpaces.push(0x20); // } // // switch (item.alignment) { // case Generator.LEFT: // bytesToWrite = bytesToWrite.concat(bytesSpaces); // break; // case Generator.RIGHT: // bytesToWrite = bytesSpaces.concat(bytesToWrite); // break; // case Generator.CENTER: // bytesToWrite = bytesToWrite.concat(bytesSpaces); // break; // } // // allocated = allocated.concat(bytesToWrite); // // let postfix = []; // if (item.postfix) { // postfix = item.postfix; // } // // allocated = allocated.concat(postfix); // // if (!item.__text_array.length) { // done_counter++; // } // }); // // // Check if there's still some text to be written // if (done_counter === values.length) { // done = true; // } // // command = command.concat(allocated); // } // } // // return this._rightModality(command); // } align(where: string | number) { let byte = null; if (where === Generator.LEFT) { byte = 0x00; } else if (where === Generator.RIGHT) { byte = 0x02; } else if (where === Generator.CENTER) { byte = 0x01; } if (byte !== null) { const bytes = [ASCII.ESC, 0x61, byte]; return this._rightModality(bytes); } else { return this._rightModality([]); } } cutPaper(modality = Generator.CUT_PAPER_FULL, feed = 0x00) { let byte = null; if (modality === Generator.CUT_PAPER_FULL) { byte = 0x41; } else if (modality === Generator.CUT_PAPER_PARTIAL) { byte = 0x42; } if (byte !== null) { const bytes = [ASCII.GS, 0x56, byte, feed]; return this._rightModality(bytes); } else { return this._rightModality([]); } } bold(on = true) { let byte = 0x00; if (on) { byte = 0x01; } const bytes = [ASCII.ESC, 0x45, byte]; return this._rightModality(bytes); } font(n = Generator.FONT_NORMAL) { const bytes = [ASCII.GS, 0x21, n]; this.font_size = n; return this._rightModality(bytes); } lineHeight(n = 3) { const bytes = [27, 51, n]; return this._rightModality(bytes); } // raw(array) { // return this._rightModality(array); // } toArray(united = false) { if (united) { return [].concat.apply([], this.commands); } else { return this.commands; } } _rightModality(command: Array | string) { if (this.modality === Generator.MODALITY_CLASS) { if (command.length) { this.commands.push(command); } return this; } else if (this.modality === Generator.MODALITY_ARRAY) { if (command.length) { this.commands.push(command); } return command; } else if (this.modality === Generator.MODALITY_DIRECT) { return command; } } _textToBytes(text: string) { if (!text) return []; return Array.from(iconv.encode(text, this._page_table)); } // Getters get maxCharactersPerLine() { return this._max_characters_per_line; } // Static variables static get MODALITY_CLASS() { return 'class'; } static get MODALITY_ARRAY() { return 'array'; } static get MODALITY_DIRECT() { return 'direct'; } static get LEFT() { return 'left'; } static get CENTER() { return 'center'; } static get RIGHT() { return 'right'; } static get CUT_PAPER_PARTIAL() { return 'partial'; } static get CUT_PAPER_FULL() { return 'full'; } static get FONT_NORMAL() { return 0b00000000; } static get FONT_DOUBLE() { return 0b00010001; } static get FONT_TRIPLE() { return 0b00100010; } }