-
Notifications
You must be signed in to change notification settings - Fork 97
/
Code128.java
950 lines (878 loc) · 34.6 KB
/
Code128.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
/*
* Copyright 2014-2018 Robin Stuart, Daniel Gredler
*
* 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 java.nio.charset.StandardCharsets.ISO_8859_1;
/**
* <p>Implements Code 128 bar code symbology according to ISO/IEC 15417:2007.
*
* <p>Code 128 supports encoding of 8-bit ISO 8859-1 (Latin-1) characters.
*
* <p>Setting GS1 mode allows encoding in GS1-128 (also known as UCC/EAN-128).
*
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
* @author Daniel Gredler
*/
public class Code128 extends Symbol {
/** Code sets and code set combinations which the user may require the symbol to use. */
public enum CodeSet {
/** Code set A, which can encode ASCII values 0-95, as well as FNC1, FNC2, FNC3 and FNC4. */
A,
/** Code set B, which can encode ASCII values 32-127, as well as FNC1, FNC2, FNC3 and FNC4. */
B,
/** Code set C, which can encode pairs of numbers, as well as FNC1. */
C,
/** Code sets A and B only (suppress code set C). */
AB,
/** No code set restrictions (code sets A, B and C are all allowed). */
ABC
}
private enum Mode {
NULL, SHIFTA, LATCHA, SHIFTB, LATCHB, SHIFTC, LATCHC, AORB, ABORC
}
private enum FMode {
SHIFTN, LATCHN, SHIFTF, LATCHF
}
private enum Composite {
OFF, CCA, CCB, CCC
}
protected static final String[] CODE128_TABLE = {
"212222", "222122", "222221", "121223", "121322", "131222", "122213",
"122312", "132212", "221213", "221312", "231212", "112232", "122132",
"122231", "113222", "123122", "123221", "223211", "221132", "221231",
"213212", "223112", "312131", "311222", "321122", "321221", "312212",
"322112", "322211", "212123", "212321", "232121", "111323", "131123",
"131321", "112313", "132113", "132311", "211313", "231113", "231311",
"112133", "112331", "132131", "113123", "113321", "133121", "313121",
"211331", "231131", "213113", "213311", "213131", "311123", "311321",
"331121", "312113", "312311", "332111", "314111", "221411", "431111",
"111224", "111422", "121124", "121421", "141122", "141221", "112214",
"112412", "122114", "122411", "142112", "142211", "241211", "221114",
"413111", "241112", "134111", "111242", "121142", "121241", "114212",
"124112", "124211", "411212", "421112", "421211", "212141", "214121",
"412121", "111143", "111341", "131141", "114113", "114311", "411113",
"411311", "113141", "114131", "311141", "411131", "211412", "211214",
"211232", "2331112"
};
private CodeSet codeSet;
private Composite compositeMode = Composite.OFF;
/**
* Creates a new instance.
*/
public Code128() {
this(CodeSet.ABC);
}
/**
* <p>Creates a new instance, using the specified code set restrictions.
*
* <p><b>NOTE:</b> Unless your application has very specific encoding requirements, it is recommended that no
* custom code set restrictions are used, allowing the system to fully optimize the encoded data.
*
* @param codeSet the code set restrictions to use
*/
public Code128(CodeSet codeSet) {
this.codeSet = codeSet;
}
/**
* <p>Sets the code set restrictions. The default value is {@link CodeSet#ABC}, which allows the use of any code set.
*
* <p><b>NOTE:</b> Unless your application has very specific encoding requirements, it is recommended that no
* custom code set restrictions are used, allowing the system to fully optimize the encoded data.
*
* @param codeSet the code set restrictions to use
*/
public void setCodeSet(CodeSet codeSet) {
this.codeSet = codeSet;
}
/**
* Returns the code set restrictions. The default value is {@link CodeSet#ABC}, which allows the use of any code set.
*
* @return the code set restrictions used
*/
public CodeSet getCodeSet() {
return codeSet;
}
protected void setCca() {
compositeMode = Composite.CCA;
}
protected void setCcb() {
compositeMode = Composite.CCB;
}
protected void setCcc() {
compositeMode = Composite.CCC;
}
public void unsetCc() {
compositeMode = Composite.OFF;
}
@Override
public boolean supportsGs1() {
return true;
}
@Override
protected boolean supportsFnc2() {
return true;
}
@Override
protected boolean supportsFnc3() {
return true;
}
@Override
protected boolean supportsFnc4() {
return true;
}
@Override
protected void encode() {
int i, j, k;
int input_point = 0;
Mode mode, last_mode;
Mode last_set, current_set;
double glyph_count;
int bar_characters = 0, total_sum = 0;
FMode f_state = FMode.LATCHN;
Mode[] mode_type = new Mode[200];
int[] mode_length = new int[200];
int[] values = new int[200];
int c;
int linkage_flag = 0;
int index_point = 0;
int read = 0;
inputData = toBytes(content, ISO_8859_1);
if (inputData == null) {
throw OkapiInputException.invalidCharactersInInput();
}
int sourcelen = inputData.length;
if (sourcelen > 170) {
throw OkapiInputException.inputTooLong();
}
/* Decide on mode using same system as PDF417 and rules of ISO 15417 Annex E */
if (sourcelen > 0) {
int letter = inputData[input_point];
int numbers = (letter >= '0' && letter <= '9' ? 1 : 0);
mode = findSubset(letter, numbers);
mode_type[0] = mode;
mode_length[0] += length(letter, mode);
for (i = 1; i < sourcelen; i++) {
letter = inputData[i];
last_mode = mode;
mode = findSubset(letter, numbers);
if (mode == last_mode) {
mode_length[index_point] += length(letter, mode);
} else {
index_point++;
mode_type[index_point] = mode;
mode_length[index_point] = length(letter, mode);
}
if (letter >= '0' && letter <= '9') {
numbers++;
} else {
numbers = 0;
}
}
index_point++;
index_point = reduceSubsetChanges(mode_type, mode_length, index_point);
}
/* Put set data into set[] (the calculated mode for each character) */
Mode[] set = new Mode[200];
read = 0;
if (sourcelen > 0) {
for (i = 0; i < index_point; i++) {
for (j = 0; j < mode_length[i]; j++) {
set[read] = mode_type[i];
read++;
}
}
} else {
set[0] = Mode.LATCHB; // empty barcode, avoid errors below
}
/* Resolve odd length LATCHC blocks */
int cs = 0, nums = 0, fncs = 0;
for (i = 0; i < read; i++) {
if (set[i] == Mode.LATCHC) {
cs++;
if (inputData[i] >= '0' && inputData[i] <= '9') {
nums++;
} else if (inputData[i] == FNC1) {
fncs++;
}
} else {
resolveOddCs(set, i, cs, nums, fncs);
cs = 0;
nums = 0;
fncs = 0;
}
}
resolveOddCs(set, i, cs, nums, fncs);
/* Adjust for strings which start with shift characters - make them latch instead */
if (set[0] == Mode.SHIFTA) {
i = 0;
do {
set[i] = Mode.LATCHA;
i++;
} while (set[i] == Mode.SHIFTA);
}
if (set[0] == Mode.SHIFTB) {
i = 0;
do {
set[i] = Mode.LATCHB;
i++;
} while (set[i] == Mode.SHIFTB);
}
/* Detect extended ASCII characters */
FMode[] fset = new FMode[200];
for (i = 0; i < sourcelen; i++) {
int ch = inputData[i];
if (ch >= 128 && ch != FNC1 && ch != FNC2 && ch != FNC3 && ch != FNC4) {
fset[i] = FMode.SHIFTF;
} else {
fset[i] = FMode.LATCHN;
}
}
/* Decide when to latch to extended mode - Annex E note 3 */
j = 0;
for (i = 0; i < sourcelen; i++) {
if (fset[i] == FMode.SHIFTF) {
j++;
} else {
j = 0;
}
if (j >= 5) {
for (k = i; k > (i - 5); k--) {
fset[k] = FMode.LATCHF;
}
}
if ((j >= 3) && (i == (sourcelen - 1))) {
for (k = i; k > (i - 3); k--) {
fset[k] = FMode.LATCHF;
}
}
}
/* Decide if it is worth reverting to 646 encodation for a few characters as described in 4.3.4.2 (d) */
for (i = 1; i < sourcelen; i++) {
if ((fset[i - 1] == FMode.LATCHF) && (fset[i] == FMode.LATCHN)) {
/* Detected a change from 8859-1 to 646 - count how long for */
/* There is one exception: code set C cannot shift in and out of extended mode */
for (j = 0; fset[i + j] == FMode.LATCHN &&
set[i + j] != Mode.LATCHC &&
set[i + j] != Mode.SHIFTC &&
i + j < sourcelen; j++) {
// keep counting
}
if ((j < 5) || ((j < 3) && ((i + j) == (sourcelen - 1)))) {
/* Uses the same figures recommended by Annex E note 3 */
/* Change to shifting back rather than latching back */
for (k = 0; k < j; k++) {
fset[i + k] = FMode.SHIFTN;
}
}
}
}
/* Now we can calculate how long the barcode is going to be - and stop it from being too long */
last_set = Mode.NULL;
glyph_count = 0.0;
for (i = 0; i < sourcelen; i++) {
if ((set[i] == Mode.SHIFTA) || (set[i] == Mode.SHIFTB)) {
glyph_count += 1.0;
}
if ((fset[i] == FMode.SHIFTF) || (fset[i] == FMode.SHIFTN)) {
glyph_count += 1.0;
}
if (((set[i] == Mode.LATCHA) || (set[i] == Mode.LATCHB)) || (set[i] == Mode.LATCHC)) {
if (set[i] != last_set) {
last_set = set[i];
glyph_count += 1.0;
}
}
if (i == 0) {
if (fset[i] == FMode.LATCHF) {
glyph_count += 2.0;
}
} else {
if ((fset[i] == FMode.LATCHF) && (fset[i - 1] != FMode.LATCHF)) {
glyph_count += 2.0;
}
if ((fset[i] != FMode.LATCHF) && (fset[i - 1] == FMode.LATCHF)) {
glyph_count += 2.0;
}
}
if (set[i] == Mode.LATCHC) {
if (inputData[i] == FNC1) {
glyph_count += 1.0;
} else {
glyph_count += 0.5;
}
} else {
glyph_count += 1.0;
}
}
if (glyph_count > 80.0) {
throw OkapiInputException.inputTooLong();
}
info("Encoding: ");
/* So now we know what start character to use - we can get on with it! */
if (readerInit) {
/* Reader Initialisation mode */
switch (set[0]) {
case LATCHA:
values[0] = 103;
current_set = Mode.LATCHA;
values[1] = 96;
bar_characters++;
info("STARTA FNC3 ");
break;
case LATCHB:
values[0] = 104;
current_set = Mode.LATCHB;
values[1] = 96;
bar_characters++;
info("STARTB FNC3 ");
break;
default: /* Start C */
values[0] = 104;
values[1] = 96;
values[2] = 99;
bar_characters += 2;
current_set = Mode.LATCHC;
info("STARTB FNC3 CODEC ");
break;
}
} else {
/* Normal mode */
switch (set[0]) {
case LATCHA:
values[0] = 103;
current_set = Mode.LATCHA;
info("STARTA ");
break;
case LATCHB:
values[0] = 104;
current_set = Mode.LATCHB;
info("STARTB ");
break;
default:
values[0] = 105;
current_set = Mode.LATCHC;
info("STARTC ");
break;
}
}
bar_characters++;
if (inputDataType == DataType.GS1) {
values[1] = 102;
bar_characters++;
info("FNC1 ");
}
if (fset[0] == FMode.LATCHF) {
switch (current_set) {
case LATCHA:
values[bar_characters] = 101;
values[bar_characters + 1] = 101;
info("FNC4 FNC4 ");
break;
case LATCHB:
values[bar_characters] = 100;
values[bar_characters + 1] = 100;
info("FNC4 FNC4 ");
break;
}
bar_characters += 2;
f_state = FMode.LATCHF;
}
/* Encode the data */
read = 0;
while (read < sourcelen) {
if (read != 0) {
if ((fset[read] == FMode.LATCHN) && (f_state == FMode.LATCHF)) {
/* Latch end of extended mode */
switch (current_set) {
case LATCHA:
values[bar_characters] = 101;
values[bar_characters + 1] = 101;
info("FNC4 FNC4 ");
break;
case LATCHB:
values[bar_characters] = 100;
values[bar_characters + 1] = 100;
info("FNC4 FNC4 ");
break;
}
bar_characters += 2;
f_state = FMode.LATCHN;
}
}
if ((read != 0) && (set[read] != current_set)) {
/* Latch different code set */
switch (set[read]) {
case LATCHA:
values[bar_characters] = 101;
bar_characters++;
current_set = Mode.LATCHA;
info("CODEA ");
break;
case LATCHB:
values[bar_characters] = 100;
bar_characters++;
current_set = Mode.LATCHB;
info("CODEB ");
break;
case LATCHC:
values[bar_characters] = 99;
bar_characters++;
current_set = Mode.LATCHC;
info("CODEC ");
break;
}
}
if (read != 0) {
if ((fset[read] == FMode.LATCHF) && (f_state == FMode.LATCHN)) {
/* Latch beginning of extended mode */
switch (current_set) {
case LATCHA:
values[bar_characters] = 101;
values[bar_characters + 1] = 101;
info("FNC4 FNC4 ");
break;
case LATCHB:
values[bar_characters] = 100;
values[bar_characters + 1] = 100;
info("FNC4 FNC4 ");
break;
}
bar_characters += 2;
f_state = FMode.LATCHF;
}
}
if ((fset[read] == FMode.SHIFTF && f_state != FMode.LATCHF) ||
(fset[read] == FMode.SHIFTN && f_state != FMode.LATCHN)) {
/* Shift to or from extended mode */
switch (current_set) {
case LATCHA:
values[bar_characters] = 101;
info("FNC4 ");
break;
case LATCHB:
values[bar_characters] = 100;
info("FNC4 ");
break;
}
bar_characters++;
}
if ((set[read] == Mode.SHIFTA && current_set != Mode.LATCHA) ||
(set[read] == Mode.SHIFTB && current_set != Mode.LATCHB)) {
/* Insert shift character */
values[bar_characters] = 98;
info("SHFT ");
bar_characters++;
}
/* Encode data characters */
c = inputData[read];
switch (set[read]) {
case SHIFTA:
case LATCHA:
if (c == FNC1) {
values[bar_characters] = 102;
info("FNC1 ");
} else if (c == FNC2) {
values[bar_characters] = 97;
info("FNC2 ");
} else if (c == FNC3) {
values[bar_characters] = 96;
info("FNC3 ");
} else if (c == FNC4) {
values[bar_characters] = 101;
info("FNC4 ");
} else if (c > 127) {
if (c < 160) {
values[bar_characters] = (c - 128) + 64;
} else {
values[bar_characters] = (c - 128) - 32;
}
infoSpace(values[bar_characters]);
} else {
if (c < 32) {
values[bar_characters] = c + 64;
} else {
values[bar_characters] = c - 32;
}
infoSpace(values[bar_characters]);
}
bar_characters++;
read++;
break;
case SHIFTB:
case LATCHB:
if (c == FNC1) {
values[bar_characters] = 102;
info("FNC1 ");
} else if (c == FNC2) {
values[bar_characters] = 97;
info("FNC2 ");
} else if (c == FNC3) {
values[bar_characters] = 96;
info("FNC3 ");
} else if (c == FNC4) {
values[bar_characters] = 100;
info("FNC4 ");
} else if (c > 127) {
values[bar_characters] = c - 32 - 128;
infoSpace(values[bar_characters]);
} else {
values[bar_characters] = c - 32;
infoSpace(values[bar_characters]);
}
bar_characters++;
read++;
break;
case LATCHC:
if (c == FNC1) {
values[bar_characters] = 102;
info("FNC1 ");
bar_characters++;
read++;
} else {
int d = inputData[read + 1];
int weight = (10 * (c - '0')) + (d - '0');
values[bar_characters] = weight;
infoSpace(values[bar_characters]);
bar_characters++;
read += 2;
}
break;
}
}
infoLine();
/* "...note that the linkage flag is an extra code set character between
the last data character and the Symbol Check Character" (GS1 Specification) */
/* Linkage flags in GS1-128 are determined by ISO/IEC 24723 section 7.4 */
switch (compositeMode) {
case CCA:
case CCB:
/* CC-A or CC-B 2D component */
switch(set[sourcelen - 1]) {
case LATCHA: linkage_flag = 100; break;
case LATCHB: linkage_flag = 99; break;
case LATCHC: linkage_flag = 101; break;
}
infoLine("Linkage Flag: " + linkage_flag);
break;
case CCC:
/* CC-C 2D component */
switch(set[sourcelen - 1]) {
case LATCHA: linkage_flag = 99; break;
case LATCHB: linkage_flag = 101; break;
case LATCHC: linkage_flag = 100; break;
}
infoLine("Linkage Flag: " + linkage_flag);
break;
default:
break;
}
if (linkage_flag != 0) {
values[bar_characters] = linkage_flag;
bar_characters++;
}
infoLine("Data Codewords: " + bar_characters);
/* Check digit calculation */
for (i = 0; i < bar_characters; i++) {
total_sum += (i == 0 ? values[i] : values[i] * i);
}
int checkDigit = total_sum % 103;
infoLine("Check Digit: " + checkDigit);
/* Build pattern string */
StringBuilder dest = new StringBuilder((6 * bar_characters) + 6 + 7);
for (i = 0; i < bar_characters; i++) {
dest.append(CODE128_TABLE[values[i]]);
}
dest.append(CODE128_TABLE[checkDigit]);
dest.append(CODE128_TABLE[106]); // stop character
/* Readable text */
if (inputDataType != DataType.GS1) {
readable = removeFncEscapeSequences(content);
if (inputDataType == DataType.HIBC) {
readable = "*" + readable + "*";
}
}
if (compositeMode == Composite.OFF) {
pattern = new String[] { dest.toString() };
row_height = new int[] { -1 };
row_count = 1;
} else {
/* Add the separator pattern for composite symbols */
pattern = new String[] { "0" + dest, dest.toString() };
row_height = new int[] { 1, -1 };
row_count = 2;
}
}
private static String removeFncEscapeSequences(String s) {
return s.replace(FNC1_STRING, "")
.replace(FNC2_STRING, "")
.replace(FNC3_STRING, "")
.replace(FNC4_STRING, "");
}
private void resolveOddCs(Mode[] set, int i, int cs, int nums, int fncs) {
if ((nums & 1) != 0) {
int index;
Mode m;
if (codeSet == CodeSet.C) {
// User wants to force the use of code set C only, but it's not possible
throw new OkapiInputException("Unable to encode the specified data using only code set C");
}
if (i - cs == 0 || fncs > 0) {
// Rule 2: first block -> swap last digit to A or B
index = i - 1;
if (index + 1 < set.length && set[index + 1] != null && set[index + 1] != Mode.LATCHC) {
// next block is either A or B -- match it
m = set[index + 1];
} else {
// next block is C, or there is no next block -- just latch to B
m = Mode.LATCHB;
}
} else {
// Rule 3b: subsequent block -> swap first digit to A or B
// Note that we make an exception for C blocks which contain one (or more) FNC1 characters,
// since swapping the first digit would place the FNC1 in an invalid position in the block
index = i - nums;
if (index - 1 >= 0 && set[index - 1] != null && set[index - 1] != Mode.LATCHC) {
// previous block is either A or B -- match it
m = set[index - 1];
} else {
// previous block is C, or there is no previous block -- just latch to B
m = Mode.LATCHB;
}
}
set[index] = m;
}
}
private Mode findSubset(int letter, int numbers) {
Mode mode;
if (letter == FNC1) {
if (numbers % 2 == 0) {
/* ISO 15417 Annex E Note 2 */
/* FNC1 may use subset C, so long as it doesn't break data into an odd number of digits */
mode = Mode.ABORC;
} else {
mode = Mode.AORB;
}
} else if (letter == FNC2 || letter == FNC3 || letter == FNC4) {
mode = Mode.AORB;
} else if (letter <= 31) {
mode = Mode.SHIFTA;
} else if ((letter >= 48) && (letter <= 57)) {
mode = Mode.ABORC;
} else if (letter <= 95) {
mode = Mode.AORB;
} else if (letter <= 127) {
mode = Mode.SHIFTB;
} else if (letter <= 159) {
mode = Mode.SHIFTA;
} else if (letter <= 223) {
mode = Mode.AORB;
} else {
mode = Mode.SHIFTB;
}
// if the user wishes to force the use of certain code sets, take that into account
if (codeSet == CodeSet.A) {
if (mode == Mode.ABORC || mode == Mode.AORB || mode == Mode.SHIFTA) {
mode = Mode.SHIFTA;
} else {
throw new OkapiInputException("Unable to encode the specified data using only code set A");
}
} else if (codeSet == CodeSet.B) {
if (mode == Mode.ABORC || mode == Mode.AORB || mode == Mode.SHIFTB) {
mode = Mode.SHIFTB;
} else {
throw new OkapiInputException("Unable to encode the specified data using only code set B");
}
} else if (codeSet == CodeSet.C) {
if (mode == Mode.ABORC) {
mode = Mode.SHIFTC;
} else {
throw new OkapiInputException("Unable to encode the specified data using only code set C");
}
} else if (codeSet == CodeSet.AB) {
if (mode == Mode.ABORC) {
mode = Mode.AORB;
}
}
return mode;
}
private int length(int letter, Mode mode) {
if (letter == FNC1 && mode == Mode.ABORC) {
/* ISO 15417 Annex E Note 2 */
/* Logical length used for making subset switching decisions, not actual length */
return 2;
} else {
return 1;
}
}
/** Implements rules from ISO 15417 Annex E. Returns the updated index point. */
private int reduceSubsetChanges(Mode[] mode_type, int[] mode_length, int index_point) {
int totalLength = 0;
int length;
Mode current, last, next, nextShift;
for (int i = 0; i < index_point; i++) {
current = mode_type[i];
length = mode_length[i];
if (i != 0) {
last = mode_type[i - 1];
} else {
last = Mode.NULL;
}
if (i != index_point - 1) {
next = mode_type[i + 1];
} else {
next = Mode.NULL;
}
/* The next shift mode is the location of the next fully-known code set. */
/* Everything between here and there will be either A/B/C or A/B. */
nextShift = Mode.NULL;
for (int j = i + 1; j < index_point; j++) {
if (mode_type[j] == Mode.SHIFTA || mode_type[j] == Mode.SHIFTB || mode_type[j] == Mode.SHIFTC) {
nextShift = mode_type[j];
break;
}
}
/* ISO 15417 Annex E Note 2 */
/* Calculate difference between logical length and actual length in this block */
int extraLength = 0;
for (int j = 0; j < length - extraLength; j++) {
if (length(inputData[totalLength + j], current) == 2) {
extraLength++;
}
}
if (i == 0) { /* first block */
if ((index_point == 1) && ((length == 2) && (current == Mode.ABORC))) { /* Rule 1a */
mode_type[i] = Mode.LATCHC;
current = Mode.LATCHC;
}
if (current == Mode.ABORC) {
if (length >= 4) { /* Rule 1b */
mode_type[i] = Mode.LATCHC;
current = Mode.LATCHC;
} else {
mode_type[i] = Mode.AORB;
current = Mode.AORB;
}
}
if (current == Mode.SHIFTA) { /* Rule 1c */
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if ((current == Mode.AORB) && (nextShift == Mode.SHIFTA)) { /* Rule 1c */
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if (current == Mode.AORB) { /* Rule 1d */
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
if (current == Mode.SHIFTC) { /* user forced use of code set C */
mode_type[i] = Mode.LATCHC;
current = Mode.LATCHC;
}
} else {
if ((current == Mode.ABORC) && (length >= 4)) { /* Rule 3 */
mode_type[i] = Mode.LATCHC;
current = Mode.LATCHC;
}
if (current == Mode.ABORC) {
mode_type[i] = Mode.AORB;
current = Mode.AORB;
}
if ((current == Mode.AORB) && (last == Mode.LATCHA)) {
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if ((current == Mode.AORB) && (last == Mode.LATCHB)) {
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
if ((current == Mode.AORB) && (nextShift == Mode.SHIFTA)) {
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if ((current == Mode.AORB) && (nextShift == Mode.SHIFTB)) {
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
if (current == Mode.AORB) {
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
if ((current == Mode.SHIFTA) && (length > 1)) { /* Rule 4 */
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if ((current == Mode.SHIFTB) && (length > 1)) { /* Rule 5 */
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
if ((current == Mode.SHIFTA) && (last == Mode.LATCHA)) {
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if ((current == Mode.SHIFTB) && (last == Mode.LATCHB)) {
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
if ((current == Mode.SHIFTA) && (next == Mode.AORB)) {
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if ((current == Mode.SHIFTB) && (next == Mode.AORB)) {
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
if ((current == Mode.SHIFTA) && (last == Mode.LATCHC)) {
mode_type[i] = Mode.LATCHA;
current = Mode.LATCHA;
}
if ((current == Mode.SHIFTB) && (last == Mode.LATCHC)) {
mode_type[i] = Mode.LATCHB;
current = Mode.LATCHB;
}
} /* Rule 2 is implemented elsewhere, Rule 6 is implied */
/* ISO 15417 Annex E Note 2 */
/* Convert logical length back to actual length for this block, now that we've decided on a subset */
mode_length[i] -= extraLength;
totalLength += mode_length[i];
}
return combineSubsetBlocks(mode_type, mode_length, index_point);
}
/** Modifies the specified mode and length arrays to combine adjacent modes of the same type, returning the updated index point. */
private int combineSubsetBlocks(Mode[] mode_type, int[] mode_length, int index_point) {
/* bring together same type blocks */
if (index_point > 1) {
for (int i = 1; i < index_point; i++) {
if (mode_type[i - 1] == mode_type[i]) {
/* bring together */
mode_length[i - 1] = mode_length[i - 1] + mode_length[i];
/* decrease the list */
for (int j = i + 1; j < index_point; j++) {
mode_length[j - 1] = mode_length[j];
mode_type[j - 1] = mode_type[j];
}
index_point--;
i--;
}
}
}
return index_point;
}
/** {@inheritDoc} */
@Override
protected int[] getCodewords() {
return getPatternAsCodewords(6);
}
}