% Copyright 2007 Mark Wibrow
%
% but subject to the LaTeX Project Public License 
% (http://www.latex-project.org/lppl.txt)
%
% and the GNU Public License 
% (http://www.gnu.org/licenses/gpl.txt)
%

% This file defines utilities common to the \pgfmath files.
%
% Version 1.414213 29/9/2007

\def\pgfmathincluded{}

% \pgfmath@ensureregister
%
% Ensure a control sequence exists as a TeX count/dimen register.
%
% #1 - count or dimen.
% #2 - a control sequence.
%
\def\pgfmath@ensureregister#1#2{%
	\begingroup%
	\setbox0=\hbox\bgroup\begingroup% In case #2 is a defined macro. Unlikely, but you never know...
		\afterassignment\pgfmath@@ensureregister%
		\noexpand#20.0pt\let\pgfmath@temp\relax\relax\pgfmath@#2#1\pgfmath@}

\def\pgfmath@@ensureregister#1#2\pgfmath@#3#4#5\pgfmath@{%
		\endgroup\egroup%
	\endgroup%
	\ifx#4c% 
		\ifx#1.% Already a count register.
		\else%
			\csname newcount\endcsname#3% This gets round \outer in plain TeX.
		\fi%
	\else%
		\ifx#4d% 
			\ifx\let#1% Already a dimen register.
			\else%
				\csname newdimen\endcsname#3%
			\fi%
		\fi%
	\fi%
}

% Check registers exist. If not, create them. 
%
\pgfmath@ensureregister{dimen}{\pgf@x}
\pgfmath@ensureregister{dimen}{\pgf@xa}
\pgfmath@ensureregister{dimen}{\pgf@xb}
\pgfmath@ensureregister{dimen}{\pgf@xc}

\pgfmath@ensureregister{dimen}{\pgf@y}
\pgfmath@ensureregister{dimen}{\pgf@ya}
\pgfmath@ensureregister{dimen}{\pgf@yb}
\pgfmath@ensureregister{dimen}{\pgf@yc}

\pgfmath@ensureregister{dimen}{\pgfutil@tempdima}
\pgfmath@ensureregister{dimen}{\pgfutil@tempdimb}

\pgfmath@ensureregister{count}{\c@pgf@counta}
\pgfmath@ensureregister{count}{\c@pgf@countb}
\pgfmath@ensureregister{count}{\c@pgf@countc}
\pgfmath@ensureregister{count}{\c@pgf@countd}

\pgfmath@ensureregister{count}{\pgfutil@tempcnta}
\pgfmath@ensureregister{count}{\pgfutil@tempcntb}

% Alias for \pgfmath.
%
\let\pgfmath@x\pgf@x
\let\pgfmath@xa\pgf@xa
\let\pgfmath@xb\pgf@xb
\let\pgfmath@xc\pgf@xc

\let\pgfmath@y\pgf@y
\let\pgfmath@ya\pgf@ya
\let\pgfmath@yb\pgf@yb
\let\pgfmath@yc\pgf@yc

\let\pgfmath@tempdima\pgfutil@tempdima
\let\pgfmath@tempdima\pgfutil@tempdima

\let\c@pgfmath@counta\c@pgf@counta
\let\c@pgfmath@countb\c@pgf@countb
\let\c@pgfmath@countc\c@pgf@countc
\let\c@pgfmath@countd\c@pgf@countd

\let\pgfmath@tempcnta\pgfutil@tempcnta
\let\pgfmath@tempcntb\pgfutil@tempcntb

% Now check if pgfutil and pgf@sys stuff exists. If it does, fine.
% Just \let pgfmath versions. If not, define pgfmath versions (Arghhh). 
%

% \...ifnextchar
%
\ifx\pgfutil@ifnextchar\pgfmath@undefined%
	\long\def\pgfmath@ifnextchar#1#2#3{%
  \let\pgfmath@reserved@d#1%
  \def\pgfmath@reserved@a{#2}%
  \def\pgfmath@reserved@b{#3}%
  \futurelet\pgfmath@let@token\pgfmath@ifnch}
	\def\pgfmath@ifnch{%
	  \ifx\pgfmath@let@token\pgfmath@sptoken%
	    \let\pgfmath@reserved@c\pgfmath@xifnch%
	  \else%
	    \ifx\pgfmath@let@token\pgfmath@reserved@d%
	      \let\pgfmath@reserved@c\pgfmath@reserved@a%
	    \else%
	      \let\pgfmath@reserved@c\pgfmath@reserved@b%
	    \fi%
	  \fi%
	  \pgfmath@reserved@c}
	{%
	  \def\:{\global\let\pgfmath@sptoken= } \:
	  \def\:{\pgfmath@xifnch} \expandafter\gdef\: {\futurelet\pgfmath@let@token\pgfmath@ifnch}
	}%
\else%
	\let\pgfmath@ifnextchar\pgfutil@ifnextchar%
	% Still need to define \pgfmath@sptoken (cannot \let).
	{%
		\def\:{\global\let\pgfmath@sptoken= } \:
	}%
\fi%

% \...ifundefined
%
\ifx\pgfutil@ifundefined\pgfmath@undefined%
	\long\def\pgfmath@ifundefined#1{%
	  \expandafter\ifx\csname#1\endcsname\relax%
	    \expandafter\pgfmath@firstoftwo%
	  \else%
	    \expandafter\pgfmath@secondoftwo%
	  \fi}%
\else%
	\let\pgfmath@ifundefined\pgfutil@ifundefined%
\fi%

% \...selectfont
%
\ifx\pgfutil@selectfont\pgfmath@undefined%
	\ifx\selectfont\pgfmath@undefined%
		\let\pgfmath@selectfont\rm% Plain TeX and ConTeXt.
	\else%
		\let\pgfmath@selectfont\selectfont% LaTeX.
	\fi%
\else%
	\let\pgfmath@selectfont\pgfutil@selectfont%
\fi%

% New definition easier than fussing around with let.
%
\newif\ifpgfmath@in@
\def\pgfmath@in@#1#2{%
 \def\pgfmath@in@@##1#1##2##3\pgfmath@in@@{%
  \ifx\pgfmath@in@##2\pgfmath@in@false\else\pgfmath@in@true\fi}%
 \pgfmath@in@@#2#1\pgfmath@in@\pgfmath@in@@}


% \...tonumber
%
\ifx\pgf@sys@tonumber\pgfmath@undefined%
  {\catcode`\p=12\catcode`\t=12\gdef\Pgf@geT#1pt{#1}}
	\def\pgfmath@tonumber#1{\expandafter\Pgf@geT\the#1}
\else%
	\let\pgfmath@tonumber\pgf@sys@tonumber%
\fi%


% \PackageWarning and \PackageError
%
\ifx\PackageWarning\pgfmath@undefined
	\def\pgfmath@PackageWarning#1#2{\immediate\write-1{Package #1: Warning! #2.}}%
\else%
	\let\pgfmath@PackageWarning\PackageWarning%
\fi%
\ifx\PackageError\pgfmath@undefined
	\def\pgfmath@PackageError#1#2#3{\immediate\write-1{Package #1: Error! #2.}}%
\else%
	\let\pgfmath@PackageError\PackageError%
\fi%

\def\pgfmath@error#1#2{%
	\pgfmath@PackageError{PGF Math}{%
		#1%
		\pgfutil@ifundefined{pgfmath@expression}{}{ (in '\pgfmath@expression')}%
	}{#2}%
}
\def\pgfmath@warning#1{%
	\pgfmath@PackageWarning{PGF Math}{%
		#1%
		\pgfutil@ifundefined{pgfmath@expression}{}{ (in '\pgfmath@expression')}%
	}%
}

% \pgfmath@pt
%
% Needed to test for 'pt' resulting from expansion using \the.
%
{\catcode`\p=12\catcode`\t=12\gdef\PgfmaTh@PT{pt}}		
\edef\pgfmath@pt{\PgfmaTh@PT}%

% \pgfmath@empty
%
% A handy macro.
%
\def\pgfmath@empty{}

\def\pgfmath@firstoftwo#1#2{#1}
\def\pgfmath@secondoftwo#1#2{#2}

% \pgfmath@gobbletilpgfmath@
%
% Gobble stream until \pgfmath@ {which is undefined}.
% 
\def\pgfmath@gobbletilpgfmath@#1\pgfmath@{}
\def\pgfmath@gobbleone#1{}%


\def\pgfmath@namedef#1{\expandafter\def\csname#1\endcsname}
\def\pgfmath@nameedef#1{\expandafter\edef\csname#1\endcsname}

% \pgfmathloop
%
% A version of the standard TeX and LaTeX
% \loop, with an additional macro \pgfmathcounter
% (which is *not* a TeX counter) which keeps
% track of the iterations.
%
\newif\ifpgfmathcontinueloop
\def\pgfmathloop#1\repeatpgfmathloop{%
	\def\pgfmathcounter{1}%
	\def\pgfmath@iterate{%
		#1\relax%
		{% Do this inside a group, just in case...
			\c@pgfmath@counta\pgfmathcounter%
			\advance\c@pgfmath@counta1\relax%
			\xdef\pgfmathloop@temp{\the\c@pgfmath@counta}%
		}%	
		\edef\pgfmathcounter{\pgfmathloop@temp}%
		\expandafter\pgfmath@iterate\fi}%
	\pgfmath@iterate\let\pgfmath@iterate\relax}
\let\repeatpgfmathloop\fi
\def\pgfmathbreakloop{\let\pgfmath@iterate\relax}%

% \pgfmath@returnone
%
% #1 - a dimension representing a number or dimension.
%
% Changed by TT: made faster and simpler since it is used
% *heavily*. #1 must be something that can be assigned to a
% dimension. 
%
\def\pgfmath@returnone#1\endgroup{%
  \pgfmath@x#1%
  \edef\pgfmath@temp{\pgfmath@tonumber{\pgfmath@x}}%
  \expandafter\endgroup\expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmath@temp}%
}

\let\pgfmathreturn=\pgfmath@returnone

% \pgfmath@smuggleone
%
% Smuggle a macro outside a group.
%
% Changed by TT: Speedup by insisting, that smuggleone is directly
% followed by \endgroup
%
\def\pgfmath@smuggleone#1\endgroup{%
  \expandafter\endgroup\expandafter\def\expandafter#1\expandafter{#1}}  

\let\pgfmathsmuggle=\pgfmath@smuggleone
