Repository navigation
Expand file tree
/
Copy pathdatamatrix.ps.src
More file actions
1239 lines (1127 loc) · 44.7 KB
/
Copy pathdatamatrix.ps.src
File metadata and controls
1239 lines (1127 loc) · 44.7 KB
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
% Barcode Writer in Pure PostScript
% https://bwipp.terryburton.co.uk
%
% vim: set sw=4 sts=4 et:
%
% Copyright (c) 2004-2026 Terry Burton
%
% $Id$
%
% Permission is hereby granted, free of charge, to any
% person obtaining a copy of this software and associated
% documentation files (the "Software"), to deal in the
% Software without restriction, including without
% limitation the rights to use, copy, modify, merge,
% publish, distribute, sublicense, and/or sell copies of
% the Software, and to permit persons to whom the Software
% is furnished to do so, subject to the following
% conditions:
%
% The above copyright notice and this permission notice
% shall be included in all copies or substantial portions
% of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
% KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
% THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
% IN THE SOFTWARE.
% --BEGIN ENCODER datamatrix--
% --REQUIRES preamble raiseerror setuphooks processoptions parseinput setanycolor fifocache rsecbinary render renmatrix--
% --DESC: Data Matrix
% --EXAM: This is Data Matrix!
% --EXOP:
% --RNDR: renmatrix
% --FMLY: Two-dimensional symbols
currentglobal
true setglobal
/setpacking where {pop currentpacking true setpacking} if
33 dict
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
dup /processoptions dup /uk.co.terryburton.bwipp findresource put
dup /parseinput dup /uk.co.terryburton.bwipp findresource put
dup /fifocache dup /uk.co.terryburton.bwipp findresource put
dup /rsecbinary dup /uk.co.terryburton.bwipp findresource put
dup /setuphooks dup /uk.co.terryburton.bwipp findresource put
dup /renmatrix dup /uk.co.terryburton.bwipp findresource put
dup /render dup /uk.co.terryburton.bwipp findresource put
begin
/datamatrix [/before /after] //setuphooks exec
(setup.define) //datamatrix.before exec
%
% Basic metrics for each symbol
%
/datamatrix.metrics [
% Standard square symbols
[ 10 10 1 1 5 1 0 ]
[ 12 12 1 1 7 1 0 ]
[ 14 14 1 1 10 1 0 ]
[ 16 16 1 1 12 1 0 ]
[ 18 18 1 1 14 1 0 ]
[ 20 20 1 1 18 1 0 ]
[ 22 22 1 1 20 1 0 ]
[ 24 24 1 1 24 1 0 ]
[ 26 26 1 1 28 1 0 ]
[ 32 32 2 2 36 1 0 ]
[ 36 36 2 2 42 1 0 ]
[ 40 40 2 2 48 1 0 ]
[ 44 44 2 2 56 1 0 ]
[ 48 48 2 2 68 1 0 ]
[ 52 52 2 2 84 2 0 ]
[ 64 64 4 4 112 2 0 ]
[ 72 72 4 4 144 4 0 ]
[ 80 80 4 4 192 4 0 ]
[ 88 88 4 4 224 4 0 ]
[ 96 96 4 4 272 4 0 ]
[ 104 104 4 4 336 6 0 ]
[ 120 120 6 6 408 6 0 ]
[ 132 132 6 6 496 8 0 ]
[ 144 144 6 6 620 10 0 ]
% Optional rectangular symbols
[ 8 18 1 1 7 1 0 ]
[ 8 32 1 2 11 1 0 ]
[ 8 48 1 2 15 1 1 ]
[ 8 64 1 4 18 1 1 ]
[ 8 80 1 4 22 1 1 ]
[ 8 96 1 4 28 1 1 ]
[ 8 120 1 6 32 1 1 ]
[ 8 144 1 6 36 1 1 ]
[ 12 26 1 1 14 1 0 ]
[ 12 36 1 2 18 1 0 ]
[ 12 64 1 4 27 1 1 ]
[ 12 88 1 4 36 1 1 ]
[ 16 36 1 2 24 1 0 ]
[ 16 48 1 2 28 1 0 ]
[ 16 64 1 4 36 1 1 ]
[ 20 36 1 2 28 1 1 ]
[ 20 44 1 2 34 1 1 ]
[ 20 64 1 4 42 1 1 ]
[ 22 48 1 2 38 1 1 ]
[ 24 48 1 2 41 1 1 ]
[ 24 64 1 4 46 1 1 ]
[ 26 40 1 2 38 1 1 ]
[ 26 48 1 2 42 1 1 ]
[ 26 64 1 4 50 1 1 ]
] readonly def
%
% Special characters and encoding values
%
/datamatrix.fnc1 -1 def /datamatrix.prog -2 def /datamatrix.m05 -3 def /datamatrix.m06 -4 def
/datamatrix.lC -5 def /datamatrix.lB -6 def /datamatrix.lX -7 def /datamatrix.lT -8 def /datamatrix.lE -9 def /datamatrix.unl -10 def
/datamatrix.sapp -11 def /datamatrix.usft -12 def /datamatrix.sft1 -13 def /datamatrix.sft2 -14 def /datamatrix.sft3 -15 def /datamatrix.eci -16 def /datamatrix.pad -17 def
/datamatrix.unlcw 254 def
/datamatrix.A 0 def /datamatrix.C 1 def /datamatrix.T 2 def /datamatrix.X 3 def /datamatrix.E 4 def /datamatrix.B 5 def
/datamatrix.encfuncs [ /encA /encCTX /encCTX /encCTX /encE /encB ] readonly def
%
% Codeword placement patterns
%
/datamatrix.dmn [
{ 2 copy 2 sub exch 2 sub exch } bind { 2 copy 1 sub exch 2 sub exch } bind
{ 2 copy 2 sub exch 1 sub exch } bind { 2 copy 1 sub exch 1 sub exch } bind
{ 2 copy exch 1 sub exch } bind { 2 copy 2 sub } bind
{ 2 copy 1 sub } bind { 2 copy } bind
] readonly def
/datamatrix.dmc1 [
{ mrows 1 sub 0 } bind { mrows 1 sub 1 } bind
{ mrows 1 sub 2 } bind { 0 mcols 2 sub } bind
{ 0 mcols 1 sub } bind { 1 mcols 1 sub } bind
{ 2 mcols 1 sub } bind { 3 mcols 1 sub } bind
] readonly def
/datamatrix.dmc2 [
{ mrows 3 sub 0 } bind { mrows 2 sub 0 } bind
{ mrows 1 sub 0 } bind { 0 mcols 4 sub } bind
{ 0 mcols 3 sub } bind { 0 mcols 2 sub } bind
{ 0 mcols 1 sub } bind { 1 mcols 1 sub } bind
] readonly def
/datamatrix.dmc3 [
{ mrows 3 sub 0 } bind { mrows 2 sub 0 } bind
{ mrows 1 sub 0 } bind { 0 mcols 2 sub } bind
{ 0 mcols 1 sub } bind { 1 mcols 1 sub } bind
{ 2 mcols 1 sub } bind { 3 mcols 1 sub } bind
] readonly def
/datamatrix.dmc4 [
{ mrows 1 sub 0 } bind { mrows 1 sub mcols 1 sub } bind
{ 0 mcols 3 sub } bind { 0 mcols 2 sub } bind
{ 0 mcols 1 sub } bind { 1 mcols 3 sub } bind
{ 1 mcols 2 sub } bind { 1 mcols 1 sub } bind
] readonly def
/datamatrix.latevars dup 16 dict def load /init {
(setup.latevars) //datamatrix.before exec
currentglobal
true setglobal
//datamatrix.latevars begin
%
% Character mapping dictionaries
%
/Avals <<
0 1 128 {dup 1 add} for
//datamatrix.pad 129
0 1 99 { % Double digits
dup 10 2 string cvrs (00) 2 string copy dup 2 3 index length sub 4 -1 roll putinterval
exch 130 add
} for
229 [ //datamatrix.lC //datamatrix.lB //datamatrix.fnc1 //datamatrix.sapp //datamatrix.prog //datamatrix.usft //datamatrix.m05 //datamatrix.m06 //datamatrix.lX //datamatrix.lT //datamatrix.lE //datamatrix.eci ] {exch 1 add dup} forall pop
>> def
/Avals <<
Avals {[exch]} forall
>> def
/CNvals <<
//datamatrix.sft1 0
//datamatrix.sft2 1
//datamatrix.sft3 2
32 3
48 1 57 {dup 44 sub} for % 0-9
65 1 90 {dup 51 sub} for % A-Z
>> readonly def
/C1vals << 0 1 31 {dup} for >> readonly def
/C2vals <<
33 1 47 {dup 33 sub} for
58 1 64 {dup 43 sub} for
91 1 95 {dup 69 sub} for
//datamatrix.fnc1 27
//datamatrix.usft 30
>> readonly def
/C3vals << 96 1 127 {dup 96 sub} for >> readonly def
/Cvals <<
CNvals {[exch]} forall
C1vals {[exch CNvals //datamatrix.sft1 get exch]} forall
C2vals {[exch CNvals //datamatrix.sft2 get exch]} forall
C3vals {[exch CNvals //datamatrix.sft3 get exch]} forall
>> def
/TNvals <<
//datamatrix.sft1 0
//datamatrix.sft2 1
//datamatrix.sft3 2
32 3
48 1 57 {dup 44 sub} for % 0-9
97 1 122 {dup 83 sub} for % a-z
>> readonly def
/T1vals << 0 1 31 {dup} for >> readonly def
/T2vals <<
33 1 47 {dup 33 sub} for
58 1 64 {dup 43 sub} for
91 1 95 {dup 69 sub} for
//datamatrix.fnc1 27
//datamatrix.usft 30
>> readonly def
/T3vals <<
96 0
65 1 90 {dup 64 sub} for
123 1 127 {dup 96 sub} for
>> readonly def
/Tvals <<
TNvals {[exch]} forall
T1vals {[exch TNvals //datamatrix.sft1 get exch]} forall
T2vals {[exch TNvals //datamatrix.sft2 get exch]} forall
T3vals {[exch TNvals //datamatrix.sft3 get exch]} forall
>> def
%
% Extended ASCII mappings
%
128 1 255 {
/i exch def
Avals i [ Avals //datamatrix.usft get aload pop Avals i 128 sub get aload pop ] put
Cvals i [ Cvals //datamatrix.usft get aload pop Cvals i 128 sub get aload pop ] put
Tvals i [ Tvals //datamatrix.usft get aload pop Tvals i 128 sub get aload pop ] put
} for
/Avals Avals readonly def
/Cvals Cvals readonly def
/Tvals Tvals readonly def
/Xvals <<
13 0
42 1
62 2
32 3
48 1 57 {dup 44 sub} for
65 1 90 {dup 51 sub} for
>> readonly def
/Xvals <<
Xvals {[exch]} forall
>> readonly def
/Evals <<
64 1 94 {dup 64 sub} for
//datamatrix.unl 31
32 1 63 {dup} for
>> readonly def
/Evals <<
Evals {[exch]} forall
>> readonly def
/Bvals <<
0 1 255 {dup} for
>> readonly def
/Bvals <<
Bvals {[exch]} forall
>> readonly def
/encvals [ Avals Cvals Tvals Xvals Evals Bvals ] readonly def
%
% Calculate the Reed-Solomon log and anti-log tables for GF(256) with
% prime polynomial 301, the anti-log table spanning two periods so that
% log-sums resolve without a modulo reduction
%
/rsalog [ 1 510 { dup 2 mul dup 256 ge {301 xor} if } repeat ] readonly def
/rslog 256 array def
1 1 255 {dup rsalog exch get exch rslog 3 1 roll put} for
/rslog rslog readonly def
/init { //datamatrix.latevars {def} forall } def
end
//datamatrix.latevars /init get exec
setglobal
(setup.latevars) //datamatrix.after exec
} bind put
/uk.co.terryburton.bwipp.global_ctx dup where {
exch get /preload known {//datamatrix.latevars /init get exec} if
} {pop} ifelse
%
% Function to calculate the product in the field
%
/datamatrix.rsprod {
dup 0 ne { 1 index 0 ne {
rslog exch get exch rslog exch get add rsalog exch get
} { pop pop 0 } ifelse } { pop pop 0 } ifelse
} bind def
%
% Generate Reed-Solomon coefficients for a given k
%
/datamatrix.gencoeffs {
/coeffs [ 1 k {0} repeat ] def
1 1 k { % i
coeffs 1 index 2 copy 1 sub get put % coeffs[i] = coeffs[i-1]
rsalog 1 index get % ai = rsalog[i]
exch 1 sub -1 1 { % j
coeffs exch 2 copy get % ... coeffs j coeffs[j]
3 index % ... ai
//datamatrix.rsprod exec % p = coeffs[j] * ai
coeffs 2 index 1 sub get % ... coeffs[j-1]
xor put % coeffs[j] = p ^ coeffs[j-1]
} for
coeffs 0 2 copy get % ... coeffs 0 coeffs[0]
3 index % ... ai
//datamatrix.rsprod exec put % coeffs[0] = coeffs[0] * ai
pop % ai
} for
%
% Deliver the coefficients reversed and in the log domain, with -1
% denoting a zero coefficient, as rsecbinary requires
%
[ k 1 sub -1 0 {
coeffs exch get dup 0 ne { rslog exch get } { pop -1 } ifelse
} for ] readonly
} bind def
%
% FIFO cache for ECC polynomials
%
% 25 unique keys: per-block 5-68 from symbol configurations
%
/datamatrix.coeffscachemax 25 def % Override with global_ctx.datamatrix.coeffscachemax
/datamatrix.coeffscachelimit 766 def % Override with global_ctx.datamatrix.coeffscachelimit
/uk.co.terryburton.bwipp.global_ctx dup where {
exch get
dup /datamatrix.coeffscachemax 2 copy known {get /datamatrix.coeffscachemax exch def} {pop pop} ifelse
/datamatrix.coeffscachelimit 2 copy known {get /datamatrix.coeffscachelimit exch def} {pop pop} ifelse
} {pop} ifelse
/datamatrix.coeffscache datamatrix.coeffscachemax datamatrix.coeffscachelimit //fifocache exec def
/datamatrix.coeffscachefetch {
/k exch def
k
{ //datamatrix.gencoeffs exec }
{ length }
//datamatrix.coeffscache /fetch get exec
} bind def
/datamatrix {
64 dict begin
{
(input.processoptions) //datamatrix.before exec
/dontdraw false def
/columns 0 def
/rows 0 def
/format (unset) def % square or rectangle
/version (unset) def
/parse false def
/parsefnc false def
/c40headerlength -1 def
/raw false def
/dmre false def
/strictspec false def
/propspec false def
/loosespec false def
/mag 1.0 def
/xdim -1.0 def
/ast (default) def
/xnom -1.0 def
/xmin -1.0 def
/xmax -1.0 def
null /apply //processoptions exec /options exch def
/global_encoder_defaults //render exec
/barcode exch def
/_render
dontdraw /uk.co.terryburton.bwipp.global_ctx dup where {exch get /enabledontdraw known} {pop false} ifelse and
/uk.co.terryburton.bwipp._dontdraw dup where {exch get} {pop false} ifelse or not
def
/modunit 2 def
/uk.co.terryburton.bwipp.global_ctx dup where {
exch get /default_modunit 2 copy known {get /modunit exch def} {pop pop} ifelse
} {pop} ifelse
%
% Apply AST spec overrides and resolve physical specification
%
/datamatrix ast /apply_ast //render exec not { //raiseerror exec } if
/resolve_strictspec //render exec
(input.processoptions) //datamatrix.after exec
//datamatrix.latevars /init get exec
%
% Input validation
%
(input.validate) //datamatrix.before exec
barcode () eq {
/bwipp.datamatrixEmptyData (The data must not be empty) //raiseerror exec
} if
barcode length 15000 gt {
/bwipp.datamatrixInputTooLong (The input data is too long) //raiseerror exec
} if
version (unset) ne rows 0 ne columns 0 ne or and {
/bwipp.datamatrixVersionRowsCols (rows and columns must not be given if version is specified) //raiseerror exec
} if
%
% Parse version=RxC into rows and columns
%
version (unset) ne {
version (x) search {
exch pop
2 copy 2 array astore {
true
1 index length 0 eq { pop false } if
exch {
dup 48 lt exch 57 gt or { pop false } if
} forall
not {
pop pop /bwipp.datamatrixVersionBadRowOrColumn (version must be formatted as RxC) //raiseerror exec
} if
} forall
/rows exch cvi def
/columns exch cvi def
/format rows columns eq { (square) } { (rectangle) } ifelse def
} {
pop /bwipp.datamatrixVersionFormat (version must be formatted as RxC) //raiseerror exec
} ifelse
} {
format (unset) eq {
/format dmre { (rectangle) } { (square) } ifelse def
} if
} ifelse
format (square) ne format (rectangle) ne and {
/bwipp.datamatrixInvalidFormat (The format must be either square or rectangle) //raiseerror exec
} if
(input.validate) //datamatrix.after exec
%
% Create array of codewords remaining to make a full symbol using stack-based accumulation
%
/urows rows def
/ucols columns def
mark
//datamatrix.metrics {
/m exch def
/rows m 0 get def % Rows in symbol
/cols m 1 get def % Columns in symbol
/regh m 2 get def % Horizontal regions
/regv m 3 get def % Vertical regions
/rscw m 4 get def % Error correction codewords
/rsbl m 5 get def % Error correction blocks
/doly m 6 get def % DMRE-only model
/mrows rows 2 regh mul sub def % Rows in the mapping matrix
/mcols cols 2 regv mul sub def % Columns in the mapping matrix
/ncws mrows mcols mul 8 idiv rscw sub def % Data codewords
/okay true def
urows 0 ne urows rows ne and {/okay false def} if
ucols 0 ne ucols cols ne and {/okay false def} if
format (square) eq rows cols ne and {/okay false def} if
format (rectangle) eq rows cols eq and {/okay false def} if
dmre not doly 1 eq and {/okay false def} if
okay { ncws } if % Push ncws
} forall
counttomark array astore /fullcws exch def pop
/numremcws [ 1558 {10000} repeat ] def
fullcws {numremcws exch 1 sub 1 put} forall
1556 -1 0 {
/i exch def
numremcws i get 1 ne {
numremcws i numremcws i 1 add get 1 add put
} if
} for
/getnumremcws {
dup numremcws length ge {
pop /bwipp.datamatrixTooMuchData (The input data exceeds the symbol capacity) //raiseerror exec
} if
numremcws exch get
} def
raw {
/cws barcode length array def
/i 0 def /j 0 def
{ % loop
i barcode length 3 sub ge {exit} if
barcode i get 94 ne {exit} if
barcode i 1 add 3 getinterval false 1 index {
dup 48 lt exch 57 gt or { pop true } if
} forall { pop exit } if
cvi /cw exch def
cw 255 gt {
/bwipp.datamatrixBadRawCodeword (Raw codewords must be 0 to 255) //raiseerror exec
} if
cws j cw put
/i i 4 add def
/j j 1 add def
} loop
i barcode length ne {
/bwipp.datamatrixBadRawFormat (Raw codewords must be formatted as ^NNN) //raiseerror exec
} if
/cws cws 0 j getinterval def
} {
%
% Parse the input
%
(input.preprocess) //datamatrix.before exec
/fncvals <<
/parse parse
/parsefnc parsefnc
/eci true
(FNC1) //datamatrix.fnc1
(PROG) //datamatrix.prog
% (MAC5) //datamatrix.m05 Encoded automatically
% (MAC6) //datamatrix.m06 Encoded automatically
>> def
barcode fncvals //parseinput exec /msg exch def
/msglen msg length def
%
% Macro 05/06 compression of [)>{RS}05/06{GS}...{RS}{EOT} structured data
%
msglen 9 ge {
msg 0 7 getinterval aload pop
29 eq exch pop exch 48 eq and exch % [)>{RS}0*{GS}...
30 eq and exch 62 eq and exch 41 eq and exch 91 eq and {
msg msglen 2 sub 2 getinterval aload pop
4 eq exch 30 eq and { % ...{RS}{EOT}
msg 5 get 53 eq { % ...5...
/msg [ //datamatrix.m05 msg 7 msg length 9 sub getinterval aload pop ] def
} {
msg 5 get 54 eq { % ...6...
/msg [ //datamatrix.m06 msg 7 msg length 9 sub getinterval aload pop ] def
} if
} ifelse
} if
} if
} if
/msglen msg length def
% Absolute capacity is 144x144 numeric.
msglen 3116 gt {
/bwipp.datamatrixInputTooLong (The input data is too long) //raiseerror exec
} if
(input.preprocess) //datamatrix.after exec
%
% Input encoding
%
(input.encoding) //datamatrix.before exec
c40headerlength msglen gt { /c40headerlength msglen def } if % Make sure it's bounded as may increment
/numD [ msglen {0} repeat 0 ] def
/nextXterm [ msglen {0} repeat 9999 ] def
/nextNonX [ msglen {0} repeat 9999 ] def
/isECI [ msglen {false} repeat false ] def
msglen 1 sub -1 0 {
/i exch def
/barchar msg i get def
barchar 48 ge barchar 57 le and {
numD i numD i 1 add get 1 add put
} if
barchar 13 eq barchar 42 eq or barchar 62 eq or {
nextXterm i 0 put
} {
nextXterm i nextXterm i 1 add get 1 add put
} ifelse
Xvals barchar known not {
nextNonX i 0 put
} {
nextNonX i nextNonX i 1 add get 1 add put
} ifelse
isECI i barchar -1000000 le put
} for
/nextXterm [nextXterm {dup 10000 gt {pop 10000} if} forall] def
/nextNonX [nextNonX {dup 10000 gt {pop 10000} if} forall] def
/isD {char 48 ge char 57 le and} def
/isC {CNvals char known} def
/isT {TNvals char known} def
/isX {Xvals char known} def
/isE {Evals char known} def
/isEA {char 127 gt} def
/isFN {char 0 lt} def
/XtermFirst {dup nextXterm exch get exch nextNonX exch get lt} def
%
% TODO To avoid roundoff, consider increasing the scores by a scale factor of 12 to convert to integers
%
/lookup {
/ac 1.0 def /cc 2.0 def /tc 2.0 def /xc 2.0 def /ec 2.0 def /bc 2.25 def
mode //datamatrix.A eq {/ac 0.0 def /cc 1.0 def /tc 1.0 def /xc 1.0 def /ec 1.0 def /bc 1.25 def} if
mode //datamatrix.C eq {/cc 0.0 def} if
mode //datamatrix.T eq {/tc 0.0 def} if
mode //datamatrix.X eq {/xc 0.0 def} if
mode //datamatrix.E eq {/ec 0.0 def} if
mode //datamatrix.B eq {/bc 0.0 def} if
1 { % common exit
isECI i get {//datamatrix.A exit} if % ECI only in ASCII
i c40headerlength lt {//datamatrix.C exit} if % C40 only for c40headerlength characters
/isfnc1 msg i get //datamatrix.fnc1 eq def
/k 0 def { % loop
i k add msglen eq {
[/ac /cc /tc /xc /ec /bc] {dup load ceiling def} forall
true [ cc tc xc ec bc] {ac exch le and} forall {//datamatrix.A exit} if
true [ac cc tc xc ec ] {bc exch lt and} forall {isfnc1 {//datamatrix.A} {//datamatrix.B} ifelse exit} if
true [ac cc tc xc bc] {ec exch lt and} forall {//datamatrix.E exit} if
true [ac cc xc ec bc] {tc exch lt and} forall {//datamatrix.T exit} if
true [ac cc tc ec bc] {xc exch lt and} forall {//datamatrix.X exit} if
//datamatrix.C exit
} if
/char msg i k add get def
/ac ac isD {1 2 div add} {isEA {ceiling 2 add} {ceiling 1 add} ifelse} ifelse def
/cc cc isC {2 3 div add} {isEA { 8 3 div add } { 4 3 div add } ifelse} ifelse def
/tc tc isT {2 3 div add} {isEA { 8 3 div add } { 4 3 div add } ifelse} ifelse def
/xc xc isX {2 3 div add} {isEA {13 3 div add } {10 3 div add } ifelse} ifelse def
/ec ec isE {3 4 div add} {isEA {17 4 div add } {13 4 div add } ifelse} ifelse def
/bc bc isFN {4 add} {1 add} ifelse def
k 4 ge {
true [ cc tc xc ec bc] {ac 1 add exch le and} forall {//datamatrix.A exit} if
bc 1 add ac le {isfnc1 {//datamatrix.A} {//datamatrix.B} ifelse exit} if
true [ cc tc xc ec ] {bc 1 add exch lt and} forall {isfnc1 {//datamatrix.A} {//datamatrix.B} ifelse exit} if
true [ac cc tc xc bc] {ec 1 add exch lt and} forall {//datamatrix.E exit} if
true [ac cc xc ec bc] {tc 1 add exch lt and} forall {//datamatrix.T exit} if
true [ac cc tc ec bc] {xc 1 add exch lt and} forall {//datamatrix.X exit} if
true [ac tc ec bc] {cc 1 add exch lt and} forall {
cc xc lt {//datamatrix.C exit} if
cc xc eq {i k add 1 add XtermFirst {//datamatrix.X exit} {//datamatrix.C exit} ifelse} if
} if
} if
/k k 1 add def
} loop
} repeat
} def
/addtocws {
dup length j add cws length gt {
pop /bwipp.datamatrixTooMuchData (The input data exceeds the symbol capacity) //raiseerror exec
} if
dup cws exch j exch putinterval
/j exch length j add def
} def
/ECItocws {
neg 1000000 sub
dup 000126 le { % ECI 000000 - 000126
1 add
1 array astore
} { dup 016382 le { % ECI 000127 - 016382
127 sub
dup 254 idiv 128 add exch
254 mod 1 add
2 array astore
} { % ECI 016383 - 999999
16383 sub
dup 64516 idiv 192 add exch
dup 254 idiv 254 mod 1 add exch
254 mod 1 add
3 array astore
} ifelse } ifelse
} def
/encA {
1 { % Common exit
isECI i get {
Avals //datamatrix.eci get addtocws
msg i get ECItocws addtocws
/i i 1 add def
exit
} if
i c40headerlength ge numD i get 2 ge and {
2 string dup 0 msg i get put dup 1 msg i 1 add get put Avals exch get addtocws
/i i 2 add def
exit
} if
/newmode lookup def
newmode mode ne {
Avals [-1 //datamatrix.lC //datamatrix.lT //datamatrix.lX //datamatrix.lE //datamatrix.lB] newmode get get addtocws
/mode newmode def
exit
} if
Avals msg i get get addtocws
/i i 1 add def
exit
} repeat
} def
/CTXvalstocws {
/in exch def
mark
0 3 in length 1 sub {
in exch 3 getinterval 0 exch {add 40 mul} forall 40 idiv 1 add
dup 256 idiv exch 256 mod
} for
counttomark array astore exch pop
} def
/encCTX {
/p 0 def
/ctxvals 2500 array def
/done false def
% Lookup the values for each character
{
i msglen eq {exit} if
encvals mode get msg i get known not {exit} if
p 3 mod 0 eq {
p 0 gt {
/newmode lookup def
newmode mode ne {
ctxvals 0 p getinterval CTXvalstocws addtocws
[//datamatrix.unlcw] addtocws
newmode //datamatrix.A ne {
Avals [-1 //datamatrix.lC //datamatrix.lT //datamatrix.lX //datamatrix.lE //datamatrix.lB] newmode get get addtocws
} if
/mode newmode def
/done true def
exit
} if
} if
msglen i sub 3 le { % Check end of data conditions
j p 3 idiv 2 mul add getnumremcws /remcws exch def
/remvals [
msg i msglen i sub getinterval {
dup encvals mode get exch known {
encvals mode get exch get aload pop
} { % Unencodable X12 characters
pop -1 -1 -1 -1
} ifelse
} forall
] def
remcws 2 eq remvals length 3 eq and {
% Encode and omit unlatch
[
ctxvals 0 p getinterval aload pop
remvals aload pop
] CTXvalstocws addtocws
/mode //datamatrix.A def
/i msglen def
/done true def
exit
} if
% Encode with sft1 as third value, omit unlatch
remcws 2 eq remvals length 2 eq and mode //datamatrix.X ne and {
[
ctxvals 0 p getinterval aload pop
remvals aload pop
encvals mode get //datamatrix.sft1 get aload pop
] CTXvalstocws addtocws
/mode //datamatrix.A def
/i msglen def
/done true def
exit
} if
remcws 2 eq remvals length 1 eq and {
% Unlatch to ASCII
ctxvals 0 p getinterval CTXvalstocws addtocws
[//datamatrix.unlcw] addtocws
Avals msg i get get addtocws
/mode //datamatrix.A def
/i msglen def
/done true def
exit
} if
remcws 1 eq remvals length 1 eq and {
% Implied unlatch to ASCII
ctxvals 0 p getinterval CTXvalstocws addtocws
Avals msg i get get addtocws
/mode //datamatrix.A def
/i msglen def
/done true def
exit
} if
} if
} if
encvals mode get msg i get get
dup length p add ctxvals length gt {
pop /bwipp.datamatrixTooMuchData (The input data exceeds the symbol capacity) //raiseerror exec
} if
dup ctxvals exch p exch putinterval
/p exch length p add def
/i i 1 add def
} loop
%
% Backtrack to a boundary and return to ASCII
%
done not {
{
p 3 mod 0 eq {exit} if
/i i 1 sub def
/p p encvals mode get msg i get get length sub def
} loop
[
ctxvals 0 p getinterval aload pop
] CTXvalstocws addtocws
[//datamatrix.unlcw] addtocws
/mode //datamatrix.A def
/c40headerlength -1 def % Disable when backtracking
% Encode something to avoid latching immediately back
i msglen ne isECI i get not and {
numD i get 2 ge {
2 string dup 0 msg i get put dup 1 msg i 1 add get put Avals exch get addtocws
/i i 2 add def
} {
Avals msg i get get addtocws
/i i 1 add def
} ifelse
} if
} if
} def
/Evalstocws {
/in exch def
/inlen in length def
/outlen in length 4 div 3 mul ceiling cvi def
/in [ in aload pop 0 0 0 ] def
mark
0 4 inlen 1 sub {
in exch 4 getinterval 0 exch {or 6 bitshift} forall -6 bitshift
dup -16 bitshift 255 and exch
dup -8 bitshift 255 and exch
255 and
} for
counttomark array astore exch pop
0 outlen getinterval
} def
/encE {
/p 0 def
/edifactvals 2100 array def
%
% Lookup the values for each character
%
{
i msglen eq {exit} if
Evals msg i get known not {exit} if
p edifactvals length ge {
/bwipp.datamatrixTooMuchData (The input data exceeds the symbol capacity) //raiseerror exec
} if
p 4 mod 0 eq {
msglen i sub 2 le { % Check end of data condition
j p 4 idiv 3 mul add getnumremcws /remcws exch def
/remvals [
msg i msglen i sub getinterval {
Avals exch get aload pop
} forall
] def
remcws 1 eq remcws 2 eq or remvals length remcws le and {
% Implied unlatch to ASCII
edifactvals 0 p getinterval Evalstocws addtocws
remvals addtocws
/mode //datamatrix.A def
/i msglen def
exit
} if
} if
lookup mode ne {exit} if
} if
Evals msg i get get
dup edifactvals exch p exch putinterval
/p exch length p add def
/i i 1 add def
} loop
%
% Unlatch and pad with zeros until boundary
%
mode //datamatrix.A ne {
j p 4 idiv 3 mul add 1 sub getnumremcws 1 sub /remcws exch def
p 4 mod 0 ne i msglen ne or remcws 3 ge or { % Unlatch unless less than 3 codewords remain
Evals //datamatrix.unl get
dup edifactvals exch p exch putinterval
/p exch length p add def
} if
edifactvals 0 p getinterval Evalstocws addtocws
/mode //datamatrix.A def
/c40headerlength -1 def % Disable when backtracking
% Encode something to avoid latching immediately back
i msglen ne isECI i get not and {
numD i get 2 ge {
2 string dup 0 msg i get put dup 1 msg i 1 add get put Avals exch get addtocws
/i i 2 add def
} {
Avals msg i get get addtocws
/i i 1 add def
} ifelse
} if
} if
} def
/encB {
/p 0 def /bvals 1558 array def {
i msglen eq {exit} if
lookup mode ne {exit} if
p bvals length ge {
/bwipp.datamatrixTooMuchData (The input data exceeds the symbol capacity) //raiseerror exec
} if
bvals p msg i get put
/p p 1 add def
/i i 1 add def
} loop
j p add getnumremcws 1 sub /remcws exch def
/bvals [
remcws 0 eq i msglen eq and { % Terminates symbol
0
} {
p 250 lt {p} {p 250 idiv 249 add p 250 mod} ifelse
} ifelse
bvals 0 p getinterval aload pop
] def
0 1 bvals length 1 sub { % Pseudo-randomise based on codeword position
/p exch def
j p add 1 add 149 mul 255 mod 1 add bvals p get add
dup 256 ge {256 sub} if
bvals exch p exch put
} for
bvals addtocws
/mode //datamatrix.A def
} def
%
% Derive the codewords
%
/cws 1558 array def
/mode //datamatrix.A def /i 0 def /j 0 def
% FNC1 in 1st position must be in ASCII to be GS1
msg 0 get //datamatrix.fnc1 eq {
Avals //datamatrix.fnc1 get addtocws
/i 1 def
% Compensate c40headerlength if any
c40headerlength 0 gt { /c40headerlength c40headerlength 1 add def } if
} if
% Macro 05/06 is only recognised when its codeword is first
msg 0 get //datamatrix.m05 eq msg 0 get //datamatrix.m06 eq or {
Avals msg 0 get get addtocws
/i 1 def
% Compensate c40headerlength if any
c40headerlength 0 gt { /c40headerlength c40headerlength 1 add def } if
} if
{
i msglen ge {exit} if
//datamatrix.encfuncs mode get load exec
} loop
/cws cws 0 j getinterval def
} ifelse
%
% Extend codewords to fill symbol with pseudo-randomised pad characters
%
/datlen cws length def
j 1 sub getnumremcws 1 sub /remcws exch def
remcws 0 gt {
/cws [ cws aload pop remcws {129} repeat ] def