-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathGridMatrix.java
1912 lines (1721 loc) · 65.8 KB
/
GridMatrix.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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.util.Arrays.positionOf;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
/**
* <p>Implements Grid Matrix bar code symbology according to AIMD014.
*
* <p>Grid Matrix is a matrix symbology which can encode characters in the ISO/IEC 8859-1 (Latin-1)
* character set as well as those in the GB-2312 character set. Input is assumed to be formatted as
* a UTF string.
*
* @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
*/
public class GridMatrix extends Symbol {
private static final char[] SHIFT_SET = {
/* From Table 7 - Encoding of control characters */
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* NULL -> SI */
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* DLE -> US */
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':',
';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'
};
private static final int[] GM_RECOMMEND_CW = {
9, 30, 59, 114, 170, 237, 315, 405, 506, 618, 741, 875, 1021
};
private static final int[] GM_MAX_CW = {
11, 40, 79, 146, 218, 305, 405, 521, 650, 794, 953, 1125, 1313
};
private static final int[] GM_DATA_CODEWORDS = {
0, 15, 13, 11, 9,
45, 40, 35, 30, 25,
89, 79, 69, 59, 49,
146, 130, 114, 98, 81,
218, 194, 170, 146, 121,
305, 271, 237, 203, 169,
405, 360, 315, 270, 225,
521, 463, 405, 347, 289,
650, 578, 506, 434, 361,
794, 706, 618, 530, 441,
953, 847, 741, 635, 529,
1125, 1000, 875, 750, 625,
1313, 1167, 1021, 875, 729
};
private static final int[] GM_N1 = {
18, 50, 98, 81, 121, 113, 113, 116, 121, 126, 118, 125, 122
};
private static final int[] GM_B1 = {
1, 1, 1, 2, 2, 2, 2, 3, 2, 7, 5, 10, 6
};
private static final int[] GM_B2 = {
0, 0, 0, 0, 0, 1, 2, 2, 4, 0, 4, 0, 6
};
private static final int[] GM_EBEB = {
/* E1 B3 E2 B4 */
0, 0, 0, 0, // version 1
3, 1, 0, 0,
5, 1, 0, 0,
7, 1, 0, 0,
9, 1, 0, 0,
5, 1, 0, 0, // version 2
10, 1, 0, 0,
15, 1, 0, 0,
20, 1, 0, 0,
25, 1, 0, 0,
9, 1, 0, 0, // version 3
19, 1, 0, 0,
29, 1, 0, 0,
39, 1, 0, 0,
49, 1, 0, 0,
8, 2, 0, 0, // version 4
16, 2, 0, 0,
24, 2, 0, 0,
32, 2, 0, 0,
41, 1, 10, 1,
12, 2, 0, 0, // version 5
24, 2, 0, 0,
36, 2, 0, 0,
48, 2, 0, 0,
61, 1, 60, 1,
11, 3, 0, 0, // version 6
23, 1, 22, 2,
34, 2, 33, 1,
45, 3, 0, 0,
57, 1, 56, 2,
12, 1, 11, 3, // version 7
23, 2, 22, 2,
34, 3, 33, 1,
45, 4, 0, 0,
57, 1, 56, 3,
12, 2, 11, 3, // version 8
23, 5, 0, 0,
35, 3, 34, 2,
47, 1, 46, 4,
58, 4, 57, 1,
12, 6, 0, 0, // version 9
24, 6, 0, 0,
36, 6, 0, 0,
48, 6, 0, 0,
61, 1, 60, 5,
13, 4, 12, 3, // version 10
26, 1, 25, 6,
38, 5, 37, 2,
51, 2, 50, 5,
63, 7, 0, 0,
12, 6, 11, 3, // version 11
24, 4, 23, 5,
36, 2, 35, 7,
47, 9, 0, 0,
59, 7, 58, 2,
13, 5, 12, 5, // version 12
25, 10, 0, 0,
38, 5, 37, 5,
50, 10, 0, 0,
63, 5, 62, 5,
13, 1, 12, 11, //version 13
25, 3, 24, 9,
37, 5, 36, 7,
49, 7, 48, 5,
61, 9, 60, 3
};
private static final int[] GM_MACRO_MATRIX = {
728, 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,
727, 624, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 651,
726, 623, 528, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 553, 652,
725, 622, 527, 440, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 463, 554, 653,
724, 621, 526, 439, 360, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 381, 464, 555, 654,
723, 620, 525, 438, 359, 288, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 307, 382, 465, 556, 655,
722, 619, 524, 437, 358, 287, 224, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 241, 308, 383, 466, 557, 656,
721, 618, 523, 436, 357, 286, 223, 168, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 183, 242, 309, 384, 467, 558, 657,
720, 617, 522, 435, 356, 285, 222, 167, 120, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 133, 184, 243, 310, 385, 468, 559, 658,
719, 616, 521, 434, 355, 284, 221, 166, 119, 80, 49, 50, 51, 52, 53, 54, 55, 56, 91, 134, 185, 244, 311, 386, 469, 560, 659,
718, 615, 520, 433, 354, 283, 220, 165, 118, 79, 48, 25, 26, 27, 28, 29, 30, 57, 92, 135, 186, 245, 312, 387, 470, 561, 660,
717, 614, 519, 432, 353, 282, 219, 164, 117, 78, 47, 24, 9, 10, 11, 12, 31, 58, 93, 136, 187, 246, 313, 388, 471, 562, 661,
716, 613, 518, 431, 352, 281, 218, 163, 116, 77, 46, 23, 8, 1, 2, 13, 32, 59, 94, 137, 188, 247, 314, 389, 472, 563, 662,
715, 612, 517, 430, 351, 280, 217, 162, 115, 76, 45, 22, 7, 0, 3, 14, 33, 60, 95, 138, 189, 248, 315, 390, 473, 564, 663,
714, 611, 516, 429, 350, 279, 216, 161, 114, 75, 44, 21, 6, 5, 4, 15, 34, 61, 96, 139, 190, 249, 316, 391, 474, 565, 664,
713, 610, 515, 428, 349, 278, 215, 160, 113, 74, 43, 20, 19, 18, 17, 16, 35, 62, 97, 140, 191, 250, 317, 392, 475, 566, 665,
712, 609, 514, 427, 348, 277, 214, 159, 112, 73, 42, 41, 40, 39, 38, 37, 36, 63, 98, 141, 192, 251, 318, 393, 476, 567, 666,
711, 608, 513, 426, 347, 276, 213, 158, 111, 72, 71, 70, 69, 68, 67, 66, 65, 64, 99, 142, 193, 252, 319, 394, 477, 568, 667,
710, 607, 512, 425, 346, 275, 212, 157, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 143, 194, 253, 320, 395, 478, 569, 668,
709, 606, 511, 424, 345, 274, 211, 156, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144, 195, 254, 321, 396, 479, 570, 669,
708, 605, 510, 423, 344, 273, 210, 209, 208, 207, 206, 205, 204, 203, 202, 201, 200, 199, 198, 197, 196, 255, 322, 397, 480, 571, 670,
707, 604, 509, 422, 343, 272, 271, 270, 269, 268, 267, 266, 265, 264, 263, 262, 261, 260, 259, 258, 257, 256, 323, 398, 481, 572, 671,
706, 603, 508, 421, 342, 341, 340, 339, 338, 337, 336, 335, 334, 333, 332, 331, 330, 329, 328, 327, 326, 325, 324, 399, 482, 573, 672,
705, 602, 507, 420, 419, 418, 417, 416, 415, 414, 413, 412, 411, 410, 409, 408, 407, 406, 405, 404, 403, 402, 401, 400, 483, 574, 673,
704, 601, 506, 505, 504, 503, 502, 501, 500, 499, 498, 497, 496, 495, 494, 493, 492, 491, 490, 489, 488, 487, 486, 485, 484, 575, 674,
703, 600, 599, 598, 597, 596, 595, 594, 593, 592, 591, 590, 589, 588, 587, 586, 585, 584, 583, 582, 581, 580, 579, 578, 577, 576, 675,
702, 701, 700, 699, 698, 697, 696, 695, 694, 693, 692, 691, 690, 689, 688, 687, 686, 685, 684, 683, 682, 681, 680, 679, 678, 677, 676
};
private static final char[] MIXED_ALPHANUM_SET = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', ' '
};
private enum Mode {
NULL, GM_NUMBER, GM_LOWER, GM_UPPER, GM_MIXED, GM_CONTROL, GM_BYTE, GM_CHINESE
}
private StringBuilder binary;
private int[] word = new int[1460];
private boolean[] grid;
private Mode appxDnextSection = Mode.NULL;
private Mode appxDlastSection = Mode.NULL;
private int preferredVersion = 0;
private int preferredEccLevel = -1;
/**
* Creates a new instance.
*/
public GridMatrix() {
this.humanReadableLocation = HumanReadableLocation.NONE;
}
/**
* Set preferred size, or "version" of the symbol according to the following
* table. This value may be ignored if the data to be encoded does not fit
* into a symbol of the selected size.
*
* <table>
* <tbody>
* <tr><th>Input</th><th>Size </th></tr>
* <tr><td>1 </td><td>18 x 18 </td></tr>
* <tr><td>2 </td><td>30 x 30 </td></tr>
* <tr><td>3 </td><td>42 x 42 </td></tr>
* <tr><td>4 </td><td>54 x 54 </td></tr>
* <tr><td>5 </td><td>66 x 66 </td></tr>
* <tr><td>6 </td><td>78 x 78 </td></tr>
* <tr><td>7 </td><td>90 x 90 </td></tr>
* <tr><td>8 </td><td>102 x 102</td></tr>
* <tr><td>9 </td><td>114 x 114</td></tr>
* <tr><td>10 </td><td>126 x 126</td></tr>
* <tr><td>11 </td><td>138 x 138</td></tr>
* <tr><td>12 </td><td>150 x 150</td></tr>
* <tr><td>13 </td><td>162 x 162</td></tr>
* </tbody>
* </table>
*
* @param version symbol version
*/
public void setPreferredVersion(int version) {
preferredVersion = version;
}
/**
* Set the preferred amount of the symbol which should be dedicated to error
* correction data. Values should be selected from the following table:
*
* <table>
* <tbody>
* <tr><th>Mode</th><th>Error Correction Capacity</th></tr>
* <tr><td>1 </td><td>Approximately 10% </td></tr>
* <tr><td>2 </td><td>Approximately 20% </td></tr>
* <tr><td>3 </td><td>Approximately 30% </td></tr>
* <tr><td>4 </td><td>Approximately 40% </td></tr>
* <tr><td>5 </td><td>Approximately 50% </td></tr>
* </tbody>
* </table>
*
* @param eccLevel error correction level
*/
public void setPreferredEccLevel(int eccLevel) {
preferredEccLevel = eccLevel;
}
@Override
public boolean supportsEci() {
return true;
}
@Override
protected void encode() {
int size, modules, dark, error_number;
int auto_layers, min_layers, layers, auto_ecc_level, min_ecc_level, ecc_level;
int x, y, i;
int data_cw, input_latch = 0;
int data_max;
int length;
StringBuilder bin = new StringBuilder();
for (i = 0; i < 1460; i++) {
word[i] = 0;
}
try {
Charset gb2312 = Charset.forName("GB2312");
if (gb2312.newEncoder().canEncode(content)) {
/* GB2312 will work, use Chinese compaction */
byte[] inputBytes = content.getBytes(gb2312);
inputData = new int[inputBytes.length];
length = 0;
for (i = 0; i < inputBytes.length; i++) {
if (((inputBytes[i] & 0xFF) >= 0xA1) && ((inputBytes[i] & 0xFF) <= 0xF7)) {
/* Double byte character */
inputData[length] = ((inputBytes[i] & 0xFF) * 256) + (inputBytes[i + 1] & 0xFF);
i++;
length++;
} else {
/* Single byte character */
inputData[length] = inputBytes[i] & 0xFF;
length++;
}
}
infoLine("Using GB2312 character encoding");
eciMode = 29;
} else {
/* GB2312 encoding won't work, use other ECI mode */
eciProcess(); // Get ECI mode
length = inputData.length;
}
} catch (UnsupportedCharsetException e) {
throw new OkapiInternalException("Byte conversion encoding error");
}
error_number = encodeGridMatrixBinary(length, readerInit);
if (error_number != 0) {
throw OkapiInputException.inputTooLong();
}
/* Determine the size of the symbol */
data_cw = binary.length() / 7;
auto_layers = 1;
for (i = 0; i < 13; i++) {
if (GM_RECOMMEND_CW[i] < data_cw) {
auto_layers = i + 1;
}
}
min_layers = 13;
for (i = 12; i > 0; i--) {
if (GM_MAX_CW[(i - 1)] >= data_cw) {
min_layers = i;
}
}
layers = auto_layers;
auto_ecc_level = 3;
if (layers == 1) {
auto_ecc_level = 5;
}
if ((layers == 2) || (layers == 3)) {
auto_ecc_level = 4;
}
min_ecc_level = 1;
if (layers == 1) {
min_ecc_level = 4;
}
if ((layers == 2) || (layers == 3)) {
min_ecc_level = 2;
}
ecc_level = auto_ecc_level;
if ((preferredVersion >= 1) && (preferredVersion <= 13)) {
input_latch = 1;
if (preferredVersion > min_layers) {
layers = preferredVersion;
} else {
layers = min_layers;
}
}
if (input_latch == 1) {
auto_ecc_level = 3;
if (layers == 1) {
auto_ecc_level = 5;
}
if ((layers == 2) || (layers == 3)) {
auto_ecc_level = 4;
}
ecc_level = auto_ecc_level;
if (data_cw > GM_DATA_CODEWORDS[(5 * (layers - 1)) + (ecc_level - 1)]) {
layers++;
}
}
if (input_latch == 0) {
if ((preferredEccLevel >= 1) && (preferredEccLevel <= 5)) {
if (preferredEccLevel > min_ecc_level) {
ecc_level = preferredEccLevel;
} else {
ecc_level = min_ecc_level;
}
}
if (data_cw > GM_DATA_CODEWORDS[(5 * (layers - 1)) + (ecc_level - 1)]) {
do {
layers++;
} while ((data_cw > GM_DATA_CODEWORDS[(5 * (layers - 1)) + (ecc_level - 1)]) && (layers <= 13));
}
}
data_max = 1313;
switch (ecc_level) {
case 2:
data_max = 1167;
break;
case 3:
data_max = 1021;
break;
case 4:
data_max = 875;
break;
case 5:
data_max = 729;
break;
}
if (data_cw > data_max) {
throw OkapiInputException.inputTooLong();
}
addErrorCorrection(data_cw, layers, ecc_level);
size = 6 + (layers * 12);
modules = 1 + (layers * 2);
infoLine("Layers: " + layers);
infoLine("ECC Level: " + ecc_level);
infoLine("Data Codewords: " + data_cw);
infoLine("ECC Codewords: " + GM_DATA_CODEWORDS[((layers - 1) * 5) + (ecc_level - 1)]);
infoLine("Grid Size: " + modules + " X " + modules);
grid = new boolean[size * size];
for (x = 0; x < size; x++) {
for (y = 0; y < size; y++) {
grid[(y * size) + x] = false;
}
}
placeDataInGrid(modules, size);
addLayerId(size, layers, modules, ecc_level);
/* Add macromodule frames */
for (x = 0; x < modules; x++) {
dark = 1 - (x & 1);
for (y = 0; y < modules; y++) {
if (dark == 1) {
for (i = 0; i < 5; i++) {
grid[((y * 6) * size) + (x * 6) + i] = true;
grid[(((y * 6) + 5) * size) + (x * 6) + i] = true;
grid[(((y * 6) + i) * size) + (x * 6)] = true;
grid[(((y * 6) + i) * size) + (x * 6) + 5] = true;
}
grid[(((y * 6) + 5) * size) + (x * 6) + 5] = true;
dark = 0;
} else {
dark = 1;
}
}
}
/* Copy values to symbol */
symbol_width = size;
row_count = size;
row_height = new int[row_count];
pattern = new String[row_count];
for (x = 0; x < size; x++) {
bin.setLength(0);
for (y = 0; y < size; y++) {
if (grid[(x * size) + y]) {
bin.append('1');
} else {
bin.append('0');
}
}
row_height[x] = moduleWidth;
pattern[x] = bin2pat(bin);
}
}
private int encodeGridMatrixBinary(int length, boolean reader) {
/* Create a binary stream representation of the input data.
7 sets are defined - Chinese characters, Numerals, Lower case letters, Upper case letters,
Mixed numerals and letters, Control characters and 8-bit binary data */
int sp, glyph = 0;
Mode current_mode, next_mode, last_mode;
int c1, c2;
boolean done;
int p = 0, ppos;
int punt = 0;
int number_pad_posn;
int byte_count_posn = 0, byte_count = 0;
int shift, i;
int[] numbuf = new int[3];
Mode[] modeMap = calculateModeMap(length);
binary = new StringBuilder();
sp = 0;
current_mode = Mode.NULL;
number_pad_posn = 0;
info("Encoding: ");
if (reader) {
binary.append("1010"); /* FNC3 - Reader Initialisation */
info("INIT ");
}
if ((eciMode != 3) && (eciMode != 29)) {
binary.append("1100"); /* ECI */
if ((eciMode >= 0) && (eciMode <= 1023)) {
binary.append('0');
for (i = 0x200; i > 0; i = i >> 1) {
if ((eciMode & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
}
if ((eciMode >= 1024) && (eciMode <= 32767)) {
binary.append("10");
for (i = 0x4000; i > 0; i = i >> 1) {
if ((eciMode & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
}
if ((eciMode >= 32768) && (eciMode <= 811799)) {
binary.append("11");
for (i = 0x80000; i > 0; i = i >> 1) {
if ((eciMode & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
}
info("ECI ");
infoSpace(eciMode);
}
do {
next_mode = modeMap[sp];
if (next_mode != current_mode) {
switch (current_mode) {
case NULL:
switch (next_mode) {
case GM_CHINESE:
binary.append("0001");
break;
case GM_NUMBER:
binary.append("0010");
break;
case GM_LOWER:
binary.append("0011");
break;
case GM_UPPER:
binary.append("0100");
break;
case GM_MIXED:
binary.append("0101");
break;
case GM_BYTE:
binary.append("0111");
break;
}
break;
case GM_CHINESE:
switch (next_mode) {
case GM_NUMBER:
binary.append("1111111100001");
break; // 8161
case GM_LOWER:
binary.append("1111111100010");
break; // 8162
case GM_UPPER:
binary.append("1111111100011");
break; // 8163
case GM_MIXED:
binary.append("1111111100100");
break; // 8164
case GM_BYTE:
binary.append("1111111100101");
break; // 8165
}
break;
case GM_NUMBER:
/* add numeric block padding value */
switch (p) {
case 1:
binary.insert(number_pad_posn, "10");
break; // 2 pad digits
case 2:
binary.insert(number_pad_posn, "01");
break; // 1 pad digit
case 3:
binary.insert(number_pad_posn, "00");
break; // 0 pad digits
}
switch (next_mode) {
case GM_CHINESE:
binary.append("1111111011");
break; // 1019
case GM_LOWER:
binary.append("1111111100");
break; // 1020
case GM_UPPER:
binary.append("1111111101");
break; // 1021
case GM_MIXED:
binary.append("1111111110");
break; // 1022
case GM_BYTE:
binary.append("1111111111");
break; // 1023
}
break;
case GM_LOWER:
case GM_UPPER:
switch (next_mode) {
case GM_CHINESE:
binary.append("11100");
break; // 28
case GM_NUMBER:
binary.append("11101");
break; // 29
case GM_LOWER:
case GM_UPPER:
binary.append("11110");
break; // 30
case GM_MIXED:
binary.append("1111100");
break; // 124
case GM_BYTE:
binary.append("1111110");
break; // 126
}
break;
case GM_MIXED:
switch (next_mode) {
case GM_CHINESE:
binary.append("1111110001");
break; // 1009
case GM_NUMBER:
binary.append("1111110010");
break; // 1010
case GM_LOWER:
binary.append("1111110011");
break; // 1011
case GM_UPPER:
binary.append("1111110100");
break; // 1012
case GM_BYTE:
binary.append("1111110111");
break; // 1015
}
break;
case GM_BYTE:
/* add byte block length indicator */
addByteCount(byte_count_posn, byte_count);
byte_count = 0;
switch (next_mode) {
case GM_CHINESE:
binary.append("0001");
break; // 1
case GM_NUMBER:
binary.append("0010");
break; // 2
case GM_LOWER:
binary.append("0011");
break; // 3
case GM_UPPER:
binary.append("0100");
break; // 4
case GM_MIXED:
binary.append("0101");
break; // 5
}
break;
}
switch (next_mode) {
case GM_CHINESE:
info("CHIN ");
break;
case GM_NUMBER:
info("NUMB ");
break;
case GM_LOWER:
info("LOWR ");
break;
case GM_UPPER:
info("UPPR ");
break;
case GM_MIXED:
info("MIXD ");
break;
case GM_BYTE:
info("BYTE ");
break;
}
}
last_mode = current_mode;
current_mode = next_mode;
switch (current_mode) {
case GM_CHINESE:
done = false;
if (inputData[sp] > 0xff) {
/* GB2312 character */
c1 = (inputData[sp] & 0xff00) >> 8;
c2 = inputData[sp] & 0xff;
if ((c1 >= 0xa0) && (c1 <= 0xa9)) {
glyph = (0x60 * (c1 - 0xa1)) + (c2 - 0xa0);
}
if ((c1 >= 0xb0) && (c1 <= 0xf7)) {
glyph = (0x60 * (c1 - 0xb0 + 9)) + (c2 - 0xa0);
}
done = true;
}
if (!(done)) {
if (sp != (length - 1)) {
if ((inputData[sp] == 13) && (inputData[sp + 1] == 10)) {
/* End of Line */
glyph = 7776;
sp++;
}
done = true;
}
}
if (!(done)) {
if (sp != (length - 1)) {
if (((inputData[sp] >= '0') && (inputData[sp] <= '9')) && ((inputData[sp + 1] >= '0') && (inputData[sp + 1] <= '9'))) {
/* Two digits */
glyph = 8033 + (10 * (inputData[sp] - '0')) + (inputData[sp + 1] - '0');
sp++;
}
}
}
if (!(done)) {
/* Byte value */
glyph = 7777 + inputData[sp];
}
infoSpace(glyph);
for (i = 0x1000; i > 0; i = i >> 1) {
if ((glyph & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
sp++;
break;
case GM_NUMBER:
if (last_mode != current_mode) {
/* Reserve a space for numeric digit padding value (2 bits) */
number_pad_posn = binary.length();
}
p = 0;
ppos = -1;
/* Numeric compression can also include certain combinations of
non-numeric character */
numbuf[0] = '0';
numbuf[1] = '0';
numbuf[2] = '0';
do {
if ((inputData[sp] >= '0') && (inputData[sp] <= '9')) {
numbuf[p] = inputData[sp];
p++;
}
switch (inputData[sp]) {
case ' ':
case '+':
case '-':
case '.':
case ',':
punt = inputData[sp];
ppos = p;
break;
}
if (sp < (length - 1)) {
if ((inputData[sp] == 13) && (inputData[sp + 1] == 10)) {
/* <end of line> */
punt = inputData[sp];
sp++;
ppos = p;
}
}
sp++;
} while ((p < 3) && (sp < length));
if (ppos != -1) {
switch (punt) {
case ' ':
glyph = 0;
break;
case '+':
glyph = 3;
break;
case '-':
glyph = 6;
break;
case '.':
glyph = 9;
break;
case ',':
glyph = 12;
break;
case 0x13:
glyph = 15;
break;
}
glyph += ppos;
glyph += 1000;
infoSpace(glyph);
for (i = 0x200; i > 0; i = i >> 1) {
if ((glyph & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
}
glyph = (100 * (numbuf[0] - '0')) + (10 * (numbuf[1] - '0')) + (numbuf[2] - '0');
infoSpace(glyph);
for (i = 0x200; i > 0; i = i >> 1) {
if ((glyph & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
break;
case GM_BYTE:
if (last_mode != current_mode) {
/* Reserve space for byte block length indicator (9 bits) */
byte_count_posn = binary.length();
}
if (byte_count == 512) {
/* Maximum byte block size is 512 bytes. If longer is needed then start a new block */
addByteCount(byte_count_posn, byte_count);
binary.append("0111");
byte_count_posn = binary.length();
byte_count = 0;
}
glyph = inputData[sp];
infoSpace(glyph);
for (i = 0x80; i > 0; i = i >> 1) {
if ((glyph & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
sp++;
byte_count++;
break;
case GM_MIXED:
shift = 1;
if ((inputData[sp] >= '0') && (inputData[sp] <= '9')) {
shift = 0;
}
if ((inputData[sp] >= 'A') && (inputData[sp] <= 'Z')) {
shift = 0;
}
if ((inputData[sp] >= 'a') && (inputData[sp] <= 'z')) {
shift = 0;
}
if (inputData[sp] == ' ') {
shift = 0;
}
if (shift == 0) {
/* Mixed Mode character */
glyph = positionOf((char) inputData[sp], MIXED_ALPHANUM_SET);
infoSpace(glyph);
for (i = 0x20; i > 0; i = i >> 1) {
if ((glyph & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
} else {
/* Shift Mode character */
binary.append("1111110110"); /* 1014 - shift indicator */
addShiftCharacter(inputData[sp]);
}
sp++;
break;
case GM_UPPER:
shift = 1;
if ((inputData[sp] >= 'A') && (inputData[sp] <= 'Z')) {
shift = 0;
}
if (inputData[sp] == ' ') {
shift = 0;
}
if (shift == 0) {
/* Upper Case character */
glyph = positionOf((char) inputData[sp], MIXED_ALPHANUM_SET) - 10;
if (glyph == 52) {
// Space character
glyph = 26;
}
infoSpace(glyph);
for (i = 0x10; i > 0; i = i >> 1) {
if ((glyph & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
} else {
/* Shift Mode character */
binary.append("1111101"); /* 127 - shift indicator */
addShiftCharacter(inputData[sp]);
}
sp++;
break;
case GM_LOWER:
shift = 1;
if ((inputData[sp] >= 'a') && (inputData[sp] <= 'z')) {
shift = 0;
}
if (inputData[sp] == ' ') {
shift = 0;
}
if (shift == 0) {
/* Lower Case character */
glyph = positionOf((char) inputData[sp], MIXED_ALPHANUM_SET) - 36;
infoSpace(glyph);
for (i = 0x10; i > 0; i = i >> 1) {
if ((glyph & i) != 0) {
binary.append('1');
} else {
binary.append('0');
}
}
} else {
/* Shift Mode character */
binary.append("1111101"); /* 127 - shift indicator */
addShiftCharacter(inputData[sp]);
}
sp++;
break;
}
if (binary.length() > 9191) {
return 1;
}
} while (sp < length);
infoLine();
if (current_mode == Mode.GM_NUMBER) {
/* add numeric block padding value */
switch (p) {
case 1:
binary.insert(number_pad_posn, "10");
break; // 2 pad digits
case 2:
binary.insert(number_pad_posn, "01");
break; // 1 pad digit
case 3:
binary.insert(number_pad_posn, "00");
break; // 0 pad digits
}
}
if (current_mode == Mode.GM_BYTE) {
/* Add byte block length indicator */
addByteCount(byte_count_posn, byte_count);
}
/* Add "end of data" character */
switch (current_mode) {
case GM_CHINESE:
binary.append("1111111100000");
break; // 8160
case GM_NUMBER:
binary.append("1111111010");
break; // 1018
case GM_LOWER: