escpos-mc 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +279 -0
- package/commands.js +324 -0
- package/image.js +156 -0
- package/index.js +967 -0
- package/package.json +49 -0
- package/promisify.js +26 -0
- package/statuses.js +315 -0
- package/utils.js +35 -0
package/image.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const getPixels = require('get-pixels');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* [Image description]
|
|
6
|
+
* @param {[type]} pixels [description]
|
|
7
|
+
*/
|
|
8
|
+
function Image(pixels){
|
|
9
|
+
if(!(this instanceof Image))
|
|
10
|
+
return new Image(pixels);
|
|
11
|
+
this.pixels = pixels;
|
|
12
|
+
|
|
13
|
+
this.data = [];
|
|
14
|
+
function rgb(pixel) {
|
|
15
|
+
return {
|
|
16
|
+
r: pixel[0],
|
|
17
|
+
g: pixel[1],
|
|
18
|
+
b: pixel[2],
|
|
19
|
+
a: pixel[3]
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
var self = this;
|
|
24
|
+
for(var i=0;i<this.pixels.data.length;i+=this.size.colors){
|
|
25
|
+
this.data.push(rgb(new Array(this.size.colors).fill(0).map(function(_, b){
|
|
26
|
+
return self.pixels.data[ i + b ];
|
|
27
|
+
})));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
this.data = this.data.map(function(pixel) {
|
|
31
|
+
if (pixel.a == 0) return 0;
|
|
32
|
+
var shouldBeWhite = pixel.r > 200 && pixel.g > 200 && pixel.b > 200;
|
|
33
|
+
return shouldBeWhite ? 0 : 1;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* [load description]
|
|
40
|
+
* @param {[type]} url [description]
|
|
41
|
+
* @param {[type]} type [description]
|
|
42
|
+
* @param {Function} callback [description]
|
|
43
|
+
* @return {[type]} [description]
|
|
44
|
+
*/
|
|
45
|
+
Image.load = function(url, type, callback){
|
|
46
|
+
if(typeof type == 'function'){
|
|
47
|
+
callback = type;
|
|
48
|
+
type = null;
|
|
49
|
+
}
|
|
50
|
+
getPixels(url, type, function(err, pixels){
|
|
51
|
+
if(err) return callback(err);
|
|
52
|
+
callback(new Image(pixels));
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* [description]
|
|
58
|
+
* @return {[type]} [description]
|
|
59
|
+
*/
|
|
60
|
+
Image.prototype.__defineGetter__('size', function(){
|
|
61
|
+
return {
|
|
62
|
+
width : this.pixels.shape[0],
|
|
63
|
+
height: this.pixels.shape[1],
|
|
64
|
+
colors: this.pixels.shape[2],
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* [toBitmap description]
|
|
70
|
+
* @param {[type]} density [description]
|
|
71
|
+
* @return {[type]} [description]
|
|
72
|
+
*/
|
|
73
|
+
Image.prototype.toBitmap = function(density) {
|
|
74
|
+
density = density || 24;
|
|
75
|
+
|
|
76
|
+
var ld, result = [];
|
|
77
|
+
var x, y, b, l, i;
|
|
78
|
+
var c = density / 8;
|
|
79
|
+
|
|
80
|
+
// n blocks of lines
|
|
81
|
+
var n = Math.ceil(this.size.height / density);
|
|
82
|
+
|
|
83
|
+
for (y = 0; y < n; y++) {
|
|
84
|
+
// line data
|
|
85
|
+
ld = result[y] = [];
|
|
86
|
+
|
|
87
|
+
for (x = 0; x < this.size.width; x++) {
|
|
88
|
+
|
|
89
|
+
for (b = 0; b < density; b++) {
|
|
90
|
+
i = x * c + (b >> 3);
|
|
91
|
+
|
|
92
|
+
if (ld[i] === undefined) {
|
|
93
|
+
ld[i] = 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
l = y * density + b;
|
|
97
|
+
if (l < this.size.height) {
|
|
98
|
+
if (this.data[l * this.size.width + x]) {
|
|
99
|
+
ld[i] += (0x80 >> (b & 0x7));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
data: result,
|
|
108
|
+
density: density
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* [toRaster description]
|
|
113
|
+
* @return {[type]} [description]
|
|
114
|
+
*/
|
|
115
|
+
Image.prototype.toRaster = function () {
|
|
116
|
+
var result = [];
|
|
117
|
+
var width = this.size.width;
|
|
118
|
+
var height = this.size.height;
|
|
119
|
+
var data = this.data;
|
|
120
|
+
|
|
121
|
+
// n blocks of lines
|
|
122
|
+
var n = Math.ceil(width / 8);
|
|
123
|
+
var x, y, b, c, i;
|
|
124
|
+
|
|
125
|
+
for (y = 0; y < height; y++) {
|
|
126
|
+
|
|
127
|
+
for (x = 0; x < n; x++) {
|
|
128
|
+
|
|
129
|
+
for (b = 0; b < 8; b++) {
|
|
130
|
+
i = x * 8 + b;
|
|
131
|
+
|
|
132
|
+
if (result[y * n + x] === undefined) {
|
|
133
|
+
result[y * n + x] = 0;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
c = x * 8 + b;
|
|
137
|
+
if (c < width) {
|
|
138
|
+
if (data[y * width + i]) {
|
|
139
|
+
result[y * n + x] += (0x80 >> (b & 0x7));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
data: result,
|
|
147
|
+
width: n,
|
|
148
|
+
height: height
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* [exports description]
|
|
154
|
+
* @type {[type]}
|
|
155
|
+
*/
|
|
156
|
+
module.exports = Image;
|