11% Barcode Writer in Pure PostScript
22% https://bwipp.terryburton.co.uk
33%
4- % Copyright (c) 2004-2024 Terry Burton
4+ % Copyright (c) 2004-2025 Terry Burton
55%
66% $Id$
77%
3838currentglobal
3939true setglobal
4040/setpacking where {pop currentpacking true setpacking} if
41- 10 dict
42- dup /loadctx dup /uk.co.terryburton.bwipp findresource put
43- dup /unloadctx dup /uk.co.terryburton.bwipp findresource put
41+ 50 dict
4442dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
4543dup /processoptions dup /uk.co.terryburton.bwipp findresource put
4644dup /renlinear dup /uk.co.terryburton.bwipp findresource put
4745begin
46+
47+ /ean5.encs [
48+ (3211) (2221) (2122) (1411) (1132)
49+ (1231) (1114) (1312) (1213) (3112)
50+ (112) (11)
51+ ] readonly def
52+
53+ /ean5.barchars (0123456789) readonly def
54+
55+ /ean5.mirrormaps [
56+ (11000) (10100) (10010) (10001) (01100)
57+ (00110) (00011) (01010) (01001) (00101)
58+ ] readonly def
59+
4860/ean5 {
4961
5062 20 dict begin
5163
5264 /ctx null def
65+
5366 /dontdraw false def
5467 /includetext false def
5568 /textfont /Helvetica def
6578 /textyoffset height 72 mul 1 add def
6679 } if
6780
68- /ean5 //loadctx exec
69-
81+ %
7082 % Validate input
83+ %
7184 barcode length 5 ne {
7285 /bwipp.ean5badLength (EAN-5 add-on must be 5 digits) //raiseerror exec
7386 } if
7790 } if
7891 } forall
7992
80- {
81- % Create an array containing the character mappings
82- /encs
83- [ (3211) (2221) (2122) (1411) (1132)
84- (1231) (1114) (1312) (1213) (3112)
85- (112) (11)
86- ] def
87-
88- % Create a string of the available characters
89- /barchars (0123456789) def
90-
91- % Determine the mirror map based on mod 10 checksum
92- /mirrormaps
93- [ (11000) (10100) (10010) (10001) (01100)
94- (00110) (00011) (01010) (01001) (00101)
95- ] def
96- } ctxdef
97-
9893 /checksum 0 def
9994 0 1 4 {
10095 /i exch def
@@ -106,28 +101,32 @@ begin
106101 } ifelse
107102 } for
108103 /checksum checksum 10 mod def
109- /mirrormap mirrormaps checksum get def
104+ /mirrormap //ean5. mirrormaps checksum get def
110105
111106 /sbs 31 string def
112107 /txt 5 array def
113108
114109 0 1 4 {
115110 /i exch def
116111
112+ %
117113 % Prefix with either a start character or separator character
114+ %
118115 i 0 eq {
119- sbs 0 encs 10 get putinterval
116+ sbs 0 //ean5. encs 10 get putinterval
120117 } {
121- sbs i 1 sub 6 mul 7 add encs 11 get putinterval
118+ sbs i 1 sub 6 mul 7 add //ean5. encs 11 get putinterval
122119 } ifelse
123120
121+ %
124122 % Lookup the encoding for the barcode character
125- barcode i 1 getinterval barchars exch search
126- pop % Discard true leaving pre
127- length /indx exch def % indx is the length of pre
128- pop pop % Discard seek and post
129- /enc encs indx get def % Get the indxth encoding
130- mirrormap i get 49 eq { % Reverse enc if 1 in this position in mirrormap
123+ %
124+ barcode i 1 getinterval //ean5.barchars exch search
125+ pop
126+ length /indx exch def
127+ pop pop
128+ /enc //ean5.encs indx get def
129+ mirrormap i get 49 eq {
131130 /enclen enc length def
132131 /revenc enclen string def
133132 0 1 enclen 1 sub {
@@ -141,12 +140,11 @@ begin
141140 txt i [barcode i 1 getinterval i 1 sub 9 mul 10 add textxoffset add textyoffset textfont textsize] put
142141 } for
143142
144- % Return the arguments
145143 <<
146144 /ren /renlinear
147145 /sbs [sbs {48 sub} forall]
148- /bhs [16{height}repeat]
149- /bbs [16{0}repeat]
146+ /bhs [16 {height} repeat]
147+ /bbs [16 {0} repeat]
150148 includetext {
151149 /txt txt
152150 } if
@@ -160,8 +158,6 @@ begin
160158
161159 dontdraw not //renlinear if
162160
163- //unloadctx exec
164-
165161 end
166162
167163}
0 commit comments