%   EPSF.TEX macro file:
%   Written by Tomas Rokicki of Radical Eye Software, 29 Mar 1989.
%   Revised by Don Knuth, 3 Jan 1990.
%   Revised by Andy Gruss, 21 Feb 1990.
%
%   TeX macros to include an Encapsulated PostScript graphic.
%   Works by finding the bounding box comment,
%   calculating the correct scale values, and inserting a vbox
%   of the appropriate size at the current position in the TeX document.
%
%   To use, simply say
%   \input epsf           % somewhere early on in your TeX file
%   \epsfbox{filename.ps} % where you want to insert a vbox for a figure
%
%   The effect will be to typeset the figure as a TeX box, at the
%   point of your \epsfbox command. By default, the graphic will have its
%   `natural' width (namely the width of its bounding box, as described
%   in filename.ps). The TeX box will have depth zero.  However, in no
%   case will the width of the graphic exceed the value of \hsize.
%
%   You can enlarge or reduce the figure by saying
%     \epsfxsize=<dimen> \epsfbox{filename.ps}
%   or
%     \epsfysize=<dimen> \epsfbox{filename.ps}.
%   Then the width/height of the TeX box will be \epsfxsize/\epsfysize,
%   and its height/width will be scaled proportionately.
%   (The \epsfbox macro resets \epsfxsize and \epsfysize to zero after
%   each use.).  If the width of the graphic exceeds \hsize after any
%   scaling is done, it will be shrunk somewhat and made to fit.
%
%   If you want TeX to report the size of the figure (as a message
%   on your terminal when it processes each figure), say `\epsfverbosetrue'.
%
\newread\epsffilein    % file to \read
\newif\ifepsffileok    % continue looking for the bounding box?
\newif\ifepsfbbfound   % success?
\newif\ifepsfverbose   % report what you're making?
\newif\ifepsfxscale    % must we scale horizontally?
\newif\ifepsfyscale    % must we scale vertically?
\newdimen\epsfxsize    % horizontal size after scaling
\newdimen\epsfysize    % vertical size after scaling
\newdimen\epsftsize    % horizontal size before scaling
\newdimen\epsfrsize    % vertical size before scaling
\newdimen\epsftmp      % register for arithmetic manipulation
\newdimen\epsfrtmp     % vertical size before scaling (temporary)
\newdimen\epsfttmp     % horizontal size before scaling (temporary)
\newdimen\pspoints     % conversion factor

\pspoints=1truebp      % Adobe points are `big'
		       % Default value, means `use natural size'
