%% background.sty                                   26 Apr 2002
%%-------------------------------------------------------------
%% History:
%% added options ignore and dvipdfm
%% runs with vlatex and dvipdfm                     10 Nov 2001
%% added some macros for including things into      26 Apr 2002
%% the background:
%%   \bgadd{...}         add something to the top left corner
%%   \bgaddcenter{...}   add something to the center
%%   \bgclear            clear all added elements
%% examples:
%%   \bgadd{\vspace{1cm}\hspace{2cm}\includegraphics[width=3cm]{pic.png}}
%%   \bgcenter{Centered Text}
%%
%%
%% Take care: because we have to insert a pdf comment starting with
%% the letter '%' the catcode of '%' will be changed temporarily.



%% Options
\newif\ifbackground@ignore    \background@ignorefalse
\newif\ifbackground@dvipdfm   \background@dvipdfmfalse
\newif\ifbackground@bgadd     \background@bgaddfalse

\DeclareOption{ignore}{\background@ignoretrue}
\DeclareOption{dvipdfm}{\background@dvipdfmtrue}
\DeclareOption{bgadd}{\background@bgaddtrue}

\ProcessOptions


%% requires package pause.sty
\ifbackground@dvipdfm
  \RequirePackage[dvipdfm]{pause}
\else
  \RequirePackage{pause}
\fi


%%
%% background picture macros, based on a macro by
%% Rolf Niepraschk (niepraschk@ptb.de)
%%

\ifbackground@bgadd
  \RequirePackage{eso-pic}

  \ifbackground@ignore
     \def\bgadd#1{}
     \def\bgaddcenter#1{}
     \def\bgclear{}
  \else
     \def\bgadd#1{%
        \AddToShipoutPicture{
          \setlength{\unitlength}{1pt}% default
           \put(0,\strip@pt\paperheight){%
              \parbox[t][\paperheight]{\paperwidth}{\vspace{0pt}#1}
           }
        }
     }

     \def\bgaddcenter#1{%
         \bgadd{%
           \vfill
           \centering#1
           \vfill
         }
     }

     \def\bgclear{\ClearShipoutPicture}
  \fi
\fi



\def\hpagecolor{\@ifnextchar [{\bg@hpagecolorTwo}{\bg@hpagecolor}}%
\def\vpagecolor{\@ifnextchar [{\bg@vpagecolorTwo}{\bg@vpagecolor}}%


\ifbackground@ignore
   \ifx\pagecolor\undefined
     % a dummy definition
     \newcommand\pagecolor[1]{}%
     \message{Warning, pagecolor definitions ignored for non pdf processing}%
   \fi
   \def\bg@hpagecolorTwo[#1]#2{}%
   \def\bg@vpagecolorTwo[#1]#2{}%
   \def\bg@hpagecolor#1{}%
   \def\bg@vpagecolor#1{}%
\else
{\catcode`\%=11\catcode`\!=14 !make '!' the comment character for now
\gdef\pagecolor#1{\@ifundefined{\string\color @#1}!
  {\message{pagecolor: color #1 is undefined}}!
  { \pdfliteral{%mbackground \pppp@colortostring{#1}}}!
      }!
\gdef\bg@vpagecolorTwo[#1]#2{\@ifundefined{\string\color @#1}!
  {\message{vpagestripes: color #1 is undefined}}!
  {\@ifundefined{\string\color @#2}!
  {\message{vpagestripes: color #2 is undefined}}!
  {\pdfliteral{%gradbackground vt \pppp@colortostring{#1}/\pppp@colortostring{#2}}}}!
      }!
\gdef\bg@hpagecolorTwo[#1]#2{\@ifundefined{\string\color @#1}!
  {\message{pagestripes: color #1 is undefined}}!
  {\@ifundefined{\string\color @#2}!
  {\message{pagestripes: color #2 is undefined}}!
  {\pdfliteral{%gradbackground ht \pppp@colortostring{#1}/\pppp@colortostring{#2}}}}!
      }!
\gdef\bg@hpagecolor#1{\@ifundefined{\string\color @#1}!
  {\message{hpagecolor: color #1 is undefined}}!
  {\pdfliteral{%gradbackground ho \pppp@colortostring{#1}}}}!
\gdef\bg@vpagecolor#1{\@ifundefined{\string\color @#1}!
  {\message{vpagecolor: color #1 is undefined}}!
  {\pdfliteral{%gradbackground vo \pppp@colortostring{#1}}}}!
}%
\fi