Coverage

96%
547
530
17

/home/johan/DevZone/JsBarcode/JsBarcode.js

86%
130
113
17
LineHitsSource
11(function(){
2
3 // Main function, calls drawCanvas(...) in the right way
41 var JsBarcode = function(image, content, options){
5 // If the image is a string, query select call again
621 if(typeof image === "string"){
71 image = document.querySelector(image);
80 JsBarcode(image, content, options);
9 }
10 // If image, draw on canvas and set the uri as src
1120 else if(typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLImageElement){
120 canvas = document.createElement('canvas');
130 drawCanvas(canvas, content, options);
140 image.setAttribute("src", canvas.toDataURL());
15 }
16 // If canvas, just draw
1720 else if(image.getContext){
1819 drawCanvas(image, content, options);
19 }
20 else{
211 throw new Error("Not supported type to draw on.");
22 }
23 }
24
25 // The main function, handles everything with the modules and draws the image
261 var drawCanvas = function(canvas, content, options) {
27 // Merge the user options with the default
2819 options = merge(JsBarcode.defaults, options);
29
30 // Fix the margins
3119 options.marginTop = options.marginTop | options.margin;
3219 options.marginBottom = options.marginBottom | options.margin;
3319 options.marginRight = options.marginRight | options.margin;
3419 options.marginLeft = options.marginLeft | options.margin;
35
36 //Abort if the browser does not support HTML5 canvas
3719 if (!canvas.getContext) {
380 throw new Error('The browser does not support canvas.');
39 }
40
41 // Automatically choose barcode if format set to "auto"...
4219 if(options.format == "auto"){
433 var encoder = new (JsBarcode.autoSelectEncoder(content))(content);
44 }
45 // ...or else, get by name
46 else{
4716 var encoder = new (JsBarcode.getModule(options.format))(content);
48 }
49
50 //Abort if the barcode format does not support the content
5118 if(!encoder.valid()){
522 options.valid(false);
532 if(options.valid == JsBarcode.defaults.valid){
541 throw new Error('The data is not valid for the type of barcode.');
55 }
561 return;
57 }
58
59 // Set the binary to a cached version if possible
6016 var cachedBinary = JsBarcode.getCache(options.format, content);
6116 if(cachedBinary){
629 var binary = cachedBinary;
63 }
64 else{
65 // Encode the content
667 var binary = encoder.encoded();
67 // Cache the encoding if it will be used again later
687 JsBarcode.cache(options.format, content, binary);
69 }
70
71 // Get the canvas context
7216 var ctx = canvas.getContext("2d");
73
74 // Set font
7516 var font = options.fontOptions + " " + options.fontSize + "px "+options.font;
7616 ctx.font = font;
77
78 // Set the width and height of the barcode
7916 var width = binary.length*options.width;
80 // Replace with width of the text if it is wider then the barcode
8116 var textWidth = ctx.measureText(encoder.getText()).width;
8216 if(options.displayValue && width < textWidth){
830 if(options.textAlign == "center"){
840 var barcodePadding = Math.floor((textWidth - width)/2);
85 }
860 else if(options.textAlign == "left"){
870 var barcodePadding = 0;
88 }
890 else if(options.textAlign == "right"){
900 var barcodePadding = Math.floor(textWidth - width);
91 }
92
930 width = textWidth;
94 }
95 // Make sure barcodePadding is not undefined
9616 var barcodePadding = barcodePadding || 0;
97
9816 canvas.width = width + options.marginLeft + options.marginRight;
99
100 // Set extra height if the value is displayed under the barcode. Multiplication with 1.3 t0 ensure that some
101 //characters are not cut in half
10216 canvas.height = options.height
103 + (options.displayValue ? options.fontSize : 0)
104 + options.textMargin
105 + options.marginTop
106 + options.marginBottom;
107
108 // Paint the canvas
10916 ctx.clearRect(0,0,canvas.width,canvas.height);
11016 if(options.background){
11116 ctx.fillStyle = options.background;
11216 ctx.fillRect(0,0,canvas.width, canvas.height);
113 }
114
115 // Creates the barcode out of the encoded binary
11616 ctx.fillStyle = options.lineColor;
11716 for(var i=0;i<binary.length;i++){
1181538 var x = i*options.width + options.marginLeft + barcodePadding;
1191538 if(binary[i] == "1"){
120730 ctx.fillRect(x, options.marginTop, options.width, options.height);
121 }
122 }
123
124 // Draw the text if displayValue is set
12516 if(options.displayValue){
12615 var x, y;
127
12815 y = options.height + options.textMargin + options.marginTop;
129
13015 ctx.font = font;
13115 ctx.textBaseline = "bottom";
13215 ctx.textBaseline = 'top';
133
134 // Draw the text in the correct X depending on the textAlign option
13515 if(options.textAlign == "left" || barcodePadding > 0){
1361 x = options.marginLeft;
1371 ctx.textAlign = 'left';
138 }
13914 else if(options.textAlign == "right"){
1401 x = canvas.width - options.marginRight;
1411 ctx.textAlign = 'right';
142 }
143 //In all other cases, center the text
144 else{
14513 x = canvas.width / 2;
14613 ctx.textAlign = 'center';
147 }
148
14915 ctx.fillText(encoder.getText(), x, y);
150 }
151
152 // Send a confirmation that the generation was successful to the valid function if it does exist
15316 options.valid(true);
154 };
155
1561 JsBarcode._modules = [];
157
158 // Add a new module sorted in the array
1591 JsBarcode.register = function(module, regex, priority){
16017 var position = 0;
16117 if(typeof priority === "undefined"){
1624 position = JsBarcode._modules.length - 1;
163 }
164 else{
16513 for(var i=0;i<JsBarcode._modules.length;i++){
16638 position = i;
16738 if(!(priority < JsBarcode._modules[i].priority)){
16810 break;
169 }
170 }
171 }
172
173 // Add the module in position position
17417 JsBarcode._modules.splice(position, 0, {
175 "regex": regex,
176 "module": module,
177 "priority": priority
178 });
179 };
180
181 // Get module by name
1821 JsBarcode.getModule = function(name){
18333 for(var i in JsBarcode._modules){
184368 if(name.search(JsBarcode._modules[i].regex) !== -1){
18532 return JsBarcode._modules[i].module;
186 }
187 }
1881 throw new Error('Module ' + name + ' does not exist or is not loaded.');
189 };
190
191 // If any format is valid with the content, return the format with highest priority
1921 JsBarcode.autoSelectEncoder = function(content){
1933 for(var i in JsBarcode._modules){
19417 var barcode = new (JsBarcode._modules[i].module)(content);
19517 if(barcode.valid(content)){
1963 return JsBarcode._modules[i].module;
197 }
198 }
1990 throw new Error("Can't automatically find a barcode format matching the string '" + content + "'");
200 };
201
202 // Defining the cache dictionary
2031 JsBarcode._cache = {};
204
205 // Cache a regerated barcode
2061 JsBarcode.cache = function(format, input, output){
2077 if(!JsBarcode._cache[format]){
2084 JsBarcode._cache[format] = {};
209 }
2107 JsBarcode._cache[format][input] = output;
211 };
212
213 // Get a chached barcode
2141 JsBarcode.getCache = function(format, input){
21516 if(JsBarcode._cache[format]){
21612 if(JsBarcode._cache[format][input]){
2179 return JsBarcode._cache[format][input];
218 }
219 }
2207 return "";
221 };
222
223 // Detect if the code is running under nodejs
2241 JsBarcode._isNode = false;
2251 if (typeof module !== 'undefined' && module.exports) {
2261 module.exports = JsBarcode; // Export to nodejs
2271 JsBarcode._isNode = true;
228
229 //Register all modules in ./barcodes/
2301 var path = require("path");
2311 var dir = path.join(__dirname, "barcodes");
2321 var files = require("fs").readdirSync(dir);
2331 for(var i in files){
2348 var barcode = require(path.join(dir, files[i]));
2358 barcode.register(JsBarcode);
236 }
237 }
238
239 //Regsiter JsBarcode for the browser
2401 if(typeof window !== 'undefined'){
2410 window.JsBarcode = JsBarcode;
242 }
243
244 // Register JsBarcode as an jQuery plugin if jQuery exist
2451 if (typeof jQuery !== 'undefined') {
2460 jQuery.fn.JsBarcode = function(content, options, validFunction){
2470 JsBarcode(this.get(0), content, options, validFunction);
2480 return this;
249 };
250 }
251
252 // All the default options. If one is not set.
2531 JsBarcode.defaults = {
254 width: 2,
255 height: 100,
256 format: "auto",
257 displayValue: true,
258 fontOptions: "",
259 font: "monospace",
260 textAlign: "center",
261 textMargin: 2,
262 fontSize: 20,
263 background: "#ffffff",
264 lineColor: "#000000",
265 margin: 10,
266 marginTop: undefined,
267 marginBottom: undefined,
268 marginLeft: undefined,
269 marginRight: undefined,
270 valid: function(valid){}
271 };
272
273 // Function to merge the default options with the default ones
2741 var merge = function(m1, m2) {
27519 var newMerge = {};
27619 for (var k in m1) {
277323 newMerge[k] = m1[k];
278 }
27919 for (var k in m2) {
28026 if(typeof m2[k] !== "undefined"){
28126 newMerge[k] = m2[k];
282 }
283 }
28419 return newMerge;
285 };
286})();
287

