%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% bgimages.sty  v1.0  Monday, January 8, 2000
%%
%%======================================================================
%% Copyright 2001 Ross R. Moore  (ross@maths.mq.edu.au)
%% All rights reserved.
%%======================================================================
%%   This file is distributed in the hope that it will be useful,
%%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%%
%% IMPORTANT COPYRIGHT NOTICE:
%%
%% You are allowed to distribute this file alone.
%%
%% Permission is granted to copy this file to another file with a
%% clearly different name and to customize the declarations in that
%% copy to serve the needs of your installation, provided that you
%% comply with the conditions in the file legal.txt from the LaTeX2e
%% distribution.
%%
%% However, NO PERMISSION is granted to generate or to distribute a
%% modified version of this file under its original name.
%%
%% You are NOT ALLOWED to change this file.
%%
%% MODIFICATION ADVICE:
%%
%% If you want to customize this file, use a different name for your
%% copy(ies) and modify the copy(ies); this will ensure that your
%% modifications do not get overwritten when you install a
%% new release.
%%
%% You can then easily distribute your modifications by distributing
%% the modified and renamed copy of the source file, taking care to
%% observe the conditions in legal.txt; this will ensure that other
%% users can safely use your modifications.
%%
%% Please report errors to:  Ross R Moore <ross@maths.mq.edu.au>
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% USAGE:
%% defines new commands as follows:
%%
%%    \backgroundimage [<adjust>] {<filename>}
%%    \scaledbgimage [<loc>] {<scale>} {<filename>}
%%
%%        include <filename> as a background image, using the specified
%%        <adjust>, <scale> and/or <loc> codes to determine the size 
%%        and alignment/location of the image (see below).
%%
%%        The <filename> will be an argumernt for \includegraphics .
%%        Multiple images may be shown simultaneously.
%%
%%    \clearbgimages
%%       remove all background images
%%
%%    \pausebgimages
%%       temporarily stop showing background images
%%
%%    \unpausebgimages
%%       restart showing background images, after \pausebgimages
%%
%%    <adjust> = V | Vl | Vr  : scale to fill Vert height;  align c,l,r
%%             | H | Ht | Hb  : scale to fill Horiz width;  align m,t,b 
%%             | VH | HV      : scale to fill both height and width
%%        (in all but the last 2 options, the aspect-ratio is retained)
%%
%%    <loc>  =  l | r | t | b      : position at center or stated edge
%%             | lt | lb | rt | rb : or into the specified corner
%%             | tl | bl | tr | tb :  (order is immaterial)
%%
%%    <scale> =  <num>             : arbitrary scale-factor...
%%             | <num>,<options>   :  ...followed by any sequence of 
%%                                      options to \includegraphics
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\fileversion{1.0}
\def\filedate{2001/01/08}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage%
{bgimages}%
[\filedate\space bgimages.sty  v\fileversion]
%=======================================================================
% LOAD  PACKAGES
% 
% requies  everyshi.sty  and its sub-package  eso-pic.sty
%
\newif\ifwithbgimage
\IfFileExists{eso-pic.sty}
 {\withbgimagetrue\RequirePackage{eso-pic}}
 {\withbgimagefalse}

%=======================================================================
% ALLOCATE  height/width registers, and delay setting them
%
  % height available on the page for background images
\newdimen\bgimage@height  
  % total width available on the page for background images
\newdimen\bgimage@width

% calculate these  width/height values when appropriate info is known
\AtBeginDocument{\global\bgimage@height=\textheight 
 \global\advance\bgimage@height\footskip\relax}
\AtBeginDocument{\global\bgimage@width=\textwidth 
  \ifx\insidemargin\@empty\else\global\advance\bgimage@width by\insidemargin\relax\fi
  \ifx\insidemargin\@empty\else\global\advance\bgimage@width by\insidemargin\relax\fi}

%
% make sure \printmargin and \insidemargin are defined,
% even if just the default value of \@empty
%
\expandafter\ifx\csname printmargin\endcsname\relax
  \let\printmargin\@empty \fi
\expandafter\ifx\csname insidemargin\endcsname\relax
  \let\insidemargin\@empty \fi

% set the appropriate width/height 
\AtBeginDocument{\global\bgimage@height=\textheight 
 \global\advance\bgimage@height\footskip\relax}
