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
4745dup /ean2 dup /uk.co.terryburton.bwipp findresource put
4846dup /ean5 dup /uk.co.terryburton.bwipp findresource put
4947begin
48+
49+ /upce.encs [
50+ (3211) (2221) (2122) (1411) (1132)
51+ (1231) (1114) (1312) (1213) (3112)
52+ (111) (111111)
53+ ] readonly def
54+
55+ /upce.barchars (0123456789) readonly def
56+
57+ /upce.mirrormaps [
58+ (000111) (001011) (001101) (001110) (010011)
59+ (011001) (011100) (010101) (010110) (011010)
60+ ] readonly def
61+
5062/upce {
5163
5264 20 dict begin
5365
5466 /ctx null def
67+
5568 /dontdraw false def
5669 /includetext false def % Enable/disable text
5770 /textfont /Helvetica def
6881 //processoptions exec /options exch def
6982 /barcode exch def
7083
71- /upce //loadctx exec
72-
84+ %
7385 % Split off the addon
86+ %
7487 barcode ( ) search {
7588 /barcode exch def
7689 pop
8093 /addon () def
8194 } ifelse
8295
96+ %
8397 % Compress a given UPC-A
98+ %
8499 barcode length 11 eq barcode length 12 eq or {
100+ %
85101 % Validate the input
102+ %
86103 barcode {
87104 dup 48 lt exch 57 gt or {
88105 /bwipp.upcEupcAbadCharacter (UPC-A must contain only digits) //raiseerror exec
@@ -121,7 +138,9 @@ begin
121138 /barcode upcecode def
122139 } if
123140
141+ %
124142 % Validate the input
143+ %
125144 barcode length 7 ne barcode length 8 ne and {
126145 /bwipp.upcEbadLength (UPC-E must be 7 or 8 digits) //raiseerror exec
127146 } if
@@ -134,29 +153,16 @@ begin
134153 /bwipp.upcEbadAddOnLength (Add-on for UPC-E must be 2 or 5 digits) //raiseerror exec
135154 } if
136155
156+ %
137157 % Ensure 0 or 1 number systems
158+ %
138159 barcode 0 get dup 48 ne exch 49 ne and {
139160 /bwipp.upcEbadNumberSystem (UPC-E must have number system 0 or 1) //raiseerror exec
140161 } if
141162
142- {
143- % Create an array containing the character mappings
144- /encs
145- [ (3211) (2221) (2122) (1411) (1132)
146- (1231) (1114) (1312) (1213) (3112)
147- (111) (111111)
148- ] def
149-
150- % Create a string of the available characters
151- /barchars (0123456789) def
152-
153- /mirrormaps
154- [ (000111) (001011) (001101) (001110) (010011)
155- (011001) (011100) (010101) (010110) (011010)
156- ] def
157- } ctxdef
158-
163+ %
159164 % Derive the equivalent UPC-A for its checksum
165+ %
160166 1 { % Common exit
161167 /upcacode 11 string def
162168 barcode 6 get 48 sub 2 le {
@@ -208,10 +214,14 @@ begin
208214 /txt 8 array def
209215 txt 0 [barcode 0 1 getinterval textxoffset textyoffset textfont textsize 2 sub] put
210216
217+ %
211218 % Determine the mirror map based on checksum
212- /mirrormap mirrormaps checksum get def
219+ %
220+ /mirrormap //upce.mirrormaps checksum get def
213221
222+ %
214223 % Invert the mirrormap if we are using a non-zero number system
224+ %
215225 barcode 0 get 48 eq {
216226 /invt mirrormap length string def
217227 0 1 mirrormap length 1 sub {
@@ -227,17 +237,18 @@ begin
227237
228238 /sbs 33 string def
229239
240+ %
230241 % Put the start character
231- sbs 0 encs 10 get putinterval
242+ %
243+ sbs 0 //upce.encs 10 get putinterval
232244
233245 1 1 6 {
234246 /i exch def
235- % Lookup the encoding for the each barcode character
236- barcode i 1 getinterval barchars exch search
247+ barcode i 1 getinterval //upce.barchars exch search
237248 pop % Discard true leaving pre
238249 length /indx exch def % indx is the length of pre
239250 pop pop % Discard seek and post
240- /enc encs indx get def % Get the indxth encoding
251+ /enc //upce. encs indx get def % Get the indxth encoding
241252 mirrormap i 1 sub get 49 eq { % Reverse enc if 1 in this position in mirrormap
242253 /enclen enc length def
243254 /revenc enclen string def
@@ -254,8 +265,10 @@ begin
254265
255266 txt 7 [barcode 7 1 getinterval 6 7 mul 21 add textxoffset add textyoffset textfont textsize 2 sub] put
256267
268+ %
257269 % Put the end character
258- sbs 27 encs 11 get putinterval
270+ %
271+ sbs 27 //upce.encs 11 get putinterval
259272
260273 /sbs [sbs {48 sub} forall] def
261274 includetext {
@@ -268,7 +281,9 @@ begin
268281 } ifelse
269282 /guardrightypos 0.0 def
270283
284+ %
271285 % Append the addon
286+ %
272287 addon () ne {
273288 /addopts <<
274289 /dontdraw true
@@ -289,7 +304,6 @@ begin
289304 /guardrightypos height 72 mul 6 sub def
290305 } if
291306
292- % Return the arguments
293307 <<
294308 /ren /renlinear
295309 /sbs sbs
@@ -307,8 +321,6 @@ begin
307321
308322 dontdraw not //renlinear if
309323
310- //unloadctx exec
311-
312324 end
313325
314326}
0 commit comments