-
Notifications
You must be signed in to change notification settings - Fork 1
/
barcodes.inc
124 lines (101 loc) · 4.27 KB
/
barcodes.inc
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
var Barcode = function() {
/* ======================================================================= */
/* Our "Barcode" object */
/* ======================================================================= */
var Barcode = function(type, code, parameters) {
var _function = __formats[type];
if (_function == null) {
throw new Error('Unsupported barcode type "' + type + '"');
}
var _barcode = _function(code, parameters);
this.parameters = _barcode.parameters || parameters || {};
this.format = type;
this.rows = _barcode.rows;
this.cols = _barcode.cols;
this.data = _barcode.data;
this.contents = code;
}
Barcode.prototype.createCanvas = function(scaleX, scaleY) {
var scale = normalizeScale(scaleX, scaleY);
/* Create our Canvas element */
canvas = document.createElement('canvas');
canvas.width = scale.x * this.cols;
canvas.height = scale.y * this.rows;
/* Paint! */
var ctx = canvas.getContext('2d');
for (var r = 0, y = 0; r < this.rows; r ++, y += scale.y) {
for (var c = 0, x = 0; c < this.cols; c ++, x += scale.x) {
if (this.data[r][c]) {
ctx.fillRect(x, y, scale.x, scale.y);
}
}
}
return canvas;
}
Barcode.prototype.createImageData = function(scaleX, scaleY) {
var scale = normalizeScale(scaleX, scaleY);
var _data = this.data;
var _rows = this.rows;
var _cols = this.cols;
return __create_image_data(_cols * scale.x, _rows * scale.y, function(x, y) {
var c = Math.floor(x / scale.x);
var r = Math.floor(y / scale.y);
if ((r >= 0) && (r < _rows) && (c >= 0) && (c < _cols)) {
return _data[r][c] ? 0 : 1;
} else {
return 1;
}
});
}
/* ======================================================================= */
/* Constants */
/* ======================================================================= */
Object.defineProperty(Barcode, 'PDF417', { enumerable: true, value: 'pdf417' });
Object.defineProperty(Barcode, 'QRCODE', { enumerable: true, value: 'qrcode' });
/* ======================================================================= */
/* Little helper functions */
/* ======================================================================= */
var normalizeScale = function(scaleX, scaleY) {
/* No parameter? Scale 1 */
if (scaleX == null) scaleX = scaleY = 1;
/* One parameter? Scale both! */
if ((scaleX != null) && (scaleY == null)) {
scaleY = scaleX;
}
/* Convert to numbers (keep barcodes pixel perfect) */
scaleX = Math.round(Number(scaleX) || 2);
scaleY = Math.round(Number(scaleY) || 2);
/* Return them */
return { x: scaleX, y: scaleY };
}
/* ======================================================================= */
/* Imported libraries */
/* ======================================================================= */
/* Formats creation functions */
var __formats = {};
var __create_image_data;
+function() {
/* BCMath library statically included */
// #include "bcmath/libbcmath.js"
// #include "bcmath/libbcmath.add.js"
// #include "bcmath/libbcmath.div.js"
// #include "bcmath/libbcmath.doaddsub.js"
// #include "bcmath/libbcmath.recmul.js"
// #include "bcmath/bcmath.js"
/* Import the PDF417 library */
// #include "pdf417/pdf417_constants.js"
// #include "pdf417/pdf417_helpers.js"
// #include "pdf417/pdf417_create.js"
__formats['pdf417'] = pdf417_create;
/* Import the QRCODE creation code */
// #include "qrcode/qrcode_create.js"
__formats['qrcode'] = qrcode_create;
/* Import the QRCODE GIF library */
// #include "qrcode/qrcode_giflib.js"
__create_image_data = create_image_data;
}();
/* ======================================================================= */
/* Return our object definition */
/* ======================================================================= */
return Barcode;
}();