-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcodablockf.ps.src
573 lines (522 loc) · 19.4 KB
/
codablockf.ps.src
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
% Barcode Writer in Pure PostScript
% https://bwipp.terryburton.co.uk
%
% Copyright (c) 2004-2024 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 codablockf--
% --REQUIRES preamble loadctx unloadctx raiseerror processoptions parseinput renmatrix--
% --DESC: Codablock F
% --EXAM: CODABLOCK F 34567890123456789010040digit
% --EXOP: columns=8
% --RNDR: renmatrix
/setpacking where {pop currentpacking true setpacking} if
10 dict
dup /loadctx dup /uk.co.terryburton.bwipp findresource put
dup /unloadctx dup /uk.co.terryburton.bwipp findresource put
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 /renmatrix dup /uk.co.terryburton.bwipp findresource put
begin
/codablockf {
20 dict begin
/ctx null def
/dontdraw false def
/rows -1 def
/columns 8 def
/rowheight 10 def
/sepheight 1 def
/parse false def
/parsefnc false def
//processoptions exec /options exch def
/barcode exch def
columns 4 lt columns 62 gt or {
/bwipp.codablockfBadColumns (Codablock F must have 4 to 62 columns) //raiseerror exec
} if
rows -1 ne rows 2 lt rows 44 gt or and {
/bwipp.codablockfBadRows (Codablock F must have 2 to 44 rows) //raiseerror exec
} if
rowheight 8 lt {
/bwipp.codablockfBadRowHeight (Codablock F must have rowheight of at least 8) //raiseerror exec
} if
sepheight 1 lt {
/bwipp.codablockfBadSepHeight (Codablock F must have sepheight of at least 1) //raiseerror exec
} if
/codablockf //loadctx exec
/c columns 4 ge columns 62 le and {columns} {8} ifelse def
/rows rows 2 ge rows 44 le and {rows} {-1} ifelse def
% Convert input into bytes accounting for FNC characters
/swa -1 def /swb -2 def /swc -3 def /sft -4 def
/fn1 -5 def /fn2 -6 def /fn3 -7 def /fn4 -8 def
/sta -9 def /stp -10 def
% Parse the input
/fncvals <<
/parse parse
/parsefnc parsefnc
(FNC1) fn1
% (FNC2) fn2 Not permitted for Codablock F
(FNC3) fn3
% (FNC4) fn4 Not user accessible as encoded automatically
>> def
/msg barcode fncvals //parseinput exec def
/msglen msg length def
% FNC4 codeword insertion for extended ASCII
/msgtmp [] def
msg {
/char exch def
/msgtmp [ msgtmp aload pop char 128 lt {char} {fn4 char 127 and} ifelse ] def
} forall
/kmsg msg def % Keep for k1/k2 check characters calculation
/msg msgtmp def
/msglen msg length def
% Character maps for each state
{
/charmaps [
% A B C A B C A B C
[ 32 32 (00) ] [ (!) (!) (01) ] [ (") (") (02) ] % 0-2
[ (#) (#) (03) ] [ ($) ($) (04) ] [ (%) (%) (05) ] % 3-5
[ (&) (&) (06) ] [ (') (') (07) ] [ 40 40 (08) ] % 6-8
[ 41 41 (09) ] [ (*) (*) (10) ] [ (+) (+) (11) ] % 9-11
[ (,) (,) (12) ] [ (-) (-) (13) ] [ (.) (.) (14) ] % 12-14
[ (/) (/) (15) ] [ (0) (0) (16) ] [ (1) (1) (17) ] % 15-17
[ (2) (2) (18) ] [ (3) (3) (19) ] [ (4) (4) (20) ] % 18-20
[ (5) (5) (21) ] [ (6) (6) (22) ] [ (7) (7) (23) ] % 21-23
[ (8) (8) (24) ] [ (9) (9) (25) ] [ (:) (:) (26) ] % 24-26
[ (;) (;) (27) ] [ (<) (<) (28) ] [ (=) (=) (29) ] % 27-29
[ (>) (>) (30) ] [ (?) (?) (31) ] [ (@) (@) (32) ] % 30-32
[ (A) (A) (33) ] [ (B) (B) (34) ] [ (C) (C) (35) ] % 33-35
[ (D) (D) (36) ] [ (E) (E) (37) ] [ (F) (F) (38) ] % 36-38
[ (G) (G) (39) ] [ (H) (H) (40) ] [ (I) (I) (41) ] % 39-41
[ (J) (J) (42) ] [ (K) (K) (43) ] [ (L) (L) (44) ] % 42-44
[ (M) (M) (45) ] [ (N) (N) (46) ] [ (O) (O) (47) ] % 45-47
[ (P) (P) (48) ] [ (Q) (Q) (49) ] [ (R) (R) (50) ] % 48-50
[ (S) (S) (51) ] [ (T) (T) (52) ] [ (U) (U) (53) ] % 51-53
[ (V) (V) (54) ] [ (W) (W) (55) ] [ (X) (X) (56) ] % 54-56
[ (Y) (Y) (57) ] [ (Z) (Z) (58) ] [ ([) ([) (59) ] % 57-59
[ 92 92 (60) ] [ (]) (]) (61) ] [ (^) (^) (62) ] % 60-62
[ (_) (_) (63) ] [ 0 (`) (64) ] [ 1 (a) (65) ] % 63-65
[ 2 (b) (66) ] [ 3 (c) (67) ] [ 4 (d) (68) ] % 66-68
[ 5 (e) (69) ] [ 6 (f) (70) ] [ 7 (g) (71) ] % 69-71
[ 8 (h) (72) ] [ 9 (i) (73) ] [ 10 (j) (74) ] % 72-74
[ 11 (k) (75) ] [ 12 (l) (76) ] [ 13 (m) (77) ] % 75-77
[ 14 (n) (78) ] [ 15 (o) (79) ] [ 16 (p) (80) ] % 78-80
[ 17 (q) (81) ] [ 18 (r) (82) ] [ 19 (s) (83) ] % 81-83
[ 20 (t) (84) ] [ 21 (u) (85) ] [ 22 (v) (86) ] % 84-86
[ 23 (w) (87) ] [ 24 (x) (88) ] [ 25 (y) (89) ] % 87-89
[ 26 (z) (90) ] [ 27 ({) (91) ] [ 28 (|) (92) ] % 90-92
[ 29 (}) (93) ] [ 30 (~) (94) ] [ 31 127 (95) ] % 93-95
[ fn3 fn3 (96) ] [ fn2 fn2 (97) ] [ sft sft (98) ] % 96-98
[ swc swc (99) ] [ swb fn4 swb ] [ fn4 swa swa ] % 99-101
[ fn1 fn1 fn1 ] [ sta sta sta ] [ stp stp stp ] % 102-104
] def
% Invert charmaps to give character to value maps for each state
/charvals [ 105 dict 105 dict 105 dict ] def
0 1 charmaps length 1 sub {
/i exch def
/encs charmaps i get def
0 1 2 {
/j exch def
encs j get dup type /stringtype eq {0 get} if % convert string to ASCII if required
charvals j get exch i put
} for
} for
/seta charvals 0 get def
/setb charvals 1 get def
/setc charvals 2 get def
} ctxdef
% Determine digit runlength and characters from given position
/numsscr {
/n 0 def /s 0 def
/p exch def {
p msglen ge {exit} if
msg p get
dup setc exch known not {pop exit} if
fn1 eq {
% FNC1 in odd position of run like two digits
s 2 mod 0 eq {/s s 1 add def} {exit} ifelse
} if
/n n 1 add def
/s s 1 add def
/p p 1 add def
} loop
n s
} def
% Encoding for each alphabet
/enca {
seta exch get cws exch j exch put
/j j 1 add def
} def
/encb {
setb exch get cws exch j exch put
/j j 1 add def
} def
/encc {
dup type /arraytype ne {
setc exch get
} {
aload pop 48 sub exch 48 sub 10 mul add
} ifelse
cws exch j exch put
/j j 1 add def
} def
% Character exclusively in either alphabet A or B
/anotb {dup seta exch known exch setb exch known not and} def
/bnota {dup setb exch known exch seta exch known not and} def
% Pre-compute relative position of next anotb and next bnota characters
/nextanotb [ msg length {0} repeat 9999 ] def
/nextbnota [ msg length {0} repeat 9999 ] def
msg length 1 sub -1 0 {
/i exch def
msg i get anotb {
nextanotb i 0 put
} {
nextanotb i nextanotb i 1 add get 1 add put
} ifelse
msg i get bnota {
nextbnota i 0 put
} {
nextbnota i nextbnota i 1 add get 1 add put
} ifelse
} for
% Does a-only come before b-only after given position and vice versa
/abeforeb {dup nextanotb exch get exch nextbnota exch get lt} def
/bbeforea {dup nextbnota exch get exch nextanotb exch get lt} def
% Add padding to row
/padrow {
{
{ % common exit
cset (seta) eq {swc enca /cset (setc) def exit} if
cset (setb) eq {swc encb /cset (setc) def exit} if
cset (setc) eq {swb encc /cset (setb) def exit} if
} loop
} repeat
} def
% Encode a if fits in row, allowing for fn4 + possible shift, assuming rem ge 1
/encafitsrow {
rem 2 le msg i get fn4 eq and {
% If have 2 left and don't need to shift
rem 2 eq msg i 1 add get 95 le and dup { % Return false if not
fn4 enca
msg i 1 add get enca
/i i 2 add def
} if
} {
msg i get enca
/i i 1 add def
true
} ifelse
} def
% Encode b if fits in row, allowing for fn4 + possible shift, assuming rem ge 1
/encbfitsrow {
rem 2 le msg i get fn4 eq and {
% If have 2 left and don't need to shift
rem 2 eq msg i 1 add get 32 ge and dup { % Return false if not
fn4 encb
msg i 1 add get encb
/i i 2 add def
} if
} {
msg i get encb
/i i 1 add def
true
} ifelse
} def
% Convert message to codewords
/cws c 5 add 44 mul array def
/i 0 def /j 0 def /r 1 def
/lastrow false def {
lastrow {exit} if
r 44 gt {
/bwipp.codablockfTooBig (Maximum length exceeded) //raiseerror exec
} if
% Add start character and code subset selector
sta enca
i msglen lt {i numsscr} {-1 -1} ifelse /nums exch def /nchars exch def
{ % common exit
msglen 0 eq {
swb enca
/cset (setb) def
exit
} if
nums 2 ge {
swc enca
/cset (setc) def
exit
} if
i abeforeb {
sft enca
/cset (seta) def
exit
} if
swb enca
/cset (setb) def
exit
} loop
/j j 1 add def % Skip row indicator position
% Main encoding loop
/endofrow false def {
/rem c 3 add j c 5 add mod sub def % Codewords to end of row
i msglen eq endofrow or {exit} if
i numsscr /nums exch def /nchars exch def
/remnums nums rem 2 mul gt {rem 2 mul} {nums} ifelse def
% Determine switches and shifts
{ % common exit
cset (seta) eq cset (setb) eq or remnums 4 ge and
msg i get fn1 ne and {
remnums 2 mod 0 eq
rem 3 ge and {
swc cset (seta) eq {enca} {encb} ifelse
/cset (setc) def
2 {
msg i get fn1 eq {
fn1 encc
/i i 1 add def
} {
msg i 2 getinterval encc
/i i 2 add def
} ifelse
} repeat
exit
} if
remnums 2 mod 0 ne
rem 4 ge and {
msg i get cset (seta) eq {enca} {encb} ifelse
/i i 1 add def
swc cset (seta) eq {enca} {encb} ifelse
/cset (setc) def
2 {
msg i get fn1 eq {
fn1 encc
/i i 1 add def
} {
msg i 2 getinterval encc
/i i 2 add def
} ifelse
} repeat
exit
} if
} if
cset (setb) eq msg i get anotb and
rem 2 ge and {
i msglen 1 sub lt {
i 1 add bbeforea {
sft encb
msg i get enca
/i i 1 add def
exit
} if
} if
swa encb
/cset (seta) def
msg i get enca
/i i 1 add def
exit
} if
cset (seta) eq msg i get bnota and
rem 2 ge and {
i msglen 1 sub lt {
i 1 add abeforeb {
sft enca
msg i get encb
/i i 1 add def
exit
} if
} if
swb enca
/cset (setb) def
msg i get encb
/i i 1 add def
exit
} if
cset (setc) eq remnums 2 lt and
rem 2 ge and {
/rem rem 1 sub def
i abeforeb {
swa encc
/cset (seta) def
encafitsrow { exit } if
} {
swb encc
/cset (setb) def
encbfitsrow { exit } if
} ifelse
} if
% No switches or latches so try to encode
cset (seta) eq seta msg i get known and
rem 1 ge and {
encafitsrow { exit } if
} if
cset (setb) eq setb msg i get known and
rem 1 ge and {
encbfitsrow { exit } if
} if
cset (setc) eq remnums 2 ge and
rem 1 ge and {
msg i get fn1 eq {
fn1 encc
/i i 1 add def
} {
msg i 2 getinterval encc
/i i 2 add def
} ifelse
exit
} if
% Encoding does not fit in the row
/endofrow true def
exit
} loop % common exit
} loop
% Determine whether this is the final row
r rows ge rows -1 eq or r 1 gt and i msglen eq and rem 2 ge and {
rem 2 sub padrow
/j j 3 add def % Skip symbol and row checksum character positions
stp enca
/lastrow true def
} {
rem padrow
/j j 1 add def % Skip row checksum character positions
stp enca
/r r 1 add def
} ifelse
} loop
/cws cws 0 j getinterval def
% Maps for row indicators and symbol checksums
{
/abmap [
64 1 95 {} for
0 1 15 {} for
26 1 63 {} for
] def
/cmap [ 0 1 85 {} for ] def
} ctxdef
% Add symbol check characters to last row
/chkmsg kmsg length array def % Using kmsg (pre-fn4 insertion)
/j 0 def
0 1 kmsg length 1 sub {
/i exch def
/char kmsg i get def
char 0 ge {chkmsg j char put /j j 1 add def} if
char fn1 eq i 0 ne and {chkmsg j 29 put /j j 1 add def} if
} for
/t1 0 def /t2 0 def /k1 0 def /k2 0 def
0 1 j 1 sub {
/i exch def
/t1 chkmsg i get i mul 86 mod def
/t2 t1 chkmsg i get add 86 mod def
/k1 k1 t2 add 86 mod def
/k2 k2 t1 add 86 mod def
} for
cws cws length 4 sub cset (setc) ne {abmap} {cmap} ifelse k1 get put
cws cws length 3 sub cset (setc) ne {abmap} {cmap} ifelse k2 get put
% Populate row indicators
cws 2 2 copy 1 sub get 99 ne {abmap} {cmap} ifelse r 2 sub get put
1 1 r 1 sub {
/i exch def
cws i c 5 add mul 2 add 2 copy 1 sub get 99 ne {abmap} {cmap} ifelse i 42 add get put
} for
% Calculate row check characters
0 1 r 1 sub {
cws exch c 5 add mul c 4 add getinterval /rcws exch def
/csum rcws 0 get def
1 1 rcws length 2 sub {
/i exch def
/csum csum rcws i get i mul add def
} for
rcws rcws length 1 sub csum 103 mod put
} for
options /debugcws known { /bwipp.debugcws cws //raiseerror exec } if
% Create an array containing the character mappings
{
/encs
[ (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) (2331112)
] def
} ctxdef
% Derive the bits for each row
/rowbits r array def
0 1 r 1 sub {
/i exch def
/sbs [
cws i c 5 add mul c 5 add getinterval {
encs exch get {48 sub} forall
} forall
] def
mark
0 sbs {1 index 1 eq {{0}} {{1}} ifelse repeat} forall
counttomark 1 sub array astore exch pop exch pop
rowbits i 3 -1 roll put
} for
% Populate the bitmap
/symwid c 11 mul 57 add def
/pixs [
symwid sepheight mul {1} repeat
0 1 r 2 sub {
/i exch def
rowheight {rowbits i get aload pop} repeat
sepheight {
1 1 0 1 0 0 0 0 1 0 0
symwid 24 sub {1} repeat
1 1 0 0 0 1 1 1 0 1 0 1 1
} repeat
} for
rowheight {rowbits r 1 sub get aload pop} repeat
symwid sepheight mul {1} repeat
] def
% Return the arguments
<<
/ren /renmatrix
/pixs pixs
/pixx symwid
/pixy pixs length symwid idiv
/height pixs length symwid idiv 72 div
/width symwid 72 div
/opt options
>>
dontdraw not //renmatrix if
//unloadctx exec
end
}
[/barcode] {null def} forall
bind def
/codablockf dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
% --END ENCODER codablockf--