diff --git a/package.json b/package.json index fa6eda9..9c8593b 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,7 @@ { "name": "@taoya7/print-help", - "version": "1.0.7", + "version": "1.0.12", "license": "MIT", - "type": "module", "keywords": [ "佳博打印机", "打印机" diff --git a/src/command/Label.ts b/src/command/Label.ts index 8d81b1b..397ccd6 100644 --- a/src/command/Label.ts +++ b/src/command/Label.ts @@ -1,5 +1,6 @@ import _ from "lodash-es"; -import iconv from "iconv-lite"; +import { Buffer } from 'buffer' + export enum FOOT { F2 = 0, F5 = 1, @@ -169,21 +170,21 @@ export class LabelCommand { public clrCommand() { this.command = []; } - private addStrToCommand(str: string): void { + addStrToCommand(str: string): void { if (str !== "") { - let bs; - try { - bs = iconv.encode(str, "GB2312"); - } catch (e) { - console.error(e); - } - if (bs) { - for (let i = 0; i < bs.length; ++i) { - this.command.push(bs[i]); - } + const bs = Buffer.from(str, "utf8"); + for (let i = 0; i < bs.length; ++i) { + this.command.push(bs[i]); } } } + /** + * 追加编码 + * @param number + */ + public addToCommand(number: number[]) { + this.command = this.command.concat(number); + } /** * 定义两张标签之间的间隙距离 * @param gap 定义标签间隙高度 (mm) @@ -488,8 +489,7 @@ export class LabelCommand { // 创建一个Buffer实例来存储字节数组 const buffer = Buffer.from(command); - // 使用iconv-lite将Buffer从GB2312解码成字符串 - const str = iconv.decode(buffer, "GB2312"); + const str = buffer.toString('utf8'); // 指定utf8编码 return str; } diff --git a/src/index.ts b/src/index.ts index f4c8ea9..7420c9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,26 @@ export * from "./command"; + +// import { +// FONTMUL, +// FONTTYPE, +// LabelCommand, +// ROTATION, +// } from "@/command/Label"; + +// function main() { +// let label = new LabelCommand(50, 30, 0); // 长,宽,gap +// label.addText( +// 0, +// 0, +// FONTTYPE.FONT_1, +// ROTATION.ROTATION_0, +// FONTMUL.MUL_1, +// FONTMUL.MUL_1, +// "中国" +// ); +// label.addToCommand([1, 2, 3]); +// var command = label.getCommand(); +// console.log(command); // 获取字节码 +// console.log(label.getCommandStr(command)); +// } +// main();