summaryrefslogtreecommitdiffstats
path: root/data/templates/js/code39.js
blob: 6028854fb31523ee95e471bcdfc85157bd9e3e62 (plain)
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
var encodings = {
	'0':'bwbWBwBwb',
	'1':'BwbWbwbwB',
	'2':'bwBWbwbwB',
	'3':'BwBWbwbwb',
	'4':'bwbWBwbwB',
	'5':'BwbWBwbwb',
	'6':'bwBWBwbwb',
	'7':'bwbWbwBwB',
	'8':'BwbWbwBwb',
	'9':'bwBWbwBwb',
	'A':'BwbwbWbwB',
	'B':'bwBwbWbwB',
	'C':'BwBwbWbwb',
	'D':'bwbwBWbwB',
	'E':'BwbwBWbwb',
	'F':'bwBwBWbwb',
	'G':'bwbwbWBwB',
	'H':'BwbwbWBwb',
	'I':'bwBwbWBwb',
	'J':'bwbwBWBwb',
	'K':'BwbwbwbWB',
	'L':'bwBwbwbWB',
	'M':'BwBwbwbWb',
	'N':'bwbwBwbWB',
	'O':'BwbwBwbWb',
	'P':'bwBwBwbWb',
	'Q':'bwbwbwBWB',
	'R':'BwbwbwBWb',
	'S':'bwBwbwBWb',
	'T':'bwbwBwBWb',
	'U':'BWbwbwbwB',
	'V':'bWBwbwbwB',
	'W':'BWBwbwbwb',
	'X':'bWbwBwbwB',
	'Y':'BWbwBwbwb',
	'Z':'bWBwBwbwb',
	'-':'bWbwbwBwB',
	'.':'BWbwbwBwb',
	' ':'bWBwbwBwb',
	'$':'bWbWbWbwb',
	'/':'bWbWbwbWb',
	'+':'bWbwbWbWb',
	'%':'bwbWbWbWb',
	'*':'bWbwBwBwb'
}

var height = 100;
var paintText = true;
var canvas;
var ctx;

var code39_init = function() {
	canvas = $('#barcode')[0];
	ctx = canvas.getContext("2d");
}

var code39_checksum = function(barcode) {
	var charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
	var subtotal = 0;
	var c;

	for (c in barcode) {
		subtotal += charset.indexOf(barcode[c]);
	}

	return charset[subtotal%43];
}

var code39_draw = function(text, add_checksum) {
	var showtext = text;

	if(add_checksum) {
		text += code39_checksum(text);
		showtext += " ";
	}

	text = "*" + text + "*";
	showtext = " " + showtext + " ";

	var txtLength = text.length;
	var totalWidth = txtLength*15 +txtLength - 1;
	cwidth = totalWidth+30;

	ctx.clearRect(0,0,canvas.width,canvas.height);

	canvas.style.height = canvas.height = height;
	canvas.style.width = canvas.width = cwidth ;
	ctx.fillStyle = "rgb(255,255,255)";
	ctx.fillRect(0,0,canvas.width,canvas.height);

	var i,j;

	/* Rounding to prevent antialising */
	var currentx = Math.round(cwidth/2-totalWidth/2), currenty = 20;

	/* wides are 3x width of narrow */
	var widewidth = 3;

	var barheight = 80;

	if(paintText) {
		barheight -= 20;
	}

	for(i=0;i<text.length;i++) {
		var code = encodings[text[i]];
		if (!code) {
			code = encodings['-'];
		}

		for(j=0;j<code.length;j++) {
			if (j%2==0) {
				/* black */
				ctx.fillStyle = "rgb(0,0,0)";
			} else {
				/* white */
				ctx.fillStyle = "rgb(255,255,255)";
			}

			if (code.charCodeAt(j)<91) {
				/* wide */
				ctx.fillRect (currentx, currenty, widewidth, barheight);
				currentx += 3;
			} else {
				/* narrow */
				ctx.fillRect (currentx, currenty, 1, barheight);
				currentx += 1;
			}

			if (paintText && (j==5) && (typeof ctx.fillText == 'function')) {
				ctx.fillStyle = "rgb(0,0,0)";
				ctx.fillText(showtext[i], currentx, 90);
			}
		}

		if (i!=text.length-1) {
			/* draw narrow white as divider */
			ctx.fillStyle = "rgb(255,255,255)";
			ctx.fillRect (currentx, currenty, 1, barheight);
			currentx += 1;
		}
	}
}

var code39_url = function() {
	return canvas.toDataURL();
}