% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS $Header: /cvsroot/pgf/pgf/generic/pgf/systemlayer/pgfsys-dvipdfmx.def,v 1.3 2009/05/22 07:25:48 jinhwancho Exp $

% Driver commands for dvipdfmx
%
% Modified by Jin-Hwan Cho <chofchof@ktug.or.kr> on April 18, 2009.
% This driver requires the version DVIPDFMX-20080605 that is contained
% in texlive 2008 or later.

%
% Load common pdf and pdf in dvi commands:
%
\input pgfsys-common-pdf.def

% For unique object name:
\newcount\pgfsys@objnum\pgfsys@objnum\@ne

% `pdf:code' is an abbreviation of `pdf:literal direct', the idea of which
% came from pdfTeX's primitive \pdfliteral direct.
\def\pgfsys@invoke#1{\special{pdf:code #1}}

% If color is changed by \pgfsys@invoke explicitly, the color
% optimization routine in dvipdfmx does not work correctly
% because the routine checks color specials only. For example,
%
% \special{color push 0}\pgfsys@invoke{1 g 1 G}\special{color push 0}
%
% the last color special will be ignored the optimization routine
% thinks that the color has changed to the same color.
% To avoid this problem, we need to change the color temporarily to another.
%
% We assume that black is the default color when no color is specified.
\def\start@force@reset@color{%
  \special{pdf:bcolor [1]}%
  \special{pdf:bcolor [0]}%
}
\def\stop@force@reset@color{%
  \special{pdf:ecolor}%
  \special{pdf:ecolor}%
}

