%
% psfigTeX macros: combined TeX/LaTeX source
%
% Previous copyright below applies.  University of Maryland changes are
% Copyright (c) 1989 University of Maryland
% Department of Computer Science.  All rights reserved.
% Permission to copy these changes for any purpose is hereby granted
% so long as this copyright notice remains intact.
%
% psfig.tex and psfig.sty are derived automatically from this file,
% by including the appropriate one of either the TeX or LaTeX specific
% lines (marked with `%tex:' and `%latex').
%
% -------------------------------------------
%
% All software, documentation, and related files in this distribution of
% psfig/tex are Copyright (c) 1987 Trevor J. Darrell
%
% Permission is granted for use and non-profit distribution of psfig/tex
% providing that this notice be clearly maintained, but the right to
% distribute any portion of psfig/tex for profit or as part of any commercial
% product is specifically reserved for the author.
%
% Psfig/tex version 1.1
%
% $Header: /usr/src/local/tex/local/mctex/psfig/RCS/psfig.src,v 3.1 89/08/30 03:42:01 chris Exp $
% based on: psfig.tex,v 1.8 87/07/25 13:21:09 trevor Exp
%

\catcode`\@=11\relax
\newwrite\@unused
\def\typeout#1{{\let\protect\string\immediate\write\@unused{#1}}}

\typeout{psfig: version 1.1 (MC-TeX)}

\newdimen\ps@dim

%\def\psglobal#1{\ps@typeout
%  {psfig: including #1 globally}\immediate\special{ps:plotfile #1 global}}
%\def\psfiginit{\ps@typeout{psfiginit}\psglobal{/usr/lib/ps/figtex.pro}}

% @psdo control structure -- similar to Latex @for.
% I redefined these with different names so that psfig can
% be used with TeX as well as LaTeX, and so that it will not
% be vunerable to future changes in LaTeX's internal
% control structure.
%
\def\@nnil{\@nil}
\def\@empty{}
\def\@psdonoop#1\@@#2#3{}
\def\@psdo#1:=#2\do#3{\edef\@psdotmp{#2}\ifx\@psdotmp\@empty \else
\expandafter\@psdoloop#2,\@nil,\@nil\@@#1{#3}\fi}
\def\@psdoloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
#5\def#4{#2}\ifx #4\@nnil \else#5\@ipsdoloop #3\@@#4{#5}\fi\fi}
\def\@ipsdoloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
\let\@nextwhile=\@psdonoop \else
#4\relax\let\@nextwhile=\@ipsdoloop\fi\@nextwhile#2\@@#3{#4}}

%
%

\def\psdraft{\def\@psdraft{0}}
\def\psfull{\def\@psdraft{100}}
\psfull

\def\ps@eat#1{}
\def\pssilent{\let\ps@typeout\ps@eat}
\def\psverbose{\let\ps@typeout\typeout}
\psverbose

\newif\if@prologfile
\newif\if@postlogfile
%%% These are for the option list.
%%% A specification of the form a = b maps to calling \@p@@sa{b}
\newif\if@bbllx
\newif\if@bblly
\newif\if@bburx
\newif\if@bbury
\newif\if@height
\newif\if@width
\newif\if@rheight
\newif\if@rwidth
\newif\if@clip
\def\@p@@sclip#1{\@cliptrue}
\def\@p@@sfile#1{\def\@p@sfile{#1}}
\def\@p@@sfigure#1{\def\@p@sfile{#1}}
\def\@p@@sbbllx#1{%\ps@typeout{bbllx is #1}
	\@bbllxtrue
	\ps@dim=#1
	\edef\@p@sbbllx{\number\ps@dim}
}
\def\@p@@sbblly#1{
	%\ps@typeout{bblly is #1}
	\@bbllytrue
	\ps@dim=#1
	\edef\@p@sbblly{\number\ps@dim}
}
\def\@p@@sbburx#1{
	%\ps@typeout{bburx is #1}
	\@bburxtrue
	\ps@dim=#1
	\edef\@p@sbburx{\number\ps@dim}
}
\def\@p@@sbbury#1{
	%\ps@typeout{bbury is #1}
	\@bburytrue
	\ps@dim=#1
	\edef\@p@sbbury{\number\ps@dim}
}
\def\@p@@sheight#1{
	\@heighttrue
	\ps@dim=#1
	\edef\@p@sheight{\number\ps@dim}
	%\ps@typeout{Height is \@p@sheight}
}
\def\@p@@swidth#1{
	%\ps@typeout{Width is #1}
	\@widthtrue
	\ps@dim=#1
	\edef\@p@swidth{\number\ps@dim}
}
\def\@p@@srheight#1{
	%\ps@typeout{Reserved height is #1}
	\@rheighttrue
	\ps@dim=#1
	\edef\@p@srheight{\number\ps@dim}
}
\def\@p@@srwidth#1{
	%\ps@typeout{Reserved width is #1}
	\@rwidthtrue
	\ps@dim=#1
	\edef\@p@srwidth{\number\ps@dim}
}
\def\@p@@sprolog#1{\@prologfiletrue\def\@prologfileval{#1}}
\def\@p@@spostlog#1{\@postlogfiletrue\def\@postlogfileval{#1}}
\def\@p@@ssilent#1{\pssilent}

\def\@cs@name#1{\csname #1\endcsname}
\def\@setparms#1=#2,{\@cs@name{@p@@s#1}{#2}}
%
% initialize the defaults (size the size of the figure)
%
\def\ps@init@parms{
	\@bbllxfalse \@bbllyfalse
	\@bburxfalse \@bburyfalse
	\@heightfalse \@widthfalse
	\@rheightfalse \@rwidthfalse
	\def\@p@sbbllx{}\def\@p@sbblly{}
	\def\@p@sbburx{}\def\@p@sbbury{}
	\def\@p@sheight{}\def\@p@swidth{}
	\def\@p@srheight{}\def\@p@srwidth{}
	\def\@p@sfile{}
	\def\@p@scost{10}
	\def\@sc{}
	\@prologfilefalse
	\@postlogfilefalse
	\@clipfalse
}
%
% Go through the options setting things up.
%
\def\parse@ps@parms#1{\@psdo\@psfiga:=#1\do{\expandafter\@setparms\@psfiga,}}

%
% Compute bb height and width
%
\newif\ifno@bb
\newif\ifnot@eof
\newread\ps@stream
\def\bb@missing{
	\ps@typeout{psfig: searching \@p@sfile \space  for bounding box}
	\openin\ps@stream=\@p@sfile
	\no@bbtrue
	\not@eoftrue
	\catcode`\%=12
	\loop
		\read\ps@stream to \line@in
		\global\toks200=\expandafter{\line@in}
		\ifeof\ps@stream \not@eoffalse \fi
		%\ps@typeout{looking at :: \the\toks200}
		\@bbtest{\toks200}
		\if@bbmatch\not@eoffalse\expandafter\bb@cull\the\toks200\fi
	\ifnot@eof \repeat
	\catcode`\%=14
}
\catcode`\%=12
\newif\if@bbmatch
\def\@bbtest#1{\expandafter\@a@\the#1%%BoundingBox:\@bbtest\@a@}
\long\def\@a@#1%%BoundingBox:#2#3\@a@{\ifx\@bbtest#2\@bbmatchfalse\else\@bbmatchtrue\fi}
\long\def\bb@cull#1 #2 #3 #4 #5 {
	\ps@dim=#2 bp\edef\@p@sbbllx{\number\ps@dim}
	\ps@dim=#3 bp\edef\@p@sbblly{\number\ps@dim}
	\ps@dim=#4 bp\edef\@p@sbburx{\number\ps@dim}
	\ps@dim=#5 bp\edef\@p@sbbury{\number\ps@dim}
	\no@bbfalse
}
\catcode`\%=14
%
\def\compute@bb{
	\no@bbfalse
	\if@bbllx \else \no@bbtrue \fi
	\if@bblly \else \no@bbtrue \fi
	\if@bburx \else \no@bbtrue \fi
	\if@bbury \else \no@bbtrue \fi
	\ifno@bb \bb@missing \fi
	\ifno@bb \typeout{FATAL ERROR: no bb supplied or found}
		\no-bb-error
	\fi
	%
	\count203=\@p@sbburx
	\count204=\@p@sbbury
	\advance\count203 by -\@p@sbbllx
	\advance\count204 by -\@p@sbblly
	\edef\@bbw{\number\count203}
	\edef\@bbh{\number\count204}
	%\ps@typeout{bbh = \@bbh, bbw = \@bbw}
}
%
% \in@hundreds performs #1 * (#2 / #3) correct to the hundreds,
%	then leaves the result in @result
%
\def\in@hundreds#1#2#3{\count240=#2 \count241=#3
	\count100=\count240	% 100 is first digit #2/#3
	\divide\count100 by \count241
	\count101=\count100
	\multiply\count101 by \count241
	\advance\count240 by -\count101
	\multiply\count240 by 10
	\count101=\count240	%101 is second digit of #2/#3
	\divide\count101 by \count241
	\count102=\count101
	\multiply\count102 by \count241
	\advance\count240 by -\count102
	\multiply\count240 by 10
	\count102=\count240	% 102 is the third digit
	\divide\count102 by \count241
	\count200=#1\count205=0
	\count201=\count200
		\multiply\count201 by \count100
	 	\advance\count205 by \count201
	\count201=\count200
		\divide\count201 by 10
		\multiply\count201 by \count101
		\advance\count205 by \count201
		%
	\count201=\count200
		\divide\count201 by 100
		\multiply\count201 by \count102
		\advance\count205 by \count201
		%
	\edef\@result{\number\count205}
}
\def\compute@wfromh{
	% computing : width = height * (bbw / bbh)
	\in@hundreds{\@p@sheight}{\@bbw}{\@bbh}
	%\ps@typeout{ \@p@sheight * \@bbw / \@bbh, = \@result }
	\edef\@p@swidth{\@result}
	%\ps@typeout{w from h: width is \@p@swidth}
}
\def\compute@hfromw{
	% computing : height = width * (bbh / bbw)
	\in@hundreds{\@p@swidth}{\@bbh}{\@bbw}
	%\ps@typeout{ \@p@swidth * \@bbh / \@bbw = \@result }
	\edef\@p@sheight{\@result}
	%\ps@typeout{h from w: height is \@p@sheight}
}
\def\compute@handw{
	\if@height \if@width \else \compute@wfromh \fi
	\else \if@width \compute@hfromw \else
		\edef\@p@sheight{\@bbh}
		\edef\@p@swidth{\@bbw} \fi
	\fi
}
\def\compute@resv{
	\if@rheight \else \edef\@p@srheight{\@p@sheight} \fi
	\if@rwidth \else \edef\@p@srwidth{\@p@swidth} \fi
}
%
% Compute any missing values
\def\compute@sizes{
	\compute@bb
	\compute@handw
	\compute@resv
}
%
% \psfig
% usage : \psfig{file=, height=, width=, bbllx=, bblly=, bburx=, bbury=,
%			rheight=, rwidth=, clip=}
%
% "clip=" is a switch and takes no value, but the `=' must be preset.
\def\psfig#1{\vbox{
    \ps@init@parms
    \parse@ps@parms{#1}
    \compute@sizes
    %
    \ifnum\@p@scost<\@psdraft
	\ps@typeout{psfig: including \@p@sfile}
	%
	\special{ps::[begin] \@p@swidth \space \@p@sheight \space
		\@p@sbbllx \space \@p@sbblly \space
		\@p@sbburx \space \@p@sbbury \space
		startTexFig \space}
	\if@clip
		\ps@typeout{(clip)}
		\special{ps:: \@p@sbbllx \space \@p@sbblly \space
			\@p@sbburx \space \@p@sbbury \space
			doclip \space}
	\fi
	\if@prologfile \special{ps: plotfile \@prologfileval \space} \fi
	\special{ps: plotfile \@p@sfile \space}
	\if@postlogfile \special{ps: plotfile \@postlogfileval \space} \fi
	\special{ps::[end] endTexFig \space}
	% Create the vbox to reserve the space for the figure
	\vbox to\@p@srheight true sp{\hbox to\@p@srwidth true sp{\hfil}\vfil}
    \else
	% draft figure, just reserve the space and print the
	% path name.
	\vbox to\@p@srheight true sp{\vss
	    \hbox to\@p@srwidth true sp{\hss\@p@sfile\hss}\vss}
    \fi
}}

\catcode`\@=12\relax

