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

\def\pgfexttransformxmirror#1{
  \pgfmathparse{#1}%
  \ifpgfmathunitsdeclared
    \pgf@xa\pgfmathresult pt\relax
    \multiply\pgf@xa2 
    \pgftransformcm{-1}{0}{0}{1}{\pgfqpoint{\pgf@xa}{0pt}}%
  \else
    \pgfexttransformmirror
      {\pgfqpointxy{\pgfmathresult}{0}}
      {\pgfpointadd{\pgfqpointxy{\pgfmathresult}{0}}
                   {\pgfqpointxy{0}{1}}}%
  \fi
}
\def\pgfexttransformymirror#1{
  \pgfmathparse{#1}%
  \ifpgfmathunitsdeclared
    \pgf@ya\pgfmathresult pt\relax
    \multiply\pgf@ya2
    \pgftransformcm{1}{0}{0}{-1}{\pgfqpoint{0pt}{\pgf@ya}}%
  \else
    \edef\pgf@marshal{%
      \noexpand\pgfexttransformmirror{\noexpand\pgfqpointxy{0}{\pgfmathresult}}%
                                     {\noexpand\pgfpointadd{\noexpand\pgfqpointxy{0}{\pgfmathresult}}
                                                           {\noexpand\pgfqpointxy{1}{0}}}}%
    \pgf@marshal
  \fi}
\def\pgfexttransformmirror#1#2{%
  \pgfextract@process\pgf@tempa{#1}%
  \pgfpointnormalised{\pgfpointdiff{\pgf@tempa}{#2}}%
  \pgf@xa=\pgf@sys@tonumber\pgf@y\pgf@x
  \pgf@xb=\pgf@sys@tonumber\pgf@x\pgf@x
  \pgf@yb=\pgf@sys@tonumber\pgf@y\pgf@y
  \multiply\pgf@xa2
  \pgf@xc=-\pgf@yb\advance\pgf@xc\pgf@xb
  \pgf@yc=-\pgf@xb\advance\pgf@yc\pgf@yb
  \edef\pgf@temp{{\the\pgf@xc}{+\the\pgf@xa}{+\the\pgf@xa}{+\the\pgf@yc}}%
  \expandafter\pgf@transformcm\pgf@temp{\pgf@tempa}%
  \pgftransformshift{\pgfpointscale{-1}{\pgf@tempa}}%
}

\def\pgfextqtransformmirror#1{%
  \pgfpointnormalised{#1}%
  \pgf@xa=\pgf@sys@tonumber\pgf@y\pgf@x
  \pgf@xb=\pgf@sys@tonumber\pgf@x\pgf@x
  \pgf@yb=\pgf@sys@tonumber\pgf@y\pgf@y
  \multiply\pgf@xa2
  \pgf@xc=-\pgf@yb\advance\pgf@xc\pgf@xb
  \pgf@yc=-\pgf@xb\advance\pgf@yc\pgf@yb
  \edef\pgf@temp{{\the\pgf@xc}{+\the\pgf@xa}{+\the\pgf@xa}{+\the\pgf@yc}}%
  \expandafter\pgf@transformcm\pgf@temp{\pgfpointorigin}%
}

% Using existent transformation (shift, rotate, yscale=-1, rotate back, shift back)
\def\pgfexttransformxMirror#1{
  \pgfmathparse{#1}%
  \ifpgfmathunitsdeclared
    \pgf@xa\pgfmathresult pt\relax
    \multiply\pgf@xa2 
    \pgftransformcm{-1}{0}{0}{1}{\pgfqpoint{\pgf@xa}{0pt}}%
  \else
    \pgfexttransformMirror
      {\pgfqpointxy{\pgfmathresult}{0}}
      {\pgfpointadd{\pgfqpointxy{\pgfmathresult}{0}}{\pgfqpointxy{0}{1}}}%
  \fi
}
\def\pgfexttransformyMirror#1{
  \pgfmathparse{#1}%
  \ifpgfmathunitsdeclared
    \pgf@ya\pgfmathresult pt\relax
    \multiply\pgf@ya2
    \pgftransformcm{1}{0}{0}{-1}{\pgfqpoint{0pt}{\pgf@ya}}%
  \else
    \edef\pgf@marshal{%
      \noexpand\pgfexttransformMirror{\noexpand\pgfqpointxy{0}{\pgfmathresult}}%
                                     {\noexpand\pgfpointadd{\noexpand\pgfqpointxy{0}{\pgfmathresult}}
                                                           {\noexpand\pgfqpointxy{1}{0}}}}%
    \pgf@marshal
  \fi}

\def\pgfexttransformMirror#1#2{%
  \pgfextract@process\pgf@tempa{#1}%
  \pgfmathanglebetweenpoints{\pgf@tempa}{#2}%
  \let\pgf@temp\pgfmathresult
  \pgftransformshift{\pgf@tempa}%
  \pgftransformrotate{\pgf@temp}%
  \pgftransformyscale{-1}%
  \pgftransformrotate{-\pgf@temp}%
  \pgftransformshift{\pgfqpointscale{-1}{\pgf@tempa}}%
}

\def\pgfextqtransformMirror#1{%
  \pgfmathanglebetweenpoints{\pgfpointorigin}{#1}%
  \let\pgf@temp\pgfmathresult
  \pgftransformrotate{\pgf@temp}%
  \pgftransformyscale{-1}%
  \pgftransformrotate{-\pgf@temp}%
}

%%% Compatibility
\tikzextset{
  /tikz-ext/compat/add library={transformations.mirror}{pre 0.6},
  compat/transformations.mirror/warn/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/transformations.mirror@}{3}%
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgftransformxmirror}\pgfexttransformxmirror\pgfutil@gobble
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgftransformymirror}\pgfexttransformymirror\pgfutil@gobble
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgftransformmirror} \pgfexttransformmirror\pgfutil@gobbletwo
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgfqtransformmirror}\pgfextqtransformmirror\pgfutil@gobble
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgftransformxMirror}\pgfexttransformxMirror\pgfutil@gobble
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgftransformyMirror}\pgfexttransformyMirror\pgfutil@gobble
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgftransformMirror} \pgfexttransformMirror\pgfutil@gobbletwo
    \tikzextutil@compatwarning@cmd{transformations.mirror}{pre 0.6}{pgfqtransformMirror}\pgfextqtransformMirror\pgfutil@gobble,
  compat/transformations.mirror/pre 0.6/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/transformations.mirror@}{2}%
    \let\pgftransformxmirror\pgfexttransformxmirror
    \let\pgftransformymirror\pgfexttransformymirror
    \let\pgftransformmirror \pgfexttransformmirror
    \let\pgfqtransformmirror\pgfextqtransformmirror
    \let\pgftransformxMirror\pgfexttransformxMirror
    \let\pgftransformyMirror\pgfexttransformyMirror
    \let\pgftransformMirror \pgfexttransformMirror
    \let\pgfqtransformMirror\pgfextqtransformMirror,
  % compat/transformations.mirror/0.6/.append code=\pgfkeyssetvalue{/tikz/ext/compat/transformations.mirror@}{1},
  % compat/transformations.mirror/newest/.append code=\pgfkeyssetvalue{/tikz/ext/compat/transformations.mirror@}{0},
  compat/transformations.mirror/\pgfkeysvalueof{/tikz/ext/compat/transformations.mirror}%
}
\endinput
