escpos-buffer
Advanced tools
Comparing version 1.1.0 to 1.3.0
@@ -23,3 +23,3 @@ import WebUSB from '../../src/connection/WebUSB'; | ||
it('writes data', async () => { | ||
const buffer = new Buffer('hello'); | ||
const buffer = Buffer.from('hello'); | ||
const webUsb = new WebUSB(device); | ||
@@ -26,0 +26,0 @@ await webUsb.write(buffer); |
@@ -1,9 +0,37 @@ | ||
import { load } from '../helper' | ||
import { Image } from '../../src' | ||
import { load } from '../helper'; | ||
import { Image } from '../../src'; | ||
import { PNG } from 'pngjs'; | ||
describe('proccess images to printing format', () => { | ||
it('load image from buffer', () => { | ||
const image = new Image(load('sample.png')) | ||
expect(image.width).toBe(180) | ||
}) | ||
}) | ||
const image = new Image(load('sample.png')); | ||
expect(image.width).toBe(180); | ||
}); | ||
it('allow image cache', () => { | ||
const cache = new Image(load('sample.png')); | ||
const image = new Image(); | ||
Object.assign(image, cache); | ||
expect(image.width).toBe(180); | ||
}); | ||
it('accepts a pre-loaded PNG instance', async () => { | ||
const imageBuffer = load('sample.png'); | ||
const png = await new Promise((resolve: Function, reject: Function) => { | ||
new PNG({ filterType: 4 }).parse( | ||
imageBuffer, | ||
(error: Error, data: Buffer) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
resolve(data); | ||
}, | ||
); | ||
}); | ||
const image = new Image(png); | ||
expect(image.width).toBe(180); | ||
}); | ||
}); |
/// <reference types="node" /> | ||
import { Filter } from './filter'; | ||
import { PNG } from 'pngjs'; | ||
export default class Image { | ||
@@ -8,3 +9,3 @@ data: Buffer; | ||
bytesPerRow: number; | ||
constructor(input: string | Buffer, filter?: Filter); | ||
constructor(input?: string | Buffer | PNG, filter?: Filter); | ||
loadImage(filename: string, filter: Filter): void; | ||
@@ -11,0 +12,0 @@ loadImageData(data: Buffer, filter: Filter): void; |
@@ -7,16 +7,16 @@ "use strict"; | ||
class Image { | ||
constructor(input, filter = null) { | ||
const _filter = filter || new filter_1.FloydSteinberg(); | ||
if (typeof input === 'string') { | ||
this.loadImage(input, _filter); | ||
constructor(input = null, filter = new filter_1.FloydSteinberg()) { | ||
if (input instanceof pngjs_1.PNG) { | ||
this.readImage(filter.process(input)); | ||
} | ||
else { | ||
this.loadImageData(input, _filter); | ||
else if (typeof input === 'string') { | ||
this.loadImage(input, filter); | ||
} | ||
else if (input) { | ||
this.loadImageData(input, filter); | ||
} | ||
} | ||
loadImage(filename, filter) { | ||
const data = fs.readFileSync(filename); | ||
const png = pngjs_1.PNG.sync.read(data); | ||
const image = filter.process(png); | ||
this.readImage(image); | ||
this.loadImageData(data, filter); | ||
} | ||
@@ -23,0 +23,0 @@ loadImageData(data, filter) { |
{ | ||
"name": "escpos-buffer", | ||
"version": "1.1.0", | ||
"version": "1.3.0", | ||
"description": "Library to generate buffer for thermal printers.", | ||
@@ -5,0 +5,0 @@ "author": "GrandChef Team <desenvolvimento@grandchef.com.br>", |
@@ -44,2 +44,19 @@ [![TypeScript version][ts-badge]][typescript-37] | ||
## Usage in the browser | ||
You can also use the new WebUSB protocol [in Chrome](https://caniuse.com/webusb) to connect directly to the printer. | ||
```js | ||
import { Printer, Model, WebUSB } from 'escpos-buffer' | ||
const device = await navigator.usb.requestDevice({ | ||
filters: [{ | ||
vendorId: VENDOR_ID | ||
}] | ||
}) | ||
const model = new Model('TM-T20') | ||
const connection = new WebUSB(device) | ||
await connection.open() | ||
const printer = new Printer(model, connection) | ||
// ... | ||
``` | ||
## Available scripts | ||
@@ -46,0 +63,0 @@ |
@@ -11,8 +11,12 @@ import { Filter, FloydSteinberg } from './filter'; | ||
constructor(input: string | Buffer, filter: Filter = null) { | ||
const _filter = filter || new FloydSteinberg(); | ||
if (typeof input === 'string') { | ||
this.loadImage(input, _filter); | ||
} else { | ||
this.loadImageData(input, _filter); | ||
constructor( | ||
input: string | Buffer | PNG = null, | ||
filter: Filter = new FloydSteinberg(), | ||
) { | ||
if (input instanceof PNG) { | ||
this.readImage(filter.process(input)); | ||
} else if (typeof input === 'string') { | ||
this.loadImage(input, filter); | ||
} else if (input) { | ||
this.loadImageData(input, filter); | ||
} | ||
@@ -24,5 +28,3 @@ } | ||
const data = fs.readFileSync(filename); | ||
const png = PNG.sync.read(data); | ||
const image = filter.process(png); | ||
this.readImage(image); | ||
this.loadImageData(data, filter); | ||
} | ||
@@ -29,0 +31,0 @@ |
Sorry, the diff of this file is not supported yet
300290
3663
85