forked from metafloor/bwip-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-bwipjs.js
executable file
·308 lines (272 loc) · 7.97 KB
/
node-bwipjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// file: node-bwipjs.js
//
// Copyright (c) 2011-2016 Mark Warren
//
// See the LICENSE file in the bwip-js root directory
// for the extended copyright notice.
//
var url = require('url'),
bwipp = require(__dirname + '/bwipp'),
bwipjs = require(__dirname + '/bwipjs'),
zlibPNG = require(__dirname + '/node-zlibPNG'),
freetype = require(__dirname + '/freetype')
;
// This module's primary export is the bwip-js HTTP request handler
module.exports = function(req, res, opts) {
var args = url.parse(req.url, true).query;
// Convert boolean empty parameters to true
for (var id in args) {
if (args[id] === '')
args[id] = true;
}
// Add in server options/overrides
opts = opts || {};
for (var id in opts) {
args[id] = opts[id];
}
module.exports.toBuffer(args, function(err, png) {
if (err) {
res.writeHead(400, { 'Content-Type':'text/plain' });
res.end(err, 'ascii');
} else {
res.writeHead(200, { 'Content-Type':'image/png' });
res.end(png, 'binary');
}
});
}
//
// bwipjs.toBuffer(options, callback)
//
// Generates a PNG-encoded image in a buffer.
//
// `options` are the bwip-js/BWIPP options wrapped in an object.
// `callback` is an event handler with prototype:
//
// function callback(err, png)
//
// `err` is an Error object or string. If `err` is set, `png` is null.
// `png` is a node Buffer containing the PNG image.
//
module.exports.toBuffer = function(args, callback) {
// Set the defaults
var scale = args.scale || 2;
var scaleX = +args.scaleX || scale;
var scaleY = +args.scaleY || scaleX;
var rot = args.rotate || 'N';
var mono = args.monochrome || false;
// To protect the server from memory exhaustion, you can optionally limit
// the size of the image. Value is in pixels.
// For example sizelimit=1024*1024 will limit images to under (roughly) 1MiB.
var sizelimit = +args.sizelimit || 0;
// The required parameters
var bcid = args.bcid;
var text = args.text;
if (!text) {
return callback('Bar code text not specified.');
}
if (!bcid) {
return callback('Bar code type not specified.');
}
// Remove the non-BWIPP options
delete args.scale;
delete args.scaleX;
delete args.scaleY;
delete args.rotate;
delete args.text;
delete args.bcid;
delete args.monochrome;
delete args.sizelimit;
// Initialize a barcode writer object. This is the interface between
// the low-level BWIPP code, freetype, and the Bitmap object.
var bw = new bwipjs(freetype, mono);
// Set the options
var opts = {};
for (var id in args) {
opts[id] = args[id];
}
// Fix a disconnect in the BWIPP rendering logic
if (opts.alttext) {
opts.includetext = true;
}
// We use mm rather than inches for height - except pharmacode2 height
// is explicitly in mm
if (opts.height && bcid != 'pharmacode2') {
opts.height = opts.height / 25.4 || 0.5;
}
// Override the `backgroundcolor` option.
if (opts.backgroundcolor) {
bw.bitmap(new Bitmap(parseInt(''+opts.backgroundcolor, 16)));
delete opts.backgroundcolor;
} else {
bw.bitmap(new Bitmap);
}
// Constrain resulting image size
bw.bitmap().limit(sizelimit);
// Add optional padding and scale the image.
bw.bitmap().pad(+opts.paddingwidth*scaleX || 0,
+opts.paddingheight*scaleY || 0);
bw.scale(scaleX, scaleY);
// Call into the BWIPP cross-compiled code
try {
bwipp()(bw, bcid, text, opts);
bw.bitmap().getPNG(rot, callback);
} catch (e) {
// Invoking this callback is synchronous.
callback('' + e);
}
}
module.exports.loadFont = function(fontname, sizemult, fontfile) {
freetype.FS_createDataFile('/', fontname, fontfile, true, false);
var load_font = freetype.cwrap("load_font", 'number',
['string','string','number']);
var rv = load_font('/' + fontname, fontname, sizemult);
if (rv != 0) {
freetype.unlink('/' + fontname);
throw 'Error: font load failed [' + rv + ']';
}
}
module.exports.unloadFont = function(fontname) {
// Unload from freetype
var close_font = freetype.cwrap("close_font", 'number', ['string']);
close_font(fontname);
// Delete from emscripten
freetype.unlink('/' + fontname);
}
module.exports.bwipjs_version = "1.1.1 (2016-09-19)";
module.exports.bwipp_version = "2016-08-29";
// bwipjs Bitmap interface
// Must export to the PostScript emulation:
// this.extent(llx, lly, urx, ury) { ## See comments below.
// this.color(r, g, b) ## The color to use for subsequent set()'s.
// this.set(x, y, a) ## Sets the pixel at (x,y) using the current
// ## color with alpha-a.
//
// bgcolor is optional. If specified, if must be an integer or string RGB
// value. Alpha channel is not supported.
function Bitmap(bgcolor) {
var _clrr = 0; // current red
var _clrg = 0; // current green
var _clrb = 0; // current blue
var _pixs = {}; // x,y = argb
var _minx = Infinity;
var _miny = Infinity;
var _maxx = 0;
var _maxy = 0;
var _padx = 0; // optional left/right padding
var _pady = 0; // optional top/bottom padding
var _sizelim = 0; // optional size limit
if (typeof bgcolor === 'string') {
bgcolor = (0xff000000 | parseInt(bgcolor, 16)) >>> 0;
} else if (bgcolor !== undefined) {
bgcolor = (0xff000000 | bgcolor) >>> 0;
}
// Optional padding. Rotates with the image.
this.pad = function(width, height) {
_padx = width;
_pady = height;
}
this.limit = function(size) {
_sizelim = size;
}
// Sets the minimim size for the drawing surface (can grow larger).
// BWIPP has logic for borders (padding) that without this custom call
// gets lost. See custom/renlinear.ps.
this.extent = function(llx, lly, urx, ury) {
llx = Math.floor(llx);
lly = Math.floor(lly);
urx = Math.floor(urx);
ury = Math.floor(ury);
if (_minx > llx) _minx = llx;
if (_miny > lly) _miny = lly;
if (_maxx < urx) _maxx = urx;
if (_maxy < ury) _maxy = ury;
// This is the only place where size limit is enforced.
if (_sizelim && _maxx * _maxy > _sizelim) {
throw 'BWIPJS: image exceeded size limit';
}
}
this.color = function(r,g,b) {
_clrr = r;
_clrg = g;
_clrb = b;
}
this.set = function(x, y, a) {
// postscript graphics work with floating-pt numbers
x = Math.floor(x);
y = Math.floor(y);
if (_minx > x) _minx = x;
if (_maxx < x) _maxx = x;
if (_miny > y) _miny = y;
if (_maxy < y) _maxy = y;
var xy = x + ',' + y;
var cx = _pixs[xy];
var r, g, b;
if (cx === undefined)
cx = bgcolor;
if (cx === undefined) {
r = _clrr;
g = _clrg;
b = _clrb;
} else {
// alpha-blend with the existing color
// dst is the existing "background" color
// src is the new color
var dsta = (cx >>> 24) / 255;
var dstr = (cx >>> 16) & 0xff;
var dstg = (cx >>> 8) & 0xff;
var dstb = cx & 0xff;
var srca = a / 255;
var newa = srca + dsta * (1 - srca); // new alpha 0.0 - 1.0
if (!newa) {
r = g = b = a = 0;
} else {
r = ((_clrr * srca + dstr * dsta * (1 - srca)) / newa)|0;
g = ((_clrg * srca + dstg * dsta * (1 - srca)) / newa)|0;
b = ((_clrb * srca + dstb * dsta * (1 - srca)) / newa)|0;
a = (newa * 255)|0;
}
}
_pixs[xy] = ((a << 24) | (r << 16) | (g << 8) | b) >>> 0;
}
// `rot` is the desired image rotation
// `callback` prototype: function(err, png)
this.getPNG = function(rot, callback) {
// determine image width and height
if (rot == 'R' || rot == 'L') {
var h = _maxx-_minx+1;
var w = _maxy-_miny+1;
// Swap padding values
var t = _pady;
_pady = _padx;
_padx = t;
} else {
var w = _maxx-_minx+1;
var h = _maxy-_miny+1;
}
var png = new zlibPNG(w + 2*_padx, h + 2*_pady, bgcolor);
for (var xy in _pixs) {
var pts = xy.split(',');
var x = +pts[0] - _minx;
var y = +pts[1] - _miny;
// PostScript builds bottom-up, we build top-down.
if (rot == 'N') {
y = h - y - 1; // Invert y
} else if (rot == 'I') {
x = w - x - 1; // Invert x
} else {
y = w - y; // Invert y
if (rot == 'L') {
var t = y;
y = h - x - 1;
x = t - 1;
} else {
var t = x;
x = w - y;
y = t;
}
}
png.set(x + _padx, y + _pady, _pixs[xy]);
}
return png.render(callback);
}
}