%--------------------------------------------------------------------------
% This is small extension of T. Rokicki's EPSF.TEX (from DVIPS 5.58):
%  -- allows safe including of EPS files generated by DVIPS
%  -- can also display bounding boxes.
%--------------------------------------------------------------------------
% AUTHORS: Bogus\l{}aw Jackowski and Piotr Pianowski
%   BOP s.c.
%   ul. Piastowska 70, 80-363 Gda\'nsk, Poland
%   email: B.Jackowski@GUST.org.pl
% USAGE:
%   \eps{file.eps} % instead of \epsffile{file.eps}
% The following settings of the parameter \tracingeps are recognized:
%   \tracingeps=0  % default, no extra information displayed
%   \tracingeps=1  % displays bounding boxes and file names
%   \tracingeps>1  % displays graphics, bounding boxes and file names
% HISTORY:
% ver. 0.5  19 VIII 1994
% ver. 0.51 25 VIII 1994 
%      * \epsnamecolor added
% ver. 0.52 31 VIII 1994 
%      * \eps renamed to \epsraw
%      * \tps renamed to \eps
% ver. 0.53 9 I 1995
%      * `\isls false N' added to \eps 
%      * virtually active slash neutralized
% ver. 0.54 9 III 1995
%      * \eps clears bop-hook
% ver. 0.55 27 III 1995
%      * a patch neutralizing unnecessary rounding added
% ver. 0.56  3 X 1995
%      * \special commands surrounding EPS box are to be nested in the \vbox
% ver. 0.57  31 VIII 1996
%      * odd characters in names enabled
% ver. 0.58  21.III.1997
%      * setpageparams and setpage neutralized
% ver. 0.585 25.III.1997
%      * comments touched
% ver. 0.59 7 VI 1998
%      * EPSF.TEX included
% ver. 0.60 10 I 1999
%      * safe multiplication of eps dimensions by 10 (expected to be
%        included in the newest version of Rokicki's package)
%--------------------------------------------------------------------------
\edef\slashcatcode{\the\catcode`\/}
\catcode`\/12
%
%%%%%%%%%%%%%%%%%%%%%%%%%%% \input epsf  -- inserted below %%%%%%%%%%%%%%%%%
%   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.
%
%   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.ps} % where you want to insert a vbox for a figure
%
%   Alternatively, you can type
%
%   \epsfbox[0 0 30 50]{filename.ps} % 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.ps}
%   (or
%     \epsfysize=<dimen> \epsfbox{filename.ps})
%   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?
\newif\ifepsfdraft     % use draft mode?
\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
%
\pspoints=1bp          % Adobe points are `big'
\epsfxsize=0pt         % Default value, means `use natural size'
\epsfysize=0pt         % ditto
%
\def\epsfbox#1{\global\def\epsfllx{72}\global\def\epsflly{72}%
   \global\def\epsfurx{540}\global\def\epsfury{720}%
   \def\lbracket{[}\def\testit{#1}\ifx\testit\lbracket
   \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.
%
\def\epsfclipon{\def\epsfclipstring{ clip}}%
\def\epsfclipoff{\def\epsfclipstring{\ifepsfdraft\space clip\fi}}%
\epsfclipoff
%
\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
      \epsfrsize=0pt
%
%   We have a sticky problem here:  TeX doesn't do floating point arithmetic!
%   Our goal is to compute y = rx/t. The following loop does this reasonably
%   fast, with an error of at most about 16 sp (about 1/4000 pt).
% 
     \else\epsftmp=\epsftsize \divide\epsftmp\epsfrsize
       \epsfxsize=\epsfysize \multiply\epsfxsize\epsftmp
       \multiply\epsftmp\epsfrsize \advance\epsftsize-\epsftmp
       \epsftmp=\epsfysize
       \loop \advance\epsftsize\epsftsize \divide\epsftmp 2
       \ifnum\epsftmp>0
          \ifnum\epsftsize<\epsfrsize\else
             \advance\epsftsize-\epsfrsize \advance\epsfxsize\epsftmp \fi
       \repeat
       \epsfrsize=0pt
     \fi
   \else \ifnum\epsfysize=0
     \epsftmp=\epsfrsize \divide\epsftmp\epsftsize
     \epsfysize=\epsfxsize \multiply\epsfysize\epsftmp   
     \multiply\epsftmp\epsftsize \advance\epsfrsize-\epsftmp
     \epsftmp=\epsfxsize
     \loop \advance\epsfrsize\epsfrsize \divide\epsftmp 2
     \ifnum\epsftmp>0
        \ifnum\epsfrsize<\epsftsize\else
           \advance\epsfrsize-\epsftsize \advance\epsfysize\epsftmp \fi
     \repeat
     \epsfrsize=0pt
    \else
     \epsfrsize=\epsfysize
    \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
   \epsftmp=10\epsfxsize \divide\epsftmp\pspoints
   \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
      \ifnum\epsfrsize=0\relax
        \special{PSfile=\ifepsfdraft psdraft.ps\else#1\fi\space
             llx=\epsfllx\space lly=\epsflly\space
             urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp
             \epsfclipstring}%
      \else
        \epsfrsize=10\epsfysize \divide\epsfrsize\pspoints
        \special{PSfile=\ifepsfdraft psdraft.ps\else#1\fi\space
             llx=\epsfllx\space lly=\epsflly\space
             urx=\epsfurx\space ury=\epsfury\space rwi=\number\epsftmp\space
             rhi=\number\epsfrsize \epsfclipstring}%
      \fi
      \hfil}}%
\global\epsfxsize=0pt\global\epsfysize=0pt}%
%
%   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\epsfempty{}%
\def\epsfgrab #1 #2 #3 #4 #5\\{%
\global\def\epsfllx{#1}\ifx\epsfllx\epsfempty
      \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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End of EPSF %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ---
\newcount\tracingeps \tracingeps0
% ---
\newbox\tmpepsbox
% ---
\ifx\epsdirundefined\epsdir\def\epsdir{}\fi
\def\epsnamecolor{1 0 0}
% ---
% BOTH \eps and \epsraw ``normalize'' the eps name, as one may wish
% to use \epsraw separately
% ---
\def\epsraw{\begingroup\escapechar-1\catcode`\%12\def~{\string~}\epsrawcont}
\def\epsrawcont#1{%
 \xdef\theepsname{\expandafter\string\csname#1\endcsname}\endgroup
 \ifnum\tracingeps=0 \hbox{\epsffile{\epsdir\theepsname}}\fi
 \ifnum\tracingeps>0
  \hbox{\setbox\tmpepsbox\hbox{\epsffile{\epsdir\theepsname}}%
   \ifnum\tracingeps>1 \rlap{\copy\tmpepsbox}\fi
   \vbox{\offinterlineskip
     \hrule\vskip-.4pt
     \hbox to\wd\tmpepsbox{\vrule\hss\vbox to\ht\tmpepsbox{
       \vss\hbox{\tt \special{ps:gsave \epsnamecolor\space setrgbcolor}%
       \theepsname\special{ps:grestore}}\vss}\hss\vrule}
     \vskip-.4pt\hrule
   }}\fi}
