% Copyright 2026 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/swap/#1@precise/.code=\def\tikz@auto@anchor@direction{#2\iftikzext@auto@precise @precise\fi},
    /tikz/auto/#1@precise/.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/right},
  /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
}

%%% node pic
% named nodes need correction

\newcount\tikzext@nodepicture
\let\tikzext@nodepicture@list\pgfutil@gobble
\newif\iftikzext@nodepicture@ilw
\newif\iftikzext@nodepicture@prefix
\tikzext@nodepicture@prefixtrue

\pgfkeysdef{/tikz/ext/node picture}{\pgfqkeys{/tikz/ext/node picture}{#1}}
\pgfkeyssetvalue{/tikz/ext/node picture/.@def}{true}
\pgfkeysdef{/tikz/ext/node picture/true}{\tikz@node@is@picfalse\tikz@is@matrixfalse\let\tikz@do@fig\tikzext@nodepicture@do}
\pgfkeysdef{/tikz/ext/node picture/false}{\let\tikz@do@fig\tikzextorig@tikz@do@fig}
\pgfkeysdef{node picture/enable}{\pgfkeysvalueof{/tikz/ext/node picture/true/.@cmd}\pgfeov}
\pgfkeysdef{node picture/disable}{\pgfkeysvalueof{//tikz/ext/node picture/false/.@cmd}\pgfeov}
\tikzextset{
  node picture/ignore line width/.is if=tikzext@nodepicture@ilw,
  node picture/prefix name/.is if=tikzext@nodepicture@prefix
}
\pgfkeysdef{/tikz/ext/every node picture}{}
\pgfkeysdef{/tikz/ext/every outer node picture}{}
\pgfkeysdef{/tikz/ext/every node picture scope}{}
\pgfkeyssetvalue{/tikz/ext/node picture/at start}{}
\pgfkeyssetvalue{/tikz/ext/node picture/at end}{}
\pgfkeyssetvalue{/tikz/ext/node picture/anchor}{}
\pgfkeyssetvalue{/tikz/ext/node picture/baseline}{}
\let\tikzext@nodepicture@anchors@list\pgfutil@gobble
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor}{2}{\edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{0}{0}}{{0}{0}}}}
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor/rectangle west}{2}{\edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{-1}{0}}{{-1}{0}}}}
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor/rectangle east}{2}{\edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{1}{0}}{{1}{0}}}}
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor/rectangle north}{2}{\edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{0}{1}}{{0}{1}}}}
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor/rectangle south}{2}{\edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{0}{-1}}{{0}{-1}}}}
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor osep}{4}{%
  \edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{0}{0}}{{#3}{#4}}}}
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor isep}{4}{%
  \edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{#3}{#4}}{{0}{0}}}}
\pgfkeysdefnargs{/tikz/ext/node picture/add anchor sep}{6}{%
  \edef\tikzext@nodepicture@anchors@list{\tikzext@nodepicture@anchors@list,{#1}{#2}{{#3}{#4}}{{#5}{#6}}}}
\pgfkeysdef{/tikz/ext/node picture/absolute}{\pgfkeysalso{/tikz/ext/node picture/anchor={#1},/tikz/at={(#1)}}}
\pgfkeyssetvalue{/tikz/ext/node picture/absolute/.@def}{0,0}
\def\tikzextnodepicture{\the\tikzext@nodepicture}%


\pgfkeysdef{/tikz/ext/node picture/reset graphic state}{%
  \tikzextset{
    every node picture scope/.prefix code={%
      \tikzset{
        /tikz/color=black,
        /tikz/arrows=-,
        /tikz/solid,
        /tikz/thin,
        /tikz/opaque,
        /tikz/line cap=butt,
        /tikz/line join=miter,
        /tikz/anchor=center,
        /tikz/midway,
        /tikz/shape=rectangle,
        /tikz/text width=,
        /tikz/text depth=,
        /tikz/text height=,
        /tikz/sloped=false,
        /tikz/allow upside down=false,
        /pgf/inner sep=+.3333em,
        /pgf/outer sep=+.5\pgflinewidth,
        /pgf/minimum size=+1pt
      }%
    }%
  }%
}
% If nodes gets defined inside a node picture,
% they need to be transformed to the place
% where they actually lie after the node picture is placed.
% This is done by a special callback
% which adds the level and the name to a list
\def\tikzext@nodepicture@callback#1{%
  \def\tikzext@temp{#1}%
  \unless\ifx\pgfutil@empty\tikzext@temp
    \xdef\tikzext@nodepicture@list{\tikzext@nodepicture@list,{\the\tikzext@nodepicture}{#1}}%
  \fi
}
% A PGF matrix uses this callback for each own purpose,
% so if a matrix inside a node picture is used,
% both the matrix callback and the node picture callback needs to be applied.
\let\tikzextorig@pgf@matrix@nodecallback\pgf@matrix@nodecallback
\def\tikzext@nodepicture@matrix@callback#1{%
  \tikzext@nodepicture@callback{#1}%
  \tikzextorig@pgf@matrix@nodecallback{#1}%
}
% This is the macro that actually goes through the list
% and applies the transformation.
% This macro is called with \tikzlastnode
% after the node picture node is placed.
\def\tikzext@nodepicture@callback@#1{%
  \unless\ifx\tikzext@nodepicture@list\pgfutil@gobble
  \begingroup
    % First install the transformation matrix of the node picture's node.
    \pgfsettransform{\csname pgf@sh@nt@#1\endcsname}%
    \csname pgf@sh@np@#1\endcsname
    \csname pgf@sh@ma@#1\endcsname
    % But the origin of the node is not at node picture's origin.
    % Usually that's at the text anchor but just in case:
    \pgftransformshift{\pgf@sh@reanchor{\csname pgf@sh@ns@#1\endcsname}{text}}%
    % Correction: Baseline and left side of node picture.
    \pgftransformshift{\pgfqpoint{-\tikzext@nodepicture@minx@g}{-\tikzext@nodepicture@baseline@}}%
    \let\tikzext@temp\tikzext@nodepicture@list
    % Empty list and loop over entries:
    \global\let\tikzext@nodepicture@list\pgfutil@gobble
    \pgfutil@for\tikzext@temp:=\tikzext@temp\do{%
      \expandafter\tikzext@nodepicture@callback@special\tikzext@temp
    }%
  \endgroup
  \fi
}
\def\tikzext@nodepicture@callback@special#1#2{%
  \ifnum#1>\tikzext@nodepicture\relax
    \unless\ifcsname tikzext@nodepicture@#2-#1@visited\endcsname
      \expandafter\let\csname tikzext@nodepicture@#2-#1@visited\endcsname\pgfutil@empty
      \begingroup
        \pgfkeys@expanded{\noexpand\tikzext@transformcm\csname pgf@sh@nt@#2\endcsname}%
        \pgfgettransform\tikzext@cm
        \expandafter\xdef\csname pgf@sh@nt@#2\endcsname{\tikzext@cm}%
      \endgroup
      \ifnum\tikzext@nodepicture>0 % one more to go
        \xdef\tikzext@nodepicture@list{\tikzext@nodepicture@list,{#1}{#2}}%
      \fi
    \fi
  \else % wrong level, readd
    \xdef\tikzext@nodepicture@list{\tikzext@nodepicture@list,{#1}{#2}}%
  \fi
}
% transformation helper
\def\tikzext@transformcm#1#2#3#4#5#6{%
  \pgftransformcm{#1}{#2}{#3}{#4}{\pgfqpoint{#5}{#6}}%
}

% node picture: baseline
\def\tikzext@nodepicture@baseline@coord#1{%
  \pgf@process{\pgfpointtransformed{#1}}%
  \xdef\tikzext@nodepicture@baseline@{\the\pgf@y}%
}
\def\tikzext@nodepicture@baseline@value#1{%
  \pgfmathsetlength\pgf@ya{#1}%
  \xdef\tikzext@nodepicture@baseline@{\the\pgf@ya}%
}
\def\tikzext@nodepicture@shift#1{%
  \pgf@process{\pgfpointtransformed{#1}}%
  \xdef\tikzext@nodepicture@shift@{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
}

% node picture: anchors
\def\tikzext@nodepicture@callback@anchors#1{%
  \unless\ifx\tikzext@nodepicture@anchorslist\pgfutil@empty
    \pgfutil@for\tikzext@temp:=\tikzext@nodepicture@anchorslist\do{%
      \edef\tikzext@marshal##1##2{%
        \noexpand\expandafter\noexpand\pgfutil@g@addto@macro\noexpand\expandafter\expandafter\noexpand\csname pgf@sh@ma@#1\endcsname
          \noexpand\expandafter{%
          \noexpand\expandafter\def\noexpand\csname pgf@anchor@\csname pgf@sh@ns@#1\endcsname @##1\endcsname{##2}}}%
      \expandafter\tikzext@marshal\tikzext@temp
    }%
  \fi
}
\def\tikzext@nodepicture@doanchors#1#2#3#4{%
  \tikz@scan@one@point\pgfpointtransformed(#2)\relax
  \global\advance\pgf@y by -\tikzext@nodepicture@baseline@\relax
  \global\advance\pgf@x by -\pgf@picminx\relax
  \tikzext@nodepicture@doanchors@xy\tikzext@nodepicture@innerxsep\tikzext@nodepicture@innerysep#3%
  \tikzext@nodepicture@doanchors@xy\tikzext@nodepicture@outerxsep\tikzext@nodepicture@outerysep#4%
  \edef\tikzext@temp{\unless\ifx\tikzext@nodepicture@anchorslist\pgfutil@empty,\fi{#1}{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
  \expandafter\pgfutil@g@addto@macro\expandafter\tikzext@nodepicture@anchorslist\expandafter{\tikzext@temp}%
}
\def\tikzext@nodepicture@doanchors@xy#1#2#3#4{%
  \pgf@xa=#1\relax
  \pgf@ya=#2\relax
  \global\advance\pgf@x by #3\pgf@xa
  \global\advance\pgf@y by #4\pgf@ya
  \edef\tikzext@temp{\unless\ifx\tikzext@nodepicture@anchorslist\pgfutil@empty,\fi{#1}{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
  \expandafter\pgfutil@g@addto@macro\expandafter\tikzext@nodepicture@anchorslist\expandafter{\tikzext@temp}%
}

\let\tikzextorig@tikz@do@fig\tikz@do@fig
\def\tikzext@nodepicture@do{%
  \tikzset{ext/every node picture=\the\tikzext@nodepicture}%
  \tikzextutil@prefixtomacro\tikz@alias{\tikzext@nodepicture@callback@anchors{\tikz@fig@name}}%
  \pgfkeysvalueof{/tikz/prefix after command/.@cmd}%
    \pgfextra{%
      \tikzext@nodepicture@callback@{\tikzlastnode}%
    }%
  \pgfeov
  % If no name is given or name prefixes are disabled
  % don't set a name prefix.
  \ifx\tikz@fig@name\pgfutil@empty % yes, this test happens twice if it's true
    \tikz@fig@mustbenamed
    \pgfkeysdef{/tikz/ext/node picture/name prefix ..}{}% no prefix to reset
  \else
    \iftikzext@nodepicture@prefix
      \tikzextset{node picture/name prefix ../.style/.expanded={/tikz/name prefix={\pgfkeysvalueof{/tikz/name prefix}}}}%
      \pgfkeyslet{/tikz/name prefix}{\tikz@fig@name}%
    \else
      \pgfkeysdef{/tikz/ext/node picture/name prefix ..}{}% no prefix to reset
    \fi
  \fi
  % Save baseline and anchor since they could be changed by node picture scope.
  \pgfkeysgetvalue{/tikz/ext/node picture/baseline}{\tikzext@nodepicture@baseline@save}%
  \pgfkeysgetvalue{/tikz/ext/node picture/anchor}{\tikzext@nodepicture@anchor@save}%
  \let\tikzext@nodepicture@anchors@list@save\tikzext@nodepicture@anchors@list
  \global\let\tikzext@nodepicture@anchorslist\pgfutil@empty
  % Execute every outer node picture inside a group to extract outer seps.
  \begingroup
    \tikzset{ext/every outer node picture=\the\tikzext@nodepicture}%
    \tikz@node@textfont
    \pgf@outer@adjust@hook
    \pgfmathsetlengthmacro\tikzext@nodepicture@innerxsep{\pgfkeysvalueof{/pgf/inner xsep}}%
    \pgfmathsetlengthmacro\tikzext@nodepicture@innerysep{\pgfkeysvalueof{/pgf/inner ysep}}%
    \pgfmathsetlengthmacro\tikzext@nodepicture@outerxsep{\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetlengthmacro\tikzext@nodepicture@outerysep{\pgfkeysvalueof{/pgf/outer ysep}}%
    \edef\tikzext@temp{%
  \endgroup
      \def\noexpand\tikzext@nodepicture@innerxsep{\tikzext@nodepicture@innerxsep}%
      \def\noexpand\tikzext@nodepicture@innerysep{\tikzext@nodepicture@innerysep}%
      \def\noexpand\tikzext@nodepicture@outerxsep{\tikzext@nodepicture@outerxsep}%
      \def\noexpand\tikzext@nodepicture@outerysep{\tikzext@nodepicture@outerysep}}%
    \tikzext@temp
  \setbox\pgfnodeparttextbox=\hbox\bgroup%
    \pgfinterruptpath
      \pgfscope
        \pgfinterruptboundingbox
          \pgftransformreset
          \advance\tikzext@nodepicture by1
          \edef\tikzext@temp{%
            \noexpand\pgfkeysvalueof{/pgf/local bounding box/.@cmd}tikzext@nodepicture\the\tikzext@nodepicture\noexpand\pgfeov
          }\tikzext@temp
          % If there's a node inside the node picture it should be a normal node again.
          \let\tikz@do@fig\tikzextorig@tikz@do@fig
          % Callbacks inside a node picture
          \let\pgf@nodecallback\tikzext@nodepicture@callback
          \let\pgf@matrix@nodecallback\tikzext@nodepicture@matrix@callback
          % Reset some things.
          \pgfkeyssetvalue{/tikz/ext/node picture/baseline}{}%
          \pgfkeyssetvalue{/tikz/ext/node picture/anchor}{}%
          \let\tikzext@nodepicture@anchors@list\pgfutil@gobble
          %
          \let\tikz@transform\relax
          \pgfkeysvalueof{/tikz/ext/node picture/at start}%
          \scope[ext/every node picture scope=\pgfinteval{\the\tikzext@nodepicture-1}]
            \bgroup
              \aftergroup\tikzext@nodepicture@done
}
\def\tikzext@nodepicture@done{%
            % Still inside the node picture scope:
            % Evaluate baseline and anchors.
            \ifx\tikzext@nodepicture@baseline@save\pgfutil@empty
              \pgf@ya=.5\pgf@picminy
              \advance\pgf@ya by .5\pgf@picmaxy
              \xdef\tikzext@nodepicture@baseline@{\the\pgf@ya}%
            \else
              \def\tikzext@temp{\tikz@handle@vec{\tikzext@nodepicture@baseline@coord}{\tikzext@nodepicture@baseline@value}}%
              \expandafter\tikzext@temp\tikzext@nodepicture@baseline@save\relax
            \fi
            % The anchor to place the node picture.
            \ifx\tikzext@nodepicture@anchor@save\pgfutil@empty
              \global\let\tikzext@nodepicture@shift@\pgfutil@empty
            \else
              \def\tikz@anchor{text}%
              \tikz@scan@one@point\tikzext@nodepicture@shift(\tikzext@nodepicture@anchor@save)\relax
            \fi
            % The anchors that the node picture also provides.
            \unless\ifx\tikzext@nodepicture@anchors@list@save\pgfutil@gobble
              \pgfutil@for\tikzext@temp:=\tikzext@nodepicture@anchors@list@save\do{%
                \expandafter\tikzext@nodepicture@doanchors\tikzext@temp
              }%
            \fi
          \endscope
          \pgfkeysvalueof{/tikz/ext/node picture/at end}%
          \iftikzext@nodepicture@ilw
            \global\pgf@picminx=\csname pgf@lbb@minx@tikzext@nodepicture\the\tikzext@nodepicture\endcsname\relax
            \global\pgf@picmaxx=\csname pgf@lbb@maxx@tikzext@nodepicture\the\tikzext@nodepicture\endcsname\relax
            \global\pgf@picminy=\csname pgf@lbb@miny@tikzext@nodepicture\the\tikzext@nodepicture\endcsname\relax
            \global\pgf@picmaxy=\csname pgf@lbb@maxy@tikzext@nodepicture\the\tikzext@nodepicture\endcsname\relax
          \fi
          % The \pgf@picminx etc. are reset by \endpgfinterruptboundingbox,
          % we need to save them and smuggle them out.
          \xdef\tikzext@gtemp{%
            \def\noexpand\tikzext@nodepicture@minx{\the\pgf@picminx}%
            \def\noexpand\tikzext@nodepicture@miny{\the\pgf@picminy}%
            \def\noexpand\tikzext@nodepicture@maxx{\the\pgf@picmaxx}%
            \def\noexpand\tikzext@nodepicture@maxy{\the\pgf@picmaxy}%
          }%
        \endpgfinterruptboundingbox
      \endpgfscope
    \endpgfinterruptpath
  \egroup
  %
  \tikzext@gtemp
  % The node picture is actually empty.
  \ifdim\tikzext@nodepicture@maxx=-16000pt
    \def\tikzext@nodepicture@minx{0pt}%
    \def\tikzext@nodepicture@maxx{0pt}%
    \def\tikzext@nodepicture@miny{0pt}%
    \def\tikzext@nodepicture@maxy{0pt}%
  \fi
  \global\let\tikzext@nodepicture@minx@g\tikzext@nodepicture@minx
  \setbox\pgfnodeparttextbox=\hbox{%
    \kern-\tikzext@nodepicture@minx\relax
    \lower\tikzext@nodepicture@baseline@\relax
    \box\pgfnodeparttextbox
    \kern\tikzext@nodepicture@maxx\relax
  }%
  \pgf@ya=\tikzext@nodepicture@maxy\relax
  \advance\pgf@ya by -\tikzext@nodepicture@baseline@
  \ht\pgfnodeparttextbox=\pgf@ya
  \pgf@ya=-\tikzext@nodepicture@miny\relax
  \advance\pgf@ya by \tikzext@nodepicture@baseline@
  \dp\pgfnodeparttextbox=\pgf@ya
  %
  \ifx\tikzext@nodepicture@shift@\pgfutil@empty
  \else
    \def\tikz@anchor{text}%
    \tikz@addtransform{%
      \pgftransformshift{%
        \tikzext@nodepicture@shift@
        \pgf@x=-\pgf@x
        \advance\pgf@x by\tikzext@nodepicture@minx
        \pgf@y=-\pgf@y
        \advance\pgf@y by\tikzext@nodepicture@baseline@
      }}%
  \fi
  \tikzset{ext/every outer node picture=\the\tikzext@nodepicture}%
  \tikz@fig@continue
}
\pgfutil@namedef{pgf@sh@ns@ext_current node picture bounding box}{rectangle}
\pgfutil@namedef{pgf@sh@np@ext_current node picture bounding box}{%
  \def\southwest{\pgfqpoint{\csname pgf@lbb@minx@tikzext@nodepicture\the\tikzext@nodepicture\endcsname}{\csname pgf@lbb@miny@tikzext@nodepicture\the\tikzext@nodepicture\endcsname}}%
  \def\northeast{\pgfqpoint{\csname pgf@lbb@maxx@tikzext@nodepicture\the\tikzext@nodepicture\endcsname}{\csname pgf@lbb@maxy@tikzext@nodepicture\the\tikzext@nodepicture\endcsname}}%
}
\pgfutil@namedef{pgf@sh@nt@ext_current node picture bounding box}{{1}{0}{0}{1}{0pt}{0pt}}
\pgfutil@namedef{pgf@sh@pi@ext_current node picture bounding box}{\pgfpictureid}


%%% 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