/home/johan/DevZone/JsBarcode/barcodes/CODE128.js

100%
133
133
0
LineHitsSource
1// ASCII value ranges 0-127, 200-211
21var validCODE128 = /^[\x00-\x7F\xC8-\xD3]+$/;
3
4// This is the master class, it does require the start code to be
5//included in the string
61function CODE128(string) {
7 // Fill the bytes variable with the ascii codes of string
818 this.bytes = [];
918 for (var i = 0; i < string.length; ++i) {
10139 this.bytes.push(string.charCodeAt(i));
11 }
12
13 // First element should be startcode, remove that
1418 this.string = string.substring(1);
15
1618 this.getText = function() {
1719 return this.string//.replace(/[^\x20-\x7E]/g, "");
18 };
19
20 // The public encoding function
2118 this.encoded = function() {
228 var encodingResult;
238 var bytes = this.bytes;
24 // Remove the startcode from the bytes and set its index
258 var startIndex = bytes.shift() - 105;
26
27 // Start encode with the right type
288 if(startIndex === 103){
293 encodingResult = nextA(bytes, 1);
30 }
315 else if(startIndex === 104){
323 encodingResult = nextB(bytes, 1);
33 }
342 else if(startIndex === 105){
352 encodingResult = nextC(bytes, 1);
36 }
37
388 return (
39 //Add the start bits
40 getEncoding(startIndex) +
41 //Add the encoded bits
42 encodingResult.result +
43 //Add the checksum
44 getEncoding((encodingResult.checksum + startIndex) % 103) +
45 //Add the end bits
46 getEncoding(106)
47 );
48 }
49
50 //Data for each character, the last characters will not be encoded but are used for error correction
51 //Numbers encode to (n + 1000) -> binary; 740 -> (740 + 1000).toString(2) -> "11011001100"
5218 var code128b = [ // + 1000
53 740, 644, 638, 176, 164, 100, 224, 220, 124, 608, 604,
54 572, 436, 244, 230, 484, 260, 254, 650, 628, 614, 764,
55 652, 902, 868, 836, 830, 892, 844, 842, 752, 734, 590,
56 304, 112, 94, 416, 128, 122, 672, 576, 570, 464, 422,
57 134, 496, 478, 142, 910, 678, 582, 768, 762, 774, 880,
58 862, 814, 896, 890, 818, 914, 602, 930, 328, 292, 200,
59 158, 68, 62, 424, 412, 232, 218, 76, 74, 554, 616,
60 978, 556, 146, 340, 212, 182, 508, 268, 266, 956, 940,
61 938, 758, 782, 974, 400, 310, 118, 512, 506, 960, 954,
62 502, 518, 886, 966, /* Start codes */ 668, 680, 692,
63 5379
64 ];
6518 var getEncoding = function(n) {
6684 return (code128b[n] ? (code128b[n] + 1000).toString(2) : '');
67 };
68
69 // Use the regexp variable for validation
7018 this.valid = function() {
7111 return !(this.string.search(validCODE128) === -1);
72 }
73
7418 function nextA(bytes, depth){
7522 if(bytes.length <= 0){
762 return {"result": "", "checksum": 0};
77 }
78
7920 var next, index;
80
81 // Special characters
8220 if(bytes[0] >= 200){
834 index = bytes[0] - 105;
84
85 //Remove first element
864 bytes.shift();
87
88 // Swap to CODE128C
894 if(index === 99){
901 next = nextC(bytes, depth + 1);
91 }
92 // Swap to CODE128B
933 else if(index === 100){
942 next = nextB(bytes, depth + 1);
95 }
96 // Continue on CODE128A but encode a special character
97 else{
981 next = nextA(bytes, depth + 1);
99 }
100 }
101 // Continue encoding of CODE128A
102 else{
10316 var charCode = bytes[0];
10416 index = charCode < 32 ? charCode + 64 : charCode - 32;
105
106 // Remove first element
10716 bytes.shift();
108
10916 next = nextA(bytes, depth + 1);
110 }
111
112 // Get the correct binary encoding and calculate the weight
11320 var enc = getEncoding(index);
11420 var weight = index * depth;
115
11620 return {"result": enc + next.result, "checksum": weight + next.checksum}
117 }
118
11918 function nextB(bytes, depth){
12027 if(bytes.length <= 0){
1213 return {"result": "", "checksum": 0};
122 }
123
12424 var next, index;
125
126 // Special characters
12724 if(bytes[0] >= 200){
1284 index = bytes[0] - 105;
129
130 //Remove first element
1314 bytes.shift();
132
133 // Swap to CODE128C
1344 if(index === 99){
1352 next = nextC(bytes, depth + 1);
136 }
137 // Swap to CODE128A
1382 else if(index === 101){
1391 next = nextA(bytes, depth + 1);
140 }
141 // Continue on CODE128B but encode a special character
142 else{
1431 next = nextB(bytes, depth + 1);
144 }
145 }
146 // Continue encoding of CODE128B
147 else {
14820 index = bytes[0] - 32;
14920 bytes.shift();
15020 next = nextB(bytes, depth + 1);
151 }
152
153 // Get the correct binary encoding and calculate the weight
15424 var enc = getEncoding(index);
15524 var weight = index * depth;
156
15724 return {"result": enc + next.result, "checksum": weight + next.checksum};
158 }
159
16018 function nextC(bytes, depth){
16119 if(bytes.length <= 0){
1623 return {"result": "", "checksum": 0};
163 }
164
16516 var next, index;
166
167 // Special characters
16816 if(bytes[0] >= 200){
1693 index = bytes[0] - 105;
170
171 // Remove first element
1723 bytes.shift();
173
174 // Swap to CODE128B
1753 if(index === 100){
1761 next = nextB(bytes, depth + 1);
177 }
178 // Swap to CODE128A
1792 else if(index === 101){
1801 next = nextA(bytes, depth + 1);
181 }
182 // Continue on CODE128C but encode a special character
183 else{
1841 next = nextC(bytes, depth + 1);
185 }
186 }
187 // Continue encoding of CODE128C
188 else{
18913 index = (bytes[0]-48) * 10 + bytes[1]-48;
19013 bytes.shift();
19113 bytes.shift();
19213 next = nextC(bytes, depth + 1);
193 }
194
195 // Get the correct binary encoding and calculate the weight
19616 var enc = getEncoding(index);
19716 var weight = index * depth;
198
19916 return {"result": enc + next.result, "checksum": weight + next.checksum};
200 }
201}
202
2031function autoSelectModes(string){
20414 var aLength = string.match(/^[\x00-\x5F]*/)[0].length;
20514 var bLength = string.match(/^[\x20-\x7F]*/)[0].length;
20614 var cLength = string.match(/^([0-9]{2})*/)[0].length;
207
208 // Select CODE128C if the string start with enough digits
20914 if(cLength >= 2){
2101 return String.fromCharCode(210) + autoSelectFromC(string);
211 }
212 // Select A/C depending on the longest match
21313 else if(aLength > bLength){
2142 return String.fromCharCode(208) + autoSelectFromA(string);
215 }
216 else{
21711 return String.fromCharCode(209) + autoSelectFromB(string);
218 }
219}
220
2211function autoSelectFromA(string){
2224 var untilC = string.match(/^([\x00-\x5F\xC8-\xCF]+?)(([0-9]{2}){2,})([^0-9]|$)/);
223
2244 if(untilC){
2251 return untilC[1] + String.fromCharCode(204) + autoSelectFromC(string.substring(untilC[1].length));
226 }
227
2283 var aChars = string.match(/^[\x00-\x5F\xC8-\xCF]+/);
2293 if(aChars[0].length === string.length){
2301 return string;
231 }
232
2332 return aChars[0] + String.fromCharCode(205) + autoSelectFromB(string.substring(aChars[0].length));
234}
235
2361function autoSelectFromB(string){
23714 var untilC = string.match(/^([\x20-\x7F\xC8-\xCF]+?)(([0-9]{2}){2,})([^0-9]|$)/);
238
23914 if(untilC){
2402 return untilC[1] + String.fromCharCode(204) + autoSelectFromC(string.substring(untilC[1].length));
241 }
242
24312 var bChars = string.match(/^[\x20-\x7F\xC8-\xCF]+/);
24412 if(bChars[0].length === string.length){
24511 return string;
246 }
247
2481 return bChars[0] + String.fromCharCode(206) + autoSelectFromA(string.substring(bChars[0].length));
249}
250
251
2521function autoSelectFromC(string){
2534 var cMatch = string.match(/^(\xCF*[0-9]{2}\xCF*)+/)[0];
2544 var length = cMatch.length;
255
2564 if(length === string.length){
2572 return string;
258 }
259
2602 string = string.substring(length);
261
262 // Select A/B depending on the longest match
2632 var aLength = string.match(/^[\x00-\x5F\xC8-\xCF]*/)[0].length;
2642 var bLength = string.match(/^[\x20-\x7F\xC8-\xCF]*/)[0].length;
2652 if(aLength > bLength){
2661 return cMatch + String.fromCharCode(206) + autoSelectFromA(string);
267 }
268 else{
2691 return cMatch + String.fromCharCode(205) + autoSelectFromB(string);
270 }
271}
272
2731function CODE128AUTO(string) {
274 // Check the validity of the string, don't even bother auto it when
275 //it's not valid
27615 if(string.search(validCODE128) !== -1){
27714 return new CODE128(autoSelectModes(string));
278 }
2791 return new CODE128(string);
280}
2811function CODE128A(string) {
2821 return new CODE128(String.fromCharCode(208) + string);
283}
2841function CODE128B(string) {
2851 return new CODE128(String.fromCharCode(209) + string);
286}
2871function CODE128C(string) {
2881 return new CODE128(String.fromCharCode(210) + string);
289}
290
291//Required to register for both browser and nodejs
2921var register = function(core) {
2931 core.register(CODE128AUTO, /^CODE128(.?AUTO)?$/, 10);
2941 core.register(CODE128A, /^CODE128.?A$/i, 2);
2951 core.register(CODE128B, /^CODE128.?B$/i, 3);
2961 core.register(CODE128C, /^CODE128.?C$/i, 2);
297}
2982try {register(JsBarcode)} catch(e) {}
2992try {module.exports.register = register} catch(e) {}
300

