% This is a set of plain TeX macros for fast preparation of tables and forms.
% See below for an example of use
%
%   file:     frames.tex
%   version:  1.1
%   author:   Igor Skalski, Gdansk, Poland
%   email:    skalgo@cto.gda.pl
%   date:     14.03.2002
%   keywords: plain TeX, table, form
%   licence:  Public domain
%
% LIST OF COMMANDS:
%
%    \frameruleswidth<dimen>            % all-rules width
%                                       % use it before the \frames command
%    \frames                            % start frames
%    \leftrulewidth<dimen>              % left rule width
%    \rightrulewidth<dimen>             % right rule width
%    \toprulewidth<dimen>               % top rule width
%    \bottomrulewidth<dimen>            % bottom rule width
%    \frame{<width>}{<height>}{<text>}  % create the frame
%    \row                               % begin a new row
%    \endframes                         % end frames
%
% SIMPLE EXAMPLE:
%
%    \input frames.tex
%
%    \frameruleswidth=0.2pt
%    \frames
%    \frame{72mm}{7mm}{\centerline{columns 1, 2 and 3, row 1}}
%    \row
%    \multirow2\frame{24mm}{24mm}{\vfill column 1\par rows 2 and 3\vfill}
%    \frame{24mm}{12mm}{column 2\par row 2}
%    \frame{24mm}{12mm}{column 3\par row 2}
%    \row
%    \leftrulewidth1pt
%    \rightrulewidth1pt
%    \toprulewidth1pt
%    \bottomrulewidth1pt
%    \hskip24mm\frame{48mm}{12mm}{columns 2 and 3, row 3}
%    \endframes
%
%    \bye

\newdimen\frameruleswidth
\frameruleswidth-1pt
\newdimen\toprulewidth
\newdimen\bottomrulewidth
\newdimen\leftrulewidth
\newdimen\rightrulewidth

\catcode`@=11

\newbox\@cellbox
\newdimen\@cellswidth
\newdimen\@cellheight
\newdimen\@cellmaxheight
\newdimen\@newcellwidth
\newdimen\@newcellheight
\newcount\@multirowcount
\newdimen\@multirowdimen
\newdimen\@tableheight
\newdimen\@tablewidth
\newdimen\@tmpdimen

\long\def\frame#1#2#3
{
  \@newcellwidth#1
  \advance\@newcellwidth-\frameruleswidth
  \@tmpdimen\leftrulewidth
  \advance\@tmpdimen\rightrulewidth
  \advance\@tmpdimen-2\frameruleswidth
  \advance\@newcellwidth-\@tmpdimen
  \@newcellheight#2
  \@tmpdimen\toprulewidth
  \advance\@tmpdimen\bottomrulewidth
  \advance\@tmpdimen-2\frameruleswidth
  \advance\@newcellheight-\@tmpdimen
  \ifnum\@multirowcount=0
  \else
    \@multirowdimen\frameruleswidth
    \multiply\@multirowdimen by\@multirowcount
    \advance\@newcellheight\@multirowdimen
  \fi
  \setbox\@cellbox\vbox
  {\hrule height\toprulewidth\hbox{\vrule width\leftrulewidth
     \vtop to\@newcellheight{
     \leftskip2mm\rightskip2mm\hsize\@newcellwidth\vskip2mm
     {\parindent0pt\lineskip1pt\baselineskip1.2em{\par#3}\par}
     \vfil}\vrule width\rightrulewidth}\hrule height\bottomrulewidth}%
  \toprulewidth\frameruleswidth
  \bottomrulewidth\frameruleswidth
  \leftrulewidth\frameruleswidth
  \rightrulewidth\frameruleswidth
  \advance\@cellswidth\wd\@cellbox
  \advance\@cellswidth-\frameruleswidth
  \@cellheight\ht\@cellbox
  \ifnum\@multirowcount=0
    \ifnum\@cellheight>\@cellmaxheight\@cellmaxheight\@cellheight\fi
  \fi
  \@multirowcount0
  \leavevmode
  \box\@cellbox
  \vskip-\@cellheight
  \hskip\@cellswidth
}

\def\row
{
  \advance\@cellmaxheight-\frameruleswidth
  \vskip\@cellmaxheight
  \global\advance\@tableheight\@cellmaxheight
  \ifnum\@tablewidth<\@cellswidth\global\@tablewidth\@cellswidth\fi
  \@cellswidth0pt
  \@cellmaxheight0pt
}

\def\multirow#1%
{
  \@multirowcount#1
  \advance\@multirowcount-1%
}

\def\frames{
  \begingroup
  \par
  \tolerance10000
  \parindent0pt
  \baselineskip0pt plus0pt minus0pt
  \parskip0pt plus0pt minus0pt
  \lineskip0pt plus0pt minus0pt
  \@cellswidth0pt
  \@cellheight0pt
  \@cellmaxheight0pt
  \ifdim\frameruleswidth=-1pt
    \frameruleswidth.5pt
  \fi
  \toprulewidth\frameruleswidth
  \bottomrulewidth\frameruleswidth
  \leftrulewidth\frameruleswidth
  \rightrulewidth\frameruleswidth
  \@multirowcount0
  \@tableheight0pt
  \@tablewidth0pt
}

\def\endframes
{
  \row
  \endgroup
  \frameruleswidth-1pt
  \par
}

\catcode`@=12

\endinput
