%%
%% This is file `graphicscache.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% graphicscache.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2021 by Max Schwarz <max.schwarz@online.de>
%% based on the sty2dtx template by Scott Pakin <scott+dtx@pakin.org>.
%% 
%% This file may be distributed and/or modified under the conditions of
%% the BSD 3-clause license (see LICENSE).
%% 
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{graphicscache}
    [2022/12/20 v0.4 Windows support and special character escaping]
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{graphicscache}[2018/10/02 Graphics Cache]
\RequirePackage{graphicx}
\RequirePackage{xstring}
\RequirePackage{filemod}
\RequirePackage{letltxmacro}
\RequirePackage{pgfopts}
\RequirePackage{pgffor}
\RequirePackage{ifplatform}
\RequirePackage{pdftexcmds}
\RequirePackage{ltxcmds}
\newif\ifgraphicscache@render
\newif\ifgraphicscache@compress
\newif\ifgraphicscache@listing
\newif\ifgraphicscache@hashshortnames
\newif\ifgraphicscache@gsnotavailable\graphicscache@gsnotavailablefalse
\def\graphicscache@graphicsargs{}
\newlength\graphicscache@tmplen
\newcommand{\graphicscache@addarg}[1]{%
  \ifx\graphicscache@graphicsargs\empty
    \edef\graphicscache@graphicsargs{#1}%
  \else
    \edef\graphicscache@graphicsargs{\graphicscache@graphicsargs,#1}%
  \fi
}
\pgfkeys{
  /graphicscache/.cd,
  render/.is if=graphicscache@render,
  render=true,
  cachedir/.store in=\graphicscache@cachedir,
  cachedir={graphicscache},
  compress/.is choice,
  compress/false/.code={\graphicscache@compressfalse},
  compress/jpeg/.code={\graphicscache@compresstrue \def\graphicscache@compress@mode{DCTEncode}},
  compress/flat/.code={\graphicscache@compresstrue \def\graphicscache@compress@mode{FlatEncode}},
  compress=jpeg,
  dpi/.store in=\graphicscache@dpi,
  dpi=300,
  qfactor/.store in=\graphicscache@qfactor,
  qfactor={0.15},
  hashshortnames/.is if=graphicscache@hashshortnames,
  hashshortnames=false,
  width/.code={%
    \setlength\graphicscache@tmplen{#1}%
    \graphicscache@addarg{width=\the\graphicscache@tmplen}%
  },
  height/.code={%
    \setlength\graphicscache@tmplen{#1}%
    \graphicscache@addarg{height=\the\graphicscache@tmplen}%
  },
  trim/.code={\graphicscache@addarg{trim=#1}},
  clip/.code={\graphicscache@addarg{clip}},
  angle/.code={%
    \edef\graphicscache@tmp{#1}%
    \graphicscache@addarg{angle=\graphicscache@tmp}%
  },
  origin/.code={\graphicscache@addarg{origin=#1}},
  keepaspectratio/.code={\graphicscache@addarg{keepaspectratio}},
  scale/.code={%
    \edef\graphicscache@tmp{#1}%
    \graphicscache@addarg{scale=\graphicscache@tmp}%
  },
  page/.code={%
    \edef\graphicscache@tmp{#1}%
    \graphicscache@addarg{page=\graphicscache@tmp}%
  },
  listing/.is if=graphicscache@listing,
  listing=false,
  frame/.code={%
    \edef\graphicscache@tmp{#1}%
    \graphicscache@addarg{frame=\graphicscache@tmp}%
  },
  valign/.code={%
    \edef\graphicscache@tmp{#1}%
    \graphicscache@addarg{valign=\graphicscache@tmp}%
  },
  raise/.code={%
    \edef\graphicscache@tmp{#1}%
    \graphicscache@addarg{raise=\graphicscache@tmp}%
  },
}
\ProcessPgfOptions{/graphicscache}\relax
\ifdefined\graphicscache@inhibit
  \pgfkeys{/graphicscache/render=false}%
\fi
\ifgraphicscache@listing
  \newwrite\graphicscache@listout
  \immediate\openout\graphicscache@listout=\jobname.graphicscache
\fi
\ifx\lastsavedimageresourcepages\@undefined
  \protected\def\graphicscache@ShellEscape{\immediate\write18 }
\else
  \protected\def\graphicscache@ShellEscape#1{%
    \directlua{os.execute("\luaescapestring{#1}")}}
\fi
\newcommand{\graphicscache@callgswithname}[1]{%
  \ifwindows
    \graphicscache@ShellEscape{#1
      -sOutputFile=\graphicscache@output\space
      -sDEVICE=pdfwrite
      -dCompatibilityLevel=1.4
      -dPDFSETTINGS=/prepress
      -dNOPAUSE -dQUIET -dBATCH
      -c "<<
        /AutoFilterColorImages false
        /EncodeColorImages true
        /ColorImageFilter /\graphicscache@compress@mode\space
        /ColorImageDict << /ColorTransform 1 /QFactor \graphicscache@qfactor\space /Blend 1 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>
        /ColorImageResolution \graphicscache@dpi\space
        /AutoFilterGrayImages false
        /EncodeGrayImages true
        /GrayImageFilter /\graphicscache@compress@mode\space
        /GrayImageDict << /ColorTransform 1 /QFactor \graphicscache@qfactor\space /Blend 1 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>
        /GrayImageResolution \graphicscache@dpi\space
      >> setdistillerparams"
      -f \graphicscache@cachedir\string\graphicscacheout.pdf
    }%
  \else
    \graphicscache@ShellEscape{#1
      -sOutputFile=\graphicscache@output\space
      -sDEVICE=pdfwrite
      -dCompatibilityLevel=1.4
      -dPDFSETTINGS=/prepress
      -dNOPAUSE -dQUIET -dBATCH
      -c '<<
        /AutoFilterColorImages false
        /EncodeColorImages true
        /ColorImageFilter /\graphicscache@compress@mode\space
        /ColorImageDict << /ColorTransform 1 /QFactor \graphicscache@qfactor\space /Blend 1 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>
        /ColorImageResolution \graphicscache@dpi\space
        /AutoFilterGrayImages false
        /EncodeGrayImages true
        /GrayImageFilter /\graphicscache@compress@mode\space
        /GrayImageDict << /ColorTransform 1 /QFactor \graphicscache@qfactor\space /Blend 1 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>
        /GrayImageResolution \graphicscache@dpi\space
      >> setdistillerparams'
      -f \graphicscache@cachedir/graphicscacheout.pdf \string|\string| rm \graphicscache@output
    }%
  \fi
}
\newcommand{\graphicscache@callgs}{%
  \ifgraphicscache@gsnotavailable
  \else
    \@ifundefined{graphicscache@gscommand}{%
      \foreach \cmd in {rungs,gs,mgs} {%
        \PackageInfo{graphicscache}{Trying \cmd\space to call ghostscript...^^J}%
        \graphicscache@callgswithname{\cmd}%
        \IfFileExists{\graphicscache@output}{%
            \PackageInfo{graphicscache}{Found a working ghostscript called '\cmd'.}%
            \global\edef\graphicscache@gscommand{\cmd}%
            \breakforeach
        }{}%
      }%
      \@ifundefined{graphicscache@gscommand}{%
        \PackageWarning{graphicscache}{Could not find a working ghostscript executable. I will not compress any images.}{}%
        \graphicscache@gsnotavailabletrue
      }{}%
    }{%
      \PackageInfo{graphicscache}{Calling gs with name '\graphicscache@gscommand'^^J}
      \graphicscache@callgswithname{\graphicscache@gscommand}
    }%
  \fi
}
\newcommand{\graphicscache@dorender}{%
  \PackageInfo{graphicscache}{Rendering \graphicscache@outputhash: \graphicscache@fname\space with args: \graphicscache@graphicsargs\space (master file)}%
  \ifwindows
    \graphicscache@ShellEscape{md "\graphicscache@cachedir" 2>NUL}%
  \else
    \graphicscache@ShellEscape{mkdir -p "\graphicscache@cachedir"}%
  \fi
  \ifwindows
    \graphicscache@ShellEscape{del /q \graphicscache@cachedir\string\graphicscacheout.pdf}
    \graphicscache@ShellEscape{pdflatex
      -jobname graphicscacheout
      -interaction nonstopmode
      -output-directory "\graphicscache@cachedir"
      "\string\documentclass{standalone}
      \string\usepackage{graphicx}
      \string\usepackage[export]{adjustbox}
      \string\begin{document}\string\includegraphics[\graphicscache@graphicsargs]{\graphicscache@fname}\string\end{document}"
    }%
    \IfFileExists{\graphicscache@cachedir/graphicscacheout.pdf}{}{%
      \PackageWarning{graphicscache}{External pdflatex call failed (see above)}%
      \def\graphicscache@output{}%
    }
  \else
    \graphicscache@ShellEscape{pdflatex
      -jobname graphicscacheout
      -interaction nonstopmode
      -output-directory "\graphicscache@cachedir"
      '\string\documentclass{standalone}
      \string\usepackage{graphicx}
      \string\usepackage[export]{adjustbox}
      \string\begin{document}\string\includegraphics[\graphicscache@graphicsargs]{\graphicscache@fname}\string\end{document}'
      > /dev/null \string|\string| rm "\graphicscache@cachedir/graphicscacheout.pdf"
    }%
  \fi
  \ifgraphicscache@compress
    \PackageInfo{graphicscache}{With compression: \graphicscache@compress@mode}%
    \graphicscache@callgs
  \else
    \PackageInfo{graphicscache}{Direct}%
    \ifwindows
      \graphicscache@ShellEscape{
        copy \graphicscache@cachedir\string\graphicscacheout.pdf \graphicscache@output
      }%
    \else
      \graphicscache@ShellEscape{
        cp \graphicscache@cachedir/graphicscacheout.pdf \graphicscache@output
      }%
    \fi
  \fi
}
\LetLtxMacro\graphicscache@includegraphics\includegraphics%
\newcommand\graphicscache@native{%
  \expandafter\graphicscache@includegraphics\expandafter[\graphicscache@graphicsargs]{\graphicscache@fname}%
}
\newcommand{\graphicscache@work}{%
  \ifgraphicscache@render
    \filemodcmp{\graphicscache@fname}{\graphicscache@output}{% input is newer
      \graphicscache@dorender%
    }{% Output is newer
      \PackageInfo{graphicscache}{Already have \graphicscache@outputhash: \graphicscache@fname}%
    }%
    \filemodcmp{\graphicscache@fname}{\graphicscache@output}{% input is newer/output does not exist
      \graphicscache@native
    }{% otherwise, use the generated file!
      \graphicscache@includegraphics{\graphicscache@output}%
    }%
  \else
    \IfFileExists{\graphicscache@output}{%
      \graphicscache@includegraphics{\graphicscache@output}%
    }{%
      \PackageWarning{graphicscache}{Could not find cache file \graphicscache@output, for \graphicscache@fname, falling back to native...}{}%
      \graphicscache@native
    }%
  \fi
}
\catcode`\*=11
\newif\ifgraphicscache@exists
\newcommand{\graphicscache@getfname}[1]{%
  \ifx\detokenize\@undefined\else
    \edef\Gin@extensions{\detokenize\expandafter{\Gin@extensions}}%
  \fi
  \begingroup
  \global\graphicscache@existstrue
  \let\input@path\Ginput@path
  \ltx@ifpackagelater{graphics}{2017/06/26}{%
    \set@curr@file{#1}%
    \expandafter\filename@parse\expandafter{\@curr@file}%
    \ifx\filename@ext\Gin@gzext
      \expandafter\filename@parse\expandafter{\filename@base}%
      \ifx\filename@ext\relax
        \let\filename@ext\Gin@gzext
      \else
        \edef\Gin@ext{\Gin@ext\Gin@sepdefault\Gin@gzext}%
      \fi
    \fi
  }{%
    \filename@parse{#1}%
  }%
  \ifx\filename@ext\relax
    \@for\Gin@temp:=\Gin@extensions\do{%
      \ifx\Gin@ext\relax
        \Gin@getbase\Gin@temp
      \fi}%
  \else
    \Gin@getbase{\Gin@sepdefault\filename@ext}%
    \ltx@ifpackagelater{graphics}{2017/06/26}{%
      \ifx\Gin@ext\relax
      \let\Gin@savedbase\filename@base
      \let\Gin@savedext\filename@ext
        \edef\filename@base{\filename@base\Gin@sepdefault\filename@ext}%
        \let\filename@ext\relax
         \@for\Gin@temp:=\Gin@extensions\do{%
            \ifx\Gin@ext\relax
              \Gin@getbase\Gin@temp
            \fi}%
        \ifx\Gin@ext\relax
          \let\filename@base\Gin@savedbase
          \let\filename@ext\Gin@savedext
        \fi
      \fi
    }{}%
  \fi
  \ifx\Gin@ext\relax
    \global\graphicscache@existsfalse
  \else
    \@ifundefined{Gin@rule@\Gin@ext}%
      {\global\graphicscache@existsfalse}%
      {}%
  \fi
  \ifgraphicscache@exists
    \xdef\graphicscache@fname{\Gin@base\Gin@ext}%
  \fi
  \endgroup
}
\catcode`\*=12
\renewcommand{\includegraphics}[2][]{%
  \begingroup
  \expandarg
  \edef\graphicscache@options{\@nameuse{opt@graphicscache.sty}}%
  \pgfkeys{/graphicscache/.cd,#1}%
  \ifgraphicscache@render
    \graphicscache@getfname{#2}%
  \else
    \edef\graphicscache@fname{#2}%
    \graphicscache@existstrue
  \fi
  \ifgraphicscache@exists
    \ifgraphicscache@hashshortnames
      \edef\graphicscache@hashedname{#2}%
    \else
      \edef\graphicscache@hashedname{\graphicscache@fname}%
    \fi
    \edef\graphicscache@outputhash{\pdf@mdfivesum{\graphicscache@options\graphicscache@graphicsargs\graphicscache@hashedname}}%
    \edef\graphicscache@output{\graphicscache@cachedir/\graphicscache@outputhash.pdf}%
    \ifgraphicscache@listing
      \PackageInfo{graphicscache}{graphicscache: includegraphics\{#2\} => \graphicscache@output}%
      \immediate\write\graphicscache@listout{#2 \graphicscache@fname\space \graphicscache@output}%
    \fi
    \graphicscache@work
  \else
    \PackageError{graphicscache}{Could not find file #2}{}%
  \fi
  \endgroup
}
\newcommand{\includegraphicscache}[3][]{%
  \begingroup
  \expandarg
  \pgfkeys{/graphicscache/.cd,#2}%
  \includegraphics[#1]{#3}%
  \endgroup
}
\endinput
%%
%% End of file `graphicscache.sty'.