/home/johan/DevZone/JsBarcode/barcodes/CODE39.js

100%
18
18
0
LineHitsSource
11function CODE39(string){
29 this.string = string.toUpperCase();
3
49 var encodings = {
5 "0": 20957, "1": 29783, "2": 23639, "3": 30485,
6 "4": 20951, "5": 29813, "6": 23669, "7": 20855,
7 "8": 29789, "9": 23645, "A": 29975, "B": 23831,
8 "C": 30533, "D": 22295, "E": 30149, "F": 24005,
9 "G": 21623, "H": 29981, "I": 23837, "J": 22301,
10 "K": 30023, "L": 23879, "M": 30545, "N": 22343,
11 "O": 30161, "P": 24017, "Q": 21959, "R": 30065,
12 "S": 23921, "T": 22385, "U": 29015, "V": 18263,
13 "W": 29141, "X": 17879, "Y": 29045, "Z": 18293,
14 "-": 17783, ".": 29021, " ": 18269, "$": 17477,
15 "/": 17489, "+": 17681, "%": 20753, "*": 35770
16 };
17
18
199 this.getText = function(){
209 return this.string;
21 };
22
239 this.encoded = function(){
245 var result = "";
255 result += encodings["*"].toString(2);
265 for(var i=0; i<this.string.length; i++){
2724 result += encodings[this.string[i]].toString(2) + "0";
28 }
295 result += encodings["*"].toString(2);
30
315 return result;
32 };
33
34 //Use the regexp variable for validation
359 this.valid = function(){
367 return this.string.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/) !== -1;
37 };
38}
39
40
41//Required to register for both browser and nodejs
421var register = function(core){
431 core.register(CODE39, /^CODE.?39$/i, 3);
44};
452try{register(JsBarcode)} catch(e){}
462try{module.exports.register = register} catch(e){}
47

