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+ /postnet.barchars (0123456789) readonly def
48+
49+ /postnet.encs [
50+ (55222) (22255) (22525) (22552) (25225)
51+ (25252) (25522) (52225) (52252) (52522)
52+ (5) (5)
53+ ] readonly def
54+
4855/postnet {
4956
5057 20 dict begin
5158
5259 /ctx null def
60+
5361 /dontdraw false def
5462 /includetext false def % Enable/disable text
5563 /validatecheck false def
@@ -62,11 +70,11 @@ begin
6270 //processoptions exec /options exch def
6371 /barcode exch def
6472
65- /postnet //loadctx exec
66-
6773 /barlen barcode length validatecheck {1 sub} if def
6874
75+ %
6976 % Validate the input
77+ %
7078 barlen 5 ne barlen 9 ne and barlen 11 ne and {
7179 /bwipp.postnetBadLength (USPS POSTNET must be 5, 9 or 11 digits excluding check digit) //raiseerror exec
7280 } if
@@ -76,26 +84,14 @@ begin
7684 } if
7785 } forall
7886
79- {
80- % Create a string of the available characters
81- /barchars (0123456789) def
82-
83- % Create an array containing the character mappings
84- /encs
85- [ (55222) (22255) (22525) (22552) (25225)
86- (25252) (25522) (52225) (52252) (52522)
87- (5) (5)
88- ] def
89- } ctxdef
90-
9187 /checksum 0 def
9288 0 1 barlen 1 sub {
9389 /i exch def
9490 /checksum checksum barcode i get 48 sub add def
9591 } for
9692 /checksum 10 checksum 10 mod sub 10 mod def
9793 validatecheck {
98- barcode barlen get barchars checksum get ne {
94+ barcode barlen get //postnet. barchars checksum get ne {
9995 /bwipp.postnetBadCheckDigit (Incorrect USPS POSTNET check digit provided) //raiseerror exec
10096 } if
10197 /barcode barcode 0 barlen getinterval def
@@ -104,8 +100,10 @@ begin
104100 /bhs barlen 5 mul 7 add array def
105101 /txt barlen 1 add array def
106102
103+ %
107104 % Put start character
108- /enc encs 10 get def
105+ %
106+ /enc //postnet.encs 10 get def
109107 /heights enc length array def
110108 0 1 enc length 1 sub {
111109 /j exch def
@@ -115,7 +113,7 @@ begin
115113
116114 0 1 barlen 1 sub {
117115 /i exch def
118- /enc encs barcode i get 48 sub get def
116+ /enc //postnet. encs barcode i get 48 sub get def
119117 /heights enc length array def
120118 0 1 enc length 1 sub {
121119 /j exch def
@@ -125,8 +123,10 @@ begin
125123 txt i [barcode i 1 getinterval i 5 mul 1 add 3.312 mul textyoffset textfont textsize] put
126124 } for
127125
126+ %
128127 % Put the checksum character
129- /enc encs checksum get def
128+ %
129+ /enc //postnet.encs checksum get def
130130 /heights enc length array def
131131 0 1 enc length 1 sub {
132132 /j exch def
@@ -135,21 +135,22 @@ begin
135135 bhs barlen 5 mul 1 add heights putinterval
136136
137137 includecheckintext {
138- txt barlen [barchars checksum 1 getinterval barlen 5 mul 1 add 3.312 mul textyoffset textfont textsize] put
138+ txt barlen [//postnet. barchars checksum 1 getinterval barlen 5 mul 1 add 3.312 mul textyoffset textfont textsize] put
139139 } {
140140 txt barlen [( ) barlen 5 mul 1 add 72 mul 25 div textyoffset textfont textsize] put
141141 } ifelse
142142
143+ %
143144 % Put end character
144- /enc encs 11 get def
145+ %
146+ /enc //postnet.encs 11 get def
145147 /heights enc length array def
146148 0 1 enc length 1 sub {
147149 /j exch def
148150 heights j enc j 1 getinterval cvi height mul 5 div put
149151 } for
150152 bhs barlen 5 mul 6 add heights putinterval
151153
152- % Return the arguments
153154 <<
154155 /ren /renlinear
155156 /bhs bhs
@@ -163,8 +164,6 @@ begin
163164
164165 dontdraw not //renlinear if
165166
166- //unloadctx exec
167-
168167 end
169168
170169}
0 commit comments