\AtBeginDocument{\global\bgimage@width=\textwidth 
  \ifx\insidemargin\@empty\else\global\advance\bgimage@width by\insidemargin\relax\fi
  \ifx\insidemargin\@empty\else\global\advance\bgimage@width by\insidemargin\relax\fi}

%=======================================================================
%% user-level commands
%%
\newcommand{\backgroundimage}{%
  \ifwithbgimage\expandafter\backgroundimage@
  \else\bgimage@error\fi}
\newcommand{\scaledbgimage}{%
  \ifwithbgimage\expandafter\scaledbgimage@
  \else\scaledbgimage@error\fi}
\newcommand{\backgroundvbox}{\backgroundvbox@}%
\newcommand{\backgroundhbox}{\backgroundhbox@}%
\newcommand{\scaledbgvbox}{\scaledbgvbox@}%
\newcommand{\scaledbghbox}{\scaledbghbox@}%
\newcommand{\clearbgimages}{\ifwithbgimage\ClearShipoutPicture\fi}
\newcommand{\pausebgimages}{%
 \let\addbgimage@\pause@bgimage
 \let\addbghbox@\pause@bgimage
 \let\addbgvbox@\pause@bgimage
}
\newcommand{\unpausebgimages}{%
 \let\addbgimage@\addbgimage@@
 \let\addbghbox@\addbghbox@@
 \let\addbgvbox@\addbgvbox@@
}

%=======================================================================
%% error messages 
%%
\newcommand{\bgimage@error}[2][]{\bgimage@message}
\newcommand{\scaledbgimage@error}[3][]{\bgimage@message}
\newcommand{\bgimage@message}{%
 \errmessage{ *** background images using \string\backgroundimage\spac
 or \string\scaledbgimage\space^^J
 *** require the 'bgimage' option to the 'marslide' package^^J
 *** which needs the 'everyshi' and 'eso-pic' packages
 to be available.^^J}}

%=======================================================================
%% lower-level commands, interpreting the optional arguments
%%
\newcommand{\backgroundimage@}{%
 \def\bg@object{\addbgimage@i}\bgroup\backgroundimage@i}
\newcommand{\backgroundvbox@}{%
 \def\bg@object{\addbgvbox@i}\bgroup\backgroundimage@i}%
\newcommand{\backgroundhbox@}{%
 \def\bg@object{\addbghbox@i}\bgroup\backgroundimage@i}%