/home/johan/DevZone/JsBarcode/barcodes/EAN_UPC.js

100%
99
99
0
LineHitsSource
11function EAN(EANnumber){
215 this.EANnumber = EANnumber+"";
3
4 //Regexp to test if the EAN code is correct formated
515 var fullEanRegexp = /^[0-9]{13}$/;
615 var needLastDigitRegexp = /^[0-9]{12}$/;
7
8 //Add checksum if it does not exist
915 if(this.EANnumber.search(needLastDigitRegexp)!=-1){
102 this.EANnumber += checksum(this.EANnumber);
11 }
12
1315 this.getText = function(){
146 return this.EANnumber;
15 };
16
1715 this.valid = function(){
1812 return valid(this.EANnumber);
19 };
20
2115 this.encoded = function (){
224 return createEAN13(this.EANnumber);
23 }
24
25 //Create the binary representation of the EAN code
26 //number needs to be a string
2715 function createEAN13(number){
284 var encoder = new EANencoder();
29
30 //Create the return variable
314 var result = "";
32
334 var structure = encoder.getEANstructure(number);
34
35 //Get the number to be encoded on the left side of the EAN code
364 var leftSide = number.substr(1,7);
37
38 //Get the number to be encoded on the right side of the EAN code
394 var rightSide = number.substr(7,6);
40
41 //Add the start bits
424 result += encoder.startBin;
43
44 //Add the left side
454 result += encoder.encode(leftSide, structure);
46
47 //Add the middle bits
484 result += encoder.middleBin;
49
50 //Add the right side
514 result += encoder.encode(rightSide,"RRRRRR");
52
53 //Add the end bits
544 result += encoder.endBin;
55
564 return result;
57 }
58
59 //Calulate the checksum digit
6015 function checksum(number){
616 var result = 0;
62
6342 for(var i=0;i<12;i+=2){result+=parseInt(number[i])}
6442 for(var i=1;i<12;i+=2){result+=parseInt(number[i])*3}
65
666 return (10 - (result % 10)) % 10;
67 }
68
6915 function valid(number){
7012 if(number.search(fullEanRegexp)!=-1){
714 return number[12] == checksum(number);
72 }
73 else{
748 return false;
75 }
76 }
77}
78
791function EAN8(EAN8number){
808 this.EAN8number = EAN8number+"";
81
82 //Regexp to test if the EAN code is correct formated
838 var fullEanRegexp = /^[0-9]{8}$/;
848 var needLastDigitRegexp = /^[0-9]{7}$/;
85
86 //Add checksum if it does not exist
878 if(this.EAN8number.search(needLastDigitRegexp)!=-1){
881 this.EAN8number += checksum(this.EAN8number);
89 }
90
918 this.getText = function(){
921 return this.EAN8number;
93 }
94
958 this.valid = function(){
968 return valid(this.EAN8number);
97 };
98
998 this.encoded = function (){
1002 return createEAN8(this.EAN8number);
101 }
102
1038 function valid(number){
1048 if(number.search(fullEanRegexp)!=-1){
1053 return number[7] == checksum(number);
106 }
107 else{
1085 return false;
109 }
110 }
111
112 //Calulate the checksum digit
1138 function checksum(number){
1144 var result = 0;
115
11620 for(var i=0;i<7;i+=2){result+=parseInt(number[i])*3}
11716 for(var i=1;i<7;i+=2){result+=parseInt(number[i])}
118
1194 return (10 - (result % 10)) % 10;
120 }
121
1228 function createEAN8(number){
1232 var encoder = new EANencoder();
124
125 //Create the return variable
1262 var result = "";
127
128 //Get the number to be encoded on the left side of the EAN code
1292 var leftSide = number.substr(0,4);
130
131 //Get the number to be encoded on the right side of the EAN code
1322 var rightSide = number.substr(4,4);
133
134 //Add the start bits
1352 result += encoder.startBin;
136
137 //Add the left side
1382 result += encoder.encode(leftSide, "LLLL");
139
140 //Add the middle bits
1412 result += encoder.middleBin;
142
143 //Add the right side
1442 result += encoder.encode(rightSide,"RRRR");
145
146 //Add the end bits
1472 result += encoder.endBin;
148
1492 return result;
150 }
151}
152
153
1541function UPC(UPCnumber){
1556 this.ean = new EAN("0"+UPCnumber);
156
1576 this.getText = function(){
1581 return this.ean.getText().substring(1);
159 }
160
1616 this.valid = function(){
1624 return this.ean.valid();
163 }
164
1656 this.encoded = function(){
1661 return this.ean.encoded();
167 }
168
169}
170
171//
172// Help class that does all the encoding
173//
1741function EANencoder(){
175 //The start bits
1766 this.startBin = "101";
177 //The end bits
1786 this.endBin = "101";
179 //The middle bits
1806 this.middleBin = "01010";
181
182 //The L (left) type of encoding
1836 var Lbinary = {
184 0: "0001101",
185 1: "0011001",
186 2: "0010011",
187 3: "0111101",
188 4: "0100011",
189 5: "0110001",
190 6: "0101111",
191 7: "0111011",
192 8: "0110111",
193 9: "0001011"};
194
195 //The G type of encoding
1966 var Gbinary = {
197 0: "0100111",
198 1: "0110011",
199 2: "0011011",
200 3: "0100001",
201 4: "0011101",
202 5: "0111001",
203 6: "0000101",
204 7: "0010001",
205 8: "0001001",
206 9: "0010111"};
207
208 //The R (right) type of encoding
2096 var Rbinary = {
210 0: "1110010",
211 1: "1100110",
212 2: "1101100",
213 3: "1000010",
214 4: "1011100",
215 5: "1001110",
216 6: "1010000",
217 7: "1000100",
218 8: "1001000",
219 9: "1110100"};
220
221 //The left side structure in EAN-13
2226 var EANstructure = {
223 0: "LLLLLL",
224 1: "LLGLGG",
225 2: "LLGGLG",
226 3: "LLGGGL",
227 4: "LGLLGG",
228 5: "LGGLLG",
229 6: "LGGGLL",
230 7: "LGLGLG",
231 8: "LGLGGL",
232 9: "LGGLGL"}
233
2346 this.getEANstructure = function(number){
2354 return EANstructure[number[0]];
236 };
237
238 //Convert a numberarray to the representing
2396 this.encode = function(number,structure){
240 //Create the variable that should be returned at the end of the function
24112 var result = "";
242
243 //Loop all the numbers
24412 for(var i = 0;i<number.length;i++){
245 //Using the L, G or R encoding and add it to the returning variable
24668 if(structure[i]=="L"){
24723 result += Lbinary[number[i]];
248 }
24945 else if(structure[i]=="G"){
2509 result += Gbinary[number[i]];
251 }
25236 else if(structure[i]=="R"){
25332 result += Rbinary[number[i]];
254 }
255 }
25612 return result;
257 };
258}
259
260
261//Required to register for both browser and nodejs
2621var register = function(core){
2631 core.register(EAN, /^EAN(.?13)?$/i, 8);
2641 core.register(EAN8, /^EAN.?8$/i, 8);
2651 core.register(UPC, /^UPC(.?A)?$/i, 8);
266}
2672try{register(JsBarcode)} catch(e){}
2682try{module.exports.register = register} catch(e){}
269

