% 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{patterns.images}
%
% Macro:
%  - \pgfSetupImageAsPattern[<opt arguments for \pgfdeclareimage>]{<name>}{<path/file>}
%
% Keys are:
%  - use image as pattern=<name>
%  - image as pattern=<opt arguments for \pgftext>

\def\pgfsetupimageaspattern{\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}%
  \sbox\pgfutil@tempboxa{\pgfuseimage{tikzext@pgf@iap@#2}}%
  \expandafter\edef\csname tikzext@pgf@iap@#2@\endcsname{{\the\wd\pgfutil@tempboxa}{\the\ht\pgfutil@tempboxa}}%
}
\tikzset{
  image as pattern/name/.initial=,
  image as pattern/option/.style=,
  image as pattern/options/.style={/tikz/image as pattern/option/.append style={#1}},
  image as pattern/.default=,
  image as pattern/.code={
    \pgfqkeys{/tikz/image as pattern}{#1}%
    \pgfkeysgetvalue{/tikz/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
            \expandafter\pgftext\expandafter[/tikz/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
      }}%
    }
  }
}
\endinput
