%
% pdftricks.sty 
%
% Copyright (c) 2001, Radhakrishnan CV <cvr@river-valley.com>
%                     Rajagopal CV <cvr3@river-valley.com>
%                     http://www.river-valley.com
%
% River Valley Technologies, Software Technology Park,
% Trivandrum, India 695034
%
% Tel: +91 471 33 7501/7502
%
%                     Antoine Chambert-Loir 
%                     <chambert@math.polytechnique.fr>
%                     http://www.math.polytechnique.fr/~chambert
%
% Ecole polytechnique, Palaiseau Cedex, France
% 
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program (gpl.txt); if not, write to the Free
% Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
% MA  02111-1307, USA.
%
% $Id: pdftricks.sty,v 1.15 2001/09/30 11:21:23 cvr Exp $
%
\NeedsTeXFormat{LaTeX2e}
\def\Fileversion$#1: #2 ${\gdef\fileversion{#2}}
\def\Filedate$#1: #2 #3 ${\gdef\filedate{#2}}
\Fileversion$Revision: 1.15 $
\Filedate$Date: 2001/09/30 11:21:23 $
\ProvidesPackage{pdftricks}
   [\filedate\space\fileversion\space psTricks support in PDF (CVRACL)]
\PackageWarningNoLine{pdftricks}
   {****************************************\MessageBreak
    Package pdftricks v,\fileversion\space loaded\MessageBreak
    [psTricks support in PDF (CVR, ACL)]\MessageBreak
    ****************************************}
\RequirePackage{graphicx,color}
\newif\if@debug\@debugfalse
\DeclareOption{debug}{\@debugtrue}
\DeclareOption{noshell}{\PDFTshellfalse}
\DeclareOption{shell}{\PDFTshelltrue}
\newif\ifPDFTshell
\newif\ifPDFTnopdf
\newif\ifnoprocess \noprocessfalse
\ExecuteOptions{shell}
\ProcessOptions\relax
\ifPDFTshell
% we must set it to false if \write18 doesn't work.
% Hack given by Thierry Bouche (Thanks !)
\def\tmpfile{/tmp/w18-test-\the\year\the\month\the\day\the\time} 
\immediate\write18{touch \tmpfile}
\IfFileExists{\tmpfile}{\PDFTshelltrue}{\PDFTshellfalse}
\fi
\ifPDFTshell
  \PackageWarningNoLine{pdftricks}
   {****************************************\MessageBreak
    Using \csname write\endcsname18 capability \MessageBreak
    for producing PDF-figures.  \MessageBreak
    ****************************************}
\else
  \PackageWarningNoLine{pdftricks}
   {****************************************\MessageBreak
    No \csname write\endcsname18 capability.\MessageBreak
    You'll have to run a script by yourself!\MessageBreak
    ****************************************}
\fi

% warning! the definition of FIGURES if psrun must be set accordingly !!
\def\PDFTfigname{\jobname-fig\thepsfig}
\def\PDFTWarning#1#2{\if@debug\PackageWarning{#1}{#2}\fi}
\def\PDFTWarningNoLine#1#2{\if@debug\PackageWarningNoLine{#1}{#2}\fi}
\def\makeinnocent#1{\catcode`#1=12 }
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}
\def\latexname{lplain}\def\latexename{LaTeX2e}
\newwrite\PDFStream

