%% boundbox.sty
%% Copyright 1997-2002 Sigitas Tolu\v sis
%% VTeX Ltd., Akademijos 4, Vilnius, Lithuania
%% e-mail sigitas@vtex.lt
%% http://www.vtex.lt/tex/download/macros/
%%
% This program can redistributed and/or modified under the terms
% of the LaTeX Project Public License Distributed from CTAN
% archives in directory macros/latex/base/lppl.txt; either
% version 1 of the License, or (at your option) any later version.
%
% PURPOSE:   Calculate TeX Bounding Box in points (LaTeX2e)
%
% SHORT DESCRIPTION:
%
% --- Package Options ---
%
% nofoot
% ------
%   excludes \footskip
%
% \@calculate
% -----------
%  Internal macro \@calculate  writes TeX box dimensions 
%  to the token list \bound in such format:
%
%        \bound{left=x1 top=y1 right=x2 bottom=y2}
%
%          x1 - distance in points from the left side of paper
%               to the left side of TeX box
%          y1 - distance in points from the top of paper
%               to the top of TeX box (including head)
%          x2 - distance in points from the left side of paper
%               to the right side of TeX box
%          y2 - distance in points from the top of paper
%               to the bottom of TeX box 
%		(including foot; with option "nofoot" without foot)
%
% \topoint#1#2
% ------------
%  Dimension #1 in any unit of measure converts to value in points
%  and defines it to macro #2
%
% This package writes such special to DVI:
%     \special{mt:TeXBBox left=x1 top=y1 right=x2 bottom=y2}
%
\def\get@CVSdate#1Id: #2,v #3 #4 #5 #6 #7${#4}%$
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{boundbox}[\get@CVSdate$Id: boundbox.sty,v 1.2 2002/06/11 10:18:43 sigitas Exp $]

\newif\if@withfoot \@withfoottrue
\DeclareOption{nofoot}{\global\@withfootfalse}
\ProcessOptions

\newcount\corr@d \corr@d=5\relax

\newtoks\bound \bound={}%
\def\@@to#1#2{\begingroup
  \xdef\a@act{\global\noexpand#1{\the#1#2}}\a@act
\endgroup}

\def\topoint#1#2{%
  \@tempdimb=#1\relax
  \@tempcnta=\@tempdimb
  \multiply\@tempcnta by10\relax
  \divide\@tempcnta by18647 \advance\@tempcnta by\@ne
  \multiply\@tempcnta by72 \divide\@tempcnta by2540\relax
  \expandafter\def\expandafter#2\expandafter{\the\@tempcnta}%
  }

\def\@calculate{%
  \@tempdima=1in\relax
  \advance\@tempdima by\hoffset
  \advance\@tempdima by\oddsidemargin
  \topoint{\@tempdima}{\@a}%
  \advance\@tempdima\textwidth
  \topoint{\@tempdima}{\@c}%
  \@tempdima=1in\relax
  \advance\@tempdima by\voffset
  \advance\@tempdima by\topmargin
  \topoint{\@tempdima}{\@b}%
  \advance\@tempdima by\headheight
  \advance\@tempdima by\headsep
  \advance\@tempdima\textheight
  \if@withfoot
    \advance\@tempdima\footskip
  \fi
  \topoint{\@tempdima}{\@d}%
  \@tempcnta=\@d \advance\@tempcnta by\corr@d
  \expandafter\def\expandafter\@d\expandafter{\the\@tempcnta}%
  \@@to\bound{left=\@a\space top=\@b\space right=\@c\space bottom=\@d}%
  }
\AtBeginDocument{\@calculate\special{mt:TeXBBox \the\bound}}
\endinput
