beautiful_scaffold 0.3.0.rc1 → 0.3.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1351 @@
1
+ /*!
2
+ * BarCode Coder Library (BCC Library)
3
+ * BCCL Version 2.0
4
+ *
5
+ * Porting : jQuery barcode plugin
6
+ * Version : 2.0.3
7
+ *
8
+ * Date : 2013-01-06
9
+ * Author : DEMONTE Jean-Baptiste <jbdemonte@gmail.com>
10
+ * HOUREZ Jonathan
11
+ *
12
+ * Web site: http://barcode-coder.com/
13
+ * dual licence : http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
14
+ * http://www.gnu.org/licenses/gpl.html
15
+ */
16
+
17
+ (function ($) {
18
+
19
+ var barcode = {
20
+ settings:{
21
+ barWidth: 1,
22
+ barHeight: 50,
23
+ moduleSize: 5,
24
+ showHRI: true,
25
+ addQuietZone: true,
26
+ marginHRI: 5,
27
+ bgColor: "#FFFFFF",
28
+ color: "#000000",
29
+ fontSize: 10,
30
+ output: "css",
31
+ posX: 0,
32
+ posY: 0
33
+ },
34
+ intval: function(val){
35
+ var type = typeof( val );
36
+ if (type == 'string'){
37
+ val = val.replace(/[^0-9-.]/g, "");
38
+ val = parseInt(val * 1, 10);
39
+ return isNaN(val) || !isFinite(val) ? 0 : val;
40
+ }
41
+ return type == 'number' && isFinite(val) ? Math.floor(val) : 0;
42
+ },
43
+ i25: { // std25 int25
44
+ encoding: ["NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN","NWNWN"],
45
+ compute: function(code, crc, type){
46
+ if (! crc) {
47
+ if (code.length % 2 != 0) code = '0' + code;
48
+ } else {
49
+ if ( (type == "int25") && (code.length % 2 == 0) ) code = '0' + code;
50
+ var odd = true, v, sum = 0;
51
+ for(var i=code.length-1; i>-1; i--){
52
+ v = barcode.intval(code.charAt(i));
53
+ if (isNaN(v)) return("");
54
+ sum += odd ? 3 * v : v;
55
+ odd = ! odd;
56
+ }
57
+ code += ((10 - sum % 10) % 10).toString();
58
+ }
59
+ return(code);
60
+ },
61
+ getDigit: function(code, crc, type){
62
+ code = this.compute(code, crc, type);
63
+ if (code == "") return("");
64
+ result = "";
65
+
66
+ var i, j;
67
+ if (type == "int25") {
68
+ // Interleaved 2 of 5
69
+
70
+ // start
71
+ result += "1010";
72
+
73
+ // digits + CRC
74
+ var c1, c2;
75
+ for(i=0; i<code.length / 2; i++){
76
+ c1 = code.charAt(2*i);
77
+ c2 = code.charAt(2*i+1);
78
+ for(j=0; j<5; j++){
79
+ result += '1';
80
+ if (this.encoding[c1].charAt(j) == 'W') result += '1';
81
+ result += '0';
82
+ if (this.encoding[c2].charAt(j) == 'W') result += '0';
83
+ }
84
+ }
85
+ // stop
86
+ result += "1101";
87
+ } else if (type == "std25") {
88
+ // Standard 2 of 5 is a numeric-only barcode that has been in use a long time.
89
+ // Unlike Interleaved 2 of 5, all of the information is encoded in the bars; the spaces are fixed width and are used only to separate the bars.
90
+ // The code is self-checking and does not include a checksum.
91
+
92
+ // start
93
+ result += "11011010";
94
+
95
+ // digits + CRC
96
+ var c;
97
+ for(i=0; i<code.length; i++){
98
+ c = code.charAt(i);
99
+ for(j=0; j<5; j++){
100
+ result += '1';
101
+ if (this.encoding[c].charAt(j) == 'W') result += "11";
102
+ result += '0';
103
+ }
104
+ }
105
+ // stop
106
+ result += "11010110";
107
+ }
108
+ return(result);
109
+ }
110
+ },
111
+ ean: {
112
+ encoding: [ ["0001101", "0100111", "1110010"],
113
+ ["0011001", "0110011", "1100110"],
114
+ ["0010011", "0011011", "1101100"],
115
+ ["0111101", "0100001", "1000010"],
116
+ ["0100011", "0011101", "1011100"],
117
+ ["0110001", "0111001", "1001110"],
118
+ ["0101111", "0000101", "1010000"],
119
+ ["0111011", "0010001", "1000100"],
120
+ ["0110111", "0001001", "1001000"],
121
+ ["0001011", "0010111", "1110100"] ],
122
+ first: ["000000","001011","001101","001110","010011","011001","011100","010101","010110","011010"],
123
+ encoding_addon2: ["00","01","10","11"],
124
+ encoding_addon5: ["11000","10100","10010","10001","01100","00110","00011","01010","01001","00101"],
125
+ getDigit: function(code, type){
126
+ // Check len (12 for ean13, 7 for ean8)
127
+ var len = type == "ean8" ? 7 : 12;
128
+ fullcode = code;
129
+ code = code.substring(0, len);
130
+ if (code.length != len) return("");
131
+ // Check each digit is numeric
132
+ var c;
133
+ for(var i=0; i<code.length; i++){
134
+ c = code.charAt(i);
135
+ if ( (c < '0') || (c > '9') ) return("");
136
+ }
137
+ // get checksum
138
+ code = this.compute(code, type);
139
+
140
+ // process analyse
141
+ var result = "101"; // start
142
+
143
+ if (type == "ean8"){
144
+
145
+ // process left part
146
+ for(var i=0; i<4; i++){
147
+ result += this.encoding[barcode.intval(code.charAt(i))][0];
148
+ }
149
+
150
+ // center guard bars
151
+ result += "01010";
152
+
153
+ // process right part
154
+ for(var i=4; i<8; i++){
155
+ result += this.encoding[barcode.intval(code.charAt(i))][2];
156
+ }
157
+
158
+ } else { // ean13
159
+ // extract first digit and get sequence
160
+ var seq = this.first[ barcode.intval(code.charAt(0)) ];
161
+
162
+ // process left part
163
+ for(var i=1; i<7; i++){
164
+ result += this.encoding[barcode.intval(code.charAt(i))][ barcode.intval(seq.charAt(i-1)) ];
165
+ }
166
+
167
+ // center guard bars
168
+ result += "01010";
169
+
170
+ // process right part
171
+ for(var i=7; i<13; i++){
172
+ result += this.encoding[barcode.intval(code.charAt(i))][ 2 ];
173
+ }
174
+ } // ean13
175
+
176
+ result += "101"; // stop
177
+
178
+ result += "000000000";
179
+
180
+ // addon 13+2 / 13+5
181
+ if(type == "ean13"){
182
+ addon = fullcode.substring(13, fullcode.length);
183
+
184
+ if (addon.length == 2){
185
+ // checksum addon
186
+ checksum = (parseInt(addon) % 4);
187
+ // binary encoding
188
+ for(var i=0; i<2; i++){
189
+ part_of_addon = barcode.intval(addon.charAt(i));
190
+ a_or_b = barcode.intval(this.encoding_addon2[barcode.intval(checksum)][i]);
191
+ partialencoding = this.encoding[part_of_addon][a_or_b];
192
+ result += partialencoding;
193
+ }
194
+ }else if(addon.length == 5){
195
+ // checksum addon
196
+ var sum = 0, odd = true;
197
+ x = 0;
198
+ y = 0;
199
+ z = 0;
200
+ for(var i=0; i<5; i++){
201
+ if(odd == false){
202
+ x += barcode.intval(addon.charAt(i));
203
+ }else{
204
+ y += barcode.intval(addon.charAt(i));
205
+ }
206
+ odd = ! odd;
207
+ }
208
+ checksum = (((9 * x) + (3 * y)) % 10);
209
+ // binary encoding
210
+ result += "1011"; // special delimiter
211
+
212
+ for(var i=0; i<5; i++){
213
+ part_of_addon = barcode.intval(addon.charAt(i));
214
+ a_or_b = barcode.intval(this.encoding_addon5[barcode.intval(checksum)][i]);
215
+ partialencoding = this.encoding[part_of_addon][a_or_b];
216
+ result += partialencoding;
217
+
218
+ // séparateur de formule 01
219
+ if(i < 4){
220
+ result += "01";
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+ return(result);
227
+ },
228
+ compute: function (code, type){
229
+ var len = type == "ean13" ? 12 : 7;
230
+ addon = code.substring(13, code.length);
231
+ code = code.substring(0, len);
232
+ var sum = 0, odd = true;
233
+ for(i=code.length-1; i>-1; i--){
234
+ sum += (odd ? 3 : 1) * barcode.intval(code.charAt(i));
235
+ odd = ! odd;
236
+ }
237
+ return(code + ((10 - sum % 10) % 10).toString() + " " + addon);
238
+ }
239
+ },
240
+ upc: {
241
+ getDigit: function(code){
242
+ if (code.length < 12) {
243
+ code = '0' + code;
244
+ }
245
+ return barcode.ean.getDigit(code, 'ean13');
246
+ },
247
+ compute: function (code){
248
+ if (code.length < 12) {
249
+ code = '0' + code;
250
+ }
251
+ return barcode.ean.compute(code, 'ean13').substr(1);
252
+ }
253
+ },
254
+ msi: {
255
+ encoding:["100100100100", "100100100110", "100100110100", "100100110110",
256
+ "100110100100", "100110100110", "100110110100", "100110110110",
257
+ "110100100100", "110100100110"],
258
+ compute: function(code, crc){
259
+ if (typeof(crc) == "object"){
260
+ if (crc.crc1 == "mod10"){
261
+ code = this.computeMod10(code);
262
+ } else if (crc.crc1 == "mod11"){
263
+ code = this.computeMod11(code);
264
+ }
265
+ if (crc.crc2 == "mod10"){
266
+ code = this.computeMod10(code);
267
+ } else if (crc.crc2 == "mod11"){
268
+ code = this.computeMod11(code);
269
+ }
270
+ } else if (typeof(crc) == "boolean"){
271
+ if (crc) code = this.computeMod10(code);
272
+ }
273
+ return(code);
274
+ },
275
+ computeMod10:function(code){
276
+ var i,
277
+ toPart1 = code.length % 2;
278
+ var n1 = 0, sum = 0;
279
+ for(i=0; i<code.length; i++){
280
+ if (toPart1) {
281
+ n1 = 10 * n1 + barcode.intval(code.charAt(i));
282
+ } else {
283
+ sum += barcode.intval(code.charAt(i));
284
+ }
285
+ toPart1 = ! toPart1;
286
+ }
287
+ var s1 = (2 * n1).toString();
288
+ for(i=0; i<s1.length; i++){
289
+ sum += barcode.intval(s1.charAt(i));
290
+ }
291
+ return(code + ((10 - sum % 10) % 10).toString());
292
+ },
293
+ computeMod11:function(code){
294
+ var sum = 0, weight = 2;
295
+ for(var i=code.length-1; i>=0; i--){
296
+ sum += weight * barcode.intval(code.charAt(i));
297
+ weight = weight == 7 ? 2 : weight + 1;
298
+ }
299
+ return(code + ((11 - sum % 11) % 11).toString());
300
+ },
301
+ getDigit: function(code, crc){
302
+ var table = "0123456789";
303
+ var index = 0;
304
+ var result = "";
305
+
306
+ code = this.compute(code, false);
307
+
308
+ // start
309
+ result = "110";
310
+
311
+ // digits
312
+ for(i=0; i<code.length; i++){
313
+ index = table.indexOf( code.charAt(i) );
314
+ if (index < 0) return("");
315
+ result += this.encoding[ index ];
316
+ }
317
+
318
+ // stop
319
+ result += "1001";
320
+
321
+ return(result);
322
+ }
323
+ },
324
+ code11: {
325
+ encoding:[ "101011", "1101011", "1001011", "1100101",
326
+ "1011011", "1101101", "1001101", "1010011",
327
+ "1101001", "110101", "101101"],
328
+ getDigit: function(code){
329
+ var table = "0123456789-";
330
+ var i, index, result = "", intercharacter = '0'
331
+
332
+ // start
333
+ result = "1011001" + intercharacter;
334
+
335
+ // digits
336
+ for(i=0; i<code.length; i++){
337
+ index = table.indexOf( code.charAt(i) );
338
+ if (index < 0) return("");
339
+ result += this.encoding[ index ] + intercharacter;
340
+ }
341
+
342
+ // checksum
343
+ var weightC = 0,
344
+ weightSumC = 0,
345
+ weightK = 1, // start at 1 because the right-most character is "C" checksum
346
+ weightSumK = 0;
347
+ for(i=code.length-1; i>=0; i--){
348
+ weightC = weightC == 10 ? 1 : weightC + 1;
349
+ weightK = weightK == 10 ? 1 : weightK + 1;
350
+
351
+ index = table.indexOf( code.charAt(i) );
352
+
353
+ weightSumC += weightC * index;
354
+ weightSumK += weightK * index;
355
+ }
356
+
357
+ var c = weightSumC % 11;
358
+ weightSumK += c;
359
+ var k = weightSumK % 11;
360
+
361
+ result += this.encoding[c] + intercharacter;
362
+
363
+ if (code.length >= 10){
364
+ result += this.encoding[k] + intercharacter;
365
+ }
366
+
367
+ // stop
368
+ result += "1011001";
369
+
370
+ return(result);
371
+ }
372
+ },
373
+ code39: {
374
+ encoding:["101001101101", "110100101011", "101100101011", "110110010101",
375
+ "101001101011", "110100110101", "101100110101", "101001011011",
376
+ "110100101101", "101100101101", "110101001011", "101101001011",
377
+ "110110100101", "101011001011", "110101100101", "101101100101",
378
+ "101010011011", "110101001101", "101101001101", "101011001101",
379
+ "110101010011", "101101010011", "110110101001", "101011010011",
380
+ "110101101001", "101101101001", "101010110011", "110101011001",
381
+ "101101011001", "101011011001", "110010101011", "100110101011",
382
+ "110011010101", "100101101011", "110010110101", "100110110101",
383
+ "100101011011", "110010101101", "100110101101", "100100100101",
384
+ "100100101001", "100101001001", "101001001001", "100101101101"],
385
+ getDigit: function(code){
386
+ var table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";
387
+ var i, index, result="", intercharacter='0';
388
+
389
+ if (code.indexOf('*') >= 0) return("");
390
+
391
+ // Add Start and Stop charactere : *
392
+ code = ("*" + code + "*").toUpperCase();
393
+
394
+ for(i=0; i<code.length; i++){
395
+ index = table.indexOf( code.charAt(i) );
396
+ if (index < 0) return("");
397
+ if (i > 0) result += intercharacter;
398
+ result += this.encoding[ index ];
399
+ }
400
+ return(result);
401
+ }
402
+ },
403
+ code93:{
404
+ encoding:["100010100", "101001000", "101000100", "101000010",
405
+ "100101000", "100100100", "100100010", "101010000",
406
+ "100010010", "100001010", "110101000", "110100100",
407
+ "110100010", "110010100", "110010010", "110001010",
408
+ "101101000", "101100100", "101100010", "100110100",
409
+ "100011010", "101011000", "101001100", "101000110",
410
+ "100101100", "100010110", "110110100", "110110010",
411
+ "110101100", "110100110", "110010110", "110011010",
412
+ "101101100", "101100110", "100110110", "100111010",
413
+ "100101110", "111010100", "111010010", "111001010",
414
+ "101101110", "101110110", "110101110", "100100110",
415
+ "111011010", "111010110", "100110010", "101011110"],
416
+ getDigit: function(code, crc){
417
+ var table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%____*", // _ => ($), (%), (/) et (+)
418
+ c, result = "";
419
+
420
+ if (code.indexOf('*') >= 0) return("");
421
+
422
+ code = code.toUpperCase();
423
+
424
+ // start : *
425
+ result += this.encoding[47];
426
+
427
+ // digits
428
+ for(i=0; i<code.length; i++){
429
+ c = code.charAt(i);
430
+ index = table.indexOf( c );
431
+ if ( (c == '_') || (index < 0) ) return("");
432
+ result += this.encoding[ index ];
433
+ }
434
+
435
+ // checksum
436
+ if (crc){
437
+ var weightC = 0,
438
+ weightSumC = 0,
439
+ weightK = 1, // start at 1 because the right-most character is "C" checksum
440
+ weightSumK = 0;
441
+ for(i=code.length-1; i>=0; i--){
442
+ weightC = weightC == 20 ? 1 : weightC + 1;
443
+ weightK = weightK == 15 ? 1 : weightK + 1;
444
+
445
+ index = table.indexOf( code.charAt(i) );
446
+
447
+ weightSumC += weightC * index;
448
+ weightSumK += weightK * index;
449
+ }
450
+
451
+ var c = weightSumC % 47;
452
+ weightSumK += c;
453
+ var k = weightSumK % 47;
454
+
455
+ result += this.encoding[c];
456
+ result += this.encoding[k];
457
+ }
458
+
459
+ // stop : *
460
+ result += this.encoding[47];
461
+
462
+ // Terminaison bar
463
+ result += '1';
464
+ return(result);
465
+ }
466
+ },
467
+ code128: {
468
+ encoding:["11011001100", "11001101100", "11001100110", "10010011000",
469
+ "10010001100", "10001001100", "10011001000", "10011000100",
470
+ "10001100100", "11001001000", "11001000100", "11000100100",
471
+ "10110011100", "10011011100", "10011001110", "10111001100",
472
+ "10011101100", "10011100110", "11001110010", "11001011100",
473
+ "11001001110", "11011100100", "11001110100", "11101101110",
474
+ "11101001100", "11100101100", "11100100110", "11101100100",
475
+ "11100110100", "11100110010", "11011011000", "11011000110",
476
+ "11000110110", "10100011000", "10001011000", "10001000110",
477
+ "10110001000", "10001101000", "10001100010", "11010001000",
478
+ "11000101000", "11000100010", "10110111000", "10110001110",
479
+ "10001101110", "10111011000", "10111000110", "10001110110",
480
+ "11101110110", "11010001110", "11000101110", "11011101000",
481
+ "11011100010", "11011101110", "11101011000", "11101000110",
482
+ "11100010110", "11101101000", "11101100010", "11100011010",
483
+ "11101111010", "11001000010", "11110001010", "10100110000",
484
+ "10100001100", "10010110000", "10010000110", "10000101100",
485
+ "10000100110", "10110010000", "10110000100", "10011010000",
486
+ "10011000010", "10000110100", "10000110010", "11000010010",
487
+ "11001010000", "11110111010", "11000010100", "10001111010",
488
+ "10100111100", "10010111100", "10010011110", "10111100100",
489
+ "10011110100", "10011110010", "11110100100", "11110010100",
490
+ "11110010010", "11011011110", "11011110110", "11110110110",
491
+ "10101111000", "10100011110", "10001011110", "10111101000",
492
+ "10111100010", "11110101000", "11110100010", "10111011110",
493
+ "10111101110", "11101011110", "11110101110", "11010000100",
494
+ "11010010000", "11010011100", "11000111010"],
495
+ getDigit: function(code){
496
+ var tableB = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
497
+ var result = "";
498
+ var sum = 0;
499
+ var isum = 0;
500
+ var i = 0;
501
+ var j = 0;
502
+ var value = 0;
503
+
504
+ // check each characters
505
+ for(i=0; i<code.length; i++){
506
+ if (tableB.indexOf(code.charAt(i)) == -1) return("");
507
+ }
508
+
509
+ // check firsts characters : start with C table only if enought numeric
510
+ var tableCActivated = code.length > 1;
511
+ var c = '';
512
+ for(i=0; i<3 && i<code.length; i++){
513
+ c = code.charAt(i);
514
+ tableCActivated &= c >= '0' && c <= '9';
515
+ }
516
+
517
+ sum = tableCActivated ? 105 : 104;
518
+
519
+ // start : [105] : C table or [104] : B table
520
+ result = this.encoding[ sum ];
521
+
522
+ i = 0;
523
+ while( i < code.length ){
524
+ if (! tableCActivated){
525
+ j = 0;
526
+ // check next character to activate C table if interresting
527
+ while ( (i + j < code.length) && (code.charAt(i+j) >= '0') && (code.charAt(i+j) <= '9') ) j++;
528
+
529
+ // 6 min everywhere or 4 mini at the end
530
+ tableCActivated = (j > 5) || ((i + j - 1 == code.length) && (j > 3));
531
+
532
+ if ( tableCActivated ){
533
+ result += this.encoding[ 99 ]; // C table
534
+ sum += ++isum * 99;
535
+ }
536
+ // 2 min for table C so need table B
537
+ } else if ( (i == code.length) || (code.charAt(i) < '0') || (code.charAt(i) > '9') || (code.charAt(i+1) < '0') || (code.charAt(i+1) > '9') ) {
538
+ tableCActivated = false;
539
+ result += this.encoding[ 100 ]; // B table
540
+ sum += ++isum * 100;
541
+ }
542
+
543
+ if ( tableCActivated ) {
544
+ value = barcode.intval(code.charAt(i) + code.charAt(i+1)); // Add two characters (numeric)
545
+ i += 2;
546
+ } else {
547
+ value = tableB.indexOf( code.charAt(i) ); // Add one character
548
+ i += 1;
549
+ }
550
+ result += this.encoding[ value ];
551
+ sum += ++isum * value;
552
+ }
553
+
554
+ // Add CRC
555
+ result += this.encoding[ sum % 103 ];
556
+
557
+ // Stop
558
+ result += this.encoding[106];
559
+
560
+ // Termination bar
561
+ result += "11";
562
+
563
+ return(result);
564
+ }
565
+ },
566
+ codabar: {
567
+ encoding:["101010011", "101011001", "101001011", "110010101",
568
+ "101101001", "110101001", "100101011", "100101101",
569
+ "100110101", "110100101", "101001101", "101100101",
570
+ "1101011011", "1101101011", "1101101101", "1011011011",
571
+ "1011001001", "1010010011", "1001001011", "1010011001"],
572
+ getDigit: function(code){
573
+ var table = "0123456789-$:/.+";
574
+ var i, index, result="", intercharacter = '0';
575
+
576
+ // add start : A->D : arbitrary choose A
577
+ result += this.encoding[16] + intercharacter;
578
+
579
+ for(i=0; i<code.length; i++){
580
+ index = table.indexOf( code.charAt(i) );
581
+ if (index < 0) return("");
582
+ result += this.encoding[ index ] + intercharacter;
583
+ }
584
+
585
+ // add stop : A->D : arbitrary choose A
586
+ result += this.encoding[16];
587
+ return(result);
588
+ }
589
+ },
590
+ datamatrix: {
591
+ lengthRows: [ 10, 12, 14, 16, 18, 20, 22, 24, 26, // 24 squares et 6 rectangular
592
+ 32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144,
593
+ 8, 8, 12, 12, 16, 16],
594
+ lengthCols: [ 10, 12, 14, 16, 18, 20, 22, 24, 26, // Number of columns for the entire datamatrix
595
+ 32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144,
596
+ 18, 32, 26, 36, 36, 48],
597
+ dataCWCount: [ 3, 5, 8, 12, 18, 22, 30, 36, // Number of data codewords for the datamatrix
598
+ 44, 62, 86, 114, 144, 174, 204, 280, 368, 456, 576, 696, 816, 1050,
599
+ 1304, 1558, 5, 10, 16, 22, 32, 49],
600
+ solomonCWCount: [ 5, 7, 10, 12, 14, 18, 20, 24, 28, // Number of Reed-Solomon codewords for the datamatrix
601
+ 36, 42, 48, 56, 68, 84, 112, 144, 192, 224, 272, 336, 408, 496, 620,
602
+ 7, 11, 14, 18, 24, 28],
603
+ dataRegionRows: [ 8, 10, 12, 14, 16, 18, 20, 22, // Number of rows per region
604
+ 24, 14, 16, 18, 20, 22, 24, 14, 16, 18, 20, 22, 24, 18, 20, 22,
605
+ 6, 6, 10, 10, 14, 14],
606
+ dataRegionCols: [ 8, 10, 12, 14, 16, 18, 20, 22, // Number of columns per region
607
+ 24, 14, 16, 18, 20, 22, 24, 14, 16, 18, 20, 22, 24, 18, 20, 22,
608
+ 16, 14, 24, 16, 16, 22],
609
+ regionRows: [ 1, 1, 1, 1, 1, 1, 1, 1, // Number of regions per row
610
+ 1, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 6, 6, 6,
611
+ 1, 1, 1, 1, 1, 1],
612
+ regionCols: [ 1, 1, 1, 1, 1, 1, 1, 1, // Number of regions per column
613
+ 1, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 6, 6, 6,
614
+ 1, 2, 1, 2, 2, 2],
615
+ interleavedBlocks:[ 1, 1, 1, 1, 1, 1, 1, 1, // Number of blocks
616
+ 1, 1, 1, 1, 1, 1, 2, 2, 4, 4, 4, 4, 6, 6, 8, 8,
617
+ 1, 1, 1, 1, 1, 1],
618
+ logTab: [ -255, 255, 1, 240, 2, 225, 241, 53, 3, // Table of log for the Galois field
619
+ 38, 226, 133, 242, 43, 54, 210, 4, 195, 39, 114, 227, 106, 134, 28,
620
+ 243, 140, 44, 23, 55, 118, 211, 234, 5, 219, 196, 96, 40, 222, 115,
621
+ 103, 228, 78, 107, 125, 135, 8, 29, 162, 244, 186, 141, 180, 45, 99,
622
+ 24, 49, 56, 13, 119, 153, 212, 199, 235, 91, 6, 76, 220, 217, 197,
623
+ 11, 97, 184, 41, 36, 223, 253, 116, 138, 104, 193, 229, 86, 79, 171,
624
+ 108, 165, 126, 145, 136, 34, 9, 74, 30, 32, 163, 84, 245, 173, 187,
625
+ 204, 142, 81, 181, 190, 46, 88, 100, 159, 25, 231, 50, 207, 57, 147,
626
+ 14, 67, 120, 128, 154, 248, 213, 167, 200, 63, 236, 110, 92, 176, 7,
627
+ 161, 77, 124, 221, 102, 218, 95, 198, 90, 12, 152, 98, 48, 185, 179,
628
+ 42, 209, 37, 132, 224, 52, 254, 239, 117, 233, 139, 22, 105, 27, 194,
629
+ 113, 230, 206, 87, 158, 80, 189, 172, 203, 109, 175, 166, 62, 127,
630
+ 247, 146, 66, 137, 192, 35, 252, 10, 183, 75, 216, 31, 83, 33, 73,
631
+ 164, 144, 85, 170, 246, 65, 174, 61, 188, 202, 205, 157, 143, 169, 82,
632
+ 72, 182, 215, 191, 251, 47, 178, 89, 151, 101, 94, 160, 123, 26, 112,
633
+ 232, 21, 51, 238, 208, 131, 58, 69, 148, 18, 15, 16, 68, 17, 121, 149,
634
+ 129, 19, 155, 59, 249, 70, 214, 250, 168, 71, 201, 156, 64, 60, 237,
635
+ 130, 111, 20, 93, 122, 177, 150],
636
+ aLogTab: [ 1, 2, 4, 8, 16, 32, 64, 128, 45, 90, // Table of aLog for the Galois field
637
+ 180, 69, 138, 57, 114, 228, 229, 231, 227, 235, 251, 219, 155, 27, 54,
638
+ 108, 216, 157, 23, 46, 92, 184, 93, 186, 89, 178, 73, 146, 9, 18, 36,
639
+ 72, 144, 13, 26, 52, 104, 208, 141, 55, 110, 220, 149, 7, 14, 28, 56,
640
+ 112, 224, 237, 247, 195, 171, 123, 246, 193, 175, 115, 230, 225, 239,
641
+ 243, 203, 187, 91, 182, 65, 130, 41, 82, 164, 101, 202, 185, 95, 190,
642
+ 81, 162, 105, 210, 137, 63, 126, 252, 213, 135, 35, 70, 140, 53, 106,
643
+ 212, 133, 39, 78, 156, 21, 42, 84, 168, 125, 250, 217, 159, 19, 38, 76,
644
+ 152, 29, 58, 116, 232, 253, 215, 131, 43, 86, 172, 117, 234, 249, 223,
645
+ 147, 11, 22, 44, 88, 176, 77, 154, 25, 50, 100, 200, 189, 87, 174, 113,
646
+ 226, 233, 255, 211, 139, 59, 118, 236, 245, 199, 163, 107, 214, 129,
647
+ 47, 94, 188, 85, 170, 121, 242, 201, 191, 83, 166, 97, 194, 169, 127,
648
+ 254, 209, 143, 51, 102, 204, 181, 71, 142, 49, 98, 196, 165, 103, 206,
649
+ 177, 79, 158, 17, 34, 68, 136, 61, 122, 244, 197, 167, 99, 198, 161,
650
+ 111, 222, 145, 15, 30, 60, 120, 240, 205, 183, 67, 134, 33, 66, 132,
651
+ 37, 74, 148, 5, 10, 20, 40, 80, 160, 109, 218, 153, 31, 62, 124, 248,
652
+ 221, 151, 3, 6, 12, 24, 48, 96, 192, 173, 119, 238, 241, 207, 179, 75,
653
+ 150, 1],
654
+ champGaloisMult: function(a, b){ // MULTIPLICATION IN GALOIS FIELD GF(2^8)
655
+ if(!a || !b) return 0;
656
+ return this.aLogTab[(this.logTab[a] + this.logTab[b]) % 255];
657
+ },
658
+ champGaloisDoub: function(a, b){ // THE OPERATION a * 2^b IN GALOIS FIELD GF(2^8)
659
+ if (!a) return 0;
660
+ if (!b) return a;
661
+ return this.aLogTab[(this.logTab[a] + b) % 255];
662
+ },
663
+ champGaloisSum: function(a, b){ // SUM IN GALOIS FIELD GF(2^8)
664
+ return a ^ b;
665
+ },
666
+ selectIndex: function(dataCodeWordsCount, rectangular){ // CHOOSE THE GOOD INDEX FOR TABLES
667
+ if ((dataCodeWordsCount<1 || dataCodeWordsCount>1558) && !rectangular) return -1;
668
+ if ((dataCodeWordsCount<1 || dataCodeWordsCount>49) && rectangular) return -1;
669
+
670
+ var n = 0;
671
+ if ( rectangular ) n = 24;
672
+
673
+ while (this.dataCWCount[n] < dataCodeWordsCount) n++;
674
+ return n;
675
+ },
676
+ encodeDataCodeWordsASCII: function(text) {
677
+ var dataCodeWords = new Array();
678
+ var n = 0, i, c;
679
+ for (i=0; i<text.length; i++){
680
+ c = text.charCodeAt(i);
681
+ if (c > 127) {
682
+ dataCodeWords[n] = 235;
683
+ c = c - 127;
684
+ n++;
685
+ } else if ((c>=48 && c<=57) && (i+1<text.length) && (text.charCodeAt(i+1)>=48 && text.charCodeAt(i+1)<=57)) {
686
+ c = ((c - 48) * 10) + ((text.charCodeAt(i+1))-48);
687
+ c += 130;
688
+ i++;
689
+ } else c++;
690
+ dataCodeWords[n] = c;
691
+ n++;
692
+ }
693
+ return dataCodeWords;
694
+ },
695
+ addPadCW: function(tab, from, to){
696
+ if (from >= to) return ;
697
+ tab[from] = 129;
698
+ var r, i;
699
+ for (i=from+1; i<to; i++){
700
+ r = ((149 * (i+1)) % 253) + 1;
701
+ tab[i] = (129 + r) % 254;
702
+ }
703
+ },
704
+ calculSolFactorTable: function(solomonCWCount){ // CALCULATE THE REED SOLOMON FACTORS
705
+ var g = new Array();
706
+ var i, j;
707
+
708
+ for (i=0; i<=solomonCWCount; i++) g[i] = 1;
709
+
710
+ for(i = 1; i <= solomonCWCount; i++) {
711
+ for(j = i - 1; j >= 0; j--) {
712
+ g[j] = this.champGaloisDoub(g[j], i);
713
+ if(j > 0) g[j] = this.champGaloisSum(g[j], g[j-1]);
714
+ }
715
+ }
716
+ return g;
717
+ },
718
+ addReedSolomonCW: function(nSolomonCW, coeffTab, nDataCW, dataTab, blocks){ // Add the Reed Solomon codewords
719
+ var temp = 0;
720
+ var errorBlocks = nSolomonCW / blocks;
721
+ var correctionCW = new Array();
722
+
723
+ var i,j,k;
724
+ for(k = 0; k < blocks; k++) {
725
+ for (i=0; i<errorBlocks; i++) correctionCW[i] = 0;
726
+
727
+ for (i=k; i<nDataCW; i=i+blocks){
728
+ temp = this.champGaloisSum(dataTab[i], correctionCW[errorBlocks-1]);
729
+ for (j=errorBlocks-1; j>=0; j--){
730
+ if ( !temp ) {
731
+ correctionCW[j] = 0;
732
+ } else {
733
+ correctionCW[j] = this.champGaloisMult(temp, coeffTab[j]);
734
+ }
735
+ if (j>0) correctionCW[j] = this.champGaloisSum(correctionCW[j-1], correctionCW[j]);
736
+ }
737
+ }
738
+ // Renversement des blocs calcules
739
+ j = nDataCW + k;
740
+ for (i=errorBlocks-1; i>=0; i--){
741
+ dataTab[j] = correctionCW[i];
742
+ j=j+blocks;
743
+ }
744
+ }
745
+ return dataTab;
746
+ },
747
+ getBits: function(entier){ // Transform integer to tab of bits
748
+ var bits = new Array();
749
+ for (var i=0; i<8; i++){
750
+ bits[i] = entier & (128 >> i) ? 1 : 0;
751
+ }
752
+ return bits;
753
+ },
754
+ next: function(etape, totalRows, totalCols, codeWordsBits, datamatrix, assigned){ // Place codewords into the matrix
755
+ var chr = 0; // Place of the 8st bit from the first character to [4][0]
756
+ var row = 4;
757
+ var col = 0;
758
+
759
+ do {
760
+ // Check for a special case of corner
761
+ if((row == totalRows) && (col == 0)){
762
+ this.patternShapeSpecial1(datamatrix, assigned, codeWordsBits[chr], totalRows, totalCols);
763
+ chr++;
764
+ } else if((etape<3) && (row == totalRows-2) && (col == 0) && (totalCols%4 != 0)){
765
+ this.patternShapeSpecial2(datamatrix, assigned, codeWordsBits[chr], totalRows, totalCols);
766
+ chr++;
767
+ } else if((row == totalRows-2) && (col == 0) && (totalCols%8 == 4)){
768
+ this.patternShapeSpecial3(datamatrix, assigned, codeWordsBits[chr], totalRows, totalCols);
769
+ chr++;
770
+ }
771
+ else if((row == totalRows+4) && (col == 2) && (totalCols%8 == 0)){
772
+ this.patternShapeSpecial4(datamatrix, assigned, codeWordsBits[chr], totalRows, totalCols);
773
+ chr++;
774
+ }
775
+
776
+ // Go up and right in the datamatrix
777
+ do {
778
+ if((row < totalRows) && (col >= 0) && (assigned[row][col]!=1)) {
779
+ this.patternShapeStandard(datamatrix, assigned, codeWordsBits[chr], row, col, totalRows, totalCols);
780
+ chr++;
781
+ }
782
+ row -= 2;
783
+ col += 2;
784
+ } while ((row >= 0) && (col < totalCols));
785
+ row += 1;
786
+ col += 3;
787
+
788
+ // Go down and left in the datamatrix
789
+ do {
790
+ if((row >= 0) && (col < totalCols) && (assigned[row][col]!=1)){
791
+ this.patternShapeStandard(datamatrix, assigned, codeWordsBits[chr], row, col, totalRows, totalCols);
792
+ chr++;
793
+ }
794
+ row += 2;
795
+ col -= 2;
796
+ } while ((row < totalRows) && (col >=0));
797
+ row += 3;
798
+ col += 1;
799
+ } while ((row < totalRows) || (col < totalCols));
800
+ },
801
+ patternShapeStandard: function(datamatrix, assigned, bits, row, col, totalRows, totalCols){ // Place bits in the matrix (standard or special case)
802
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[0], row-2, col-2, totalRows, totalCols);
803
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[1], row-2, col-1, totalRows, totalCols);
804
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[2], row-1, col-2, totalRows, totalCols);
805
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[3], row-1, col-1, totalRows, totalCols);
806
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[4], row-1, col, totalRows, totalCols);
807
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[5], row, col-2, totalRows, totalCols);
808
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[6], row, col-1, totalRows, totalCols);
809
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[7], row, col, totalRows, totalCols);
810
+ },
811
+ patternShapeSpecial1: function(datamatrix, assigned, bits, totalRows, totalCols ){
812
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[0], totalRows-1, 0, totalRows, totalCols);
813
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[1], totalRows-1, 1, totalRows, totalCols);
814
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[2], totalRows-1, 2, totalRows, totalCols);
815
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[3], 0, totalCols-2, totalRows, totalCols);
816
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[4], 0, totalCols-1, totalRows, totalCols);
817
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[5], 1, totalCols-1, totalRows, totalCols);
818
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[6], 2, totalCols-1, totalRows, totalCols);
819
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[7], 3, totalCols-1, totalRows, totalCols);
820
+ },
821
+ patternShapeSpecial2: function(datamatrix, assigned, bits, totalRows, totalCols ){
822
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[0], totalRows-3, 0, totalRows, totalCols);
823
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[1], totalRows-2, 0, totalRows, totalCols);
824
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[2], totalRows-1, 0, totalRows, totalCols);
825
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[3], 0, totalCols-4, totalRows, totalCols);
826
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[4], 0, totalCols-3, totalRows, totalCols);
827
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[5], 0, totalCols-2, totalRows, totalCols);
828
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[6], 0, totalCols-1, totalRows, totalCols);
829
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[7], 1, totalCols-1, totalRows, totalCols);
830
+ },
831
+ patternShapeSpecial3: function(datamatrix, assigned, bits, totalRows, totalCols ){
832
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[0], totalRows-3, 0, totalRows, totalCols);
833
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[1], totalRows-2, 0, totalRows, totalCols);
834
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[2], totalRows-1, 0, totalRows, totalCols);
835
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[3], 0, totalCols-2, totalRows, totalCols);
836
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[4], 0, totalCols-1, totalRows, totalCols);
837
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[5], 1, totalCols-1, totalRows, totalCols);
838
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[6], 2, totalCols-1, totalRows, totalCols);
839
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[7], 3, totalCols-1, totalRows, totalCols);
840
+ },
841
+ patternShapeSpecial4: function(datamatrix, assigned, bits, totalRows, totalCols ){
842
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[0], totalRows-1, 0, totalRows, totalCols);
843
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[1], totalRows-1, totalCols-1, totalRows, totalCols);
844
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[2], 0, totalCols-3, totalRows, totalCols);
845
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[3], 0, totalCols-2, totalRows, totalCols);
846
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[4], 0, totalCols-1, totalRows, totalCols);
847
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[5], 1, totalCols-3, totalRows, totalCols);
848
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[6], 1, totalCols-2, totalRows, totalCols);
849
+ this.placeBitInDatamatrix(datamatrix, assigned, bits[7], 1, totalCols-1, totalRows, totalCols);
850
+ },
851
+ placeBitInDatamatrix: function(datamatrix, assigned, bit, row, col, totalRows, totalCols){ // Put a bit into the matrix
852
+ if (row < 0) {
853
+ row += totalRows;
854
+ col += 4 - ((totalRows+4)%8);
855
+ }
856
+ if (col < 0) {
857
+ col += totalCols;
858
+ row += 4 - ((totalCols+4)%8);
859
+ }
860
+ if (assigned[row][col] != 1) {
861
+ datamatrix[row][col] = bit;
862
+ assigned[row][col] = 1;
863
+ }
864
+ },
865
+ addFinderPattern: function(datamatrix, rowsRegion, colsRegion, rowsRegionCW, colsRegionCW){ // Add the finder pattern
866
+ var totalRowsCW = (rowsRegionCW+2) * rowsRegion;
867
+ var totalColsCW = (colsRegionCW+2) * colsRegion;
868
+
869
+ var datamatrixTemp = new Array();
870
+ datamatrixTemp[0] = new Array();
871
+ for (var j=0; j<totalColsCW+2; j++){
872
+ datamatrixTemp[0][j] = 0;
873
+ }
874
+ for (var i=0; i<totalRowsCW; i++){
875
+ datamatrixTemp[i+1] = new Array();
876
+ datamatrixTemp[i+1][0] = 0;
877
+ datamatrixTemp[i+1][totalColsCW+1] = 0;
878
+ for (var j=0; j<totalColsCW; j++){
879
+ if (i%(rowsRegionCW+2) == 0){
880
+ if (j%2 == 0){
881
+ datamatrixTemp[i+1][j+1] = 1;
882
+ } else {
883
+ datamatrixTemp[i+1][j+1] = 0;
884
+ }
885
+ } else if (i%(rowsRegionCW+2) == rowsRegionCW+1){
886
+ datamatrixTemp[i+1][j+1] = 1;
887
+ } else if (j%(colsRegionCW+2) == colsRegionCW+1){
888
+ if (i%2 == 0){
889
+ datamatrixTemp[i+1][j+1] = 0;
890
+ } else {
891
+ datamatrixTemp[i+1][j+1] = 1;
892
+ }
893
+ } else if (j%(colsRegionCW+2) == 0){
894
+ datamatrixTemp[i+1][j+1] = 1;
895
+ } else{
896
+ datamatrixTemp[i+1][j+1] = 0;
897
+ datamatrixTemp[i+1][j+1] = datamatrix[i-1-(2*(parseInt(i/(rowsRegionCW+2))))][j-1-(2*(parseInt(j/(colsRegionCW+2))))];
898
+ }
899
+ }
900
+ }
901
+ datamatrixTemp[totalRowsCW+1] = new Array();
902
+ for (var j=0; j<totalColsCW+2; j++){
903
+ datamatrixTemp[totalRowsCW+1][j] = 0;
904
+ }
905
+ return datamatrixTemp;
906
+ },
907
+ getDigit: function(text, rectangular){
908
+ var dataCodeWords = this.encodeDataCodeWordsASCII(text); // Code the text in the ASCII mode
909
+ var dataCWCount = dataCodeWords.length;
910
+ var index = this.selectIndex(dataCWCount, rectangular); // Select the index for the data tables
911
+ var totalDataCWCount = this.dataCWCount[index]; // Number of data CW
912
+ var solomonCWCount = this.solomonCWCount[index]; // Number of Reed Solomon CW
913
+ var totalCWCount = totalDataCWCount + solomonCWCount; // Number of CW
914
+ var rowsTotal = this.lengthRows[index]; // Size of symbol
915
+ var colsTotal = this.lengthCols[index];
916
+ var rowsRegion = this.regionRows[index]; // Number of region
917
+ var colsRegion = this.regionCols[index];
918
+ var rowsRegionCW = this.dataRegionRows[index];
919
+ var colsRegionCW = this.dataRegionCols[index];
920
+ var rowsLengthMatrice = rowsTotal-2*rowsRegion; // Size of matrice data
921
+ var colsLengthMatrice = colsTotal-2*colsRegion;
922
+ var blocks = this.interleavedBlocks[index]; // Number of Reed Solomon blocks
923
+ var errorBlocks = (solomonCWCount / blocks);
924
+
925
+ this.addPadCW(dataCodeWords, dataCWCount, totalDataCWCount); // Add codewords pads
926
+
927
+ var g = this.calculSolFactorTable(errorBlocks); // Calculate correction coefficients
928
+
929
+ this.addReedSolomonCW(solomonCWCount, g, totalDataCWCount, dataCodeWords, blocks); // Add Reed Solomon codewords
930
+
931
+ var codeWordsBits = new Array(); // Calculte bits from codewords
932
+ for (var i=0; i<totalCWCount; i++){
933
+ codeWordsBits[i] = this.getBits(dataCodeWords[i]);
934
+ }
935
+
936
+ var datamatrix = new Array(); // Put data in the matrix
937
+ var assigned = new Array();
938
+
939
+ for (var i=0; i<colsLengthMatrice; i++){
940
+ datamatrix[i] = new Array();
941
+ assigned[i] = new Array();
942
+ }
943
+
944
+ // Add the bottom-right corner if needed
945
+ if ( ((rowsLengthMatrice * colsLengthMatrice) % 8) == 4) {
946
+ datamatrix[rowsLengthMatrice-2][colsLengthMatrice-2] = 1;
947
+ datamatrix[rowsLengthMatrice-1][colsLengthMatrice-1] = 1;
948
+ datamatrix[rowsLengthMatrice-1][colsLengthMatrice-2] = 0;
949
+ datamatrix[rowsLengthMatrice-2][colsLengthMatrice-1] = 0;
950
+ assigned[rowsLengthMatrice-2][colsLengthMatrice-2] = 1;
951
+ assigned[rowsLengthMatrice-1][colsLengthMatrice-1] = 1;
952
+ assigned[rowsLengthMatrice-1][colsLengthMatrice-2] = 1;
953
+ assigned[rowsLengthMatrice-2][colsLengthMatrice-1] = 1;
954
+ }
955
+
956
+ // Put the codewords into the matrix
957
+ this.next(0,rowsLengthMatrice,colsLengthMatrice, codeWordsBits, datamatrix, assigned);
958
+
959
+ // Add the finder pattern
960
+ datamatrix = this.addFinderPattern(datamatrix, rowsRegion, colsRegion, rowsRegionCW, colsRegionCW);
961
+
962
+ return datamatrix;
963
+ }
964
+ },
965
+ // little endian convertor
966
+ lec:{
967
+ // convert an int
968
+ cInt: function(value, byteCount){
969
+ var le = '';
970
+ for(var i=0; i<byteCount; i++){
971
+ le += String.fromCharCode(value & 0xFF);
972
+ value = value >> 8;
973
+ }
974
+ return le;
975
+ },
976
+ // return a byte string from rgb values
977
+ cRgb: function(r,g,b){
978
+ return String.fromCharCode(b) + String.fromCharCode(g) + String.fromCharCode(r);
979
+ },
980
+ // return a byte string from a hex string color
981
+ cHexColor: function(hex){
982
+ var v = parseInt('0x' + hex.substr(1));
983
+ var b = v & 0xFF;
984
+ v = v >> 8;
985
+ var g = v & 0xFF;
986
+ var r = v >> 8;
987
+ return(this.cRgb(r,g,b));
988
+ }
989
+ },
990
+ hexToRGB: function(hex){
991
+ var v = parseInt('0x' + hex.substr(1));
992
+ var b = v & 0xFF;
993
+ v = v >> 8;
994
+ var g = v & 0xFF;
995
+ var r = v >> 8;
996
+ return({r:r,g:g,b:b});
997
+ },
998
+ // test if a string is a hexa string color (like #FF0000)
999
+ isHexColor: function (value){
1000
+ var r = new RegExp("#[0-91-F]", "gi");
1001
+ return value.match(r);
1002
+ },
1003
+ // encode data in base64
1004
+ base64Encode: function(value) {
1005
+ var r = '', c1, c2, c3, b1, b2, b3, b4;
1006
+ var k = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
1007
+ var i = 0;
1008
+ while (i < value.length) {
1009
+ c1 = value.charCodeAt(i++);
1010
+ c2 = value.charCodeAt(i++);
1011
+ c3 = value.charCodeAt(i++);
1012
+ b1 = c1 >> 2;
1013
+ b2 = ((c1 & 3) << 4) | (c2 >> 4);
1014
+ b3 = ((c2 & 15) << 2) | (c3 >> 6);
1015
+ b4 = c3 & 63;
1016
+ if (isNaN(c2)) b3 = b4 = 64;
1017
+ else if (isNaN(c3)) b4 = 64;
1018
+ r += k.charAt(b1) + k.charAt(b2) + k.charAt(b3) + k.charAt(b4);
1019
+ }
1020
+ return r;
1021
+ },
1022
+ // convert a bit string to an array of array of bit char
1023
+ bitStringTo2DArray: function( digit ){
1024
+ var d = []; d[0] = [];
1025
+ for(var i=0; i<digit.length; i++) d[0][i] = digit.charAt(i);
1026
+ return(d);
1027
+ },
1028
+ // clear jQuery Target
1029
+ resize: function($container, w){
1030
+ $container
1031
+ .css("padding", "0px")
1032
+ .css("overflow", "auto")
1033
+ .css("width", w + "px")
1034
+ .html("");
1035
+ return $container;
1036
+ },
1037
+ // bmp barcode renderer
1038
+ digitToBmpRenderer: function($container, settings, digit, hri, mw, mh){
1039
+ var lines = digit.length;
1040
+ var columns = digit[0].length;
1041
+ var i = 0;
1042
+ var c0 = this.isHexColor(settings.bgColor) ? this.lec.cHexColor(settings.bgColor) : this.lec.cRgb(255,255,255);
1043
+ var c1 = this.isHexColor(settings.color) ? this.lec.cHexColor(settings.color) : this.lec.cRgb(0,0,0);
1044
+ var bar0 = '';
1045
+ var bar1 = '';
1046
+
1047
+ // create one bar 0 and 1 of "mw" byte length
1048
+ for(i=0; i<mw; i++){
1049
+ bar0 += c0;
1050
+ bar1 += c1;
1051
+ }
1052
+ var bars = '';
1053
+
1054
+ var padding = (4 - ((mw * columns * 3) % 4)) % 4; // Padding for 4 byte alignment ("* 3" come from "3 byte to color R, G and B")
1055
+ var dataLen = (mw * columns + padding) * mh * lines;
1056
+
1057
+ var pad = '';
1058
+ for(i=0; i<padding; i++) pad += '\0';
1059
+
1060
+ // Bitmap header
1061
+ var bmp = 'BM' + // Magic Number
1062
+ this.lec.cInt(54 + dataLen, 4) + // Size of Bitmap size (header size + data len)
1063
+ '\0\0\0\0' + // Unused
1064
+ this.lec.cInt(54, 4) + // The offset where the bitmap data (pixels) can be found
1065
+ this.lec.cInt(40, 4) + // The number of bytes in the header (from this point).
1066
+ this.lec.cInt(mw * columns, 4) + // width
1067
+ this.lec.cInt(mh * lines, 4) + // height
1068
+ this.lec.cInt(1, 2) + // Number of color planes being used
1069
+ this.lec.cInt(24, 2) + // The number of bits/pixel
1070
+ '\0\0\0\0' + // BI_RGB, No compression used
1071
+ this.lec.cInt(dataLen, 4) + // The size of the raw BMP data (after this header)
1072
+ this.lec.cInt(2835, 4) + // The horizontal resolution of the image (pixels/meter)
1073
+ this.lec.cInt(2835, 4) + // The vertical resolution of the image (pixels/meter)
1074
+ this.lec.cInt(0, 4) + // Number of colors in the palette
1075
+ this.lec.cInt(0, 4); // Means all colors are important
1076
+ // Bitmap Data
1077
+ for(var y=lines-1; y>=0; y--){
1078
+ var line = '';
1079
+ for (var x=0; x<columns; x++){
1080
+ line += digit[y][x] == '0' ? bar0 : bar1;
1081
+ }
1082
+ line += pad;
1083
+ for(var k=0; k<mh; k++){
1084
+ bmp += line;
1085
+ }
1086
+ }
1087
+ // set bmp image to the container
1088
+ var object = document.createElement('object');
1089
+ object.setAttribute('type', 'image/bmp');
1090
+ object.setAttribute('data', 'data:image/bmp;base64,'+ this.base64Encode(bmp));
1091
+ this.resize($container, mw * columns + padding).append(object);
1092
+
1093
+ },
1094
+ // bmp 1D barcode renderer
1095
+ digitToBmp: function($container, settings, digit, hri){
1096
+ var w = barcode.intval(settings.barWidth);
1097
+ var h = barcode.intval(settings.barHeight);
1098
+ this.digitToBmpRenderer($container, settings, this.bitStringTo2DArray(digit), hri, w, h);
1099
+ },
1100
+ // bmp 2D barcode renderer
1101
+ digitToBmp2D: function($container, settings, digit, hri){
1102
+ var s = barcode.intval(settings.moduleSize);
1103
+ this.digitToBmpRenderer($container, settings, digit, hri, s, s);
1104
+ },
1105
+ // css barcode renderer
1106
+ digitToCssRenderer : function($container, settings, digit, hri, mw, mh){
1107
+ var lines = digit.length;
1108
+ var columns = digit[0].length;
1109
+ var content = "";
1110
+ var bar0 = "<div style=\"float: left; font-size: 0px; background-color: " + settings.bgColor + "; height: " + mh + "px; width: &Wpx\"></div>";
1111
+ var bar1 = "<div style=\"float: left; font-size: 0px; width:0; border-left: &Wpx solid " + settings.color + "; height: " + mh + "px;\"></div>";
1112
+
1113
+ var len, current;
1114
+ for(var y=0; y<lines; y++){
1115
+ len = 0;
1116
+ current = digit[y][0];
1117
+ for (var x=0; x<columns; x++){
1118
+ if ( current == digit[y][x] ) {
1119
+ len++;
1120
+ } else {
1121
+ content += (current == '0' ? bar0 : bar1).replace("&W", len * mw);
1122
+ current = digit[y][x];
1123
+ len=1;
1124
+ }
1125
+ }
1126
+ if (len > 0){
1127
+ content += (current == '0' ? bar0 : bar1).replace("&W", len * mw);
1128
+ }
1129
+ }
1130
+ if (settings.showHRI){
1131
+ content += "<div style=\"clear:both; width: 100%; background-color: " + settings.bgColor + "; color: " + settings.color + "; text-align: center; font-size: " + settings.fontSize + "px; margin-top: " + settings.marginHRI + "px;\">"+hri+"</div>";
1132
+ }
1133
+ this.resize($container, mw * columns).html(content);
1134
+ },
1135
+ // css 1D barcode renderer
1136
+ digitToCss: function($container, settings, digit, hri){
1137
+ var w = barcode.intval(settings.barWidth);
1138
+ var h = barcode.intval(settings.barHeight);
1139
+ this.digitToCssRenderer($container, settings, this.bitStringTo2DArray(digit), hri, w, h);
1140
+ },
1141
+ // css 2D barcode renderer
1142
+ digitToCss2D: function($container, settings, digit, hri){
1143
+ var s = barcode.intval(settings.moduleSize);
1144
+ this.digitToCssRenderer($container, settings, digit, hri, s, s);
1145
+ },
1146
+ // svg barcode renderer
1147
+ digitToSvgRenderer: function($container, settings, digit, hri, mw, mh){
1148
+ var lines = digit.length;
1149
+ var columns = digit[0].length;
1150
+
1151
+ var width = mw * columns;
1152
+ var height = mh * lines;
1153
+ if (settings.showHRI){
1154
+ var fontSize = barcode.intval(settings.fontSize);
1155
+ height += barcode.intval(settings.marginHRI) + fontSize;
1156
+ }
1157
+
1158
+ // svg header
1159
+ var svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' + width + '" height="' + height + '">';
1160
+
1161
+ // background
1162
+ svg += '<rect width="' + width + '" height="' + height + '" x="0" y="0" fill="' + settings.bgColor + '" />';
1163
+
1164
+ var bar1 = '<rect width="&W" height="' + mh + '" x="&X" y="&Y" fill="' + settings.color + '" />';
1165
+
1166
+ var len, current;
1167
+ for(var y=0; y<lines; y++){
1168
+ len = 0;
1169
+ current = digit[y][0];
1170
+ for (var x=0; x<columns; x++){
1171
+ if ( current == digit[y][x] ) {
1172
+ len++;
1173
+ } else {
1174
+ if (current == '1') {
1175
+ svg += bar1.replace("&W", len * mw).replace("&X", (x - len) * mw).replace("&Y", y * mh);
1176
+ }
1177
+ current = digit[y][x];
1178
+ len=1;
1179
+ }
1180
+ }
1181
+ if ( (len > 0) && (current == '1') ){
1182
+ svg += bar1.replace("&W", len * mw).replace("&X", (columns - len) * mw).replace("&Y", y * mh);
1183
+ }
1184
+ }
1185
+
1186
+ if (settings.showHRI){
1187
+ svg += '<g transform="translate(' + Math.floor(width/2) + ' 0)">';
1188
+ svg += '<text y="' + (height - Math.floor(fontSize/2)) + '" text-anchor="middle" style="font-family: Arial; font-size: ' + fontSize + 'px;" fill="' + settings.color + '">' + hri + '</text>';
1189
+ svg += '</g>';
1190
+ }
1191
+ // svg footer
1192
+ svg += '</svg>';
1193
+
1194
+ // create a dom object, flush container and add object to the container
1195
+ var object = document.createElement('object');
1196
+ object.setAttribute('type', 'image/svg+xml');
1197
+ object.setAttribute('data', 'data:image/svg+xml,'+ svg);
1198
+ this.resize($container, width).append(object);
1199
+ },
1200
+ // svg 1D barcode renderer
1201
+ digitToSvg: function($container, settings, digit, hri){
1202
+ var w = barcode.intval(settings.barWidth);
1203
+ var h = barcode.intval(settings.barHeight);
1204
+ this.digitToSvgRenderer($container, settings, this.bitStringTo2DArray(digit), hri, w, h);
1205
+ },
1206
+ // svg 2D barcode renderer
1207
+ digitToSvg2D: function($container, settings, digit, hri){
1208
+ var s = barcode.intval(settings.moduleSize);
1209
+ this.digitToSvgRenderer($container, settings, digit, hri, s, s);
1210
+ },
1211
+
1212
+ // canvas barcode renderer
1213
+ digitToCanvasRenderer : function($container, settings, digit, hri, xi, yi, mw, mh){
1214
+ var canvas = $container.get(0);
1215
+ if ( !canvas || !canvas.getContext ) return; // not compatible
1216
+
1217
+ var lines = digit.length;
1218
+ var columns = digit[0].length;
1219
+
1220
+ var ctx = canvas.getContext('2d');
1221
+ ctx.lineWidth = 1;
1222
+ ctx.lineCap = 'butt';
1223
+ ctx.fillStyle = settings.bgColor;
1224
+ ctx.fillRect (xi, yi, columns * mw, lines * mh);
1225
+
1226
+ ctx.fillStyle = settings.color;
1227
+
1228
+ for(var y=0; y<lines; y++){
1229
+ var len = 0;
1230
+ var current = digit[y][0];
1231
+ for(var x=0; x<columns; x++){
1232
+ if (current == digit[y][x]) {
1233
+ len++;
1234
+ } else {
1235
+ if (current == '1'){
1236
+ ctx.fillRect (xi + (x - len) * mw, yi + y * mh, mw * len, mh);
1237
+ }
1238
+ current = digit[y][x];
1239
+ len=1;
1240
+ }
1241
+ }
1242
+ if ( (len > 0) && (current == '1') ){
1243
+ ctx.fillRect (xi + (columns - len) * mw, yi + y * mh, mw * len, mh);
1244
+ }
1245
+ }
1246
+ if (settings.showHRI){
1247
+ var dim = ctx.measureText(hri);
1248
+ ctx.fillText(hri, xi + Math.floor((columns * mw - dim.width)/2), yi + lines * mh + settings.fontSize + settings.marginHRI);
1249
+ }
1250
+ },
1251
+ // canvas 1D barcode renderer
1252
+ digitToCanvas: function($container, settings, digit, hri){
1253
+ var w = barcode.intval(settings.barWidth);
1254
+ var h = barcode.intval(settings.barHeight);
1255
+ var x = barcode.intval(settings.posX);
1256
+ var y = barcode.intval(settings.posY);
1257
+ this.digitToCanvasRenderer($container, settings, this.bitStringTo2DArray(digit), hri, x, y, w, h);
1258
+ },
1259
+ // canvas 2D barcode renderer
1260
+ digitToCanvas2D: function($container, settings, digit, hri){
1261
+ var s = barcode.intval(settings.moduleSize);
1262
+ var x = barcode.intval(settings.posX);
1263
+ var y = barcode.intval(settings.posY);
1264
+ this.digitToCanvasRenderer($container, settings, digit, hri, x, y, s, s);
1265
+ }
1266
+ };
1267
+
1268
+ $.fn.extend({
1269
+ barcode: function(datas, type, settings) {
1270
+ var digit = "",
1271
+ hri = "",
1272
+ code = "",
1273
+ crc = true,
1274
+ rect = false,
1275
+ b2d = false;
1276
+
1277
+ if (typeof(datas) == "string"){
1278
+ code = datas;
1279
+ } else if (typeof(datas) == "object"){
1280
+ code = typeof(datas.code) == "string" ? datas.code : "";
1281
+ crc = typeof(datas.crc) != "undefined" ? datas.crc : true;
1282
+ rect = typeof(datas.rect) != "undefined" ? datas.rect : false;
1283
+ }
1284
+ if (code == "") return(false);
1285
+
1286
+ if (typeof(settings) == "undefined") settings = [];
1287
+ for(var name in barcode.settings){
1288
+ if (settings[name] == undefined) settings[name] = barcode.settings[name];
1289
+ }
1290
+
1291
+ switch(type){
1292
+ case "std25":
1293
+ case "int25":
1294
+ digit = barcode.i25.getDigit(code, crc, type);
1295
+ hri = barcode.i25.compute(code, crc, type);
1296
+ break;
1297
+ case "ean8":
1298
+ case "ean13":
1299
+ digit = barcode.ean.getDigit(code, type);
1300
+ hri = barcode.ean.compute(code, type);
1301
+ break;
1302
+ case "upc":
1303
+ digit = barcode.upc.getDigit(code);
1304
+ hri = barcode.upc.compute(code);
1305
+ break;
1306
+ case "code11":
1307
+ digit = barcode.code11.getDigit(code);
1308
+ hri = code;
1309
+ break;
1310
+ case "code39":
1311
+ digit = barcode.code39.getDigit(code);
1312
+ hri = code;
1313
+ break;
1314
+ case "code93":
1315
+ digit = barcode.code93.getDigit(code, crc);
1316
+ hri = code;
1317
+ break;
1318
+ case "code128":
1319
+ digit = barcode.code128.getDigit(code);
1320
+ hri = code;
1321
+ break;
1322
+ case "codabar":
1323
+ digit = barcode.codabar.getDigit(code);
1324
+ hri = code;
1325
+ break;
1326
+ case "msi":
1327
+ digit = barcode.msi.getDigit(code, crc);
1328
+ hri = barcode.msi.compute(code, crc);
1329
+ break;
1330
+ case "datamatrix":
1331
+ digit = barcode.datamatrix.getDigit(code, rect);
1332
+ hri = code;
1333
+ b2d = true;
1334
+ break;
1335
+ }
1336
+ if (digit.length == 0) return($(this));
1337
+
1338
+ // Quiet Zone
1339
+ if ( !b2d && settings.addQuietZone) digit = "0000000000" + digit + "0000000000";
1340
+
1341
+ var $this = $(this);
1342
+ var fname = 'digitTo' + settings.output.charAt(0).toUpperCase() + settings.output.substr(1) + (b2d ? '2D' : '');
1343
+ if (typeof(barcode[fname]) == 'function') {
1344
+ barcode[fname]($this, settings, digit, hri);
1345
+ }
1346
+
1347
+ return($this);
1348
+ }
1349
+ });
1350
+
1351
+ }(jQuery));