-
Notifications
You must be signed in to change notification settings - Fork 2
/
code128.js
347 lines (262 loc) · 8.29 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* This file is part of HTML Barcode SDK.
*
*
* ConnectCode provides its HTML Barcode SDK under a dual license model designed
* to meet the development and distribution needs of both commercial application
* distributors and open source projects.
*
* For open source projects, please see the GNU GPL notice below.
*
* For Commercial Application Distributors (OEMs, ISVs and VARs),
* please see <http://www.barcoderesource.com/duallicense.shtml> for more information.
*
*
*
*
* GNU GPL v3.0 License
*
* HTML Barcode SDK is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HTML Barcode SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
*/
export const pr = function(filtereddata) {
var data = code128(filtereddata)
var asiiStr = ""
for(var i=0; i<data.length; i++){
asiiStr += data.charCodeAt(i)+" "
}
return asiiStr;
}
export default function code128(filtereddata) {
if (detectAllNumbers(filtereddata) ) {
return digitCode(filtereddata)
}
return nonDigitCode(filtereddata)
}
// const START_CODEA = 208
const START_CODEB = 209
const START_CODEC = 210
const END_CODE = 211
const END_CODE_CHARCTER = String.fromCharCode(END_CODE)
function nonDigitCode(filtereddata) {
filtereddata = addShift(filtereddata);
let cd = generateCheckDigit_Code128ABAuto(filtereddata);
filtereddata = getAutoSwitchingAB(filtereddata);
filtereddata = filtereddata + cd;
let Result = filtereddata;
var startc = START_CODEB;
Result = String.fromCharCode(startc) + Result + END_CODE_CHARCTER
return Result
}
function digitCode(filtereddata) {
let cd = generateCheckDigit_Code128CAuto(filtereddata);
let lenFiltered = filtereddata.length;
let Result = ""
for (x = 0; x < lenFiltered; x = x + 2) {
var tstr = filtereddata.substr(x, 2);
num = parseInt(tstr, 10);
Result = Result + getCode128CCharacterAuto(num);
}
Result = Result + cd;
startc = START_CODEC;
Result = String.fromCharCode(startc) + Result + END_CODE_CHARCTER
return Result;
}
function getCode128ABValueAuto(inputchar) {
var returnvalue = 0;
if ((inputchar <= 31) && (inputchar >= 0))
returnvalue = (inputchar + 64);
else if ((inputchar <= 127) && (inputchar >= 32))
returnvalue = (inputchar - 32);
else if (inputchar == 230)
returnvalue = 98;
else
returnvalue = -1;
return returnvalue;
}
function getCode128ABCharacterAuto(inputvalue) {
if ((inputvalue <= 94) && (inputvalue >= 0))
inputvalue = inputvalue + 32;
else if ((inputvalue <= 106) && (inputvalue >= 95))
inputvalue = inputvalue + 105;
else
inputvalue = -1;
return String.fromCharCode(inputvalue);
}
function getCode128CCharacterAuto(inputvalue) {
if ((inputvalue <= 94) && (inputvalue >= 0))
inputvalue = inputvalue + 32;
else if ((inputvalue <= 106) && (inputvalue >= 95))
inputvalue = inputvalue + 105;
else
inputvalue = -1;
return String.fromCharCode(inputvalue);
}
function generateCheckDigit_Code128ABAuto(data) {
var datalength = 0;
var Sum = 104;
var Result = -1;
var strResult = "";
datalength = data.length;
var num = 0;
var Weight = 1;
var x = 0;
while (x < data.length) {
num = ScanAhead_8orMore_Numbers(data, x);
if (num >= 8) {
endpoint = x + num;
var BtoC = 99;
Sum = Sum + (BtoC * (Weight));
Weight = Weight + 1;
while (x < endpoint) {
num = parseInt(data.substr(x, 2), 10);
Sum = Sum + (num * (Weight));
x = x + 2;
Weight = Weight + 1;
}
var CtoB = 100;
Sum = Sum + (CtoB * (Weight));
Weight = Weight + 1;
}
else {
num = data.charCodeAt(x);
Sum = Sum + (getCode128ABValueAuto(num) * (Weight));
x = x + 1;
Weight = Weight + 1;
}
}
Result = Sum % 103;
strResult = getCode128ABCharacterAuto(Result);
return strResult;
}
function getCode128CCharacter(inputvalue) {
if ((inputvalue <= 94) && (inputvalue >= 0))
inputvalue = inputvalue + 32;
else if (inputvalue >= 95)
// inputvalue = inputvalue + 32 + 100;//original 인데
inputvalue = inputvalue + 105;
else
inputvalue = -1;
return String.fromCharCode(inputvalue);
}
function generateCheckDigit_Code128CAuto(data) {
var Sum = 105;
var Result = -1;
var strResult = "";
var datalength = data.length;
var x = 0;
var Weight = 1;
var num = 0;
for (x = 0; x < datalength; x = x + 2) {
num = parseInt(data.substr(x, 2), 10);
Sum = Sum + (num * Weight);
Weight = Weight + 1;
}
Result = Sum % 103;
strResult = getCode128CCharacter(Result);
return strResult;
}
function OptimizeNumbers(data, x, strResult, num) {
var BtoC = String.fromCharCode(204);
var strResult = strResult + BtoC;
var endpoint = x + num;
while (x < endpoint) {
var twonum = parseInt(data.substr(x, 2), 10);
strResult = strResult + getCode128CCharacterAuto(twonum);
x = x + 2;
}
var CtoB = String.fromCharCode(205);
strResult = strResult + CtoB;
return strResult;
}
function ScanAhead_8orMore_Numbers(data, x) {
var numNumbers = 0;
var exitx = 0;
while ((x < data.length) && (exitx == 0)) {
var barcodechar = data.substr(x, 1);
var barcodevalue = barcodechar.charCodeAt(0);
if (barcodevalue >= 48 && barcodevalue <= 57)
numNumbers = numNumbers + 1;
else
exitx = 1;
x = x + 1;
}
if (numNumbers > 8) {
if (numNumbers % 2 == 1)
numNumbers = numNumbers - 1;
}
return numNumbers;
}
function getAutoSwitchingAB(data) {
var datalength = 0;
var strResult = "";
var shiftchar = String.fromCharCode(230);
datalength = data.length;
var barcodechar = "";
var x = 0;
for (x = 0; x < datalength; x++) {
barcodechar = data.substr(x, 1);
var barcodevalue = barcodechar.charCodeAt(0);
if (barcodevalue == 31) {
barcodechar = String.fromCharCode(barcodechar.charCodeAt(0) + 96 + 100);
strResult = strResult + barcodechar;
}
else if (barcodevalue == 127) {
barcodechar = String.fromCharCode(barcodechar.charCodeAt(0) + 100);
strResult = strResult + barcodechar;
}
else {
var num = ScanAhead_8orMore_Numbers(data, x);
if (num >= 8) {
strResult = OptimizeNumbers(data, x, strResult, num);
x = x + num;
x = x - 1;
}
else
strResult = strResult + barcodechar;
}
}
return strResult;
}
function detectAllNumbers(data) {
var datalength = data.length;
if (datalength % 2 == 1) {
return false;
}
for (x = 0; x < datalength; x++) {
var barcodechar = data.charCodeAt(x);
if (barcodechar > 57 || barcodechar < 48) {
return false
}
}
return true;
}
function addShift(data) {
var datalength = 0;
var strResult = "";
var shiftchar = String.fromCharCode(230);
datalength = data.length;
for (var x = 0; x < datalength; x++) {
var barcodechar = data.substr(x, 1);
var barcodevalue = barcodechar.charCodeAt(0);
if ((barcodevalue <= 31) && (barcodevalue >= 0)) {
strResult = strResult + shiftchar;
barcodechar = String.fromCharCode(barcodechar.charCodeAt(0) + 96);
strResult = strResult + barcodechar;
}
else
strResult = strResult + barcodechar;
}
return strResult;
}