% 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 Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\def\pgfsysdriver{pgfsys-dvisvgm.def}

\PassOptionsToPackage{hypertex}{hyperref}
\makeatletter
\g@addto@macro\selectfont{%
  \long\edef\longf@family{\f@family}%
  \ifx\longf@family\ttdefault
    \ignoreligaturesinfont\font\relax
  \fi
}
\makeatother

\usepackage{graphicx,xcolor}

\def\graphicsdvisvgmtext{dvisvgm.def}

\expandafter\ifx\csname Gin@driver\endcsname\graphicsdvisvgmtext
\else
  \PackageError{manual}{You must use dvisvgm.def as your graphics driver}
\fi

% Make sure to compile this using lualatex.


% Add forward button to each page:

\usepackage{everyshi}

\makeatletter
\newbox\pgfmanualbuttonbox
\newcount\pgfmanualcount
\newdimen\mydim

\EveryShipout{%%
  \setbox255=\vbox{%
    \pgfkeys{/pgf/fpu=false}%
    \@tempdima=\ht255\relax%
    \unvbox255%
    \vskip-\@tempdima%
    \pgfmanualaddforwardbackward%
    \pgfmanualaddtoc%
  }%
}

\def\pgfmanualaddforwardbackward{%
  % Adds page forward and page backward buttons at the left and right
  % end of each page
  \let\protect=\relax%
  \pgfmanualcount=\c@page%
  \advance\pgfmanualcount by-1\relax%
  \ifnum\pgfmanualcount>0\relax%
    \begin{pgfpicture}{1in}{-1in}{1in}{-1in}
      \special{dvisvgm:raw <g pointer-events="all" visibility="hidden">}
      \pgftext[top,left]{\href{\jobname-\pgfmanualpadded{\pgfmanualcount}.svg}{{\color{white}\vrule width 2cm height \paperheight}}}
      \special{dvisvgm:raw </g>}
      \special{dvisvgm:raw <g pointer-events="all">}
      \pgftext[top,left]{%
        \href{\jobname-\pgfmanualpadded{\pgfmanualcount}.svg}
             {\vbox to \paperheight{\hsize=2cm\vss\centerline{$\blacktriangleleft$}\vss}}}
      \special{dvisvgm:raw </g>}
    \end{pgfpicture}%
  \fi%
  \advance\pgfmanualcount by2\relax%
  \begin{pgfpicture}{-\paperwidth+1in}{-1in}{-\paperwidth+1in}{-1in}
    \special{dvisvgm:raw <g pointer-events="all" visibility="hidden">}
    \pgftext[top,right]{\href{\jobname-\pgfmanualpadded{\pgfmanualcount}.svg}{\color{white}{\vrule width 2cm height \paperheight}}}
    \special{dvisvgm:raw </g>}
    \special{dvisvgm:raw <g pointer-events="all">}
    \pgftext[top,right]{%
      \href{\jobname-\pgfmanualpadded{\pgfmanualcount}.svg}%
           {\vbox to \paperheight{\hsize=2cm\vss\centerline{$\blacktriangleright$}\vss}}}
    \special{dvisvgm:raw </g>}
  \end{pgfpicture}%
}

\newbox\chapterbox

\newcount\pgfmanualpartcount
\newcount\pgfmanualsectioncount
\newcount\pgfmanualsubsectioncount
\newcount\pgfmanualsubsubsectioncount

\newcount\pgfmanualtempcount

