% Copyright 2007 by Mark Wibrow
%
% 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.

% This file loads all the parsing, functions and operator stuff
%
% Version 1.414213 29/9/2007

\input pgfmathutil.code.tex
\input pgfmathparser.code.tex
\input pgfmathoperations.code.tex
\input pgfmathbase.code.tex

% \pgfmathsetlength, \pgfmathaddtolength
%
% #1 = dimension register
% #2 = expression
%
% Description:
%
% These functions work similar to \setlength and \addtolength. Only,
% they allow #2 to contain an expression, which is evaluated before
% assignment. Furthermore, the font is setup before the assignment is
% done, so that dimensions like 1em are evaluated correctly.
%
% If #2 starts with "+", then a simple assignment is done (but the
% font is still setup). This is orders of magnitude faster than a
% parsed assignment.

\def\pgfmathsetlength#1#2{%
	\expandafter\pgfmath@onquick#2\pgfmath@%
	{%
		% Ok, quick version:
		\begingroup%
			\pgfmath@selectfont%
			\pgfmath@x#2\unskip%
			\pgfmath@returnone\pgfmath@x%
		\endgroup%
		#1\pgfmathresult pt\relax%
	}%
	{%
		\pgfmathparse{#2}%
		#1\pgfmathresult pt\relax%
	}%
	\ignorespaces%
}

% \pgfmathaddtolength
%
% Add to #1 the result of evaluating #2.
%
% #1 - a dimension register
% #2 - an expression
%
\def\pgfmathaddtolength#1#2{%
	\expandafter\pgfmath@onquick#2\pgfmath@%
  {%
  	\begingroup%
  		\pgfmath@selectfont%
  		\pgfmath@x#1\relax%
  		\advance\pgfmath@x#2\unskip%
  		\pgfmath@returnone\pgfmath@x%
    \endgroup%
    #1\pgfmathresult pt\relax%
  }%
  {%
  	\pgfmathparse{#2}%
  	\advance#1\pgfmathresult pt\relax%
  }%
  \ignorespaces%
}

% \pgfmathsetcount
%
% Assign #1 the truncated evaluation of #2.
%
% #1 - a count register
% #2 - an expression
%
\def\pgfmathsetcount#1#2{%
  \expandafter\pgfmath@onquick#2\pgfmath@%
  {%
    #1#2\relax%
  }%
  {%
    \pgfmathparse{#2}%
    \afterassignment\pgfmath@gobbletilpgfmath@%
    #1\pgfmathresult\relax\pgfmath@%
  }%
  \ignorespaces%
}

% \pgfmathaddtocount
%
% Add to #1 the truncated evaluation of #2.
%
% #1 - a count register
% #2 - an expression
%
\def\pgfmathaddtocount#1#2{%
  \expandafter\pgfmath@onquick#2\pgfmath@%
  {%
    \advance#1#2\relax%
  }%
  {%
    \edef\pgfmath@addtocounttemp{\the#1}%
    \pgfmathparse{#2}%
    \afterassignment\pgfmath@gobbletilpgfmath@%
    #1\pgfmathresult\relax\pgfmath@%
    \advance#1\pgfmath@addtocounttemp\relax%
  }%
  \ignorespaces%
}

% \pgfmathnewcounter
%
% LaTeX style counter which also works in plain TeX. Defines
% \c@<name> as a count register and also defines \the<name>.
%
% #1 the name of the counter.
%
% Example:
%
% \pgfmathnewcounter{counter}
% \pgfmathsetcounter{counter}{12}
% \thecounter  (same as \the\c@counter)
%
\def\pgfmathnewcounter#1{%
	\expandafter\ifx\csname c@#1\endcsname\relax%
		\def\pgfmath@marshal{\csname newcount\endcsname}% Ha! Who cares about \outer?
		\expandafter\pgfmath@marshal\csname c@#1\endcsname%
		\expandafter\def\csname the#1\endcsname{\expandafter\the\csname c@#1\endcsname}%
	\fi%
}

% \pgfmathsetcounter
%
% Set the counter #1 to the evaluation of #2.
%
% #1 - a counter name
% #2 - an expression
%
\def\pgfmathsetcounter#1#2{%
 	\expandafter\pgfmathsetcount\csname c@#1\endcsname{#2}%
}

% \pgfmathaddtocounter
%
% Add the counter #1 to the evaluation of #2.
%
% #1 - a counter name
% #2 - an expression
%
\def\pgfmathaddtocounter#1#2{%
  \expandafter\pgfmathaddtocount\csname c@#1\endcsname{#2}%
}

% \pgfmathmakecounterglobal
%
% Make the current value of a counter globally defined.
%
% #1 - a (valid) counter name.
%
\def\pgfmath@pgftext{pgf}
\def\pgfmath@tikztext{tikz}
\def\pgfmathmakecounterglobal#1{%
	\pgfmath@ifundefined{c@#1}{}{%
		\expandafter\pgfmath@in@\expandafter{\pgfmath@pgftext}{#1}%
		\ifpgfmath@in@%
		\else%
			\expandafter\pgfmath@in@\expandafter{\pgfmath@tikztext}{#1}%
			\ifpgfmath@in@%
			\else%
				\expandafter\global\csname c@#1\endcsname\csname c@#1\endcsname\relax%
			\fi%
		\fi%
	}%
}

% \pgfmathsetmacro
%
% \edef#1 as the result of evaluating #2.
%
% #1 - a macro
% #2 - an expression
%
\def\pgfmathsetmacro#1#2{%
	\begingroup%
		\pgfmathsetlength\pgfmath@x{#2}%
		\edef#1{\pgfmath@tonumber{\pgfmath@x}}%
		\pgfmath@smuggleone{#1}%
	\endgroup%
}

% \pgfmathsetlengthmacro
%
% \edef#1 as the result of evaluating #2 with pt.
%
% #1 - a macro
% #2 - an expression
%
\def\pgfmathsetlengthmacro#1#2{%
	\begingroup%
		\pgfmathsetlength\pgfmath@x{#2}%
		\edef#1{\the\pgfmath@x}%
		\pgfmath@smuggleone{#1}%
	\endgroup%
}

% \pgfmathtruncatemacro
%
% \edef#1 as the truncated result of evaluating #2.
%
% #1 - a macro
% #2 - an expression
%
\def\pgfmathtruncatemacro#1#2{%
	\begingroup%
		\pgfmathsetcount\c@pgfmath@counta{#2}%
		\edef#1{\the\c@pgfmath@counta}%
		\pgfmath@smuggleone{#1}%
	\endgroup%
}

% Check whether a given parameter starts with quick.
%
% The command should be followed by nonempty text, ending with
% \pgfmath@ as a stop-token. Then should follow
%
% #1 - code to execute if text starts with +
% #2 - code to execute if text does not
%
% Example:
%
% \pgfmath@onquick+0pt\pgfmath@{is quick}{is slow}

\def\pgfmath@onquick{%
  \afterassignment\pgfmath@afterquick%
  \let\pgfmath@next=%
}

\def\pgfmath@afterquick#1\pgfmath@{%
  \ifx\pgfmath@next+%
    \expandafter\pgfmath@firstoftwo%
  \else%
    \expandafter\pgfmath@secondoftwo%
  \fi%
}