% 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

\unless\ifcsname ifmemoizing\endcsname
  \expandafter\newif\csname ifmemoizing\endcsname
\fi

\def\tikzextspwrite#1#2#3{%
  \immediate\write\pgfutil@auxout{%
    \noexpand\expandafter\gdef
    \noexpand\csname tikzext@sp@#1\endcsname{{#2}{#3}}%
  }%
}
\def\tikzext@sp@savepicturesize{%
  \pgf@process{%
    \pgfpointdiff
      {\pgfpointanchor{current bounding box}{south west}}%
      {\pgfpointanchor{current bounding box}{north east}}}%
  \pgfutil@tempswafalse % is this the final size adjustment?
  \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
    \tikzext@sp@ifapproxequalto{\pgf@x}{\tikzext@sp@width}{%
      \tikzext@sp@ifapproxequalto{\pgf@y}{\tikzext@sp@height}{%
        % This freezes the picture size when the target is reached (Memoize or no).
        \pgf@x=\tikzext@sp@width\relax
        \pgf@y=\tikzext@sp@height\relax
        \pgfutil@tempswatrue
      }{}%
    }{}%
  }%
  \tikzextspwrite{\pgfpictureid}{\the\pgf@x}{\the\pgf@y}%
  \ifmemoizing
    \ifpgfutil@tempswa % the final adjustment, we won't abort
      % Let Memoize broadcast the picture size to .aux, so that we immediately
      % get the correct picture size if we don't load it anymore.
      \xtoksapp\mmzCCMemo{%
        \noexpand\tikzextspwrite{\pgfpictureid}{\the\pgf@x}{\the\pgf@y}%
      }%
    \else % we'll adjust the size some more, abort
      \mmzAbort
    \fi
  \fi
}
%
% pgfmathapproxequalto checks against 0.0001, too precise for this
%
% SZ: I rewrote this (copying from pgfmathapproxequalto), so that the macro
% doesn't destroy the given arguments.
\def\tikzext@sp@ifapproxequalto#1#2{%
  \begingroup
    \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/tikz/ext/scale picture diff}}%
    \pgfmath@x=#1\relax
    \pgfmath@y=#2\relax
    \advance\pgfmath@x by-\pgfmath@y
    \ifdim\pgfmath@x<0pt\relax
      \multiply\pgfmath@x by-1\relax
    \fi
  \expandafter\endgroup
  \ifdim\pgfmath@x<\pgfutil@tempdima\relax
    \expandafter\pgfutil@firstoftwo
  \else
    \expandafter\pgfutil@secondoftwo
  \fi
}

\def\tikzext@sp@width{%
  \expandafter\expandafter\expandafter\pgfutil@firstoftwo
  \csname tikzext@sp@\pgfpictureid\endcsname
}
\def\tikzext@sp@height{%
  \expandafter\expandafter\expandafter\pgfutil@secondoftwo
  \csname tikzext@sp@\pgfpictureid\endcsname
}

\def\tikzextpicturewidth{%
  \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{0pt}{\tikzext@sp@width}}
\def\tikzextpictureheight{%
  \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{0pt}{\tikzext@sp@height}}

