% Copyright 2019 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{pgfsys-dvi.def}

% Driver commands for pure, ultra-portable dvi, in the tradition of
% old-style LaTeX pictures.
%
% Do not expect magic from this file


\newtoks\pgf@sys@dvi@path
\def\pgf@sys@dvi@addtopath#1{\pgf@sys@dvi@path\expandafter{\the\pgf@sys@dvi@path#1}}

% Placing an object:
\def\pgf@sys@dvi@put#1#2#3{%
  {%
    \setbox\pgf@hbox=\hbox{\raise#2\hbox to0pt{\kern#1\relax#3\hss}}%
    \wd\pgf@hbox=0pt%
    \ht\pgf@hbox=0pt%
    \dp\pgf@hbox=0pt%
    \box\pgf@hbox%
  }%
}

\def\pgf@sys@dvi@drawrect#1#2#3#4{%
  {%
    \pgf@x=#1%
    \pgf@y=#2%
    \pgf@xa=#3%
    \pgf@ya=#4%
    \ifdim\pgf@xa<0pt%
      \advance\pgf@x by\pgf@xa%
      \pgf@xa=-\pgf@xa%
    \fi%
    \ifdim\pgf@ya<0pt%
      \advance\pgf@y by\pgf@ya%
      \pgf@ya=-\pgf@ya%
    \fi%
    \pgf@sys@dvi@put{\pgf@x}{\pgf@y}{\vrule width\pgf@xa height\pgf@ya}%
  }%
}

\def\pgf@sys@dvi@bezier{%
  {%
    % First iteration:
    \pgf@x=\pgf@time@t\pgf@x\advance\pgf@x by\pgf@time@s\pgf@xa%
    \pgf@y=\pgf@time@t\pgf@y\advance\pgf@y by\pgf@time@s\pgf@ya%
    \pgf@xa=\pgf@time@t\pgf@xa\advance\pgf@xa by\pgf@time@s\pgf@xb%
    \pgf@ya=\pgf@time@t\pgf@ya\advance\pgf@ya by\pgf@time@s\pgf@yb%
    \pgf@xb=\pgf@time@t\pgf@xb\advance\pgf@xb by\pgf@time@s\pgf@xc%
    \pgf@yb=\pgf@time@t\pgf@yb\advance\pgf@yb by\pgf@time@s\pgf@yc%
    % Second iteration:
    \pgf@x=\pgf@time@t\pgf@x\advance\pgf@x by\pgf@time@s\pgf@xa%
    \pgf@y=\pgf@time@t\pgf@y\advance\pgf@y by\pgf@time@s\pgf@ya%
    \pgf@xa=\pgf@time@t\pgf@xa\advance\pgf@xa by\pgf@time@s\pgf@xb%
    \pgf@ya=\pgf@time@t\pgf@ya\advance\pgf@ya by\pgf@time@s\pgf@yb%
    % Third iteration:
    \pgf@x=\pgf@time@t\pgf@x\advance\pgf@x by\pgf@time@s\pgf@xa%
    \pgf@y=\pgf@time@t\pgf@y\advance\pgf@y by\pgf@time@s\pgf@ya%
    %
    \advance\pgf@x by-.5\pgf@sys@dvi@linewidth%
    \advance\pgf@y by-.5\pgf@sys@dvi@linewidth%
    \pgf@sys@dvi@drawrect{\pgf@x}{\pgf@y}{\pgf@sys@dvi@linewidth}{\pgf@sys@dvi@linewidth}%
  }%
}

