export default class TSPLGenerator { private commands: any[]; constructor() { this.commands = [] } init(sound=false) { this.commands = [] this.setSize() this.setSpeed() this.setDensity() this.setDirection() this.setReference(10, 0) this.setOffset() this.setShift() if(sound){ this.setSound(2,100) } this.setCls() return this } addCommand(content:string) { // 将指令转成数组装起 this.commands.push(content) return this } setSize(pageWidth = 48, pageHeight = 30) { // 设置页面大小 const data = `SIZE ${pageWidth} mm, ${pageHeight} mm` this.addCommand(data) } setSpeed(printSpeed = 5) { // 设置打印机速度 const data = `SPEED ${printSpeed}` this.addCommand(data) } setDensity(printDensity = 7) { // 设置打印机浓度 const data = `DENSITY ${printDensity}` this.addCommand(data) } setDirection(direction = 1) { // 设置打印方向 const data = `DIRECTION ${direction}` this.addCommand(data) } setReference(x = 0, y = 0) { // 设置坐标原点,与打印方向有关 const data = `REFERENCE ${x},${y}` this.addCommand(data) return this } setOffset(n = 0) { const data = `OFFSET ${n} mm` this.addCommand(data) } setShift(n = 0) { const data = `SHIFT ${n}` this.addCommand(data) } setSound(level = 1, interval = 300) { // 控制蜂鸣器 const data = `SOUND ${level},${interval}` this.addCommand(data) } setCls() { // 清除打印机缓存 const data = 'CLS' this.addCommand(data) } _text(content:string, size = 1, x = 0, y = 0) { this.setText(x, y, 'TSS24.BF2', 0, size, size, content) return this } setText(x:number, y:number, font:string, rotation:number, x_:number, y_:number, content:string) { // 打印文字 const data = `TEXT ${x},${y},"${font}",${rotation},${x_},${y_},"${content}"` this.addCommand(data) } _barCode(content:string, x:number, y:number, height:number) { this.setBarCode(x, y, '128', height, 1, 0, 2, 1, content) return this } setBarCode(x:number, y:number, codeType:string, height:number, readable:number, rotation:number, narrow:number, wide:number, content:string) { // 打印条形码 const data = `BARCODE ${x},${y},"${codeType}",${height},${readable},${rotation},${narrow},${wide},"${content}"` this.addCommand(data) } setGap(printGap:string) { // 传感器 const data = 'GAP ' + printGap.toString() + ' mm\r\n' this.addCommand(data) } setFeed(feed:number) { // 将纸向前推出n const data = 'FEED ' + feed + '\r\n' this.addCommand(data) } setBackFeed(backup:number) { // 将纸向后回拉n const data = 'BACKFEED ' + backup + '\r\n' this.addCommand(data) } setFromfeed() { // 根据Size进一张标签纸 const data = 'FORMFEED \r\n' this.addCommand(data) } setHome() { // 根据Size找到下一张标签纸的位置 const data = 'HOME \r\n' this.addCommand(data) } setLimitfeed(limit:number) { // 检测垂直间距 const data = 'LIMITFEED ' + limit + '\r\n' this.addCommand(data) } setBar(x:number, y:number, width:number, height:number) { // 绘制线条 const data = 'BAR ' + x + ',' + y + ',' + width + ',' + height + '\r\n' this.addCommand(data) } setBox(x_start:number, y_start:number, x_end:number, y_end:number, thickness:number) { // 绘制方框 const data = 'BOX ' + x_start + ',' + y_start + ',' + x_end + ',' + y_end + ',' + thickness + '\r\n' this.addCommand(data) } // setErase(x_start, y_start, x_width, y_height) { // 清除指定区域的数据 // const data = 'ERASE ' + x_start + ',' + y_start + ',' + x_width + ',' + y_height + '\r\n' // this.addCommand(data) // } // setReverse(x_start, y_start, x_width, y_height) { // 将指定的区域反相打印 // const data = 'REVERSE ' + x_start + ',' + y_start + ',' + x_width + ',' + y_height + '\r\n' // this.addCommand(data) // } // setQR(x, y, level, width, mode, content) { // 打印二维码 // const data = 'QRCODE ' + x + ',' + y + ',' + level + ',' + width + ',' + mode + ',' + 0 + ',"' + content + '"\r\n' // this.addCommand(data) // } // setBitmap(x, y, mode, res) { // let i // // 添加图片,res为画布参数 // console.log('setBitmap',res) // const width = parseInt(((res.width + 7) / 8 * 8 / 8).toString()); // const height = res.height; // let time = 1; // let temp = res.data.length - width * 32; // const pointList = []; // console.log(width + '--' + height) // const data = 'BITMAP ' + x + ',' + y + ',' + width + ',' + height + ',' + mode + ',' // this.addCommand(data) // for (i = 0; i < height; ++i) { // console.log(temp) // for (let j = 0; j < width; ++j) { // for (let k = 0; k < 32; k += 4) { // if (res.data[temp] == 0 && res.data[temp + 1] == 0 && res.data[temp + 2] == 0 && res.data[temp + 3] == 0) { // pointList.push(1) // } else { // pointList.push(0) // } // temp += 4 // } // } // time++ // temp = res.data.length - width * 32 * time // } // for (i = 0; i < pointList.length; i += 8) { // const p = pointList[i] * 128 + pointList[i + 1] * 64 + pointList[i + 2] * 32 + pointList[i + 3] * 16 + pointList[i + 4] * 8 + pointList[i + 5] * 4 + pointList[i + 6] * 2 + pointList[i + 7] // this.commands.push(p) // } // } setPrint(printNum:number) { // 打印页面 const data = `PRINT ${printNum},1` this.addCommand(data) } // 获取打印数据 getData() { return this.commands } }