/home/johan/DevZone/JsBarcode/barcodes/ITF.js

100%
31
31
0
LineHitsSource
11function ITF(ITFNumber){
2
36 this.ITFNumber = ITFNumber+"";
4
56 this.getText = function(){
61 return this.ITFNumber;
7 };
8
96 this.valid = function(){
104 return valid(this.ITFNumber);
11 };
12
136 this.encoded = function(){
14 //Create the variable that should be returned at the end of the function
151 var result = "";
16
17 //Always add the same start bits
181 result += startBin;
19
20 //Calculate all the digit pairs
211 for(var i=0;i<this.ITFNumber.length;i+=2){
223 result += calculatePair(this.ITFNumber.substr(i,2));
23 }
24
25 //Always add the same end bits
261 result += endBin;
27
281 return result;
29 }
30
31 //The structure for the all digits, 1 is wide and 0 is narrow
326 var digitStructure = {
33 "0":"00110"
34 ,"1":"10001"
35 ,"2":"01001"
36 ,"3":"11000"
37 ,"4":"00101"
38 ,"5":"10100"
39 ,"6":"01100"
40 ,"7":"00011"
41 ,"8":"10010"
42 ,"9":"01010"}
43
44 //The start bits
456 var startBin = "1010";
46 //The end bits
476 var endBin = "11101";
48
49 //Regexp for a valid Inter25 code
506 var regexp = /^([0-9][0-9])+$/;
51
52 //Calculate the data of a number pair
536 function calculatePair(twoNumbers){
543 var result = "";
55
563 var number1Struct = digitStructure[twoNumbers[0]];
573 var number2Struct = digitStructure[twoNumbers[1]];
58
59 //Take every second bit and add to the result
603 for(var i=0;i<5;i++){
6115 result += (number1Struct[i]=="1") ? "111" : "1";
6215 result += (number2Struct[i]=="1") ? "000" : "0";
63 }
643 return result;
65 }
66
676 function valid(number){
684 return number.search(regexp)!==-1;
69 }
70}
71
72//Required to register for both browser and nodejs
731var register = function(core){
741 core.register(ITF, /^ITF$/i, 4);
75};
762try{register(JsBarcode)} catch(e){}
772try{module.exports.register = register} catch(e){}
78

