%   EPSF.TEX macro file:
%   Written by Tomas Rokicki of Radical Eye Software, 29 Mar 1989.
%   Revised by Don Knuth, 3 Jan 1990.
%   Revised by Tomas Rokicki to accept bounding boxes with no
%      space after the colon, 18 Jul 1990.
%   Modified by Peter Galko to be appropriate to Textures handling of
%   the special command and not dvips. 5 Nov 91
%
%   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 with the center environment of LaTeX, preface the \epsffile
%   call with a \leavevmode.  (LaTeX should probably supply this itself
%   for the center environment.)
%
%   To use, simply say
%
%   \input epsf           % somewhere early on in your TeX file
%   \epsfbox{filename}    % where you want to insert a vbox for a figure
%
%   (or better still, read this file in before issuing a \dump command
%   to create a format file)
%
%   Alternatively, you can type
%
%   \epsfbox[0 0 30 50]{filename} % to supply your own BB
%
%   which will not read in the file, and will instead use the bounding
%   box you specify.
%
%   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.
%
%   You can enlarge or reduce the figure by saying
%     \epsfxsize=<dimen> \epsfbox{filename}
%   (or
%     \epsfysize=<dimen> \epsfbox{filename})
%   instead. Then the width of the TeX box will be \epsfxsize and its
%   height will be scaled proportionately (or the height will be
%   \epsfysize and its width will be scaled proportiontally).  The
%   width (and height) is restored to zero after each use.
%
%   A more general facility for sizing is available by defining the
%   \epsfsize macro.    Normally you can redefine this macro
%   to do almost anything.  The first parameter is the natural x size of
%   the PostScript graphic, the second parameter is the natural y size
%   of the PostScript graphic.  It must return the xsize to use, or 0 if
%   natural scaling is to be used.  Common uses include:
%
%      \epsfxsize  % just leave the old value alone
%      0pt         % use the natural sizes
%      #1          % use the natural sizes
%      \hsize      % scale to full width
%      0.5#1       % scale to 50% of natural size
%      \ifnum#1>\hsize\hsize\else#1\fi  % smaller of natural, hsize
%
%   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?
\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\pspoints     % conversion factor
\newcount\scalefx      % scale factor for horizontal scaling
\newcount\scalefy      % scale factor for vertical scaling
%
\pspoints=1bp          % Adobe points are `big'
\epsfxsize=0pt         % Default value, means `use natural size'
\epsfysize=0pt         % ditto
\scalefx=1000          % default scaling is not to resize
\scalefy=1000          % ditto
%
\def\epsfbox#1{\global\def\epsfllx{72}\global\def\epsflly{72}%
   \global\def\epsfurx{540}\global\def\epsfury{720}%
   \ifx#1[\let\next=\epsfgetlitbb\else\let\next=\epsfnormal\fi\next{#1}}%
%
\def\epsfgetlitbb#1#2 #3 #4 #5]#6{\epsfgrab #2 #3 #4 #5 .\\%
   \epsfsetgraph{#6}}%
%
\def\epsfnormal#1{\epsfgetbb{#1}\epsfsetgraph{#1}}%
%
\def\epsfgetbb#1{%
%
%   The first thing we need to do is to open the
%   PostScript file, if possible.
%
\openin\epsffilein=#1
\ifeof\epsffilein\errmessage{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\fi
   }\closein\epsffilein\fi}%
%
%   Now we have to calculate the scale and offset values to use.
%   First we compute the natural sizes.
%
\catcode`\@=11
\def\epsfsetgraph#1{%
   \epsfrsize=\epsfury\pspoints
   \advance\epsfrsize by-\epsflly\pspoints
   \epsftsize=\epsfurx\pspoints
   \advance\epsftsize by-\epsfllx\pspoints
%
%   If `epsfxsize' is 0, we default to the natural size of the picture.
%   Otherwise we scale the graph to be \epsfxsize wide.
%
   \epsfxsize=\epsfsize\epsftsize\epsfrsize
   \ifnum\epsfxsize=0
     \ifnum\epsfysize=0
       \epsfxsize=\epsftsize \epsfysize=\epsfrsize \scalefx=1000 \scalefy=1000
     \else
       \dimen0=\epsfysize \multiply\dimen0 by 10 \dimen1=\dimen0 
       \divide\dimen0 by \epsfrsize \scalefy=\dimen0 \count@=\scalefy
       \multiply\scalefy by 100 \advance\dimen1 by -\count@\epsfrsize
       \dimen0=\dimen1 \multiply\dimen0 by 10 \dimen1=\dimen0
       \divide\dimen0 by \epsfrsize \count@=\dimen0
       \advance\dimen1 by -\count@\epsfrsize \multiply\count@ by 10
       \advance\scalefy by \count@
       \multiply\dimen1 by 10 \advance\dimen1 by 0.5\epsfrsize
       \divide\dimen1 by \epsfrsize \advance\scalefy by \dimen1