\epsfxsize=0pt \epsfysize=0pt
%
\def\epsfbox#1{%
%
%   The first thing we need to do is to open the
%   PostScript file, if possible.
%
\openin\epsffilein=#1
\ifeof\epsffilein\message{I couldn't open #1, will ignore it}\else
%
%   Okay, we got it. Now we'll scan lines until we find one that doesn't
%   start with %. We're looking for the bounding box comment.
%
   {\epsffileoktrue \chardef\other=12
    \def\do##1{\catcode`##1=\other}\dospecials \catcode`\ =10
    \loop
       \read\epsffilein to \epsffileline
       \ifeof\epsffilein\epsffileokfalse\else
%
%   We check to see if the first character is a % sign;
%   if not, we stop reading (unless the line was entirely blank);
%   if so, we look further and stop only if the line begins with
%   `%%BoundingBox: '.
%
          \expandafter\epsfaux\epsffileline. \\%
       \fi
   \ifepsffileok\repeat
   \ifepsfbbfound\else
      \ifepsfverbose\message{No bounding box comment in #1; using defaults}\fi
      \global\def\epsfllx{72}%
      \global\def\epsflly{72}%
      \global\def\epsfurx{540}%
      \global\def\epsfury{720}\fi
   }\closein\epsffilein
%
%   Now we have to calculate the scale and offset values to use.
%   First we compute the natural sizes.
%
   \epsfrsize=\epsfury\pspoints
   \advance\epsfrsize by-\epsflly\pspoints
   \epsftsize=\epsfurx\pspoints
   \advance\epsftsize by-\epsfllx\pspoints
%
%   If `epsfxsize' is 0 and `epsfysize' is 0, we default to the natural
%   size of the picture.  Otherwise we scale the graph to be \epsfxsize
%   wide or \epsfysize high (whichever is non-zero).
%
   \ifnum\epsfxsize=0 \ifnum\epsfysize=0
     \epsfxsize=\epsftsize \epsfysize=\epsfrsize
   \fi \fi
   \epsfxscalefalse \ifnum\epsfysize=0 \epsfxscaletrue \fi
   \epsfyscalefalse \ifnum\epsfxsize=0 \epsfyscaletrue \fi
   % Change temporary values in case we must do multiple scaling passes.
   \epsfttmp=\epsftsize \epsfrtmp=\epsfrsize
%
%   We have a sticky problem here:  TeX doesn't do floating point arithmetic!
%   Our goal is to compute y = rx/t and/or x = ty/r. The following loops do
%   this reasonably fast, with an error of at most about 16 sp (about 1/4000
%   pt).
%   
				% No taller than the page, please.
   \ifnum\epsfysize>\vsize \epsfysize=\vsize \epsfyscaletrue \fi
   \ifepsfyscale		% Use epsfysize to scale (find x = ty/r)
     \epsftmp=\epsftsize \epsfttmp=\epsftsize
     \divide\epsftmp\epsfrsize
     \epsfxsize=\epsfysize \multiply\epsfxsize\epsftmp   
     \multiply\epsftmp\epsfrsize \advance\epsfttmp-\epsftmp
     \epsftmp=\epsfysize
     \loop \advance\epsfttmp\epsfttmp \divide\epsftmp 2
     \ifnum\epsftmp>0
        \ifnum\epsfttmp<\epsfrsize\else
           \advance\epsfttmp-\epsfrsize \advance\epsfxsize\epsftmp \fi
     \repeat
   \fi
				% No wider than the margins, please.
   \ifnum\epsfxsize>\hsize \epsfxsize=\hsize \epsfxscaletrue \fi
   \ifepsfxscale		% Use epsfxsize to scale (find y = rx/t)
     \epsftmp=\epsfrsize \epsfrtmp=\epsfrsize
     \divide\epsftmp\epsftsize
     \epsfysize=\epsfxsize \multiply\epsfysize\epsftmp   
     \multiply\epsftmp\epsftsize \advance\epsfrtmp-\epsftmp
     \epsftmp=\epsfxsize
     \loop \advance\epsfrtmp\epsfrtmp \divide\epsftmp 2
     \ifnum\epsftmp>0
        \ifnum\epsfrtmp<\epsftsize\else
           \advance\epsfrtmp-\epsftsize \advance\epsfysize\epsftmp \fi
     \repeat     
   \fi
%
%  Finally, we make the vbox and stick in a \special that dvips can parse.
%
   \ifepsfverbose
     \message{#1(read): width=\the\epsftsize, height=\the\epsfrsize}
     \message{#1(calc): width=\the\epsfxsize, height=\the\epsfysize}
   \fi
   \epsftmp=10\epsfxsize \divide\epsftmp\pspoints
   \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
      \special{psfile=#1 llx=\epsfllx\space lly=\epsflly\space
          urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp}%
      \hfil}}%
%
%  Reset \epsfxsize and \epsfysize back to zero for the next call to
%  \epsfbox.  However, stash the values used to draw the graphic in
%  \epsftsize and \epsfrsize in case the user needs them for something.
%
   \global\epsftsize=\epsfxsize \epsfxsize=0pt
   \global\epsfrsize=\epsfysize \epsfysize=0pt
\fi
}
%
%   We still need to define the tricky \epsfaux macro. This requires
%   a couple of magic constants for comparison purposes.
%
{\catcode`\%=12 \global\let\epsfpercent=%\global\def\epsfbblit{%BoundingBox:}}%
%
%   So we're ready to check for `%BoundingBox:' and to grab the
%   values if they are found.
%
\long\def\epsfaux#1#2 #3\\{\ifx#1\epsfpercent
   \def\testit{#2}\ifx\testit\epsfbblit
      \epsfgrab #3 . . . \\%
      \epsffileokfalse
      \global\epsfbbfoundtrue
   \fi\else\ifx#1\par\else\epsffileokfalse\fi\fi}%
%
%   Here we grab the values and stuff them in the appropriate definitions.
%
\def\epsfgrab#1 #2 #3 #4 #5\\{\global\def\epsfllx{#1}\global\def\epsflly{#2}%
   \global\def\epsfurx{#3}\global\def\epsfury{#4}}%