\long\def\ProcessStream#1% start it all of
   {\begingroup%
    \def\CurrentStream{#1}%
    \let\do\makeinnocent \dospecials 
    \makeinnocent\^^L% and whatever other special cases
    \endlinechar`\^^M \catcode`\^^M=12 \xStream}
{\catcode`\^^M=12 \endlinechar=-1 %
 \gdef\xStream#1^^M{%
    \expandafter\ProcessStreamLine}
 \gdef\ProcessStreamLine#1^^M{\def\test{#1}
      \csarg\ifx{End\CurrentStream Test}\test
          \edef\next{\noexpand\EndOfStream{\CurrentStream}}%
      \else \ThisStream{#1}\let\next\ProcessStreamLine
      \fi \next}
}
\long\def\streaminfo{\string\end{document}}
\def\CSstringmeaning#1{\expandafter\CSgobblearrow\meaning#1}
\def\CSstringcsnoescape#1{\expandafter\CSgobbleescape\string#1}
{\escapechar-1
\expandafter\expandafter\expandafter\gdef
  \expandafter\expandafter\expandafter\CSgobblearrow
    \expandafter\string\csname macro:->\endcsname{}
}
\def\CSgobbleescape#1{\ifnum`\\=`#1 \else #1\fi}
\def\WriteStreamLine#1{\def\CStmp{#1}%
    \immediate\write\PDFStream{\CSstringmeaning\CStmp}}

\def\AfterIncludedStream
   {\immediate\closeout\PDFStream  %changed on 2001/1/20
    \relax
    }%
\def\BeforeIncludedStream
   {\stepcounter{psfig}\xdef\PDFCutFile{\PDFTfigname.tex}%
    \message{Opening PDFStream=\PDFCutFile}%
    \immediate\openout\PDFStream=\PDFCutFile
    \immediate\write\PDFStream{\string\documentclass{article}}
    \immediate\write\PDFStream{\string\input\space tmp.inputs}
    \immediate\write\PDFStream{\string\pagestyle{empty}}
    \immediate\write\PDFStream{\string\usepackage{amssymb,amsbsy}}
    \immediate\write\PDFStream{\string\begin{document}}
    \let\ThisStream\WriteStreamLine}
\long\def\specialstream #1#2#3{%
     \message{Special stream '#1'}%
    \csarg\def{After#1Stream}{#2\AfterIncludedStream#3}%
    \csarg\def{#1}{\BeforeIncludedStream\relax
          \ProcessStream{#1}}%
    \PDFEndDef{#1}}
\def\EndOfStream#1{\endgroup\end{#1}%
    \csname After#1Stream\endcsname}
\def\PDFEndDef#1{{\escapechar=-1\relax
    \csarg\xdef{End#1Test}{\string\\end\string\{#1\string\}}%
    }}
%%
%% The real meat of psfile manipulation starts here.
%%
%%
\AtEndDocument{\endPShook%
		\ifPDFTnopdf
        \PackageWarningNoLine{pdftricks}
        {******************************************\MessageBreak
         Some PDF files of images were not found.\MessageBreak
         Run the script `pst2pdf' before the next\MessageBreak
         run of pdfLaTeX\MessageBreak
         ******************************************}
		 \fi
}
\gdef\endPShook{}
\def\noprocess{\global\noprocesstrue
  \PackageWarning{pdftricks}
        {******************************************\MessageBreak
		     Figure Number: \PDFTfigname\space is not processed \MessageBreak
         ******************************************\MessageBreak}
}
\specialstream{pdfpic}{%
         \immediate\write\PDFStream{\streaminfo}}
        {\psgraphicsinclude\global\noprocessfalse}
