% This file is pgf-latex-emulation.tex

% Copyright 2005 by Till Tantau <tantau@cs.tu-berlin.de>.
%
% 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.

\catcode`\@=11\relax

\def\pgf@test@fmtname{LaTeX2e}
\expandafter\ifx\csname fmtname\endcsname\pgf@test@fmtname%
  \PackageError{pgf}{This file should not be loaded with \LaTeX.}{}
\else%

% Direct copy from latex.ltx:
\long\def\@ifundefined#1#2#3{\expandafter\ifx\csname #1\endcsname\relax#2\else#3\fi}
\def\typeout#1{\immediate\write\@unused{#1}}
\alloc@7\write\chardef\sixt@@n\@unused
\let\@@par=\par
\def\@empty{}
\def\@gobble #1{}
\def\@gobbletwo #1#2{}
\long\def\@ifnextchar#1#2#3{%
  \let\reserved@d=#1%
  \def\reserved@a{#2}%
  \def\reserved@b{#3}%
  \futurelet\@let@token\@ifnch}
\def\@ifnch{%
  \ifx\@let@token\@sptoken
    \let\reserved@c\@xifnch
  \else
    \ifx\@let@token\reserved@d
      \let\reserved@c\reserved@a
    \else
      \let\reserved@c\reserved@b
    \fi
  \fi
  \reserved@c}
\def\in@#1#2{%
 \def\in@@##1#1##2##3\in@@{%
  \ifx\in@##2\in@false\else\in@true\fi}%
 \in@@#2#1\in@\in@@}
\newif\ifin@
\newdimen\@tempdima
\newdimen\@tempdimb
\newcount\@tempcnta
\newcount\@tempcntb
\def\@namedef#1{\expandafter\def\csname #1\endcsname}
\long\def\g@addto@macro#1#2{%
  \begingroup
    \toks@\expandafter{#1#2}%
    \xdef#1{\the\toks@}%
 \endgroup}
\long\def\mbox#1{\leavevmode\hbox{#1}}
\begingroup
\def\:{\global\let\@sptoken= } \:
\def\:{\@xifnch} \expandafter\def\: {\futurelet\@let@token\@ifnch}
\endgroup
\def\@nnil{\@nil}
\def\@fornoop#1\@@#2#3{}
\long\def\@for#1:=#2\do#3{%
  \expandafter\def\expandafter\@fortmp\expandafter{#2}%
  \ifx\@fortmp\@empty \else
    \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}\fi}
\long\def\@forloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@iforloop #3\@@#4{#5}\fi\fi}
\long\def\@iforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
       \expandafter\@fornoop \else
      #4\relax\expandafter\@iforloop\fi#2\@@#3{#4}}
\def\@tfor#1:={\@tf@r#1 }
\long\def\@tf@r#1#2\do#3{\def\@fortmp{#2}\ifx\@fortmp\space\else
    \@tforloop#2\@nil\@nil\@@#1{#3}\fi}
\long\def\@tforloop#1#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
       \expandafter\@fornoop \else
      #4\relax\expandafter\@tforloop\fi#2\@@#3{#4}}
\chardef\@inputcheck0
\def\IfFileExists#1#2#3{%
  \openin\@inputcheck#1 %
  \ifeof\@inputcheck
     #3\relax
  \else
    #2\relax
  \fi
  \closein\@inputcheck}
\newbox\@tempboxa
\newif\if@tempswa
\long\def\@firstoftwo#1#2{#1}
\long\def\@secondoftwo#1#2{#2}
    
% Simplified version of latex.ltx:
\def\PackageInfo#1#2{}%\typeout{Package #1: Info: #2.}}%
\def\PackageWarning#1#2{\typeout{Package #1: Warning! #2.}}%
\def\PackageError#1#2#3{\typeout{Package #1: Error! #2.}}%
\long\def\AtBeginDocument#1{#1}%
\long\def\AtBeginDvi#1{#1}%
\def\InputIfFileExists#1#2#3{\input #1\relax#2}%
\def\minipage[#1]#2{%
  \hbox to#2\bgroup%
    \hsize=#2\relax%
    \vbox\bgroup\leavevmode%
}
\def\endminipage{\egroup\egroup}
\def\providecommand#1#2{\def#1{#2}}

\def\newcounter#1{\expandafter\alloc@\expandafter0\expandafter\count\expandafter\countdef\expandafter\insc@unt\csname c@#1\endcsname}%
\def\setcounter#1#2{\global\csname c@#1\endcsname#2\relax}%
\def\addtocounter#1#2{\global\advance\csname c@#1\endcsname #2\relax}
\def\setlength#1#2{#1#2\relax}
\def\addtolength#1#2{\advance#1 #2\relax}


% Driver detector (not so good):
\ifx\pdfoutput\@undefined\alloc@0\count\countdef\insc@unt\pdfoutput\fi
\ifx\pdfoutput\relax\alloc@0\count\countdef\insc@unt\pdfoutput\fi
\ifcase\pdfoutput%
  \gdef\Gin@driver{dvips.def}%
\else%
  \gdef\Gin@driver{pdftex.def}%
\fi%


% Things that are not too clever, yet:
\def\selectfont{\rm}
\def\applycolormixins#1{}


% My ultra-quick xcolor.sty emulation...
\newif\ifglobalcolors
\def\definecolor#1#2#3{\csname pgf@emu@#2\endcsname{#1}#3\@nil}
\def\pgf@emu@rgb#1#2,#3,#4\@nil{\expandafter\def\csname\string\color@#1\endcsname{\xcolor@{}{}{rgb}{#2,#3,#4}}}
\def\pgf@emu@gray#1#2\@nil{\expandafter\def\csname\string\color@#1\endcsname{\xcolor@{}{}{rgb}{#2,#2,#2}}}

\definecolor{white}{gray}{1}
\definecolor{black}{gray}{0}
\definecolor{gray}{gray}{0.5}
\definecolor{red}{rgb}{1,0,0}
\definecolor{green}{rgb}{0,1,0}
\definecolor{blue}{rgb}{0,0,1}
\definecolor{cyan}{rgb}{0,1,1}
\definecolor{magenta}{rgb}{1,0,1}
\definecolor{yellow}{rgb}{1,1,0}
\definecolor{orange}{rgb}{1,0.5,0}
\definecolor{violet}{rgb}{0.5,0,0.5}
\definecolor{purple}{rgb}{0.75,0,0.25}
\definecolor{brown}{rgb}{0.75,0.5,0.25}

\def\color#1{%
  \pgfsysprotocol@getcurrentprotocol\pgf@emu@temp%
  {%
    \pgfsysprotocol@setcurrentprotocol\@empty%
    \pgfsysprotocol@bufferedtrue%
    \pgfsetcolor{#1}%
    \expandafter\pgfsys@outerinvoke\expandafter{\pgfsysprotocol@currentprotocol}%
  }%
  \pgfsysprotocol@setcurrentprotocol\pgf@emu@temp%
  \edef\pgf@marshal{\noexpand\let\expandafter\noexpand\csname\string\color@.\endcsname=%
                    \expandafter\noexpand\csname\string\color@#1\endcsname}%
  \pgf@marshal%
  \aftergroup\reset@color%                
}
\def\reset@color{%
  \pgfsysprotocol@getcurrentprotocol\pgf@emu@temp%
  {%
    \pgfsysprotocol@setcurrentprotocol\@empty%
    \pgfsysprotocol@bufferedtrue%
    \pgfsetcolor{.}%
    \expandafter\pgfsys@outerinvoke\expandafter{\pgfsysprotocol@currentprotocol}%
  }%
  \pgfsysprotocol@setcurrentprotocol\pgf@emu@temp%
}
\expandafter\def\csname\string\color@.\endcsname{\xcolor@{}{}{rgb}{0,0,0}}


\def\colorlet#1#2{%
  \in@!{#2}%
  \ifin@%
    % compute mixture
    {%
      \pgf@emu@mix#2!white!\@nil%
      \xdef\pgf@marshal{\noexpand\def\expandafter\noexpand\csname\string\color@#1\endcsname{%
          \noexpand\xcolor@{}{}{rgb}{\pgf@sys@tonumber\pgf@xa,\pgf@sys@tonumber\pgf@xb,\pgf@sys@tonumber\pgf@xc}}}%
    }%
    \pgf@marshal%
  \else%
    \edef\pgf@marshal{\noexpand\let\expandafter\noexpand\csname\string\color@#1\endcsname=%
                      \expandafter\noexpand\csname\string\color@#2\endcsname}%
    \pgf@marshal%
  \fi%  
}
\def\pgf@emu@mix#1!#2!#3!#4\@nil{%
  \expandafter\expandafter\expandafter\pgf@emu@unpack\csname\string\color@#1\endcsname%
  \pgf@ya=\pgf@xa%
  \pgf@yb=\pgf@xb%
  \pgf@yc=\pgf@xc%
  \expandafter\expandafter\expandafter\pgf@emu@unpack\csname\string\color@#3\endcsname%
  \c@pgf@counta=#2\relax%
  \c@pgf@countb=100\relax%
  \advance\c@pgf@countb by-\c@pgf@counta\relax%
  \pgf@xa=\c@pgf@countb\pgf@xa%
  \advance\pgf@xa by\c@pgf@counta\pgf@ya%
  \divide\pgf@xa by 100\relax%
  \pgf@xb=\c@pgf@countb\pgf@xb%
  \advance\pgf@xb by\c@pgf@counta\pgf@yb%
  \divide\pgf@xb by 100\relax%
  \pgf@xc=\c@pgf@countb\pgf@xc%
  \advance\pgf@xc by\c@pgf@counta\pgf@yc%
  \divide\pgf@xc by 100\relax%
}
\def\pgf@emu@unpack#1#2#3#4#5{%
  \pgf@emu@@unpack#5\@nil%
}
\def\pgf@emu@@unpack#1,#2,#3\@nil{%
  \pgf@xa=#1pt%
  \pgf@xb=#2pt%
  \pgf@xc=#3pt%
}

\def\extractcolorspec#1#2{%
  \def#2{{#1}}%
}

\def\convertcolorspec#1#2#3{%
  \edef#3{\expandafter\expandafter\expandafter\pgf@emu@select\csname\string\color@#1\endcsname}%
}
\def\pgf@emu@select#1#2#3#4#5{#5}




% Adapted from keyval.sty:
\newtoks\KV@toks@

\ifx\setkeys\@undefined
\def\setkeys#1#2{%
  \def\KV@prefix{KV@#1@}%
  \let\@tempc\relax
  \KV@do#2,\relax,}
\def\KV@do#1,{%
 \ifx\relax#1\empty\else
  \KV@split#1==\relax
  \expandafter\KV@do\fi}
\def\KV@split#1=#2=#3\relax{%
  \KV@@sp@def\@tempa{#1}%
  \ifx\@tempa\@empty\else
    \expandafter\let\expandafter\@tempc
      \csname\KV@prefix\@tempa\endcsname
    \ifx\@tempc\relax
      \KV@errx
       {\@tempa\space undefined}%
    \else
      \ifx\@empty#3\@empty
        \KV@default
      \else
        \KV@@sp@def\@tempb{#2}%
        \expandafter\@tempc\expandafter{\@tempb}\relax
      \fi
    \fi
  \fi}
\def\KV@default{%
  \expandafter\let\expandafter\@tempb
    \csname\KV@prefix\@tempa @default\endcsname
  \ifx\@tempb\relax
    \KV@errx{No value specified for \@tempa}%
  \else
    \@tempb\relax
  \fi}
\def\KV@errx#1{\PackageError{keyval}{#1}{}}
\def\@tempa#1{%
\def\KV@@sp@def##1##2{%
  \futurelet\KV@tempa\KV@@sp@d##2\@nil\@nil#1\@nil\relax##1}%
\def\KV@@sp@d{%
  \ifx\KV@tempa\@sptoken
    \expandafter\KV@@sp@b
  \else
    \expandafter\KV@@sp@b\expandafter#1%
  \fi}%
\def\KV@@sp@b#1##1 \@nil{\KV@@sp@c##1}%
  }
\@tempa{ }
\def\KV@@sp@c#1\@nil#2\relax#3{\KV@toks@{#1}\edef#3{\the\KV@toks@}}
\def\define@key#1#2{%
  \@ifnextchar[{\KV@def{#1}{#2}}{\@namedef{KV@#1@#2}####1}}
\def\KV@def#1#2[#3]{%
  \@namedef{KV@#1@#2@default\expandafter}\expandafter
    {\csname KV@#1@#2\endcsname{#3}}%
  \@namedef{KV@#1@#2}##1}
\fi%
\fi%

\endinput
