% 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.
%
\pgfutil@IfUndefined{pgfnodepartthreebox}{%
  \csname newbox\endcsname\pgfnodepartleftbox
  \csname newbox\endcsname\pgfnodepartrightbox
}{%
  \let\pgfnodepartleftbox\pgfnodeparttwobox
  \let\pgfnodepartrightbox\pgfnodepartthreebox
}
\unless\ifcsname pgfextset\endcsname
  \input tikzext-util.tex
\fi

\newif\iftikzext@uncenteredrectangle@usesavedcenter

\pgfextset{
  uncentered rectangle use saved center/.is if=tikzext@uncenteredrectangle@usesavedcenter,
  uncentered rectangle center yshift/.initial=,
  uncentered rectangle center/@center/.initial=2,
  uncentered rectangle center/.is choice,
  uncentered rectangle center/real/.code=\pgfkeyssetvalue{/pgf/ext/uncentered rectangle center/@center}{0},
  uncentered rectangle center/left/.code=\pgfkeyssetvalue{/pgf/ext/uncentered rectangle center/@center}{1},
  uncentered rectangle center/text/.code=\pgfkeyssetvalue{/pgf/ext/uncentered rectangle center/@center}{2},
  uncentered rectangle center/right/.code=\pgfkeyssetvalue{/pgf/ext/uncentered rectangle center/@center}{3}}

