%
% 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
%
% 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.14 2001/03/03 08:22:26 cvr Exp $
%
\NeedsTeXFormat{LaTeX2e}
\def\Fileversion$#1: #2 ${\gdef\fileversion{#2}}
\def\Filedate$#1: #2 #3 ${\gdef\filedate{#2}}
\Fileversion$Revision: 1.14 $
\Filedate$Date: 2001/03/03 08:22:26 $
\ProvidesPackage{pdftricks}
   [\filedate\space\fileversion\space psTricks support in PDF (CVR)]
\PackageWarningNoLine{pdftricks}
   {****************************************\MessageBreak
    Package pdftricks v,\fileversion\space loaded\MessageBreak
    [psTricks support in PDF (CVR)]\MessageBreak
    ****************************************}
\RequirePackage{graphicx,color}
\newif\if@debug\@debugfalse
\DeclareOption{debug}{\@debugtrue}
\ProcessOptions
\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
\def\PDFCutFile{\jobname.images}
\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}}
\AtBeginDocument{\immediate\openout\PDFStream=\PDFCutFile}
\def\AfterIncludedStream
   {\immediate\closeout\PDFStream  %changed on 2001/1/20
    \relax
    }%
\def\BeforeIncludedStream
   {\immediate\openout\PDFStream=\PDFCutFile
    \immediate\write\PDFStream{\string\documentclass{article}}
    \immediate\write\PDFStream{\string\input{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.
%%
\newwrite\job
\AtBeginDocument{\immediate\openout\job=psrun
  \PDFTWarning{pdftricks}{Psrun.tex job opened}}
\AtEndDocument{\endPShook%
        \PackageWarningNoLine{pdftricks}
        {******************************************\MessageBreak
         Run the script `psrun' before the next\MessageBreak
         run of pdfLaTeX\MessageBreak
         ******************************************}
 }
\gdef\endPShook{%
         \immediate\closeout\job%
         \immediate\write18{mv psrun.tex psrun}
         \immediate\write18{chmod +x psrun}}
\specialstream{pdfpic}{\immediate\closeout\job%%
         \immediate\write\PDFStream{\streaminfo}}
        {\psgraphicsinclude}
\newcounter{psfig}
\newif\if@pdfGINwidth
\newif\if@pdfGINheight
\newif\if@pdfGINscale
\long\gdef\psgraphicsinclude{\stepcounter{psfig}
  \immediate\write18{mv \jobname.images fig\thepsfig.tex}
  \PDFTWarning{pdftricks}{Moved \jobname.images to fig\thepsfig.tex}
  \IfFileExists{fig\thepsfig.dvi}{%
   \immediate\write18{dvips -o fig\thepsfig.ps fig\thepsfig}
   \immediate\write18{ps2eps -f -c -l fig\thepsfig.ps}
   \PDFTWarning{pdftricks}{EPS generated -- fig\thepsfig.eps}}{}
  \IfFileExists{fig\thepsfig.eps}{%
   \immediate\write18{epstopdf fig\thepsfig.eps}
   \PDFTWarning{pdftricks}{fig\thepsfig.eps converted to fig\thepsfig.pdf}}{}
   \IfFileExists{fig\thepsfig.pdf}%
     {\begin{center}
     \bgroup\fboxsep\@PDFboxsep\fboxrule\@PDFboxrule%
      \color{\@PDFgraphiccolor}%
      \fcolorbox{\@PDFgraphiclinecolor}{\@PDFgraphicbackground}%
     {\if@pdfGINwidth%
       \includegraphics[width=\@PDFgraphicwidth]{fig\thepsfig}\else%
      \if@pdfGINheight%
       \includegraphics[height=\@PDFgraphicheight]{fig\thepsfig}\else%
      \if@pdfGINscale%
       \includegraphics[scale=\@PDFgraphicscale]{fig\thepsfig}\else%
       \includegraphics{fig\thepsfig}\fi\fi\fi%
     }\egroup\end{center}%
      \global\@pdfGINwidthfalse\let\@PDFgraphicwidth\relax
      \global\@pdfGINheightfalse\let\@PDFgraphicheight\relax
      \global\@pdfGINscalefalse\let\@PDFgraphicscale\relax
      }{}
   \immediate\write18{echo "latex fig\thepsfig" >> psrun.tex}
   \PDFTWarning{pdftricks}{LaTeX command written to psrun.tex}
   \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) #2}
  }
\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}
%%
%% End of file `pdftricks.sty'
%%
