|
@@ -0,0 +1,398 @@
|
|
1
|
+const CHAR_TILDE = 126;
|
|
2
|
+const CODE_FNC1 = 102;
|
|
3
|
+const SET_STARTA = 103;
|
|
4
|
+const SET_STARTB = 104;
|
|
5
|
+const SET_STARTC = 105;
|
|
6
|
+const SET_SHIFT = 98;
|
|
7
|
+const SET_CODEA = 101;
|
|
8
|
+const SET_CODEB = 100;
|
|
9
|
+const SET_STOP = 106;
|
|
10
|
+
|
|
11
|
+const REPLACE_CODES = {
|
|
12
|
+ CHAR_TILDE: CODE_FNC1 //~ corresponds to FNC1 in GS1-128 standard
|
|
13
|
+}
|
|
14
|
+
|
|
15
|
+const CODESET = {
|
|
16
|
+ ANY: 1,
|
|
17
|
+ AB: 2,
|
|
18
|
+ A: 3,
|
|
19
|
+ B: 4,
|
|
20
|
+ C: 5
|
|
21
|
+};
|
|
22
|
+
|
|
23
|
+function getBytes(str) {
|
|
24
|
+ var bytes = [];
|
|
25
|
+ for (var i = 0; i < str.length; i++) {
|
|
26
|
+ bytes.push(str.charCodeAt(i));
|
|
27
|
+ }
|
|
28
|
+ return bytes;
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+export function code128(ctx, text, width, height) {
|
|
32
|
+
|
|
33
|
+ width = parseInt(width);
|
|
34
|
+
|
|
35
|
+ height = parseInt(height);
|
|
36
|
+
|
|
37
|
+ var codes = stringToCode128(text);
|
|
38
|
+
|
|
39
|
+ var g = new Graphics(ctx, width, height);
|
|
40
|
+
|
|
41
|
+ var barWeight = g.area.width / ((codes.length - 3) * 11 + 35);
|
|
42
|
+
|
|
43
|
+ var x = g.area.left;
|
|
44
|
+ var y = g.area.top;
|
|
45
|
+ for (var i = 0; i < codes.length; i++) {
|
|
46
|
+ var c = codes[i];
|
|
47
|
+ //two bars at a time: 1 black and 1 white
|
|
48
|
+ for (var bar = 0; bar < 8; bar += 2) {
|
|
49
|
+ var barW = PATTERNS[c][bar] * barWeight;
|
|
50
|
+ // var barH = height - y - this.border;
|
|
51
|
+ var barH = height - y;
|
|
52
|
+ var spcW = PATTERNS[c][bar + 1] * barWeight;
|
|
53
|
+
|
|
54
|
+ //no need to draw if 0 width
|
|
55
|
+ if (barW > 0) {
|
|
56
|
+ g.fillFgRect(x, y, barW, barH);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ x += barW + spcW;
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+function stringToCode128(text) {
|
|
66
|
+
|
|
67
|
+ var barc = {
|
|
68
|
+ currcs: CODESET.C
|
|
69
|
+ };
|
|
70
|
+
|
|
71
|
+ var bytes = getBytes(text);
|
|
72
|
+ //decide starting codeset
|
|
73
|
+ var index = bytes[0] == CHAR_TILDE ? 1 : 0;
|
|
74
|
+
|
|
75
|
+ var csa1 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB;
|
|
76
|
+ var csa2 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB;
|
|
77
|
+ barc.currcs = getBestStartSet(csa1, csa2);
|
|
78
|
+ barc.currcs = perhapsCodeC(bytes, barc.currcs);
|
|
79
|
+
|
|
80
|
+ //if no codeset changes this will end up with bytes.length+3
|
|
81
|
+ //start, checksum and stop
|
|
82
|
+ var codes = new Array();
|
|
83
|
+
|
|
84
|
+ switch (barc.currcs) {
|
|
85
|
+ case CODESET.A:
|
|
86
|
+ codes.push(SET_STARTA);
|
|
87
|
+ break;
|
|
88
|
+ case CODESET.B:
|
|
89
|
+ codes.push(SET_STARTB);
|
|
90
|
+ break;
|
|
91
|
+ default:
|
|
92
|
+ codes.push(SET_STARTC);
|
|
93
|
+ break;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+ for (var i = 0; i < bytes.length; i++) {
|
|
98
|
+ var b1 = bytes[i]; //get the first of a pair
|
|
99
|
+ //should we translate/replace
|
|
100
|
+ if (b1 in REPLACE_CODES) {
|
|
101
|
+ codes.push(REPLACE_CODES[b1]);
|
|
102
|
+ i++ //jump to next
|
|
103
|
+ b1 = bytes[i];
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ //get the next in the pair if possible
|
|
107
|
+ var b2 = bytes.length > (i + 1) ? bytes[i + 1] : -1;
|
|
108
|
+
|
|
109
|
+ codes = codes.concat(codesForChar(b1, b2, barc.currcs));
|
|
110
|
+ //code C takes 2 chars each time
|
|
111
|
+ if (barc.currcs == CODESET.C) i++;
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ //calculate checksum according to Code 128 standards
|
|
115
|
+ var checksum = codes[0];
|
|
116
|
+ for (var weight = 1; weight < codes.length; weight++) {
|
|
117
|
+ checksum += (weight * codes[weight]);
|
|
118
|
+ }
|
|
119
|
+ codes.push(checksum % 103);
|
|
120
|
+
|
|
121
|
+ codes.push(SET_STOP);
|
|
122
|
+
|
|
123
|
+ //encoding should now be complete
|
|
124
|
+ return codes;
|
|
125
|
+
|
|
126
|
+ //chr1 is current byte
|
|
127
|
+ //chr2 is the next byte to process. looks ahead.
|
|
128
|
+ function codesForChar(chr1, chr2, currcs) {
|
|
129
|
+ var result = [];
|
|
130
|
+ var shifter = -1;
|
|
131
|
+
|
|
132
|
+ if (charCompatible(chr1, currcs)) {
|
|
133
|
+ if (currcs == CODESET.C) {
|
|
134
|
+ if (chr2 == -1) {
|
|
135
|
+ shifter = SET_CODEB;
|
|
136
|
+ currcs = CODESET.B;
|
|
137
|
+ }
|
|
138
|
+ else if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
|
|
139
|
+ //need to check ahead as well
|
|
140
|
+ if (charCompatible(chr2, CODESET.A)) {
|
|
141
|
+ shifter = SET_CODEA;
|
|
142
|
+ currcs = CODESET.A;
|
|
143
|
+ }
|
|
144
|
+ else {
|
|
145
|
+ shifter = SET_CODEB;
|
|
146
|
+ currcs = CODESET.B;
|
|
147
|
+ }
|
|
148
|
+ }
|
|
149
|
+ }
|
|
150
|
+ }
|
|
151
|
+ else {
|
|
152
|
+ //if there is a next char AND that next char is also not compatible
|
|
153
|
+ if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
|
|
154
|
+ //need to switch code sets
|
|
155
|
+ switch (currcs) {
|
|
156
|
+ case CODESET.A:
|
|
157
|
+ shifter = SET_CODEB;
|
|
158
|
+ currcs = CODESET.B;
|
|
159
|
+ break;
|
|
160
|
+ case CODESET.B:
|
|
161
|
+ shifter = SET_CODEA;
|
|
162
|
+ currcs = CODESET.A;
|
|
163
|
+ break;
|
|
164
|
+ }
|
|
165
|
+ }
|
|
166
|
+ else {
|
|
167
|
+ //no need to shift code sets, a temporary SHIFT will suffice
|
|
168
|
+ shifter = SET_SHIFT;
|
|
169
|
+ }
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ //ok some type of shift is nessecary
|
|
173
|
+ if (shifter != -1) {
|
|
174
|
+ result.push(shifter);
|
|
175
|
+ result.push(codeValue(chr2));
|
|
176
|
+ }
|
|
177
|
+ else {
|
|
178
|
+ if (currcs == CODESET.C) {
|
|
179
|
+ //include next as well
|
|
180
|
+ result.push(codeValue(chr1, chr2));
|
|
181
|
+ }
|
|
182
|
+ else {
|
|
183
|
+ result.push(codeValue(chr1));
|
|
184
|
+ }
|
|
185
|
+ }
|
|
186
|
+ barc.currcs = currcs;
|
|
187
|
+
|
|
188
|
+ return result;
|
|
189
|
+ }
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+function perhapsCodeC(bytes, codeset) {
|
|
193
|
+ for (var i = 0; i < bytes.length; i++) {
|
|
194
|
+ var b = bytes[i]
|
|
195
|
+ if ((b < 48 || b > 57) && b != CHAR_TILDE)
|
|
196
|
+ return codeset;
|
|
197
|
+ }
|
|
198
|
+ return CODESET.C;
|
|
199
|
+}
|
|
200
|
+
|
|
201
|
+function getBestStartSet(csa1, csa2) {
|
|
202
|
+ //tries to figure out the best codeset
|
|
203
|
+ //to start with to get the most compact code
|
|
204
|
+ var vote = 0;
|
|
205
|
+ vote += csa1 == CODESET.A ? 1 : 0;
|
|
206
|
+ vote += csa1 == CODESET.B ? -1 : 0;
|
|
207
|
+ vote += csa2 == CODESET.A ? 1 : 0;
|
|
208
|
+ vote += csa2 == CODESET.B ? -1 : 0;
|
|
209
|
+ //tie goes to B due to my own predudices
|
|
210
|
+ return vote > 0 ? CODESET.A : CODESET.B;
|
|
211
|
+}
|
|
212
|
+
|
|
213
|
+//reduce the ascii code to fit into the Code128 char table
|
|
214
|
+function codeValue(chr1, chr2) {
|
|
215
|
+ if (typeof chr2 == "undefined") {
|
|
216
|
+ return chr1 >= 32 ? chr1 - 32 : chr1 + 64;
|
|
217
|
+ }
|
|
218
|
+ else {
|
|
219
|
+ return parseInt(String.fromCharCode(chr1) + String.fromCharCode(chr2));
|
|
220
|
+ }
|
|
221
|
+}
|
|
222
|
+
|
|
223
|
+function charCompatible(chr, codeset) {
|
|
224
|
+ var csa = codeSetAllowedFor(chr);
|
|
225
|
+ if (csa == CODESET.ANY) return true;
|
|
226
|
+ //if we need to change from current
|
|
227
|
+ if (csa == CODESET.AB) return true;
|
|
228
|
+ if (csa == CODESET.A && codeset == CODESET.A) return true;
|
|
229
|
+ if (csa == CODESET.B && codeset == CODESET.B) return true;
|
|
230
|
+ return false;
|
|
231
|
+}
|
|
232
|
+
|
|
233
|
+function codeSetAllowedFor(chr) {
|
|
234
|
+ if (chr >= 48 && chr <= 57) {
|
|
235
|
+ //0-9
|
|
236
|
+ return CODESET.ANY;
|
|
237
|
+ }
|
|
238
|
+ else if (chr >= 32 && chr <= 95) {
|
|
239
|
+ //0-9 A-Z
|
|
240
|
+ return CODESET.AB;
|
|
241
|
+ }
|
|
242
|
+ else {
|
|
243
|
+ //if non printable
|
|
244
|
+ return chr < 32 ? CODESET.A : CODESET.B;
|
|
245
|
+ }
|
|
246
|
+}
|
|
247
|
+
|
|
248
|
+var Graphics = function(ctx, width, height) {
|
|
249
|
+
|
|
250
|
+ this.width = width;
|
|
251
|
+ this.height = height;
|
|
252
|
+ this.quiet = Math.round(this.width / 40);
|
|
253
|
+
|
|
254
|
+ this.border_size = 0;
|
|
255
|
+ this.padding_width = 0;
|
|
256
|
+
|
|
257
|
+ this.area = {
|
|
258
|
+ width : width - this.padding_width * 2 - this.quiet * 2,
|
|
259
|
+ height: height - this.border_size * 2,
|
|
260
|
+ top : this.border_size - 4,
|
|
261
|
+ left : this.padding_width + this.quiet
|
|
262
|
+ };
|
|
263
|
+
|
|
264
|
+ this.ctx = ctx;
|
|
265
|
+ this.fg = "#000000";
|
|
266
|
+ this.bg = "#ffffff";
|
|
267
|
+
|
|
268
|
+ // fill background
|
|
269
|
+ this.fillBgRect(0,0, width, height);
|
|
270
|
+
|
|
271
|
+ // fill center to create border
|
|
272
|
+ this.fillBgRect(0, this.border_size, width, height - this.border_size * 2);
|
|
273
|
+}
|
|
274
|
+
|
|
275
|
+//use native color
|
|
276
|
+Graphics.prototype._fillRect = function(x, y, width, height, color) {
|
|
277
|
+ this.ctx.fillStyle = color
|
|
278
|
+ this.ctx.fillRect(x, y, width, height)
|
|
279
|
+}
|
|
280
|
+
|
|
281
|
+Graphics.prototype.fillFgRect = function(x,y, width, height) {
|
|
282
|
+ this._fillRect(x, y, width, height, this.fg);
|
|
283
|
+}
|
|
284
|
+
|
|
285
|
+Graphics.prototype.fillBgRect = function(x,y, width, height) {
|
|
286
|
+ this._fillRect(x, y, width, height, this.bg);
|
|
287
|
+}
|
|
288
|
+
|
|
289
|
+var PATTERNS = [
|
|
290
|
+ [2, 1, 2, 2, 2, 2, 0, 0], // 0
|
|
291
|
+ [2, 2, 2, 1, 2, 2, 0, 0], // 1
|
|
292
|
+ [2, 2, 2, 2, 2, 1, 0, 0], // 2
|
|
293
|
+ [1, 2, 1, 2, 2, 3, 0, 0], // 3
|
|
294
|
+ [1, 2, 1, 3, 2, 2, 0, 0], // 4
|
|
295
|
+ [1, 3, 1, 2, 2, 2, 0, 0], // 5
|
|
296
|
+ [1, 2, 2, 2, 1, 3, 0, 0], // 6
|
|
297
|
+ [1, 2, 2, 3, 1, 2, 0, 0], // 7
|
|
298
|
+ [1, 3, 2, 2, 1, 2, 0, 0], // 8
|
|
299
|
+ [2, 2, 1, 2, 1, 3, 0, 0], // 9
|
|
300
|
+ [2, 2, 1, 3, 1, 2, 0, 0], // 10
|
|
301
|
+ [2, 3, 1, 2, 1, 2, 0, 0], // 11
|
|
302
|
+ [1, 1, 2, 2, 3, 2, 0, 0], // 12
|
|
303
|
+ [1, 2, 2, 1, 3, 2, 0, 0], // 13
|
|
304
|
+ [1, 2, 2, 2, 3, 1, 0, 0], // 14
|
|
305
|
+ [1, 1, 3, 2, 2, 2, 0, 0], // 15
|
|
306
|
+ [1, 2, 3, 1, 2, 2, 0, 0], // 16
|
|
307
|
+ [1, 2, 3, 2, 2, 1, 0, 0], // 17
|
|
308
|
+ [2, 2, 3, 2, 1, 1, 0, 0], // 18
|
|
309
|
+ [2, 2, 1, 1, 3, 2, 0, 0], // 19
|
|
310
|
+ [2, 2, 1, 2, 3, 1, 0, 0], // 20
|
|
311
|
+ [2, 1, 3, 2, 1, 2, 0, 0], // 21
|
|
312
|
+ [2, 2, 3, 1, 1, 2, 0, 0], // 22
|
|
313
|
+ [3, 1, 2, 1, 3, 1, 0, 0], // 23
|
|
314
|
+ [3, 1, 1, 2, 2, 2, 0, 0], // 24
|
|
315
|
+ [3, 2, 1, 1, 2, 2, 0, 0], // 25
|
|
316
|
+ [3, 2, 1, 2, 2, 1, 0, 0], // 26
|
|
317
|
+ [3, 1, 2, 2, 1, 2, 0, 0], // 27
|
|
318
|
+ [3, 2, 2, 1, 1, 2, 0, 0], // 28
|
|
319
|
+ [3, 2, 2, 2, 1, 1, 0, 0], // 29
|
|
320
|
+ [2, 1, 2, 1, 2, 3, 0, 0], // 30
|
|
321
|
+ [2, 1, 2, 3, 2, 1, 0, 0], // 31
|
|
322
|
+ [2, 3, 2, 1, 2, 1, 0, 0], // 32
|
|
323
|
+ [1, 1, 1, 3, 2, 3, 0, 0], // 33
|
|
324
|
+ [1, 3, 1, 1, 2, 3, 0, 0], // 34
|
|
325
|
+ [1, 3, 1, 3, 2, 1, 0, 0], // 35
|
|
326
|
+ [1, 1, 2, 3, 1, 3, 0, 0], // 36
|
|
327
|
+ [1, 3, 2, 1, 1, 3, 0, 0], // 37
|
|
328
|
+ [1, 3, 2, 3, 1, 1, 0, 0], // 38
|
|
329
|
+ [2, 1, 1, 3, 1, 3, 0, 0], // 39
|
|
330
|
+ [2, 3, 1, 1, 1, 3, 0, 0], // 40
|
|
331
|
+ [2, 3, 1, 3, 1, 1, 0, 0], // 41
|
|
332
|
+ [1, 1, 2, 1, 3, 3, 0, 0], // 42
|
|
333
|
+ [1, 1, 2, 3, 3, 1, 0, 0], // 43
|
|
334
|
+ [1, 3, 2, 1, 3, 1, 0, 0], // 44
|
|
335
|
+ [1, 1, 3, 1, 2, 3, 0, 0], // 45
|
|
336
|
+ [1, 1, 3, 3, 2, 1, 0, 0], // 46
|
|
337
|
+ [1, 3, 3, 1, 2, 1, 0, 0], // 47
|
|
338
|
+ [3, 1, 3, 1, 2, 1, 0, 0], // 48
|
|
339
|
+ [2, 1, 1, 3, 3, 1, 0, 0], // 49
|
|
340
|
+ [2, 3, 1, 1, 3, 1, 0, 0], // 50
|
|
341
|
+ [2, 1, 3, 1, 1, 3, 0, 0], // 51
|
|
342
|
+ [2, 1, 3, 3, 1, 1, 0, 0], // 52
|
|
343
|
+ [2, 1, 3, 1, 3, 1, 0, 0], // 53
|
|
344
|
+ [3, 1, 1, 1, 2, 3, 0, 0], // 54
|
|
345
|
+ [3, 1, 1, 3, 2, 1, 0, 0], // 55
|
|
346
|
+ [3, 3, 1, 1, 2, 1, 0, 0], // 56
|
|
347
|
+ [3, 1, 2, 1, 1, 3, 0, 0], // 57
|
|
348
|
+ [3, 1, 2, 3, 1, 1, 0, 0], // 58
|
|
349
|
+ [3, 3, 2, 1, 1, 1, 0, 0], // 59
|
|
350
|
+ [3, 1, 4, 1, 1, 1, 0, 0], // 60
|
|
351
|
+ [2, 2, 1, 4, 1, 1, 0, 0], // 61
|
|
352
|
+ [4, 3, 1, 1, 1, 1, 0, 0], // 62
|
|
353
|
+ [1, 1, 1, 2, 2, 4, 0, 0], // 63
|
|
354
|
+ [1, 1, 1, 4, 2, 2, 0, 0], // 64
|
|
355
|
+ [1, 2, 1, 1, 2, 4, 0, 0], // 65
|
|
356
|
+ [1, 2, 1, 4, 2, 1, 0, 0], // 66
|
|
357
|
+ [1, 4, 1, 1, 2, 2, 0, 0], // 67
|
|
358
|
+ [1, 4, 1, 2, 2, 1, 0, 0], // 68
|
|
359
|
+ [1, 1, 2, 2, 1, 4, 0, 0], // 69
|
|
360
|
+ [1, 1, 2, 4, 1, 2, 0, 0], // 70
|
|
361
|
+ [1, 2, 2, 1, 1, 4, 0, 0], // 71
|
|
362
|
+ [1, 2, 2, 4, 1, 1, 0, 0], // 72
|
|
363
|
+ [1, 4, 2, 1, 1, 2, 0, 0], // 73
|
|
364
|
+ [1, 4, 2, 2, 1, 1, 0, 0], // 74
|
|
365
|
+ [2, 4, 1, 2, 1, 1, 0, 0], // 75
|
|
366
|
+ [2, 2, 1, 1, 1, 4, 0, 0], // 76
|
|
367
|
+ [4, 1, 3, 1, 1, 1, 0, 0], // 77
|
|
368
|
+ [2, 4, 1, 1, 1, 2, 0, 0], // 78
|
|
369
|
+ [1, 3, 4, 1, 1, 1, 0, 0], // 79
|
|
370
|
+ [1, 1, 1, 2, 4, 2, 0, 0], // 80
|
|
371
|
+ [1, 2, 1, 1, 4, 2, 0, 0], // 81
|
|
372
|
+ [1, 2, 1, 2, 4, 1, 0, 0], // 82
|
|
373
|
+ [1, 1, 4, 2, 1, 2, 0, 0], // 83
|
|
374
|
+ [1, 2, 4, 1, 1, 2, 0, 0], // 84
|
|
375
|
+ [1, 2, 4, 2, 1, 1, 0, 0], // 85
|
|
376
|
+ [4, 1, 1, 2, 1, 2, 0, 0], // 86
|
|
377
|
+ [4, 2, 1, 1, 1, 2, 0, 0], // 87
|
|
378
|
+ [4, 2, 1, 2, 1, 1, 0, 0], // 88
|
|
379
|
+ [2, 1, 2, 1, 4, 1, 0, 0], // 89
|
|
380
|
+ [2, 1, 4, 1, 2, 1, 0, 0], // 90
|
|
381
|
+ [4, 1, 2, 1, 2, 1, 0, 0], // 91
|
|
382
|
+ [1, 1, 1, 1, 4, 3, 0, 0], // 92
|
|
383
|
+ [1, 1, 1, 3, 4, 1, 0, 0], // 93
|
|
384
|
+ [1, 3, 1, 1, 4, 1, 0, 0], // 94
|
|
385
|
+ [1, 1, 4, 1, 1, 3, 0, 0], // 95
|
|
386
|
+ [1, 1, 4, 3, 1, 1, 0, 0], // 96
|
|
387
|
+ [4, 1, 1, 1, 1, 3, 0, 0], // 97
|
|
388
|
+ [4, 1, 1, 3, 1, 1, 0, 0], // 98
|
|
389
|
+ [1, 1, 3, 1, 4, 1, 0, 0], // 99
|
|
390
|
+ [1, 1, 4, 1, 3, 1, 0, 0], // 100
|
|
391
|
+ [3, 1, 1, 1, 4, 1, 0, 0], // 101
|
|
392
|
+ [4, 1, 1, 1, 3, 1, 0, 0], // 102
|
|
393
|
+ [2, 1, 1, 4, 1, 2, 0, 0], // 103
|
|
394
|
+ [2, 1, 1, 2, 1, 4, 0, 0], // 104
|
|
395
|
+ [2, 1, 1, 2, 3, 2, 0, 0], // 105
|
|
396
|
+ [2, 3, 3, 1, 1, 1, 2, 0] // 106
|
|
397
|
+]
|
|
398
|
+
|