-
Notifications
You must be signed in to change notification settings - Fork 305
/
demo.html
322 lines (303 loc) · 13 KB
/
demo.html
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!doctype html>
<html><head><title>bwip-js - JavaScript Barcode Generator</title>
<meta charset="utf-8" />
<meta name="keywords" content="bar code, barcode generator, online barcode generator, free barcode generator, javascript, javascript library, nodejs, QR Code, EAN, EAN 13, Code 128, UPC, ISBN, ITF 14, Code 39, GS1, GS1 128, PDF417, HIBC, DataMatrix, Data Matrix, DataBar, OCR, OCR B, OCR font, Aztec Code, PostNet, USPS, Pharmacode" />
<meta name="description" content="JavaScript barcode generator and library. Create any barcode in your browser." />
<link rel="stylesheet" type="text/css" href="lib/demo.css">
<script type="text/javascript" src="dist/bwip-js.js"></script>
<script type="text/javascript" src="lib/symdesc.js"></script>
<script type="text/javascript" src="lib/canvas-toblob.js"></script>
<script type="text/javascript" src="lib/filesaver.js"></script>
<script type="text/javascript" src="lib/inconsolata.js"></script>
<script type="text/javascript">
window.addEventListener('load', function() {
var lastSymbol, lastBarText, lastAltText, lastOptions, lastRotate, lastScaleX, lastScaleY,
lastRenderAs = 'render-canvas';
try {
lastSymbol = localStorage.getItem('bwipjsLastSymbol');
lastBarText = localStorage.getItem('bwipjsLastBarText');
lastAltText = localStorage.getItem('bwipjsLastAltText');
lastOptions = localStorage.getItem('bwipjsLastOptions');
lastRotate = localStorage.getItem('bwipjsLastRotate');
lastScaleX = +localStorage.getItem('bwipjsLastScaleX');
lastScaleY = +localStorage.getItem('bwipjsLastScaleY');
lastRenderAs = localStorage.getItem('bwipjsLastRenderAs');
} catch (e) {
}
// Set up the select list of barcode types
var sel = document.getElementById('symbol');
var opts = [];
for (var id in symdesc) {
opts.push(symdesc[id]);
}
opts.sort(function (a,b) { return a.desc < b.desc ? -1 : 1 });
for (var i = 0, l = opts.length; i < l; i++) {
var elt = document.createElement('option');
elt.textContent = opts[i].desc;
elt.value = opts[i].sym;
sel.appendChild(elt);
}
sel.addEventListener('change', function(ev) {
var desc = symdesc[sel.value];
if (desc) {
document.getElementById('symtext').value = desc.text;
document.getElementById('symopts').value = desc.opts;
} else {
document.getElementById('symtext').value = '';
document.getElementById('symopts').value = '';
}
document.getElementById('symaltx').value = '';
document.getElementById('saveas').style.visibility = 'hidden';
document.getElementById('stats').textContent = '';
document.getElementById('canvas').style.display = 'none';
document.getElementById('svgdiv').style.display = 'none';
document.getElementById('output').textContent = '';
});
if (lastSymbol) {
sel.value = lastSymbol;
} else {
sel.selectedIndex = 0;
}
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
sel.dispatchEvent(evt);
if (lastBarText) {
document.getElementById('symtext').value = lastBarText;
document.getElementById('symaltx').value = lastAltText;
document.getElementById('symopts').value = lastOptions;
}
if (lastRenderAs) {
document.getElementById(lastRenderAs).checked = true;
}
if (lastScaleX && lastScaleY) {
document.getElementById('scaleX').value = lastScaleX;
document.getElementById('scaleY').value = lastScaleY;
}
if (lastRotate) {
document.getElementById('rotate' + lastRotate).checked = true;
}
document.getElementById('scaleX').addEventListener('change', function(ev) {
document.getElementById('scaleY').value = ev.target.value;
});
document.getElementById('render').addEventListener('click', render);
document.getElementById('saveas').style.visibility = 'hidden';
// Allow Enter to render
document.getElementById('params').addEventListener('keypress', function(ev) {
if (ev.which == 13) {
render();
ev.stopPropagation();
ev.preventDefault();
return false;
}
});
document.getElementById('versions').textContent =
'bwip-js ' + bwipjs.BWIPJS_VERSION + ' / BWIPP ' + bwipjs.BWIPP_VERSION;
// A reasonable match to OCR-B metrics.
bwipjs.loadFont("Inconsolata", 95, 105, Inconsolata);
});
function render() {
var elt = symdesc[document.getElementById('symbol').value];
var text = document.getElementById('symtext').value.trim();
var alttext = document.getElementById('symaltx').value.trim();
var options = document.getElementById('symopts').value.trim();
var rotate = document.querySelector('input[name="rotate"]:checked').value;
var scaleX = +document.getElementById('scaleX').value || 2;
var scaleY = +document.getElementById('scaleY').value || 2;
var renderAs = document.querySelector('input[name="render-as"]:checked').id;
try {
localStorage.setItem('bwipjsLastSymbol', elt.sym);
localStorage.setItem('bwipjsLastBarText', text);
localStorage.setItem('bwipjsLastAltText', alttext);
localStorage.setItem('bwipjsLastOptions', options);
localStorage.setItem('bwipjsLastScaleX', scaleX);
localStorage.setItem('bwipjsLastScaleY', scaleY);
localStorage.setItem('bwipjsLastRotate', rotate);
localStorage.setItem('bwipjsLastRenderAs', renderAs);
} catch (e) {
}
// Clear the page
document.getElementById('output').value = '';
document.getElementById('stats').value = '';
document.getElementById('saveas').style.visibility = 'hidden';
document.getElementById('output').textContent = '';
var canvas = document.getElementById('canvas');
canvas.height = 1;
canvas.width = 1;
canvas.style.display = 'none';
var svgdiv = document.getElementById('svgdiv');
svgdiv.style.display = 'none';
// Convert the options to an object.
let opts = {};
let aopts = options.split(' ');
for (let i = 0; i < aopts.length; i++) {
if (!aopts[i]) {
continue;
}
var eq = aopts[i].indexOf('=');
if (eq == -1) {
// !name => false
// name => true
if (aopts[i][0] == '!') {
opts[aopts[i].substr(1)] = false;
} else {
opts[aopts[i]] = true;
}
} else {
opts[aopts[i].substr(0, eq)] = aopts[i].substr(eq+1);
}
}
// Finish up the options
opts.text = text;
opts.bcid = elt.sym;
opts.scaleX = scaleX;
opts.scaleY = scaleY;
opts.rotate = rotate;
if (alttext) {
opts.alttext = alttext;
}
if (renderAs == 'render-canvas') {
// Draw the bar code to the canvas
try {
let ts0 = new Date;
bwipjs.toCanvas(canvas, opts);
showCVS(ts0, new Date);
} catch (e) {
// Watch for BWIPP generated raiseerror's.
var msg = (''+e).trim();
if (msg.indexOf("bwipp.") >= 0) {
document.getElementById('output').textContent = msg;
} else if (e.stack) {
// GC includes the message in the stack. FF does not.
document.getElementById('output').textContent =
(e.stack.indexOf(msg) == -1 ? msg + '\n' : '') + e.stack;
} else {
document.getElementById('output').textContent = msg;
}
return;
}
} else {
// Draw the bar code as SVG
try {
let ts0 = new Date;
let svg = bwipjs.toSVG(opts);
showSVG(svg, ts0, new Date);
} catch (e) {
// Watch for BWIPP generated raiseerror's.
var msg = (''+e).trim();
if (msg.indexOf("bwipp.") >= 0) {
document.getElementById('output').textContent = msg;
} else if (e.stack) {
// GC includes the message in the stack. FF does not.
document.getElementById('output').textContent =
(e.stack.indexOf(msg) == -1 ? msg + '\n' : '') + e.stack;
} else {
document.getElementById('output').textContent = msg;
}
return;
}
}
function showCVS(ts0, ts1) {
canvas.style.display = '';
setURL();
document.getElementById('stats').textContent = elt.sym + ' rendered in ' + (ts1-ts0) + ' msecs.';
document.getElementById('saveas').style.visibility = 'visible';
saveCanvas.basename = elt.sym + '-' + text.replace(/[^a-zA-Z0-9._]+/g, '-');
if (window.devicePixelRatio) {
canvas.style.zoom = 1 / window.devicePixelRatio;
} else {
canvas.style.zoom = 1;
}
}
function showSVG(svg, ts0, ts1) {
let [ , width, height ] = /viewBox="0 0 (\d+) (\d+)"/.exec(svg);
svgdiv.style.width = width + 'px';
svgdiv.style.height = height + 'px';
if (window.devicePixelRatio) {
svgdiv.style.zoom = 1 / window.devicePixelRatio;
} else {
svgdiv.style.zoom = 1;
}
svgdiv.innerHTML = svg;
svgdiv.style.display = '';
document.getElementById('output').textContent = svg;
document.getElementById('stats').textContent = elt.sym + ' rendered in ' + (ts1-ts0) + ' msecs.';
}
}
function saveCanvas(type, ext) {
var canvas = document.getElementById('canvas');
canvas.toBlob(function (blob) {
saveAs(blob, saveCanvas.basename + ext);
}, type, 1);
}
function setURL() {
var elt = symdesc[document.getElementById('symbol').value];
var text = document.getElementById('symtext').value.trim();
var alttext = document.getElementById('symaltx').value.trim();
var options = document.getElementById('symopts').value.trim();
var rotate = document.querySelector('input[name="rotate"]:checked').value;
var scaleX = +document.getElementById('scaleX').value || 2;
var scaleY = +document.getElementById('scaleY').value || scaleX;
var url = 'http://bwipjs-api.metafloor.com/?bcid=' + elt.sym +
'&text=' + encodeURIComponent(text) +
(alttext ? '&alttext=' + encodeURIComponent(alttext) : '') +
(options ? '&' + options.replace(/ +/g, '&') : '') +
(rotate != 'N' ? '&rotate=' + rotate : '') +
(scaleX == scaleY ? '&scale=' + scaleX
: '&scaleX=' + scaleX + '&scaleY=' + scaleY)
;
document.getElementById('apiurl').href = url;
}
</script>
</head><body>
<div id="header">
<div id="bwip-js">bwip-js // Barcode Writer in Pure JavaScript</div>
<div id="versions"></div>
</div>
<div id="params">
<table border=0 cellpading=0 cellspacing=0><tr>
<td style="vertical-align:top">
<table border=0 cellpading=0 cellspacing=0>
<tr><th>Barcode Type:<td><select id="symbol"></select>
<tr><th>Bar Text:<td><input id="symtext" type="text" spellcheck="false">
<tr><th>Alt Text:<td><input id="symaltx" type="text" spellcheck="false">
<tr><th>Options:<td><input id="symopts" type="text" spellcheck="false">
<tr><td><td>
<div id="stats"></div>
<div id="saveas">
<a class="saveas" href="javascript:saveCanvas('image/png','.png')">Save As PNG</a>
<a class="saveas" href="javascript:saveCanvas('image/jpeg','.jpg')">Save As JPEG</a>
<a class="saveas" id="apiurl" href="">Goto URL</a>
</div>
</table>
<td style="padding-left:10mm;vertical-align:top">
<table border=0 cellpading=0 cellspacing=5>
<tr><th>Render As:<td>
<label for="render-canvas"><input type="radio" name="render-as" id="render-canvas" checked>Canvas</label>
<label for="render-svg"><input type="radio" name="render-as" id="render-svg">SVG</label>
<tr><th>Scale X,Y:<td>
<input type="number" min=1 max=9 step=1 id="scaleX" value=2>
<input type="number" min=1 max=9 step=1 id="scaleY" value=2>
<tr><th>Image Rotation:<td>
<label for="rotateN"><input type="radio" name="rotate" value="N"
id="rotateN" checked>Normal</label>
<label for="rotateR"><input type="radio" name="rotate" value="R"
id="rotateR">Right</label>
<label for="rotateL"><input type="radio" name="rotate" value="L"
id="rotateL">Left</label>
<label for="rotateI"><input type="radio" name="rotate" value="I"
id="rotateI">Invert</label>
<tr><td><td><button style="margin-top:1ex" id="render">Show Barcode</button>
</table>
<tr><td><br>
</table>
</div>
<div id="content">
<canvas id="canvas" width=1 height=1 style="display:none"></canvas>
<div id="svgdiv" style="display:none"></div>
<div id="output" style="white-space:pre"></div>
</div>
</body>
</html>