% We cannot use many codes in pgfsys-common-pdf-via-dvi.def.
% So it is rewritten here (with some bugs fixed).
\def\pgfsys@hboxsynced#1{%
  {%
    \pgfsys@beginscope%
    \setbox\pgf@hbox=\hbox{%
      \hskip\pgf@pt@x%
      \raise\pgf@pt@y\hbox{%
        \pgf@pt@x=0pt%
        \pgf@pt@y=0pt%
        % The order of `pdf:bcontent' is important here. It should be
        % followed by \pgflowlevelsynccm, not reverse order.
        \special{pdf:bcontent}%
        \pgflowlevelsynccm% 
        \start@force@reset@color%
        \pgfsys@hbox#1%
        \stop@force@reset@color%
        \special{pdf:econtent}}%
      \hss%
    }%
    \wd\pgf@hbox=0pt%
    \ht\pgf@hbox=0pt%
    \dp\pgf@hbox=0pt%
    \box\pgf@hbox%
    \pgfsys@endscope%
  }%
}

% All text materials between `pdf:bcontent' and `pdf:econtent' are
% treated in the relative coordinate.
\def\pgfsys@beginpicture{\special{pdf:bcontent}}
\def\pgfsys@endpicture{\special{pdf:econtent}}

\def\pgfsys@imagesuffixlist{.pdf:.jpg:.jpeg:.png:.bmp:}

% Shaidng came from pgfsys-pdftex.def
\def\pgfsys@horishading#1#2#3{%
  {%
    \pgf@parsefunc{#3}%
    \pgfmathparse{#2}%
    \pgf@process{\pgfpoint{\pgf@max}{#2}}%
    \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
      \special{pdf:bxobj @pgfshade\the\pgfsys@objnum\space width \the\pgf@max\space height \pgfmathresult pt}%
      \special{pdf:put @resources
      <<
        /Shading << /Sh << /ShadingType 2
        /ColorSpace /DeviceRGB
        /Domain [\pgf@pdfparseddomain]
        /Coords [\pgf@doma\space0 \pgf@domb\space0]
        /Function \pgf@pdfparsedfunction
        /Extend [false false] >> >>
      >>}%
      \pgfsys@invoke{/Sh sh}%
      \special{pdf:exobj}}}\@tempa% <<
    \expandafter\xdef\csname @pgfshading#1!\endcsname{%
      \hbox to\the\pgf@max{\vbox to\pgfmathresult pt{\vfil\special{pdf:uxobj @pgfshade\the\pgfsys@objnum}}\hfil}}%
  }%
  \global\advance\pgfsys@objnum\@ne%
}
\def\pgfsys@vertshading#1#2#3{%
  {%
    \pgf@parsefunc{#3}%
    \pgfmathparse{#2}%
    \pgf@process{\pgfpoint{\pgf@max}{#2}}%
    \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
      \special{pdf:bxobj @pgfshade\the\pgfsys@objnum\space width \pgfmathresult pt\space height \the\pgf@max}%
      \special{pdf:put @resources
      <<
        /Shading << /Sh << /ShadingType 2
        /ColorSpace /DeviceRGB
        /Domain [\pgf@pdfparseddomain]
        /Coords [0 \pgf@doma\space0 \pgf@domb]
        /Function \pgf@pdfparsedfunction
        /Extend [false false] >> >>
      >>}%
      \pgfsys@invoke{/Sh sh}%
      \special{pdf:exobj}}}\@tempa% <<
    \expandafter\xdef\csname @pgfshading#1!\endcsname{%
      \hbox to\pgfmathresult pt{\vbox to\the\pgf@max{\vfil\special{pdf:uxobj @pgfshade\the\pgfsys@objnum}}\hfil}}% 
  }%
  \global\advance\pgfsys@objnum\@ne%
}
\def\pgfsys@radialshading#1#2#3{%
  {%
    \pgf@parsefunc{#3}%
    \pgf@process{#2}%
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \pgf@process{\pgfpoint{\pgf@max}{\pgf@max}}%
    \advance\pgf@xa by \pgf@x%
    \advance\pgf@ya by \pgf@y%
    \pgf@sys@bp@correct{\pgf@x}%
    \pgf@sys@bp@correct{\pgf@y}%
    \pgf@sys@bp@correct{\pgf@xa}%
    \pgf@sys@bp@correct{\pgf@ya}%
    \pgfutil@tempdima=2\pgf@max%
    \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
      \special{pdf:bxobj @pgfshade\the\pgfsys@objnum\space width \the\pgfutil@tempdima\space height \the\pgfutil@tempdima}%
      \special{pdf:put @resources
      <<
        /Shading << /Sh << /ShadingType 3
        /ColorSpace /DeviceRGB
        /Domain [\pgf@pdfparseddomain]
        /Coords [\pgf@sys@tonumber{\pgf@xa} \pgf@sys@tonumber{\pgf@ya} \pgf@doma\space \pgf@sys@tonumber{\pgf@x} \pgf@sys@tonumber{\pgf@y} \pgf@domb]
        /Function \pgf@pdfparsedfunction
        /Extend [true false] >> >>
      >>}%
      \pgfsys@invoke{/Sh sh}%
      \special{pdf:exobj}}}\@tempa% <<
    \expandafter\xdef\csname @pgfshading#1!\endcsname{%
      \hbox to\the\pgfutil@tempdima{\vbox to\the\pgfutil@tempdima{\vfil\special{pdf:uxobj @pgfshade\the\pgfsys@objnum}}\hfil}}% 
  }%
  \global\advance\pgfsys@objnum\@ne%
}
\def\pgfsys@functionalshading#1#2#3#4{%
  {%
    \pgf@process{#2}%
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \pgf@process{#3}%
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    \advance\pgf@x by-\pgf@xa%
    \advance\pgf@y by-\pgf@ya%
    \pgf@sys@bp@correct{\pgf@xa}%
    \pgf@sys@bp@correct{\pgf@ya}%
    \pgf@sys@bp@correct{\pgf@xb}%
    \pgf@sys@bp@correct{\pgf@yb}%
    \pgf@xc=-\pgf@xa%
    \pgf@yc=-\pgf@ya%
    % Now build the function
    \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
      \special{pdf:stream @pgfstream\the\pgfsys@objnum\space({#4}) <</FunctionType 4
        /Domain [\pgf@sys@tonumber{\pgf@xa}\space\pgf@sys@tonumber{\pgf@xb}\space\pgf@sys@tonumber{\pgf@ya}\space\pgf@sys@tonumber{\pgf@yb}]
        /Range [0 1 0 1 0 1]>>}}}\@tempa%
    \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
      \special{pdf:bxobj @pgfshade\the\pgfsys@objnum\space width \the\pgf@x\space height \the\pgf@y}%
      \special{pdf:put @resources <</Shading <</Sh <</ShadingType 1
        /ColorSpace /DeviceRGB
        /Matrix [1 0 0 1 \pgf@sys@tonumber{\pgf@xc}\space\pgf@sys@tonumber{\pgf@yc}]
        /Domain [\pgf@sys@tonumber{\pgf@xa}\space\pgf@sys@tonumber{\pgf@xb}\space\pgf@sys@tonumber{\pgf@ya}\space\pgf@sys@tonumber{\pgf@yb}]
        /Function @pgfstream\the\pgfsys@objnum>> >> >>}%
      \pgfsys@invoke{/Sh sh}%
      \special{pdf:exobj}}}\@tempa% <<
    \expandafter\xdef\csname @pgfshading#1!\endcsname{%
      \leavevmode\hbox to\the\pgf@x{\vbox to\the\pgf@y{\vfil\special{pdf:uxobj @pgfshade\the\pgfsys@objnum}}\hfil}}% 
  }%
  \global\advance\pgfsys@objnum\@ne%
}

