% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.




% The aux files, needed for reading back coordinates
\def\pgfutil@aux@read@hook{
  \pgfutil@IfFileExists{\jobname.pgf}{\input \jobname.pgf\relax}{}
  \csname newwrite\endcsname\pgfutil@auxout
  \csname openout\endcsname\pgfutil@auxout\jobname.pgf
}

% XColor-like support for ConTeXt

\def\pgfutil@definecolor#1#2#3{\csname pgfutil@emu@#2\endcsname{#1}#3\@nil}

\def\pgfutil@emu@rgb#1#2,#3,#4\@nil{\expandafter\def\csname\string\color@#1\endcsname{\xcolor@{}{}{rgb}{#2,#3,#4}}}
\def\pgfutil@emu@gray#1#2\@nil{\expandafter\def\csname\string\color@#1\endcsname{\xcolor@{}{}{rgb}{#2,#2,#2}}}

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

\def\pgfutil@color#1{%
  \pgfutil@colorlet{.}{#1}%
  \pgfsysprotocol@getcurrentprotocol\pgfutil@emu@temp%
  {%
    \pgfsysprotocol@setcurrentprotocol\pgfutil@empty%
    \pgfsysprotocol@bufferedtrue%
    \pgfsetcolor{.}%
    \expandafter\pgfsys@outerinvoke\expandafter{\pgfsysprotocol@currentprotocol}%
  }%
  \pgfsysprotocol@setcurrentprotocol\pgfutil@emu@temp%
  \aftergroup\pgfutil@reset@color%                
}

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

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

\let\pgfutil@doifcolorelse=\doifcolorelse


\def\pgfutil@reset@color{%
  \pgfsysprotocol@getcurrentprotocol\pgfutil@emu@temp%
  {%
    \pgfsysprotocol@setcurrentprotocol\pgfutil@empty%
    \pgfsysprotocol@bufferedtrue%
    \pgfsetcolor{.}%
    \expandafter\pgfsys@outerinvoke\expandafter{\pgfsysprotocol@currentprotocol}%
  }%
  \pgfsysprotocol@setcurrentprotocol\pgfutil@emu@temp%
}
\expandafter\def\csname\string\color@.\endcsname{\xcolor@{}{}{rgb}{0,0,0}}

\def\pgfutil@colorlet#1#2{%
  \edef\pgf@marshal{#2}%
  \expandafter\pgfutil@in@\expandafter!\expandafter{\pgf@marshal}%
  \ifpgfutil@in@%
    % compute mixture
    {%
      \expandafter\pgfutil@emu@mix\pgf@marshal!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%
    \expandafter\ifx\csname\string\color@#2\endcsname\relax%
      \pgfutil@registercolor{#2}%
    \fi%
    \edef\pgf@marshal{\noexpand\let\expandafter\noexpand\csname\string\color@#1\endcsname=%
                      \expandafter\noexpand\csname\string\color@#2\endcsname}%
    \pgf@marshal%
  \fi%  
}
\def\pgfutil@emu@mix#1!#2!#3!#4\@nil{%
  \expandafter\ifx\csname\string\color@#1\endcsname\relax%
    \pgfutil@registercolor{#1}%
  \fi%
  \expandafter\ifx\csname\string\color@#3\endcsname\relax%
    \pgfutil@registercolor{#3}%
  \fi%
  \expandafter\expandafter\expandafter\pgfutil@emu@unpack\csname\string\color@#1\endcsname%
  \pgf@ya=\pgf@xa%
  \pgf@yb=\pgf@xb%
  \pgf@yc=\pgf@xc%
  \expandafter\expandafter\expandafter\pgfutil@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\pgfutil@emu@unpack#1#2#3#4#5{%
  \pgfutil@emu@@unpack#5\@nil%
}
\def\pgfutil@emu@@unpack#1,#2,#3\@nil{%
  \pgf@xa=#1pt%
  \pgf@xb=#2pt%
  \pgf@xc=#3pt%
}

\def\pgfutil@emu@select#1#2#3#4#5{#5}


\def\pgfutil@registercolor#1{%
  \edef\pgf@temp{\PDFcolor{#1}}%
  \edef\pgf@marshal{\noexpand\pgfutil@in@{ g}{\pgf@temp}}%
  \pgf@marshal%
  \ifpgfutil@in@%
    \expandafter\pgfutil@context@parse@gray\pgf@temp{#1}%
  \else%
    \edef\pgf@marshal{\noexpand\pgfutil@in@{ rg}{\pgf@temp}}%
    \pgf@marshal%
    \ifpgfutil@in@%
      \expandafter\pgfutil@context@parse@rgb\pgf@temp{#1}%
    \else%
      \PackageError{pgf}{Color #1 has an unsupported color model.}{}%
      \pgfutil@definecolor{#1}{gray}{0}
    \fi%
  \fi%
}

\def\pgfutil@context@parse@gray#1 g#2{%
  \pgfutil@definecolor{#2}{gray}{#1}
}

\def\pgfutil@context@parse@rgb#1 #2 #3 rg#4{%
  \pgfutil@definecolor{#4}{rgb}{#1,#2,#3}
}



% pgfutil@minipage

\def\pgfutil@minipage[#1]#2{%
  \hbox to#2\bgroup%
    \hsize=#2\relax%
    \vbox\bgroup\leavevmode%
}
\def\pgfutil@endminipage{\egroup\egroup}



% Driver detector (how should we do this in ConTeXt?) :

\ifx\pdfoutput\@undefined\newcount\pdfoutput\fi
\ifx\pdfoutput\relax\newcount\pdfoutput\fi
\ifcase\pdfoutput%
  \gdef\Gin@driver{dvips.def}%
\else%
  \gdef\Gin@driver{pdftex.def}%
\fi%



% Global colors

\let\pgfutil@globalcolorsfalse=\relax
\let\pgfutil@globalcolorstrue=\relax



% Font stuff

\def\pgfutil@font@tiny{\tfxx}            % How to do this correctly?
\def\pgfutil@font@scriptsize{\tfxx}
\def\pgfutil@font@footnotesize{\tfx}
\def\pgfutil@font@small{\tfx}
\def\pgfutil@font@normalsize{\tf}
\def\pgfutil@font@large{\tfa}
\def\pgfutil@font@Large{\tfb}
\def\pgfutil@font@huge{\tfc}
\def\pgfutil@font@Huge{\tfc}

\def\pgfutil@font@itshape{\it}
\def\pgfutil@font@bfseries{\bf}

\let\pgfutil@font@normalfont=\rm

\let\pgfutil@selectfont=\rm


% Extra counters, registers, boxes

\newcount\pgfutil@tempcnta
\newcount\pgfutil@tempcntb

\newbox\pgfutil@tempboxa

\newdimen\pgfutil@tempdima
\newdimen\pgfutil@tempdimb


% Module stuff

\def\pgfutil@usemodule#1{\usemodule[#1]}



% End of job stuff

\appendtoks \the\pgfutil@everybye \to \everybye




% The following is still messy and needs to be cleanup up (everything
% prefixed by pgfutil@): 

\def\PackageInfo#1#2{}
\def\PackageWarning#1#2{\immediate\write-1{Package #1: Warning! #2.}}%
\def\PackageError#1#2#3{\immediate\write-1{Package #1: Error! #2.}}%
\long\def\AtBeginDocument#1{#1}%
\long\def\AtBeginDvi#1{#1}%

\endinput