\def\pgf@sys@dvi@drawcurve#1#2#3#4#5#6#7#8{%
  {%
    \pgf@x=#1%
    \pgf@y=#2%
    \pgf@xa=#3%
    \pgf@ya=#4%
    \pgf@xb=#5%
    \pgf@yb=#6%
    \pgf@xc=#7%
    \pgf@yc=#8%
    % Compute number of steps:
    \pgfutil@tempdima=\pgf@x%
    \advance\pgfutil@tempdima by-\pgf@xa%
    \ifdim\pgfutil@tempdima<0pt%
      \pgfutil@tempdimb=-\pgfutil@tempdima%
    \else%
      \pgfutil@tempdimb=\pgfutil@tempdima%
    \fi%
    \pgfutil@tempdima=\pgf@y%
    \advance\pgfutil@tempdima by-\pgf@ya%
    \ifdim\pgfutil@tempdima<0pt%
      \pgfutil@tempdima=-\pgfutil@tempdima%
    \fi%
    \ifdim\pgfutil@tempdima>\pgfutil@tempdimb%
      \pgfutil@tempdimb=\pgfutil@tempdima%
    \fi%
    \pgfutil@tempdima=\pgf@xb%
    \advance\pgfutil@tempdima by-\pgf@xc%
    \ifdim\pgfutil@tempdima<0pt%
      \pgfutil@tempdima=-\pgfutil@tempdima%
    \fi%
    \ifdim\pgfutil@tempdima>\pgfutil@tempdimb%
      \pgfutil@tempdimb=\pgfutil@tempdima%
    \fi%
    \pgfutil@tempdima=\pgf@yb%
    \advance\pgfutil@tempdima by-\pgf@yc%
    \ifdim\pgfutil@tempdima<0pt%
      \pgfutil@tempdima=-\pgfutil@tempdima%
    \fi%
    \ifdim\pgfutil@tempdima>\pgfutil@tempdimb%
      \pgfutil@tempdimb=\pgfutil@tempdima%
    \fi%
    \pgfutil@tempdima=\pgf@x%
    \advance\pgfutil@tempdima by-\pgf@xc%
    \ifdim\pgfutil@tempdima<0pt%
      \pgfutil@tempdima=-\pgfutil@tempdima%
    \fi%
    \ifdim\pgfutil@tempdima>\pgfutil@tempdimb%
      \pgfutil@tempdimb=\pgfutil@tempdima%
    \fi%
    \pgfutil@tempdima=\pgf@y%
    \advance\pgfutil@tempdima by-\pgf@yc%
    \ifdim\pgfutil@tempdima<0pt%
      \pgfutil@tempdima=-\pgfutil@tempdima%
    \fi%
    \ifdim\pgfutil@tempdima>\pgfutil@tempdimb%
      \pgfutil@tempdimb=\pgfutil@tempdima%
    \fi%
    \pgfutil@tempcnta=\pgfutil@tempdimb%
    \ifdim\pgf@sys@dvi@linewidth>0.3pt%
      \pgfutil@tempdimb=0.15pt%
    \else%
      \ifdim\pgf@sys@dvi@linewidth<0.05pt%
        \pgfutil@tempdimb=.025pt%
      \else%
        \pgfutil@tempdimb=.5\pgf@sys@dvi@linewidth%
      \fi%
    \fi%
    \pgfutil@tempcntb=\pgfutil@tempdimb%
    \divide\pgfutil@tempcnta by\pgfutil@tempcntb\relax%
    \ifnum\pgfutil@tempcnta>0\relax%
      \pgfutil@tempcntb=0\relax%
      \loop%
        {%
          \pgfutil@tempdima=\the\pgfutil@tempcntb pt%
          \pgfutil@tempdimb=\the\pgfutil@tempcnta pt%
          \advance\pgfutil@tempdimb by-\pgfutil@tempdima\relax%
          \divide\pgfutil@tempdima by\pgfutil@tempcnta\relax%
          \divide\pgfutil@tempdimb by\pgfutil@tempcnta\relax%
          \edef\pgf@time@t{\pgf@sys@tonumber{\pgfutil@tempdima}}%
          \edef\pgf@time@s{\pgf@sys@tonumber{\pgfutil@tempdimb}}%
          \pgf@sys@dvi@bezier%
        }%
        \advance\pgfutil@tempcntb by1\relax
      \ifnum\pgfutil@tempcntb<\pgfutil@tempcnta%
        {\def\pgf@time@t{1}\def\pgf@time@s{0}\pgf@sys@dvi@bezier}%
      \repeat%
    \fi%
  }%
}

% Text:
\def\pgfsys@hbox#1{%
  \pgfsys@begin@idscope%
  \wd#1=0pt%
  \ht#1=0pt%
  \dp#1=0pt%
  \pgf@sys@dvi@put{\pgf@sys@dvi@pt@x}{\pgf@sys@dvi@pt@y}{\box#1}%
  \pgfsys@end@idscope%
}



% Literals:

\def\pgfsys@invoke#1{#1}


% Buffered path construction:

\def\pgfsys@moveto#1#2{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@addtopath{\noexpand\pgf@sys@dvi@moveto{#1}{#2}}}}
\def\pgfsys@lineto#1#2{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@addtopath{\noexpand\pgf@sys@dvi@lineto{#1}{#2}}}}
\def\pgfsys@curveto#1#2#3#4#5#6{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@addtopath{\noexpand\pgf@sys@dvi@curveto{#1}{#2}{#3}{#4}{#5}{#6}}}}
\def\pgfsys@rect#1#2#3#4{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@addtopath{\noexpand\pgf@sys@dvi@rect{#1}{#2}{#3}{#4}}}}
\def\pgfsys@closepath{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@addtopath{\noexpand\pgf@sys@dvi@closepath}}}


% Scoping
\def\pgfsys@beginscope{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@beginscope}}%
\def\pgfsys@endscope{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@endscope}}%

