-
Notifications
You must be signed in to change notification settings - Fork 97
/
Code2Of5.java
497 lines (421 loc) · 14.1 KB
/
Code2Of5.java
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Copyright 2014 Robin Stuart
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.org.okapibarcode.backend;
import static uk.org.okapibarcode.backend.HumanReadableLocation.NONE;
import static uk.org.okapibarcode.backend.HumanReadableLocation.TOP;
import uk.org.okapibarcode.graphics.Rectangle;
import uk.org.okapibarcode.graphics.TextBox;
/**
* Implements the Code 2 of 5 family of barcode standards.
*
* @author <a href="mailto:[email protected]">Robert Elliott</a>
*/
public class Code2Of5 extends Symbol {
/**
* The different Code 2 of 5 barcode variants available to encode.
*/
public enum ToFMode {
/**
* Standard Code 2 of 5 mode, also known as Code 2 of 5 Matrix. Encodes any
* length numeric input (digits 0-9). This is the default mode.
*/
MATRIX,
/**
* Industrial Code 2 of 5 which can encode any length numeric input (digits 0-9)
* and does not include a check digit.
*/
INDUSTRIAL,
/**
* International Air Transport Agency variation of Code 2 of 5. Encodes any length
* numeric input (digits 0-9) and does not include a check digit.
*/
IATA,
/**
* Code 2 of 5 Data Logic. Encodes any length numeric input (digits 0-9) and does
* not include a check digit.
*/
DATA_LOGIC,
/**
* Interleaved Code 2 of 5. Encodes pairs of numbers, and so can only encode an even
* number of digits (0-9). If an odd number of digits is entered a leading zero is
* added. No check digit is calculated.
*/
INTERLEAVED,
/**
* Interleaved Code 2 of 5 with check digit. Encodes pairs of numbers, and so can
* only encode an even number of digits (0-9). If adding the check digit results
* in an odd number of digits then a leading zero is added.
*/
INTERLEAVED_WITH_CHECK_DIGIT,
/**
* ITF-14, also known as UPC Shipping Container Symbol or Case Code. Requires a
* 13-digit numeric input (digits 0-9). One modulo-10 check digit is calculated.
*/
ITF14,
/**
* Deutsche Post Leitcode. Requires a 13-digit numerical input. Check digit is
* calculated.
*/
DP_LEITCODE,
/**
* Deutsche Post Identcode. Requires an 11-digit numerical input. Check digit is
* calculated.
*/
DP_IDENTCODE
}
private static final String[] C25_MATRIX_TABLE = {
"113311", "311131", "131131", "331111", "113131", "313111", "133111", "111331", "311311", "131311"
};
private static final String[] C25_INDUSTRIAL_TABLE = {
"1111313111", "3111111131", "1131111131", "3131111111", "1111311131", "3111311111", "1131311111", "1111113131", "3111113111", "1131113111"
};
private static final String[] C25_INTERLEAVED_TABLE = {
"11331", "31113", "13113", "33111", "11313", "31311", "13311", "11133", "31131", "13131"
};
/** The 2-of-5 mode. */
private ToFMode mode;
/** Ratio of wide bar width to narrow bar width. */
private double moduleWidthRatio = 3;
/**
* Creates a new instance, using mode {@link ToFMode#MATRIX}.
*/
public Code2Of5() {
this(ToFMode.MATRIX);
}
/**
* Creates a new instance, using the specified mode.
*
* @param mode the 2-of-5 mode
*/
public Code2Of5(ToFMode mode) {
this.mode = mode;
}
/**
* Sets the 2-of-5 mode. The default value is {@link ToFMode#MATRIX}.
*
* @param mode the 2-of-5 mode
*/
public void setMode(ToFMode mode) {
this.mode = mode;
}
/**
* Returns the 2-of-5 mode.
*
* @return the 2-of-5 mode
*/
public ToFMode getMode() {
return mode;
}
/**
* Sets the ratio of wide bar width to narrow bar width. Valid values are usually
* between {@code 2} and {@code 3}. The default value is {@code 3}.
*
* @param moduleWidthRatio the ratio of wide bar width to narrow bar width
*/
public void setModuleWidthRatio(double moduleWidthRatio) {
this.moduleWidthRatio = moduleWidthRatio;
}
/**
* Returns the ratio of wide bar width to narrow bar width.
*
* @return the ratio of wide bar width to narrow bar width
*/
public double getModuleWidthRatio() {
return moduleWidthRatio;
}
@Override
protected void encode() {
switch (mode) {
case MATRIX:
dataMatrix();
break;
case INDUSTRIAL:
industrial();
break;
case IATA:
iata();
break;
case INTERLEAVED:
interleaved(false);
break;
case INTERLEAVED_WITH_CHECK_DIGIT:
interleaved(true);
break;
case DATA_LOGIC:
dataLogic();
break;
case ITF14:
itf14();
break;
case DP_LEITCODE:
deutschePostLeitcode();
break;
case DP_IDENTCODE:
deutschePostIdentcode();
break;
}
}
private void dataMatrix() {
if (!content.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
String dest = "311111";
for (int i = 0; i < content.length(); i++) {
dest += C25_MATRIX_TABLE[Character.getNumericValue(content.charAt(i))];
}
dest += "31111";
readable = content;
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private void industrial() {
if (!content.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
String dest = "313111";
for (int i = 0; i < content.length(); i++) {
dest += C25_INDUSTRIAL_TABLE[Character.getNumericValue(content.charAt(i))];
}
dest += "31113";
readable = content;
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private void iata() {
if (!content.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
String dest = "1111";
for (int i = 0; i < content.length(); i++) {
dest += C25_INDUSTRIAL_TABLE[Character.getNumericValue(content.charAt(i))];
}
dest += "311";
readable = content;
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private void dataLogic() {
if (!content.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
String dest = "1111";
for (int i = 0; i < content.length(); i++) {
dest += C25_MATRIX_TABLE[Character.getNumericValue(content.charAt(i))];
}
dest += "311";
readable = content;
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private void interleaved(boolean addCheckDigit) {
int i;
String dest;
readable = content;
if (!readable.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
if (addCheckDigit) {
char checkDigit = checkDigit(readable, 1, 3);
readable += checkDigit;
infoLine("Check Digit: " + checkDigit);
}
if ((readable.length() & 1) != 0) {
readable = "0" + readable;
}
dest = "1111";
for (i = 0; i < readable.length(); i += 2) {
dest += interlace(i, i + 1);
}
dest += "311";
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private String interlace(int x, int y) {
char a = readable.charAt(x);
char b = readable.charAt(y);
String one = C25_INTERLEAVED_TABLE[Character.getNumericValue(a)];
String two = C25_INTERLEAVED_TABLE[Character.getNumericValue(b)];
StringBuilder f = new StringBuilder(10);
for (int i = 0; i < 5; i++) {
f.append(one.charAt(i));
f.append(two.charAt(i));
}
return f.toString();
}
private void itf14() {
int i;
int input_length = content.length();
String dest;
if (!content.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
if (input_length > 13) {
throw OkapiInputException.inputTooLong();
}
readable = "";
for (i = input_length; i < 13; i++) {
readable += "0";
}
readable += content;
char checkDigit = checkDigit(readable, 1, 3);
readable += checkDigit;
infoLine("Check Digit: " + checkDigit);
dest = "1111";
for (i = 0; i < readable.length(); i += 2) {
dest += interlace(i, i + 1);
}
dest += "311";
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private void deutschePostLeitcode() {
int i;
int input_length = content.length();
String dest;
if (!content.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
if (input_length > 13) {
throw OkapiInputException.inputTooLong();
}
readable = "";
for (i = input_length; i < 13; i++) {
readable += "0";
}
readable += content;
char checkDigit = checkDigit(readable, 9, 4);
readable += checkDigit;
infoLine("Check digit: " + checkDigit);
dest = "1111";
for (i = 0; i < readable.length(); i += 2) {
dest += interlace(i, i + 1);
}
dest += "311";
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private void deutschePostIdentcode() {
int i;
int input_length = content.length();
String dest;
if (!content.matches("[0-9]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
if (input_length > 11) {
throw OkapiInputException.inputTooLong();
}
readable = "";
for (i = input_length; i < 11; i++) {
readable += "0";
}
readable += content;
char checkDigit = checkDigit(readable, 9, 4);
readable += checkDigit;
infoLine("Check Digit: " + checkDigit);
dest = "1111";
for (i = 0; i < readable.length(); i += 2) {
dest += interlace(i, i + 1);
}
dest += "311";
pattern = new String[] { dest };
row_count = 1;
row_height = new int[] { -1 };
}
private static char checkDigit(String s, int multiplier1, int multiplier2) {
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if ((i & 1) != 0) {
count += multiplier1 * (s.charAt(i) - '0');
} else {
count += multiplier2 * (s.charAt(i) - '0');
}
}
return (char) (((10 - (count % 10)) % 10) + '0');
}
@Override
protected void plotSymbol() {
resetPlotElements();
int baseY;
if (humanReadableLocation == TOP) {
baseY = getTheoreticalHumanReadableHeight();
} else {
baseY = 0;
}
double x = 0;
int y = baseY;
int h = 0;
boolean black = true;
int offset = 0;
if (mode == ToFMode.ITF14) {
offset = 20;
}
for (int xBlock = 0; xBlock < pattern[0].length(); xBlock++) {
char c = pattern[0].charAt(xBlock);
double w = getModuleWidth(c - '0') * moduleWidth;
if (black) {
if (row_height[0] == -1) {
h = default_height;
} else {
h = row_height[0];
}
if (w != 0 && h != 0) {
Rectangle rect = new Rectangle(x + offset, y, w, h);
rectangles.add(rect);
}
symbol_width = (int) Math.ceil(x + w + (2 * offset));
}
black = !black;
x += w;
}
symbol_height = h;
if (mode == ToFMode.ITF14) {
// Add bounding box
Rectangle topBar = new Rectangle(0, baseY, symbol_width, 4);
Rectangle bottomBar = new Rectangle(0, baseY + symbol_height - 4, symbol_width, 4);
Rectangle leftBar = new Rectangle(0, baseY, 4, symbol_height);
Rectangle rightBar = new Rectangle(symbol_width - 4, baseY, 4, symbol_height);
rectangles.add(topBar);
rectangles.add(bottomBar);
rectangles.add(leftBar);
rectangles.add(rightBar);
}
if (humanReadableLocation != NONE && !readable.isEmpty()) {
double baseline;
if (humanReadableLocation == TOP) {
baseline = fontSize;
} else {
baseline = symbol_height + fontSize;
}
texts.add(new TextBox(0, baseline, symbol_width, readable, humanReadableAlignment));
}
}
/** {@inheritDoc} */
@Override
protected double getModuleWidth(int originalWidth) {
if (originalWidth == 1) {
return 1;
} else {
return moduleWidthRatio;
}
}
}