\pgfdeclareshape{ext_uncentered rectangle}{%
  \nodeparts{text,left,right}%
  \savedanchor\middleCenter{%
    \pgf@x=.5\wd\pgfnodeparttextbox
    %
    \pgf@y=\dp\pgfnodeparttextbox
    \ifdim\dp\pgfnodepartleftbox>\pgf@y
      \pgf@y=\dp\pgfnodepartleftbox
    \fi
    \ifdim\dp\pgfnodepartrightbox>\pgf@y
      \pgf@y=\dp\pgfnodepartrightbox
    \fi
    %
    \pgf@ya=\ht\pgfnodeparttextbox
    \ifdim\ht\pgfnodepartleftbox>\pgf@ya
      \pgf@ya=\ht\pgfnodepartleftbox
    \fi
    \ifdim\ht\pgfnodepartrightbox>\pgf@ya
      \pgf@ya=\ht\pgfnodepartrightbox
    \fi
    \advance\pgf@ya by -\pgf@y
    \pgf@y=.5\pgf@ya
  }%
  \savedanchor\leftCenter{%
    % y should be still the same as before
    \pgf@x=-.5\wd\pgfnodepartleftbox
  }%
  \savedanchor\rightCenter{%
    % y should be still the same as before
    \pgf@x=.5\wd\pgfnodepartrightbox
    \advance\pgf@x by \wd\pgfnodeparttextbox
  }%
  \savedanchor\realCenter{%
    % y should be still the same as before
    \pgf@x=-.5\wd\pgfnodepartleftbox
    \advance\pgf@x by .5\wd\pgfnodeparttextbox
    \advance\pgf@x by .5\wd\pgfnodepartrightbox
  }%
  \savedanchor\southwest{%
    % total width
    \pgf@xa=\wd\pgfnodepartleftbox
    \advance\pgf@xa by\wd\pgfnodepartrightbox
    \advance\pgf@xa by\wd\pgfnodeparttextbox
    %
    \pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/inner xsep}}%
    \advance\pgf@xa by 2\pgf@xc
    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/minimum width}}%
    \ifdim\pgf@xb>\pgf@xa
      \pgf@xa=\pgf@xb
    \fi
    % left side of shape
    \advance\pgf@x by -.5\pgf@xa
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
    \advance\pgf@x by -\pgf@xa
    %
    % total height
    \pgf@ya=\dp\pgfnodepartleftbox
    \ifdim\pgf@ya<\dp\pgfnodeparttextbox
      \pgf@ya=\dp\pgfnodeparttextbox
    \fi
    \ifdim\pgf@ya<\dp\pgfnodepartrightbox
      \pgf@ya=\dp\pgfnodepartrightbox
    \fi
    \pgf@yb=\ht\pgfnodepartleftbox
    \ifdim\pgf@yb<\ht\pgfnodeparttextbox
      \pgf@yb=\ht\pgfnodeparttextbox
    \fi
    \ifdim\pgf@yb<\ht\pgfnodepartrightbox
      \pgf@yb=\ht\pgfnodepartrightbox
    \fi
    \advance\pgf@ya by \pgf@yb
    %
    \pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
    \advance\pgf@ya by 2\pgf@yc
    \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
    \ifdim\pgf@yb>\pgf@ya
      \pgf@ya=\pgf@yb
    \fi
    % lower side of the shape
    \advance\pgf@y by -.5\pgf@ya
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
    \advance\pgf@y by -\pgf@ya
  }%
  \savedanchor\northeast{%
    % first, let's calculate the real center again:
    %
    \pgf@x=-.5\wd\pgfnodepartleftbox
    \advance\pgf@x by .5\wd\pgfnodeparttextbox
    \advance\pgf@x by .5\wd\pgfnodepartrightbox
    %
    \pgf@y=\dp\pgfnodeparttextbox
    \ifdim\dp\pgfnodepartleftbox>\pgf@y
      \pgf@y=\dp\pgfnodepartleftbox
    \fi
    \ifdim\dp\pgfnodepartrightbox>\pgf@y
      \pgf@y=\dp\pgfnodepartrightbox
    \fi
    %
    \pgf@ya=\ht\pgfnodeparttextbox
    \ifdim\ht\pgfnodepartleftbox>\pgf@ya
      \pgf@ya=\ht\pgfnodepartleftbox
    \fi
    \ifdim\ht\pgfnodepartrightbox>\pgf@ya
      \pgf@ya=\ht\pgfnodepartrightbox
    \fi
    \advance\pgf@ya by -\pgf@y
    \pgf@y=.5\pgf@ya
    %
    % now we can calculate the anchor
    % total width
    %
    \pgf@xa=\wd\pgfnodepartleftbox
    \advance\pgf@xa by\wd\pgfnodepartrightbox
    \advance\pgf@xa by\wd\pgfnodeparttextbox
    %
    \pgfmathsetlength\pgf@xc{\pgfkeysvalueof{/pgf/inner xsep}}%
    \advance\pgf@xa by 2\pgf@xc
    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/pgf/minimum width}}%
    \ifdim\pgf@xb>\pgf@xa
      \pgf@xa=\pgf@xb
    \fi
    % right side of shape
    \advance\pgf@x by .5\pgf@xa
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/outer xsep}}%
    \advance\pgf@x by \pgf@xa
    %
    % total height
    \pgf@ya=\dp\pgfnodepartleftbox
    \ifdim\pgf@ya<\dp\pgfnodeparttextbox
      \pgf@ya=\dp\pgfnodeparttextbox
    \fi
    \ifdim\pgf@ya<\dp\pgfnodepartrightbox
      \pgf@ya=\dp\pgfnodepartrightbox
    \fi
    \pgf@yb=\ht\pgfnodepartleftbox
    \ifdim\pgf@yb<\ht\pgfnodeparttextbox
      \pgf@yb=\ht\pgfnodeparttextbox
    \fi
    \ifdim\pgf@yb<\ht\pgfnodepartrightbox
      \pgf@yb=\ht\pgfnodepartrightbox
    \fi
    \advance\pgf@ya by \pgf@yb
    %
    \pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
    \advance\pgf@ya by 2\pgf@yc
    \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
    \ifdim\pgf@yb>\pgf@ya
      \pgf@ya=\pgf@yb
    \fi
    % upper side of the shape
    \advance\pgf@y by .5\pgf@ya
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/outer ysep}}%
    \advance\pgf@y by \pgf@ya
  }%
  %
  \savedmacro\center{%
    \pgfkeysgetvalue{/pgf/ext/uncentered rectangle center/@center}\center
  }
  \anchor{center}{%
    \ifcase
      \iftikzext@uncenteredrectangle@usesavedcenter
        \center
      \else
        \pgfkeysvalueof{/pgf/ext/uncentered rectangle center/@center}%
      \fi\relax
      \realCenter\or
      \leftCenter\or
      \middleCenter\or
      \rightCenter\else
      \realCenter
    \fi
    \pgfkeysgetvalue{/pgf/ext/uncentered rectangle center yshift}\pgf@temp
    \pgfutil@ifxempty\pgf@temp{% real center used
    }{% calculate center
      \pgfmathsetlength\pgf@y{\pgf@temp}%
    }%
  }%
  \anchor{left}        {\leftCenter  \multiply\pgf@x by2 \pgf@y=0pt }
  \anchor{right}       {\middleCenter\multiply\pgf@x by2 \pgf@y=0pt }
  \anchor{real center} {\realCenter}
  \anchor{left center} {\leftCenter}
  \anchor{right center}{\rightCenter}
  \anchor{text center} {\middleCenter}
  \anchor{south west}  {\southwest}
  \anchor{north east}  {\northeast}
  \anchor{mid}{%
    \csname pgf@anchor@ext_uncentered rectangle@center\endcsname
    \pgfmathsetlength\pgf@y{.5ex}}%
  \anchor{base}{%
    \csname pgf@anchor@ext_uncentered rectangle@center\endcsname
    \pgf@y=0pt
  }%
  \anchor{left mid}   {\leftCenter  \pgfmathsetlength\pgf@y{+.5ex}}
  \anchor{text mid}   {\middleCenter\pgfmathsetlength\pgf@y{+.5ex}}
  \anchor{right mid}  {\rightCenter \pgfmathsetlength\pgf@y{+.5ex}}
  \anchor{real mid}   {\realCenter  \pgfmathsetlength\pgf@y{+.5ex}}
  \anchor{left base}  {\leftCenter  \pgf@y=0pt }
  \anchor{text base}  {\middleCenter\pgf@y=0pt }
  \anchor{right base} {\rightCenter \pgf@y=0pt }
  \anchor{real base}  {\realCenter  \pgf@y=0pt }
  \anchor{real south} {\pgf@anchor@rectangle@south}
  \anchor{real north} {\pgf@anchor@rectangle@north}
  \anchor{text north} {\pgf@anchor@rectangle@north\pgf@ya=\pgf@y\middleCenter\pgf@y=\pgf@ya}
  \anchor{left north} {\pgf@anchor@rectangle@north\pgf@ya=\pgf@y\leftCenter  \pgf@y=\pgf@ya}
  \anchor{right north}{\pgf@anchor@rectangle@north\pgf@ya=\pgf@y\rightCenter \pgf@y=\pgf@ya}
  \anchor{text south} {\pgf@anchor@rectangle@south\pgf@ya=\pgf@y\middleCenter\pgf@y=\pgf@ya}
  \anchor{left south} {\pgf@anchor@rectangle@south\pgf@ya=\pgf@y\leftCenter  \pgf@y=\pgf@ya}
  \anchor{right south}{\pgf@anchor@rectangle@south\pgf@ya=\pgf@y\rightCenter \pgf@y=\pgf@ya}
  \anchor{north}{%
    \pgf@anchor@rectangle@north
    \pgf@ya=\pgf@y
    \csname pgf@anchor@ext_uncentered rectangle@center\endcsname
    \pgf@y=\pgf@ya
  }
  \anchor{south}{%
    \pgf@anchor@rectangle@south
    \pgf@ya=\pgf@y
    \csname pgf@anchor@ext_uncentered rectangle@center\endcsname
    \pgf@y=\pgf@ya
  }
  %\anchor{base right}{\middleCenter\pgf@xa=2\pgf@x}% TODO
  \anchor{real west}{%
    \pgf@process{\northeast}%
    \pgf@ya=.5\pgf@y
    \pgf@process{\southwest}%
    \pgf@y=.5\pgf@y
    \advance\pgf@y by \pgf@ya
  }%
  \anchor{real east}{%
    \pgf@process{\southwest}%
    \pgf@ya=.5\pgf@y
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y
    \advance\pgf@y by \pgf@ya
  }%
  \anchor{west}{%
    \pgf@process{\southwest}%
    \pgf@xa=\pgf@x
    \pgf@process{\csname pgf@anchor@ext_uncentered rectangle@center\endcsname}%
    \pgf@x=\pgf@xa
  }%
  \anchor{east}{%
    \pgf@process{\northeast}%
    \pgf@xa=\pgf@x
    \pgf@process{\csname pgf@anchor@ext_uncentered rectangle@center\endcsname}%
    \pgf@x=\pgf@xa
  }%
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{south east}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritbackgroundpath[from=rectangle]
  \anchorborder{%
    % xa/ya is target
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    % xc/yc is "center"
    \pgf@process{\csname pgf@anchor@ext_uncentered rectangle@center\endcsname}%
    \pgf@xc=\pgf@x
    \pgf@yc=\pgf@y
    \ifdim\pgf@xa<0pt
      % left side
      \southwest
      \pgf@xb=\pgf@x
      \pgf@yb=\pgf@y
      \advance\pgf@xb by -\pgf@xc
      \ifdim\pgf@ya<0pt
        % south west side
        \advance\pgf@yb by -\pgf@yc
        \pgf@yb=-\pgf@yb
      \else
        % north west side
        \northeast
        \pgf@yb=\pgf@y
        \advance\pgf@yb by -\pgf@yc
      \fi
      \pgf@xb=-\pgf@xb
      \edef\pgf@marshal{%
        \noexpand\pgfpointborderrectangle
          {\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}
          {\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}%
      }%
    \else
      % right side
      \northeast
      \pgf@xb=\pgf@x
      \pgf@yb=\pgf@y
      \advance\pgf@xb by -\pgf@xc
      \ifdim\pgf@ya>0pt
        % north east side
        \advance\pgf@yb by -\pgf@yc
      \else
        % south east side
        \southwest
        \pgf@yb=\pgf@y
        \advance\pgf@yb by -\pgf@yc
        \pgf@yb=-\pgf@yb
      \fi
      \edef\pgf@marshal{%
        \noexpand\pgfpointborderrectangle
          {\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}
          {\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}%
      }%
    \fi
    \pgf@process{\pgf@marshal}%
    \advance\pgf@x by\pgf@xc
    \advance\pgf@y by\pgf@yc
  }%
}
%%% Compatibility
\pgfkeysifdefined{/tikz/ext/compat/shapes/.@cmd}{}{\pgfextset{/tikz-ext/compat/add library={shapes}{pre 0.6}}}
\tikzextset{
  compat/shapes/warn/.append style=/tikz/ext/compat/shapes.uncenteredrectangle/warn,
  compat/shapes/pre 0.6/.append style=/tikz/ext/compat/shapes.uncenteredrectangle/pre 0.6,
  compat/shapes.uncenteredrectangle/warn/.code={%
    \tikzextutil@compatwarning@key{shapes}{pre 0.6}{/pgf/uncentered rectangle use saved center}[true]{/pgf/ext/uncentered rectangle use saved center}%
    \tikzextutil@compatwarning@valuekey{shapes}{pre 0.6}{/pgf/uncentered rectangle center yshift}{/pgf/ext/uncentered rectangle center yshift}
    \tikzextutil@compatwarning@key{shapes}{pre 0.6}{/pgf/uncentered rectangle center}{/pgf/ext/uncentered rectangle center}
    \tikzextutil@compat@cloneshape{uncentered rectangle}{text,north,north west,north east,center,west,east,mid,mid west,mid east,base,base west,base east,south,south west,south east,center,real center,real mid,real base,real south,left,left north,left center,left mid,left base,left south,text north,text center,text mid,text base,text south,right north,right center,right mid,right base,right south}%
    \def\tikzext@temp{\tikzextutil@compatmaybewarning{shape 'uncentered rectangle'}{'ext_uncentered rectangle'}{shapes = pre 0.6}}%
    \expandafter\tikzextutil@prefix@macrotomacro\csname pgf@sh@s@uncentered rectangle\endcsname\tikzext@temp
  },
  compat/shapes.uncenteredrectangle/pre 0.6/.code={%
    \pgfkeysdef{/pgf/uncentered rectangle use saved center}{\pgfkeysvalueof{/pgf/ext/uncentered rectangle use saved center/.@cmd}##1\pgfeov}%
    \pgfkeyssetvalue{/pgf/uncentered rectangle use saved center/.@def}{true}%
    \pgfkeysdef{/pgf/uncentered rectangle center}{\pgfkeysvalueof{/pgf/ext/uncentered rectangle center/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/pgf/uncentered rectangle center yshift}{\pgfkeyssetvalue{/pgf/ext/uncentered rectangle center yshift}{##1}}%
    \pgfkeyssetvalue{/pgf/uncentered rectangle center yshift}{\pgfkeysvalueof{/pgf/ext/uncentered rectangle center yshift}}%
    \tikzextutil@compat@cloneshape{uncentered rectangle}{text,north,north west,north east,center,west,east,mid,mid west,mid east,base,base west,base east,south,south west,south east,center,real center,real mid,real base,real south,left,left north,left center,left mid,left base,left south,text north,text center,text mid,text base,text south,right north,right center,right mid,right base,right south}%
  },
  compat/shapes.uncenteredrectangle/\pgfkeysvalueof{/tikz/ext/compat/shapes}%
}
\endinput