/home/johan/DevZone/JsBarcode/barcodes/ITF14.js

100%
41
41
0
LineHitsSource
11function ITF14(string){
29 this.string = string+"";
3}
4
51ITF14.prototype.getText = function(){
61 return this.string;
7};
8
91ITF14.prototype.valid = function(){
106 return valid(this.string);
11};
12
131ITF14.prototype.encoded = function(){
14 //Create the variable that should be returned at the end of the function
152 var result = "";
16
17 //If checksum is not already calculated, do it
182 if(this.string.length == 13){
191 this.string += checksum(this.string);
20 }
21
22 //Always add the same start bits
232 result += startBin;
24
25 //Calculate all the digit pairs
262 for(var i=0;i<14;i+=2){
2714 result += calculatePair(this.string.substr(i,2));
28 }
29
30 //Always add the same end bits
312 result += endBin;
32
332 return result;
34};
35
36//The structure for the all digits, 1 is wide and 0 is narrow
371var digitStructure = {
38 "0":"00110"
39,"1":"10001"
40,"2":"01001"
41,"3":"11000"
42,"4":"00101"
43,"5":"10100"
44,"6":"01100"
45,"7":"00011"
46,"8":"10010"
47,"9":"01010"}
48
49//The start bits
501var startBin = "1010";
51//The end bits
521var endBin = "11101";
53
54//Regexp for a valid ITF14 code
551var regexp = /^[0-9]{13,14}$/;
56
57//Calculate the data of a number pair
581function calculatePair(twoNumbers){
5914 var result = "";
60
6114 var number1Struct = digitStructure[twoNumbers[0]];
6214 var number2Struct = digitStructure[twoNumbers[1]];
63
64 //Take every second bit and add to the result
6514 for(var i=0;i<5;i++){
6670 result += (number1Struct[i]=="1") ? "111" : "1";
6770 result += (number2Struct[i]=="1") ? "000" : "0";
68 }
6914 return result;
70}
71
72//Calulate the checksum digit
731function checksum(numberString){
743 var result = 0;
75
7642 for(var i=0;i<13;i++){result+=parseInt(numberString[i])*(3-(i%2)*2)}
77
783 return 10 - (result % 10);
79}
80
811function valid(number){
826 if(number.search(regexp)==-1){
833 return false;
84 }
85 //Check checksum if it is already calculated
863 else if(number.length==14){
872 return number[13] == checksum(number);
88 }
891 return true;
90}
91
92//Required to register for both browser and nodejs
931var register = function(core){
941 core.register(ITF14, /^ITF.?14$/i, 5);
95}
962try{register(JsBarcode)} catch(e){}
972try{module.exports.register = register} catch(e){}
98

