% \iffalse meta-comment
%
% Copyright (C) 2012 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Sigitas Tolu\v{s}is.
%
% This work consists of the files boundbox.dtx and boundbox.ins
% and the derived filebase boundbox.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{boundbox.dtx}
%</driver>
%<*package>
%% Copyright (C) 1997-2012 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
%% VTeX Ltd., Akademijos 4, Vilnius, Lithuania
%% http://www.vtex.lt/tex/download/macros/
%% --------------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% PURPOSE: Calculate TeX bounding box in rounded points (LaTeX2e)
%%          from left top corner of the paper
%%
%</package>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{boundbox}
%<*package>
    [2012/05/29 v1.3 Calculate TeX bounding box in points]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{boundbox}[2012/05/29]
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{boundbox.dtx}
  %%\PrintChanges
  %%\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{186}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.3}{2012/05/29}{Source converted to DTX file}
% \changes{v1.3}{2012/05/29}{Added package option nospec}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{boundbox.dtx}
% \title{The \textsf{boundbox} package}
% \author{Sigitas Tolu\v{s}is \\ \url{sigitas@vtex.lt}}
% \date{\fileversion~from \filedate}
%
% \maketitle
%
% \section{Introduction}
%
% The package is used to calculate textbox coordinates on the page.
% I guess that the most useable part of this package can be internal
% macro \verb|\roundtexdim| for rounding pt values to int numbers.
%
% \section{Usage}
%
% Just add package to the tex file as: \verb|\usepackage{boundbox}|. 
% \\\medskip
% Rounding tex dimension values:
% 
% \newdimen\testdim \testdim=1in\verb|\newdimen\testdim|
%
% \verb|\testdim=1in \the\testdim ->|
% \the\testdim
%
% \medskip
% \roundtexdim{\testdim}\foo\verb|\roundtexdim{\testdim}\foo \number\foo->|\number\foo
%
% \medskip
% \roundtexdim floor{\testdim}\foo\verb|\roundtexdim floor{\testdim}\foo \number\foo->|\number\foo
%
% \medskip
% \roundtexdim ceil{\testdim}\foo\verb|\roundtexdim ceil{\testdim}\foo \number\foo->|\number\foo
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
% The package contains two options:
%
% \meta{nofoot} -- do not include foot to the bbox.
%
% \meta{nospec} -- do not write specials with bbox info.
%
%    \begin{macrocode}
%
%%
\newif\if@withfoot \@withfoottrue
\newif\if@output@bbox@special \@output@bbox@specialtrue
\DeclareOption{nofoot}{\global\@withfootfalse}
\DeclareOption{nospec}{\global\@output@bbox@specialfalse}
\ProcessOptions
%%
% 
%    \end{macrocode}
%
% Definitions of special prefixes. 
%
%    \begin{macrocode}
\def\special@bbox@odd@prefix{mt:TeXBBox:odd}
\def\special@bbox@even@prefix{mt:TeXBBox:even}
%    \end{macrocode}
%
% \begin{macro}{\roundtexdim}
% \marg{round type}\marg{tex dimension}\marg{command}
% 
% \meta{tex dimension} value in pt rounded with \meta{round type}
% is defined as \meta{command}.
%
%    \begin{macrocode}
%
%%
\def\roundtexdim#1#{\round@tex@dim{#1}}
\def\round@tex@dim#1#2#3{%
  \expandafter\expandafter\csname get@#1@pt@data\endcsname\the#2\to#3\relax
  }
%    \end{macrocode}
% \end{macro}
%
%
% Internal macro for calculating rounded value to the floor.
%
%
% \begin{macro}{\get@floor@pt@data}
%
%    \begin{macrocode}
\def\get@floor@pt@data#1.#2#3\to#4{%
  \bgroup
    \@tempcnta=#1\relax
    \xdef#4{\the\@tempcnta}%
  \egroup
  }
%    \end{macrocode}
% \end{macro}
%
%
% Internal macro for calculating rounded value by first number after point.
%
%
% \begin{macro}{\get@round@pt@data}
%
%    \begin{macrocode}
\def\get@round@pt@data#1.#2#3\to#4{%
  \bgroup
    \@tempcnta=#1\relax
    \ifnum#2>4\relax
      \advance\@tempcnta by\@ne
    \fi
    \xdef#4{\the\@tempcnta}%
  \egroup  
  }