\def\tikzext@sp@init#1{%
  \pgfkeysvalueof{/tikz/execute at end picture/.@cmd}\tikzext@sp@savepicturesize\pgfeov
  \ifmemoizing
    \pgfkeysvalueof{/mmz/context/.@cmd}/tikz/ext/#1\pgfeov
  \fi
}
\tikzextset{
  scale picture diff/.initial=+0.05pt,
  scale picture diff/.value to context/.try,
  %%% keep aspect ratio
  %%  width and height
  minimum picture size/.code 2 args={%
    \tikzext@sp@init{minimum picture size=\the\dimexpr#1\relax\the\dimexpr#2\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@xa{#1}%
      \pgfmathsetlength\pgf@ya{#2}%
      \def\tikz@tempa{0}%
      \def\tikz@temp{0}%
      \ifdim\tikzext@sp@width<\pgf@xa
        \pgfmathsetmacro\tikz@tempa{\pgf@xa/\tikzext@sp@width}%
      \fi
      \ifdim\tikzext@sp@height<\pgf@ya
        \pgfmathsetmacro\tikz@temp{\pgf@ya/\tikzext@sp@height}%
      \fi
      \ifdim\tikz@tempa pt>\tikz@temp pt
        \let\tikz@temp\tikz@tempa
      \fi
      \ifdim\tikz@temp pt>0pt
        \tikzset{scale/.expand once=\tikz@temp}%
      \fi
    }%
  },
  maximum picture size/.code 2 args={%
    \tikzext@sp@init{maximum picture size=\the\dimexpr#1\relax\the\dimexpr#2\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@xa{#1}%
      \pgfmathsetlength\pgf@ya{#2}%
      \def\tikz@tempa{0}%
      \def\tikz@temp{0}%
      \ifdim\tikzext@sp@width>\pgf@xa
        \pgfmathsetmacro\tikz@tempa{\pgf@xa/\tikzext@sp@width}%
      \fi
      \ifdim\tikzext@sp@height>\pgf@ya
        \pgfmathsetmacro\tikz@temp{\pgf@ya/\tikzext@sp@height}%
      \fi
      \ifdim\tikz@tempa pt<\tikz@temp pt
        \let\tikz@temp\tikz@tempa
      \fi
      \ifdim\tikz@temp pt>0pt
        \tikzset{scale/.expand once=\tikz@temp}%
      \fi
    }%
  },
  %%% keep aspect ratio
  %%  width
  picture width/.code={%
    \tikzext@sp@init{picture width=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \tikzset{scale={(#1)/\tikzext@sp@width}}%
    }%
  },
  minimum picture width/.code={%
    \tikzext@sp@init{minimum picture width=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@xa{#1}%
      \ifdim\tikzext@sp@width<\pgf@xa
        \tikzset{scale={\pgf@xa/\tikzext@sp@width}}
      \fi
    }%
  },
  maximum picture width/.code={%
    \tikzext@sp@init{maximum picture width=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@xa{#1}%
      \ifdim\tikzext@sp@width>\pgf@xa
        \tikzset{scale={\pgf@xa/\tikzext@sp@width}}
      \fi
    }%
  },
  %%% xscale only
  %%  width
  picture width*/.code={%
    \tikzext@sp@init{picture width*=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \tikzset{xscale={(#1)/\tikzext@sp@width}}%
    }%
  },
  minimum picture width*/.code={%
    \tikzext@sp@init{minimum picture width*=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@xa{#1}%
      \ifdim\tikzext@sp@width<\pgf@xa
        \tikzset{xscale={\pgf@xa/\tikzext@sp@width}}
      \fi
    }%
  },
  maximum picture width*/.code={%
    \tikzext@sp@init{maximum picture width*=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@xa{#1}%
      \ifdim\tikzext@sp@width>\pgf@xa
        \tikzset{xscale={\pgf@xa/\tikzext@sp@width}}
      \fi
    }%
  },
  %%% keep aspect ratio
  %%  height
  picture height/.code={%
    \tikzext@sp@init{picture height=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \tikzset{scale={(#1)/\tikzext@sp@height}}%
    }%
  },
  minimum picture height/.code={%
    \tikzext@sp@init{minimum picture height=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@ya{#1}%
      \ifdim\tikzext@sp@height<\pgf@ya
        \tikzset{scale={\pgf@ya/\tikzext@sp@height}}
      \fi
    }%
  },
  maximum picture height/.code={%
    \tikzext@sp@init{maximum picture height=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@ya{#1}%
      \ifdim\tikzext@sp@height>\pgf@ya
        \tikzset{scale={\pgf@ya/\tikzext@sp@height}}
      \fi
    }%
  },
  %%% yscale only
  %%  height
  picture height*/.code={%
    \tikzext@sp@init{picture height*=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \tikzset{yscale={(#1)/\tikzext@sp@height}}%
    }%
  },
  minimum picture height*/.code={%
    \tikzext@sp@init{minimum picture height*=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@ya{#1}%
      \ifdim\tikzext@sp@height<\pgf@ya
        \tikzset{yscale={\pgf@ya/\tikzext@sp@height}}
      \fi
    }%
  },
  maximum picture height*/.code={%
    \tikzext@sp@init{maximum picture height*=\the\dimexpr#1\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
      \pgfmathsetlength\pgf@ya{#1}%
      \ifdim\tikzext@sp@height>\pgf@ya
        \tikzset{yscale={\pgf@ya/\tikzext@sp@height}}
      \fi
    }%
  },
  %%% xscale or yscale
  picture size*/.code 2 args={% #1 = width, #2 = height
    \tikzext@sp@init{picture size*=\the\dimexpr#1\relax\the\dimexpr#2\relax}%
    \pgfutil@IfUndefined{tikzext@sp@\pgfpictureid}{}{%
    \tikzset{
      xscale={(#1)/\tikzext@sp@width},
      yscale={(#2)/\tikzext@sp@height}}%
    }%
  }
}

%%% Compatibility
\tikzextset{
  /tikz-ext/compat/add library={scalepicture}{pre 0.6},
  compat/scalepicture/warn/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/scalepicture@}{3}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/picture width}          {/tikz/ext/picture width}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/minimum picture width}  {/tikz/ext/minimum picture width}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/maximum picture width}  {/tikz/ext/maximum picture width}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/picture height}         {/tikz/ext/picture height}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/minimum picture height} {/tikz/ext/minimum picture height}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/maximum picture height} {/tikz/ext/maximum picture height}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/picture size}           {/tikz/ext/picture size}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/minimum picture size}   {/tikz/ext/minimum picture size}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/maximum picture size}   {/tikz/ext/maximum picture size}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/picture width*}         {/tikz/ext/picture width*}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/minimum picture width*} {/tikz/ext/minimum picture width*}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/maximum picture width*} {/tikz/ext/maximum picture width*}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/picture height*}        {/tikz/ext/picture height*}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/minimum picture height*}{/tikz/ext/minimum picture height*}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/maximum picture height*}{/tikz/ext/maximum picture height*}%
    \tikzextutil@compatwarning@key{scalepicture}{pre 0.6}{/tikz/picture size*}          {/tikz/ext/picture size*},%
  compat/scalepicture/pre 0.6/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/scalepicture@}{2}%
    \pgfkeysdef{/tikz/picture width}          {\pgfkeysvalueof{/tikz/ext/picture width/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/minimum picture width}  {\pgfkeysvalueof{/tikz/ext/minimum picture width/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/maximum picture width}  {\pgfkeysvalueof{/tikz/ext/maximum picture width/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/picture height}         {\pgfkeysvalueof{/tikz/ext/picture height/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/minimum picture height} {\pgfkeysvalueof{/tikz/ext/minimum picture height/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/maximum picture height} {\pgfkeysvalueof{/tikz/ext/maximum picture height/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/picture size}           {\pgfkeysvalueof{/tikz/ext/picture size/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/minimum picture size}   {\pgfkeysvalueof{/tikz/ext/minimum picture size/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/maximum picture size}   {\pgfkeysvalueof{/tikz/ext/maximum picture size/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/picture width*}         {\pgfkeysvalueof{/tikz/ext/picture width*/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/minimum picture width*} {\pgfkeysvalueof{/tikz/ext/minimum picture width*/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/maximum picture width*} {\pgfkeysvalueof{/tikz/ext/maximum picture width*/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/picture height*}        {\pgfkeysvalueof{/tikz/ext/picture height*/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/minimum picture height*}{\pgfkeysvalueof{/tikz/ext/minimum picture height*/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/maximum picture height*}{\pgfkeysvalueof{/tikz/ext/maximum picture height*/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/picture size*}          {\pgfkeysvalueof{/tikz/ext/picture size*/.@cmd}##1\pgfeov},
  % compat/scalepicture/0.6/.append code=\pgfkeyssetvalue{/tikz/ext/compat/scalepicture@}{1},
  % compat/scalepicture/newest/.append code=\pgfkeyssetvalue{/tikz/ext/compat/scalepicture@}{0},
  compat/scalepicture/\pgfkeysvalueof{/tikz/ext/compat/scalepicture}%
}
\endinput