% 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.
%
% This is the TikZ library patterns.images
% Load with \usetikzlibrary{ext.patterns.images}
%
% Macro:
%  - \pgfextsetupimageaspattern[<opt arguments for \pgfdeclareimage>]{<name>}{<path/file>}
%
% Keys are:
%  - use image as pattern=<name>
%  - image as pattern=<opt arguments for \pgftext>

\unless\ifcsname tikzextset\endcsname
  \input tikzext-util.tex
\fi

\def\tikzextsetupimageaspattern{\pgfutil@ifnextchar[\tikzext@pgfsetupimageaspattern{\tikzext@pgfsetupimageaspattern[]}}
\def\tikzext@pgfsetupimageaspattern[#1]#2#3{% #1 = options
                                           % #2 = image name
                                           % #3 = file name
  \pgfdeclareimage[#1]{tikzext@pgf@iap@#2}{#3}%
  \setbox\pgfutil@tempboxa\hbox{\pgfuseimage{tikzext@pgf@iap@#2}}%
  \expandafter\edef\csname tikzext@pgf@iap@#2@\endcsname{{\the\wd\pgfutil@tempboxa}{\the\ht\pgfutil@tempboxa}}%
}
\tikzextset{
  image as pattern/name/.initial=,
  image as pattern/option/.style=,
  image as pattern/options/.style={/tikz/ext/image as pattern/option/.append style={#1}},
  image as pattern/.default=,
  image as pattern/.code={
    \pgfqkeys{/tikz/ext/image as pattern}{#1}%
    \pgfkeysgetvalue{/tikz/ext/image as pattern/name}\tikz@temp
    \pgfutil@ifxempty\tikz@temp{%
      \tikzerror{You need to specify a name for the ``image-pattern'' to be used.}%
    }{%
      \edef\tikz@tempa{\expandafter\expandafter\expandafter\pgfutil@firstoftwo\csname tikzext@pgf@iap@\tikz@temp @\endcsname}%
      \edef\tikz@tempb{\expandafter\expandafter\expandafter\pgfutil@secondoftwo\csname tikzext@pgf@iap@\tikz@temp @\endcsname}%
      \pgfkeysalso{/tikz/path picture={%
        \pgf@process{\pgfpointanchor{path picture bounding box}{north east}}%
        \pgf@xa\pgf@x\pgf@ya\pgf@y
        \pgf@process{\pgfpointanchor{path picture bounding box}{south west}}%
        \pgf@xb\pgf@x\pgf@yb\pgf@y\pgf@yc\pgf@yb
        \pgfutil@loop
          {%
          \pgfutil@loop
            \pgftext[/tikz/ext/image as pattern/option,at=\pgfqpoint{\pgf@xb}{\pgf@yb}]{\pgfuseimage{tikzext@pgf@iap@\tikz@temp}}%
            \ifdim\pgf@yb<\pgf@ya
            \advance\pgf@yb\tikz@tempb
          \pgfutil@repeat
          }%
          \ifdim\pgf@xb<\pgf@xa
          \advance\pgf@xb\tikz@tempa
          \pgf@yb\pgf@yc
        \pgfutil@repeat
      }}%
    }
  }
}
%%% Compatibility
\tikzextset{
  /tikz-ext/compat/add library={patterns.images}{pre 0.6},
  compat/patterns.images/warn/.append code={%
    \tikzextutil@compatwarning@key{patterns.images}{pre 0.6}{/tikz/image as pattern}{/pgf/ext/image as pattern}%
    \tikzextutil@compatwarning@key{patterns.images}{pre 0.6}{/tikz/image as pattern/options}{/pgf/ext/image as pattern/options}%
    \tikzextutil@compatwarning@valuekey{patterns.images}{pre 0.6}{/tikz/image as pattern/name}{/tikz/ext/image as pattern/name}%
    \tikzextutil@compatwarning@cmd{patterns.images}{pre 0.6}{pgfsetupimageaspattern}{\tikzextsetupimageaspattern}{}%
    \pgfkeysaddvalue{/tikz/ext/image as pattern/option}{\pgfkeysalso{/tikz/image as pattern/option}}{}%
  },
  compat/patterns.images/pre 0.6/.append code={%
    \pgfkeysdef{/tikz/image as pattern}{\pgfkeysvalueof{/tikz/ext/image as pattern/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/image as pattern/options}{\pgfkeysvalueof{/tikz/ext/image as pattern/options/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/image as pattern/name}{\pgfkeyssetvalue{/tikz/ext/image as pattern/name/.@cmd}##1\pgfeov}%
    \pgfkeyssetvalue{/tikz/image as pattern/name}{\pgfkeysvalueof{/tikz/ext/image as pattern/name}}%
    \pgfkeysaddvalue{/tikz/ext/image as pattern/option}{\pgfkeysalso{/tikz/image as pattern/option}}{}%
    \let\pgfsetupimageaspattern\tikzextsetupimageaspattern
  },
  compat/patterns.images/\pgfkeysvalueof{/tikz/ext/compat/patterns.images}%
}
\endinput
