-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathposicode.ps.src
433 lines (396 loc) · 14.6 KB
/
posicode.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
% 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 posicode--
% --REQUIRES preamble loadctx unloadctx raiseerror processoptions parseinput renlinear--
% --DESC: PosiCode
% --EXAM: ABC123
% --EXOP: version=b inkspread=-0.5 parsefnc includetext
% --RNDR: renlinear
/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 /renlinear dup /uk.co.terryburton.bwipp findresource put
begin
/posicode {
20 dict begin % Confine variables to local scope
/ctx null def
/dontdraw false def
/includetext false def % Enable/disable text
/textfont /Courier def
/textsize 10.0 def
/textxoffset 0.0 def
/textyoffset -7.0 def
/height 1.0 def
/encoding (auto) def
/version (a) def
/checkoffset 0 def
/raw false def
/parse false def
/parsefnc false def
//processoptions exec /options exch def
/barcode exch def
/posicode //loadctx exec
{
% Special function characters
/la0 -1 def /la1 -2 def /la2 -3 def
/sf0 -4 def /sf1 -5 def /sf2 -6 def
/fn1 -7 def /fn2 -8 def /fn3 -9 def /fn4 -10 def
% Character maps for each state
/charmapsnormal [
% 0 1 2 0 1 2 0 1 2
[ (0) (^) (') ] [ (1) (;) 27 ] [ (2) (<) 28 ] % 0-2
[ (3) (=) 29 ] [ (4) (>) 30 ] [ (5) (?) 31 ] % 3-5
[ (6) (@) (!) ] [ (7) ([) (") ] [ (8) 92 (#) ] % 6-8
[ (9) (]) (&) ] [ (A) (a) 1 ] [ (B) (b) 2 ] % 9-11
[ (C) (c) 3 ] [ (D) (d) 4 ] [ (E) (e) 5 ] % 12-14
[ (F) (f) 6 ] [ (G) (g) 7 ] [ (H) (h) 8 ] % 15-17
[ (I) (i) 9 ] [ (J) (j) 10 ] [ (K) (k) 11 ] % 18-20
[ (L) (l) 12 ] [ (M) (m) 13 ] [ (N) (n) 14 ] % 21-23
[ (O) (o) 15 ] [ (P) (p) 16 ] [ (Q) (q) 17 ] % 24-26
[ (R) (r) 18 ] [ (S) (s) 19 ] [ (T) (t) 20 ] % 27-29
[ (U) (u) 21 ] [ (V) (v) 22 ] [ (W) (w) 23 ] % 30-32
[ (X) (x) 24 ] [ (Y) (y) 25 ] [ (Z) (z) 26 ] % 33-35
[ (-) (_) 40 ] [ (.) (`) 41 ] [ ( ) 127 0 ] % 36-38
[ ($) ({) (*) ] [ (/) (|) (,) ] [ (+) (}) (:) ] % 39-41
[ (%) (~) fn1 ] [ la1 la0 fn2 ] [ sf1 sf0 fn3 ] % 42-44
[ sf2 sf2 fn4 ] % 45
] def
/chapmapslimited [
[ (0) -98 -98 ] [ (1) -98 -98 ] [ (2) -98 -98 ] % 0-2
[ (3) -98 -98 ] [ (4) -98 -98 ] [ (5) -98 -98 ] % 3-5
[ (6) -98 -98 ] [ (7) -98 -98 ] [ (8) -98 -98 ] % 6-8
[ (9) -98 -98 ] [ (A) -98 -98 ] [ (B) -98 -98 ] % 9-11
[ (C) -98 -98 ] [ (D) -98 -98 ] [ (E) -98 -98 ] % 12-14
[ (F) -98 -98 ] [ (G) -98 -98 ] [ (H) -98 -98 ] % 15-17
[ (I) -98 -98 ] [ (J) -98 -98 ] [ (K) -98 -98 ] % 18-20
[ (L) -98 -98 ] [ (M) -98 -98 ] [ (N) -98 -98 ] % 21-23
[ (O) -98 -98 ] [ (P) -98 -98 ] [ (Q) -98 -98 ] % 24-26
[ (R) -98 -98 ] [ (S) -98 -98 ] [ (T) -98 -98 ] % 27-29
[ (U) -98 -98 ] [ (V) -98 -98 ] [ (W) -98 -98 ] % 30-32
[ (X) -98 -98 ] [ (Y) -98 -98 ] [ (Z) -98 -98 ] % 33-35
[ (-) -98 -98 ] [ (.) -98 -98 ] % 36-37
] def
} ctxdef
version (a) ne version (b) ne and version (limiteda) ne and version (limitedb) ne and {
/bwipp.posicodeInvalidVersion (The version must be either a, b, limiteda or limitedb) //raiseerror exec
} if
/charmaps version (a) eq version (b) eq or {charmapsnormal} {chapmapslimited} ifelse def
% Invert charmaps to give character to value maps for each state
/charvals [
charmaps length dict charmaps length dict charmaps length 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
/set0 charvals 0 get def
/set1 charvals 1 get def
/set2 charvals 2 get def
% Validate the input if limited
version (limiteda) eq version (limitedb) eq or {
0 1 barcode length 1 sub {
barcode exch 1 getinterval 0 get set0 exch known not {
/bwipp.posicodeBadCharacter (Posicode limited must contain only digits, capital letters, and the symbols - and .) //raiseerror exec
} if
} for
} if
raw {/encoding (raw) def} if
encoding (raw) eq {
/cws barcode length array def
/i 0 def /j 0 def
{ % loop
i barcode length 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
/text () def
} if
encoding (auto) eq {
% Parse the input
/fncvals <<
/parse parse
/parsefnc parsefnc
(FNC1) fn1
(FNC2) fn2
(FNC3) fn3
% (FNC4) fn4 Not user accessible as encoded automatically
>> def
/msg barcode fncvals //parseinput exec def
/msglen msg length def
/text msglen string def
0 1 msglen 1 sub {
/i exch def
text i msg i get dup 0 lt { pop 32 } if put
} for
% Standard and extended ASCII runlength at position
/numSA [ msglen {0} repeat 0 ] def
/numEA [ msglen {0} repeat 0 ] def
msglen 1 sub -1 0 {
/i exch def
msg i get 0 ge {
msg i get 128 ge {
numEA i numEA i 1 add get 1 add put
} {
numSA i numSA i 1 add get 1 add put
} ifelse
} if
} for
% FNC4 codeword insertion for extended ASCII
/ea false def /msgtmp [] def
0 1 msglen 1 sub {
/i exch def
/c msg i get def
ea c 128 lt xor not c 0 ge and { % Other mode required
ea {numSA} {numEA} ifelse i get dup % Runlength of other mode
i add msglen eq {3} {5} ifelse % Does run terminate symbol
lt { % Shift
/msgtmp [ msgtmp aload pop fn4 ] def
} { % Latch
/msgtmp [ msgtmp aload pop fn4 fn4 ] def
/ea ea not def
} ifelse
} if
/msgtmp [ msgtmp aload pop c 0 ge {c 127 and} {c} ifelse ] def
} for
/msg msgtmp def
/msglen msg length def
% Convert the input message to codewords
/enc {
exch get cws exch j exch put
/j j 1 add def
} def
/cws msglen 2 mul array def
/i 0 def /j 0 def /cset (set0) def
{
i msglen eq {exit} if
{ % common exit
% Read next two characters
/char1 msg i get def
/char2 i 1 add msglen lt {msg i 1 add get} {-99} ifelse def
% If current mode is sufficient then directly encode
cset load char1 known {
char1 cset load enc
/i i 1 add def
exit
} if
% If char1 is in set2 then shift encode
set2 char1 known {
sf2 cset load enc
char1 set2 enc
/i i 1 add def
exit
} if
% If char2 is also not in this set then latch else shift encode
cset load char2 known not {
cset (set0) eq {
la1 cset load enc
/cset (set1) def
} {
la0 cset load enc
/cset (set0) def
} ifelse
exit
} {
cset (set0) eq {
sf1 cset load enc
char1 set1 enc
} {
sf0 cset load enc
char1 set0 enc
} ifelse
/i i 1 add def
exit
} ifelse
exit
} loop
} loop
/cws cws 0 j getinterval def
} if % auto encoding
% Create an array containing the character mappings
{
/encmaps << % ";"=11, "<"=12
/a
[ (141112) (131212) (121312) (111412) (131113)
(121213) (111313) (121114) (111214) (111115)
(181111) (171211) (161311) (151411) (141511)
(131611) (121711) (111811) (171112) (161212)
(151312) (141412) (131512) (121612) (111712)
(161113) (151213) (141313) (131413) (121513)
(111613) (151114) (141214) (131314) (121414)
(111514) (141115) (131215) (121315) (111415)
(131116) (121216) (111316) (121117) (111217)
(111118) (1<111112) (111111111;1)
]
/b
[ (151213) (141313) (131413) (121513) (141214)
(131314) (121414) (131215) (121315) (121216)
(191212) (181312) (171412) (161512) (151612)
(141712) (131812) (121912) (181213) (171313)
(161413) (151513) (141613) (131713) (121813)
(171214) (161314) (151414) (141514) (131614)
(121714) (161215) (151315) (141415) (131515)
(121615) (151216) (141316) (131416) (121516)
(141217) (131317) (121417) (131218) (121318)
(121219) (1<121312) (121212121<1)
]
/limiteda
[ (111411) (111312) (111213) (111114) (121311)
(121212) (121113) (141111) (131211) (131112)
(171111) (161211) (151311) (141411) (131511)
(121611) (111711) (161112) (151212) (141312)
(131412) (121512) (111612) (151113) (141213)
(131313) (121413) (111513) (141114) (131214)
(121314) (111414) (131115) (121215) (111315)
(121116) (111216) (111117) (151111) (1)
]
/limitedb
[ (121512) (121413) (121314) (121215) (131412)
(131313) (131214) (151212) (141312) (141213)
(181212) (171312) (161412) (151512) (141612)
(131712) (121812) (171213) (161313) (151413)
(141513) (131613) (121713) (161214) (151314)
(141414) (131514) (121614) (151215) (141315)
(131415) (121515) (141216) (131316) (121416)
(131217) (121317) (121218) (141212) (1)
]
>> def
} ctxdef
encmaps version get /encs exch def
{
/c2w [
[ 495 330 210 126 70 35 15 5 ]
[ 165 120 84 56 35 20 10 4 ]
[ 45 36 28 21 15 10 6 3 ]
[ 9 8 7 6 5 4 3 2 ]
[ 1 1 1 1 1 1 1 1 ]
] def
} ctxdef
/v 0 def
cws {
/cw exch def
6 {
cw v xor 1 and 0 ne {
/v v 7682 xor def
} if
/v v -1 bitshift def
/cw cw -1 bitshift def
} repeat
} forall
/v v checkoffset add def
version (limiteda) eq version (limitedb) eq or {
/v v 1023 and def
v 824 gt v 853 lt and {
/v v 292 add def
} if
} {
/v v 1023 and 45 add def
} ifelse
/d [ 2 2 2 2 2 2 ] def
/r 0 def /c 0 def /w 0 def /sum 0 def
{
sum v eq {exit} if
/t sum c2w r get c get add def
t v eq {
/w w 1 add def
d r w 2 add put
/sum t def
} if
t v gt {
d r w 2 add put
/r r 1 add def
/w 0 def
} if
t v lt {
/c c 1 add def
/w w 1 add def
/sum t def
} if
} loop
20 0 1 4 {d exch get sub} for
d exch 5 exch put
version (b) eq version (limitedb) eq or {
/d [ d {1 add} forall ] def
} if
/cbs (111111111111) 12 string copy def
5 -1 0 {
/i exch def
cbs 5 i sub 2 mul 1 add d i get 47 add put
} for
% Concatenate the space bar successions
/sbs cws length 6 mul 31 add string def
encs encs length 2 sub get dup
sbs exch 0 exch putinterval
length /j exch def
0 1 cws length 1 sub {
/i exch def
sbs j encs cws i get get putinterval
/j j 6 add def
} for
sbs j cbs putinterval
/j j 12 add def
encs encs length 1 sub get dup
sbs exch j exch putinterval
length j add /j exch def
/sbs sbs 0 j getinterval def
% Return the arguments
<<
/ren /renlinear
/sbs [sbs {48 sub} forall]
/bhs [sbs length 1 add 2 idiv {height} repeat]
/bbs [sbs length 1 add 2 idiv {0} repeat]
/txt [ [text textxoffset textyoffset textfont textsize] ]
/textxalign (center)
/borderleft << /a 0.0 /b 0.0 /limiteda 12.0 /limitedb 13.0 >> version get
/borderright << /a 0.0 /b 0.0 /limiteda 12.0 /limitedb 13.0 >> version get
/opt options
>>
dontdraw not //renlinear if
//unloadctx exec
end
}
[/barcode] {null def} forall
bind def
/posicode dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
% --END ENCODER posicode--