\def\pgf@sys@dvi@beginscope{%
  % Save current stuff...
  \edef\pgf@sys@dvi@return{%
    \global\pgf@sys@dvi@linewidth=\the\pgf@sys@dvi@linewidth%
    \global\pgf@sys@dvi@pt@x=\the\pgf@sys@dvi@pt@x%
    \global\pgf@sys@dvi@pt@y=\the\pgf@sys@dvi@pt@y%
    \gdef\noexpand\pgf@sys@dvi@pt@aa{\pgf@sys@dvi@pt@aa}%
    \gdef\noexpand\pgf@sys@dvi@pt@ab{\pgf@sys@dvi@pt@ab}%
    \gdef\noexpand\pgf@sys@dvi@pt@ba{\pgf@sys@dvi@pt@ba}%
    \gdef\noexpand\pgf@sys@dvi@pt@bb{\pgf@sys@dvi@pt@bb}%
  }%
  \begingroup%
  \ignorespaces%
}

\def\pgf@sys@dvi@endscope{%
  \endgroup%
  \pgf@sys@dvi@return%
}



% Line width

\newdimen\pgf@sys@dvi@linewidth
\pgf@sys@dvi@linewidth=0.4pt

\def\pgfsys@setlinewidth#1{\global\pgf@sys@dvi@linewidth=#1\relax}


% Transformation matrix

\newdimen\pgf@sys@dvi@pt@x
\newdimen\pgf@sys@dvi@pt@y

\def\pgf@sys@dvi@pt@aa{1.0} \def\pgf@sys@dvi@pt@ab{0.0}
\def\pgf@sys@dvi@pt@ba{0.0} \def\pgf@sys@dvi@pt@bb{1.0}

\def\pgf@sys@dvi@pos@transform#1#2{%
  \pgf@pt@temp=#1%
  #1=\pgf@sys@dvi@pt@aa#1%
  \advance#1 by\pgf@sys@dvi@pt@ba#2%
  #2=\pgf@sys@dvi@pt@bb#2%
  \advance#2 by\pgf@sys@dvi@pt@ab\pgf@pt@temp%
  \advance#1 by\pgf@sys@dvi@pt@x%
  \advance#2 by\pgf@sys@dvi@pt@y%
}

\def\pgf@sys@dvi@transformcm#1#2#3#4#5#6{%
  {%
    \pgf@xc=#5%
    \pgf@yc=#6%
    %
    \pgf@x=#1pt%
    \pgf@y=#2pt%
    %
    \pgf@xa=\pgf@sys@dvi@pt@aa\pgf@x%
    \advance\pgf@xa by \pgf@sys@dvi@pt@ba\pgf@y%
    %
    \pgf@ya=\pgf@sys@dvi@pt@ab\pgf@x%
    \advance\pgf@ya by \pgf@sys@dvi@pt@bb\pgf@y%
    %
    \pgf@x=#3pt%
    \pgf@y=#4pt%
    %
    \pgf@xb=\pgf@sys@dvi@pt@aa\pgf@x%
    \advance\pgf@xb by \pgf@sys@dvi@pt@ba\pgf@y%
    %
    \pgf@yb=\pgf@sys@dvi@pt@ab\pgf@x%
    \advance\pgf@yb by \pgf@sys@dvi@pt@bb\pgf@y%
    %
    \pgf@x=\pgf@sys@dvi@pt@aa\pgf@xc%
    \advance\pgf@x by\pgf@sys@dvi@pt@ba\pgf@yc%
    \advance\pgf@x by\pgf@sys@dvi@pt@x%
    %
    \pgf@y=\pgf@sys@dvi@pt@ab\pgf@xc%
    \advance\pgf@y by\pgf@sys@dvi@pt@bb\pgf@yc%
    \advance\pgf@y by\pgf@sys@dvi@pt@y%
    %
    \global\pgf@sys@dvi@pt@x=\pgf@x%
    \global\pgf@sys@dvi@pt@y=\pgf@y%
    \xdef\pgf@sys@dvi@pt@aa{\pgf@sys@tonumber{\pgf@xa}}%
    \xdef\pgf@sys@dvi@pt@ab{\pgf@sys@tonumber{\pgf@ya}}%
    \xdef\pgf@sys@dvi@pt@ba{\pgf@sys@tonumber{\pgf@xb}}%
    \xdef\pgf@sys@dvi@pt@bb{\pgf@sys@tonumber{\pgf@yb}}%
  }%
}

\def\pgfsys@transformcm#1#2#3#4#5#6{%
  {%
    \pgf@x=#5\relax%
    \pgf@y=#6\relax%
    \pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@transformcm{#1}{#2}{#3}{#4}{\the\pgf@x}{\the\pgf@y}}%
  }%
}


% Stroking

\def\pgfsys@stroke{\pgfsysprotocol@literal{\noexpand\pgf@sys@dvi@stroke}}%


