% border.tex
% Author: James A. Brister
% Mon Jan 28 19:43:19 1991
%
% Macros to put black borders around a page in PLAIN TeX. The border is
% laid over the top of the page as it would normally be. Changes to
% \hoffset and \voffset do not affect where the border goes.
%
% To turn borders on use the command \borderson, to turn them off use
% \bordersoff.
%
% Interesting dimensions are:
%
%	\borderindent	the distance from the outside edge of the border to
%			the paper edge. Default is 10 pts
%	\borderwidth	the width of the border. Default is 5pt
%	\borderspace	the (minimum) space between the inside edge of the 
%			border and the outside edge of the box containing 
%			all the page material. Default is 10pt. Not really 
%			useful in this context though, because it implies the 
%			internal box will be shifted, but the size
%			of the internal box is adjusted to allow for this 
%			dimension. The macro that uses it (boxit) is
%			useful, and borderspace can really be set to
%			anything inside a group .e.g.
%			{\borderwidth=5pt 
%                        \borderspace=5pt 
%                        \boxit{\hbox{fubar}}}
%			won't (ok, shouldn't) affect the output routine.
%	\paperheight	the height of the paper, default is 11 inches
%	\paperwidth	the width of the paper, default is 8.5 inches
%
% NOTE: If you change these dimensions you can get
% overfull box errors. To avoid this adjust \vsize, \hsize, \hoffset, and
% \voffset accordingly.
%
%

\newdimen\borderspace \borderspace=10pt
\newdimen\borderwidth \borderwidth=5pt
\newdimen\borderindent \borderindent=10pt
\newdimen\paperheight \paperheight=11in
\newdimen\paperwidth \paperwidth=8.5in


\newdimen\bigboxheight % height of final box inside border
\newdimen\bigboxwidth % width of same box
\newdimen\leftsidekern % distance between inside of border and left of page box
\newdimen\topsidekern % distance between inside of border and top of pagebox
\newdimen\boxadjust % distance from paper edge to inside edge of border space.

%
% put a border around a box. Taken from the TeX book. Border width is
% defined by \borderwidth. Distance out from box is \borderspace.
%
% This macro can be called in a page like so:
% {\borderwidth=5pt \borderspace=5pt \boxit{\hbox{fubar}}}
%

\def\boxit#1{\vbox{\hrule height \borderwidth depth 0pt
  \hbox{\vrule width \borderwidth\kern\borderspace
\vbox{\kern\borderspace#1\kern\borderspace}\kern\borderspace \vrule width
\borderwidth}\hrule height \borderwidth depth 0pt}}

%
% Wrap up the page in a border and ship it out.
%

\def\borderoutput{
  \setbox255=\vbox{\makeheadline
  \pagebody
  \makefootline}%
  \setbox255=\vbox{\makebigbox{\box255}}
  \begingroup
    \hoffset= -1in \advance\hoffset by \borderindent
    \voffset= -1in \advance\voffset by \borderindent
    \shipout\boxit{\box255}
  \endgroup
  \advancepageno
  \ifnum\outputpenalty>-20000\else\dosupereject\fi}

%
% make a big box to hold the page, that's big enough to wrap the border
% around.
%

\def\makebigbox#1{%
  \boxadjust=\borderindent
  \advance\boxadjust by \borderwidth
  \advance\boxadjust by \borderspace

  \bigboxheight=\paperheight
  \bigboxwidth=\paperwidth

  \advance\bigboxheight by -\boxadjust
  \advance\bigboxheight by -\boxadjust
  \advance\bigboxwidth by -\boxadjust
  \advance\bigboxwidth by -\boxadjust

  \leftsidekern=\hoffset \advance\leftsidekern by 1in
  \advance\leftsidekern by -\boxadjust
  \topsidekern=\voffset \advance\topsidekern by 1in
  \advance\topsidekern by -\boxadjust

  \hbox to \bigboxwidth{\kern\leftsidekern
    \vbox to \bigboxheight{\kern\topsidekern#1\vfil}
    \hfil}}

\def\borderson{\output={\borderoutput}}
\def\bordersoff{\output={\plainoutput}} % reset to TeX's standard output
