api/js/jquery/barcode/jquery-barcode.js   F
last analyzed

Complexity

Total Complexity 230
Complexity/F 4.11

Size

Lines of Code 1282
Function Count 56

Duplication

Duplicated Lines 52
Ratio 4.06 %

Importance

Changes 0
Metric Value
eloc 932
dl 52
loc 1282
rs 1.668
c 0
b 0
f 0
wmc 230
mnd 174
bc 174
fnc 56
bpm 3.1071
cpm 4.1071
noi 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like api/js/jquery/barcode/jquery-barcode.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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