Line | Hits | Source |
---|---|---|
1 | 1 | (function(){ |
2 | ||
3 | // Main function, calls drawCanvas(...) in the right way | |
4 | 1 | var JsBarcode = function(image, content, options, validFunction){ |
5 | // If the image is a string, query select call again | |
6 | 21 | if(typeof image === "string"){ |
7 | 1 | image = document.querySelector(image); |
8 | 0 | JsBarcode(image, content, options, validFunction); |
9 | } | |
10 | // If image, draw on canvas and set the uri as src | |
11 | 20 | else if(typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLImageElement){ |
12 | 0 | canvas = document.createElement('canvas'); |
13 | 0 | drawCanvas(canvas, content, options, validFunction); |
14 | 0 | image.setAttribute("src", canvas.toDataURL()); |
15 | } | |
16 | // If canvas, just draw | |
17 | 20 | else if(image.getContext){ |
18 | 19 | drawCanvas(image, content, options, validFunction); |
19 | } | |
20 | else{ | |
21 | 1 | 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 | |
26 | 1 | var drawCanvas = function(canvas, content, options, validFunction) { |
27 | ||
28 | // This tries to call the valid function only if it's specified. Otherwise nothing happens | |
29 | 19 | var validFunctionIfExist = function(valid){ |
30 | 18 | if(validFunction){ |
31 | 2 | validFunction(valid); |
32 | } | |
33 | }; | |
34 | ||
35 | // Merge the user options with the default | |
36 | 19 | options = merge(JsBarcode.defaults, options); |
37 | ||
38 | // Fix the margins | |
39 | 19 | options.marginTop = options.marginTop | options.margin; |
40 | 19 | options.marginBottom = options.marginBottom | options.margin; |
41 | 19 | options.marginRight = options.marginRight | options.margin; |
42 | 19 | options.marginLeft = options.marginLeft | options.margin; |
43 | ||
44 | //Abort if the browser does not support HTML5 canvas | |
45 | 19 | if (!canvas.getContext) { |
46 | 0 | throw new Error('The browser does not support canvas.'); |
47 | } | |
48 | ||
49 | // Automatically choose barcode if format set to "auto"... | |
50 | 19 | if(options.format == "auto"){ |
51 | 3 | var encoder = new (JsBarcode.autoSelectEncoder(content))(content); |
52 | } | |
53 | // ...or else, get by name | |
54 | else{ | |
55 | 16 | var encoder = new (JsBarcode.getModule(options.format))(content); |
56 | } | |
57 | ||
58 | //Abort if the barcode format does not support the content | |
59 | 18 | if(!encoder.valid()){ |
60 | 2 | validFunctionIfExist(false); |
61 | 2 | if(!validFunction){ |
62 | 1 | throw new Error('The data is not valid for the type of barcode.'); |
63 | } | |
64 | 1 | return; |
65 | } | |
66 | ||
67 | // Set the binary to a cached version if possible | |
68 | 16 | var cachedBinary = JsBarcode.getCache(options.format, content); |
69 | 16 | if(cachedBinary){ |
70 | 9 | var binary = cachedBinary; |
71 | } | |
72 | else{ | |
73 | // Encode the content | |
74 | 7 | var binary = encoder.encoded(); |
75 | // Cache the encoding if it will be used again later | |
76 | 7 | JsBarcode.cache(options.format, content, binary); |
77 | } | |
78 | ||
79 | // Get the canvas context | |
80 | 16 | var ctx = canvas.getContext("2d"); |
81 | ||
82 | // Set font | |
83 | 16 | var font = options.fontOptions + " " + options.fontSize + "px "+options.font; |
84 | 16 | ctx.font = font; |
85 | ||
86 | // Set the width and height of the barcode | |
87 | 16 | var width = binary.length*options.width; |
88 | // Replace with width of the text if it is wider then the barcode | |
89 | 16 | var textWidth = ctx.measureText(encoder.getText()).width; |
90 | 16 | if(options.displayValue && width < textWidth){ |
91 | 0 | if(options.textAlign == "center"){ |
92 | 0 | var barcodePadding = Math.floor((textWidth - width)/2); |
93 | } | |
94 | 0 | else if(options.textAlign == "left"){ |
95 | 0 | var barcodePadding = 0; |
96 | } | |
97 | 0 | else if(options.textAlign == "right"){ |
98 | 0 | var barcodePadding = Math.floor(textWidth - width); |
99 | } | |
100 | ||
101 | 0 | width = textWidth; |
102 | } | |
103 | // Make sure barcodePadding is not undefined | |
104 | 16 | var barcodePadding = barcodePadding || 0; |
105 | ||
106 | 16 | canvas.width = width + options.marginLeft + options.marginRight; |
107 | ||
108 | // Set extra height if the value is displayed under the barcode. Multiplication with 1.3 t0 ensure that some | |
109 | //characters are not cut in half | |
110 | 16 | canvas.height = options.height |
111 | + (options.displayValue ? options.fontSize : 0) | |
112 | + options.textMargin | |
113 | + options.marginTop | |
114 | + options.marginBottom; | |
115 | ||
116 | // Paint the canvas | |
117 | 16 | ctx.clearRect(0,0,canvas.width,canvas.height); |
118 | 16 | if(options.background){ |
119 | 16 | ctx.fillStyle = options.background; |
120 | 16 | ctx.fillRect(0,0,canvas.width, canvas.height); |
121 | } | |
122 | ||
123 | // Creates the barcode out of the encoded binary | |
124 | 16 | ctx.fillStyle = options.lineColor; |
125 | 16 | for(var i=0;i<binary.length;i++){ |
126 | 1538 | var x = i*options.width + options.marginLeft + barcodePadding; |
127 | 1538 | if(binary[i] == "1"){ |
128 | 730 | ctx.fillRect(x, options.marginTop, options.width, options.height); |
129 | } | |
130 | } | |
131 | ||
132 | // Draw the text if displayValue is set | |
133 | 16 | if(options.displayValue){ |
134 | 15 | var x, y; |
135 | ||
136 | 15 | y = options.height + options.textMargin + options.marginTop; |
137 | ||
138 | 15 | ctx.font = font; |
139 | 15 | ctx.textBaseline = "bottom"; |
140 | 15 | ctx.textBaseline = 'top'; |
141 | ||
142 | // Draw the text in the correct X depending on the textAlign option | |
143 | 15 | if(options.textAlign == "left" || barcodePadding > 0){ |
144 | 1 | x = options.marginLeft; |
145 | 1 | ctx.textAlign = 'left'; |
146 | } | |
147 | 14 | else if(options.textAlign == "right"){ |
148 | 1 | x = canvas.width - options.marginRight; |
149 | 1 | ctx.textAlign = 'right'; |
150 | } | |
151 | //In all other cases, center the text | |
152 | else{ | |
153 | 13 | x = canvas.width / 2; |
154 | 13 | ctx.textAlign = 'center'; |
155 | } | |
156 | ||
157 | 15 | ctx.fillText(encoder.getText(), x, y); |
158 | } | |
159 | ||
160 | // Send a confirmation that the generation was successful to the valid function if it does exist | |
161 | 16 | validFunctionIfExist(true); |
162 | }; | |
163 | ||
164 | 1 | JsBarcode._modules = []; |
165 | ||
166 | // Add a new module sorted in the array | |
167 | 1 | JsBarcode.register = function(module, regex, priority){ |
168 | 15 | var position = 0; |
169 | 15 | if(typeof priority === "undefined"){ |
170 | 4 | position = JsBarcode._modules.length - 1; |
171 | } | |
172 | else{ | |
173 | 11 | for(var i=0;i<JsBarcode._modules.length;i++){ |
174 | 32 | position = i; |
175 | 32 | if(!(priority < JsBarcode._modules[i].priority)){ |
176 | 9 | break; |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | // Add the module in position position | |
182 | 15 | JsBarcode._modules.splice(position, 0, { |
183 | "regex": regex, | |
184 | "module": module, | |
185 | "priority": priority | |
186 | }); | |
187 | }; | |
188 | ||
189 | // Get module by name | |
190 | 1 | JsBarcode.getModule = function(name){ |
191 | 31 | for(var i in JsBarcode._modules){ |
192 | 312 | if(name.search(JsBarcode._modules[i].regex) !== -1){ |
193 | 30 | return JsBarcode._modules[i].module; |
194 | } | |
195 | } | |
196 | 1 | throw new Error('Module ' + name + ' does not exist or is not loaded.'); |
197 | }; | |
198 | ||
199 | // If any format is valid with the content, return the format with highest priority | |
200 | 1 | JsBarcode.autoSelectEncoder = function(content){ |
201 | 3 | for(var i in JsBarcode._modules){ |
202 | 17 | var barcode = new (JsBarcode._modules[i].module)(content); |
203 | 17 | if(barcode.valid(content)){ |
204 | 3 | return JsBarcode._modules[i].module; |
205 | } | |
206 | } | |
207 | 0 | throw new Error("Can't automatically find a barcode format matching the string '" + content + "'"); |
208 | }; | |
209 | ||
210 | // Defining the cache dictionary | |
211 | 1 | JsBarcode._cache = {}; |
212 | ||
213 | // Cache a regerated barcode | |
214 | 1 | JsBarcode.cache = function(format, input, output){ |
215 | 7 | if(!JsBarcode._cache[format]){ |
216 | 4 | JsBarcode._cache[format] = {}; |
217 | } | |
218 | 7 | JsBarcode._cache[format][input] = output; |
219 | }; | |
220 | ||
221 | // Get a chached barcode | |
222 | 1 | JsBarcode.getCache = function(format, input){ |
223 | 16 | if(JsBarcode._cache[format]){ |
224 | 12 | if(JsBarcode._cache[format][input]){ |
225 | 9 | return JsBarcode._cache[format][input]; |
226 | } | |
227 | } | |
228 | 7 | return ""; |
229 | }; | |
230 | ||
231 | // Detect if the code is running under nodejs | |
232 | 1 | JsBarcode._isNode = false; |
233 | 1 | if (typeof module !== 'undefined' && module.exports) { |
234 | 1 | module.exports = JsBarcode; // Export to nodejs |
235 | 1 | JsBarcode._isNode = true; |
236 | ||
237 | //Register all modules in ./barcodes/ | |
238 | 1 | var path = require("path"); |
239 | 1 | var dir = path.join(__dirname, "barcodes"); |
240 | 1 | var files = require("fs").readdirSync(dir); |
241 | 1 | for(var i in files){ |
242 | 8 | var barcode = require(path.join(dir, files[i])); |
243 | 8 | barcode.register(JsBarcode); |
244 | } | |
245 | } | |
246 | ||
247 | //Regsiter JsBarcode for the browser | |
248 | 1 | if(typeof window !== 'undefined'){ |
249 | 0 | window.JsBarcode = JsBarcode; |
250 | } | |
251 | ||
252 | // Register JsBarcode as an jQuery plugin if jQuery exist | |
253 | 1 | if (typeof jQuery !== 'undefined') { |
254 | 0 | jQuery.fn.JsBarcode = function(content, options, validFunction){ |
255 | 0 | JsBarcode(this.get(0), content, options, validFunction); |
256 | 0 | return this; |
257 | }; | |
258 | } | |
259 | ||
260 | // All the default options. If one is not set. | |
261 | 1 | JsBarcode.defaults = { |
262 | width: 2, | |
263 | height: 100, | |
264 | format: "auto", | |
265 | displayValue: true, | |
266 | fontOptions: "", | |
267 | font: "monospace", | |
268 | textAlign: "center", | |
269 | textMargin: 2, | |
270 | fontSize: 14, | |
271 | background: "#fff", | |
272 | lineColor: "#000", | |
273 | margin: 10, | |
274 | marginTop: undefined, | |
275 | marginBottom: undefined, | |
276 | marginLeft: undefined, | |
277 | marginRight: undefined | |
278 | }; | |
279 | ||
280 | // Function to merge the default options with the default ones | |
281 | 1 | var merge = function(m1, m2) { |
282 | 19 | var newMerge = {}; |
283 | 19 | for (var k in m1) { |
284 | 304 | newMerge[k] = m1[k]; |
285 | } | |
286 | 19 | for (var k in m2) { |
287 | 24 | if(typeof m2[k] !== "undefined"){ |
288 | 24 | newMerge[k] = m2[k]; |
289 | } | |
290 | } | |
291 | 19 | return newMerge; |
292 | }; | |
293 | })(); | |
294 |
Line | Hits | Source |
---|---|---|
1 | 1 | function CODE128(string, code){ |
2 | 13 | code = code || "B"; |
3 | ||
4 | 13 | this.string = string+""; |
5 | ||
6 | 13 | this.valid = valid; |
7 | ||
8 | 13 | this.getText = function(){ |
9 | 19 | return this.string; |
10 | }; | |
11 | ||
12 | //The public encoding function | |
13 | 13 | this.encoded = function(){ |
14 | 3 | return calculate["code128" + code](string); |
15 | } | |
16 | ||
17 | //Data for each character, the last characters will not be encoded but are used for error correction | |
18 | 13 | var code128b = [ |
19 | [" ","11011001100",0], | |
20 | ["!","11001101100",1], | |
21 | ["\"","11001100110",2], | |
22 | ["#","10010011000",3], | |
23 | ["$","10010001100",4], | |
24 | ["%","10001001100",5], | |
25 | ["&","10011001000",6], | |
26 | ["'","10011000100",7], | |
27 | ["(","10001100100",8], | |
28 | [")","11001001000",9], | |
29 | ["*","11001000100",10], | |
30 | ["+","11000100100",11], | |
31 | [",","10110011100",12], | |
32 | ["-","10011011100",13], | |
33 | [".","10011001110",14], | |
34 | ["/","10111001100",15], | |
35 | ["0","10011101100",16], | |
36 | ["1","10011100110",17], | |
37 | ["2","11001110010",18], | |
38 | ["3","11001011100",19], | |
39 | ["4","11001001110",20], | |
40 | ["5","11011100100",21], | |
41 | ["6","11001110100",22], | |
42 | ["7","11101101110",23], | |
43 | ["8","11101001100",24], | |
44 | ["9","11100101100",25], | |
45 | [":","11100100110",26], | |
46 | [";","11101100100",27], | |
47 | ["<","11100110100",28], | |
48 | ["=","11100110010",29], | |
49 | [">","11011011000",30], | |
50 | ["?","11011000110",31], | |
51 | ["@","11000110110",32], | |
52 | ["A","10100011000",33], | |
53 | ["B","10001011000",34], | |
54 | ["C","10001000110",35], | |
55 | ["D","10110001000",36], | |
56 | ["E","10001101000",37], | |
57 | ["F","10001100010",38], | |
58 | ["G","11010001000",39], | |
59 | ["H","11000101000",40], | |
60 | ["I","11000100010",41], | |
61 | ["J","10110111000",42], | |
62 | ["K","10110001110",43], | |
63 | ["L","10001101110",44], | |
64 | ["M","10111011000",45], | |
65 | ["N","10111000110",46], | |
66 | ["O","10001110110",47], | |
67 | ["P","11101110110",48], | |
68 | ["Q","11010001110",49], | |
69 | ["R","11000101110",50], | |
70 | ["S","11011101000",51], | |
71 | ["T","11011100010",52], | |
72 | ["U","11011101110",53], | |
73 | ["V","11101011000",54], | |
74 | ["W","11101000110",55], | |
75 | ["X","11100010110",56], | |
76 | ["Y","11101101000",57], | |
77 | ["Z","11101100010",58], | |
78 | ["[","11100011010",59], | |
79 | ["\\","11101111010",60], | |
80 | ["]","11001000010",61], | |
81 | ["^","11110001010",62], | |
82 | ["_","10100110000",63], | |
83 | ["`","10100001100",64], | |
84 | ["a","10010110000",65], | |
85 | ["b","10010000110",66], | |
86 | ["c","10000101100",67], | |
87 | ["d","10000100110",68], | |
88 | ["e","10110010000",69], | |
89 | ["f","10110000100",70], | |
90 | ["g","10011010000",71], | |
91 | ["h","10011000010",72], | |
92 | ["i","10000110100",73], | |
93 | ["j","10000110010",74], | |
94 | ["k","11000010010",75], | |
95 | ["l","11001010000",76], | |
96 | ["m","11110111010",77], | |
97 | ["n","11000010100",78], | |
98 | ["o","10001111010",79], | |
99 | ["p","10100111100",80], | |
100 | ["q","10010111100",81], | |
101 | ["r","10010011110",82], | |
102 | ["s","10111100100",83], | |
103 | ["t","10011110100",84], | |
104 | ["u","10011110010",85], | |
105 | ["v","11110100100",86], | |
106 | ["w","11110010100",87], | |
107 | ["x","11110010010",88], | |
108 | ["y","11011011110",89], | |
109 | ["z","11011110110",90], | |
110 | ["{","11110110110",91], | |
111 | ["|","10101111000",92], | |
112 | ["}","10100011110",93], | |
113 | ["~","10001011110",94], | |
114 | [String.fromCharCode(127),"10111101000",95], | |
115 | [String.fromCharCode(128),"10111100010",96], | |
116 | [String.fromCharCode(129),"11110101000",97], | |
117 | [String.fromCharCode(130),"11110100010",98], | |
118 | [String.fromCharCode(131),"10111011110",99], | |
119 | [String.fromCharCode(132),"10111101110",100], | |
120 | [String.fromCharCode(133),"11101011110",101], | |
121 | [String.fromCharCode(134),"11110101110",102], | |
122 | //Start codes | |
123 | [String.fromCharCode(135),"11010000100",103], | |
124 | [String.fromCharCode(136),"11010010000",104], | |
125 | [String.fromCharCode(137),"11010011100",105]]; | |
126 | ||
127 | //The end bits | |
128 | 13 | var endBin = "1100011101011"; |
129 | ||
130 | //Use the regexp variable for validation | |
131 | 13 | function valid(){ |
132 | 11 | if(this.string.search(/^[!-~ ]+$/)==-1){ |
133 | 1 | return false; |
134 | } | |
135 | 10 | return true; |
136 | } | |
137 | ||
138 | //The encoder function that return a complete binary string. Data need to be validated before sent to this function | |
139 | //This is general calculate function, which is called by code specific calculate functions | |
140 | 13 | function calculateCode128(string, encodeFn, startCode, checksumFn){ |
141 | 3 | var result = ""; |
142 | ||
143 | //Add the start bits | |
144 | 3 | result += encodingById(startCode); |
145 | ||
146 | //Add the encoded bits | |
147 | 3 | result += encodeFn(string); |
148 | ||
149 | //Add the checksum | |
150 | 3 | result += encodingById(checksumFn(string, startCode)); |
151 | ||
152 | //Add the end bits | |
153 | 3 | result += endBin; |
154 | ||
155 | 3 | return result; |
156 | } | |
157 | ||
158 | //Code specific calculate functions | |
159 | 13 | var calculate = { |
160 | code128B: function(string){ | |
161 | 2 | return calculateCode128(string, encodeB, 104, checksumB); |
162 | }, | |
163 | code128C: function(string){ | |
164 | 1 | string = string.replace(/ /g, ""); |
165 | 1 | return calculateCode128(string, encodeC, 105, checksumC); |
166 | } | |
167 | } | |
168 | ||
169 | //Encode the characters (128 B) | |
170 | 13 | function encodeB(string){ |
171 | 2 | var result = ""; |
172 | 2 | for(var i=0;i<string.length;i++){ |
173 | 10 | result+=encodingByChar(string[i]); |
174 | } | |
175 | 2 | return result; |
176 | } | |
177 | ||
178 | //Encode the characters (128 C) | |
179 | 13 | function encodeC(string){ |
180 | 1 | var result = ""; |
181 | 1 | for(var i=0;i<string.length;i+=2){ |
182 | 3 | result+=encodingById(parseInt(string.substr(i, 2))); |
183 | } | |
184 | 1 | return result; |
185 | } | |
186 | ||
187 | //Calculate the checksum (128 B) | |
188 | 13 | function checksumB(string, startCode){ |
189 | 2 | var sum = 0; |
190 | 2 | for(var i=0;i<string.length;i++){ |
191 | 10 | sum += weightByCharacter(string[i])*(i+1); |
192 | } | |
193 | 2 | return (sum+startCode) % 103; |
194 | } | |
195 | ||
196 | //Calculate the checksum (128 C) | |
197 | 13 | function checksumC(string, startCode){ |
198 | 1 | var sum = 0; |
199 | 1 | var w = 1; |
200 | 1 | for(var i=0;i<string.length;i+=2){ |
201 | 3 | sum += parseInt(string.substr(i, 2))*(w); |
202 | 3 | w++; |
203 | } | |
204 | 1 | return (sum+startCode) % 103; |
205 | } | |
206 | ||
207 | //Get the encoded data by the id of the character | |
208 | 13 | function encodingById(id){ |
209 | 9 | for(var i=0;i<code128b.length;i++){ |
210 | 565 | if(code128b[i][2]==id){ |
211 | 9 | return code128b[i][1]; |
212 | } | |
213 | } | |
214 | } | |
215 | ||
216 | //Get the id (weight) of a character | |
217 | 13 | function weightByCharacter(character){ |
218 | 10 | for(var i=0;i<code128b.length;i++){ |
219 | 527 | if(code128b[i][0]==character){ |
220 | 10 | return code128b[i][2]; |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | //Get the encoded data of a character | |
226 | 13 | function encodingByChar(character){ |
227 | 10 | for(var i=0;i<code128b.length;i++){ |
228 | 527 | if(code128b[i][0]==character){ |
229 | 10 | return code128b[i][1]; |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | 1 | function CODE128B(string) { |
236 | 12 | return new CODE128(string, "B"); |
237 | } | |
238 | 1 | function CODE128C(string) { |
239 | 1 | return new CODE128(string, "C"); |
240 | }; | |
241 | ||
242 | //Required to register for both browser and nodejs | |
243 | 1 | var register = function(core){ |
244 | 1 | core.register(CODE128B, /^CODE128(.?B)?$/i, 2); |
245 | 1 | core.register(CODE128C, /^CODE128.?C$/i, 2); |
246 | } | |
247 | 2 | try{register(JsBarcode)} catch(e){} |
248 | 2 | try{module.exports.register = register} catch(e){} |
249 |
Line | Hits | Source |
---|---|---|
1 | 1 | function CODE39(string){ |
2 | 9 | this.string = string.toUpperCase(); |
3 | ||
4 | 9 | var code39 = [ |
5 | [0,"0","101000111011101"] | |
6 | ,[1,"1","111010001010111"] | |
7 | ,[2,"2","101110001010111"] | |
8 | ,[3,"3","111011100010101"] | |
9 | ,[4,"4","101000111010111"] | |
10 | ,[5,"5","111010001110101"] | |
11 | ,[6,"6","101110001110101"] | |
12 | ,[7,"7","101000101110111"] | |
13 | ,[8,"8","111010001011101"] | |
14 | ,[9,"9","101110001011101"] | |
15 | ,[10,"A","111010100010111"] | |
16 | ,[11,"B","101110100010111"] | |
17 | ,[12,"C","111011101000101"] | |
18 | ,[13,"D","101011100010111"] | |
19 | ,[14,"E","111010111000101"] | |
20 | ,[15,"F","101110111000101"] | |
21 | ,[16,"G","101010001110111"] | |
22 | ,[17,"H","111010100011101"] | |
23 | ,[18,"I","101110100011101"] | |
24 | ,[19,"J","101011100011101"] | |
25 | ,[20,"K","111010101000111"] | |
26 | ,[21,"L","101110101000111"] | |
27 | ,[22,"M","111011101010001"] | |
28 | ,[23,"N","101011101000111"] | |
29 | ,[24,"O","111010111010001"] | |
30 | ,[25,"P","101110111010001"] | |
31 | ,[26,"Q","101010111000111"] | |
32 | ,[27,"R","111010101110001"] | |
33 | ,[28,"S","101110101110001"] | |
34 | ,[29,"T","101011101110001"] | |
35 | ,[30,"U","111000101010111"] | |
36 | ,[31,"V","100011101010111"] | |
37 | ,[32,"W","111000111010101"] | |
38 | ,[33,"X","100010111010111"] | |
39 | ,[34,"Y","111000101110101"] | |
40 | ,[35,"Z","100011101110101"] | |
41 | ,[36,"-","100010101110111"] | |
42 | ,[37,".","111000101011101"] | |
43 | ,[38," ","100011101011101"] | |
44 | ,[39,"$","100010001000101"] | |
45 | ,[40,"/","100010001010001"] | |
46 | ,[41,"+","100010100010001"] | |
47 | ,[42,"%","101000100010001"]]; | |
48 | ||
49 | 9 | this.getText = function(){ |
50 | 9 | return this.string; |
51 | }; | |
52 | ||
53 | 9 | this.encoded = function(){ |
54 | 5 | var result = ""; |
55 | 5 | result += "1000101110111010"; |
56 | 5 | for(var i=0;i<this.string.length;i++){ |
57 | 25 | result+=encodingByChar(this.string[i])+"0"; |
58 | } | |
59 | 5 | result += "1000101110111010"; |
60 | 5 | return result; |
61 | }; | |
62 | ||
63 | //Use the regexp variable for validation | |
64 | 9 | this.valid = function(){ |
65 | 7 | if(this.string.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/)==-1){ |
66 | 1 | return false; |
67 | } | |
68 | 6 | return true; |
69 | } | |
70 | ||
71 | //Get the encoded data of a character | |
72 | 9 | function encodingByChar(character){ |
73 | 25 | for(var i=0;i<code39.length;i++){ |
74 | 431 | if(code39[i][1]==character){ |
75 | 24 | return code39[i][2]; |
76 | } | |
77 | } | |
78 | 1 | return ""; |
79 | } | |
80 | } | |
81 | ||
82 | ||
83 | //Required to register for both browser and nodejs | |
84 | 1 | var register = function(core){ |
85 | 1 | core.register(CODE39, /^CODE.?39$/i, 3); |
86 | } | |
87 | 2 | try{register(JsBarcode)} catch(e){} |
88 | 2 | try{module.exports.register = register} catch(e){} |
89 |
Line | Hits | Source |
---|---|---|
1 | 1 | function EAN(EANnumber){ |
2 | 15 | this.EANnumber = EANnumber+""; |
3 | ||
4 | //Regexp to test if the EAN code is correct formated | |
5 | 15 | var fullEanRegexp = /^[0-9]{13}$/; |
6 | 15 | var needLastDigitRegexp = /^[0-9]{12}$/; |
7 | ||
8 | //Add checksum if it does not exist | |
9 | 15 | if(this.EANnumber.search(needLastDigitRegexp)!=-1){ |
10 | 2 | this.EANnumber += checksum(this.EANnumber); |
11 | } | |
12 | ||
13 | 15 | this.getText = function(){ |
14 | 6 | return this.EANnumber; |
15 | }; | |
16 | ||
17 | 15 | this.valid = function(){ |
18 | 12 | return valid(this.EANnumber); |
19 | }; | |
20 | ||
21 | 15 | this.encoded = function (){ |
22 | 4 | return createEAN13(this.EANnumber); |
23 | } | |
24 | ||
25 | //Create the binary representation of the EAN code | |
26 | //number needs to be a string | |
27 | 15 | function createEAN13(number){ |
28 | 4 | var encoder = new EANencoder(); |
29 | ||
30 | //Create the return variable | |
31 | 4 | var result = ""; |
32 | ||
33 | 4 | var structure = encoder.getEANstructure(number); |
34 | ||
35 | //Get the number to be encoded on the left side of the EAN code | |
36 | 4 | var leftSide = number.substr(1,7); |
37 | ||
38 | //Get the number to be encoded on the right side of the EAN code | |
39 | 4 | var rightSide = number.substr(7,6); |
40 | ||
41 | //Add the start bits | |
42 | 4 | result += encoder.startBin; |
43 | ||
44 | //Add the left side | |
45 | 4 | result += encoder.encode(leftSide, structure); |
46 | ||
47 | //Add the middle bits | |
48 | 4 | result += encoder.middleBin; |
49 | ||
50 | //Add the right side | |
51 | 4 | result += encoder.encode(rightSide,"RRRRRR"); |
52 | ||
53 | //Add the end bits | |
54 | 4 | result += encoder.endBin; |
55 | ||
56 | 4 | return result; |
57 | } | |
58 | ||
59 | //Calulate the checksum digit | |
60 | 15 | function checksum(number){ |
61 | 6 | var result = 0; |
62 | ||
63 | 42 | for(var i=0;i<12;i+=2){result+=parseInt(number[i])} |
64 | 42 | for(var i=1;i<12;i+=2){result+=parseInt(number[i])*3} |
65 | ||
66 | 6 | return (10 - (result % 10)) % 10; |
67 | } | |
68 | ||
69 | 15 | function valid(number){ |
70 | 12 | if(number.search(fullEanRegexp)!=-1){ |
71 | 4 | return number[12] == checksum(number); |
72 | } | |
73 | else{ | |
74 | 8 | return false; |
75 | } | |
76 | } | |
77 | } | |
78 | ||
79 | 1 | function EAN8(EAN8number){ |
80 | 8 | this.EAN8number = EAN8number+""; |
81 | ||
82 | //Regexp to test if the EAN code is correct formated | |
83 | 8 | var fullEanRegexp = /^[0-9]{8}$/; |
84 | 8 | var needLastDigitRegexp = /^[0-9]{7}$/; |
85 | ||
86 | //Add checksum if it does not exist | |
87 | 8 | if(this.EAN8number.search(needLastDigitRegexp)!=-1){ |
88 | 1 | this.EAN8number += checksum(this.EAN8number); |
89 | } | |
90 | ||
91 | 8 | this.getText = function(){ |
92 | 1 | return this.EAN8number; |
93 | } | |
94 | ||
95 | 8 | this.valid = function(){ |
96 | 8 | return valid(this.EAN8number); |
97 | }; | |
98 | ||
99 | 8 | this.encoded = function (){ |
100 | 2 | return createEAN8(this.EAN8number); |
101 | } | |
102 | ||
103 | 8 | function valid(number){ |
104 | 8 | if(number.search(fullEanRegexp)!=-1){ |
105 | 3 | return number[7] == checksum(number); |
106 | } | |
107 | else{ | |
108 | 5 | return false; |
109 | } | |
110 | } | |
111 | ||
112 | //Calulate the checksum digit | |
113 | 8 | function checksum(number){ |
114 | 4 | var result = 0; |
115 | ||
116 | 20 | for(var i=0;i<7;i+=2){result+=parseInt(number[i])*3} |
117 | 16 | for(var i=1;i<7;i+=2){result+=parseInt(number[i])} |
118 | ||
119 | 4 | return (10 - (result % 10)) % 10; |
120 | } | |
121 | ||
122 | 8 | function createEAN8(number){ |
123 | 2 | var encoder = new EANencoder(); |
124 | ||
125 | //Create the return variable | |
126 | 2 | var result = ""; |
127 | ||
128 | //Get the number to be encoded on the left side of the EAN code | |
129 | 2 | var leftSide = number.substr(0,4); |
130 | ||
131 | //Get the number to be encoded on the right side of the EAN code | |
132 | 2 | var rightSide = number.substr(4,4); |
133 | ||
134 | //Add the start bits | |
135 | 2 | result += encoder.startBin; |
136 | ||
137 | //Add the left side | |
138 | 2 | result += encoder.encode(leftSide, "LLLL"); |
139 | ||
140 | //Add the middle bits | |
141 | 2 | result += encoder.middleBin; |
142 | ||
143 | //Add the right side | |
144 | 2 | result += encoder.encode(rightSide,"RRRR"); |
145 | ||
146 | //Add the end bits | |
147 | 2 | result += encoder.endBin; |
148 | ||
149 | 2 | return result; |
150 | } | |
151 | } | |
152 | ||
153 | ||
154 | 1 | function UPC(UPCnumber){ |
155 | 6 | this.ean = new EAN("0"+UPCnumber); |
156 | ||
157 | 6 | this.getText = function(){ |
158 | 1 | return this.ean.getText().substring(1); |
159 | } | |
160 | ||
161 | 6 | this.valid = function(){ |
162 | 4 | return this.ean.valid(); |
163 | } | |
164 | ||
165 | 6 | this.encoded = function(){ |
166 | 1 | return this.ean.encoded(); |
167 | } | |
168 | ||
169 | } | |
170 | ||
171 | // | |
172 | // Help class that does all the encoding | |
173 | // | |
174 | 1 | function EANencoder(){ |
175 | //The start bits | |
176 | 6 | this.startBin = "101"; |
177 | //The end bits | |
178 | 6 | this.endBin = "101"; |
179 | //The middle bits | |
180 | 6 | this.middleBin = "01010"; |
181 | ||
182 | //The L (left) type of encoding | |
183 | 6 | 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 | |
196 | 6 | 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 | |
209 | 6 | 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 | |
222 | 6 | 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 | ||
234 | 6 | this.getEANstructure = function(number){ |
235 | 4 | return EANstructure[number[0]]; |
236 | }; | |
237 | ||
238 | //Convert a numberarray to the representing | |
239 | 6 | this.encode = function(number,structure){ |
240 | //Create the variable that should be returned at the end of the function | |
241 | 12 | var result = ""; |
242 | ||
243 | //Loop all the numbers | |
244 | 12 | for(var i = 0;i<number.length;i++){ |
245 | //Using the L, G or R encoding and add it to the returning variable | |
246 | 68 | if(structure[i]=="L"){ |
247 | 23 | result += Lbinary[number[i]]; |
248 | } | |
249 | 45 | else if(structure[i]=="G"){ |
250 | 9 | result += Gbinary[number[i]]; |
251 | } | |
252 | 36 | else if(structure[i]=="R"){ |
253 | 32 | result += Rbinary[number[i]]; |
254 | } | |
255 | } | |
256 | 12 | return result; |
257 | }; | |
258 | } | |
259 | ||
260 | ||
261 | //Required to register for both browser and nodejs | |
262 | 1 | var register = function(core){ |
263 | 1 | core.register(EAN, /^EAN(.?13)?$/i, 8); |
264 | 1 | core.register(EAN8, /^EAN.?8$/i, 8); |
265 | 1 | core.register(UPC, /^UPC(.?A)?$/i, 8); |
266 | } | |
267 | 2 | try{register(JsBarcode)} catch(e){} |
268 | 2 | try{module.exports.register = register} catch(e){} |
269 |
Line | Hits | Source |
---|---|---|
1 | 1 | function ITF(ITFNumber){ |
2 | ||
3 | 6 | this.ITFNumber = ITFNumber+""; |
4 | ||
5 | 6 | this.getText = function(){ |
6 | 1 | return this.ITFNumber; |
7 | }; | |
8 | ||
9 | 6 | this.valid = function(){ |
10 | 4 | return valid(this.ITFNumber); |
11 | }; | |
12 | ||
13 | 6 | this.encoded = function(){ |
14 | //Create the variable that should be returned at the end of the function | |
15 | 1 | var result = ""; |
16 | ||
17 | //Always add the same start bits | |
18 | 1 | result += startBin; |
19 | ||
20 | //Calculate all the digit pairs | |
21 | 1 | for(var i=0;i<this.ITFNumber.length;i+=2){ |
22 | 3 | result += calculatePair(this.ITFNumber.substr(i,2)); |
23 | } | |
24 | ||
25 | //Always add the same end bits | |
26 | 1 | result += endBin; |
27 | ||
28 | 1 | return result; |
29 | } | |
30 | ||
31 | //The structure for the all digits, 1 is wide and 0 is narrow | |
32 | 6 | 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 | |
45 | 6 | var startBin = "1010"; |
46 | //The end bits | |
47 | 6 | var endBin = "11101"; |
48 | ||
49 | //Regexp for a valid Inter25 code | |
50 | 6 | var regexp = /^([0-9][0-9])+$/; |
51 | ||
52 | //Calculate the data of a number pair | |
53 | 6 | function calculatePair(twoNumbers){ |
54 | 3 | var result = ""; |
55 | ||
56 | 3 | var number1Struct = digitStructure[twoNumbers[0]]; |
57 | 3 | var number2Struct = digitStructure[twoNumbers[1]]; |
58 | ||
59 | //Take every second bit and add to the result | |
60 | 3 | for(var i=0;i<5;i++){ |
61 | 15 | result += (number1Struct[i]=="1") ? "111" : "1"; |
62 | 15 | result += (number2Struct[i]=="1") ? "000" : "0"; |
63 | } | |
64 | 3 | return result; |
65 | } | |
66 | ||
67 | 6 | function valid(number){ |
68 | 4 | return number.search(regexp)!==-1; |
69 | } | |
70 | } | |
71 | ||
72 | //Required to register for both browser and nodejs | |
73 | 1 | var register = function(core){ |
74 | 1 | core.register(ITF, /^ITF$/i, 4); |
75 | }; | |
76 | 2 | try{register(JsBarcode)} catch(e){} |
77 | 2 | try{module.exports.register = register} catch(e){} |
78 |
Line | Hits | Source |
---|---|---|
1 | 1 | function ITF14(string){ |
2 | 9 | this.string = string+""; |
3 | } | |
4 | ||
5 | 1 | ITF14.prototype.getText = function(){ |
6 | 1 | return this.string; |
7 | }; | |
8 | ||
9 | 1 | ITF14.prototype.valid = function(){ |
10 | 6 | return valid(this.string); |
11 | }; | |
12 | ||
13 | 1 | ITF14.prototype.encoded = function(){ |
14 | //Create the variable that should be returned at the end of the function | |
15 | 2 | var result = ""; |
16 | ||
17 | //If checksum is not already calculated, do it | |
18 | 2 | if(this.string.length == 13){ |
19 | 1 | this.string += checksum(this.string); |
20 | } | |
21 | ||
22 | //Always add the same start bits | |
23 | 2 | result += startBin; |
24 | ||
25 | //Calculate all the digit pairs | |
26 | 2 | for(var i=0;i<14;i+=2){ |
27 | 14 | result += calculatePair(this.string.substr(i,2)); |
28 | } | |
29 | ||
30 | //Always add the same end bits | |
31 | 2 | result += endBin; |
32 | ||
33 | 2 | return result; |
34 | }; | |
35 | ||
36 | //The structure for the all digits, 1 is wide and 0 is narrow | |
37 | 1 | var 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 | |
50 | 1 | var startBin = "1010"; |
51 | //The end bits | |
52 | 1 | var endBin = "11101"; |
53 | ||
54 | //Regexp for a valid ITF14 code | |
55 | 1 | var regexp = /^[0-9]{13,14}$/; |
56 | ||
57 | //Calculate the data of a number pair | |
58 | 1 | function calculatePair(twoNumbers){ |
59 | 14 | var result = ""; |
60 | ||
61 | 14 | var number1Struct = digitStructure[twoNumbers[0]]; |
62 | 14 | var number2Struct = digitStructure[twoNumbers[1]]; |
63 | ||
64 | //Take every second bit and add to the result | |
65 | 14 | for(var i=0;i<5;i++){ |
66 | 70 | result += (number1Struct[i]=="1") ? "111" : "1"; |
67 | 70 | result += (number2Struct[i]=="1") ? "000" : "0"; |
68 | } | |
69 | 14 | return result; |
70 | } | |
71 | ||
72 | //Calulate the checksum digit | |
73 | 1 | function checksum(numberString){ |
74 | 3 | var result = 0; |
75 | ||
76 | 42 | for(var i=0;i<13;i++){result+=parseInt(numberString[i])*(3-(i%2)*2)} |
77 | ||
78 | 3 | return 10 - (result % 10); |
79 | } | |
80 | ||
81 | 1 | function valid(number){ |
82 | 6 | if(number.search(regexp)==-1){ |
83 | 3 | return false; |
84 | } | |
85 | //Check checksum if it is already calculated | |
86 | 3 | else if(number.length==14){ |
87 | 2 | return number[13] == checksum(number); |
88 | } | |
89 | 1 | return true; |
90 | } | |
91 | ||
92 | //Required to register for both browser and nodejs | |
93 | 1 | var register = function(core){ |
94 | 1 | core.register(ITF14, /^ITF.?14$/i, 5); |
95 | } | |
96 | 2 | try{register(JsBarcode)} catch(e){} |
97 | 2 | try{module.exports.register = register} catch(e){} |
98 |
Line | Hits | Source |
---|---|---|
1 | 1 | var prototype = {}; |
2 | ||
3 | 1 | prototype.getText = function(){ |
4 | 11 | return this.string; |
5 | }; | |
6 | ||
7 | 1 | prototype.encoded = function(){ |
8 | 2 | var ret = "110"; |
9 | ||
10 | 2 | for(var i=0;i<this.string.length;i++){ |
11 | 16 | var digit = parseInt(this.string[i]); |
12 | 16 | var bin = digit.toString(2); |
13 | 16 | bin = addZeroes(bin, 4-bin.length); |
14 | 16 | for(var b=0;b<bin.length;b++){ |
15 | 64 | ret += bin[b]==0 ? "100" : "110"; |
16 | } | |
17 | } | |
18 | ||
19 | 2 | ret += "1001"; |
20 | 2 | return ret; |
21 | }; | |
22 | ||
23 | 1 | prototype.valid = function(){ |
24 | 7 | return this.string.search(/^[0-9]+$/) != -1; |
25 | }; | |
26 | ||
27 | 1 | function MSI(string){ |
28 | 5 | this.string = ""+string; |
29 | } | |
30 | ||
31 | 1 | MSI.prototype = Object.create(prototype); |
32 | ||
33 | 1 | function MSI10(string){ |
34 | 3 | this.string = ""+string; |
35 | 3 | this.string += mod10(this.string); |
36 | } | |
37 | 1 | MSI10.prototype = Object.create(prototype); |
38 | ||
39 | 1 | function MSI11(string){ |
40 | 4 | this.string = ""+string; |
41 | 4 | this.string += mod11(this.string); |
42 | } | |
43 | 1 | MSI11.prototype = Object.create(prototype); |
44 | ||
45 | 1 | function MSI1010(string){ |
46 | 2 | this.string = ""+string; |
47 | 2 | this.string += mod10(this.string); |
48 | 2 | this.string += mod10(this.string); |
49 | } | |
50 | 1 | MSI1010.prototype = Object.create(prototype); |
51 | ||
52 | 1 | function MSI1110(string){ |
53 | 2 | this.string = ""+string; |
54 | 2 | this.string += mod11(this.string); |
55 | 2 | this.string += mod10(this.string); |
56 | } | |
57 | 1 | MSI1110.prototype = Object.create(prototype); |
58 | ||
59 | 1 | function mod10(number){ |
60 | 9 | var sum = 0; |
61 | 9 | for(var i=0;i<number.length;i++){ |
62 | 54 | var n = parseInt(number[i]); |
63 | 54 | if((i + number.length) % 2 == 0){ |
64 | 24 | sum += n; |
65 | } | |
66 | else{ | |
67 | 30 | sum += (n*2)%10 + Math.floor((n*2)/10) |
68 | } | |
69 | } | |
70 | 9 | return (10-(sum%10))%10; |
71 | } | |
72 | ||
73 | 1 | function mod11(number){ |
74 | 6 | var sum = 0; |
75 | 6 | var weights = [2,3,4,5,6,7]; |
76 | 6 | for(var i=0;i<number.length;i++){ |
77 | 46 | var n = parseInt(number[number.length-1-i]); |
78 | 46 | sum += weights[i % weights.length] * n; |
79 | } | |
80 | 6 | return (11-(sum%11))%11; |
81 | } | |
82 | ||
83 | 1 | function addZeroes(number, n){ |
84 | 16 | for(var i=0;i<n;i++){ |
85 | 24 | number = "0"+number; |
86 | } | |
87 | 16 | return number; |
88 | } | |
89 | ||
90 | //Required to register for both browser and nodejs | |
91 | 1 | var register = function(core){ |
92 | 1 | core.register(MSI, /^MSI$/i, 4); |
93 | 1 | core.register(MSI10, /^MSI.?10$/i); |
94 | 1 | core.register(MSI11, /^MSI.?11$/i); |
95 | 1 | core.register(MSI1010, /^MSI.?1010$/i); |
96 | 1 | core.register(MSI1110, /^MSI.?1110$/i); |
97 | } | |
98 | 2 | try{register(JsBarcode)} catch(e){} |
99 | 2 | try{module.exports.register = register} catch(e){} |
100 |
Line | Hits | Source |
---|---|---|
1 | 1 | function pharmacode(number){ |
2 | //Ensure that the input is inturpreted as a number | |
3 | 4 | this.number = parseInt(number); |
4 | ||
5 | 4 | this.getText = function(){ |
6 | 1 | return this.number + ""; |
7 | }; | |
8 | ||
9 | 4 | function recursiveEncoding(code,state){ |
10 | //End condition | |
11 | 7 | if(code.length == 0) return ""; |
12 | ||
13 | 5 | var generated; |
14 | 5 | var nextState = false; |
15 | 5 | var nZeros = zeros(code); |
16 | 5 | if(nZeros == 0){ |
17 | 1 | generated = state ? "001" : "00111"; |
18 | 1 | nextState = state; |
19 | } | |
20 | else{ | |
21 | 4 | generated = "001".repeat(nZeros - (state ? 1 : 0)); |
22 | 4 | generated += "00111"; |
23 | } | |
24 | 5 | return recursiveEncoding(code.substr(0,code.length - nZeros - 1),nextState) + generated; |
25 | }; | |
26 | ||
27 | 4 | this.encoded = function(){ |
28 | 1 | return recursiveEncoding(this.number.toString(2),true).substr(2); |
29 | }; | |
30 | ||
31 | 4 | this.valid = function(){ |
32 | 2 | 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) | |
36 | 4 | var zeros = function(code){ |
37 | 5 | var i = code.length - 1; |
38 | 5 | var zeros = 0; |
39 | 5 | while(code[i]=="0" || i<0){ |
40 | 6 | zeros++; |
41 | 6 | i--; |
42 | } | |
43 | 5 | return zeros; |
44 | }; | |
45 | ||
46 | //http://stackoverflow.com/a/202627 | |
47 | 4 | String.prototype.repeat = function( num ) |
48 | { | |
49 | 4 | return new Array( num + 1 ).join( this ); |
50 | } | |
51 | }; | |
52 | ||
53 | //Required to register for both browser and nodejs | |
54 | 1 | var register = function(core){ |
55 | 1 | core.register(pharmacode, /^pharmacode$/i, 2); |
56 | } | |
57 | 2 | try{register(JsBarcode)} catch(e){} |
58 | 2 | try{module.exports.register = register} catch(e){} |
59 |