forked from metafloor/bwip-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode-bwipjs.js
executable file
·240 lines (215 loc) · 7.11 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
// file: bwip-js/node-bwipjs.js
//
// This is part of the bwip-js project available at:
//
// http://metafloor.github.io/bwip-js
//
// Copyright (c) 2011-2018 Mark Warren
//
// The MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
"use strict";
var url = require('url'),
bwipp = require('./bwipp'),
bwipjs = require('./bwipjs'),
Bitmap = require('./node-bitmap'),
fixedfont = require('./node-fonts') // freetype alternative, default
;
// freetype is the module, freefont is the font manager interface identical to fixedfont
var freetype, freefont;
// 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.stack || err), 'utf-8');
} 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 bwip-js 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;
var padX = +args.paddingwidth || 0;
var padY = +args.paddingheight || 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.');
}
// Initialize a barcode writer object. This is the interface between
// the low-level BWIPP code, the font manager, and the Bitmap object.
var bw = new bwipjs(freefont || fixedfont, mono);
// Set the BWIPP options
var opts = {};
for (var id in args) {
opts[id] = args[id];
}
// Remove the non-BWIPP options
delete opts.bcid;
delete opts.text;
delete opts.scale;
delete opts.scaleX;
delete opts.scaleY;
delete opts.rotate;
delete opts.monochrome;
delete opts.paddingwidth;
delete opts.paddingheight;
// 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
// which is already in mm.
if (+opts.height && bcid != 'pharmacode2') {
opts.height = opts.height / 25.4 || 0.5;
}
// Likewise, width
if (+opts.width) {
opts.width = opts.width / 25.4 || 0;
}
// Override the `backgroundcolor` option.
if (opts.backgroundcolor != null) {
bw.bitmap(new Bitmap(rot, opts.backgroundcolor, opts));
delete opts.backgroundcolor;
} else {
bw.bitmap(new Bitmap(rot, null, opts));
}
// Add optional padding and scale the image.
bw.bitmap().pad(padX*scaleX || 0, padY*scaleY || 0);
bw.scale(scaleX, scaleY);
// Call into the BWIPP cross-compiled code.
try {
var ts0 = Date.now();
bwipp()(bw, bcid, text, opts);
var ts1 = Date.now();
bw.render(callback);
var ts2 = Date.now();
} catch (e) {
callback(e);
}
// For testing...
//bw.render(function (err, png) {
// if (err) {
// callback(err);
// } else {
// var ts2 = Date.now();
// console.log('Encoded,Rendered,Elapsed: ' + (ts1-ts0) + ',' + (ts2-ts1) +
// ',' + (ts2-ts0) + ' msecs');
// callback(null, png);
// }
//});
}
module.exports.useFreetype = function(useFT) {
if (useFT || useFT == null) {
freetype = require('./freetype');
var ft_monochr = freetype.cwrap("monochrome", 'number', ['number']);
var ft_lookup = freetype.cwrap("find_font", 'number', ['string']);
var ft_bitmap = freetype.cwrap("get_bitmap", 'number',
['number','number','number','number']);
var ft_width = freetype.cwrap("get_width", 'number', []);
var ft_height = freetype.cwrap("get_height", 'number', []);
var ft_left = freetype.cwrap("get_left", 'number', []);
var ft_top = freetype.cwrap("get_top", 'number', []);
var ft_advance = freetype.cwrap("get_advance", 'number', []);
// bwipjs needs the following interfaces:
// lookup(fontname) returns fontid
// monochrome(bool) set the fonts to monochrome or anti-aliased
// getglyph(fontid, charcode, width, height)
freefont = {
lookup(name) {
return ft_lookup(name);
},
monochrome(enable) {
return ft_monochr(enable ? 1 : 0);
},
getglyph(fontid, charcode, size) {
var offset = ft_bitmap(fontid, charcode, size, size);
if (offset <= 0) {
return { width:0, height:0, top:0, left:0, advance:ft_advance() };
}
return {
width:ft_width(), height:ft_height(), top:ft_top(), left:ft_left(),
advance:ft_advance(), bytes:freetype.HEAPU8, offset:offset
}
}
};
// Bring in the custom symbol font for maxicode support (and dotcode when that
// renderer changes).
module.exports.loadFont('Symbol', 100,
require('fs').readFileSync(__dirname + '/fonts/BWIPJS-Symbol.otf', 'binary'));
} else {
freetype = freefont = null;
}
}
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.FS_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.FS_unlink('/' + fontname);
}
module.exports.bwipjs_version = bwipjs.VERSION;
module.exports.bwipp_version = bwipp.VERSION;