Skip to content

Commit d602792

Browse files
authored
Merge pull request #48 from grandchef/refactor/decoupling-image-and-qrcode-libs
refactor: decoupling image and qrcode libs
2 parents 5837424 + c50e002 commit d602792

23 files changed

+467
-118
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ node_modules/
99
# Coverage
1010
coverage
1111

12-
# VS Code
13-
.vscode
14-
!.vscode/tasks.js
15-
1612
# JetBrains IDEs
1713
.idea/
1814

.vscode/launch.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Test and Watch",
9+
"type": "node",
10+
"request": "launch",
11+
"cwd": "${workspaceFolder}",
12+
"runtimeExecutable": "yarn",
13+
"runtimeArgs": [
14+
"test:debug"
15+
],
16+
"port": 9229
17+
}
18+
]
19+
}

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
}

__tests__/Printer.spec.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import { ImageManager } from 'escpos-buffer-image';
24
import { Image } from '../src';
35
import { Capability } from '../src/capabilities';
46
import InMemory from '../src/connection/InMemory';
@@ -127,8 +129,16 @@ describe('print formatted text', () => {
127129

128130
it('draw picture from file', async () => {
129131
const connection = new InMemory();
130-
const printer = await Printer.CONNECT('MP-4200 TH', connection);
131-
const image = new Image(path.join(__dirname, 'resources/sample.png'));
132+
const imageManager = new ImageManager();
133+
const printer = await Printer.CONNECT(
134+
'MP-4200 TH',
135+
connection,
136+
imageManager,
137+
);
138+
const imageData = await imageManager.loadImage(
139+
path.join(__dirname, 'resources/sample.png'),
140+
);
141+
const image = new Image(imageData);
132142
await printer.setAlignment(Align.Center);
133143
await printer.draw(image);
134144
await printer.setAlignment(Align.Left);
@@ -139,8 +149,18 @@ describe('print formatted text', () => {
139149

140150
it('draw picture from buffer', async () => {
141151
const connection = new InMemory();
142-
const printer = await Printer.CONNECT('MP-4200 TH', connection);
143-
const image = new Image(load('sample.png'));
152+
const imageManager = new ImageManager();
153+
const printer = await Printer.CONNECT(
154+
'MP-4200 TH',
155+
connection,
156+
imageManager,
157+
);
158+
// tslint:disable: non-literal-fs-path
159+
const imageBuffer = fs.readFileSync(
160+
path.join(__dirname, 'resources/sample.png'),
161+
);
162+
const imageData = await imageManager.loadImageFromBuffer(imageBuffer);
163+
const image = new Image(imageData);
144164
await printer.setAlignment(Align.Center);
145165
await printer.draw(image);
146166
await printer.setAlignment(Align.Left);

__tests__/graphics/Image.spec.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
1-
import { load } from '../helper';
1+
import * as path from 'path';
2+
import { ImageManager } from 'escpos-buffer-image';
3+
24
import { Image } from '../../src';
3-
import { PNG } from '../../src';
45

56
describe('proccess images to printing format', () => {
6-
it('load image from buffer', () => {
7-
const image = new Image(load('sample.png'));
7+
it('load image from buffer', async () => {
8+
const imageManager = new ImageManager();
9+
const imageData = await imageManager.loadImage(
10+
path.join(__dirname, '../resources/sample.png'),
11+
);
12+
const image = new Image(imageData);
813
expect(image.width).toBe(180);
914
});
1015

11-
it('allow image cache', () => {
12-
const cache = new Image(load('sample.png'));
13-
const image = new Image();
16+
it('allow image cache', async () => {
17+
const imageManager = new ImageManager();
18+
const imageDataCache = await imageManager.loadImage(
19+
path.join(__dirname, '../resources/sample.png'),
20+
);
21+
const imageData = await imageManager.loadImage(
22+
path.join(__dirname, '../resources/transparent_sample.png'),
23+
);
24+
const cache = new Image(imageDataCache);
25+
const image = new Image(imageData);
1426
Object.assign(image, cache);
1527
expect(image.width).toBe(180);
1628
});
17-
18-
it('accepts a pre-loaded PNG instance', async () => {
19-
const imageBuffer = load('sample.png');
20-
21-
const png = await new Promise((resolve: Function, reject: Function) => {
22-
new PNG({ filterType: 4 }).parse(
23-
imageBuffer,
24-
(error: Error, data: PNG) => {
25-
if (error) {
26-
reject(error);
27-
return;
28-
}
29-
resolve(data);
30-
},
31-
);
32-
});
33-
34-
const image = new Image(png as PNG);
35-
expect(image.width).toBe(180);
36-
});
3729
});
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import * as path from 'path';
2+
import { ImageManager } from 'escpos-buffer-image';
13
import { load } from '../../helper';
24
import { BayerOrdered, Image } from '../../../src';
35

46
describe('proccess images using Bayer ordered algorithm', () => {
5-
it('apply filter on image from buffer', () => {
6-
const image = new Image(load('sample.png'), new BayerOrdered());
7+
it('apply filter on image from buffer', async () => {
8+
const imageManager = new ImageManager();
9+
const imageData = await imageManager.loadImage(
10+
path.join(__dirname, '../../resources/sample.png'),
11+
);
12+
const image = new Image(imageData, new BayerOrdered());
713
expect(image.data).toStrictEqual(load('bayer_filter', image.data));
814
});
915
});

__tests__/graphics/filter/FloydSteinberg.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import * as path from 'path';
2+
import { ImageManager } from 'escpos-buffer-image';
13
import { load } from '../../helper';
24
import { FloydSteinberg, Image } from '../../../src';
35

46
describe('proccess images using Floyd and Steinberg algorithm', () => {
5-
it('apply filter on image from buffer', () => {
6-
const image = new Image(load('sample.png'), new FloydSteinberg());
7+
it('apply filter on image from buffer', async () => {
8+
const imageManager = new ImageManager();
9+
const imageData = await imageManager.loadImage(
10+
path.join(__dirname, '../../resources/sample.png'),
11+
);
12+
const image = new Image(imageData, new FloydSteinberg());
713
expect(image.data).toStrictEqual(
814
load('floyd_steinberg_filter', image.data),
915
);
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import * as path from 'path';
2+
import { ImageManager } from 'escpos-buffer-image';
13
import { load } from '../../helper';
24
import { Threshold, Image } from '../../../src';
35

46
describe('proccess images using threshold algorithm', () => {
5-
it('apply filter on image from buffer', () => {
6-
const image = new Image(load('sample.png'), new Threshold());
7+
it('apply filter on image from buffer', async () => {
8+
const imageManager = new ImageManager();
9+
const imageData = await imageManager.loadImage(
10+
path.join(__dirname, '../../resources/sample.png'),
11+
);
12+
const image = new Image(imageData, new Threshold());
713
expect(image.data).toStrictEqual(load('threshold_filter', image.data));
814
});
915
});

__tests__/profile/ControliD.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import InMemory from '../../src/connection/InMemory';
22
import Printer from '../../src/Printer';
33
import { Align, Style } from '../../src/actions';
44
import { load } from '../helper';
5+
import { ImageManager } from 'escpos-buffer-image';
56

67
describe('controlid model profile', () => {
78
it('write bold text from model PrintiD', async () => {
@@ -42,9 +43,18 @@ describe('controlid model profile', () => {
4243
);
4344
});
4445

45-
it('draw qrcode from model PrintiD', async () => {
46+
it('draw qrcode from model PrintiD without a image manager', async () => {
4647
const connection = new InMemory();
4748
const printer = await Printer.CONNECT('PrintiD', connection);
49+
await expect(
50+
printer.qrcode('https://github.com/grandchef/escpos-buffer'),
51+
).rejects.toThrow('No image manager to draw qrcode');
52+
});
53+
54+
it('draw qrcode from model PrintiD', async () => {
55+
const connection = new InMemory();
56+
const imageManager = new ImageManager();
57+
const printer = await Printer.CONNECT('PrintiD', connection, imageManager);
4858
await printer.setAlignment(Align.Center);
4959
await printer.qrcode('https://github.com/grandchef/escpos-buffer');
5060
await printer.setAlignment(Align.Left);
@@ -55,7 +65,12 @@ describe('controlid model profile', () => {
5565

5666
it('draw qrcode from model PrintiD Touch', async () => {
5767
const connection = new InMemory();
58-
const printer = await Printer.CONNECT('PrintiD-Touch', connection);
68+
const imageManager = new ImageManager();
69+
const printer = await Printer.CONNECT(
70+
'PrintiD-Touch',
71+
connection,
72+
imageManager,
73+
);
5974
await printer.setAlignment(Align.Center);
6075
await printer.qrcode('https://github.com/grandchef/escpos-buffer');
6176
await printer.setAlignment(Align.Left);

__tests__/profile/Daruma.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as path from 'path';
2+
import { ImageManager } from 'escpos-buffer-image';
13
import InMemory from '../../src/connection/InMemory';
24
import Printer from '../../src/Printer';
35
import { Align, Style } from '../../src/actions';
@@ -49,8 +51,12 @@ describe('daruma model profile', () => {
4951

5052
it('draw picture from buffer from model DR800', async () => {
5153
const connection = new InMemory();
52-
const printer = await Printer.CONNECT('DR800', connection);
53-
const image = new Image(load('sample.png'));
54+
const imageManager = new ImageManager();
55+
const printer = await Printer.CONNECT('DR800', connection, imageManager);
56+
const imageData = await imageManager.loadImage(
57+
path.join(__dirname, '../resources/sample.png'),
58+
);
59+
const image = new Image(imageData);
5460
await printer.setAlignment(Align.Center);
5561
await printer.draw(image);
5662
await printer.setAlignment(Align.Left);

0 commit comments

Comments
 (0)