From 42af6d89066383c90f0f4920bc7e422c3ea2ee48 Mon Sep 17 00:00:00 2001 From: taoya7 <569781231@qq.com> Date: Wed, 13 Mar 2024 22:44:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +++- pnpm-lock.yaml | 32 ++++++++++++++++++++++++++++++ src/command/Label.ts | 47 ++++++++++++++++++++++++++------------------ src/index.ts | 26 ------------------------ 4 files changed, 63 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index 0464369..fa6eda9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@taoya7/print-help", - "version": "1.0.4", + "version": "1.0.7", "license": "MIT", "type": "module", "keywords": [ @@ -26,10 +26,12 @@ "devDependencies": { "@types/lodash-es": "^4.17.12", "@types/node": "^20.11.25", + "buffer": "^6.0.3", "typescript": "^5.4.2", "unbuild": "^2.0.0" }, "dependencies": { + "iconv-lite": "^0.6.3", "lodash-es": "^4.17.21", "rollup-obfuscator": "^4.1.1" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a680820..26d2ffd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + iconv-lite: + specifier: ^0.6.3 + version: 0.6.3 lodash-es: specifier: ^4.17.21 version: 4.17.21 @@ -19,6 +22,9 @@ devDependencies: '@types/node': specifier: ^20.11.25 version: 20.11.25 + buffer: + specifier: ^6.0.3 + version: 6.0.3 typescript: specifier: ^5.4.2 version: 5.4.2 @@ -711,6 +717,10 @@ packages: /balanced-match@1.0.2: resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==} + /base64-js@1.5.1: + resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} + dev: true + /boolbase@1.0.0: resolution: {integrity: sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww==} dev: true @@ -750,6 +760,13 @@ packages: resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} dev: false + /buffer@6.0.3: + resolution: {integrity: sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==} + dependencies: + base64-js: 1.5.1 + ieee754: 1.2.1 + dev: true + /builtin-modules@3.3.0: resolution: {integrity: sha512-zhaCDicdLuWN5UbN5IMnFqNMhNfo919sH85y2/ea+5Yg9TsTkeZxpL+JLbp6cgYFS4sRLp3YV4S6yDuqVWHYOw==} engines: {node: '>=6'} @@ -1351,6 +1368,17 @@ packages: resolution: {integrity: sha512-Yc+BQe8SvoXH1643Qez1zqLRmbA5rCL+sSmk6TVos0LWVfNIB7PGncdlId77WzLGSIB5KaWgTaNTs2lNVEI6VQ==} dev: true + /iconv-lite@0.6.3: + resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} + engines: {node: '>=0.10.0'} + dependencies: + safer-buffer: 2.1.2 + dev: false + + /ieee754@1.2.1: + resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==} + dev: true + /ignore@5.3.1: resolution: {integrity: sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw==} engines: {node: '>= 4'} @@ -2145,6 +2173,10 @@ packages: queue-microtask: 1.2.3 dev: true + /safer-buffer@2.1.2: + resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==} + dev: false + /scule@1.3.0: resolution: {integrity: sha512-6FtHJEvt+pVMIB9IBY+IcCJ6Z5f1iQnytgyfKMhDKgmzYG+TeH/wx1y3l27rshSbLiSanrR9ffZDrEsmjlQF2g==} dev: true diff --git a/src/command/Label.ts b/src/command/Label.ts index 3b19016..8d81b1b 100644 --- a/src/command/Label.ts +++ b/src/command/Label.ts @@ -1,5 +1,5 @@ import _ from "lodash-es"; - +import iconv from "iconv-lite"; export enum FOOT { F2 = 0, F5 = 1, @@ -171,9 +171,16 @@ export class LabelCommand { } private addStrToCommand(str: string): void { if (str !== "") { - let bs: Buffer = Buffer.from(str, "utf8"); - for (let i = 0; i < bs.length; ++i) { - this.command.push(bs[i]); + 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]); + } } } } @@ -219,7 +226,7 @@ export class LabelCommand { } /** * 设定打印机的打印速度 - * @param speed + * @param speed */ addSpeed(speed: SPEED): void { let str: string = `SPEED ${speed / 10}\r\n`; @@ -227,7 +234,7 @@ export class LabelCommand { } /** * 设定打印机的打印浓度 - * @param density + * @param density */ addDensity(density: DENSITY): void { let str: string = `DENSITY ${density}\r\n`; @@ -236,7 +243,7 @@ export class LabelCommand { /** * 设定打印方向,此设定将被记录于 EEPROM * @param direction 0-翻转 1-顺序 - * @param mirror + * @param mirror */ addDirection(direction: DIRECTION, mirror: MIRROR): void { let str: string = `DIRECTION ${direction}, ${mirror}\r\n`; @@ -378,7 +385,7 @@ export class LabelCommand { } /** * 添加自定义指令 - * @param command 指令字符串 + * @param command 指令字符串 */ addUserCommand(command: string): void { this.addStrToCommand(command); @@ -478,10 +485,12 @@ export class LabelCommand { * 获取命令字符串 */ getCommandStr(command: number[]) { - let str = ""; - for (let i = 0; i < command.length; i++) { - str += String.fromCharCode(command[i]); - } + // 创建一个Buffer实例来存储字节数组 + const buffer = Buffer.from(command); + + // 使用iconv-lite将Buffer从GB2312解码成字符串 + const str = iconv.decode(buffer, "GB2312"); + return str; } addQueryPrinterType(): void { @@ -544,13 +553,13 @@ export class LabelCommand { * 印制一维条码, * @param x 明定条码左上角 X 座标 * @param y 明定条码左上角 Y 座标 - * @param type - * @param height - * @param readable - * @param rotation - * @param narrow - * @param width - * @param content + * @param type + * @param height + * @param readable + * @param rotation + * @param narrow + * @param width + * @param content */ add1DBarcode( x: number, diff --git a/src/index.ts b/src/index.ts index f477457..f4c8ea9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,27 +1 @@ export * from "./command"; - -import { - FONTMUL, - FONTTYPE, - LabelCommand, - ROTATION, - EEC, -} 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, - "Hello World" - ); - label.addQRCode(0, 0, EEC.LEVEL_L, 5, ROTATION.ROTATION_0, "taoya.art"); - var command = label.getCommand(); - console.log(command); // 获取字节码 - console.log(label.getCommandStr(command)); -} -main();