Skip to content

Commit d1b6286

Browse files
authored
Merge pull request #10 from node-escpos/develop
Develop
2 parents 83a0740 + 3e94bc9 commit d1b6286

File tree

10 files changed

+103
-62
lines changed

10 files changed

+103
-62
lines changed

.changeset/lovely-hairs-stare.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@node-escpos/core": patch
3+
"@node-escpos/usb-adapter": patch
4+
---
5+
6+
🐛 Solved a bug that printing images is not correct.
7+
🧾 Update demo of README.

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<br/>
88
<br/>
99

10-
> It is a fork of [node-escpos](https://github.com/song940/node-escpos) with some improvements. Thanks to the original [author](https://github.com/song940). And I'll bring more improvements in the future.
10+
> It is a fork of [node-escpos](https://github.com/node-escpos/driver) with some improvements. Thanks to the original [author](https://github.com/song940). And I'll bring more improvements in the future.
1111
1212
### Improvements
1313
- 🛠 It is rewritten in TypeScript.
@@ -42,15 +42,26 @@ const device = new USB();
4242
const options = { encoding: "GB18030" /* default */ }
4343
const printer = new Printer(device, options);
4444

45-
device.open(function(error){
45+
device.open(async function(err){
46+
if(err){
47+
// handle error
48+
return
49+
}
50+
51+
let printer = new Printer(device, {});
52+
53+
// Path to png image
54+
const tux = join();
55+
const image = await Image.load(tux);
56+
4657
printer
4758
.font("a")
4859
.align("ct")
4960
.style("bu")
5061
.size(1, 1)
5162
.text("The quick brown fox jumps over the lazy dog")
5263
.text("敏捷的棕色狐狸跳过懒狗")
53-
.barcode(1234567, "EAN13", { width: 50, height: 50 })
64+
.barcode(112233445566, "EAN13", { width: 50, height: 50 })
5465
.table(["One", "Two", "Three"])
5566
.tableCustom(
5667
[
@@ -60,11 +71,15 @@ device.open(function(error){
6071
],
6172
{ encoding: "cp857", size: [1, 1] }, // Optional
6273
)
63-
.qrimage("https://github.com/song940/node-escpos")
64-
.then((printer) => {
65-
printer.cut();
66-
printer.close();
67-
});
74+
75+
// inject qrimage to printer
76+
printer = await printer.qrimage("https://github.com/node-escpos/driver")
77+
// inject image to printer
78+
printer = await printer.image(image, "s8")
79+
80+
printer
81+
.cut()
82+
.close()
6883
});
6984
````
7085
- See `./examples/demo` for more examples.

examples/demo/test/barcode.test.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
1-
import { Printer } from "@node-escpos/core";
1+
import { Image, Printer } from "@node-escpos/core";
22
import USB from "@node-escpos/usb-adapter";
3+
import { join } from "path";
34
import { describe, it } from "vitest";
45

56
describe("should work as expected", () => {
67
it("printing", async () => {
7-
const device = new USB();
8-
const printer = new Printer(device, {});
8+
const device = new USB();
9+
await new Promise<void>((resolve,reject) => {
10+
device.open(async function(err){
11+
if(err){
12+
reject(err);
13+
return
14+
}
915

10-
await new Promise<void>((resolve) => {
11-
printer
12-
.font("a")
13-
.align("ct")
14-
.style("bu")
15-
.size(1, 1)
16-
.text("The quick brown fox jumps over the lazy dog")
17-
.text("敏捷的棕色狐狸跳过懒狗")
18-
.barcode(1234567, "EAN13", { width: 50, height: 50 })
19-
.table(["One", "Two", "Three"])
20-
.tableCustom(
21-
[
22-
{ text: "Left", align: "LEFT", width: 0.33, style: "B" },
23-
{ text: "Center", align: "CENTER", width: 0.33 },
24-
{ text: "Right", align: "RIGHT", width: 0.33 },
25-
],
26-
{ encoding: "cp857", size: [1, 1] }, // Optional
27-
)
28-
.qrimage("https://github.com/song940/node-escpos")
29-
.then((printer) => {
30-
printer.cut();
31-
printer.close();
32-
}).finally(() => {
33-
resolve();
34-
});
16+
let printer = new Printer(device, {});
17+
18+
const tux = join(__dirname, '../assets/tux.png');
19+
const image = await Image.load(tux);
20+
21+
printer
22+
.font("a")
23+
.align("ct")
24+
.style("bu")
25+
.size(1, 1)
26+
.text("The quick brown fox jumps over the lazy dog")
27+
.text("敏捷的棕色狐狸跳过懒狗")
28+
.barcode(112233445566, "EAN13", { width: 50, height: 50 })
29+
.table(["One", "Two", "Three"])
30+
.tableCustom(
31+
[
32+
{ text: "Left", align: "LEFT", width: 0.33, style: "B" },
33+
{ text: "Center", align: "CENTER", width: 0.33 },
34+
{ text: "Right", align: "RIGHT", width: 0.33 },
35+
],
36+
{ encoding: "cp857", size: [1, 1] }, // Optional
37+
)
38+
39+
// inject qrimage to printer
40+
printer = await printer.qrimage("https://github.com/node-escpos/driver")
41+
// inject image to printer
42+
printer = await printer.image(image, "s8")
43+
44+
printer
45+
.cut()
46+
.close()
47+
.finally(resolve)
48+
});
3549
});
3650
});
3751
});

examples/demo/test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ device.open(function(err){
1717
.text('The quick brown fox jumps over the lazy dog')
1818
.text('敏捷的棕色狐狸跳过懒狗')
1919
.barcode('1234567', 'EAN8')
20-
.qrimage('https://github.com/song940/node-escpos', function(err){
20+
.qrimage('https://github.com/node-escpos/driver', function(err){
2121
this.cut();
2222
this.close();
2323
});

examples/demo/test/multiple.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ device.open(function(){
2424
.text('The quick brown fox jumps over the lazy dog')
2525
.text('敏捷的棕色狐狸跳过懒狗')
2626
.barcode('12345678', 'EAN8')
27-
.qrimage('https://github.com/song940/node-escpos', function(err){
27+
.qrimage('https://github.com/node-escpos/driver', function(err){
2828
this.cut();
2929
this.close();
3030
});

examples/demo/test/qs_text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ device.open(function(err){
2020
.close();
2121
// .text('敏捷的棕色狐狸跳过懒狗')
2222
// .barcode('1234567', 'EAN8')
23-
// .qrimage('https://github.com/song940/node-escpos', function(err){
23+
// .qrimage('https://github.com/node-escpos/driver', function(err){
2424
// this.cut();
2525
// this.close();
2626
// });

examples/demo/test/star_printer_index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ device.open(function(err){
2020
.text('敏捷的棕色狐狸跳过懒狗')
2121
.align("STAR_RA")
2222
.barcode('1234567', 'EAN8')
23-
.qrimage('https://github.com/song940/node-escpos', function(err){
23+
.qrimage('https://github.com/node-escpos/driver', function(err){
2424
this.fullCut()
2525
this.close();
2626
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"author": "Caspian <[email protected]> (https://github.com/dohooo)",
55
"scripts": {
66
"build": "pnpm -F '@node-escpos/*' build",
7+
"build:watch": "pnpm -F '@node-escpos/*' build:watch",
78
"publish": "pnpm run build && changeset publish",
89
"version": "changeset version"
910
},

packages/core/src/image.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,25 @@ export default class Image {
2222
);
2323
}
2424

25-
this.data = rgbaData.map(
26-
([r, g, b, a]) => a !== 0 && r > 200 && g > 200 && b > 200,
27-
);
25+
// Convert RGB to grayscale:
26+
this.data = rgbaData.map(([r, g, b, a]) => this.RGB2Gray(r, g, b, a));
2827
}
2928

30-
private get size() {
29+
get size() {
3130
return {
3231
width: this.pixels.shape[0],
3332
height: this.pixels.shape[1],
3433
colors: this.pixels.shape[2],
3534
};
3635
}
3736

38-
/**
39-
* [toBitmap description]
40-
* @param {[type]} density [description]
41-
* @return {[type]} [description]
42-
*/
37+
private RGB2Gray(r: number, g: number, b: number, a: number) {
38+
if (a === 0) {
39+
return false;
40+
}
41+
return 0.29900 * r + 0.57800 * g + 0.11400 * b < 128;
42+
}
43+
4344
toBitmap(density = 24) {
4445
const result: number[][] = [];
4546
let x, y, b, l, i;
@@ -56,12 +57,13 @@ export default class Image {
5657
for (b = 0; b < density; b++) {
5758
i = x * c + (b >> 3);
5859

59-
if (ld[i] === undefined) ld[i] = 0;
60+
if (ld[i] === undefined) { ld[i] = 0; }
6061

6162
l = y * density + b;
6263
if (l < this.size.height) {
63-
if (this.data[l * this.size.width + x])
64+
if (this.data[l * this.size.width + x]) {
6465
ld[i] += (0x80 >> (b & 0x7));
66+
}
6567
}
6668
}
6769
}
@@ -73,12 +75,8 @@ export default class Image {
7375
};
7476
}
7577

76-
/**
77-
* [toRaster description]
78-
* @return {[type]} [description]
79-
*/
8078
toRaster() {
81-
const result = [];
79+
const result: number[] = [];
8280
const { width, height } = this.size;
8381

8482
// n blocks of lines
@@ -89,13 +87,15 @@ export default class Image {
8987
for (let b = 0; b < 8; b++) {
9088
const i = x * 8 + b;
9189

92-
if (result[y * n + x] === undefined)
90+
if (result[y * n + x] === undefined) {
9391
result[y * n + x] = 0;
92+
}
9493

9594
const c = x * 8 + b;
9695
if (c < width) {
97-
if (this.data[y * width + i])
96+
if (this.data[y * width + i]) {
9897
result[y * n + x] += (0x80 >> (b & 0x7));
98+
}
9999
}
100100
}
101101
}
@@ -113,11 +113,15 @@ export default class Image {
113113
* @param {[type]} type [description]
114114
* @return {[Promise<Image>]} [description]
115115
*/
116-
static load(url: string, type: ImageMimeType | null = null): Promise<Image> {
116+
static load(url: string | Uint8Array, type: ImageMimeType | null = null): Promise<Image> {
117117
return new Promise((resolve, reject) => {
118118
getPixels(url, type ?? "", (error, pixels) => {
119-
if (error) reject(error);
120-
else resolve(new Image(pixels as NdArray<Uint8Array>));
119+
if (error) {
120+
reject(error);
121+
}
122+
else {
123+
resolve(new Image(pixels as NdArray<Uint8Array>));
124+
}
121125
});
122126
});
123127
}

packages/usb-adapter/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default class USBAdapter extends Adapter<[]> {
148148
}
149149

150150
close(callback?: ((error: Error | null) => void) | undefined): this {
151-
if (!this.device) callback && callback(null);
151+
if (!this.device) callback?.(new Error("Device not found"));
152152
try {
153153
this.device?.close();
154154
usb.removeAllListeners("detach");

0 commit comments

Comments
 (0)