%%
%% This is file `xetex.def',
%% based upon other drivers for LaTeX color and graphics support.
%%
%%  Copyright 2004, 2005, 2006, 2009  Ross Moore <ross@maths.mq.edu.au>
%%                        modified by Jonathan Kew <jonathan_kew@sil.org>
%%
%% This file is *not yet* part of the Standard LaTeX `Graphics Bundle'.
%% It may be distributed under the terms of the LaTeX Project Public
%% License, as described in lppl.txt in the base LaTeX distribution.
%% Either version 1.0 or, at your option, any later version.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Version History
%%
%%  2009/10/28  [RRM] Version v0.93  fix big-point incompatibility;
%%
%%  2006/08/10  [JK] Version v0.92  correct type-check in \G@measure@QTm;
%%              remove \G@PDF@ext, apply \lowercase instead;
%%              make \XeTeXquote have catcode 12 rather than 11;
%%              explicit space after "filename.ext" (reqd by xetex 0.995)
%%
%%  2006/07/19  [JK] Version v0.91  use \special{color ...} rather than
%%              \special{x:textcolor ...} etc, for compatibility with
%%              both xdv2pdf and xdvipdfmx drivers;
%%              added .bmp to list of recognized graphics extensions
%%
%%  2006/01/09  [JK] Version v0.9  removed support for XeTeX versions
%%              prior to 0.6 (when the \XeTeXpicfile parameters were
%%              modified), to avoid problem when \XeTeXrevision inc. letters
%%
%%  2005/10/12  Version v0.8  removed spurious space in \convertHEX@
%%               -- found by Morten Hogholm
%%
%%  2004/05/09  Version v0.7 changed support for \pagecolor, adapted
%%              from pdftex.def, to put a \special in the \shipout ;
%%              this driver should now work with  miniltx.tex ;
%%              changed some control-sequence names;
%%              implemented HSB to RGB color conversion
%%              identified the redundant (v0.5) coding more clearly.
%%
%%  2004/05/08  Version v0.6 properly implements scaling and rotations
%%              following the coding used with  pdftex.def ;
%%              implemented `viewport' and prepared for `clip';
%%              don't rely on " to have correct catcode;
%%              altered the suffix order for graphics, to be:
%%                .pdf .png .jpg .pict .tif ....
%%
%%  2004/04/29  Version v0.5 adapts to the changed names for the
%%              graphics inclusion primitives, and version strings.
%%
%%  2004/04/23  Version v0.41 works with graphics.sty as well as
%%              graphicx.sty ; \G@pdf@ext is '.pdf';
%%              similarly define and use \G@PDF@ext for '.PDF';
%%              removed some log-messages.
%%
%%  2004/04/22  Version v0.4 designed to work with XeTeX v0.4
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesFile{xetex.def}
        [2009/10/28 v0.93 LaTeX color/graphics driver for XeTeX (RRM, mod. JK)]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% make sure we can use " with correct catcode
{\catcode`\"=12
 \gdef\XeTeXquote{"}%
}

% miniltx.tex does not define \PackageWarning
% or \PackageWarningNoLine.
\@ifundefined{PackageWarning}{%
  \def\PackageWarning#1#2{%
    \begingroup
      \newlinechar=10 %
      \def\MessageBreak{%
        ^^J(#1)\@spaces\@spaces\@spaces\@spaces
      }%
      \immediate\write16{^^JPackage #1 Warning: #2\on@line.^^J}%
    \endgroup
  }%
}{}
\@ifundefined{PackageWarningNoLine}{%
 \def\PackageWarningNoLine#1#2{%
   \PackageWarning{#1}{#2\@gobble}%
 }%
}{}
\@ifundefined{on@line}{%
 \def\on@line{ on input line \the\inputlineno}%
}{}
\@ifundefined{@spaces}{%
 \def\@spaces{\space\space\space\space}%
}{}
\def\GXT@warn{\PackageWarning{xetex.def}}%
\ifx\Gin@log\@undefined
  \def\Gin@log{\message}
\fi

% Colour Support. The following models may be used.
%   * rgb  \
%   * cmyk  \
%   * gray  /-- all passed directly to the driver
%   * hsb  /
%   * RGB  ---- converted to rgb here
%   * named
%
\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{gray #2}%
  }
\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{cmyk #1 #2 #3 #4}%
  }
\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{rgb #1 #2 #3}%
  }
\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@hsb#1#2{\c@lor@@hsb#2\@@#1}
\def\c@lor@@hsb#1,#2,#3\@@#4{%
  \c@lor@arg{#1}%
  \c@lor@arg{#2}%
  \c@lor@arg{#3}%
  \edef#4{hsb #1 #2 #3}%
  }
\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{ #1}}%
  }

\def\current@color{gray 0}

%
% v0.02t: support for \pagecolor
\def\set@page@color{%
  \global\let\current@page@color\current@color
  \@ifundefined{GXTorg@shipout}{%
    \global\let\GXTorg@shipout\shipout
    \gdef\shipout{%
      \afterassignment\GXT@shipout
      \global\setbox\@cclv=%
    }%
    \gdef\GXT@shipout{%
      \ifvoid\@cclv\relax
        \aftergroup\GXT@@shipout
        \GXT@setpagecolor
      \else
        \GXTorg@shipout\vbox{%
          \GXT@setpagecolor
          \box\@cclv
        }%
      \fi
    }%
    \gdef\GXT@@shipout{%
      \GXTorg@shipout\box\@cclv\relax
    }%
    \gdef\GXT@setpagecolor{%
      \special{background \current@page@color}%
    }%
  }{}%
}

\def\set@color@stack{%
  \special{color push \current@color}%
  \aftergroup\reset@color
}
\def\set@color@nostack{%
  \special{color \current@color}%
  \aftergroup\reset@color
}
\def\reset@color@nostack{%
  \special{color \current@color}%
  \expandafter\checkfor@moreresets
}
\def\reset@color@stack{%
  \special{color pop}%
  \expandafter\checkfor@moreresets
}
\def\checkfor@moreresets{\futurelet\tmp\checkfor@moreresets@}
\def\checkfor@moreresets@{%
  \ifx\tmp\reset@color\expandafter\gobble@color\fi}
\def\gobble@color#1{\checkfor@moreresets}

\def\define@color@named#1#2{%
  \expandafter\edef\csname col@#1\endcsname{#2}}
%
\def\check@for@XeTeX@colorstack{%
  \expandafter\ifx\csname use@XeTeX@colorstack\endcsname\relax
   \let\set@color\set@color@stack
   \let\reset@color\reset@color@stack
   \let\checkfor@moreresets\relax
  \else
   \let\set@color\set@color@nostack
   \let\reset@color\reset@color@nostack
  \fi
}
% 
% use \noXeTeXcolorstack to turn off the color-stack
% mechanism; use it within the preamble only.
% This may be useful with some packages
% e.g. TeXpower which makes partial page-builds.
%
\def\noXeTeXcolorstack{\def\use@XeTeX@colorstack{}}
\AtBeginDocument{\check@for@XeTeX@colorstack}
%
% < end of color support for XeTeX >

% < graphics inclusion for XeTeX >
%
% PDF is a special case which has extra features
\def\G@pdf@ext{.pdf}

% \Gin@XeTeX@page: page number of pdf image or \@empty otherwise.
% This code adapted from  pdftex.def
\let\Gin@XeTeX@page\@empty
\begingroup
  \toks@{\define@key{Gin}{page}{\def\Gin@XeTeX@page{#1}}}%
  \@ifundefined{define@key}{%
   \edef\x{\endgroup\noexpand\AtBeginDocument{%
     \noexpand\@ifundefined{define@key}{}{\the\toks@}}%
    }\x
  }{\expandafter\endgroup\the\toks@}

% rotating graphics is supported, but 
% rotation of arbitrary material is not supported yet
%
\def\GXT@erotate#1#2{\Grot@setangle{#1}\hbox{#2}}%
\def\set@GXT@erotate{\let\Gin@erotate\GXT@erotate}
%\AtBeginDocument{\set@GXT@erotate}

\expandafter\ifx\csname XeTeXpicfile\endcsname\relax
 \let\picfile@cmd\picfile
 \let\pdffile@cmd\pdffile
\else 
 \let\picfile@cmd\XeTeXpicfile
 \let\pdffile@cmd\XeTeXpdffile
\fi 

% all graphics are handled by Apple's QuickTime
%
\def\XeTeX@include@QTm#1{%
  \def\picpage{}%
  \lowercase{\edef\type@ext{\Gin@ext}}%
  \ifx\type@ext\G@pdf@ext
   \ifx\Gin@XeTeX@page\@empty\else
     \edef\picpage{ page\Gin@XeTeX@page}\fi
   \def\picfilecommand{\pdffile@cmd}%
  \else
   \def\picfilecommand{\picfile@cmd}%
  \fi
  \edef\picfileargs{ \XeTeXquote#1\XeTeXquote\space\picpage}%
  \Gin@log{%
    <use \picfileargs>%
  }%
  \ifx\Gscale@start\relax
    \setbox\z@=\hbox{\expandafter\picfilecommand\picfileargs}%
    \edef\next{\box\z@\egroup
      \def\noexpand\width{\the\wd\z@}\relax
      \def\noexpand\height{\the\ht\z@}\bgroup}%
    \expandafter\next
  \else
    \XeTeX@scaleinclude{#1}%
  \fi
}
\def\XeTeX@scaleinclude#1{%
  \def\@tempa{!}%
  \ifx\Gin@scaley\@tempa
   \let\Gin@scaley\Gin@scalex
  \else
   \ifx\Gin@scalex\@tempa\let\Gin@scalex\Gin@scaley\fi
  \fi
  \hbox{%
    \Gscale@box{\Gin@scalex}[\Gin@scaley]{%
      \@ifundefined{Gin@vllx}{%
        \GXT@viewportfalse
        % without viewport/trim clipping does not make sense
        % for XeTeX
        \Gin@clipfalse
      }{}%
    \ifGin@clip
      \GXT@warn{No clipping support in XeTeX yet}%
      \if!\Gin@vllx\Gin@vlly\Gin@vurx\Gin@vury!%
      \else
        \let\GXT@clipend\GXT@DoClipEnd
        \setbox\@tempboxa\hbox\bgroup
      \fi
    \fi
    \ifGXT@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
%
    \expandafter\picfilecommand\picfileargs
%
    \ifGXT@viewport
      \ifdim\Gin@vlly\p@=\z@
      \else
        \egroup
      \fi
      \GXT@clipend
    \fi
    }%
  }%
}
\newif\ifGXT@viewport
\GXT@viewporttrue
\let\GXT@clipend\relax
\def\GXT@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@vllx\p@
  \wd\@tempboxa\strip@pt\dimen@ bp\relax
%  \wd\@tempboxa\dimen@ \relax
  \dimen@\Gin@ury\p@
  \advance\dimen@ -\Gin@vlly\p@
  \ht\@tempboxa\strip@pt\dimen@ bp\relax
%  \ht\@tempboxa\dimen@ \relax
  %  when clipping is implemented for XeTeX
  %  make the changes here:
  \box\@tempboxa
}


\def\Gread@QTm#1{\IfFileExists{\Gin@base.bb}%
   {\Gread@eps{\Gin@base.bb}}%
   {\G@measure@QTm{\Gin@base}{\Gin@ext}}%
}
\def\G@measure@QTm#1#2{%
 % \@warning{measuring size of QuickTime graphic #1#2}%
  \bgroup
   \lowercase{\edef\type@ext{#2}}%
   \ifx\type@ext\G@pdf@ext
    \ifx\Gin@XeTeX@page\@empty \def\picpage{}%
    \else \edef\picpage{ page\Gin@XeTeX@page}\fi
   \fi
   \setbox0=\hbox{%
     \ifx\type@ext\G@pdf@ext
      \edef\piccommand{\noexpand\pdffile@cmd
         \XeTeXquote#1#2\XeTeXquote\space\picpage}%
     \else
      \edef\piccommand{\noexpand\picfile@cmd 
         \XeTeXquote#1#2\XeTeXquote\space}%
     \fi
     \piccommand
    }\dimen0=\wd0 \dimen2=\ht0
   \edef\next{\egroup
     \edef\noexpand\Gin@urx{\the\dimen0}%
     \edef\noexpand\Gin@ury{\the\dimen2}%
     }%
  \next
  \Gin@defaultbp\Gin@llx{0}%
  \Gin@defaultbp\Gin@lly{0}%
  \expandafter\Gin@defaultbp\expandafter\Gin@urx\expandafter{\Gin@urx}%
  \expandafter\Gin@defaultbp\expandafter\Gin@ury\expandafter{\Gin@ury}%
%  \typeout{XeTeX: HiRes BoundingBox = (0,0)\space x (\Gin@urx,\Gin@ury) }%
  \Gin@nat@width=\Gin@urx\p@
  \Gin@nat@height=\Gin@ury\p@
  % these emulate  \@begin@tempboxa 
  \edef\width{\the\Gin@nat@width}%
  \edef\height{\the\Gin@nat@height}%
  \def\depth{0pt}%
  \let\totalheight\@ovri
  \totalheight\height
}

\let\Ginclude@QTm\XeTeX@include@QTm

% For a new graphics type, you can include a line here ...
\@namedef{Gin@rule@.pdf}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.gif}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.jpg}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.mac}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.pict}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.png}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.psd}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.sgi}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.tga}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.tif}#1{{QTm}{QTm}{#1}}
\@namedef{Gin@rule@.bmp}#1{{QTm}{QTm}{#1}}

% movies not supported yet:
%\@namedef{Gin@rule@.qt}#1{{QTm}{QTm}{#1}}
%\@namedef{Gin@rule@.mpg}#1{{QTm}{QTm}{#1}}
%\@namedef{Gin@rule@.mov}#1{{QTm}{QTm}{#1}}
%\@namedef{Gin@rule@.moov}#1{{QTm}{QTm}{#1}}


% ... though this default rule will try QuickTime anyway ...
\@namedef{Gin@rule@*}#1{{QTm}{QTm}{#1}}

% ... and add it's extension here
\def\Gin@extensions{.pdf,.png,.jpg,.bmp,.pict,.tif,.psd,.mac,.sga,.tga,.gif}

%
% Rotation & Scaling
%  supported from xetex v0.6 onwards
%

\def\Grot@start{\wd\z@\z@ 
  \special{x:gsave}\special{x:rotate \Grot@angle}}
\def\Grot@end{\special{x:grestore}}

\def\Gscale@start{%
  \special{x:gsave}\special{x:scale \Gscale@x\space\Gscale@y}}
\let\Gscale@end\Grot@end
%

\endinput
%%
%% End of file `xetex.def'.
