-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
datamatrix.ps
599 lines (548 loc) · 19.8 KB
/
datamatrix.ps
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
%!PS
% Barcode Writer in Pure PostScript
% http://www.terryburton.co.uk/barcodewriter/
%
% Copyright (c) 2004-2013 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 renmatrix--
% --DESC: Data Matrix
% --EXAM: This is Data Matrix!
% --EXOP: rows=32 columns=32
% --RNDR: renmatrix
/setpacking where {pop currentpacking true setpacking} if
1 dict
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
dup /renmatrix dup /uk.co.terryburton.bwipp findresource put
begin
/datamatrix {
20 dict begin
/options exch def
/barcode exch def
/dontdraw false def
/columns 0 def
/rows 0 def
/prefix () def
/encoding (ascii) def
/raw false def
/parse false def
/parsefnc false def
% Parse the input options
options type /stringtype eq {
1 dict begin
options {
token false eq {exit} if dup length string cvs (=) search
true eq {cvlit exch pop exch def} {cvlit true def} ifelse
} loop
currentdict end /options exch def
} if
options {def} forall
/columns columns cvi def
/rows rows cvi def
% Parse ordinals of the form ^NNN to ASCII
parse {
/msg barcode length string def
/j 0 def
barcode
{ % loop
(^) search {
dup msg exch j exch putinterval
length j add 1 add /j exch def
pop
dup 0 3 getinterval cvi msg exch j 1 sub exch put
dup length 3 sub 3 exch getinterval
} {
dup msg exch j exch putinterval
length j add /j exch def
/barcode msg 0 j getinterval def
exit
} ifelse
} loop
} if
/premaps <<
/FNC1 [ 232 ] /PROG [ 234 ] /MAC5 [ 236 ] /MAC6 [ 237 ]
() []
>> def
premaps prefix known {
/precws premaps prefix get def
} {
/bwipp.datamatrixBadPrefix (The prefix for Data Matrix must be FNC1, PROG, MAC5 or MAC6) //raiseerror exec
} ifelse
/prelen precws length def
/barlen barcode length def
raw {/encoding (raw) def} if
encoding (raw) eq {
/cws barlen prelen add array def
cws 0 precws putinterval
/i 0 def /j prelen def
{ % loop
i barlen eq {exit} if
/cw barcode i 1 add 3 getinterval cvi def
cws j cw put
/i i 4 add def
/j j 1 add def
} loop
/cws cws 0 j getinterval def
} if
% Special function characters
/fn1 -1 def
encoding (ascii) eq {
/fncvals <<
(FNC1) fn1
>> def
/msg barlen array def
/i 0 def /j 0 def {
i barlen eq {exit} if
/char barcode i get def
parsefnc char 94 eq and i barlen 4 sub lt and {
barcode i 1 add get 94 ne {
/char fncvals barcode i 1 add 4 getinterval get def
/i i 4 add def
} {
/i i 1 add def
} ifelse
} if
msg j char put
/i i 1 add def
/j j 1 add def
} loop
/msg msg 0 j getinterval def
/msglen msg length def
/cws msglen 2 mul prelen add array def
cws 0 precws putinterval
/i 0 def /j prelen def
{ % loop
i msglen eq {exit} if
/char msg i get def
char fn1 eq { % FNC1
cws j 232 put
/i i 1 add def
/j j 1 add def
} { char 48 ge char 57 le and i msglen 1 sub lt and {
msg i 1 add get dup 48 ge exch 57 le and { % Digit pair
cws j msg i get 48 sub 10 mul msg i 1 add get 48 sub add 130 add put
/i i 2 add def
/j j 1 add def
} { % Singular digit
cws j char 1 add put
/i i 1 add def
/j j 1 add def
} ifelse
} { char 127 le { % ASCII
cws j char 1 add put
/i i 1 add def
/j j 1 add def
} { % Extended ASCII
cws j 235 put
cws j 1 add char 127 sub put
/i i 1 add def
/j j 2 add def
} ifelse } ifelse } ifelse
} loop
/cws cws 0 j getinterval def
} if
/base40 false def % Used for size optimization
encoding (c40) eq encoding (text) eq encoding (x12) eq or or {
% Pathological case : 8-bit chars encode to 4 base-40 values
% plus extra 2 for possible nul-pading
/b40 barlen 4 mul 2 add array def
/base40 true def
% Maps are encoded as static strings with the base-40 value in
% the right 6-bits and the shift-state in the two left bits.
% The value 255 indicates an invalid mapping (x12 only).
encoding (c40) eq {
/mode 230 def
/8bit true def
/charmap <
404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f
03808182838485868788898a8b8c8d8e0405060708090a0b0c0d8f9091929394
950e0f101112131415161718191a1b1c1d1e1f2021222324252627969798999a
c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf
> def
} if
encoding (text) eq {
/mode 239 def
/8bit true def
/charmap <
404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f
03808182838485868788898a8b8c8d8e0405060708090a0b0c0d8f9091929394
95c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9da969798999a
c00e0f101112131415161718191a1b1c1d1e1f2021222324252627dbdcdddedf
> def
} if
encoding (x12) eq {
/mode 238 def
/8bit false def
/parsefnc false def
/charmap <
ffffffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffff
03ffffffffffffffffff01ffffffffff0405060708090a0b0c0dffffffff02ff
ff0e0f101112131415161718191a1b1c1d1e1f2021222324252627ffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
> def
} if
/i 0 def /j 0 def
{ % loop
i barlen ge {exit} if
/char barcode i get def
% ^FNC1
parsefnc char 94 eq and i barlen 4 sub lt and {
barcode i 5 getinterval (^FNC1) eq {
b40 j [ 1 27 ] putinterval % set-2, FNC1
/char -1 def
/i i 5 add def
/j j 2 add def
} if
} if
% Replace ^^ with ^
parsefnc char 94 eq and i barlen 1 sub lt and {
barcode i 1 add get 94 eq {
/i i 1 add def
} if
} if
8bit char 127 gt and {
/char char 127 and def
b40 j [ 1 30 ] putinterval % set-2, high-bit
/j j 2 add def
} if
char 0 ge char 127 le and {
charmap char get
dup 255 ne {
dup -6 bitshift 1 sub
dup 0 ge {
b40 j 3 -1 roll put
/j j 1 add def
} {
pop
} ifelse
63 and b40 j 3 -1 roll put
/j j 1 add def
} {
/bwipp.datamatrixBadX12character (Invalid character for Data Matrix X12 encoding) //raiseerror exec
} ifelse
/i i 1 add def
} if
} loop
% Nul-pad to full triples
{ % loop
j 3 mod 0 eq { exit } if
b40 j 0 put
/j j 1 add def
} loop
% Convert triples to 2 bytes
/b40len j def
/cws b40len 3 div 2 mul prelen add 1 add cvi array def
cws 0 precws putinterval
cws prelen mode put
/i 0 def /j prelen 1 add def
{ % loop
i b40len eq { exit } if
cws j [
b40 i get 1600 mul b40 i 1 add get 40 mul add b40 i 2 add get add 1 add
dup -8 bitshift
exch 255 and
] putinterval
/i i 3 add def
/j j 2 add def
} loop
} if
% Basic metrics for the each symbol
% rows cols regh regv rscw rsbl
/metrics [
% Standard square symbols
[ 10 10 1 1 5 1 ]
[ 12 12 1 1 7 1 ]
[ 14 14 1 1 10 1 ]
[ 16 16 1 1 12 1 ]
[ 18 18 1 1 14 1 ]
[ 20 20 1 1 18 1 ]
[ 22 22 1 1 20 1 ]
[ 24 24 1 1 24 1 ]
[ 26 26 1 1 28 1 ]
[ 32 32 2 2 36 1 ]
[ 36 36 2 2 42 1 ]
[ 40 40 2 2 48 1 ]
[ 44 44 2 2 56 1 ]
[ 48 48 2 2 68 1 ]
[ 52 52 2 2 84 2 ]
[ 64 64 4 4 112 2 ]
[ 72 72 4 4 144 4 ]
[ 80 80 4 4 192 4 ]
[ 88 88 4 4 224 4 ]
[ 96 96 4 4 272 4 ]
[ 104 104 4 4 336 6 ]
[ 120 120 6 6 408 6 ]
[ 132 132 6 6 496 8 ]
[ 144 144 6 6 620 10 ]
% Optional rectangular symbols
[ 8 18 1 1 7 1 ]
[ 8 32 1 2 11 1 ]
[ 12 26 1 1 14 1 ]
[ 12 36 1 2 18 1 ]
[ 16 36 1 2 24 1 ]
[ 16 48 1 2 28 1 ]
] def
% Select metrics of an appropriate symbol
/urows rows def
/ucols columns def
/i 0 def
{ % loop
/m metrics i get 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
/mrows rows 2 regh mul sub def % Rows in the mapping matrix
/mcols cols 2 regv mul sub def % Columns in the mapping matrix
/rrows mrows regh idiv def % Rows per region
/rcols mcols regv idiv def % Columns per region
/ncws mrows mcols mul 8 idiv rscw sub def % Data codewords
/okay true def
cws length ncws gt {/okay false def} if
urows 0 ne urows rows ne and {/okay false def} if
ucols 0 ne ucols cols ne and {/okay false def} if
okay {exit} if
/i i 1 add def
} loop
% C40/TEXT/X12 size optimization - do not emit the unlatch unless we pad
base40 cws length ncws lt and {
/cws [ cws {} forall 254 ] def
} if
% Extend cws to ncws codewords by addition of pseudo-randomised pad characters
cws length ncws lt {
/datlen cws length def
/cws [ cws {} forall ncws datlen sub {129} repeat ] def
datlen 1 add 1 ncws 1 sub {
/i exch def
i 1 add 149 mul 253 mod 1 add 129 add
dup 254 gt {254 sub} if
cws exch i exch put
} for
} if
% De-interleave the codewords into blocks
/cwbs rsbl array def % Array of data codeword blocks
/ecbs rsbl array def % Array of error correction blocks
0 1 rsbl 1 sub {
/i exch def
cws length 1558 ne {
/cwbsize cws length rsbl idiv def
} {
i 7 le {/cwbsize 156 def} {/cwbsize 155 def} ifelse
} ifelse
/cwb cwbsize array def
0 1 cwbsize 1 sub {
/j exch def
cwb j cws j rsbl mul i add get put
} for
cwbs i cwb put
ecbs i [ rscw rsbl idiv {0} repeat ] put
} for
% Calculate the log and anti-log tables
/rsalog [ 1 255 { dup 2 mul dup 256 ge {301 xor} if } repeat ] def
/rslog 256 array def
1 1 255 {dup rsalog exch get exch rslog 3 1 roll put} for
% Function to calculate the product in the field
/rsprod {
2 copy 0 ne exch 0 ne and {
rslog exch get exch rslog exch get add 255 mod rsalog exch get
} {
pop pop 0
} ifelse
} bind def
% Generate the coefficients
/coeffs [ 1 rscw rsbl idiv {0} repeat ] def
1 1 rscw rsbl idiv {
/i exch def
coeffs i coeffs i 1 sub get put
i 1 sub -1 1 {
/j exch def
coeffs j coeffs j 1 sub get coeffs j get rsalog i get rsprod xor put
} for
coeffs 0 coeffs 0 get rsalog i get rsprod put
} for
/coeffs coeffs 0 coeffs length 1 sub getinterval def
% Calculate the error correction codewords for each block
0 1 cwbs length 1 sub {
/i exch def
/cwb cwbs i get def
/ecb ecbs i get def
0 1 cwb length 1 sub {
/t exch cwb exch get ecb 0 get xor def
ecb length 1 sub -1 0 {
/j exch def
/p ecb length j sub 1 sub def
ecb p t coeffs j get rsprod put
j 0 gt { ecb p ecb p 1 add get ecb p get xor put } if
} for
} for
} for
% Strange fixup for 144x144 symbols
ncws 1558 eq {
/ecbs [ ecbs 8 2 getinterval {} forall ecbs 0 8 getinterval {} forall ] def
} if
% Extend codewords with the interleaved error correction codes
/cws [ cws {} forall rscw {0} repeat ] def
0 1 rscw 1 sub {
/i exch def
cws ncws i add ecbs i rsbl mod get i rsbl idiv get put
} for
% Place the codewords into the matrix
/module {
% cw row col shape
4 -1 roll
(00000000) 8 string copy dup 3 -1 roll 2 8 string cvrs
dup length 8 exch sub exch putinterval
7 -1 0 {1 index exch get 48 sub exch} for pop
11 -3 roll
% 1 2 4 8 16 32 64 128 row col shape
{ % Place each bit of the codeword in the shape, MSB first
exec
1 index 0 lt {
exch mrows add exch
4 mrows 4 add 8 mod sub add
} if
dup 0 lt {
mcols add exch
4 mcols 4 add 8 mod sub add exch
} if
exch mcols mul add
mmat exch 5 -1 roll put
} forall
} bind def
% Placement patterns
/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
] def
/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
] def
/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
] def
/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
] def
/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
] def
/mmat [ mrows mcols mul {-1} repeat ] def
cws length 1 sub -1 0 {cws exch get} for % Reverse cws onto stack
4 0 % Starting row and column
{ % loop
2 copy 0 eq exch mrows eq and {
dmc1 module
} if
2 copy 0 eq exch mrows 2 sub eq and mcols 4 mod 0 ne and {
dmc2 module
} if
2 copy 0 eq exch mrows 2 sub eq and mcols 8 mod 4 eq and {
dmc3 module
} if
2 copy 2 eq exch mrows 4 add eq and mcols 8 mod 0 eq and {
dmc4 module
} if
{ % loop for sweeping upwards
2 copy 0 ge exch mrows lt and {
2 copy exch mcols mul add mmat exch get -1 eq {
dmn module
} if
} if
2 add exch 2 sub exch
2 copy mcols lt exch 0 ge and not {exit} if
} loop
3 add exch 1 add exch
{ % loop for sweeping downwards
2 copy mcols lt exch 0 ge and {
2 copy exch mcols mul add mmat exch get -1 eq {
dmn module
} if
} if
2 sub exch 2 add exch
2 copy 0 ge exch mrows lt and not {exit} if
} loop
1 add exch 3 add exch
2 copy mcols lt exch mrows lt or not {pop pop exit} if
} loop
% Set checker pattern if required
mmat mrows mcols mul 1 sub get -1 eq {
mmat mrows mcols 1 sub mul 2 sub [1 0] putinterval
mmat mrows mcols mul 2 sub [0 1] putinterval
} if
% Place the modules onto a pixel map between alignment patterns
/pixs rows cols mul array def
/cwpos 0 def
0 1 rows 1 sub {
/i exch def
i rrows 2 add mod 0 eq { pixs i cols mul [ cols 2 idiv {1 0} repeat ] putinterval } if
i rrows 2 add mod rrows 1 add eq { pixs i cols mul [ cols {1} repeat ] putinterval } if
i rrows 2 add mod 0 ne i rrows 2 add mod rrows 1 add ne and {
0 1 cols 1 sub {
/j exch def
j rcols 2 add mod 0 eq { pixs i cols mul j add 1 put } if
j rcols 2 add mod rcols 1 add eq { pixs i cols mul j add i 2 mod put } if
j rcols 2 add mod 0 ne j rcols 2 add mod rcols 1 add ne and {
pixs i cols mul j add mmat cwpos get put
/cwpos cwpos 1 add def
} if
} for
} if
} for
% Return the arguments
<<
/ren //renmatrix
/pixs pixs
/pixx cols
/pixy rows
/height rows 2 mul 72 div
/width cols 2 mul 72 div
/opt options
>>
dontdraw not //renmatrix if
end
} bind def
/datamatrix dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
% --END ENCODER datamatrix--