% ---
\def\eps{\begingroup \escapechar-1 \catcode`\%12 \def~{\string~}\epscont}
\def\epscont#1{%
  \xdef\theepsname{\expandafter\string\csname#1\endcsname}\endgroup
  \vbox{\special{ps: /SItps save N /isls false N
    statusdict begin
      /setpageparams {pop pop pop pop} def /setpage {pop pop pop} def
    end
    userdict begin /bop-hook {} def end }%
  \epsraw\theepsname\special{ps: SItps restore }}}
% ---
% Redefinition of epsf macros:
%
% The values of `rwi' and `rhi' parameters are to be given in tenths
% of big point; the older version of the epsf package used to multiply
% a dimen register by 10, which put severe limitations on the
% available EPS size.
%
% The macro `\decimalshift' multiplies a given value by 10 ``syntactically,''
% i.e., accomplishes ``decimal shifting''; the following conditions
% are assumed to hold:
%  *  leading zeros are meaningless;
%  *  <digit>+<period><dimen unit> is a proper form of a dimen value;
%  *  the command `\the' applied to a dimen register yields a canonical form:
%        [<minus>]<digit>+[<period><digit>+]<pt unit>
%     where <pt unit> are `p' and `t' characters having category code 12.
%
\def\decimalshift#1{% #1 -- a dimen register
    \expandafter\expandafter\expandafter
    \decimalsplit\expandafter\gobblePT\the#1.\relax
}%
\def\decimalsplit#1.#2\relax{%
    \ifnum#1=0 \ifdim#11sp=-1sp -\fi \else #1\fi
    \ifdim0#2sp=0sp 0\else \fractionshift#2\fi
}%
\def\fractionshift#1#2.{\ifnum0#2=0 #1\else #1.#2\fi}%
{\catcode`\p12 \catcode`\t12 \gdef\gobblePT#1pt{#1}}%
\def\pttobp{0.99626401}% = 72/72.27
% ---
\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
      \epsfrsize=0pt
%
%   We have a sticky problem here:  TeX doesn't do floating point arithmetic!
%   Our goal is to compute y = rx/t. The following loop does this reasonably
%   fast, with an error of at most about 16 sp (about 1/4000 pt).
% 
     \else\epsftmp=\epsftsize \divide\epsftmp\epsfrsize
       \epsfxsize=\epsfysize \multiply\epsfxsize\epsftmp
       \multiply\epsftmp\epsfrsize \advance\epsftsize-\epsftmp
       \epsftmp=\epsfysize
       \loop \advance\epsftsize\epsftsize \divide\epsftmp 2
       \ifnum\epsftmp>0
          \ifnum\epsftsize<\epsfrsize\else
             \advance\epsftsize-\epsfrsize \advance\epsfxsize\epsftmp \fi
       \repeat
       \epsfrsize=0pt
     \fi
   \else \ifnum\epsfysize=0
     \epsftmp=\epsfrsize \divide\epsftmp\epsftsize
     \epsfysize=\epsfxsize \multiply\epsfysize\epsftmp   
     \multiply\epsftmp\epsftsize \advance\epsfrsize-\epsftmp
     \epsftmp=\epsfxsize
     \loop \advance\epsfrsize\epsfrsize \divide\epsftmp 2
     \ifnum\epsftmp>0
        \ifnum\epsfrsize<\epsftsize\else
           \advance\epsfrsize-\epsftsize \advance\epsfysize\epsftmp \fi
     \repeat
     \epsfrsize=0pt
    \else
     \epsfrsize=\epsfysize
    \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
   \epsftmp=\pttobp\epsfxsize
   \vbox to\epsfysize{\vfil\hbox to\epsfxsize{%
      \ifnum\epsfrsize=0\relax
        \special{PSfile=\ifepsfdraft psdraft.ps\else#1\fi\space
             llx=\epsfllx\space lly=\epsflly\space
             urx=\epsfurx\space ury=\epsfury\space
             rwi=\decimalshift\epsftmp
             \epsfclipstring}%
      \else
        \epsfrsize=\pttobp\epsfysize
        \special{PSfile=\ifepsfdraft psdraft.ps\else#1\fi\space
             llx=\epsfllx\space lly=\epsflly\space
             urx=\epsfurx\space ury=\epsfury\space
             rwi=\decimalshift\epsftmp\space
             rhi=\decimalshift\epsfrsize
             \epsfclipstring}%
      \fi
      \hfil}}%
\global\epsfxsize=0pt\global\epsfysize=0pt}%
% ---
\catcode`\/\slashcatcode
% ---
\endinput
