forked from lindell/JsBarcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsBarcode.js
More file actions
executable file
·157 lines (127 loc) · 4.2 KB
/
JsBarcode.js
File metadata and controls
executable file
·157 lines (127 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(function($){
JsBarcode = function(image, content, options, validFunction) {
//Check if the image parameter should be
if(typeof image === "string"){
image = document.querySelector(image);
}
var merge = function(m1, m2) {
var newMerge = {};
for (var k in m1) {
newMerge[k] = m1[k];
}
for (var k in m2) {
newMerge[k] = m2[k];
}
return newMerge;
};
//This tries to call the valid function only if it's specified. Otherwise nothing happens
var validFunctionIfExist = function(valid){
if(validFunction){
validFunction(valid);
}
};
//Merge the user options with the default
options = merge(JsBarcode.defaults, options);
//Create the canvas where the barcode will be drawn on
// Check if the given image is already a canvas
var canvas = image;
// check if it is a jQuery object
if ($ && canvas instanceof $) {
// get the DOM element of the object
canvas = image.get(0);
}
// check if DOM element is a canvas, otherwise it will be probably an image so create a canvas
if (!(canvas instanceof HTMLCanvasElement)) {
canvas = document.createElement('canvas');
}
//Abort if the browser does not support HTML5canvas
if (!canvas.getContext) {
return image;
}
var encoder = new window[options.format](content);
//Abort if the barcode format does not support the content
if(!encoder.valid()){
validFunctionIfExist(false);
throw new Error('The data is not valid for the type of barcode.');
}
//Encode the content
var binary = encoder.encoded();
var _drawBarcodeText = function (text) {
var x, y;
y = options.height;
ctx.font = options.fontOptions + " " + options.fontSize + "px "+options.font;
ctx.textBaseline = "bottom";
ctx.textBaseline = 'top';
if(options.textAlign == "left"){
x = options.quite;
ctx.textAlign = 'left';
}
else if(options.textAlign == "right"){
x = canvas.width - options.quite;
ctx.textAlign = 'right';
}
else{ //All other center
x = canvas.width / 2;
ctx.textAlign = 'center';
}
ctx.fillText(text, x, y);
}
//Get the canvas context
var ctx = canvas.getContext("2d");
//Set the width and height of the barcode
canvas.width = binary.length*options.width+2*options.quite;
//Set extra height if the value is displayed under the barcode. Multiplication with 1.3 t0 ensure that some
//characters are not cut in half
canvas.height = options.height + (options.displayValue ? options.fontSize * 1.3 : 0);
//Paint the canvas
ctx.clearRect(0,0,canvas.width,canvas.height);
if(options.backgroundColor){
ctx.fillStyle = options.backgroundColor;
ctx.fillRect(0,0,canvas.width,canvas.height);
}
//Creates the barcode out of the encoded binary
ctx.fillStyle = options.lineColor;
for(var i=0;i<binary.length;i++){
var x = i*options.width+options.quite;
if(binary[i] == "1"){
ctx.fillRect(x,0,options.width,options.height);
}
}
if(options.displayValue){
_drawBarcodeText(encoder.getText());
}
//Grab the dataUri from the canvas
uri = canvas.toDataURL('image/png');
// check if given image is a jQuery object
if ($ && image instanceof $) {
// check if DOM element of jQuery selection is not a canvas, so assume that it is an image
if (!(image.get(0) instanceof HTMLCanvasElement)) {
//Put the data uri into the image
image.attr("src", uri);
}
} else if (!(image instanceof HTMLCanvasElement)) {
// There is no jQuery object so just check if the given image was a canvas, if not set the source attr
image.setAttribute("src", uri);
}
validFunctionIfExist(true);
};
JsBarcode.defaults = {
width: 2,
height: 100,
quite: 10,
format: "CODE128",
displayValue: false,
fontOptions: "",
font:"monospace",
textAlign:"center",
fontSize: 12,
backgroundColor:"",
lineColor:"#000"
};
if ($) {
$.fn.JsBarcode = function(content, options,validFunction){
JsBarcode(this, content, options,validFunction);
return this;
};
}
})(typeof jQuery != 'undefined' ? jQuery : null);