% dimpic.sty v1.1 2005/04/12
%
% Copyright 2005 Stephan Otto
%
% Contact software@stephan-otto.net
%
% 
% Package dimpic
% ==============
%
%
% Description
% -----------
%
% This package allows to use the LaTeX 'picture' environment by means
% of dimensions instead of coordinates. It is very incomplete in that
% only the 'put' command is translated, but provides a simple generic 
% interface for using and translating every other 'picture' command.
%
%
% License
% -------
%
% This file 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 2003/12/01 or
% later.
%
%
% Installation
% ------------
%
% Put this file somewhere TeX can find it. See http://faq.tug.org if
% you have questions.
%
%
% Usage
% -----
%
% Include this package with
%
%   \usepackage[<options>]{dimpic}
%
% where possible <options> are
%
%   'calc': use package 'calc' to parse dimensions
%   'setul': set dimension \unitlength to 1sp
%
% A regular 'picture' environment is created between
%
%  \begin{dimpicture}{<width>}{<height>}
%  ...
%  \end{dimpicture}
%
% where <width> and <height> are the picture dimensions. In this
% environment, you will most probably call
%
%  \dimput{<width>}{<height>}{<command>}
%
% to have the output of <command> positioned at dimensions <width> and
% <height> from the picture origin. If you wish to use a 'picture'
% command other than 'put', simply call
%
%  \makexy{<width>}{<height>}
%
% to have the required coordinates stored in the counters \x and \y. 
% \makexy uses the more basic command
%
%  \makenumber{<command>}{<dimension>}
%
% which stores the coordinate of <dimension> in <command>. For your
% convenience, the command \zero is defined as 0sp. 
%
%

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{dimpic}

\let\@mncalc0
\let\@mnsetul0

\DeclareOption{calc}{
  \let\@mncalc1
}
\DeclareOption{setul}{
  \let\@mnsetul1
}

\ProcessOptions

\if\@mncalc1
  \RequirePackage{calc}
\fi

\newcount{\x}
\newcount{\y}

\newcommand{\mnset@dima}[1]{%
  \if\@mncalc1%
    \setlength{\@tempdima}{#1}%
  \else%
    \@tempdima=#1%
  \fi%
}  

\newcommand{\mncheck@dima}{%
  \ifdim \@tempdima>\zero%
    \ifdim \@tempdima<1000\unitlength%
      \PackageWarning{dimpic}{%
        Length \the\@tempdima\space is too small\MessageBreak%
        for unit length \the\unitlength%
      }%
    \fi%
  \fi%
}

\newcommand{\makenumber}[2]{%
  \mnset@dima{#2}%
  \@tempcnta=\@tempdima%
  \@tempcntb=\unitlength%
  \ifnum \@tempcntb=0%
    \PackageError{dimpic}{%
      Unit length \the\unitlength\space is too small%
    }{%
      Set \string\unitlength\space to at least 1sp %
      or use option 'setul'!%
    }%
  \fi%
  \divide \@tempcnta by \@tempcntb%
  \global#1=\@tempcnta%
}

\newcommand{\makexy}[2]{%
  \makenumber{\x}{#1}%
  \makenumber{\y}{#2}%
}

\newenvironment{dimpicture}[2]{%
  \if\@mnsetul1%
    \unitlength=1sp%
  \fi%
  \mnset@dima{#1}%
  \mncheck@dima%
  \mnset@dima{#2}%
  \mncheck@dima%
  \makexy{#1}{#2}%
  \begin{picture}(\x,\y)%
}{%
  \end{picture}%
}

\newcommand{\dimput}[3]{%
  \makexy{#1}{#2}%
  \put(\x,\y){#3}%
}

\newcommand{\zero}{0sp}

