% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
\usepgfkeyslibrary{ext.pgfkeys-plus}%

%% full arc
%% https://tex.stackexchange.com/a/144297/16595
%% The postfix operator R is added to PGFmath,
%% it allows the use of angular segments.
%% full arc = 10 makes it so that
%%   1R =  36°
%%   2R =  72°
%%   …
%%  10R = 360°
%% Calling the full arc key with an empty value 
%% makes 1R = 1°
\pgfkeyslet{/pgf/ext/full arc}\pgfutil@empty
\pgfextset{
  full arc/.code=%
    \edef\pgf@temp{#1}%
    \ifx\pgfutil@empty\pgf@temp
      \pgfkeyslet{/pgf/ext/full arc}\pgfutil@empty
    \else
      \pgfmathdivide{360}{#1}%
      \pgfkeyslet{/pgf/ext/full arc}\pgfmathresult
    \fi
}
\pgfmathdeclareoperator{R}{extfullarc}{1}{postfix}{950}
\pgfmathdeclarefunction{extfullarc}{1}{%
  \begingroup
    \pgfmath@x=#1pt\relax
    \expandafter\ifx\csname pgfk@/pgf/ext/full arc\endcsname\pgfutil@empty
    \else
      \pgfmath@x\csname pgfk@/pgf/ext/full arc\endcsname\pgfmath@x
    \fi
    \pgfmath@returnone\pgfmath@x
  \endgroup
}

\unless\ifcsname iftikzext@pgffor@nolist\endcsname
  \input pgffor-ext.code.tex
\fi

%% PGFmath
%% strrepeat("x", 5) = "xxxxx"
\pgfmathdeclarefunction{extstrrepeat}{2}{%
  \begingroup\pgfmathint{#2}\pgfmath@count\pgfmathresult
    \let\pgfmathresult\pgfutil@empty
    \pgfutil@loop\ifnum\pgfmath@count>0\relax
      \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult#1}%
      \advance\pgfmath@count-1\relax
    \pgfutil@repeat\pgfmath@smuggleone\pgfmathresult\endgroup}
%% isInString("foo", "foobar") = true (= 1)
%% isInString("boo", "foobar") = false (= 0)
\pgfmathdeclarefunction{extisInString}{2}{%
  \pgfutil@in@{#1}{#2}%
  \ifpgfutil@in@\def\pgfmathresult{1}\else\def\pgfmathresult{0}\fi}

%% strcat("foo", "bar") = "foobar" (can have more than two parameters)
\pgfmathdeclarefunction{extstrcat}{...}{%
  \begingroup
    \let\pgfmathresult\pgfutil@empty
    \pgfextmathstrcat@@#1\pgfmath@stop
}
\def\pgfextmathstrcat@@#1{%
  \ifx\pgfmath@stop#1%
    \def\pgfmath@next{\pgfmath@smuggleone\pgfmathresult\endgroup}%
  \else
    \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult#1}%
    \let\pgfmath@next\pgfextmathstrcat@@
  \fi
  \pgfmath@next
}

% http://tex.stackexchange.com/questions/244569/bounding-lines-around-tax-nodes/244619#244619
\pgfmathdeclarefunction{extatanXY}{2}{\pgfmathatantwo@{#2}{#1}}
\pgfmathdeclarefunction{extatanYX}{2}{\pgfmathatantwo@{#1}{#2}}

\pgfmathdeclarefunction{extdistancebetween}{2}{% only coordinates/nodes
  \begingroup
    \nullfont
    \pgfpointdiff{\pgfpointanchor{#1}{center}}{\pgfpointanchor{#2}{center}}%
    \edef\pgfmath@temp{{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}}%
    \expandafter\pgfmathveclen@\pgfmath@temp
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}
\pgfmathdeclarefunction{extqdistancebetween}{1}{% only coordinates/nodes
  \begingroup
    \nullfont
    \pgfpointdiff{\pgfpointorigin}{\pgfpointanchor{#1}{center}}%
    \edef\pgfmath@temp{{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}}%
    \expandafter\pgfmathveclen@\pgfmath@temp
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}

\pgfmathdeclarefunction{extqanglebetween}{1}{%
  \begingroup
    \nullfont
    \pgfmathanglebetweenpoints{\pgfpointorigin}{\pgfpointanchor{#1}{center}}%
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}

\pgfmathdeclarefunction{extanglebetween}{2}{%
  \begingroup
    \nullfont  
    \pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}{\pgfpointanchor{#2}{center}}%
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}

\pgfmathdeclarefunction{extisEmpty}{1}{%
  \begingroup
    \def\pgfmath@temp{#1}%
    \pgfutil@ifxempty\pgfmath@temp{\def\pgfmathresult{1}}{\def\pgfmathresult{0}}%                           
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}


\pgfextset{
  declare constant/.code={%
    \let\pgfmathdeclareconstant@@@\pgfutil@empty
    \pgfkeysvalueof{/pgf/declare function/execute at begin function}%
    \pgfmathdeclareconstant@#1@=@;\pgf@stop
    \pgfkeysvalueof{/pgf/declare function/execute at end function}%
    \pgfmathdeclareconstant@@@
  }}
\def\pgfmathdeclareconstant@{%
  \pgfutil@ifnextchar x\pgfmathdeclareconstant@@\pgfmathdeclareconstant@@
}
\def\pgfmathdeclareconstant@@#1=#2;#3\pgf@stop{%
  \edef\pgfmath@local@temp{#1}%
  \pgfutil@ifx\pgfmath@local@temp\pgfmath@local@at{}{%
    \pgfutil@g@addto@macro\pgfmathdeclareconstant@@@{\pgfmathdeclarepseudoconstant{#1}{\def\pgfmathresult{#2}}}%
    \pgfmathdeclareconstant@#3\pgf@stop
  }%
}

%%% Some shape

\tikzextset{% quick versions of reverse clips
  reverse clip/.is choice,
  reverse clip/clockwise/.code={%
    \begingroup
      \pgfsetcornersarced{\pgfpointorigin}%
      \pgfpathqmoveto{16000pt}{16000pt}%
      \pgfpathqlineto{16000pt}{-16000pt}%
      \pgfpathqlineto{-16000pt}{-16000pt}%
      \pgfpathqlineto{-16000pt}{16000pt}%
      \pgfpathclose
    \endgroup
  },
  reverse clip/counter clockwise/.code={%
    \begingroup
      \pgfsetcornersarced{\pgfpointorigin}%
      \pgfpathqmoveto{16000pt}{16000pt}%
      \pgfpathqlineto{-16000pt}{16000pt}%
      \pgfpathqlineto{-16000pt}{-16000pt}%
      \pgfpathqlineto{16000pt}{-16000pt}%
      \pgfpathclose
    \endgroup
  },
  clip rule/.is choice,
  clip rule/even odd/.code=\pgfseteorule,
  clip rule/nonzero/.code=\pgfsetnonzerorule,
  reverse clip/.default=counter clockwise,
}
%%% Compatibility
\tikzextset{
  /tikz-ext/compat/add library={misc}{pre 0.6},
  compat/misc/warn/.append code={%
    \tikzextutil@compatwarning@key{misc}{pre 0.6}{/pgf/full arc}{/pgf/ext/full arc}%
    \tikzextutil@compatwarning@mathcmdII{misc}{pre 0.6}{strrepeat}{extstrrepeat}%
    \tikzextutil@compatwarning@mathcmdII{misc}{pre 0.6}{isInString}{extisInString}%
    \ifcsname pgfmathstrcat@\endcsname\else
      \let\pgfmathextstrcat@noforest\relax
      \tikzextutil@compatwarning@mathcmdX{misc}{pre 0.6}{strcat}{extstrcat}%
    \fi
    \tikzextutil@compatwarning@mathcmdI{misc}{pre 0.6}{isEmpty}{extisEmpty}%
    \tikzextutil@compatwarning@mathcmdII{misc}{pre 0.6}{atanXY}{extatanXY}%
    \tikzextutil@compatwarning@mathcmdII{misc}{pre 0.6}{atanYX}{extatanYX}%
    \tikzextutil@compatwarning@mathcmdII{misc}{pre 0.6}{anglebetween}{extanglebetween}%
    \tikzextutil@compatwarning@mathcmdII{misc}{pre 0.6}{distancebetween}{extdistancebetween}%
    \tikzextutil@compatwarning@mathcmdI{misc}{pre 0.6}{qanglebetween}{extqanglebetween}%
    \tikzextutil@compatwarning@mathcmdI{misc}{pre 0.6}{qdistancebetween}{extqdistancebetween}%
  },
  compat/misc/pre 0.6/.append code={%
    \pgfkeysdef{/pgf/full arc}{\pgfkeysvalueof{/pgf/ext/full arc/.@cmd}##1\pgfeov}%
    \pgfmathdeclarefunction*{strrepeat}{2}{\pgfmathextstrrepeat@{##1}{##2}}%
    \pgfmathdeclarefunction*{isInString}{2}{\pgfmathextisInString@{##1}{##2}}%
    \pgfmathdeclarefunction*{isEmpty}{1}{\pgfmathextisEmpty@{##1}}%
    \pgfmathdeclarefunction*{anglebetween}{2}{\pgfmathextanglebetween@{##1}{##2}}%
    \pgfmathdeclarefunction*{qanglebetween}{1}{\pgfmathextqanglebetween@{##1}}%
    \pgfmathdeclarefunction*{distancebetween}{2}{\pgfmathextdistancebetween@{##1}{##2}}%
    \pgfmathdeclarefunction*{qdistancebetween}{1}{\pgfmathextqdistancebetween@{##1}}%
    \pgfmathdeclarefunction*{atanXY}{2}{\pgfmathextatantwo@{##2}{##1}}%
    \pgfmathdeclarefunction*{atanYX}{2}{\pgfmathextatantwo@{##2}{##1}}%
    \ifcsname pgfmathstrcat@\endcsname
      \ifcsname pgfmathextstrcat@noforest\endcsname
        \pgfmathdeclarefunction*{strcat}{...}{\pgfmathextstrcat##1}%
      \fi
    \else
      \let\pgfmathextstrcat@noforest\relax
      \pgfmathdeclarefunction{strcat}{...}{\pgfmathextstrcat##1}%
    \fi
  },
  compat/misc/\pgfkeysvalueof{/tikz/ext/compat/misc}%
}