% 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-tex4ht.def,v 1.16 2008/02/07 19:25:41 tantau Exp $


\RequirePackage{xcolor}


% Driver commands for tex4ht

%
% Load common pdf commands:
%
\input pgfsys-common-svg.def

%
% tex4ht-specific stuff:
%
\def\pgfsys@invoke#1{\pgf@sys@fail{svg code in preamble}}
\def\pgfsys@body@invoke#1{\HCode{#1}}

\newcount\pgf@sys@svg@picnum


\def\pgfsys@beginpicture{%
  \Configure{[]}{$}{$}
  \Configure{()}{$}{$}
  \Configure{$$}{}{}{}%$$
  \Configure{$}{}{}{}%$
  \Configure{SUB}{\HCode{<tspan baseline-shift="sub">}}{\HCode{</tspan>}}
  \Configure{SUP}{\HCode{<tspan baseline-shift="sub">}}{\HCode{</tspan>}}
  \Configure{SUBSUP}{}{}{}
  \Configure{htf} 
    {0}{+}{<tspan font-family="}
    {\%s}{" font-size="\%s}{}{}
    {">}{</tspan>}
}
\def\pgfsys@endpicture{%
  % undo:
  \Configure{htf}{0}{+}{<span\Hnewline
     class="}{\%s}{-\%s}{x-x-\%d}{}{">}{</span>}
}
 
\def\pgfsys@typesetpicturebox#1{%
  \global\advance\pgf@sys@svg@picnum by 1\relax%
  % Ok, compute width/height
  \pgf@xa=\pgf@picminx%
  \pgf@ya=\pgf@picminy%
  \pgf@xb=\pgf@picmaxx%
  \pgf@yb=\pgf@picmaxy%
  \pgf@x=\pgf@xb%
  \advance\pgf@x by-\pgf@xa%
  \pgf@y=\pgf@yb%
  \advance\pgf@y by-\pgf@ya%
  \ifdim\pgf@y<1pt%
    \pgf@y=1pt%
  \fi%
  \pgf@xc=\pgf@x%  
  \pgf@yc=\pgf@y%  
  \pgfsysprotocol@flushcurrentprotocol%
  \HCode{<object data="\jobname-\the\pgf@sys@svg@picnum.svg"
    width="}%
  % Calculate pixel coordinates (this does not really make sense, but
  % we have to do it anyway). Assume 96dpi.
  \pgf@x=1.3333333\pgf@x%
  \pgf@y=1.3333333\pgf@y%
  % Add one pixel for security:
  \advance\pgf@x by1pt%
  \advance\pgf@y by1pt%
  \pgf@sys@pt\pgf@x%
  \pgfsysprotocol@flushcurrentprotocol%
  \pgfsys@invoke{" height="}%
  \pgf@sys@pt\pgf@y%
  \pgfsysprotocol@flushcurrentprotocol%
  \pgfsys@invoke{" type="image/svg+xml"><p>SVG-Viewer needed.</p></object>}%
  \special{t4ht>\jobname-\the\pgf@sys@svg@picnum.svg}%
  \pgfsys@invoke{<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink" width="\the\pgf@xc"
    height="\the\pgf@yc" viewBox="}%
  \pgf@sys@pt{\pgf@xa}\pgf@sys@pt{\pgf@ya}\pgf@sys@pt{\pgf@xc}\pgf@sys@pt{\pgf@yc}%
  \pgfsysprotocol@flushcurrentprotocol%
  \pgf@ya=-\pgf@ya%
  \pgfsys@invoke{">\Hnewline  <g transform="translate(0,}
  \pgf@sys@pt\pgf@yb\pgfsysprotocol@flushcurrentprotocol%
  \pgfsys@invoke{) scale(1,-1) translate(0,}%
  \pgf@sys@pt\pgf@ya\pgfsysprotocol@flushcurrentprotocol%
  \pgfsys@invoke{)">\Hnewline }%
  \pgfmathsetlength\pgf@x{\pgf@shift@baseline}%
  \advance\pgf@x by-\pgf@picminy\relax%
  \advance\pgf@picmaxy by-\pgf@picminy\relax% maxy is now the height
  \advance\pgf@picmaxx by-\pgf@picminx\relax% maxx is now the width
  \setbox#1=\hbox{\hskip-\pgf@picminx\lower\pgf@picminy\box#1}%
  \ht#1=\pgf@picmaxy%
  \wd#1=\pgf@picmaxx%
  \dp#1=0pt%
  \leavevmode\raise-\pgf@x\box#1%
  \pgfsys@invoke{</g>\Hnewline </svg>\Hnewline }%
  \special{t4ht<\jobname-\the\pgf@sys@svg@picnum.svg}%
}

\def\pgfsys@hbox#1{%
  \pgf@x=.5\wd#1%
  \ifx\tikz@textcolor\relax%
  \else%
    \ifx\tikz@textcolor\pgfutil@empty%
      \pgfsetfillcolor{.}%
    \else%
      \pgfsetfillcolor{\tikz@textcolor}%
    \fi%
  \fi% tweak for TikZ
  \HCode{<text
    style="stroke:none"
    transform="scale(1,-1) translate(\pgf@sys@tonumber{\pgf@x},0)"
    text-anchor="middle"
  }%
  \ifx\f@size\pgfutil@empty%
  \else%
    \HCode{ font-size="\f@size"}%
  \fi%  
  \HCode{>\Hnewline }%
    \wd#1=0pt%
    \ht#1=0pt%
    \dp#1=0pt%
    \box#1
  \HCode{</text>\Hnewline }%
}

\def\pgfsys@outerinvoke{\ifpgfpicture\expandafter\pgfsys@invoke\else\expandafter\pgfutil@gobble\fi}

\def\pgfsys@shadingoutsidepgfpicture#1{\pgf@sys@fail{svg code in preamble}}
\def\pgfsys@shadinginsidepgfpicture#1{\pgf@sys@fail{svg code in preamble}}

\def\pgfsys@body@shadingoutsidepgfpicture#1{%
  \begingroup%
    #1%
    \setbox\pgfpic=\hbox to0pt{%
      \pgfsys@beginpicture%
        \pgfsys@beginscope%
          \pgf@sys@svg@sh@defs%
          \pgf@sys@svg@sh%
        \pgfsys@endscope%
      \pgfsys@endpicture%
      \hss%  
    }%
    \pgf@process{\pgf@sys@svg@pos}%
    \pgf@picminx=0pt%
    \pgf@picminy=0pt%
    \pgf@picmaxx=\pgf@x%
    \pgf@picmaxy=\pgf@y%
    \def\pgf@shift@baseline{0pt}%
    \pgfsys@typesetpicturebox\pgfpic%
  \endgroup%
}

\def\pgfsys@body@shadinginsidepgfpicture#1{%
  #1%
  \pgf@sys@svg@sh@defs% hmmm....
  \pgf@process{\pgf@sys@svg@pos}
  \pgf@xa=-.5\pgf@x%
  \pgf@ya=-.5\pgf@y%
  \pgfsysprotocol@literal{<g transform="translate(\pgf@sys@tonumber{\pgf@xa},\pgf@sys@tonumber{\pgf@ya})">\Hnewline}%
  \pgf@sys@svg@sh%
  \pgfsysprotocol@literal{</g>\Hnewline}%
}

% Protect against color.4ht evil meddling with xcolor:
\let\pgf@xcolor@declaredcolor=\@declaredcolor
\let\pgf@xcolor@undeclaredcolor=\@undeclaredcolor

\AtBeginDocument{
  \let\pgfsys@invoke=\pgfsys@body@invoke
  \let\pgfsys@shadingoutsidepgfpicture=\pgfsys@body@shadingoutsidepgfpicture
  \let\pgfsys@shadinginsidepgfpicture=\pgfsys@body@shadinginsidepgfpicture
  \let\pgf@texht@declaredcolor=\@declaredcolor
  \let\pgf@texht@undeclaredcolor=\@undeclaredcolor
  \def\@declaredcolor{\ifpgfpicture\expandafter\pgf@xcolor@declaredcolor\else\expandafter\pgf@texht@declaredcolor\fi}
  \def\@undeclaredcolor{\ifpgfpicture\expandafter\pgf@xcolor@undeclaredcolor\else\expandafter\pgf@texht@undeclaredcolor\fi}
  \ConfigureEnv{pgfpicture}{}{}{}{}
}

\endinput


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