% \pgf@sys@pdf@dvi@tracker was not touched!
\def\pgf@sys@pdf@dvi@tracker{
  \def\pgfsys@markposition##1{%
    \pdfsavepos%
    \edef\pgf@temp{##1}%
    \expandafter\write\expandafter\pgfutil@auxout\expandafter{%
      \expandafter\noexpand\expandafter\pgfsyspdfmark\expandafter{\pgf@temp}{\the\pdflastxpos}{\the\pdflastypos}}%
  }
  \def\pgfsyspdfmark##1##2##3{%
    \expandafter\gdef\csname pgf@sys@pdf@mark@pos@##1\endcsname{\pgfqpoint{##2sp}{##3sp}}%
  }
  \def\pgfsys@getposition##1##2{%
    \edef\pgf@marshal{\let\noexpand##2=\expandafter\noexpand\csname pgf@sys@pdf@mark@pos@##1\endcsname}%
    \pgf@marshal%
  }
  \def\pgf@sys@pdf@mark@pos@pgfpageorigin{\pgfpointorigin}
}

% Resource management: bugs were fixed.
\newif\ifpgf@sys@pdf@colorspaces@exists
\newif\ifpgf@sys@pdf@patterns@exists
\newif\ifpgf@sys@pdf@extgs@exists
\newif\ifpgf@sys@pdf@colorspaces@created
\newif\ifpgf@sys@pdf@patterns@created
\newif\ifpgf@sys@pdf@extgs@created
\newif\ifpgf@sys@pdf@any@resources

