% zip.tex
% 
% J. Daniel Smith
% 24 May 1990
%
% Modified from a STY file snarfed from someplace else.  But I don't
% remember where the original code came from...  OOPS!!!
%
% Make a barcode for a ZIP code!
%
% Example usage is below.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\catcode`@=11    % letter
%
% Dimensions of bars to be used in the barcode
\newdimen\b@rwidth \newdimen\b@rlong \newdimen\b@rshort \newdimen\b@rsep
\b@rwidth=0.02true in \b@rlong=0.125true in \b@rshort=0.05true in
\b@rsep=0.0275true in
\def\sb@r{\vrule height\b@rshort width\b@rwidth depth0pt \kern\b@rsep}
\def\lb@r{\vrule height\b@rlong width\b@rwidth depth0pt \kern\b@rsep}

% The barcodes for each digit
\def\zerob@r{\lb@r\lb@r\sb@r\sb@r\sb@r}
\def\oneb@r{\sb@r\sb@r\sb@r\lb@r\lb@r}
\def\twob@r{\sb@r\sb@r\lb@r\sb@r\lb@r}
\def\threeb@r{\sb@r\sb@r\lb@r\lb@r\sb@r}
\def\fourb@r{\sb@r\lb@r\sb@r\sb@r\lb@r}
\def\fiveb@r{\sb@r\lb@r\sb@r\lb@r\sb@r}
\def\sixb@r{\sb@r\lb@r\lb@r\sb@r\sb@r}
\def\sevenb@r{\lb@r\sb@r\sb@r\sb@r\lb@r}
\def\eightb@r{\lb@r\sb@r\sb@r\lb@r\sb@r}
\def\nineb@r{\lb@r\sb@r\lb@r\sb@r\sb@r}

\newcount\zipb@rm
\newcount\zipb@rn
\chardef\ten=10

%Almost like using \ifcase... except `-' is ignored.
\def\zipb@r@@@#1#2{\expandafter\def\csname zipb@r@@#1\endcsname%
                   {#2\advance\zipb@rn#1\relax}}

\newbox\zipbarcode
\zipb@r@@@0{\global\setbox\zipbarcode\hbox{\box\zipbarcode\zerob@r}}
\zipb@r@@@1{\global\setbox\zipbarcode\hbox{\box\zipbarcode\oneb@r}}
\zipb@r@@@2{\global\setbox\zipbarcode\hbox{\box\zipbarcode\twob@r}}
\zipb@r@@@3{\global\setbox\zipbarcode\hbox{\box\zipbarcode\threeb@r}}
\zipb@r@@@4{\global\setbox\zipbarcode\hbox{\box\zipbarcode\fourb@r}}
\zipb@r@@@5{\global\setbox\zipbarcode\hbox{\box\zipbarcode\fiveb@r}}
\zipb@r@@@6{\global\setbox\zipbarcode\hbox{\box\zipbarcode\sixb@r}}
\zipb@r@@@7{\global\setbox\zipbarcode\hbox{\box\zipbarcode\sevenb@r}}
\zipb@r@@@8{\global\setbox\zipbarcode\hbox{\box\zipbarcode\eightb@r}}
\zipb@r@@@9{\global\setbox\zipbarcode\hbox{\box\zipbarcode\nineb@r}}

\def\zipb@r@@#1{\csname zipb@r@@#1\endcsname}

\def\zipb@r@#1{\ifx#1\null
                 \let\next\relax
 	       \else
  		 \zipb@r@@{#1}#1%
  	          \let\next\zipb@r@
 	       \fi
 	       \next}

\def\zipbar#1{\setbox\zipbarcode=\null
 \hbox{ % put numbers in an \hbox
   \global\setbox\zipbarcode\hbox{\box\zipbarcode\lb@r} % start with long bar
   \zipb@rn\z@\zipb@r@#1\null
   \zipb@rm\zipb@rn \divide\zipb@rm\ten \multiply\zipb@rm\ten
   \advance\zipb@rm-\zipb@rn
   \ifnum\zipb@rm<0
    \advance\zipb@rm\ten
   \fi
   \zipb@r@@{\the\zipb@rm} % last digit, so that the sum is divisible by ten
   \global\setbox\zipbarcode\hbox{\box\zipbarcode\lb@r}% end with a long bar
   } % end of \hbox
}

\catcode`@=12  % disable private sequences
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%\zipbar{1234567890}
%
%\zipbar{48837}
%
%\zipbar{78653--0102}
%\bye
\endinput
