-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathCodablockF.java
869 lines (800 loc) · 35.7 KB
/
CodablockF.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
/*
* 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 java.nio.charset.StandardCharsets;
import uk.org.okapibarcode.graphics.Rectangle;
/**
* <p>Implements Codablock F according to AIM Europe "Uniform Symbology Specification - Codablock F", 1995.
*
* <p>Codablock-F is a multi-row symbology using Code 128 encoding. It can encode any 8-bit ISO 8859-1 (Latin-1)
* data up to approximately 1000 alpha-numeric characters or 2000 numeric digits in length.
*
* @author <a href="mailto:[email protected]">Robin Stuart</a>
* @author Daniel Gredler
*/
public class CodablockF extends Symbol {
private enum Mode {
SHIFTA, LATCHA, SHIFTB, LATCHB, SHIFTC, LATCHC, AORB, ABORC, CANDB, CANDBB
}
private enum CfMode {
MODEA, MODEB, MODEC
}
/* Annex A Table A.1 */
private static final String[] C_128_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 int[][] blockmatrix = new int[44][62];
private int columns;
private int rows;
private CfMode finalMode;
private CfMode[] subsets = new CfMode[44];
/**
* Creates a new instance.
*/
public CodablockF() {
this.humanReadableLocation = HumanReadableLocation.NONE;
this.default_height = 15;
}
/**
* Codablock F is not currently listed in the GS1 specification as a GS1-supporting symbology. However,
* it did support EAN/UCC application identifiers before EAN and UCC became GS1, so it did historically
* support what eventually became the GS1 data format.
*/
@Override
public boolean supportsGs1() {
return true;
}
@Override
protected void encode() {
int input_length, i, j, k;
int min_module_height;
Mode last_mode, this_mode;
double estimate_codelength;
String row_pattern;
int[] row_indicator = new int[44];
int[] row_check = new int[44];
finalMode = CfMode.MODEA;
if (!content.matches("[\u0000-\u00FF]*")) {
throw OkapiInputException.invalidCharactersInInput();
}
inputData = toBytes(content, StandardCharsets.ISO_8859_1, 0x00);
input_length = inputData.length - 1;
if (input_length > 5450) {
throw OkapiInputException.inputTooLong();
}
// guess how many characters will be needed to encode the data,
// we need two characters right off the bat for the check digits
estimate_codelength = 2.0;
last_mode = Mode.AORB; // Codablock always starts with Code A
for (i = 0; i < input_length; i++) {
this_mode = findSubset(inputData[i]);
if (this_mode != last_mode) {
estimate_codelength += 1.0;
}
if (this_mode != Mode.ABORC) {
estimate_codelength += 1.0;
} else {
estimate_codelength += 0.5;
}
if (inputData[i] > 127) {
estimate_codelength += 1.0;
}
last_mode = this_mode;
}
/* Decide symbol size based on the above guess */
rows = (int) (0.5 + Math.sqrt(estimate_codelength / 1.45));
if (rows < 2) {
rows = 2;
}
if (rows > 44) {
rows = 44;
}
columns = (int) estimate_codelength / rows;
if (columns < 4) {
columns = 4;
}
if (columns > 62) {
throw OkapiInputException.inputTooLong();
}
/* Encode the data */
data_encode_blockf();
/* Add check digits - Annex F */
int k1 = 0;
int k2 = 0;
for (i = 0; i < input_length; i++) {
if (inputData[i] == FNC1) {
k1 += (i + 1) * 29; /* GS */
k2 += i * 29;
} else {
k1 += (i + 1) * inputData[i];
k2 += i * inputData[i];
}
}
k1 = k1 % 86;
k2 = k2 % 86;
k1 = toSymbolValue(k1, finalMode);
k2 = toSymbolValue(k2, finalMode);
blockmatrix[rows - 1][columns - 2] = k1;
blockmatrix[rows - 1][columns - 1] = k2;
/* Calculate row height (4.6.1.a) */
min_module_height = (int) (0.55 * (columns + 3)) + 3;
if (min_module_height < 8) {
min_module_height = 8;
}
/* Encode the Row Indicator in the First Row of the Symbol - Table D2 */
if (subsets[0] == CfMode.MODEC) {
/* Code C */
row_indicator[0] = rows - 2;
} else {
/* Code A or B */
row_indicator[0] = rows + 62;
if (row_indicator[0] > 95) {
row_indicator[0] -= 95;
}
}
/* Encode the Row Indicator in the Second and Subsequent Rows of the Symbol - Table D3 */
for (i = 1; i < rows; i++) {
/* Note that the second row is row number 1 because counting starts from 0 */
if (subsets[i] == CfMode.MODEC) {
/* Code C */
row_indicator[i] = i + 42;
} else {
/* Code A or B */
if (i < 6) {
row_indicator[i] = i + 10;
} else {
row_indicator[i] = i + 20;
}
}
}
/* Calculate row check digits - Annex E */
for (i = 0; i < rows; i++) {
k = 103;
switch (subsets[i]) {
case MODEA:
k += 98;
break;
case MODEB:
k += 100;
break;
case MODEC:
k += 99;
break;
}
k += 2 * row_indicator[i];
for (j = 0; j < columns; j++) {
k += (j + 3) * blockmatrix[i][j];
}
row_check[i] = k % 103;
}
readable = "";
row_count = rows;
pattern = new String[row_count];
row_height = new int[row_count];
infoLine("Grid Size: " + columns + " X " + rows);
infoLine("K1 Check Digit: " + k1);
infoLine("K2 Check Digit: " + k2);
/* Resolve the data into patterns and place in symbol structure */
info("Encoding: ");
for (i = 0; i < rows; i++) {
row_pattern = "";
/* Start character */
row_pattern += C_128_TABLE[103]; /* Always Start A */
switch (subsets[i]) {
case MODEA:
row_pattern += C_128_TABLE[98];
info("MODEA ");
break;
case MODEB:
row_pattern += C_128_TABLE[100];
info("MODEB ");
break;
case MODEC:
row_pattern += C_128_TABLE[99];
info("MODEC ");
break;
}
row_pattern += C_128_TABLE[row_indicator[i]];
infoSpace(row_indicator[i]);
for (j = 0; j < columns; j++) {
row_pattern += C_128_TABLE[blockmatrix[i][j]];
infoSpace(blockmatrix[i][j]);
}
row_pattern += C_128_TABLE[row_check[i]];
info("(" + row_check[i] + ") ");
/* Stop character */
row_pattern += C_128_TABLE[106];
/* Write the information into the symbol */
pattern[i] = row_pattern;
row_height[i] = default_height;
}
infoLine();
}
private static Mode findSubset(int letter) {
Mode mode;
if (letter == FNC1) {
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;
}
return mode;
}
/** Converts a data check character value to a symbol character value, per Table F.1. */
private static int toSymbolValue(int k, CfMode mode) {
if (mode == CfMode.MODEC) {
return k;
}
if (k < 32) {
return k + 64;
}
if (k < 48) {
return k - 32;
}
return k - 22;
}
private void data_encode_blockf() {
int i, j, input_position, current_row;
int column_position, c;
CfMode current_mode;
boolean done, exit_status;
int input_length = inputData.length - 1;
exit_status = false;
current_row = 0;
current_mode = CfMode.MODEA;
column_position = 0;
input_position = 0;
c = 0;
do {
done = false;
/* 'done' ensures that the instructions are followed in the correct order for each input character */
if (column_position == 0) {
/* The Beginning of a row */
c = columns;
current_mode = character_subset_select(input_position);
subsets[current_row] = current_mode;
if ((current_row == 0) && (inputDataType == DataType.GS1)) {
/* Section 4.4.7.1 */
blockmatrix[current_row][column_position] = 102; /* FNC1 */
column_position++;
c--;
}
}
if (inputData[input_position] == FNC1) {
blockmatrix[current_row][column_position] = 102; /* FNC1 */
column_position++;
c--;
input_position++;
done = true;
}
if (!done) {
if (c <= 2) {
/* Annex B section 1 rule 1 */
/* Ensure that there is sufficient encodation capacity to continue (using the rules of Annex B.2). */
switch (current_mode) {
case MODEA: /* Table B1 applies */
if (findSubset(inputData[input_position]) == Mode.ABORC) {
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
done = true;
}
if ((findSubset(inputData[input_position]) == Mode.SHIFTB) && (c == 1)) {
/* Needs two symbols */
blockmatrix[current_row][column_position] = 100; /* Code B */
column_position++;
c--;
done = true;
}
if ((inputData[input_position] >= 244) && (!done)) {
/* Needs three symbols */
blockmatrix[current_row][column_position] = 100; /* Code B */
column_position++;
c--;
if (c == 1) {
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
}
done = true;
}
if ((inputData[input_position] >= 128) && (!done) && c == 1) {
/* Needs two symbols */
blockmatrix[current_row][column_position] = 100; /* Code B */
column_position++;
c--;
done = true;
}
break;
case MODEB: /* Table B2 applies */
if (findSubset(inputData[input_position]) == Mode.ABORC) {
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
done = true;
}
if ((findSubset(inputData[input_position]) == Mode.SHIFTA) && (c == 1)) {
/* Needs two symbols */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
done = true;
}
if (((inputData[input_position] >= 128)
&& (inputData[input_position] <= 159)) && (!done)) {
/* Needs three symbols */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
if (c == 1) {
blockmatrix[current_row][column_position] = 100; /* Code B */
column_position++;
c--;
}
done = true;
}
if ((inputData[input_position] >= 160) && (!done) && c == 1) {
/* Needs two symbols */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
done = true;
}
break;
case MODEC: /* Table B3 applies */
if ((findSubset(inputData[input_position]) != Mode.ABORC) && (c == 1)) {
/* Needs two symbols */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
done = true;
}
if (((findSubset(inputData[input_position]) == Mode.ABORC)
&& (findSubset(inputData[input_position + 1]) != Mode.ABORC))
&& (c == 1)) {
/* Needs two symbols */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
done = true;
}
if (inputData[input_position] >= 128) {
/* Needs three symbols */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
if (c == 1) {
blockmatrix[current_row][column_position] = 100; /* Code B */
column_position++;
c--;
}
done = true;
}
break;
}
}
}
if (!done) {
if (((findSubset(inputData[input_position]) == Mode.AORB)
|| (findSubset(inputData[input_position]) == Mode.SHIFTA))
&& (current_mode == CfMode.MODEA)) {
/* Annex B section 1 rule 2 */
/* If in Code Subset A and the next data character can be encoded in Subset A encode the next
character. */
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 101; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
done = true;
}
}
if (!done) {
if (((findSubset(inputData[input_position]) == Mode.AORB)
|| (findSubset(inputData[input_position]) == Mode.SHIFTB))
&& (current_mode == CfMode.MODEB)) {
/* Annex B section 1 rule 3 */
/* If in Code Subset B and the next data character can be encoded in subset B, encode the next
character. */
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 100; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
done = true;
}
}
if (!done) {
if (((findSubset(inputData[input_position]) == Mode.ABORC)
&& (findSubset(inputData[input_position + 1]) == Mode.ABORC))
&& (current_mode == CfMode.MODEC)) {
/* Annex B section 1 rule 4 */
/* If in Code Subset C and the next data are 2 digits, encode them. */
blockmatrix[current_row][column_position]
= ((inputData[input_position] - '0') * 10)
+ (inputData[input_position + 1] - '0');
column_position++;
c--;
input_position += 2;
done = true;
}
}
if (!done) {
if (((current_mode == CfMode.MODEA) || (current_mode == CfMode.MODEB))
&& ((findSubset(inputData[input_position]) == Mode.ABORC)
|| (inputData[input_position] == FNC1))) {
// Count the number of numeric digits
// If 4 or more numeric data characters occur together when in subsets A or B:
// a. If there is an even number of numeric data characters, insert a Code C character before the
// first numeric digit to change to subset C.
// b. If there is an odd number of numeric data characters, insert a Code Set C character immediately
// after the first numeric digit to change to subset C.
i = 0;
j = 0;
do {
i++;
if (inputData[input_position + j] == FNC1) {
i++;
}
j++;
} while ((findSubset(inputData[input_position + j]) == Mode.ABORC)
|| ((inputData[input_position + j] == FNC1) && i % 2 == 0));
i--;
if (i >= 4) {
/* Annex B section 1 rule 5 */
if (i % 2 == 1) {
/* Annex B section 1 rule 5a */
blockmatrix[current_row][column_position] = 99; /* Code C */
column_position++;
c--;
blockmatrix[current_row][column_position] =
((inputData[input_position] - '0') * 10) +
(inputData[input_position + 1] - '0');
column_position++;
c--;
input_position += 2;
current_mode = CfMode.MODEC;
} else {
/* Annex B section 1 rule 5b */
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
}
done = true;
} else {
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
done = true;
}
}
}
if (!done) {
if ((current_mode == CfMode.MODEB) && (findSubset(inputData[input_position]) == Mode.SHIFTA)) {
/* Annex B section 1 rule 6 */
/* When in subset B and an ASCII control character occurs in the data:
a. If there is a lower case character immediately following the control character, insert a Shift
character before the control character.
b. Otherwise, insert a Code A character before the control character to change to subset A. */
if ((inputData[input_position + 1] >= 96) && (inputData[input_position + 1] <= 127)) {
/* Annex B section 1 rule 6a */
blockmatrix[current_row][column_position] = 98; /* Shift */
column_position++;
c--;
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 101; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
} else {
/* Annex B section 1 rule 6b */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 101; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
current_mode = CfMode.MODEA;
}
done = true;
}
}
if (!done) {
if ((current_mode == CfMode.MODEA) && (findSubset(inputData[input_position]) == Mode.SHIFTB)) {
/* Annex B section 1 rule 7 */
/* When in subset A and a lower case character occurs in the data:
a. If following that character, a control character occurs in the data before the occurrence of
another lower case character, insert a Shift character before the lower case character.
b. Otherwise, insert a Code B character before the lower case character to change to subset B. */
if (input_position + 2 < inputData.length
&& (findSubset(inputData[input_position + 1]) == Mode.SHIFTA)
&& (findSubset(inputData[input_position + 2]) == Mode.SHIFTB)) {
/* Annex B section 1 rule 7a */
blockmatrix[current_row][column_position] = 98; /* Shift */
column_position++;
c--;
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 100; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
} else {
/* Annex B section 1 rule 7b */
blockmatrix[current_row][column_position] = 100; /* Code B */
column_position++;
c--;
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 100; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
current_mode = CfMode.MODEB;
}
done = true;
}
}
if (!done) {
if ((current_mode == CfMode.MODEC) && ((findSubset(inputData[input_position]) != Mode.ABORC)
|| (findSubset(inputData[input_position + 1]) != Mode.ABORC))) {
/* Annex B section 1 rule 8 */
/* When in subset C and a non-numeric character (or a single digit) occurs in the data, insert a Code
A or Code B character before that character, following rules 8a and 8b to determine between code
subsets A and B.
a. If an ASCII control character (eg NUL) occurs in the data before any lower case character, use
Code A.
b. Otherwise use Code B. */
if (findSubset(inputData[input_position]) == Mode.SHIFTA) {
/* Annex B section 1 rule 8a */
blockmatrix[current_row][column_position] = 101; /* Code A */
column_position++;
c--;
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 101; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
current_mode = CfMode.MODEA;
} else {
/* Annex B section 1 rule 8b */
blockmatrix[current_row][column_position] = 100; /* Code B */
column_position++;
c--;
if (inputData[input_position] >= 128) {
/* Extended ASCII character */
blockmatrix[current_row][column_position] = 100; /* FNC4 */
column_position++;
c--;
}
blockmatrix[current_row][column_position] = a3_convert(inputData[input_position]);
column_position++;
c--;
input_position++;
current_mode = CfMode.MODEB;
}
done = true;
}
}
if (input_position == input_length) {
/* End of data - Annex B rule 5a */
if (c == 1) {
if (current_mode == CfMode.MODEA) {
blockmatrix[current_row][column_position] = 100; /* Code B */
current_mode = CfMode.MODEB;
} else {
blockmatrix[current_row][column_position] = 101; /* Code A */
current_mode = CfMode.MODEA;
}
column_position++;
c--;
}
if (c == 0) {
/* Another row is needed */
column_position = 0;
c = columns;
current_row++;
subsets[current_row] = CfMode.MODEA;
current_mode = CfMode.MODEA;
}
if (c > 2) {
/* Fill up the last row */
do {
if (current_mode == CfMode.MODEA) {
blockmatrix[current_row][column_position] = 100; /* Code B */
current_mode = CfMode.MODEB;
} else {
blockmatrix[current_row][column_position] = 101; /* Code A */
current_mode = CfMode.MODEA;
}
column_position++;
c--;
} while (c > 2);
}
/* If (c == 2) { do nothing } */
exit_status = true;
finalMode = current_mode;
} else {
if (c <= 0) {
/* Start new row - Annex B rule 5b */
column_position = 0;
current_row++;
if (current_row > 43) {
throw new OkapiInputException("Too many rows.");
}
}
}
} while (!exit_status);
if (current_row == 0) {
/* fill up the first row */
for(c = column_position; c <= columns; c++) {
if(current_mode == CfMode.MODEA) {
blockmatrix[current_row][c] = 100; /* Code B */
current_mode = CfMode.MODEB;
} else {
blockmatrix[current_row][c] = 101; /* Code A */
current_mode = CfMode.MODEA;
}
}
current_row++;
/* add a second row */
subsets[current_row] = CfMode.MODEA;
current_mode = CfMode.MODEA;
for(c = 0; c <= columns - 2; c++) {
if(current_mode == CfMode.MODEA) {
blockmatrix[current_row][c] = 100; /* Code B */
current_mode = CfMode.MODEB;
} else {
blockmatrix[current_row][c] = 101; /* Code A */
current_mode = CfMode.MODEA;
}
}
}
rows = current_row + 1;
}
private CfMode character_subset_select(int input_position) {
/* Section 4.5.2 - Determining the Character Subset Selector in a Row */
if((inputData[input_position] >= '0') && (inputData[input_position] <= '9')) {
/* Rule 1 */
return CfMode.MODEC;
}
if((inputData[input_position] >= 128) && (inputData[input_position] <= 160)) {
/* Rule 2 (i) */
return CfMode.MODEA;
}
if((inputData[input_position] >= 0) && (inputData[input_position] <= 31)) {
/* Rule 3 */
return CfMode.MODEA;
}
/* Rule 4 */
return CfMode.MODEB;
}
private static int a3_convert(int source) {
/* Annex A section 3 */
if (source < 32) {
return source + 64;
} else if (source <= 127) {
return source - 32;
} else if (source <= 159) {
return (source - 128) + 64;
} else {
return (source - 128) - 32; // source >= 160
}
}
@Override
protected void plotSymbol() {
int xBlock, yBlock;
int x, y, w, h;
boolean black;
resetPlotElements();
y = 1;
h = 1;
for (yBlock = 0; yBlock < row_count; yBlock++) {
black = true;
x = 0;
for (xBlock = 0; xBlock < pattern[yBlock].length(); xBlock++) {
char c = pattern[yBlock].charAt(xBlock);
w = (c - '0') * moduleWidth;
if (black) {
h = row_height[yBlock];
if (w != 0 && h != 0) {
Rectangle rect = new Rectangle(x, y, w, h);
rectangles.add(rect);
}
if ((x + w) > symbol_width) {
symbol_width = x + w;
}
}
black = !black;
x += w;
}
y += h;
if (y > symbol_height) {
symbol_height = y;
}
/* Add bars between rows */
if (yBlock != (row_count - 1)) {
Rectangle rect = new Rectangle(11 * moduleWidth, y - 1, symbol_width - (24 * moduleWidth), 2);
rectangles.add(rect);
}
}
/* Add top and bottom binding bars */
rectangles.add(new Rectangle(0, 0, symbol_width, 2)); // top
rectangles.add(new Rectangle(0, y - 1, symbol_width, 2)); // bottom
symbol_height = (rows * default_height) + 2; // one extra line above, one below
}
}