\def\pgf@sys@setuppdfresources@plain{%
  % This command will setup pdf resource in plain and in latex
  % mode. ConTeXt has its own handler
  \pgfutil@insertatbegineverypage{%
    \pgf@sys@pdf@any@resourcesfalse%
    \ifpgf@sys@pdf@extgs@exists%
      \pgf@sys@pdf@any@resourcestrue%
      \ifpgf@sys@pdf@extgs@created\else\special{pdf:obj @pgfextgs <<>>}\global\pgf@sys@pdf@extgs@createdtrue\fi% <<
    \fi%
    \ifpgf@sys@pdf@colorspaces@exists%
      \pgf@sys@pdf@any@resourcestrue%
      \ifpgf@sys@pdf@colorspaces@created\else\special{pdf:obj @pgfcolorspaces <<>>}\global\pgf@sys@pdf@colorspaces@createdtrue\fi% <<
    \fi%
    \ifpgf@sys@pdf@patterns@exists%
      \pgf@sys@pdf@any@resourcestrue%
      \ifpgf@sys@pdf@patterns@created\else\special{pdf:obj @pgfpatterns <<>>}\global\pgf@sys@pdf@patterns@createdtrue\fi% <<
    \fi%
    \ifpgf@sys@pdf@any@resources%
      \special{pdf:put @resources
      <<
        \ifpgf@sys@pdf@extgs@exists /ExtGState @pgfextgs \fi
        \ifpgf@sys@pdf@patterns@exists /Pattern @pgfpatterns \fi
        \ifpgf@sys@pdf@colorspaces@exists /ColorSpace @pgfcolorspaces \fi
      >>}% <<
    \fi%
  }
  \def\pgf@sys@addpdfresource@extgs@plain##1{%
    \global\pgf@sys@pdf@extgs@existstrue%
    \pgfutil@insertatbegincurrentpage{\special{pdf:put @pgfextgs << ##1 >>}}% <<
  }
  \def\pgf@sys@addpdfresource@colorspaces@plain##1{%
    \global\pgf@sys@pdf@colorspaces@existstrue%
    \pgfutil@insertatbegincurrentpage{\special{pdf:put @pgfcolorspaces << ##1 >>}}% <<
  }
  \def\pgf@sys@addpdfresource@patterns@plain##1{%
    \global\pgf@sys@pdf@patterns@existstrue%
    \pgfutil@insertatbegincurrentpage{\special{pdf:put @pgfpatterns << ##1 >>}}% <<
  }
}
\pgfutil@setuppdfresources % possibly call the above

% As pgfsys-pdftex.def, the following code is required
% to give colors on pattern objects.
\pgfutil@addpdfresource@colorspaces{ /pgfprgb [/Pattern /DeviceRGB] }

% Image Mask came from pgfsys-pdftex.def
% generic/pgf/basiclayer/pgfcoreimage.code.tex must be modified!
\def\pgfsys@defineimage{% width, height, page number
  \ifx\includegraphics\relax
    \ifx\pgf@imagewidth\pgfutil@empty\edef\pgf@imagewidth{1bp}\fi% width?
    \ifx\pgf@imageheight\pgfutil@empty\edef\pgf@imageheight{1bp}\fi% height?
    \ifx\pgf@imagepage\pgfutil@empty\else\edef\pgf@imagepage{ page \pgf@imagepage}\fi%
    \ifx\pgf@imageinterpolate\pgfutil@empty\else\edef\pgf@imageinterpolate{ /Interpolate\space\pgf@imageinterpolate}\fi%
    \ifx\pgf@imagemask\pgfutil@empty\else\xdef\pgf@imagemask{ /SMask @\pgf@imagemask}\fi%
    \edef\pgf@image{\noexpand\hbox to \pgf@imagewidth{\vbox to \pgf@imageheight{\vfil\special{pdf:image width \pgf@imagewidth\space height \pgf@imageheight\space\pgf@imagepage\space(\pgf@filename) <<\pgf@imageinterpolate\pgf@imagemask\space>>}}\hfil}}%
  \else
    \ifx\pgf@imagewidth\pgfutil@empty\else\edef\pgf@imagewidth{width=\pgf@imagewidth,}\fi%
    \ifx\pgf@imageheight\pgfutil@empty\else\edef\pgf@imageheight{height=\pgf@imageheight,}\fi%
    \ifx\pgf@imagepage\pgfutil@empty\else\edef\pgf@imagepage{page=\pgf@imagepage,}\fi%
    \ifx\pgf@imageinterpolate\pgfutil@empty\else\edef\pgf@imageinterpolate{interpolate=\pgf@imageinterpolate,}\fi%
    \ifx\pgf@imagemask\pgfutil@empty\else\xdef\pgf@imagemask{mask=\pgf@imagemask,}\fi%
    \edef\pgf@image{\noexpand\includegraphics[\pgf@imageheight\pgf@imagewidth\pgf@imagepage\pgf@imageinterpolate\pgf@imagemask]{\pgf@filename}}%
  \fi%
}
\def\pgfsys@definemask#1{%
  \ifx\pgf@maskmatte\pgfutil@empty%
  \else%
    \edef\pgf@maskmatte{/Matte [\pgf@maskmatte]}%
  \fi%
  \edef\pgf@attr{\pgf@maskmatte\space /Decode [1 0]}%
  \special{pdf:image @pgfmask\the\pgfsys@objnum\space hide (\pgf@filename) << \pgf@attr >>}%
  \edef\pgf@mask{pgfmask\the\pgfsys@objnum}%
  \global\advance\pgfsys@objnum\@ne%
}

% Patterns came from pgfsys-pdftex.def
% dvipdfmx provides a new special `pdf:stream' for a stream object.
\def\pgfsys@declarepattern#1#2#3#4#5#6#7#8#9{%
  % Start building the pattern dictionary:
  \pgf@xa=#2\relax%
  \pgf@ya=#3\relax%
  \pgf@xb=#4\relax%
  \pgf@yb=#5\relax%
  \pgf@xc=#6\relax%
  \pgf@yc=#7\relax%
  \pgf@sys@bp@correct\pgf@xa%
  \pgf@sys@bp@correct\pgf@ya%
  \pgf@sys@bp@correct\pgf@xb%
  \pgf@sys@bp@correct\pgf@yb%
  \pgf@sys@bp@correct\pgf@xc%
  \pgf@sys@bp@correct\pgf@yc%
  % Now create the pattern object:
  \special{pdf:stream @pgf@patternobject@#1 (#8)
    << /Type /Pattern
       /PatternType 1
       /PaintType \ifnum#9=0 2 \else 1 \fi
       /TilingType 1
       /BBox [\pgf@sys@tonumber\pgf@xa\space\pgf@sys@tonumber\pgf@ya\space\pgf@sys@tonumber\pgf@xb\space\pgf@sys@tonumber\pgf@yb]
       /XStep \pgf@sys@tonumber\pgf@xc\space
       /YStep \pgf@sys@tonumber\pgf@yc\space
       /Resources << >> %<<
    >>}%
  \pgfutil@addpdfresource@patterns{/pgfpat#1\space @pgf@patternobject@#1}%
}
\def\pgfsys@setpatternuncolored#1#2#3#4{%
  \pgfsysprotocol@literal{/pgfprgb cs #2 #3 #4 /pgfpat#1\space scn}%
}
\def\pgfsys@setpatterncolored#1{%
  \pgfsysprotocol@literal{/Pattern cs /pgfpat#1\space scn}%
}

% Opacity masks (fadings) came from pgfsys-pdftex.def
%
% One problem was that the second argument of \pgfsys@fadingfrombox
% is a box so that \pgfutil@insertatbegincurrentpage cannot be used.
% So, the new command \pgfutil@insertatbegincurrentpagefrombox is written
% in generic/pgf/utilities/pgfutil-[context,latex,plain].def.
\def\pgfsys@fadingfrombox#1#2{%
  \expandafter\ifx\csname pgfsmaskxform@#1\endcsname\relax%
    {%
      \pgf@x=-.5\wd#2%
      \pgf@y=-.5\ht#2%
      \advance\pgf@y by.5\dp#2%
      \expandafter\xdef\csname pgfsmasktrans@#1\endcsname{%
        \noexpand\pgftransformcm{1}{0}{0}{1}{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
      \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
        \special{pdf:bxobj @pgfsmask\the\pgfsys@objnum\space%
          width \the\wd#2\space height \the\ht#2\space depth \the\dp#2}%
        \wd#2=0pt%
        \ht#2=0pt%
        \dp#2=0pt%
        \box#2%
        \special{pdf:exobj <</Group <</S/Transparency /CS/DeviceGray>> >>}}}\@tempa%
      \expandafter\xdef\csname pgfsmaskxform@#1\endcsname{\the\pgfsys@objnum}%
    }%
    \global\advance\pgfsys@objnum\@ne%
  \fi%
}
\def\pgfsys@usefading#1#2#3#4#5#6#7{%
  \expandafter\ifx\csname pgfsmaskxform@#1\endcsname\relax%
    \PackageError{pgf}{Undefined fading '#1'}{}%
  \else%
    {%
      \expandafter\ifx\csname pgfsmask@#1\endcsname\relax%
        \pgf@sys@pdf@install@mask{#1}%
      \fi%
      \pgftransformreset%
      \pgftransformcm{#2}{#3}{#4}{#5}{\pgfpoint{#6}{#7}}%
      \csname pgfsmasktrans@#1\endcsname%
      {%
        \pgflowlevelsynccm%
        \pgfsys@invoke{/pgfsmask\csname pgfsmaskxform@#1\endcsname\space gs}%
      }%
      \pgftransforminvert%
      \pgflowlevelsynccm%
    }%
  \fi
}
\def\pgf@sys@pdf@install@mask#1{%
  \pgfutil@addpdfresource@extgs{%
    /pgfsmask\csname pgfsmaskxform@#1\endcsname\space <<
      /SMask <<
        /S /Luminosity
        /G @pgfsmask\csname pgfsmaskxform@#1\endcsname
      >> >>}%<<
  \expandafter\let\csname pgfsmask@#1\endcsname\expandafter\csname pgfsmaskxform@#1\endcsname%
}

% Transparency groups came from pgfsys-pdftex.def
\def\pgfsys@transparencygroupfrombox#1{%
  {%
    \pgf@sys@bp@correct\pgf@picminx%
    \pgf@sys@bp@correct\pgf@picmaxx%
    \pgf@sys@bp@correct\pgf@picminy%
    \pgf@sys@bp@correct\pgf@picmaxy%
    \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
      \special{pdf:bxobj @pgftransgroup\the\pgfsys@objnum\space bbox \pgf@sys@tonumber\pgf@picminx\space\pgf@sys@tonumber\pgf@picminy\space\pgf@sys@tonumber\pgf@picmaxx\space\pgf@sys@tonumber\pgf@picmaxy}%
      \wd#1=0pt%
      \ht#1=0pt%
      \dp#1=0pt%
      \hskip\pgf@picminx bp\box#1\hskip-\pgf@picminx bp%
      \special{pdf:exobj <</Group <</S/Transparency>> >>}}}\@tempa%
    \special{pdf:uxobj @pgftransgroup\the\pgfsys@objnum}%
  }%
  \global\advance\pgfsys@objnum\@ne%
}

%
% Position tracking (needs pdftex >= 1.40.0-alpha-20051205 in dvi mode)
%
\ifx\pdftexversion\@undefined
\else
  \ifnum\pdftexversion>139
    \pgf@sys@pdf@dvi@tracker
  \fi
\fi

\endinput

%%% Local Variables: 
%%% mode: latex
%%% End: 