\let\get@@pt@data\get@round@pt@data
%    \end{macrocode}
% \end{macro}
%
%
% Internal macro for calculating rounded value to the ceiling.
%
%
% \begin{macro}{\get@ceil@pt@data}
%
%    \begin{macrocode}
\def\get@ceil@pt@data#1.#2#3\to#4{%
  \bgroup
    \@tempcnta=#1\relax
    \afterassignment\@gobbletwo
    \@tempcntb=#2#3\relax
    \ifnum\@tempcntb>0\relax
      \advance\@tempcnta by\@ne
    \fi
    \xdef#4{\the\@tempcnta}%
  \egroup
  }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@calculate@bbox}
%
%  Internal macro \verb|\@calculate@bbox|  calculates TeX box dimensions: 
%
%  \begin{itemize}
%  \item \verb|\bbox@odd@top@pt| and \verb|\bbox@even@top@pt|:
%       distance in rounded ``floor'' points from the top of paper to the top of TeX box
%       (including head)  (the same for odd and even pages);
%  \item \verb|\bbox@odd@left@pt| and \verb|\bbox@even@left@pt|:
%       distance in rounded ``floor'' points from the left side of paper
%       to the left side of TeX box for odd and even pages;
%  \item \verb|\bbox@odd@right@pt| and \verb|\bbox@even@right@pt|:
%        distance in rounded ``ceil'' points from the left side of paper to the right
%        side of TeX box accordingly for odd and even pages;
%  \item \verb|\bbox@odd@bottom@pt| and \verb|\bbox@even@bottom@pt|:
%        distance in rounded ``ceil''  points from the top of paper to the bottom of TeX
%        box (including foot by default; with option "nofoot" without
%        foot) (the same for odd and even pages).
%  \end{itemize}
%
%    \begin{macrocode}
\def\@calculate@bbox{%
  \bgroup
    \@tempdima=1in\relax
    \advance\@tempdima by\hoffset
    \advance\@tempdima by\oddsidemargin
    \roundtexdim floor{\@tempdima}\bbox@odd@left@pt
    \advance\@tempdima\textwidth
    \roundtexdim ceil{\@tempdima}\bbox@odd@right@pt
    \@tempdima=1in\relax
    \advance\@tempdima by\hoffset
    \advance\@tempdima by\evensidemargin
    \roundtexdim floor{\@tempdima}\bbox@even@left@pt
    \advance\@tempdima\textwidth
    \roundtexdim ceil{\@tempdima}\bbox@even@right@pt
    \@tempdima=1in\relax
    \advance\@tempdima by\voffset
    \advance\@tempdima by\topmargin
    \roundtexdim floor{\@tempdima}\bbox@odd@top@pt
    \global\let\bbox@even@top@pt\bbox@odd@top@pt
    \advance\@tempdima by\headheight
    \advance\@tempdima by\headsep
    \advance\@tempdima\textheight
    \if@withfoot
      \advance\@tempdima\footskip
    \fi
    \roundtexdim ceil{\@tempdima}\bbox@odd@bottom@pt
    \global\let\bbox@even@bottom@pt\bbox@odd@bottom@pt
  \egroup
  }
%    \end{macrocode}
% \end{macro}
%
% Without package option \meta{nospec} two specials are written to the
% output file. Prefix of particular special is defined by:
% \verb|\special@bbox@odd@prefix| and \verb|\special@bbox@even@prefix|.
%
%    \begin{macrocode}
%
%%
\AtBeginDocument{%
  \@calculate@bbox
  \if@output@bbox@special
    \special{\special@bbox@odd@prefix\space
           left=\csname bbox@odd@left@pt\endcsname\space
           top=\csname bbox@odd@top@pt\endcsname\space 
           right=\csname bbox@odd@right@pt\endcsname\space 
           bottom=\csname bbox@odd@bottom@pt\endcsname
           }%
    \special{\special@bbox@even@prefix\space
           left=\csname bbox@even@left@pt\endcsname\space
           top=\csname bbox@even@top@pt\endcsname\space 
           right=\csname bbox@even@right@pt\endcsname\space 
           bottom=\csname bbox@even@bottom@pt\endcsname
           }%
  \fi
  }
\endinput
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
%
% \Finale