\newcounter{psfig}
\newif\if@pdfGINwidth
\newif\if@pdfGINheight
\newif\if@pdfGINscale
\long\gdef\psgraphicsinclude{%
  \@ifundefined{Fig\thepsfig}
  {\PDFTWarningNoLine{pdftricks}{************ PROCESSING FIG: \thepsfig\space **********}}
  {\noprocess}
   \ifPDFTshell\ifnoprocess\relax\else
    \IfFileExists{\PDFTfigname.tex}{%
     \immediate\write18{latex -interaction=batchmode \PDFTfigname}
     \PDFTWarning{pdftricks3}{\PDFTfigname.tex converted to \PDFTfigname.dvi}}{}
    \IfFileExists{\PDFTfigname.dvi}{%
     \immediate\write18{dvips -E -o \PDFTfigname.eps \PDFTfigname}
     \PDFTWarning{pdftricks}{EPS generated -- \PDFTfigname.eps}}{}
    \IfFileExists{\PDFTfigname.eps}{%
      \immediate\write18{epstopdf \PDFTfigname.eps}
      \PDFTWarning{pdftricks3}{\PDFTfigname.eps converted to \PDFTfigname.pdf}}{}
    \immediate\write18{rm \PDFTfigname.aux \PDFTfigname.dvi \PDFTfigname.log \PDFTfigname.eps}
   \fi\fi
%  \fi
  \IfFileExists{\PDFTfigname.pdf}%
     {\begin{center}
     \bgroup\fboxsep\@PDFboxsep\fboxrule\@PDFboxrule%
      \color{\@PDFgraphiccolor}%
      \fcolorbox{\@PDFgraphiclinecolor}{\@PDFgraphicbackground}%
     {\if@pdfGINwidth%
       \includegraphics[width=\@PDFgraphicwidth]{\PDFTfigname}\else%
      \if@pdfGINheight%
       \includegraphics[height=\@PDFgraphicheight]{\PDFTfigname}\else%
      \if@pdfGINscale%
       \includegraphics[scale=\@PDFgraphicscale]{\PDFTfigname}\else%
       \includegraphics{\PDFTfigname}\fi\fi\fi%
     }\egroup\end{center}%
      \global\@pdfGINwidthfalse\let\@PDFgraphicwidth\relax
      \global\@pdfGINheightfalse\let\@PDFgraphicheight\relax
      \global\@pdfGINscalefalse\let\@PDFgraphicscale\relax
      }{\PDFTnopdftrue}
   \gdef\@PDFgraphiclinecolor{white}
   \gdef\@PDFgraphicbackground{white}
   \gdef\@PDFboxsep{0pt}
   \gdef\@PDFboxrule{0pt}
}
\definecolor{gray30}{gray}{.70}
\definecolor{gray10}{gray}{.90}
\RequirePackage{keyval}
\def\configure[#1][#2]{\setkeys{#1}{#2}
  \PDFTWarning{pdftricks}{Reconfigured #1 parameter(s)\MessageBreak  #2\MessageBreak}
  }
\define@key{pdfgraphic}{width}     {\gdef\@PDFgraphicwidth{#1}\global\@pdfGINwidthtrue}
\define@key{pdfgraphic}{height}    {\gdef\@PDFgraphicheight{#1}\global\@pdfGINheighttrue}
\define@key{pdfgraphic}{scale}     {\gdef\@PDFgraphicscale{#1}\global\@pdfGINscaletrue}
\define@key{pdfgraphic}{color}     {\gdef\@PDFgraphiccolor{#1}}
\define@key{pdfgraphic}{linecolor} {\gdef\@PDFgraphiclinecolor{#1}}
\define@key{pdfgraphic}{background}{\gdef\@PDFgraphicbackground{#1}}
\define@key{pdfgraphic}{linewidth} {\gdef\@PDFboxrule{#1}}
\define@key{pdfgraphic}{rulesep}   {\gdef\@PDFboxsep{#1}}
\gdef\@PDFgraphiccolor{black}
\gdef\@PDFgraphiclinecolor{white}
\gdef\@PDFgraphicbackground{white}
\gdef\@PDFboxrule{0pt}
\gdef\@PDFboxsep{0pt}
%%
%% Tweak to grab all the packages used in the master doc.
%% This forces you to load pdftricks as the first package.
%%
\newenvironment{psinputs}{\begingroup
 \newwrite\CVinputs
 \immediate\openout\CVinputs=tmp.inputs
  \def\usepackage{\@ifnextchar[\@CVUsepackage\@@CVUsepackage}
  \def\@CVUsepackage[##1]##2{\immediate\write\CVinputs%
     {\string\usepackage[##1]{##2}}}
  \def\@@CVUsepackage##1{\immediate\write\CVinputs%
     {\string\usepackage{##1}}}
 }
 {\endgroup\immediate\closeout\CVinputs}
%%
%% Arrays to keep the fig numbers
%%
\makeatletter
\newcounter{arraylength}%
\newcounter{ArrayIndex}%
\newcounter{zeroCtr}%
\newcounter{recordCtr}
\setcounter{recordCtr}{1}
\newcounter{Ctr}
\def\DeclareArray#1{\Array{#1}[0]{}}%
%
\def\Array#1[#2]#3{%
		\expandafter\gdef\csname #1#2\endcsname{#3}%
		\expandafter\gdef\csname #1\endcsname[##1]{\csname #1##1\endcsname}}%
%
\def\getArraylength#1{\setcounter{arraylength}{0}%
	\loop\expandafter\ifx\csname #1\thearraylength\endcsname\relax%
	\else\stepcounter{arraylength}\repeat}%
%
\def\addToArray#1#2{\setcounter{arraylength}{0}%
	\loop\expandafter\ifx\csname #1\thearraylength\endcsname\relax%
	\else\stepcounter{arraylength}\repeat%
	\Array{#1}[\thearraylength]{#2}}%
%
\def\clearArray#1{\getArraylength{#1}%
	\loop\ifnum\c@arraylength >0%
	\global\expandafter\let\csname #1\thearraylength\endcsname\relax%
	\addtocounter{arraylength}{-1}\repeat}%
%
\long\def\ArrayIterator#1#2{%
	\setcounter{ArrayIndex}{1}\getArraylength{#1}%
	\setcounter{zeroCtr}{\c@arraylength}%
	\loop\ifnum\c@ArrayIndex<\c@zeroCtr{#2}%
	\stepcounter{ArrayIndex}\repeat%
}%
\def\@nnil{\@nil}
\def\@empty{}
\def\@cvrstop#1\@@#2{}
%%
%% Equivalent of \@tfor and \@for where any delimiter can be 
%% provided instead of LaTeX's default comma character
%%
\long\def\cvr@delimfor#1#2#3{\DeclareArray{#1}\clearArray{#1}%
	\long\def\@icvrloop##1#2##2\@@##3{\def##3{##1}\ifx ##3\@nnil%
  	\expandafter\@cvrstop \else\addToArray{#1}{##1}%
    \relax\expandafter\@icvrloop\fi##2\@@##3}%
	\long\def\@cvrloop##1#2##2#2##3\@@##4{\addToArray{#1}{##1}%
	\def##4{##1}\ifx ##4\@nnil \else%
    \def##4{##2}\def\y@y{##2}\ifx\y@y\@nnil\else%
			\addToArray{#1}{##2}\fi\ifx ##4\@nnil \else%
		\@icvrloop ##3\@@##4\fi\fi}%
  \expandafter\def\expandafter\@fortmp\expandafter{#3}%
  \ifx\@fortmp\@empty \else%
    \expandafter\@cvrloop#3#2\@nil#2\@nil\@@\@ee@\fi}%
%
% Dont look into the following code. It is harmful
% for your eyes and brain as well.
%
\newcounter{f@irstCtr}
\newcounter{s@econdCtr}
\long\gdef\NoProcess[#1]{%
	\long\def\@i@@noprocess##1,##2\@@##3{\def##3{##1}\ifx ##3\@nnil%
  	\expandafter\@cvrstop \else
		\expandafter\hypencheck##1-@-*[*]
    \relax\expandafter\@i@@noprocess\fi##2\@@##3}%
	\long\def\@@@noprocess##1,##2,##3\@@##4{
		\expandafter\hypencheck##1-@-*[*]
	\def##4{##1}\ifx ##4\@nnil \else%
    \def##4{##2}\def\y@y{##2}\ifx\y@y\@nnil\else%
		\expandafter\hypencheck##2-@-*[*]
			\fi\ifx ##4\@nnil \else%
		\@i@@noprocess ##3\@@##4\fi\fi}%
  \expandafter\def\expandafter\@fortmp\expandafter{#1}%
  \ifx\@fortmp\@empty \else%
    \expandafter\@@@noprocess#1,\@nil,\@nil\@@\@ee@\fi}%
\def\d@d#1[*]{}
\def\hypencheck#1-#2-#3{\def\r@r{@}\def\s@s{*}\edef\c@c{#3}
	\ifx\c@c\r@r
	\setcounter{f@irstCtr}{#1}
	\setcounter{s@econdCtr}{#2}
	\stepcounter{s@econdCtr}
	\loop\ifnum\thes@econdCtr > \thef@irstCtr% 
		\expandafter\edef\csname Fig\thef@irstCtr\endcsname{TRUE}
		\stepcounter{f@irstCtr}
	\repeat%
	\else\ifx\c@c\s@s% 
		\expandafter\edef\csname Fig#1\endcsname{TRUE}
	\fi\fi\d@d}

%%
%%
%% End of file `pdftricks.sty'
%%
