🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.postscript
Article #71

Re: Zip Code "Bar Codes"

#71
From: Tod McQuillin
Date: Thu, 29 Oct 1992 23:45
189 lines
6536 bytes
robert@unlv.edu (Robert Cray) writes:
> Does anyone have postscript code that will generate the zip+4 bar code
> for a zip-code?

I don't know if you already have my version, but the one I originally
posted last year (which, much to my surprise was distributed as a
sample ghostscript file!) had a bug in it whereby zip codes beginning
with 0 were not printed.  Here is the new version with this bug fixed.
(Many thanks to Brent Carruth for pointing out my error.)

--- Cut Here ---
%!PS

% Postscript program to address a business-size envelope in accordance
% with US Postal Service guidelines, including the 52-bit POSTNET barcode
% Use of this program can help your mail arrive up to 2 days faster!
% Note: A barcode will only be printed if the last word of the last line of
%       the address is a 9-digit zipcode.

% by Tod McQuillin
% tm8t+@andrew.cmu.edu
% 10-Oct-1991
% Copyright (C) 1991 Tod McQuillin
% Revised 29-Oct-1992
% This is version 1.1, incorporating patches courtesy of Brent Carruth
% (BLCARR01@ulkyvx.louisville.edu)

% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 1, or (at your option)
% any later version.

% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.

/inch {72 mul} def
/cm {inch 2.54 div} def

% LWIDTH and LHEIGHT define letter size paper.  The envelope will be printed
% centered in this area, flush with the top of the page.  This works great
% with a HP III printer; your mileage may vary
/LWIDTH 8.5 inch def
/LHEIGHT 11 inch def

% EHEIGHT and EWIDTH define the com10 size envelope.  They're supposed to be
% 9.5 x 4.25 inches, but the ones I had were only 10.5 cm tall.  Change these
% values to suit your envelopes -- the program will work with any envelopes
% provided these values are set appropriately.
/EHEIGHT 10.5 cm def
/EWIDTH 9.5 inch def

/interline {            % define default leading
  POINTSIZE 1.2 mul
  } def

/fontset {              % fontname pointsize => -
  exch findfont exch dup /POINTSIZE exch def scalefont setfont
  } def

/defleading {           % define leading for nl
  /LEADING interline def
  } def

/nl {                   % print string and move to start of next line
  dup stringwidth pop exch show neg LEADING neg rmoveto
  } def

/addressfont {          % set font for address
  /gillsans 14 fontset defleading
  } def

/returnfont {           % set font for return address
  /gillsans 12 fontset defleading
  } def

/envelope {             % translate and rotate coordinate system to match com10
  LWIDTH EHEIGHT sub 2 div EHEIGHT add LHEIGHT EWIDTH sub translate
  90 rotate
  0 3 32 div inch translate % fudge factor for my printer - change or remove
  } def

/returnaddress {        % Print the return address
  gsave
    returnfont
    % This positions the return address .25 inches from upper left of envelope
    .25 inch EHEIGHT .25 inch sub POINTSIZE 3 64 div inch sub sub moveto
    {nl} forall
  grestore
  } def

/address {              % Print the address
  gsave
    addressfont
    4 inch 2.25 inch moveto     % Magic numbers conform to USPS guidelines
    dup                         % save the address
    {nl} forall
    dup length 1 sub get        % Get the last line of the address
    mark
      { exch dup /str exch def  % Tokenise the line and save remainder in str
        token not {exit} if
	/barcodestring str def
      } loop                    % Get the last word of the last line

    type cvlit /marktype ne {barcodestring barcode} if
    cleartomark
  grestore
  } def

/barcodebits [          % From "A Guide to Business Mail Preparation"
  (11000)       %0      % USPS Brochure/Pamphlet
  (00011)       %1
  (00101)       %2
  (00110)       %3
  (01001)       %4
  (01010)       %5
  (01100)       %6
  (10001)       %7
  (10010)       %8
  (10100)       %9
  ] def

/bit1 {                 % Draw a 1 bit of the POSTNET Barcode
  0 .125 inch rlineto
  1 inch 21 div .125 inch neg rmoveto
  } bind def

/bit0 {                 % Draw a 0 bit of the POSTNET Barcode
  0 .05 inch rlineto
  1 inch 21 div .05 inch neg rmoveto
  } bind def  

/dodigit {              % Do a complete digit of the barcode
  barcodebits exch get {
    2 mod 1 eq
      {bit1}
      {bit0}
    ifelse
    /BITS BITS 1 add def
    } forall
  } def

/barcode {              % Do a complete barcode
  gsave
    /CHECKSUM 0 def
    /BITS 0 def
    % More magic numbers to conform to USPS guidelines
    EWIDTH 3.9375 inch sub .25 inch moveto
    bit1
      {
      (0) 0 get sub dup dup 0 ge exch 9 le and {
        dup /CHECKSUM exch CHECKSUM add def
        dodigit
        } if
      } forall
    CHECKSUM 10 mod neg 10 add dup 10 eq {pop 0} if
    dodigit
    bit1
    BITS 50 eq {.02 inch setlinewidth stroke} if
% if you want 5 digit zip codes barcoded as well, make the previous line be
%   BITS 50 eq BITS 30 eq or {.02 inch setlinewidth stroke} if
% I don't think it's a good idea to barcode a 5-digit zip.  Kinda defeats
% the purpose.
  grestore
  } def

% Note that if you print your own barcode, you need not follow the address font
% guidelines since the only use of the OCR machines is to print barcodes.
  
/tod [                  % That's me!
  (Tod N. McQuillin)
  (25 Dilworth Street)
  (Apartment 2)
  (Pittsburgh, PA 15211-1913)
  ] def

/addressee [
  (Joe Random)
  (123 Anystreet)
  (Pittsburgh, PA  15212-3011)
  ] def

envelope
tod returnaddress
addressee address
showpage

%--- Cut Here ---
Technical Details
Message-ID: <Aew7SfK00YUo0crb9D@andrew.cmu.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!kithrup!hoptoad!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!eff!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!fs7.ece.cmu.edu!crabapple.srv.cs.cmu.edu!andrew.cmu.edu!tm8t+
References: <1992Oct23.173146.20984@unlv.edu>

Show Full Headers
Complete Article Headers
Organization: Carnegie Mellon, Pittsburgh, PA
Path: arc1-ca.pugleaf.net!sparky!uunet!kithrup!hoptoad!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!eff!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!fs7.ece.cmu.edu!crabapple.srv.cs.cmu.edu!andrew.cmu.edu!tm8t+
Newsgroups: comp.lang.postscript
Message-ID: <Aew7SfK00YUo0crb9D@andrew.cmu.edu>
Date: Thu, 29 Oct 1992 18:45:47 -0500 
From: Tod McQuillin <tm8t+@andrew.cmu.edu>
Subject: Re: Zip Code "Bar Codes"
In-Reply-To: <1992Oct23.173146.20984@unlv.edu>
References: <1992Oct23.173146.20984@unlv.edu>
Distribution: usa
Lines: 189