%% BEGIN pst-eps.tex
%%
%% Writing EPS files with PSTricks 97.
%% See the PSTricks User's Guide for description.
%%
%%
%% COPYRIGHT 1993, 1994, 1999 by Timothy Van Zandt, tvz@nwu.edu.
%% COPYRIGHT 2006 by Herbert Voss, hvoss@tug.org.
%%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN
%% archives in directory macros/latex/base/lppl.txt.
%%

\csname PSTfilesLoaded\endcsname
\let\PSTfilesLoaded\endinput
\ifx\PSTricksLoaded\endinput\else
  \def\next{\input pstricks.tex}
  \expandafter\next
\fi
\ifx\PSTXKeyLoaded\endinput\else\input pst-xkey\fi
%
\def\fileversion{1.00}
\def\filedate{2006/11/04}
\message{ v\fileversion, \filedate}
%
\edef\TheAtCode{\the\catcode`\@}
\catcode`\@=11
\pst@addfams{pst-eps}

% Exporting PostScript files

% DG/SR modification begin - May 5, 1997 - Patch 1
% With LaTeX 2e, we must force now inclusion of header files
%\def\TeXtoEPS{\begingroup\pst@makebox{}\bgroup\ignorespaces}
\def\TeXtoEPS{%
  \ifx\documentclass\@undefined\else\@begindvi\fi
  \begingroup\pst@makebox{}\bgroup\ignorespaces}
% DG/SR modification end
\def\endTeXtoEPS{%
    \egroup
% Get contents of page, in case it contains special's:
    \begingroup
      \global\setbox\pst@boxg\box\voidb@x
      \output{\global\setbox\pst@boxg\box\@cclv}%
      \par\hbox{}\penalty-10000
    \endgroup
    \dp\pst@boxg\z@
    \ht\pst@boxg\z@
    \wd\pst@boxg\z@
  \pst@dimg=3pt
  \shipout\hbox{%
    \box\pst@boxg
    \pst@Verb{gsave CM \tx@STV CP newpath moveto
      \pst@number\pst@dimg neg 0 rmoveto clip setmatrix}%
    \vrule width \wd\pst@hbox height \ht\pst@hbox depth \dp\pst@hbox
    \pstVerb{currentpoint grestore moveto}%
    \kern -\wd\pst@hbox
    \unhbox\pst@hbox}%
  \endgroup
  \typeout{PSTricks: Converting TeX box to EPS.}%
  \typeout{\@spaces\@spaces\space\space With dvips, use -E option.}}

% Writing PSTricks graphics to EPS files

\newwrite\pst@epsout
\newwrite\pst@tempout

\def\pst@getbp#1#2#3{%
  \csname psset#1length\endcsname\pst@dimg{#2}%
  \advance\pst@dimg .49bp
  \pst@cntg=\pst@dimg
  \multiply\pst@cntg 5
  \divide\pst@cntg 328909
  \edef#3{\the\pst@cntg}}

\define@key[psset]{pst-eps}{bbllx}{\pst@getbp{x}{#1}\psk@bbllx}
\define@key[psset]{pst-eps}{bblly}{\pst@getbp{y}{#1}\psk@bblly}
\define@key[psset]{pst-eps}{bburx}{\pst@getbp{x}{#1}\psk@bburx}
\define@key[psset]{pst-eps}{bbury}{\pst@getbp{y}{#1}\psk@bbury}
\psset[pst-eps]{bbllx=-1pt,bblly=-1pt,bburx=1pt,bbury=1pt}

\define@key[psset]{pst-eps}{headers}{%
  \def\pst@tempg{#1}
  \ifx\pst@tempg\@none
    \def\psk@headers{\z@}%		0 -> none
  \else
    \def\pst@temph{all}%
    \ifx\pst@tempg\pst@temph
      \def\psk@headers{2\relax}%	2 -> all
    \else
        \def\psk@headers{1\relax}%	1 -> user
    \fi
  \fi}
%
\define@key[psset]{pst-eps}{headerfile}{\def\psk@headerfile{#1}}
\define@boolkey[psset]{pst-eps}[Pst@]{checkfile}[true]{}
\psset[pst-eps]{headers=none,checkfile=true,headerfile={}}

\define@key[psset]{pst-eps}{makeeps}{%
  \def\pst@tempg{#1}%
  \ifx\pst@tempg\@none
    \def\psk@makeeps{\z@}%		0 -> none
  \else
    \def\pst@temph{all}%
    \ifx\pst@tempg\pst@temph
      \def\psk@makeeps{3\relax}%	3 -> all
    \else
      \def\pst@temph{all*}%		2 -> all*
      \ifx\pst@tempg\pst@temph
        \def\psk@makeeps{2\relax}%
      \else
        \def\psk@makeeps{1\relax}%	1 -> new
      \fi
    \fi
  \fi}
\psset[pst-eps]{makeeps=new}
%
\def\pst@checkfile#1{%
  \typeout{pst-eps: check for file #1}
  \ifcase\psk@makeeps
    \@pstfalse
  \or
    \immediate\openin1=#1
    \ifeof1\relax\@psttrue\else\@pstfalse\fi
    \immediate\closein1
  \or
    \immediate\openin1=#1
    \ifeof1
      \@psttrue
    \else
      \ifPst@checkfile
        \typeout{^^J
        PSTricks warning:^^J
        !! File `#1' exists and will be erased if in current directory!^^J
        !! Make `#1'? (y=yes; n=no)}
        \read16 to \pst@tempg
        \if y\pst@tempg\relax \@psttrue\else\@pstfalse\fi
      \else\@psttrue\fi
    \fi
    \immediate\closein1
  \or\@psttrue
  \fi}
%
\def\PSTtoEPS{\def\pst@par{}\pst@object{PSTtoEPS}}
\begingroup
\catcode`\%=12
\catcode`\"=14
\long\gdef\PSTtoEPS@i#1#2{"
  \begingroup
    \use@par
    \newlinechar`\^^J"
    \pst@checkfile{#1}"
    \if@pst
      \immediate\openout\pst@epsout=#1
      \def\write@eps##1{\immediate\write\pst@epsout{##1}}"
      \def\addto@pscode##1{"
        \begingroup
          \newlinechar`\ "
          \write@eps{##1}"
        \endgroup}
      \write@eps{"
        %!PS-Adobe-3.0 EPSF-3.0^^J"
        %%Creator: PSTricks^^J"
        %%Title: #1^^J"
        %%CreationDate: \the\year/\the\month/\the\day^^J"
        %%BoundingBox:
          \psk@bbllx\space\psk@bblly\space\psk@bburx\space\psk@bbury^^J"
        %%EndComments}"
      \ifcase\psk@headers
        \def\pst@tempg{}"
      \or
        \let\pst@tempg\psk@headerfile
      \or
        \edef\pst@tempg{\pst@theheaders,\psk@headerfile}"
      \fi
      \ifx\pst@tempg\@empty\else
        \expandafter\pst@writeheaders\pst@tempg,\@nil
      \fi
      \write@eps{%%BeginSetup}"
      \addto@pscode\pst@dict
      \write@eps{%%EndSetup}"
      \ifdim\pstunit=1bp\else
        \pst@dimg=\pstunit\relax
        \write@eps{\number\pst@dimg\space 65781.8 div dup scale}"
      \fi
      \addto@pscode{"
        \tx@STP
        0 setlinecap
        0 setlinejoin
        10 setmiterlimit
        [] 0 setdash
        newpath}"
      \setbox\pst@hbox=\hbox{"
        \def\init@pscode{"
          \write@eps{"
            gsave
            \psk@origin
            \psk@swapaxes
            \pst@number\pslinewidth SLW
            \pst@usecolor\pslinecolor}}"
        \def\use@pscode{\write@eps{grestore}}"
        \let\pst@rawfile\pst@filetoeps
        \def\psclip#1{\pst@misplaced\psclip}"
        \let\endpsclip\relax
        \def\pstextpath@@@[##1](##2,##3)##4{\pst@misplaced\pstextpath}"
        \def\nc@object##1##2##3##4{\pst@misplaced{node connection}}"
        \def\PSTtoEPS@i##1##2{\pst@misplaced\PSTtoEPS}"
        #2}"
      \write@eps{"
        %%Trailer^^J"
        end^^J"
        %%EOF}"
      \immediate\closeout\pst@epsout
    \fi
  \endgroup
  \ignorespaces}

\gdef\pst@writeheaders#1,{"
  \def\pst@tempg{#1}"
  \ifx\pst@tempg\@empty\else
    \write@eps{%%BeginProcSet: #1}"
    \pst@filetoeps{#1}"
    \write@eps{%%EndProcSet}"
  \fi
  \@ifnextchar\@nil{\let\pst@tempg}{\pst@writeheaders}}
\endgroup

\def\pst@filetoeps#1{%
  \begingroup
    \def\do##1{\catcode`##1=12\relax}%
    \dospecials
    \def\addto@pscode##1{\write@eps{##1}}%
    \pst@@rawfile{#1}%
  \endgroup}

\catcode`\@=\TheAtCode\relax

\endinput
%%
%% END pst-eps.tex