\def\pgf@sys@dvi@stroke{%
  \let\pgf@sys@dvi@moveto=\pgf@sys@dvi@moveto@stroke%
  \let\pgf@sys@dvi@lineto=\pgf@sys@dvi@lineto@stroke%
  \let\pgf@sys@dvi@curveto=\pgf@sys@dvi@curveto@stroke%
  \let\pgf@sys@dvi@rect=\pgf@sys@dvi@rect@stroke%
  \let\pgf@sys@dvi@closepath=\pgf@sys@dvi@closepath@stroke%
  {\the\pgf@sys@dvi@path}%
  \pgf@sys@dvi@path{}%
}

\def\pgf@sys@dvi@moveto@stroke#1#2{%
  % These two store the current position:
  \pgf@xc=#1\relax%
  \pgf@yc=#2\relax%
  % These two store the last moveto:
  \pgf@xb=\pgf@xc%
  \pgf@yb=\pgf@yc%
}

\def\pgf@sys@dvi@lineto@stroke#1#2{%
  {%
    \pgf@xa=#1\relax%
    \pgf@ya=#2\relax%
    \pgf@x=\pgf@xc%
    \pgf@y=\pgf@yc%
    \pgf@sys@dvi@pos@transform{\pgf@xa}{\pgf@ya}%
    \pgf@sys@dvi@pos@transform{\pgf@x}{\pgf@y}%
    % Now, we need to draw a line from (x/y) to (xa/ya)
    %
    % Horizontal?
    \ifdim\pgf@y=\pgf@ya%
      % Ok, that should be easy...
      \pgf@xb=\pgf@xa%
      \advance\pgf@xb by-\pgf@x%
      \advance\pgf@y by-0.5\pgf@sys@dvi@linewidth%
      \pgf@sys@dvi@drawrect{\pgf@x}{\pgf@y}{\pgf@xb}{\pgf@sys@dvi@linewidth}%
    \else%
      \ifdim\pgf@x=\pgf@xa%
        % Ok, also easy...
        \pgf@yb=\pgf@ya%
        \advance\pgf@yb by-\pgf@y%
        \advance\pgf@x by-0.5\pgf@sys@dvi@linewidth%
        \pgf@sys@dvi@drawrect{\pgf@x}{\pgf@y}{\pgf@sys@dvi@linewidth}{\pgf@yb}%
      \else%
        \edef\pgf@marshal{\noexpand\pgf@sys@dvi@drawcurve{\the\pgf@x}{\the\pgf@y}{\the\pgf@x}{\the\pgf@y}{\the\pgf@xa}{\the\pgf@ya}{\the\pgf@xa}{\the\pgf@ya}}%
        \pgf@marshal%
      \fi%
    \fi%
  }%
  % Remember:
  \pgf@xc=#1\relax%
  \pgf@yc=#2\relax%
}


\def\pgf@sys@dvi@curveto@stroke#1#2#3#4#5#6{%
  \pgf@x=\pgf@xc%
  \pgf@y=\pgf@yc%
  \pgf@xa=#1%
  \pgf@ya=#2%
  \pgf@xb=#3%
  \pgf@yb=#4%
  \pgf@xc=#5%
  \pgf@yc=#6%
  {%
    \pgf@sys@dvi@pos@transform{\pgf@x}{\pgf@y}%
    \pgf@sys@dvi@pos@transform{\pgf@xa}{\pgf@ya}%
    \pgf@sys@dvi@pos@transform{\pgf@xb}{\pgf@yb}%
    \pgf@sys@dvi@pos@transform{\pgf@xc}{\pgf@yc}%
    \pgf@sys@dvi@drawcurve{\pgf@x}{\pgf@y}{\pgf@xa}{\pgf@ya}{\pgf@xb}{\pgf@yb}{\pgf@xc}{\pgf@yc}%
  }%
}

\def\pgf@sys@dvi@closepath@stroke{%
  \pgf@sys@dvi@lineto@stroke{\pgf@xc}{\pgf@yc}%
}

\def\pgf@sys@dvi@rect@stroke#1#2#3#4{%
  \pgf@x=#1\relax%
  \pgf@y=#2\relax%
  \pgf@xa=#3\relax%
  \pgf@ya=#4\relax%
  \advance\pgf@xa by\pgf@x%
  \advance\pgf@ya by\pgf@y%
  \pgf@sys@dvi@moveto@stroke{\pgf@x}{\pgf@y}%
  \pgf@sys@dvi@lineto@stroke{\pgf@xa}{\pgf@y}%
  \pgf@sys@dvi@lineto@stroke{\pgf@xa}{\pgf@ya}%
  \pgf@sys@dvi@lineto@stroke{\pgf@x}{\pgf@ya}%
  \pgf@sys@dvi@closepath@stroke%
}



\endinput

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