/home/johan/DevZone/JsBarcode/barcodes/MSI.js

100%
63
63
0
LineHitsSource
11var prototype = {};
2
31prototype.getText = function(){
411 return this.string;
5};
6
71prototype.encoded = function(){
82 var ret = "110";
9
102 for(var i=0;i<this.string.length;i++){
1116 var digit = parseInt(this.string[i]);
1216 var bin = digit.toString(2);
1316 bin = addZeroes(bin, 4-bin.length);
1416 for(var b=0;b<bin.length;b++){
1564 ret += bin[b]==0 ? "100" : "110";
16 }
17 }
18
192 ret += "1001";
202 return ret;
21};
22
231prototype.valid = function(){
247 return this.string.search(/^[0-9]+$/) != -1;
25};
26
271function MSI(string){
285 this.string = ""+string;
29}
30
311MSI.prototype = Object.create(prototype);
32
331function MSI10(string){
343 this.string = ""+string;
353 this.string += mod10(this.string);
36}
371MSI10.prototype = Object.create(prototype);
38
391function MSI11(string){
404 this.string = "" + string;
414 this.string += mod11(this.string);
42}
431MSI11.prototype = Object.create(prototype);
44
451function MSI1010(string){
462 this.string = "" + string;
472 this.string += mod10(this.string);
482 this.string += mod10(this.string);
49}
501MSI1010.prototype = Object.create(prototype);
51
521function MSI1110(string){
532 this.string = "" + string;
542 this.string += mod11(this.string);
552 this.string += mod10(this.string);
56}
571MSI1110.prototype = Object.create(prototype);
58
591function mod10(number){
609 var sum = 0;
619 for(var i=0;i<number.length;i++){
6254 var n = parseInt(number[i]);
6354 if((i + number.length) % 2 == 0){
6424 sum += n;
65 }
66 else{
6730 sum += (n*2)%10 + Math.floor((n*2)/10)
68 }
69 }
709 return (10-(sum%10))%10;
71}
72
731function mod11(number){
746 var sum = 0;
756 var weights = [2,3,4,5,6,7];
766 for(var i=0;i<number.length;i++){
7746 var n = parseInt(number[number.length-1-i]);
7846 sum += weights[i % weights.length] * n;
79 }
806 return (11-(sum%11))%11;
81}
82
831function addZeroes(number, n){
8416 for(var i=0;i<n;i++){
8524 number = "0"+number;
86 }
8716 return number;
88}
89
90//Required to register for both browser and nodejs
911var register = function(core){
921 core.register(MSI, /^MSI$/i, 4);
931 core.register(MSI10, /^MSI.?10$/i);
941 core.register(MSI11, /^MSI.?11$/i);
951 core.register(MSI1010, /^MSI.?1010$/i);
961 core.register(MSI1110, /^MSI.?1110$/i);
97}
982try{register(JsBarcode)} catch(e){}
992try{module.exports.register = register} catch(e){}
100

