% LaTeX macros for plain and encapsulated Postscript boxes 
% \def\lastedit{2008-10-27 20:38:10 by stolfi}
% Extracted 93-12-02 by Jorge Stolfi 
%   from eps-macros.tex of 12 Oct 90 by Jack Snoeyink 
%   w/ ideas from John Hershberger and code from Tom Rokicki 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% SUMMARY
%
%   The most important macros defined by this file are:
% 
%     \epsbox
%       Form a box containing encapsulated postscript from a file at a
%       given scale.
%
%     \psbox
%       Form a box containing plain postscript from a file at a given
%       scale, width, and height.
% 
% USAGE
%
%     \epsbox[cmds] {file} {scale}
%
%     \psbox[cmds] {file} {scale} {width} {height}
%
%   where
%
%     cmds  are optional local PostScript commands which are executed 
%           after scaling. As described below, they are useful for 
%           setting origins, non-uniform scaling, etc.
%
%     file  is the filename in the \figuredirectory and without the .ps/.eps
%           extension.
%
%     scale is a real number: 1 to use the drawn size.  0.9 to use 90% of
%           drawn size.  Can be changed by local commands. (see below)
%
%     width and height are dimensions of the box produced by \psbox.
%
% OPTIONS
%
%   The following macros affect the operation of \psbox and \epsbox,
%   and may be redefined by the user:
%
%     \DVIscaling \figuredirectory \missingfigure \boxer
%     \scale \horizscale \vertscale
%
%   Here are their meanings:
%
%     \DVIscaling 
%       is the number the dvi to ps converter considers a unit
%       scaling transformation.  Default: 1 for dvi2ps  --- 100 for
%       some of Rokicki's versions of dvips.
%
%     \globalscaling
%       is the scaling applied to every picture --- useful
%       for making double size figures for journals.  Default: 1
%
%     \figuredirectory
%       is the pathname where figure postscript files are kept.
%       Default: ./figure
%
%     \missingfigure{file}{ext}
%       is executed when the file #1#2 is not found.
%       Default: \newcommand{\missingfigure}[2]{\hbox{Missing figure #1#2}}
%
%     \boxer
%       is the command used by \psbox to make a box of the desired size
%       containing the figure.  Possible commands depend on the origin of
%       the figure, and include \llboxer \lcboxer \ulboxer and \ccboxer.
%       (ll = lower left, ul = upper left, cc = dead center, etc.)
%       \boxer can be defined in the local commands or globally.
%
%     \horizscale, \vertscale, \scale
%       Predefined PostScript macros that can be used in the "cmds"
%       argument of \epsbox and \psbox to scale the picture
%       horizontally and vertically by different factors.  \scale
%       scales the picture uniformly.  Scales accumulate.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\catcode`@=11 \endlinechar=-1   % Use LaTeX internals, igore line breaks

\newcommand{\DVIscaling}{1}              % for dvi2ps
\newcommand{\globalscaling}{1}           % for scaling everything
\newcommand{\figuredirectory}{./figures} % where to find the .ps/.eps files 
\let\boxer=\llboxer             % default origin in lower left for non-
                                %   encapsulated postscript figures

\newcommand{\missingfigure}[2]{\hbox{Missing figure #1#2}}

\special{! /@scaleunit 1 def }

%  Various `boxers' --- which one is used depends on where the origin
%  of the picture is.  If you are using the bounding boxes of
%  encapsulated postscript, just use the default \llboxer.  Otherwise,
%  you have to figure out where the origin is and set \boxer appropriately.

\newdimen\@psboxht % height of postscript box
\newdimen\@psboxwd % width of postscript box

\newcommand{\llboxer}[1]{
  \vbox to \@psboxht{\vfil\hbox to \@psboxwd{#1\hfill}}
}

\newcommand{\lcboxer}[1]{
  \vbox to \@psboxht{\vfil\hbox to \@psboxwd{\hfill#1\hfill}}
}

\newcommand{\oldboxer}[1]{
  \vbox to \@psboxht{\vfil \hbox to \@psboxwd{\hfill\llap{#1\hskip4.25in}\hfill}}
}

\newcommand{\ulboxer}[1]{
  \vbox to \@psboxht{\hbox to \@psboxwd{#1\hfill}\vfil}
}

\newcommand{\ccboxer}[1]{
  \vbox to \@psboxht{\vfil\hbox to \@psboxwd{\hfill#1\hfill}\vfil}
}

% a macro to strip the pt off a dimension
%
{\catcode`\p=12\catcode`\t=12 \gdef\removedimen#1pt{#1}}

\newcommand{\defscaled}[2]{
  #2=\DVIscaling#2\xdef#1{\expandafter\removedimen\the#2}
}

\def\DVIspace{ }
\newdimen\hscalefactor
\newdimen\vscalefactor

\newcommand{\scale}[1]{\horizscale{#1}\vertscale{#1}}
\newcommand{\horizscale}[1]{\hscalefactor=#1\hscalefactor\@psboxwd=#1\@psboxwd}
\newcommand{\vertscale}[1]{\vscalefactor=#1\vscalefactor\@psboxht=#1\@psboxht}

\newcommand{\psbox}{\@ifnextchar[{\@psboxaux}{\@psboxaux[\relax]}}

\def\@psboxaux[#1]#2#3#4#5{
  { 
    \@psboxwd=#4
    \@psboxht=#5
    \hscalefactor=1pt
    \vscalefactor=1pt
    \scale{#3}
    \scale{\globalscaling}
    #1
    \defscaled\DVIhscale\hscalefactor
    \defscaled\DVIvscale\vscalefactor
    \boxer{
      \special{
        psfile=\figuredirectory/#2.ps \DVIspace
        hscale=\DVIhscale\DVIspace 
        vscale=\DVIvscale
      }
    }
  }
}

% The following is from tex/inputs/epsf.tex by Tom Rokicki
% with slight modifications by Jack Snoeyink.
%
%   Written by Tomas Rokicki of Radical Eye Software, 29 Mar 1989.
%
%   Include an ecapsulated postscript graphic.
%   Works by finding the bounding box comment.
%
\newread\@epsffilein
\newif\if@epsffileok
\newif\if@epsfbbfound

\newdimen\pspoints
\pspoints=1in
\divide\pspoints by 72

\newcommand{\epsbox}{
  \@ifnextchar[{\@epsboxaux}{\@epsboxaux[\relax]}
}

\def\@epsboxaux[#1]#2#3{
  %
  % The first thing we need to do is to open the
  % PostScript file, if possible.
  %
  \openin\@epsffilein=\figuredirectory/#2.eps\relax
  \ifeof\@epsffilein
    \message{I couldn't open \figuredirectory/#2.eps }
    \missingfigure{#2}{.eps}
  \else
    %
    %   Okay, we got it.  Now we scan lines until we find one
    %   that doesn't start with a percentage sign.  We're
    %   looking for the bounding box comment.
    %
    {
      \@epsffileoktrue\@epsfbbfoundfalse
      \catcode`\%=11 \catcode`\\=11
      \catcode`\{=11 \catcode`\}=11
      \catcode`\$=11 \catcode`\^=11
      \catcode`\&=11 \catcode`\#=11
      \catcode`\~=11 \catcode`\_=11
      \loop
        \read\@epsffilein to \@epsffileline
        \ifeof\@epsffilein
          \@epsffileokfalse
        \else
	  %
	  %   Now we check to make sure the first character is a % sign,
	  %   and that the rest are `%BoundingBox:' if necessary.
	  \expandafter\@epsfaux\@epsffileline . .\\
        \fi
      \if@epsffileok\repeat
      \if@epsfbbfound
        \@psboxht=\@epsfury\pspoints
        \advance\@psboxht by-\@epsflly\pspoints
        \@psboxwd=\@epsfurx\pspoints
        \advance\@psboxwd by-\@epsfllx\pspoints
      \else
        \message{No bounding box comment in \figuredirectory/#2.eps }
        \@psboxwd=2in\@psboxht=1in
      \fi
      \immediate\closein\@epsffilein
      \hscalefactor=1pt
      \vscalefactor=1pt
      \scale{#3}
      \scale{\globalscaling}
      #1

      \defscaled\DVIhscale\hscalefactor
      \defscaled\DVIvscale\vscalefactor

      \hscalefactor=-\@epsfllx\hscalefactor
      \hscalefactor=1.00375\hscalefactor  % 72.27/72 = TeXpoints/PSpoints
      \defscaled\DVIhoffset\hscalefactor

      \vscalefactor=-\@epsflly\vscalefactor
      \vscalefactor=1.00375\vscalefactor  % 72.27/72 = TeXpoints/PSpoints
      \defscaled\DVIvoffset\vscalefactor

      \llboxer{
        \special{
          psfile=\figuredirectory/#2.eps \DVIspace
          hoffset=\DVIhoffset\DVIspace
          voffset=\DVIvoffset\DVIspace
          hscale=\DVIhscale\DVIspace 
          vscale=\DVIvscale
        }
      }
    }
  \fi
}


%   We need these for comparison purposes.
%
{\catcode`\%=11 
  \global\let\@epsfpar=\par
  \global\let\@epsfpercent=%
  \global\def\@epsfbblit{%BoundingBox:}
}

%   This is our function that checks for `%BoundingBox:' and grabs the
%   values if they are found.
%
\long\def\@epsfaux#1#2 #3\\{
  \relax
  \ifx#1\@epsfpercent
    \def\testit{#2}
    \ifx\testit\@epsfbblit
      \@epsfsize #3 . . . .\\
      \global\@epsffileokfalse
      \global\@epsfbbfoundtrue
    \fi
  \else
    \ifx#1\@epsfpar
    \else
      \global\@epsffileokfalse
    \fi
  \fi
}

%   Here we grab the values and stuff them in the appropriate definitions.
%
\def\@epsfsize#1 #2 #3 #4 #5\\{
  \global\def\@epsfllx{#1}
  \global\def\@epsflly{#2}
  \global\def\@epsfurx{#3}
  \global\def\@epsfury{#4}
}

\catcode`@=12 \endlinechar=`\^^M

