% 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.
%
\unless\ifcsname tikzextset\endcsname
  \input tikzext-util.tex
\fi

\newcount\tikzext@nodesonpath
\tikzextset{
  pic/.is if=tikz@node@is@pic,
  /tikz-ext/nodes/node on line/.style 2 args={
    /tikz/to path={
      \pgfextra{%
        \edef\tikz@temp{% rescuing nodes and target for edge
          edge[
            line to,%   --
            path only,% no draw, no fill, …
            every edge quotes/.append style={auto=false},% node *on* the line
            nodes={alias=tikzext-node on line}]
          coordinate(tikzext-node on line)% fallback coordinate
          \pgfutil@unexpanded\expandafter{\tikz@tonodes}(\tikztotarget)
        }\expandafter
      }\tikz@temp
      -- (tikzext-node on line#1)
         (tikzext-node on line#2)
      -- (\tikztotarget)
    }
  },
  /tikz-ext/nodes/@node on line/.style args={#1 and #2}{
    /tikz-ext/nodes/node on line={.#1}{.#2}
  },
  node on line/.default=,
  node on line/.code={%
    \pgfutil@ifempty{#1}{%
      \pgfkeys{/tikz-ext/nodes/node on line={}{}}%
    }{%
      \pgfutil@in@{ and }{#1}%
      \ifpgfutil@in@
        \pgfkeys{/tikz-ext/nodes/@node on line={#1}}%
      \else
        \pgfkeys{/tikz-ext/nodes/@node on line={#1 and #1}}%
      \fi
    }%
  },
  nodes on line/.style={
    /tikz/to path={
      \pgfextra{%
        \tikzext@nodesonpath=0
        \edef\tikz@temp{% rescuing nodes and target for edge
          edge[
            line to,%   --
            path only,% no draw, no fill, …
            every edge quotes/.append style={auto=false},% node *on* the line
            nodes={
              /utils/exec=\global\noexpand\advance\tikzext@nodesonpath by 1,
              alias=tikzext-node on line-\noexpand\the\tikzext@nodesonpath
            }]
          \pgfutil@unexpanded\expandafter{\tikz@tonodes}(\tikztotarget)
        }\expandafter
      }\tikz@temp
      \ifnum\tikzext@nodesonpath=0
        -- (\tikztotarget)
      \else
        -- (tikzext-node on line-1)
        \unless\ifnum\tikzext@nodesonpath=1
          foreach[expand list] \tikzext@counter in {2, ..., \the\tikzext@nodesonpath}{
               (tikzext-node on line-\pgfinteval{\tikzext@counter-1})
            -- (tikzext-node on line-\tikzext@counter)
          }
        \fi
        (tikzext-node on line-\the\tikzext@nodesonpath) -- (\tikztotarget)
      \fi
    }
  },
  %%% nodes on curve, needs spath3
  /tikz-ext/spath/split multiple at intersections/.style n args={3}{
    /utils/temp/.style={
      /tikz/spath/split at intersections with={#1}{#2##1}
    },
    /utils/temp/.list={#3}
  },
  nodes on curve/.default=line to,
  nodes on curve'/.default=line to,
  nodes on curve/.style={% normal path
    /tikz-ext/nodes/nodes on curve={#1}{/tikz/spath/append}{}
  },
  nodes on curve'/.style={% for edges and tos
    /tikz-ext/nodes/nodes on curve={#1}{/tikz/spath/use}{%
      \ifx\tikz@to@or@edge@function\tikz@do@to(\tikztotarget)\fi
    }
  },
  % spath/prefix/tikzext-nodes/.style={
  %   set prefix=tikzext-node on curve
  % },
  % spath/suffix/tikzext-nodes/.style={
  %   set suffix=
  % },
  /tikz-ext/nodes/nodes on curve/.code n args={3}{%
    \pgfutil@IfUndefined{tikz@library@spath3@loaded}{%
      \pgfutil@packagerror{tikz-ext}{%
        You need to say \string\usetikzlibrary{spath3} to use nodes on curve.}{}%
    }{%
      \tikzset{to path={%
      % \def\tikz@to@path{% to path = 
        \pgfextra{%
          \tikzext@nodesonpath=0
          \edef\tikz@temp{% rescuing nodes and target for edge
            edge[%
              #1, path only,% path only = no draw, no fill, …
              every edge quotes/.append style={auto=false},% node *on* the line
              nodes={
                /utils/exec=\global\noexpand\advance\tikzext@nodesonpath by 1,
                % spath/set name=tikzext-nodes,
                spath/save global=tikzext-node on curve-\noexpand\the\tikzext@nodesonpath,
              },
              % spath/set name=tikzext-nodes,
              spath/save global=tikzext-node on curve-curve
            ]
            \pgfutil@unexpanded\expandafter{\tikz@tonodes}(\tikztotarget)
          }\expandafter
        }\tikz@temp
        [%
          /tikz-ext/spath/split multiple at intersections/.expanded=%
            {tikzext-node on curve-curve}%
            {tikzext-node on curve-}%
            {1\ifnum\tikzext@nodesonpath>1 ,...,\the\tikzext@nodesonpath\fi},
          spath/remove components/.expanded={tikzext-node on curve-curve}{%
            2%
            \ifnum\tikzext@nodesonpath>1
              ,4,...,\pgfinteval{2*\the\tikzext@nodesonpath}%
            \fi
          },
          #2=tikzext-node on curve-curve%
        ]%
        #3%
      }}%
    }%
  }%
}
% auto extensions
\newif\iftikzext@auto@offset
\newif\iftikzext@auto@precise
\pgfkeysdef{/tikz/auto/}{\let\tikz@do@auto@anchor\tikz@auto@anchor@on}
\tikzset{
  swap/.style={% overwriting
    /tikz/swap/\tikz@auto@anchor@direction
  },
  auto/.is choice, % overwriting
  auto/.default=,
  auto/false/.code=\let\tikz@do@auto@anchor\relax,
  /tikz/ext/nodes/install auto/.style 2 args={
    /tikz/swap/#1/.code=\def\tikz@auto@anchor@direction{#2\iftikzext@auto@precise @precise\fi},
    /tikz/auto/#1/.code=\let\tikz@do@auto@anchor\tikz@auto@anchor@on\def\tikz@auto@anchor@direction{#1\iftikzext@auto@precise @precise\fi}%
  },
  /tikz/ext/nodes/install auto={left}{right},
  /tikz/ext/nodes/install auto={right}{left},
  /tikz/ext/nodes/install auto={ext/left}{ext/light},
  /tikz/ext/nodes/install auto={ext/right}{ext/left},
  /tikz/ext/nodes/install auto={ext/above}{ext/below},
  /tikz/ext/nodes/install auto={ext/below}{ext/above},
  /tikz/ext/nodes/install auto={ext/north}{ext/south},
  /tikz/ext/nodes/install auto={ext/south}{ext/north},
  /tikz/ext/nodes/install auto={ext/west}{ext/east},
  /tikz/ext/nodes/install auto={ext/east}{ext/west},
  sloped/.default=true,
  sloped/.is choice, % overwriting
  sloped/true/.code=\pgfslopedattimetrue\tikzext@auto@precisefalse,
  sloped/false/.code=\pgfslopedattimefalse
}
\tikzextset{
  precise auto angle/.default=true,
  precise auto angle/.is choice,
  precise auto angle/true/.code=\tikzext@auto@precisetrue\pgfslopedattimefalse,
  precise auto angle/false/.code=\tikzext@auto@precisefalse,
  auto offset/.initial=1cm,
  auto with offset/.is if=tikzext@auto@offset,
  % next four keys needs decorations
  nodes/install auto offset for brace decoration/.default=0pt,
  nodes/install auto offset for brace decoration/.style={%
    /pgf/decoration/raise/.append code=\pgfkeyssetvalue{/pgf/decoration/raise}{##1},
    /pgf/decoration/raise={#1},
    /tikz/ext/auto offset for brace decoration/.style={
      /tikz/ext/auto offset=\pgfdecorationsegmentamplitude+(\pgfkeysvalueof{/pgf/decoration/raise})%
    },
    /tikz/ext/every brace node/.style={
      /tikz/auto=% Ugh?
        \ifx\tikz@dec@mirror\relax
          \ifpgf@decorate@inputsegmentobjects@reverse right\else left\fi
        \else
          \ifpgf@decorate@inputsegmentobjects@reverse left\else right\fi
        \fi,
      /tikz/ext/auto with offset=true,
      /tikz/ext/auto offset for brace decoration,
      /tikz/pos=\ifpgf@decorate@inputsegmentobjects@reverse1-\fi(\pgfdecorationsegmentaspect)
    },
    /tikz/ext/nodes/install auto offset for brace decoration/.code=% don't apply twice
  },
}
\def\tikzext@auto@pre{%
  \begingroup
    \let\iftikzext@slopedattime\ifpgfslopedattime
    \let\iftikzext@allowupsidedownattime\ifpgfallowupsidedownattime
    \begingroup
      \pgfresetnontranslationattimefalse
      \pgfslopedattimetrue
      \pgfallowupsidedownattimetrue
      \tikz@timer
      \pgf@x=\pgf@pt@aa pt%
      \pgf@y=\pgf@pt@ab pt%
      \pgfpointnormalised{}%
    \endgroup
    \edef\tikzext@unslopedx{\the\pgf@x}%
    \edef\tikzext@unslopedy{\the\pgf@y}%
    \pgfresetnontranslationattimefalse
    \ifpgfslopedattime
      \pgfslopedattimefalse%
    \else
      \pgfslopedattimetrue%
    \fi
    \pgfallowupsidedownattimetrue%
    \tikz@timer%
    \pgf@x=\pgf@pt@aa pt%
    \pgf@y=\pgf@pt@ab pt%
    \pgfpointnormalised{}%
}
\def\tikzext@auto@post{%
    \edef\tikz@temp{%
      \def\noexpand\tikz@anchor{\tikz@anchor}%
      \def\noexpand\tikzext@autooffset@angle{\tikzext@autooffset@angle}}%
  \expandafter\endgroup\tikz@temp
}
\def\tikzext@auto@offset@A#1{%
  \iftikzext@auto@offset
    \xdef\tikz@marshal{%
      \pgf@process{%
        \noexpand\pgftransformreset
        \noexpand\pgftransformrotate{#1}%
        \noexpand\pgfpointtransformed{%
          \noexpand\pgfpointscale{\noexpand\pgfkeysvalueof{/tikz/ext/auto offset}}
                                 {\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}}%
      \advance\pgf@pt@x\pgf@x
      \advance\pgf@pt@y\pgf@y
    }%
  \fi
}
\def\tikzext@auto@offset@B#1{%
  \iftikzext@auto@offset
    \ifpgfslopedattime
      \pgf@xb=\pgf@x
      \pgf@yb=\pgf@y
      \pgf@process{%
        \pgftransformreset
        \pgftransformrotate{#1}%
        \pgfpointtransformed{%
          \pgf@process{%
            \tikz@timer%
            \pgf@x=\pgf@pt@aa pt
            \pgf@y=\pgf@pt@ab pt
            \pgfpointnormalised{}%
            \pgfpointscale{\pgfkeysvalueof{/tikz/ext/auto offset}}{}}}}%
      \advance\pgf@pt@x\pgf@x
      \advance\pgf@pt@y\pgf@y
      \pgf@x=\pgf@xb
      \pgf@y=\pgf@yb
    \else
      \tikz@marshal
    \fi
  \fi
}
\def\tikzext@auto@offset@B@precise{%
  \iftikzext@auto@offset
    \ifpgfslopedattime
    \else
      \tikz@marshal
    \fi
  \fi
}

% direction: left
\def\tikz@auto@anchor@left{% overwriting
  \tikz@auto@pre
  \tikz@auto@anchor
  \tikzext@auto@offset@A{90}%
  \tikz@auto@post
  \tikzext@auto@offset@B{90}%
}
\def\tikz@auto@anchor@left@precise{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \pgfmathadd@{\pgfmathresult}{-90}%
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{90}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% direction: right
\def\tikz@auto@anchor@right{% overwriting
  \tikz@auto@pre
  \tikz@auto@anchor@prime
  \tikzext@auto@offset@A{-90}%
  \tikz@auto@post
  \tikzext@auto@offset@B{-90}%
}
\def\tikz@auto@anchor@right@precise{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \pgfmathadd@{\pgfmathresult}{90}%
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{-90}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% direction: Left
\pgfutil@namedef{tikz@auto@anchor@ext/left}{%
  \tikzext@auto@pre
  \def\tikzext@autooffset@angle{90}%
  \iftikzext@allowupsidedownattime
    \tikz@auto@anchor
  \else
    \iftikzext@slopedattime
      \ifdim\tikzext@unslopedx<0pt
        \def\tikz@anchor{north}%
        \def\tikzext@autooffset@angle{-90}%
      \else
        \def\tikz@anchor{south}%
      \fi
    \else
      \tikz@auto@anchor
    \fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}
\pgfutil@namedef{tikz@auto@anchor@ext/left@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \pgfmathadd@{\pgfmathresult}{-90}%
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{90}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% direction: Right
\pgfutil@namedef{tikz@auto@anchor@ext/right}{%
  \tikzext@auto@pre
  \def\tikzext@autooffset@angle{-90}%
  \iftikzext@allowupsidedownattime
    \tikz@auto@anchor@prime
  \else
    \iftikzext@slopedattime
      \ifdim\tikzext@unslopedx<0pt
        \def\tikz@anchor{south}%
        \def\tikzext@autooffset@angle{90}%
      \else
        \def\tikz@anchor{north}%
      \fi
    \else
      \tikz@auto@anchor@prime
    \fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}
\pgfutil@namedef{tikz@auto@anchor@ext/right@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \pgfmathadd@{\pgfmathresult}{90}%
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{-90}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% direction: North (always above the line)
\pgfutil@namedef{tikz@auto@anchor@ext/north}{%
  \tikzext@auto@pre
  \def\tikz@anchor{south}%
  \def\tikzext@autooffset@angle{90}%
  \iftikzext@allowupsidedownattime
    \ifdim\tikzext@unslopedx<0pt
      \def\tikzext@autooffset@angle{-90}%
      \def\tikz@anchor{north}%
    \fi
  \else
    \ifdim\pgf@x>0.05pt
      \ifdim\pgf@y>0.05pt
        \def\tikz@anchor{south east}%
      \else\ifdim\pgf@y<-0.05pt
        \def\tikz@anchor{south west}%
      \fi\fi
    \else\ifdim\pgf@x<-0.05pt
      \def\tikzext@autooffset@angle{-90}%
      \ifdim\pgf@y>0.05pt
        \def\tikz@anchor{south west}%
      \else\ifdim\pgf@y<-0.05pt
        \def\tikz@anchor{south east}%
      \fi\fi
    \else
      \ifdim\pgf@y>0pt
        \def\tikz@anchor{east}%
      \else
        \def\tikz@anchor{west}%
      \fi
    \fi\fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}
\pgfutil@namedef{tikz@auto@anchor@ext/north@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \ifdim\pgf@x<0pt
    \pgfmathadd@{\pgfmathresult}{90}%
    \def\tikzext@autooffset@angle{-90}%
  \else
    \pgfmathadd@{\pgfmathresult}{-90}%
    \def\tikzext@autooffset@angle{90}%
  \fi
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% auto: South (always below the line)
\pgfutil@namedef{tikz@auto@anchor@ext/south}{%
  \tikzext@auto@pre
  \def\tikz@anchor{north}%
  \def\tikzext@autooffset@angle{-90}%
  \iftikzext@allowupsidedownattime
    \ifdim\tikzext@unslopedx<0pt
      \def\tikzext@autooffset@angle{90}%
      \def\tikz@anchor{south}%
    \fi
  \else
    \ifdim\pgf@x>0.05pt
      \ifdim\pgf@y>0.05pt
        \def\tikz@anchor{north west}%
      \else\ifdim\pgf@y<-0.05pt
        \def\tikz@anchor{north east}%
      \fi\fi
    \else\ifdim\pgf@x<-0.05pt
      \def\tikzext@autooffset@angle{90}%
      \ifdim\pgf@y>0.05pt
        \def\tikz@anchor{north east}%
      \else\ifdim\pgf@y<-0.05pt
        \def\tikz@anchor{north west}%
      \fi\fi
    \else
      \ifdim\pgf@y>0pt
        \def\tikz@anchor{west}%
      \else
        \def\tikz@anchor{east}%
      \fi
    \fi\fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}
\pgfutil@namedef{tikz@auto@anchor@ext/south@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \ifdim\pgf@x<0pt
    \pgfmathadd@{\pgfmathresult}{-90}%
    \def\tikzext@autooffset@angle{90}%
  \else
    \pgfmathadd@{\pgfmathresult}{90}%
    \def\tikzext@autooffset@angle{-90}%
  \fi
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% auto: West (always to the left of the line)
\pgfutil@namedef{tikz@auto@anchor@ext/west}{%
  \tikzext@auto@pre
  \def\tikz@anchor{east}%
  \def\tikzext@autooffset@angle{90}%
  \iftikzext@slopedattime
    \def\tikz@anchor{south}%
    \iftikzext@allowupsidedownattime
      \ifdim\tikzext@unslopedy<-0.05pt
        \def\tikz@anchor{north}%
        \def\tikzext@autooffset@angle{-90}%
      \fi
    \else
      \ifdim\tikzext@unslopedy>0.05pt
        \ifdim\tikzext@unslopedx<-0.05pt
          \def\tikz@anchor{north}%
          \def\tikzext@autooffset@angle{-90}%
        \fi
      \else
        \ifdim\tikzext@unslopedy<-0.05pt
          \ifdim\tikzext@unslopedx<-0.05pt
          \else
            \def\tikz@anchor{north}%
            \def\tikzext@autooffset@angle{-90}%
          \fi
        \else
          \ifdim\tikzext@unslopedx>0pt
          \else
            \def\tikz@anchor{north}%
            \def\tikzext@autooffset@angle{-90}%
          \fi
        \fi
      \fi
    \fi
  \else
    \ifdim\pgf@y>0.05pt
      \ifdim\pgf@x>0.05pt
        \def\tikz@anchor{south east}%
      \else\ifdim\pgf@x<-0.05pt
        \def\tikz@anchor{north east}%
      \fi\fi
    \else
      \ifdim\pgf@y<-0.05pt
        \def\tikzext@autooffset@angle{-90}%
        \ifdim\pgf@x>0.05pt
          \def\tikz@anchor{north east}%
        \else\ifdim\pgf@x<-0.05pt
          \def\tikz@anchor{south east}%
        \fi\fi
      \else
        \ifdim\pgf@x>0pt
          \def\tikz@anchor{south}%
        \else
          \def\tikz@anchor{north}%
        \fi
      \fi
    \fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}

\pgfutil@namedef{tikz@auto@anchor@ext/west@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \ifdim\pgf@y<0pt
    \pgfmathadd@{\pgfmathresult}{90}%
    \def\tikzext@autooffset@angle{-90}%
  \else
    \pgfmathadd@{\pgfmathresult}{-90}%
    \def\tikzext@autooffset@angle{90}%
  \fi
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% auto: East (always to the right of the line)
\pgfutil@namedef{tikz@auto@anchor@ext/east}{%
  \tikzext@auto@pre
  \def\tikz@anchor{west}%
  \def\tikzext@autooffset@angle{-90}%
  \iftikzext@slopedattime
    \def\tikz@anchor{north}%
    \iftikzext@allowupsidedownattime
      \ifdim\tikzext@unslopedy<-0.05pt
        \def\tikz@anchor{south}%
        \def\tikzext@autooffset@angle{90}%
      \fi
    \else
      \ifdim\tikzext@unslopedy>0.05pt
        \ifdim\tikzext@unslopedx<-0.05pt
          \def\tikz@anchor{south}%
          \def\tikzext@autooffset@angle{90}%
        \fi
      \else
        \ifdim\tikzext@unslopedy<-0.05pt
          \ifdim\tikzext@unslopedx<-0.05pt
          \else
            \def\tikz@anchor{south}%
            \def\tikzext@autooffset@angle{90}%
          \fi
        \else
          \ifdim\tikzext@unslopedx>0pt
          \else
            \def\tikz@anchor{south}%
            \def\tikzext@autooffset@angle{90}%
          \fi
        \fi
      \fi
    \fi
  \else
    \ifdim\pgf@y>0.05pt
      \ifdim\pgf@x>0.05pt
        \def\tikz@anchor{north west}%
      \else\ifdim\pgf@x<-0.05pt
        \def\tikz@anchor{south west}%
      \fi\fi
    \else
      \ifdim\pgf@y<-0.05pt
        \ifdim\pgf@x>0.05pt
          \def\tikz@anchor{south west}%
        \else\ifdim\pgf@x<-0.05pt
          \def\tikz@anchor{north west}%
        \fi\fi
      \else
        \ifdim\pgf@x>0pt
          \def\tikz@anchor{north}%
        \else
          \def\tikz@anchor{south}%
        \fi
      \fi
    \fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}
\pgfutil@namedef{tikz@auto@anchor@ext/east@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \ifdim\pgf@y<0pt
    \pgfmathadd@{\pgfmathresult}{-90}%
    \def\tikzext@autooffset@angle{90}%
  \else
    \pgfmathadd@{\pgfmathresult}{90}%
    \def\tikzext@autooffset@angle{-90}%
  \fi
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% auto: above (always towards end of line)
\pgfutil@namedef{tikz@auto@anchor@ext/above}{%
  \tikzext@auto@pre
  \def\tikzext@autooffset@angle{0}%
  \ifdim\pgf@y>0.05pt
    \def\tikz@anchor{south}%
    \ifdim\pgf@x>0.05pt
      \def\tikz@anchor{south west}%
    \else\ifdim\pgf@x<-0.05pt
      \def\tikz@anchor{south east}%
    \fi\fi
  \else
    \ifdim\pgf@y<-0.05pt
      \def\tikz@anchor{north}%
      \ifdim\pgf@x>0.05pt
        \def\tikz@anchor{north west}%
      \else\ifdim\pgf@x<-0.05pt
        \def\tikz@anchor{north east}%
      \fi\fi
    \else
      \ifdim\pgf@x>0pt
        \def\tikz@anchor{west}%
      \else
        \def\tikz@anchor{east}%
      \fi
    \fi
  \fi
  \iftikzext@allowupsidedownattime
  \else
    \iftikzext@slopedattime
      \ifdim\tikzext@unslopedx<-0.05pt
        \def\tikz@anchor{east}%
        \def\tikzext@autooffset@angle{180}%
      \fi
    \fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}
\pgfutil@namedef{tikz@auto@anchor@ext/above@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \pgfmathadd@{\pgfmathresult}{180}%
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{0}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

% auto: below (always towards the start of the line)
\pgfutil@namedef{tikz@auto@anchor@ext/below}{%
  \tikzext@auto@pre
  \def\tikzext@autooffset@angle{180}%
  \ifdim\pgf@y>0.05pt
    \def\tikz@anchor{north}%
    \ifdim\pgf@x>0.05pt
      \def\tikz@anchor{north east}%
    \else\ifdim\pgf@x<-0.05pt
      \def\tikz@anchor{north west}%
    \fi\fi
  \else
    \ifdim\pgf@y<-0.05pt
      \def\tikz@anchor{south}%
      \ifdim\pgf@x>0.05pt
        \def\tikz@anchor{south east}%
      \else\ifdim\pgf@x<-0.05pt
        \def\tikz@anchor{south west}%
      \fi\fi
    \else
      \ifdim\pgf@x>0pt
        \def\tikz@anchor{east}%
      \else
        \def\tikz@anchor{west}%
      \fi
    \fi
  \fi
  \iftikzext@allowupsidedownattime
  \else
    \iftikzext@slopedattime
      \ifdim\tikzext@unslopedx<-0.05pt
        \def\tikz@anchor{west}%
        \def\tikzext@autooffset@angle{0}%
      \fi
    \fi
  \fi
  \tikzext@auto@offset@A{\tikzext@autooffset@angle}%
  \tikzext@auto@post
  \tikzext@auto@offset@B{\tikzext@autooffset@angle}%
}
\pgfutil@namedef{tikz@auto@anchor@ext/below@precise}{%
  \tikz@auto@pre
  \edef\tikz@temp{%
    \noexpand\pgfmathatantwo@{\pgf@sys@tonumber\pgf@y}{\pgf@sys@tonumber\pgf@x}}%
  \tikz@temp
  \let\tikz@anchor\pgfmathresult
  \tikzext@auto@offset@A{180}%
  \tikz@auto@post
  \tikzext@auto@offset@B@precise
}

%%% Compatibility
\tikzextset{
  /tikz-ext/compat/add library={nodes}{pre 0.6},
  compat/nodes/warn/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/nodes@}{3}%
    \tikzextutil@compatwarning@key{nodes}{pre 0.6}{/tikz/pic}            [true]{/tikz/ext/pic}%
    \tikzextutil@compatwarning@key{nodes}{pre 0.6}{/tikz/node on line}   []    {/tikz/ext/node on line}%
    \tikzextutil@compatwarning@key{nodes}{pre 0.6}{/tikz/nodes on line}        {/tikz/ext/nodes on line}%
    \tikzextutil@compatwarning@key{nodes}{pre 0.6}{/tikz/nodes on curve} []    {/tikz/ext/nodes on curve}%
    \tikzextutil@compatwarning@key{nodes}{pre 0.6}{/tikz/nodes on curve'}[]    {/tikz/ext/nodes on curve'},
  compat/nodes/pre 0.6/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/nodes@}{2}%
    \pgfkeysdef{/tikz/pic}            {\pgfkeysvalueof{/tikz/ext/pic/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/node on line}   {\pgfkeysvalueof{/tikz/ext/node on line/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/nodes on line}  {\pgfkeysvalueof{/tikz/ext/nodes on line/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/nodes on curve} {\pgfkeysvalueof{/tikz/ext/nodes on curve/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/nodes on curve'}{\pgfkeysvalueof{/tikz/ext/nodes on curve'/.@cmd}##1\pgfeov}%
    \pgfkeyssetvalue{/tikz/pic/.@def}{true}%
    \pgfkeyssetvalue{/tikz/node on line/.@def}{}%
    \pgfkeyssetvalue{/tikz/nodes on curve/.@def}{}%
    \pgfkeyssetvalue{/tikz/nodes on curve'/.@def}{},
  % compat/nodes/0.6/.append code=\pgfkeyssetvalue{/tikz/ext/compat/nodes@}{1},
  % compat/nodes/newest/.append code=\pgfkeyssetvalue{/tikz/ext/compat/nodes@}{0},
  compat/nodes/\pgfkeysvalueof{/tikz/ext/compat/nodes}%
}
\endinput