-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbarcode.js
More file actions
690 lines (588 loc) · 16.5 KB
/
Copy pathbarcode.js
File metadata and controls
690 lines (588 loc) · 16.5 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* This is made possible by the incredible work
of lindell's repo 'JsBarcode' at https://github.com/lindell/JsBarcode
Copyright (c) 2012 Johan Lindell (johan@lindell.me)
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.
*/
function EAN(EANnumber){
this.EANnumber = EANnumber+"";
this.valid = function(){
return valid(this.EANnumber);
};
this.encoded = function (){
if(valid(this.EANnumber)){
return createUPC(this.EANnumber);
}
return "";
}
//The L (left) type of encoding
var Lbinary = {
0: "0001101",
1: "0011001",
2: "0010011",
3: "0111101",
4: "0100011",
5: "0110001",
6: "0101111",
7: "0111011",
8: "0110111",
9: "0001011"}
//The G type of encoding
var Gbinary = {
0: "0100111",
1: "0110011",
2: "0011011",
3: "0100001",
4: "0011101",
5: "0111001",
6: "0000101",
7: "0010001",
8: "0001001",
9: "0010111"}
//The R (right) type of encoding
var Rbinary = {
0: "1110010",
1: "1100110",
2: "1101100",
3: "1000010",
4: "1011100",
5: "1001110",
6: "1010000",
7: "1000100",
8: "1001000",
9: "1110100"}
//The left side structure in EAN-13
var EANstruct = {
0: "LLLLLL",
1: "LLGLGG",
2: "LLGGLG",
3: "LLGGGL",
4: "LGLLGG",
5: "LGGLLG",
6: "LGGGLL",
7: "LGLGLG",
8: "LGLGGL",
9: "LGGLGL"}
//The start bits
var startBin = "101";
//The end bits
var endBin = "101";
//The middle bits
var middleBin = "01010";
//Regexp to test if the EAN code is correct formated
var regexp = /^[0-9]{13}$/;
//Create the binary representation of the EAN code
//number needs to be a string
function createUPC(number){
//Create the return variable
var result = "";
//Get the first digit (for later determination of the encoding type)
var firstDigit = number[0];
//Get the number to be encoded on the left side of the EAN code
var leftSide = number.substr(1,7);
//Get the number to be encoded on the right side of the EAN code
var rightSide = number.substr(7,6);
//Add the start bits
result += startBin;
//Add the left side
result += encode(leftSide,EANstruct[firstDigit]);
//Add the middle bits
result += middleBin;
//Add the right side
result += encode(rightSide,"RRRRRR");
//Add the end bits
result += endBin;
return result;
}
//Convert a numberarray to the representing
function encode(number,struct){
//Create the variable that should be returned at the end of the function
var result = "";
//Loop all the numbers
for(var i = 0;i<number.length;i++){
//Using the L, G or R encoding and add it to the returning variable
if(struct[i]=="L"){
result += Lbinary[number[i]];
}
else if(struct[i]=="G"){
result += Gbinary[number[i]];
}
else if(struct[i]=="R"){
result += Rbinary[number[i]];
}
}
return result;
}
//Calulate the checksum digit
function checksum(number){
var result = 0;
for(var i=0;i<12;i+=2){result+=parseInt(number[i])}
for(var i=1;i<12;i+=2){result+=parseInt(number[i])*3}
return (10 - (result % 10)) % 10;
}
function valid(number){
if(number.search(regexp)==-1){
return false;
}
else{
return number[12] == checksum(number);
}
}
}
function UPC(UPCnumber){
this.ean = new EAN("0"+UPCnumber);
this.valid = function(){
return this.ean.valid();
}
this.encoded = function(){
return this.ean.encoded();
}
}
function CODE128(string, code){
code = code || "B";
this.string128 = string+"";
this.valid = valid;
//The public encoding function
this.encoded = function(){
if(valid(string)){
return calculate["code128" + code](string);
}
else{
return "";
}
}
//Data for each character, the last characters will not be encoded but are used for error correction
var code128b = [
[" ","11011001100",0],
["!","11001101100",1],
["\"","11001100110",2],
["#","10010011000",3],
["$","10010001100",4],
["%","10001001100",5],
["&","10011001000",6],
["'","10011000100",7],
["(","10001100100",8],
[")","11001001000",9],
["*","11001000100",10],
["+","11000100100",11],
[",","10110011100",12],
["-","10011011100",13],
[".","10011001110",14],
["/","10111001100",15],
["0","10011101100",16],
["1","10011100110",17],
["2","11001110010",18],
["3","11001011100",19],
["4","11001001110",20],
["5","11011100100",21],
["6","11001110100",22],
["7","11101101110",23],
["8","11101001100",24],
["9","11100101100",25],
[":","11100100110",26],
[";","11101100100",27],
["<","11100110100",28],
["=","11100110010",29],
[">","11011011000",30],
["?","11011000110",31],
["@","11000110110",32],
["A","10100011000",33],
["B","10001011000",34],
["C","10001000110",35],
["D","10110001000",36],
["E","10001101000",37],
["F","10001100010",38],
["G","11010001000",39],
["H","11000101000",40],
["I","11000100010",41],
["J","10110111000",42],
["K","10110001110",43],
["L","10001101110",44],
["M","10111011000",45],
["N","10111000110",46],
["O","10001110110",47],
["P","11101110110",48],
["Q","11010001110",49],
["R","11000101110",50],
["S","11011101000",51],
["T","11011100010",52],
["U","11011101110",53],
["V","11101011000",54],
["W","11101000110",55],
["X","11100010110",56],
["Y","11101101000",57],
["Z","11101100010",58],
["[","11100011010",59],
["\\","11101111010",60],
["]","11001000010",61],
["^","11110001010",62],
["_","10100110000",63],
["`","10100001100",64],
["a","10010110000",65],
["b","10010000110",66],
["c","10000101100",67],
["d","10000100110",68],
["e","10110010000",69],
["f","10110000100",70],
["g","10011010000",71],
["h","10011000010",72],
["i","10000110100",73],
["j","10000110010",74],
["k","11000010010",75],
["l","11001010000",76],
["m","11110111010",77],
["n","11000010100",78],
["o","10001111010",79],
["p","10100111100",80],
["q","10010111100",81],
["r","10010011110",82],
["s","10111100100",83],
["t","10011110100",84],
["u","10011110010",85],
["v","11110100100",86],
["w","11110010100",87],
["x","11110010010",88],
["y","11011011110",89],
["z","11011110110",90],
["{","11110110110",91],
["|","10101111000",92],
["}","10100011110",93],
["~","10001011110",94],
[String.fromCharCode(127),"10111101000",95],
[String.fromCharCode(128),"10111100010",96],
[String.fromCharCode(129),"11110101000",97],
[String.fromCharCode(130),"11110100010",98],
[String.fromCharCode(131),"10111011110",99],
[String.fromCharCode(132),"10111101110",100],
[String.fromCharCode(133),"11101011110",101],
[String.fromCharCode(134),"11110101110",102],
//Start codes
[String.fromCharCode(135),"11010000100",103],
[String.fromCharCode(136),"11010010000",104],
[String.fromCharCode(137),"11010011100",105]];
//The end bits
var endBin = "1100011101011";
//This regexp is used for validation
var regexp = /^[!-~ ]+$/;
//Use the regexp variable for validation
function valid(){
if(string.search(regexp)==-1){
return false;
}
return true;
}
//The encoder function that return a complete binary string. Data need to be validated before sent to this function
//This is general calculate function, which is called by code specific calculate functions
function calculateCode128(string, encodeFn, startCode, checksumFn){
var result = "";
//Add the start bits
result += encodingById(startCode);
//Add the encoded bits
result += encodeFn(string);
//Add the checksum
result += encodingById(checksumFn(string, startCode));
//Add the end bits
result += endBin;
return result;
}
//Code specific calculate functions
var calculate = {
code128B: function(string){
return calculateCode128(string, encodeB, 104, checksumB);
},
code128C: function(string){
string = string.replace(/ /g, "");
return calculateCode128(string, encodeC, 105, checksumC);
}
}
//Encode the characters (128 B)
function encodeB(string){
var result = "";
for(var i=0;i<string.length;i++){
result+=encodingByChar(string[i]);
}
return result;
}
//Encode the characters (128 C)
function encodeC(string){
var result = "";
for(var i=0;i<string.length;i+=2){
result+=encodingById(parseInt(string.substr(i, 2)));
}
return result;
}
//Calculate the checksum (128 B)
function checksumB(string, startCode){
var sum = 0;
for(var i=0;i<string.length;i++){
sum += weightByCharacter(string[i])*(i+1);
}
return (sum+startCode) % 103;
}
//Calculate the checksum (128 C)
function checksumC(string, startCode){
var sum = 0;
var w = 1;
for(var i=0;i<string.length;i+=2){
sum += parseInt(string.substr(i, 2))*(w);
w++;
}
return (sum+startCode) % 103;
}
//Get the encoded data by the id of the character
function encodingById(id){
for(var i=0;i<code128b.length;i++){
if(code128b[i][2]==id){
return code128b[i][1];
}
}
return "";
}
//Get the id (weight) of a character
function weightByCharacter(character){
for(var i=0;i<code128b.length;i++){
if(code128b[i][0]==character){
return code128b[i][2];
}
}
return 0;
}
//Get the encoded data of a character
function encodingByChar(character){
for(var i=0;i<code128b.length;i++){
if(code128b[i][0]==character){
return code128b[i][1];
}
}
return "";
}
}
function CODE128B(string) {
return new CODE128(string, "B");
}
function CODE128C(string) {
return new CODE128(string, "C");
};
function CODE39(string){
var code39 = [
[0,"0","101000111011101"]
,[1,"1","111010001010111"]
,[2,"2","101110001010111"]
,[3,"3","111011100010101"]
,[4,"4","101000111010111"]
,[5,"5","111010001110101"]
,[6,"6","101110001110101"]
,[7,"7","101000101110111"]
,[8,"8","111010001011101"]
,[9,"9","101110001011101"]
,[10,"A","111010100010111"]
,[11,"B","101110100010111"]
,[12,"C","111011101000101"]
,[13,"D","101011100010111"]
,[14,"E","111010111000101"]
,[15,"F","101110111000101"]
,[16,"G","101010001110111"]
,[17,"H","111010100011101"]
,[18,"I","101110100011101"]
,[19,"J","101011100011101"]
,[20,"K","111010101000111"]
,[21,"L","101110101000111"]
,[22,"M","111011101010001"]
,[23,"N","101011101000111"]
,[24,"O","111010111010001"]
,[25,"P","101110111010001"]
,[26,"Q","101010111000111"]
,[27,"R","111010101110001"]
,[28,"S","101110101110001"]
,[29,"T","101011101110001"]
,[30,"U","111000101010111"]
,[31,"V","100011101010111"]
,[32,"W","111000111010101"]
,[33,"X","100010111010111"]
,[34,"Y","111000101110101"]
,[35,"Z","100011101110101"]
,[36,"-","100010101110111"]
,[37,".","111000101011101"]
,[38," ","100011101011101"]
,[39,"$","100010001000101"]
,[40,"/","100010001010001"]
,[41,"+","100010100010001"]
,[42,"%","101000100010001"]];
this.valid = valid;
//The public encoding function
this.encoded = function(){
if(valid(string)){
return encode(string);
}
else{
return "";
}
}
//This regexp is used for validation
var regexp = /^[0-9A-Z\-\.\ \$\/\+\%]+$/;
//Use the regexp variable for validation
function valid(){
if(string.search(regexp)==-1){
return false;
}
return true;
}
//Encode the characters
function encode(string){
var result = "";
result += "1000101110111010";
for(var i=0;i<string.length;i++){
result+=encodingByChar(string[i])+"0";
}
result += "1000101110111010";
return result;
}
//Get the encoded data of a character
function encodingByChar(character){
for(var i=0;i<code39.length;i++){
if(code39[i][1]==character){
return code39[i][2];
}
}
return "";
}
}
(function($){
JsBarcode = function(image, content, options) {
var merge = function(m1, m2) {
var newMerge = {};
for (var k in m1) {
newMerge[k] = m1[k];
}
for (var k in m2) {
newMerge[k] = m2[k];
}
return newMerge;
};
//Merge the user options with the default
options = merge(JsBarcode.defaults, options);
//Create the canvas where the barcode will be drawn on
// Check if the given image is already a canvas
var canvas = image;
// check if it is a jQuery object
if (canvas instanceof jQuery) {
// get the DOM element of the object
canvas = image.get(0);
}
// check if DOM element is a canvas, otherwise it will be probably an image so create a canvas
if (!(canvas instanceof HTMLCanvasElement)) {
canvas = document.createElement('canvas');
}
//Abort if the browser does not support HTML5canvas
if (!canvas.getContext) {
return image;
}
var encoder = new window[options.format](content);
//Abort if the barcode format does not support the content
if(!encoder.valid()){
return this;
}
//Encode the content
var binary = encoder.encoded();
var _drawBarcodeText = function (text) {
var x, y;
y = options.height;
ctx.font = options.fontSize + "px "+options.font;
ctx.textBaseline = "bottom";
ctx.textBaseline = 'top';
if(options.textAlign == "left"){
x = options.quite;
ctx.textAlign = 'left';
}
else if(options.textAlign == "right"){
x = canvas.width - options.quite;
ctx.textAlign = 'right';
}
else{ //All other center
x = canvas.width / 2;
ctx.textAlign = 'center';
}
ctx.fillText(text, x, y);
}
//Get the canvas context
var ctx = canvas.getContext("2d");
//Set the width and height of the barcode
canvas.width = binary.length*options.width+2*options.quite;
canvas.height = options.height + (options.displayValue ? options.fontSize : 0);
//Paint the canvas
ctx.clearRect(0,0,canvas.width,canvas.height);
if(options.backgroundColor){
ctx.fillStyle = options.backgroundColor;
ctx.fillRect(0,0,canvas.width,canvas.height);
}
//Creates the barcode out of the encoded binary
ctx.fillStyle = options.lineColor;
for(var i=0;i<binary.length;i++){
var x = i*options.width+options.quite;
if(binary[i] == "1"){
ctx.fillRect(x,0,options.width,options.height);
}
}
if(options.displayValue){
_drawBarcodeText(content);
}
//Grab the dataUri from the canvas
uri = canvas.toDataURL('image/png');
// check if given image is a jQuery object
if (image instanceof jQuery) {
// check if DOM element of jQuery selection is not a canvas, so assume that it is an image
if (!(image.get(0) instanceof HTMLCanvasElement)) {
//Put the data uri into the image
return image.attr("src", uri);
}
} else if (!(image instanceof HTMLCanvasElement)) {
// There is no jQuery object so just check if the given image was a canvas, if not set the source attr
image.setAttribute("src", uri);
}
};
JsBarcode.defaults = {
width: 2,
height: 100,
quite: 10,
format: "CODE128",
displayValue: false,
font:"Monospaced",
textAlign:"center",
fontSize: 12,
backgroundColor:"",
lineColor:"#000"
};
$.fn.JsBarcode = function(content, options){
JsBarcode(this, content, options);
return this;
};
//Look for instances of barcode on the order in Order Printer
$('.item_barcode').each(function(index){
var this_barcode = $(this).attr('data-barcode'),
this_display = $(this).attr('data-display'),
this_width = $(this).attr('data-width'),
this_height = $(this).attr('data-height'),
this_font = $(this).attr('data-font'),
this_fontsize = $(this).attr('data-fontsize'),
this_format = $(this).attr('data-format');
if( !(this_display) || ( this_display == "false") ) { this_display = false; }else{ this_display = true; }
if( !(this_width) ) { this_width = 1; }
if( !(this_height) ) { this_height = 50; }
if( !(this_font) ) { this_font = "Monospaced"; }
if( !(this_fontsize) ) { this_fontsize = 16; }
if( !(this_format) ) { this_format = "CODE128"; }
$(this).JsBarcode(this_barcode,{
width: Number(this_width),
height: Number(this_height),
quite: 10,
format: this_format,
displayValue: this_display,
font: this_font,
textAlign:"center",
fontSize: Number(this_fontsize),
backgroundColor:"",
lineColor:"#000"
});
});
})(jQuery);