%
       \dimen0=\epsftsize \dimen2=\dimen0 \divide\dimen2 by 1000
       \advance\dimen0 by -1000\dimen2 \multiply\dimen0 by \scalefy
       \divide\dimen0 by 1000 \advance\dimen0 by \scalefy\dimen2
       \epsfxsize=\dimen0 \scalefx=\scalefy
     \fi
   \else 
     \dimen0=\epsfxsize \multiply\dimen0 by 10 \dimen1=\dimen0
     \divide\dimen0 by \epsftsize \scalefx=\dimen0 \count@=\scalefx
     \multiply\scalefx by 100 \advance\dimen1 by -\count@\epsftsize
     \dimen0=\dimen1 \multiply\dimen0 by 10 \dimen1=\dimen0
     \divide\dimen0 by \epsftsize \count@=\dimen0
     \advance\dimen1 by -\count@\epsftsize \multiply\count@ by 10
     \advance\scalefx by \count@
     \multiply\dimen1 by 10 \advance\dimen1 by 0.5\epsftsize
     \divide\dimen1 by \epsftsize \advance\scalefx by \dimen1
     \ifnum\epsfysize=0
       \dimen1=\epsfrsize \dimen2=\dimen1 \divide\dimen2 by 1000         
       \advance\dimen1 by -1000\dimen2 \multiply\dimen1 by \scalefx
       \divide\dimen1 by 1000 \advance\dimen1 by \scalefx\dimen2
       \epsfysize=\dimen1 \scalefy=\scalefx
     \else
       \dimen0=\epsfysize \multiply\dimen0 by 10 \dimen1=\dimen0 
       \divide\dimen0 by \epsfrsize \scalefy=\dimen0 \count@=\scalefy
       \multiply\scalefy by 100 \advance\dimen1 by -\count@\epsfrsize
       \dimen0=\dimen1 \multiply\dimen0 by 10 \dimen1=\dimen0
       \divide\dimen0 by \epsfrsize \count@=\dimen0
       \advance\dimen1 by -\count@\epsfrsize \multiply\count@ by 10
       \advance\scalefy by \count@
       \multiply\dimen1 by 10 \advance\dimen1 by 0.5\epsfrsize
       \divide\dimen1 by \epsfrsize \advance\scalefy by \dimen1
     \fi
   \fi
%
%  Finally, we make the vbox and stick in a \special that dvips can parse.
%
   \ifepsfverbose\message{#1: width=\the\epsfxsize, height=\the\epsfysize}\fi
   \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
      {\transformation {\the\epsfxsize} {\the\epsftsize} {\the\epsfysize} 
       {\the\epsfrsize} \special{illustration #1}}\hss}}%
\epsfxsize=0pt\epsfysize=0pt}%

\def\transformation #1 #2 #3 #4 {\dimen0=#1 \count0=\dimen0 
  \dimen0=#2 \count1=\dimen0 \dimen0=#3 \count2=\dimen0 
  \dimen0=#4 \count3=\dimen0 \@transf}
\def\@transf{\aftergroup\@untransf
\special
{postscript
  0 0 transform                 % Get current location in device coordinates.
  grestore                      % Undoes TeXtures gsave.
  matrix currentmatrix          % Save current transform on stack for use
                                % by \@unrotate.
  3 1 roll                      % Put transform at back of current location.
  itransform                    % Current location in TeXtures coords
  dup 3 -1 roll                 % Duplicate the location; x y ==> x y x y
  dup 4 1 roll exch
  translate                     % Translate origin to current location
  1 -1 scale                    % Flip y coordinate
  \the\count0 \space 
  \the\count1 \space div        % compute x scale factor (=#1/#2)
  \the\count2 \space 
  \the\count3 \space div scale  % compute y scale factor with flip (=#3/#4)
  1 -1 scale                    % Unflip y coordinate
  neg exch neg exch translate   % Translate origin back
  gsave}}                       % To match grestore
\def\@untransf{\special
{postscript
  grestore                      % Undoes TeXtures gsave
  setmatrix                     % Set current transform to value saved on
                                % stack. (Hopefully, it's still there.)
  gsave}}                       % To match grestore

%
%   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}\ifx\epsfllx\empty
      \epsfgrab #2 #3 #4 #5 .\\\else
   \global\def\epsflly{#2}%
   \global\def\epsfurx{#3}\global\def\epsfury{#4}\fi}%
%
%   We default the epsfsize macro.
%
\def\epsfsize#1#2{\epsfxsize}
%
%   Finally, another definition for compatibility with older macros.
%
\let\epsffile=\epsfbox
\catcode`\@=12
