forked from metafloor/bwip-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser-bwipjs.js
executable file
·109 lines (97 loc) · 2.79 KB
/
browser-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
// file: bwip-js/browser-bwipjs.js
//
// Copyright (c) 2011-2018 Mark Warren
//
// See the LICENSE file in the bwip-js root directory
// for the extended copyright notice.
//
"use strict";
var bwipp = require('./bwipp'),
bwipjs = require('./bwipjs'),
Bitmap = require('./browser-bitmap'),
fontlib = require('./browser-fonts')
;
// This module's one and only export is the canvas rendering.
// `cvs` is either an id to a canvas element, or the actual canvas element.
// `opts` is the bwip-js/BWIPP options object.
// `callback` has the usual node callback signature:
//
// function (err, cvs)
//
// Where cvs is the same parameter as passed into this call.
module.exports = function toCanvas(cvs, opts, callback) {
// Make a mutable copy of the user's options
var vals = {};
for (var id in opts) {
vals[id] = opts[id];
}
// Set the defaults
var scale = vals.scale || 2;
var scaleX = +vals.scaleX || scale;
var scaleY = +vals.scaleY || scaleX;
var rot = vals.rotate || 'N';
var mono = vals.monochrome || false;
var padX = +vals.paddingwidth || 0;
var padY = +vals.paddingheight || 0;
// The required parameters
var bcid = vals.bcid;
var text = vals.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 vals.scale;
delete vals.scaleX;
delete vals.scaleY;
delete vals.rotate;
delete vals.text;
delete vals.bcid;
delete vals.monochrome;
delete vals.paddingwidth;
delete vals.paddingheight;
// 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(fontlib, mono);
// Fix a disconnect in the BWIPP rendering logic
if (vals.alttext) {
vals.includetext = true;
}
// We use mm rather than inches for height - except pharmacode2 height
// which is already in mm.
if (+vals.height && bcid != 'pharmacode2') {
vals.height = vals.height / 25.4 || 0.5;
}
// Likewise, width
if (+vals.width) {
vals.width = vals.width / 25.4 || 0;
}
// Override the `backgroundcolor` option.
if (vals.backgroundcolor) {
bw.bitmap(new Bitmap(cvs, rot, parseInt(''+vals.backgroundcolor, 16)));
delete vals.backgroundcolor;
} else {
bw.bitmap(new Bitmap(cvs, rot));
}
// 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 {
bwipp()(bw, bcid, text, vals);
// Let the font manager demand-load any fonts.
fontlib.loadfonts(function(err) {
if (err) {
callback(err);
} else {
bw.render(callback);
}
});
} catch (e) {
callback(e.stack || e);
}
}
module.exports.bwipjs_version = bwipjs.VERSION;
module.exports.bwipp_version = bwipp.VERSION;