Skip to content

Commit e9107de

Browse files
committed
upca: Uplift
1 parent 5a8ca45 commit e9107de

1 file changed

Lines changed: 41 additions & 30 deletions

File tree

src/upca.ps.src

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
%
@@ -38,20 +38,28 @@
3838
currentglobal
3939
true 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
4442
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
4543
dup /processoptions dup /uk.co.terryburton.bwipp findresource put
4644
dup /renlinear dup /uk.co.terryburton.bwipp findresource put
4745
dup /ean2 dup /uk.co.terryburton.bwipp findresource put
4846
dup /ean5 dup /uk.co.terryburton.bwipp findresource put
4947
begin
48+
49+
/upca.encs [
50+
(3211) (2221) (2122) (1411) (1132)
51+
(1231) (1114) (1312) (1213) (3112)
52+
(111) (11111) (111)
53+
] readonly def
54+
55+
/upca.barchars (0123456789) readonly def
56+
5057
/upca {
5158

5259
20 dict begin
5360

5461
/ctx null def
62+
5563
/dontdraw false def
5664
/includetext false def
5765
/textfont /Helvetica def
@@ -68,9 +76,9 @@ begin
6876
//processoptions exec /options exch def
6977
/barcode exch def
7078

71-
/upca //loadctx exec
72-
79+
%
7380
% Split off the addon
81+
%
7482
barcode ( ) search {
7583
/barcode exch def
7684
pop
@@ -80,9 +88,13 @@ begin
8088
/addon () def
8189
} ifelse
8290

91+
%
8392
% Expand a given UPC-E
93+
%
8494
barcode length 7 eq barcode length 8 eq or {
95+
%
8596
% Validate the input
97+
%
8698
barcode {
8799
dup 48 lt exch 57 gt or {
88100
/bwipp.upcAupcEbadCharacter (UPC-E must contain only digits) //raiseerror exec
@@ -125,7 +137,9 @@ begin
125137
/barcode upcacode def
126138
} if
127139

140+
%
128141
% Validate the input
142+
%
129143
barcode length 11 ne barcode length 12 ne and {
130144
/bwipp.upcAbadLength (UPC-A must be 11 or 12 digits) //raiseerror exec
131145
} if
@@ -138,7 +152,9 @@ begin
138152
/bwipp.upcAbadAddOnLength (Add-on for UPC-A must be 2 or 5 digits) //raiseerror exec
139153
} if
140154

155+
%
141156
% Add checksum digit to barcode
157+
%
142158
/pad 12 string def % Create pad one bigger than barcode
143159
/checksum 0 def
144160
0 1 10 {
@@ -160,33 +176,24 @@ begin
160176
pad 11 checksum 48 add put % Put ascii for checksum at end of pad
161177
/barcode pad def
162178

163-
% Create an array containing the character mappings
164-
{
165-
/encs
166-
[ (3211) (2221) (2122) (1411) (1132)
167-
(1231) (1114) (1312) (1213) (3112)
168-
(111) (11111) (111)
169-
] def
170-
171-
% Create a string of the available characters
172-
/barchars (0123456789) def
173-
} ctxdef
174-
175179
/sbs 59 string def
176180
/txt 12 array def
177181

182+
%
178183
% Put the start character
179-
sbs 0 encs 10 get putinterval
184+
%
185+
sbs 0 //upca.encs 10 get putinterval
180186

187+
%
181188
% Left side
189+
%
182190
0 1 5 {
183-
% Lookup the encoding for the each barcode character
184191
/i exch def
185-
barcode i 1 getinterval barchars exch search
192+
barcode i 1 getinterval //upca.barchars exch search
186193
pop % Discard true leaving pre
187194
length /indx exch def % indx is the length of pre
188195
pop pop % Discard seek and post
189-
/enc encs indx get def % Get the indxth encoding
196+
/enc //upca.encs indx get def % Get the indxth encoding
190197
sbs i 4 mul 3 add enc putinterval % Put encoded digit into sbs
191198
i 0 eq { % First digit is before the guard bars
192199
txt 0 [barcode 0 1 getinterval textxoffset textyoffset textfont textsize 2 sub] put
@@ -195,18 +202,21 @@ begin
195202
} ifelse
196203
} for
197204

205+
%
198206
% Put the middle character
199-
sbs 6 4 mul 3 add encs 11 get putinterval
207+
%
208+
sbs 6 4 mul 3 add //upca.encs 11 get putinterval
200209

210+
%
201211
% Right side
212+
%
202213
6 1 11 {
203-
% Lookup the encoding for the each barcode character
204214
/i exch def
205-
barcode i 1 getinterval barchars exch search
215+
barcode i 1 getinterval //upca.barchars exch search
206216
pop % Discard true leaving pre
207217
length /indx exch def % indx is the length of pre
208218
pop pop % Discard seek and post
209-
/enc encs indx get def % Get the indxth encoding
219+
/enc //upca.encs indx get def % Get the indxth encoding
210220
sbs i 4 mul 8 add enc putinterval % Put encoded digit into sbs
211221
i 11 eq { % Last digit is after guard bars
212222
txt 11 [barcode 11 1 getinterval textxoffset 107 add textyoffset textfont textsize 2 sub] put
@@ -215,8 +225,10 @@ begin
215225
} ifelse
216226
} for
217227

228+
%
218229
% Put the end character
219-
sbs 56 encs 12 get putinterval
230+
%
231+
sbs 56 //upca.encs 12 get putinterval
220232

221233
/sbs [sbs {48 sub} forall] def
222234
includetext {
@@ -229,7 +241,9 @@ begin
229241
} ifelse
230242
/guardrightypos 0.0 def
231243

244+
%
232245
% Append the addon
246+
%
233247
addon () ne {
234248
/addopts <<
235249
/dontdraw true
@@ -250,7 +264,6 @@ begin
250264
/guardrightypos height 72 mul 6 sub def
251265
} if
252266

253-
% Return the arguments
254267
<<
255268
/ren /renlinear
256269
/sbs sbs
@@ -268,8 +281,6 @@ begin
268281

269282
dontdraw not //renlinear if
270283

271-
//unloadctx exec
272-
273284
end
274285

275286
}

0 commit comments

Comments
 (0)