% This file defines utilities common to the \pgfmath files.
%
% (c) 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)
%
% Version 0.0 08/03/2007

% Alias some pgf stuff, just in case it needs replacing later.
%
\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\c@pgfmath@counta\c@pgf@counta
\let\c@pgfmath@countb\c@pgf@countb
\let\c@pgfmath@countc\c@pgf@countc

\let\pgfmath@ifnextchar\pgfutil@ifnextchar

\let\pgfmath@tonumber\pgf@sys@tonumber

\let\pgfmath@ifundefined\pgfutil@ifundefined

\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@@}
 
% Need to redefine to something more appropriate...?
%
\def\pgfmath@error#1#2{\PackageError{PGF Math}{#1}{#2}}
\def\pgfmath@warning#1#2{\PackageWarning{PGF Math}{#1}{#2}}

% \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@ifin@
%
% Interface for \pgfutil@in@
%
\def\pgfmath@ifin@#1#2{%
	\pgfutil@in@{#1}{#2}%
	\ifpgfutil@in@%
		\expandafter\pgfmath@firstoftwo%
	\else%
		\expandafter\pgfmath@secondoftwo%
	\fi%
}

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


% \ifpgfmath@empty
%
% Has #1 been let to \pgf@empty?
%
\def\ifpgfmath@empty#1{%
	\ifx#1\pgfmath@empty\relax
		\expandafter\pgfmath@firstoftwo%
	\else%
		\expandafter\pgfmath@secondoftwo%
	\fi%
}
\def\pgfmath@firstoftwo#1#2{#1}
\def\pgfmath@secondoftwo#1#2{#2}

% \ifpgfmath@ifletto
%
% Has #1 been let to #2?
%
\def\pgfmath@ifletto#1#2{%
	\ifx#1#2\relax%
		\expandafter\pgfmath@firstoftwo%
	\else%
		\expandafter\pgfmath@secondoftwo%
	\fi%
}

% \ifpgfmath@iflettoeither
%
% Has #1 been let to #2 or #3?
%
\def\pgfmath@iflettoeither#1#2#3{%
	\ifx#1#2\relax%
		\let\pgfmath@equaltoeithernext\pgfmath@firstoftwo%
	\else%
		\ifx#1#3\relax%
			\let\pgfmath@equaltoeithernext\pgfmath@firstoftwo%
		\else%
			\let\pgfmath@equaltoeithernext\pgfmath@secondoftwo%
		\fi%
	\fi%
	\pgfmath@equaltoeithernext%
}

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


% \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@counta by1\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


% \pgfmath@returnone
%
% \edef \pfgmathresult to the value of #1 after the end
% of a group.
%
% #1 - a dimension/count/skip register or a macro
%      representing a number or dimension.
%
\def\pgfmath@returnone#1{%
	\afterassignment\pgfmath@gobbletilpgfmath@%
	\pgfmath@x#1pt\relax\pgfmath@%
	\xdef\pgfmath@resulttemp{\pgf@sys@tonumber{\pgfmath@x}}%
	\gdef\pgfmath@returnone@{%
		\edef\pgfmathresult{\pgfmath@resulttemp}%
	}%
	\aftergroup\pgfmath@returnone@}


% \pgfmath@returntwo
%
% \edef \pfgmathresultx to the value of #1, and
% \pgfmathresulty to the value of #2, after the end
% of a group.
%
% #1 - a dimension/count/skip register or a macro
%      representing a number or dimension.
%
% #2 - a dimension/count/skip register or a macro
%      representing a number or dimension.
%	
\def\pgfmath@returntwo#1#2{%
	\afterassignment\pgfmath@gobbletilpgfmath@%
	\pgfmath@x#1pt\relax\pgfmath@%
	\xdef\pgfmath@resulttempx{\pgf@sys@tonumber{\pgfmath@x}}%
	\afterassignment\pgfmath@gobbletilpgfmath@%
	\pgfmath@x#2pt\relax\pgfmath@%
	\xdef\pgfmath@resulttempx{\pgf@sys@tonumber{\pgfmath@x}}%
	\gdef\pgfmath@returntwo@{%
		\edef\pgfmathresultx{\pgfmath@resulttempx}%
		\edef\pgfmathresulty{\pgfmath@resulttempy}%
	}%
	\aftergroup\pgfmath@returntwo@}

% \pgfmath@smuggleone
%
% Summgle a macro outside a group.
%
\def\pgfmath@smuggleone#1{%
	\xdef\pgfmath@smuggleditem{#1}%
	\gdef\pgfmath@@smuggleone{\edef#1{\pgfmath@smuggleditem}}%
	\aftergroup\pgfmath@@smuggleone}