feat: 中文

This commit is contained in:
陶林 2024-03-13 22:44:10 +08:00
parent 922d82e27c
commit 42af6d8906
4 changed files with 63 additions and 46 deletions

View File

@ -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"
}

View File

@ -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

View File

@ -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,

View File

@ -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();