\newcommand{\backgroundimage@i}[1][]{\def\bgoption{#1}%
 \def\tmp{V}\ifx\tmp\bgoption\aftergroup\bgVimage\else
 \def\tmp{Vl}\ifx\tmp\bgoption\aftergroup\bgVLimage\else
 \def\tmp{Vr}\ifx\tmp\bgoption\aftergroup\bgVRimage\else
 \def\tmp{H}\ifx\tmp\bgoption\aftergroup\bgHimage\else
 \def\tmp{Ht}\ifx\tmp\bgoption\aftergroup\bgHTimage\else
 \def\tmp{Hb}\ifx\tmp\bgoption\aftergroup\bgHBimage\else
 \def\tmp{VH}\ifx\tmp\bgoption\aftergroup\bgVHimage\else
 \def\tmp{HV}\ifx\tmp\bgoption\aftergroup\bgVHimage\else
 \def\tmp{l}\ifx\tmp\bgoption\aftergroup\bgPLimage\else
 \def\tmp{r}\ifx\tmp\bgoption\aftergroup\bgPRimage\else
 \def\tmp{t}\ifx\tmp\bgoption\aftergroup\bgPTimage\else
 \def\tmp{b}\ifx\tmp\bgoption\aftergroup\bgPBimage\else
 \def\tmp{}\ifx\tmp\bgoption\aftergroup\bgPlainImage\else
 \aftergroup\bgPlainImage
 \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\egroup
}

\newcommand{\scaledbgimage@}{%
 \def\bg@object{\addbgimage@i}\bgroup\scaledbgimage@i}%
\newcommand{\scaledbgvbox@}{%
 \def\bg@object{\addbgvbox@i}\bgroup\scaledbgimage@i}%
\newcommand{\scaledbghbox@}{%
 \def\bg@object{\addbghbox@i}\bgroup\scaledbgimage@i}%

\newcommand{\scaledbgimage@i}[1][]{\def\bgoption{#1}%
 \def\tmp{l}\ifx\tmp\bgoption\aftergroup\bgSLimage\else
 \def\tmp{r}\ifx\tmp\bgoption\aftergroup\bgSRimage\else
 \def\tmp{t}\ifx\tmp\bgoption\aftergroup\bgSTimage\else
 \def\tmp{b}\ifx\tmp\bgoption\aftergroup\bgSBimage\else
 \def\tmp{lt}\ifx\tmp\bgoption\aftergroup\bgLTimage\else
 \def\tmp{tl}\ifx\tmp\bgoption\aftergroup\bgLTimage\else
 \def\tmp{rt}\ifx\tmp\bgoption\aftergroup\bgRTimage\else
 \def\tmp{tr}\ifx\tmp\bgoption\aftergroup\bgRTimage\else
 \def\tmp{lb}\ifx\tmp\bgoption\aftergroup\bgLBimage\else
 \def\tmp{bl}\ifx\tmp\bgoption\aftergroup\bgLBimage\else
 \def\tmp{rb}\ifx\tmp\bgoption\aftergroup\bgRBimage\else
 \def\tmp{br}\ifx\tmp\bgoption\aftergroup\bgRBimage\else
 \def\tmp{}\ifx\tmp\bgoption\aftergroup\bgScaledImage\else
 \aftergroup\bgScaledImage
 \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\egroup
}

% scaled vertically, centered horizontally
\newcommand{\bgVimage}{\bgVimage@{}{}{}{}}
% scaled vertically, left/right-aligned
\newcommand{\bgVLimage}{\bgVimage@{}{}{}\hfill}
\newcommand{\bgVRimage}{\bgVimage@{}{}\hfill{}}

\newcommand{\bgVimage@}[4]{% scaled vertically
%  \addbgimage@i
 \bg@object
  {to\bgimage@height}{#1}{#2}%
  {to\bgimage@width}{#3}{#4}%
  {[height=\bgimage@height]}{V}%
}

% scaled horizontally, centered vertically
\newcommand{\bgHimage}{\bgHimage@{}{}{}{}}
% scaled horizontally, top/bottom-aligned
\newcommand{\bgHTimage}{\bgHimage@{}\vfill{}{}}
\newcommand{\bgHBimage}{\bgHimage@\vfill{}{}{}}

\newcommand{\bgHimage@}[4]{% scaled horizontally
%  \addbgimage@i
 \bg@object
  {to\bgimage@height}{#1}{#2}%
  {to\bgimage@width}{#3}{#4}%
  {[width=\bgimage@width]}{H}%
}

\newcommand{\bgVHimage}{% scaled vertically, centered horizontally
%  \addbgimage@i
 \bg@object
  {to\bgimage@height}{}{}%
  {to\bgimage@width}{}{}%
  {[height=\bgimage@height,width=\bgimage@width]}{B}%
}
\newcommand{\bgPlainImage}{\bgPlainImage@{}{}{}{}{}1}%
\newcommand{\bgPLimage}{\bgPlainImage@{}{}{}\hfill{}1}%
\newcommand{\bgPRimage}{\bgPlainImage@{}{}\hfill{}{}1}%
\newcommand{\bgPTimage}{\bgPlainImage@{}\vfill{}{}{}1}%
\newcommand{\bgPBimage}{\bgPlainImage@\vfill{}{}{}{}1}%
\newcommand{\bgScaledImage}{\bgPlainImage@{}{}{}{}{}}%
\newcommand{\bgSLimage}{\bgPlainImage@{}{}{}\hfill{}}%
\newcommand{\bgSRimage}{\bgPlainImage@{}{}\hfill{}{}}%
\newcommand{\bgSTimage}{\bgPlainImage@{}\vfill{}{}{}}%
\newcommand{\bgSBimage}{\bgPlainImage@\vfill{}{}{}{}}%
\newcommand{\bgLTimage}{\bgPlainImage@{}\vfill{}\hfill{}}%
\newcommand{\bgRTimage}{\bgPlainImage@{}\vfill\hfill{}{}}%
\newcommand{\bgLBimage}{\bgPlainImage@\vfill{}{}\hfill{}}%
\newcommand{\bgRBimage}{\bgPlainImage@\vfill{}\hfill{}{}}%

\newcommand{\bgPlainImage@}[6]{% natural/scaled size
%  \addbgimage@i
 \bg@object
   {to\bgimage@height}{#1}{#2}%
%   {to\bgimage@width}{#3}{#4}{[scale=#5]}%
   {to\bgimage@width}{#3}{#4}{#5}{#6}%
}

%=======================================================================
%% the basic low-level command  \addbgimage@
%% writes \addbgimage@{#1}{#2}...{#9} into \@shipouttoks
%%
\newcommand{\addbgimage@i}[9]{%
 \AddToShipoutPicture{\addbgimage@{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}}

\newcommand{\addbghbox@i}[9]{%
 \AddToShipoutPicture{\addbghbox@{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}}

\newcommand{\addbgvbox@i}[9]{%
 \AddToShipoutPicture{\addbgvbox@{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}}

%=======================================================================
%% the low-level command  \addbgimage@@  is a binding for \addbgimage@
%% which does all the work...
%%
\newcommand{\addbgimage@@}[9]{\bgroup\setbox0=\hbox{%
% \bgimage@height=\textheight \advance\bgimage@height\footskip\relax
 \def\tmp{#7}\ifx\tmp\@empty\def\tmp{[scale=#8]}\fi
 \ifx\printmargin\@empty\else\raise\printmargin\relax\fi
 \vbox#1{#2\vss %\bgimage@width=\textwidth 
  %\ifx\insidemargin\@empty\else\advance\bgimage@width by\insidemargin\relax\fi
  %\ifx\insidemargin\@empty\else\advance\bgimage@width by\insidemargin\relax\fi
  \hbox to\paperwidth{\hfil\hbox to\bgimage@width{\hss
   \hbox#4{#5\hss\expandafter\includegraphics\tmp{#9}#6\hss}\hss}\hfil}%
    #3\vss}}\wd0\z@ \ht0\z@ \dp0\z@ \box0\egroup}%

\def\choose@bg@widthheight#1{%
 \def\tmp{#1}\ifx\tmp\@empty
 \else \def\test{H}\ifx\tmp\test
   \def\tmp{\resizebox{\bgimage@width}{!}}%
 \else \def\test{V}\ifx\tmp\test
   \def\tmp{\resizebox{!}{\bgimage@height}}%
 \else \def\test{B}\ifx\tmp\test
   \def\tmp{\resizebox*{\bgimage@width}{\bgimage@height}}%
 \else
   \def\tmp{\scalebox{#1}}%
 \fi\fi\fi\fi
}
\newcommand{\addbghbox@@}[9]{\bgroup\setbox0=\hbox{%
 \choose@bg@widthheight{#8}% defines \tmp
 \ifx\printmargin\@empty\else\raise\printmargin\relax\fi
 \vbox#1{#2\vss
  \hbox to\paperwidth{\hfil\hbox to\bgimage@width{\hss
   \hbox#4{#5\hss\hbox{\tmp{#9}}#6\hss}\hss}\hfil}%
    #3\vss}}\wd0\z@ \ht0\z@ \dp0\z@ \box0\egroup}%

\newcommand{\addbgvbox@@}[9]{\bgroup\setbox0=\hbox{%
 \choose@bg@widthheight{#8}% defines \tmp
 \ifx\printmargin\@empty\else\raise\printmargin\relax\fi
 \vbox#1{#2\vss
  \hbox to\paperwidth{\hfil\hbox to\bgimage@width{\hss
   \setbox0=\hbox#4{#5\hss\tmp{\begin{tabular}{p{.25\textwidth}}#9\end{tabular}%
    }#6\hss}\typeout{ht=\the\ht0,dp=\the\dp0}\box0%
  \hss}\hfil}#3\vss}}\wd0\z@ \ht0\z@ \dp0\z@ \box0\egroup}%

%% ...but it can be switched off using \pausebgimage
%% which just rebinds  \addbgimage@ to the following:
\newcommand{\pause@bgimage}[9]{}

% initially make the binding active
\unpausebgimages


\endinput
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
