Skip to content

Commit

Permalink
fix: Solved a bug that printing images is not correct
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Oct 24, 2022
1 parent ab0b1b7 commit bc7e2e5
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 62 deletions.
7 changes: 7 additions & 0 deletions .changeset/lovely-hairs-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@node-escpos/core": patch
"@node-escpos/usb-adapter": patch
---

🐛 Solved a bug that printing images is not correct.
🧾 Update demo of README.
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<br/>
<br/>

> 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.
> 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.
### Improvements
- 🛠 It is rewritten in TypeScript.
Expand Down Expand Up @@ -42,15 +42,26 @@ const device = new USB();
const options = { encoding: "GB18030" /* default */ }
const printer = new Printer(device, options);

device.open(function(error){
device.open(async function(err){
if(err){
// handle error
return
}

let printer = new Printer(device, {});

// Path to png image
const tux = join();
const image = await Image.load(tux);

printer
.font("a")
.align("ct")
.style("bu")
.size(1, 1)
.text("The quick brown fox jumps over the lazy dog")
.text("敏捷的棕色狐狸跳过懒狗")
.barcode(1234567, "EAN13", { width: 50, height: 50 })
.barcode(112233445566, "EAN13", { width: 50, height: 50 })
.table(["One", "Two", "Three"])
.tableCustom(
[
Expand All @@ -60,11 +71,15 @@ device.open(function(error){
],
{ encoding: "cp857", size: [1, 1] }, // Optional
)
.qrimage("https://github.com/song940/node-escpos")
.then((printer) => {
printer.cut();
printer.close();
});

// inject qrimage to printer
printer = await printer.qrimage("https://github.com/node-escpos/driver")
// inject image to printer
printer = await printer.image(image, "s8")

printer
.cut()
.close()
});
````
- See `./examples/demo` for more examples.
Expand Down
70 changes: 42 additions & 28 deletions examples/demo/test/barcode.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
import { Printer } from "@node-escpos/core";
import { Image, Printer } from "@node-escpos/core";
import USB from "@node-escpos/usb-adapter";
import { join } from "path";
import { describe, it } from "vitest";

describe("should work as expected", () => {
it("printing", async () => {
const device = new USB();
const printer = new Printer(device, {});
const device = new USB();
await new Promise<void>((resolve,reject) => {
device.open(async function(err){
if(err){
reject(err);
return
}

await new Promise<void>((resolve) => {
printer
.font("a")
.align("ct")
.style("bu")
.size(1, 1)
.text("The quick brown fox jumps over the lazy dog")
.text("敏捷的棕色狐狸跳过懒狗")
.barcode(1234567, "EAN13", { width: 50, height: 50 })
.table(["One", "Two", "Three"])
.tableCustom(
[
{ text: "Left", align: "LEFT", width: 0.33, style: "B" },
{ text: "Center", align: "CENTER", width: 0.33 },
{ text: "Right", align: "RIGHT", width: 0.33 },
],
{ encoding: "cp857", size: [1, 1] }, // Optional
)
.qrimage("https://github.com/song940/node-escpos")
.then((printer) => {
printer.cut();
printer.close();
}).finally(() => {
resolve();
});
let printer = new Printer(device, {});

const tux = join(__dirname, '../assets/tux.png');
const image = await Image.load(tux);

printer
.font("a")
.align("ct")
.style("bu")
.size(1, 1)
.text("The quick brown fox jumps over the lazy dog")
.text("敏捷的棕色狐狸跳过懒狗")
.barcode(112233445566, "EAN13", { width: 50, height: 50 })
.table(["One", "Two", "Three"])
.tableCustom(
[
{ text: "Left", align: "LEFT", width: 0.33, style: "B" },
{ text: "Center", align: "CENTER", width: 0.33 },
{ text: "Right", align: "RIGHT", width: 0.33 },
],
{ encoding: "cp857", size: [1, 1] }, // Optional
)

// inject qrimage to printer
printer = await printer.qrimage("https://github.com/node-escpos/driver")
// inject image to printer
printer = await printer.image(image, "s8")

printer
.cut()
.close()
.finally(resolve)
});
});
});
});
2 changes: 1 addition & 1 deletion examples/demo/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ device.open(function(err){
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('1234567', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
.qrimage('https://github.com/node-escpos/driver', function(err){
this.cut();
this.close();
});
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/test/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ device.open(function(){
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
.qrimage('https://github.com/node-escpos/driver', function(err){
this.cut();
this.close();
});
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/test/qs_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ device.open(function(err){
.close();
// .text('敏捷的棕色狐狸跳过懒狗')
// .barcode('1234567', 'EAN8')
// .qrimage('https://github.com/song940/node-escpos', function(err){
// .qrimage('https://github.com/node-escpos/driver', function(err){
// this.cut();
// this.close();
// });
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/test/star_printer_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ device.open(function(err){
.text('敏捷的棕色狐狸跳过懒狗')
.align("STAR_RA")
.barcode('1234567', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
.qrimage('https://github.com/node-escpos/driver', function(err){
this.fullCut()
this.close();
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"author": "Caspian <[email protected]> (https://github.com/dohooo)",
"scripts": {
"build": "pnpm -F '@node-escpos/*' build",
"build:watch": "pnpm -F '@node-escpos/*' build:watch",
"publish": "pnpm run build && changeset publish",
"version": "changeset version"
},
Expand Down
46 changes: 25 additions & 21 deletions packages/core/src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ export default class Image {
);
}

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

private get size() {
get size() {
return {
width: this.pixels.shape[0],
height: this.pixels.shape[1],
colors: this.pixels.shape[2],
};
}

/**
* [toBitmap description]
* @param {[type]} density [description]
* @return {[type]} [description]
*/
private RGB2Gray(r: number, g: number, b: number, a: number) {
if (a === 0) {
return false;
}
return 0.29900 * r + 0.57800 * g + 0.11400 * b < 128;
}

toBitmap(density = 24) {
const result: number[][] = [];
let x, y, b, l, i;
Expand All @@ -56,12 +57,13 @@ export default class Image {
for (b = 0; b < density; b++) {
i = x * c + (b >> 3);

if (ld[i] === undefined) ld[i] = 0;
if (ld[i] === undefined) { ld[i] = 0; }

l = y * density + b;
if (l < this.size.height) {
if (this.data[l * this.size.width + x])
if (this.data[l * this.size.width + x]) {
ld[i] += (0x80 >> (b & 0x7));
}
}
}
}
Expand All @@ -73,12 +75,8 @@ export default class Image {
};
}

/**
* [toRaster description]
* @return {[type]} [description]
*/
toRaster() {
const result = [];
const result: number[] = [];
const { width, height } = this.size;

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

if (result[y * n + x] === undefined)
if (result[y * n + x] === undefined) {
result[y * n + x] = 0;
}

const c = x * 8 + b;
if (c < width) {
if (this.data[y * width + i])
if (this.data[y * width + i]) {
result[y * n + x] += (0x80 >> (b & 0x7));
}
}
}
}
Expand All @@ -113,11 +113,15 @@ export default class Image {
* @param {[type]} type [description]
* @return {[Promise<Image>]} [description]
*/
static load(url: string, type: ImageMimeType | null = null): Promise<Image> {
static load(url: string | Uint8Array, type: ImageMimeType | null = null): Promise<Image> {
return new Promise((resolve, reject) => {
getPixels(url, type ?? "", (error, pixels) => {
if (error) reject(error);
else resolve(new Image(pixels as NdArray<Uint8Array>));
if (error) {
reject(error);
}
else {
resolve(new Image(pixels as NdArray<Uint8Array>));
}
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/usb-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class USBAdapter extends Adapter<[]> {
}

close(callback?: ((error: Error | null) => void) | undefined): this {
if (!this.device) callback && callback(null);
if (!this.device) callback?.(new Error("Device not found"));
try {
this.device?.close();
usb.removeAllListeners("detach");
Expand Down

0 comments on commit bc7e2e5

Please sign in to comment.