\newtoks\toctoks
{% Read toc into \toctoks

  \global\toctoks{
    \global\pgfmanualpartcount0\relax
    \global\pgfmanualsectioncount0\relax
    \global\pgfmanualsubsectioncount0\relax
    \global\pgfmanualsubsubsectioncount0\relax
  }
  
  \def\contentsline#1#2#3#4{%
    \expandafter\let\expandafter\pgfmanualhandler\csname pgfmanualhandlekind#1\endcsname%
    \ifx\pgfmanualhandler\relax%
      \PackageError{manual}{Unknown contents level '#1'}{}%
    \else%
      \pgfmanualhandler{#2}{#3}{#4}%%
    \fi%
    \ignorespaces%
  }

  \def\pgfmanualhandlekindpart#1#2#3{%
    \pgfmanualtempcount#2\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \edef\pgfmanualtemponepagebefore{\the\pgfmanualtempcount}%
    \pgfmanualtempcount\pgfmanualpartcount\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \expandafter\xdef\csname pgfmanualpartendpage\the\pgfmanualpartcount\endcsname{\pgfmanualtemponepagebefore}%
    \global\advance\pgfmanualpartcount by1\relax%
    \expandafter\gdef\csname pgfmanualpartstartpage\the\pgfmanualpartcount\endcsname{#2}%
    \expandafter\global\expandafter\toctoks\expandafter{\the\toctoks\pgfmanualtypesetpart{#1}{#2}}%
  }
  \def\pgfmanualhandlekindsection#1#2#3{%
    \pgfmanualtempcount#2\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \edef\pgfmanualtemponepagebefore{\the\pgfmanualtempcount}%
    \pgfmanualtempcount\pgfmanualsectioncount\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \expandafter\xdef\csname pgfmanualsectionendpage\the\pgfmanualsectioncount\endcsname{\pgfmanualtemponepagebefore}%
    \global\advance\pgfmanualsectioncount by1\relax%
    \expandafter\gdef\csname pgfmanualsectionstartpage\the\pgfmanualsectioncount\endcsname{#2}%
    \expandafter\global\expandafter\toctoks\expandafter{\the\toctoks\pgfmanualtypesetsection{#1}{#2}}%
  }
  \def\pgfmanualhandlekindsubsection#1#2#3{%
    \pgfmanualtempcount#2\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \edef\pgfmanualtemponepagebefore{\the\pgfmanualtempcount}%
    \pgfmanualtempcount\pgfmanualsubsectioncount\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \expandafter\xdef\csname pgfmanualsubsectionendpage\the\pgfmanualsubsectioncount\endcsname{\pgfmanualtemponepagebefore}%
    \global\advance\pgfmanualsubsectioncount by1\relax%
    \expandafter\gdef\csname pgfmanualsubsectionstartpage\the\pgfmanualsubsectioncount\endcsname{#2}%
    \expandafter\global\expandafter\toctoks\expandafter{\the\toctoks\pgfmanualtypesetsubsection{#1}{#2}}%
  }
  \def\pgfmanualhandlekindsubsubsection#1#2#3{%
    \pgfmanualtempcount#2\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \edef\pgfmanualtemponepagebefore{\the\pgfmanualtempcount}%
    \pgfmanualtempcount\pgfmanualsubsubsectioncount\relax%
    \advance\pgfmanualtempcount by-1\relax%
    \expandafter\xdef\csname pgfmanualsubsubsectionendpage\the\pgfmanualsubsubsectioncount\endcsname{\pgfmanualtemponepagebefore}%
    \global\advance\pgfmanualsubsubsectioncount by1\relax%
    \expandafter\gdef\csname pgfmanualsubsubsectionstartpage\the\pgfmanualsubsubsectioncount\endcsname{#2}%
    \expandafter\global\expandafter\toctoks\expandafter{\the\toctoks\pgfmanualtypesetsubsubsection{#1}{#2}}%
  }
  \def\pgfmanualhandlekindparagraph#1#2#3{%
    % ignore
  }
  
  \IfFileExists{\jobname.toc}{\input{\jobname.toc}}{}%
}

{\catcode`\#=11
\gdef\pgfmanualhashmark{#}
}

\def\pgfmanualaddifnotempty#1#2{%
  \ifvoid#1%
  \else%
    \setbox#1=\vtop{%
      \hsize\mydim%
      \leavevmode{\strut#2}%
      \unvbox#1%
    }%
  \fi%
}

\definecolor{tocbackground}{rgb}{0.99,0.99,0.97}

\def\pgfmanualpickpage#1#2#3#4#5{%
  \c@pgf@counta#2\relax%
  \xdef\pgfmanualtocpage{\the\c@pgf@counta}%
}

\def\pgfmanualaddtoc{%
  \begingroup
    \expandafter\let\expandafter\pgfmanualtemp\csname r@table-of-contents\endcsname%
    \ifx\pgfmanualtemp\relax%
      \def\pgfmanualtocpage{1}%
    \else%
      \expandafter\pgfmanualpickpage\pgfmanualtemp%
    \fi%
    \let\numberline\pgfmanualpagetocnumberline%
    \mydim\paperwidth\advance\mydim by-3cm%
    \mydim=\mydim\relax%
    \footnotesize%
    \setbox\chapterbox=\box\voidb@x%
    \pgfmanualpagetoctobox{\chapterbox}{\pgfmanualpagetocline{\pgfmanualtocpage}{\Large\textbf{Contents}\vrule
        width0pt height7mmdepth2.5mm}{0cm}{tocbackground}}%
    \the\toctoks%
    \begin{pgfpicture}{1in-1.5cm}{-1in}{1in-1.5cm}{-1in}
      \pgftext[top,left]{\vbox{%
          \nointerlineskip%
          \parskip0pt%
          \hsize=\mydim%
          \leftskip0pt%
          \rightskip0pt%
          \parindent0pt%
          \special{dvisvgm:raw
            <style type="text/css">
            \pgfmanualhashmark toc:hover \pgfmanualhashmark content { visibility: visible }
            </style>
            <g id="toc" opacity=".97">
          }%
          \special{dvisvgm:raw <g pointer-events="all" visibility="hidden">}
          {\color{white}{\vrule width\mydim height 2cm}}\break%
          \special{dvisvgm:raw </g> }%
          \hbox{}\vskip-2.6cm\break\leavevmode%
          \special{dvisvgm:raw
            <g id="content" visibility="hidden">
          }%
          \box\chapterbox\break%
          \hbox{}\vskip-2pt\break%
          {\color{tocbackground}{\vrule width\mydim height 5mm}}\break%
          \special{dvisvgm:raw </g> }%
          \special{dvisvgm:raw </g> }
        }}%
    \end{pgfpicture}    
  \endgroup
}
\def\pgfmanualpadded#1{%
  \ifnum#1<1000 0\fi%
  \ifnum#1<100 0\fi%
  \ifnum#1<10 0\fi%
  \the#1%
}

\def\pgfmanualpagetocnumberline#1{}
\def\pgfmanualpagetoctobox#1#2{%
  \setbox#1=\vtop{%
    \unvbox#1%
    #2}%
}
\newdimen\pgfmanualtoclineskip
\pgfmanualtoclineskip1.5pt
\def\pgfmanualpagetocline#1#2#3#4{%
  \pgfmanualtempcount#1%
  \vskip-1pt%
  \hbox{\href{\jobname-\pgfmanualpadded{\pgfmanualtempcount}.svg}{%
      \fboxsep0pt%
      \colorbox{#4}{\vbox{%
          \vskip\pgfmanualtoclineskip
          \hsize\mydim%
          \leftskip#3%
          \advance\leftskip by5mm%
          \rightskip1em plus1fil%
          \advance\rightskip by5mm%
          \noindent\strut#2\strut}}}}%
}


\newif\ifpgfmanualbetweentest

\def\pgfmanualifin#1#2#3#4{%
  \pgfmanualbetweentestfalse
  \expandafter\let\expandafter\pgfmanualstartnum\csname pgfmanual#1startpage\expandafter\the\csname pgfmanual#1count\endcsname\endcsname%
  \ifx\pgfmanualstartnum\relax%
    \def\pgfmanualstartnum{-1}%
  \fi%
  \expandafter\let\expandafter\pgfmanualendnum\csname pgfmanual#1endpage\expandafter\the\csname pgfmanual#1count\endcsname\endcsname%
  \ifx\pgfmanualendnum\relax%
    \def\pgfmanualendnum{1000000000}%
  \fi%
  \ifnum\pgfmanualstartnum>\pgfmanualendnum\relax%
    \let\pgfmanualendnum\pgfmanualstartnum%
  \fi%
  \ifnum\pgfmanualstartnum>#2\relax%
  \else%
    \ifnum\pgfmanualendnum<#2\relax%
    \else%
      \pgfmanualbetweentesttrue%
    \fi%
  \fi%  
  \ifpgfmanualbetweentest%
    #3%
  \else
    #4%
  \fi%
}

\def\pgfmanualtypesetpart#1#2{%
  \pgfmanualcleanuppart#1\relax%
  \global\advance\pgfmanualpartcount by1\relax%
  \pgfmanualifin{part}{\c@page}{\global\let\pgfmanualthispart\pgfmanualparttext}{}%
  \pgfmanualpagetoctobox{\chapterbox}{%
    \pgfmanualpagetocline{#2}{%
      \pgfmanualifin{part}{\c@page}{\bfseries\pgfmanualparttext}{\pgfmanualparttext}%
    }{1.5cm}{tocbackground}}%
}
\def\pgfmanualcleanuppart#1\hspace#2#3\relax{%
  \def\pgfmanualparttext{\llap{\hbox to 1.5cm{Part #1\hss}}#3}%
}

\def\pgfmanualtypesetsection#1#2{%
  \global\advance\pgfmanualsectioncount by1\relax%
  \pgfmanualifin{part}{\c@page}{%
    \pgfmanualifin{section}{\c@page}{\gdef\pgfmanualthischapter{#1}}{}%
    \pgfmanualpagetoctobox{\chapterbox}{%
      \def\numberline##1{\llap{##1\hskip3mm}}%
      \pgfmanualpagetocline{#2}{%
        \pgfmanualifin{section}{\c@page}{\bfseries\itshape#1}{\itshape#1}%
      }{1.5cm}{tocbackground}}%
  }{}%
}
\def\pgfmanualtypesetsubsection#1#2{%
  \global\advance\pgfmanualsubsectioncount by1\relax%
  \pgfmanualifin{part}{\c@page}{%
    \pgfmanualifin{section}{\c@page}{%
      \pgfmanualpagetoctobox{\chapterbox}{%
        \pgfmanualextraskip5mm%
        \pgfmanualpagetocline{#2}{
          \pgfmanualifin{subsection}{\c@page}{\bfseries}{}#1%
        }{2cm}{tocbackground}}%
    }{}%
  }{}%
}
\def\pgfmanualtypesetsubsubsection#1#2{%
  \global\advance\pgfmanualsubsubsectioncount by1\relax%
  \pgfmanualifin{part}{\c@page}{%
    \pgfmanualifin{section}{\c@page}{%
        \pgfmanualextraskip10mm%
        \pgfmanualpagetoctobox{\chapterbox}{\pgfmanualpagetocline{#2}{%
            \pgfmanualifin{subsubsection}{\c@page}{\bfseries}{}\itshape#1%
          }{2.5cm}{tocbackground}}%
    }{}%
  }{}%
}
\def\pgfmanualsplitnumberline#1{%
  \pgfmanualsplitnumberline@#1\relax%
}
\def\pgfmanualsplitnumberline@#1.#2\relax{%
  \llap{#1\rlap{.#2}\hskip3mm\hskip\pgfmanualextraskip}%
}
\newdimen\pgfmanualextraskip

\makeatother