/home/johan/DevZone/JsBarcode/barcodes/pharmacode.js

100%
32
32
0
LineHitsSource
11function pharmacode(number){
2 //Ensure that the input is inturpreted as a number
36 this.number = parseInt(number);
4
56 this.getText = function(){
61 return this.number + "";
7 };
8
96 function recursiveEncoding(code,state){
10 //End condition
1121 if(code.length == 0) return "";
12
1315 var generated;
1415 var nextState = false;
1515 var nZeros = zeros(code);
1615 if(nZeros == 0){
177 generated = state ? "001" : "00111";
187 nextState = state;
19 }
20 else{
218 generated = "001".repeat(nZeros - (state ? 1 : 0));
228 generated += "00111";
23 }
2415 return recursiveEncoding(code.substr(0,code.length - nZeros - 1),nextState) + generated;
25 };
26
276 this.encoded = function(){
283 return recursiveEncoding(this.number.toString(2),true).substr(2);
29 };
30
316 this.valid = function(){
322 return this.number >= 3 && this.number <= 131070;
33 };
34
35 //A help function to calculate the zeros at the end of a string (the code)
366 var zeros = function(code){
3715 var i = code.length - 1;
3815 var zeros = 0;
3915 while(code[i]=="0" || i<0){
4013 zeros++;
4113 i--;
42 }
4315 return zeros;
44 };
45
46 //http://stackoverflow.com/a/202627
476 String.prototype.repeat = function( num )
48 {
498 return new Array( num + 1 ).join( this );
50 }
51};
52
53//Required to register for both browser and nodejs
541var register = function(core){
551 core.register(pharmacode, /^pharmacode$/i, 2);
56}
572try{register(JsBarcode)} catch(e){}
582try{module.exports.register = register} catch(e){}
59