%% luatex.def Copyright (C) 2000-2015 David Carlisle, Sebastian Rahtz,
%%                                    Hans Hagen, Heiko Oberdiek and
%%                                    Martin Schr\"oder
%%            Copyright (C) 2016-2017 LaTeX3 project and Heiko Oberdiek 
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is the LaTeX3 Project.
%%
%% https://github.com/latex3/graphics-def/issues
%%
\ProvidesFile{luatex.def}
  [2017/06/17 v1.0d Graphics/color driver for luatex]
\def\c@lor@arg#1{%
  \dimen@#1\p@
  \ifdim\dimen@<\z@\dimen@\maxdimen\fi
  \ifdim\dimen@>\p@
    \PackageError{color}{Argument `#1' not in range [0,1]}\@ehd
  \fi}
\def\color@gray#1#2{%
  \c@lor@arg{#2}%
  \edef#1{#2 g #2 G}%
  }
\def\color@cmyk#1#2{\c@lor@@cmyk#2\@@#1}
\def\c@lor@@cmyk#1,#2,#3,#4\@@#5{%
  \c@lor@arg{#4}%
  \c@lor@arg{#1}%
  \c@lor@arg{#2}%
  \c@lor@arg{#3}%
  \edef#5{#1 #2 #3 #4 k #1 #2 #3 #4 K}%
  }
\def\color@rgb#1#2{\c@lor@@rgb#2\@@#1}
\def\c@lor@@rgb#1,#2,#3\@@#4{%
  \c@lor@arg{#1}%
  \c@lor@arg{#2}%
  \c@lor@arg{#3}%
  \edef#4{#1 #2 #3 rg #1 #2 #3 RG}%
  }
\def\color@RGB#1#2{\c@lor@@RGB#2\@@#1}
\def\c@lor@@RGB#1,#2,#3\@@#4{%
 \c@lor@RGB@rgb{#1}\@tempa
 \c@lor@RGB@rgb{#2}\@tempb
 \c@lor@RGB@rgb{#3}\@tempc
 \c@lor@@rgb\@tempa,\@tempb,\@tempc\@@#4%
  }
\def\c@lor@RGB@rgb#1#2{%
  \dimen@#1\p@
  \divide\dimen@\@cclv
  \edef#2{\strip@pt\dimen@}}
\def\color@named#1#2{\c@lor@@named#2,,\@@#1}
\def\c@lor@@named#1,#2,#3\@@#4{%
  \@ifundefined{col@#1}%
    {\PackageError{color}{Undefined color `#1'}\@ehd}%
  {\edef#4{\csname col@#1\endcsname}}%
  }
\chardef\main@pdfcolorstack=0 %
\@ifundefined{@pdfcolorstack}
  {\def\@pdfcolorstack{\main@pdfcolorstack}}{}
\def\current@color{0 g 0 G}
\def\set@color{%
  \pdfextension colorstack\@pdfcolorstack push{\current@color}%
  \aftergroup\reset@color}
\def\reset@color{\pdfextension colorstack\@pdfcolorstack pop\relax}
\newif\ifGPT@pagecolor
\begingroup\expandafter\expandafter\expandafter\endgroup
\def\set@page@color{%
  \global\GPT@pagecolortrue
  \global\let\current@page@color\current@color
  \@ifundefined{GPT@outputbox}{%
    % \newbox is outer in plain
    \csname newbox\endcsname\GPT@outputbox
  }{}%
  \@ifundefined{GPTorg@shipout}{%
    \global\let\GPTorg@shipout\shipout
    \gdef\shipout{%
     \edef\GPT@saved@grouplevel{\number\currentgrouplevel}%
      \afterassignment\GPT@shipout
      \global\setbox\GPT@outputbox=%
    }%
    \gdef\GPT@shipout{%
     \ifnum\GPT@saved@grouplevel=\currentgrouplevel
     \else
        \expandafter\aftergroup
      \fi
      \GPT@@shipout
    }%
    \gdef\GPT@@shipout{%
      \ifvoid\GPT@outputbox\relax
        \PackageInfo{pdftex.def}{Ignoring void output box}%
      \else
        \setbox\GPT@outputbox=\vbox{%
          \GPT@pageliteral
          \box\GPT@outputbox
        }%
        \GPTorg@shipout\box\GPT@outputbox\relax
      \fi
    }%
    \gdef\GPT@pageliteral{%
      \ifGPT@pagecolor
        \pdfextension literal page{%
          q % gsave
          \current@page@color\space
          n % newpath
          0 0 \strip@pt\pagewidth\space
          \strip@pt\pageheight\space re % rectangle
          % there is no need to convert to bp
          f % fill
          Q% grestore
        }%
      \fi
    }%
  }{}%
}
\def\define@color@named#1#2{%
  \expandafter\edef\csname col@#1\endcsname{#2}}
\def\no@page@color{%
  \global\GPT@pagecolorfalse
}
\def\Gread@pdftex#1{%
  \IfFileExists{#1}
    {\Gread@@pdftex{#1}}%
    {%
      \def\Gin@llx{0}%
      \let\Gin@lly\Gin@llx
      \def\Gin@urx{72}%
      \let\Gin@ury\Gin@urx
      \ifGin@draft
      \else
        \PackageError{luatex.def}{File `#1' not found: using draft setting}%
        \@ehc
        \Gin@drafttrue
      \fi
    }%
}
\let\Gin@attr@hash\@empty
\@ifundefined{Gin@log}
  {\let\Gin@log\wlog}
  {}
\@ifundefined{Gin@page}
  {\let\Gin@page\@empty}
  {}
\@ifundefined{Gin@pagebox}
  {\def\Gin@pagebox{cropbox}}
  {}
\@ifundefined{Gin@interpolate}
  {\csname newif\endcsname\ifGin@interpolate}
  {}
\def\Gread@@pdftex#1{%
  \edef\Gin@attr@hash{%
    \ifx\Gin@pagebox\@empty
    \else
      :\Gin@pagebox
    \fi
    \ifx\Gin@page\@empty
    \else
      :P\Gin@page
    \fi
    \ifGin@interpolate
      :I%
    \fi
  }%
  \@ifundefined{#1 image\Gin@attr@hash}%
    {%
      \saveimageresource
        \ifGin@interpolate attr{/Interpolate true}\fi
        \ifx\Gin@page\@empty\else page \Gin@page\fi
        \Gin@pagebox
        {#1}%
      \setbox\@tempboxa=\hbox{\useimageresource\lastsavedimageresourceindex}%
      \def\Gin@llx{0}\let\Gin@lly\Gin@llx
      \Gin@defaultbp\Gin@urx{\wd\@tempboxa}%
      \Gin@defaultbp\Gin@ury{\ht\@tempboxa}%
      \expandafter\xdef\csname #1 image\Gin@attr@hash\endcsname
        {\useimageresource\the\lastsavedimageresourceindex}%
      \expandafter\xdef\csname #1 height\Gin@attr@hash\endcsname
        {\the\ht\@tempboxa}%
      \expandafter\xdef\csname #1 width\Gin@attr@hash\endcsname
        {\the\wd\@tempboxa}%
      \Gin@log{%
        <#1, %
        id=\the\lastsavedimageresourceindex, %
        \ifx\Gin@page\@empty\else page=\Gin@page , \fi
        \ifx\Gin@pagebox\@empty\else pagebox=\Gin@pagebox , \fi
        \ifGin@interpolate interpolate=true, \fi
        \the\wd\@tempboxa\space x \the\ht\@tempboxa
        >%
      }%
    }{%
      \def\Gin@llx{0}\let\Gin@lly\Gin@llx
      \Gin@defaultbp\Gin@urx{\csname #1 width\Gin@attr@hash\endcsname}%
      \Gin@defaultbp\Gin@ury{\csname #1 height\Gin@attr@hash\endcsname}%
    }%
}
\def\Gread@png#1{%
  \let\Gin@page\@empty
  \Gread@pdftex{#1}%
}
\let\Gread@jpg\Gread@png
\expandafter\let\csname Gread@jbig2\endcsname\Gread@png
\def\Gread@pdf#1{%
  \Gin@interpolatefalse
  \Gread@pdftex{#1}%
}
\let\Gread@mps\Gread@eps
\def\GPT@setup@scale{%
  \def\@tempa{!}%
  \ifx\Gin@scaley\@tempa
    \let\Gin@scaley\Gin@scalex
  \else
    \ifx\Gin@scalex\@tempa\let\Gin@scalex\Gin@scaley\fi
  \fi
}
\def\Ginclude@pdftex#1{%
  \GPT@setup@scale
  \Gin@log{%
    <use #1%
    \ifx\Gin@page\@empty\else, page \Gin@page\fi
    >%
  }%
  \PackageInfo{luatex.def}{%
    #1 \ifx\Gin@page\@empty\else, page\Gin@page\fi
    \space used\ifx\on@line\@undefined\else\on@line\fi.}%
  \PackageInfo{luatex.def}{%
    Requested size: %
    \the\Gin@req@width\space x \the\Gin@req@height
  }%
  \Ginclude@@pdftex{\@nameuse{#1 image\Gin@attr@hash}}%
}
\def\Ginclude@mps#1{%
  \GPT@setup@scale
  \Ginclude@@pdftex{\convertMPtoPDF{#1}{1}{1}}%
}
\def\Ginclude@@pdftex#1{%
  \hbox{%
    \Gscale@box{\Gin@scalex}[\Gin@scaley]{%
      \@ifundefined{Gin@vllx}{%
        \GPT@viewportfalse
        % without viewport/trim clipping does not make sense
        % for pdfTeX
        \Gin@clipfalse
      }{}%
      \ifGin@clip
        \if!\Gin@vllx\Gin@vlly\Gin@vurx\Gin@vury!%
        \else
          \let\GPT@clipend\GPT@DoClipEnd
          \setbox\@tempboxa\hbox\bgroup
        \fi
      \fi
      \ifGPT@viewport
        \ifdim\Gin@vlly\p@=\z@
        \else
          \lower\Gin@vlly bp\hbox\bgroup
        \fi
        \ifdim\Gin@vllx\p@=\z@
        \else
          \hskip-\Gin@vllx bp\relax
        \fi
      \fi
      #1%
      \ifGPT@viewport
        \ifdim\Gin@vlly\p@=\z@
        \else
          \egroup
        \fi
        \GPT@clipend
      \fi
    }%
  }%
}
\newif\ifGPT@viewport
\GPT@viewporttrue
\let\GPT@clipend\relax
\def\GPT@DoClipEnd{%
  \egroup
  \dp\@tempboxa\z@
  % \Gin@urx and \Gin@ury already contain the correct values,
  % so both cases viewport and trim can be handled together:
  \dimen@\Gin@urx\p@
  \advance\dimen@ -\Gin@llx\p@
  \wd\@tempboxa\strip@pt\dimen@ bp\relax
  \dimen@\Gin@ury\p@
  \advance\dimen@ -\Gin@lly\p@
  \ht\@tempboxa\strip@pt\dimen@ bp\relax
  \saveboxresource\@tempboxa
  \useboxresource\lastsavedboxresourceindex
}
\let\Ginclude@png\Ginclude@pdftex
\let\Ginclude@jpg\Ginclude@pdftex
\let\Ginclude@pdf\Ginclude@pdftex
\expandafter\let\csname Ginclude@jbig2\endcsname\Ginclude@pdftex
\def\GPT@MatrixIdentity{1 0 0 1}
\def\GPT@Zero{0}
\def\GPT@Minus{-}
\def\GPT@NormalizeNumber#1{%
  \edef#1{#1}%
  \edef#1{\expandafter\zap@space#1 \@empty}%
  \edef#1{\expandafter\GPT@ZapPlus#1+\@nil}%
  \edef#1{\expandafter\GPT@ZapMinusMinus#1--\@nil}%
  \expandafter\GPT@Split#1..\@nil
  \ifx\GPT@frac\@empty
  \else
    \edef\GPT@frac{%
      \expandafter\GPT@Reverse\expandafter{\expandafter}\GPT@frac\@nil
    }%
    \edef\GPT@frac{%
      \expandafter\GPT@ZapLeadingZeros\GPT@frac\@empty
    }%
    \ifx\GPT@frac\@empty
    \else
      \edef\GPT@frac{%
        \expandafter\GPT@Reverse\expandafter{\expandafter}\GPT@frac\@nil
      }%
    \fi
  \fi
  \edef\GPT@sign{\expandafter\@car\GPT@int\@empty\@nil}%
  \ifx\GPT@sign\GPT@Minus
    \edef\GPT@int{\expandafter\@cdr\GPT@int\@nil}%
  \else
    \def\GPT@sign{}%
  \fi
  \edef\GPT@int{%
    \expandafter\GPT@ZapLeadingZeros\GPT@int\@empty
  }%
  \edef\GPT@temp{\GPT@int\GPT@frac}%
  \ifx\GPT@temp\@empty
    \def#1{0}%
  \else
    \edef#1{%
      \GPT@sign
      \GPT@int
      \ifx\GPT@frac\@empty
      \else
        .\GPT@frac
      \fi
    }%
  \fi
}
\def\GPT@ZapPlus#1+#2\@nil{%
  #1%
  \ifx\@empty#2\@empty
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {%
    \GPT@ZapPlus#2\@nil
  }%
}
\def\GPT@ZapMinusMinus#1--#2\@nil{%
  #1%
  \ifx\@empty#2\@empty
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {%
    \GPT@ZapMinusMinus#2\@nil
  }%
}
\def\GPT@Split#1.#2.#3\@nil{%
  \def\GPT@int{#1}%
  \ifx\@empty#2\@empty
    \let\GPT@frac\@empty
  \else
    \def\GPT@frac{#2}%
  \fi
}
\def\GPT@Reverse#1#2#3\@nil{%
  \ifx\@empty#3\@empty
    #2#1%
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {%
    \GPT@Reverse{#2#1}#3\@nil
  }%
}
\def\GPT@ZapLeadingZeros#1{%
  \ifx0#1%
    \expandafter\GPT@ZapLeadingZeros
  \else
    #1%
  \fi
}
\def\Grot@start{%
  \GPT@NormalizeNumber\Grot@sin
  \GPT@NormalizeNumber\Grot@cos
  \edef\GPT@temp{%
    \Grot@cos\space\Grot@sin\space
    \if-\Grot@sin
    \else
      \ifx\Grot@sin\GPT@Zero
        \GPT@Zero
      \else
        -\Grot@sin
      \fi
    \fi
    \space\Grot@cos
  }%
  \ifx\GPT@temp\GPT@MatrixIdentity
    \def\Grot@end{}%
  \else
    \pdfextension save\relax
    \pdfextension setmatrix{\GPT@temp}%
    % Slightly hacky, but set width of box 0 to 0pt otherwise
    % the CTM gets restored in the wrong place
    \wd\z@\z@
  \fi
}
\def\Grot@end{\pdfextension restore\relax}
\def\Gscale@start{%
  \GPT@NormalizeNumber\Gscale@x
  \GPT@NormalizeNumber\Gscale@y
  \edef\GPT@temp{%
    \Gscale@x\space0 0 \Gscale@y
  }%
  \ifx\GPT@temp\GPT@MatrixIdentity
    \def\Gscale@end{}%
  \else
    \pdfextension save\relax
    \pdfextension setmatrix{\GPT@temp}%
  \fi
}
\def\Gscale@end{\pdfextension restore\relax}
\@ifundefined{ifGin@setpagesize}
  {\expandafter\let\csname ifGin@setpagesize\expandafter\endcsname
                    \csname iftrue\endcsname}
  {}
\ifGin@setpagesize
\ifx\paperwidth\@undefined\else
  \AtBeginDocument{%
% If a package has changed \mag, assume it knows what it is doing
% and leave page size alone
\ifnum\mag=\@m
  \@ifundefined{stockwidth}{%
  \@ifundefined{paperwidth}{%
  }{%
    \ifdim\paperwidth>0pt\relax
      \ifdim\paperheight>0pt\relax
        \pagewidth=\paperwidth
        \pageheight=\paperheight
      \fi
    \fi
  }%
}{%
  \ifdim\stockwidth>0pt\relax
    \ifdim\stockheight>0pt\relax
      \pagewidth=\stockwidth
      \pageheight=\stockheight
    \fi
  \fi
}%
\fi
}
\fi
\fi
\def\Gin@extensions{%
  .pdf,.png,.jpg,.mps,.jpeg,.jbig2,.jb2,%
  .PDF,.PNG,.JPG,.JPEG,.JBIG2,.JB2
}
\@namedef{Gin@rule@.jpg}#1{{jpg}{.jpg}{#1}}
\@namedef{Gin@rule@.jpeg}#1{{jpg}{.jpeg}{#1}}
\@namedef{Gin@rule@.JPG}#1{{jpg}{.JPG}{#1}}
\@namedef{Gin@rule@.JPEG}#1{{jpg}{.JPEG}{#1}}
\@namedef{Gin@rule@.jb2}#1{{jbig2}{.jb2}{#1}}%
\@namedef{Gin@rule@.jbig2}#1{{jbig2}{.jbig2}{#1}}%
\@namedef{Gin@rule@.png}#1{{png}{.png}{#1}}
\@namedef{Gin@rule@.PNG}#1{{png}{.PNG}{#1}}
\@namedef{Gin@rule@.mps}#1{{mps}{.mps}{#1}}
\@namedef{Gin@rule@.pdf}#1{{pdf}{.pdf}{#1}}
\@namedef{Gin@rule@.PDF}#1{{pdf}{.PDF}{#1}}
\@namedef{Gin@rule@.eps}#1{{eps}{.eps}{#1}}
% MPS conversion
\def\GPT@LoadSuppPdf{%
  \@ifundefined{convertMPtoPDF}{%
    \edef\@tempa{%
      \catcode`\noexpand\<\the\catcode`\<\relax
      \catcode`\noexpand\>\the\catcode`\>\relax
      }%
    \@makeother\<\@makeother\<%
    \InputIfFileExists{supp-pdf.mkii}{}{}%
    \@tempa
  }{%
    % supp-pdf is probably already loaded.
  }%
  % If supp-pdf is not available, \convertMPtoPDF generates
  % an error message. \providecommand inside the third argument
  % of \InputIfFileExists will not work:
  % * \InputIfFileExists has problems with parameters (#1).
  % * Implementation of \providecommand is insufficient in miniltx.tex
  \@ifundefined{convertMPtoPDF}{%
% 2016/05/21 v0.01c use \newcommand here not \newcommand*  graphics/4472
\newcommand{\convertMPtoPDF}[3]{%
      \PackageError{luatex.def}{%
        Cannot convert ##1 from MPS to PDF.
        The support file `supp-pdf.mkii' (supp-pdf.tex) is missing%
      }{%
        The graphics driver for pdfTeX needs `supp-pdf.mkii'
        (or `supp-pdf.tex' and/or `supp-mis.tex')
        to convert MetaPost output files to PDF.
        These files are part of ConTeXt and can also be found here:%
        \space\space CTAN:macros/pdftex/graphics/.
        You'll get more errors if you'll continue now.%
      }%
    }%
  }{}%
  \let\GPT@LoadSuppPdf\relax
}
\AtBeginDocument{%
  \GPT@LoadSuppPdf
}
% Load package epstopdf if
% a) LaTeX is running (plain TeX users are on their own),
% b) and shell escape is enabled (possibly restricted),
% c) and \DoNotLoadEpstopdf either does not exist or is not \relax,
% d) and neither pst-pdf nor pdftricks is loaded (by \begin{document}).
% 
% In other words, by default .eps files will be automatically
% converted to .pdf files when outputting pdf.  This can be wrong!
% If the .pdf is the source, rather than the .eps, you should put
%  \newcommand{\DoNotLoadEpstopdf}{}
% before even the \documentclass line of your document.
% 
% More information about the epstopdf run will be in the .log file;
% see http://ctan.org/pkg/epstopdf-pkg for more info about the
% epstopdf package itself.  In turn, it calls the epstopdf script
% (http://ctan.org/pkg/epstopdf).
% 
% For the first discussion and background on this, see the thread on latex-l:
% http://thread.gmane.org/gmane.comp.tex.latex.latex3/1465
%
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname DoNotLoadEpstopdf\endcsname\relax
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname AtEndOfPackage\endcsname\relax
  \else
    \AtEndOfPackage{%
      \AtBeginDocument{%
        \ifx\includegraphics\@undefined\else
        \@ifpackageloaded{pst-pdf}{}{%
          \@ifpackageloaded{pdftricks}{}{%
            \@ifpackageloaded{graphics}{%
              \ifnum\directlua{tex.sprint(status.shell_escape)}>0 %
          % only add .eps to file extension list if epstopdf is loaded
                \edef\Gin@extensions{\Gin@extensions,.eps}%
                \RequirePackage{epstopdf-base}[2009/07/12]%
              \fi}{}
          }% pdftricks not loaded
        }% pst-pdf not loaded
        \fi
      }% AtBeginDocument block
    }% AtEndofPackage block
  \fi % AtEndofPackage not defined
\fi % DoNotLoadEpstopdf not defined
\endinput
