forked from lindell/JsBarcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CODE128.js
212 lines (192 loc) · 5.03 KB
/
CODE128.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function CODE128(string){
this.string128 = string;
this.valid = valid;
//The public encoding function
this.encoded = function(){
if(valid(string)){
return calculateCode128(string);
}
else{
return "";
}
}
//Data for each character, the last characters will not be encoded but are used for error correction
var code128b = [
[" ","11011001100",0],
["!","11001101100",1],
["\"","11001100110",2],
["#","10010011000",3],
["$","10010001100",4],
["%","10001001100",5],
["&","10011001000",6],
["'","10011000100",7],
["(","10001100100",8],
[")","11001001000",9],
["*","11001000100",10],
["+","11000100100",11],
[",","10110011100",12],
["-","10011011100",13],
[".","10011001110",14],
["/","10111001100",15],
["0","10011101100",16],
["1","10011100110",17],
["2","11001110010",18],
["3","11001011100",19],
["4","11001001110",20],
["5","11011100100",21],
["6","11001110100",22],
["7","11101101110",23],
["8","11101001100",24],
["9","11100101100",25],
[":","11100100110",26],
[";","11101100100",27],
["<","11100110100",28],
["=","11100110010",29],
[">","11011011000",30],
["?","11011000110",31],
["@","11000110110",32],
["A","10100011000",33],
["B","10001011000",34],
["C","10001000110",35],
["D","10110001000",36],
["E","10001101000",37],
["F","10001100010",38],
["G","11010001000",39],
["H","11000101000",40],
["I","11000100010",41],
["J","10110111000",42],
["K","10110001110",43],
["L","10001101110",44],
["M","10111011000",45],
["N","10111000110",46],
["O","10001110110",47],
["P","11101110110",48],
["Q","11010001110",49],
["R","11000101110",50],
["S","11011101000",51],
["T","11011100010",52],
["U","11011101110",53],
["V","11101011000",54],
["W","11101000110",55],
["X","11100010110",56],
["Y","11101101000",57],
["Z","11101100010",58],
["[","11100011010",59],
["\\","11101111010",60],
["]","11001000010",61],
["^","11110001010",62],
["_","10100110000",63],
["`","10100001100",64],
["a","10010110000",65],
["b","10010000110",66],
["c","10000101100",67],
["d","10000100110",68],
["e","10110010000",69],
["f","10110000100",70],
["g","10011010000",71],
["h","10011000010",72],
["i","10000110100",73],
["j","10000110010",74],
["k","11000010010",75],
["l","11001010000",76],
["m","11110111010",77],
["n","11000010100",78],
["o","10001111010",79],
["p","10100111100",80],
["q","10010111100",81],
["r","10010011110",82],
["s","10111100100",83],
["t","10011110100",84],
["u","10011110010",85],
["v","11110100100",86],
["w","11110010100",87],
["x","11110010010",88],
["y","11011011110",89],
["z","11011110110",90],
["{","11110110110",91],
["|","10101111000",92],
["}","10100011110",93],
["~","10001011110",94],
[String.fromCharCode(127),"10111101000",95],
[String.fromCharCode(128),"10111100010",96],
[String.fromCharCode(129),"11110101000",97],
[String.fromCharCode(130),"11110100010",98],
[String.fromCharCode(131),"10111011110",99],
[String.fromCharCode(132),"10111101110",100],
[String.fromCharCode(133),"11101011110",101],
[String.fromCharCode(134),"11110101110",102]
//The start bits should not be used
/*,
[String.fromCharCode(135),"11010000100",103],
[String.fromCharCode(136),"11010010000",104],
[String.fromCharCode(137),"11010011100",105]*/];
//The start bits
var startBin = "11010010000";
//The end bits
var endBin = "1100011101011";
//This regexp is used for validation
var regexp = /^[!-~ ]+$/;
//Use the regexp variable for validation
function valid(){
if(string.search(regexp)==-1){
return false;
}
return true;
}
//The encoder function that return a complete binary string. Data need to be validated before sent to this function
function calculateCode128(string){
var result = "";
//Add the start bits
result += startBin;
//Add the encoded bits
result += encode(string);
//Add the checksum
result += encodingById(checksum(string));
//Add the end bits
result += endBin;
return result;
}
//Encode the characters
function encode(string){
var result = "";
for(var i=0;i<string.length;i++){
result+=encodingByChar(string[i]);
}
return result;
}
//Calculate the checksum
function checksum(string){
var sum = 0;
for(var i=0;i<string.length;i++){
sum += weightByCharacter(string[i])*(i+1);
}
return (sum+104) % 103;
}
//Get the encoded data by the id of the character
function encodingById(id){
for(var i=0;i<code128b.length;i++){
if(code128b[i][2]==id){
return code128b[i][1];
}
}
return "";
}
//Get the id (weight) of a character
function weightByCharacter(character){
for(var i=0;i<code128b.length;i++){
if(code128b[i][0]==character){
return code128b[i][2];
}
}
return 0;
}
//Get the encoded data of a character
function encodingByChar(character){
for(var i=0;i<code128b.length;i++){
if(code128b[i][0]==character){
return code128b[i][1];
}
}
return "";
}
}