% draftdoc.sty: display the word ``DRAFT'' on each page of a document.
% $Id: draftdoc.sty,v 1.1 1998/01/03 11:52:04 bert Exp $
%
% Idea derived from texdraft.ps by Jim Rees (1992)
% Implementation by Albert Dvornik <bert@mit.edu> (1997)

% This is designed to work with dvips; other formatters will likely barf.
% Assumptions about the universe:
% (1) /bop-hook is invoked just after the beginning of every page if
%     it's defined in userdict.
% (2) No other dvips hacks will override /bop-hook.  [We hope.]
% (3) Postscript symbols ``vsize'' and ``hsize'' refer to page width
%     and height in PostScript points (ie, TeX's ``bp'' units)
% (4) Sane things happen with comments inside \special{}.

% The following parameters may be redefined any time after \documentstyle

\def\drafttext{Draft}			% Text to be displayed.
\def\draftfont{Times-Roman}		% PS font to display it in.
\def\draftscaling{100}			% Nominal size (has effect only on
					%   fonts which do not scale linearly)
\def\draftangle{vsize hsize atan}	% Angle (PS expression or constant)
\def\draftgray{0.9}			% Level of grayness (higher==lighter)
\def\draftmargins{18}                   % Margins in PostScript points (bp's)

\newif\ifdraftoutline			% If true draw an outline, else fill
\draftoutlinefalse

% Automate setting all params at once

\def\setdraftparams#1#2#3#4#5{%
  \def\drafttext{#1}\def\draftfont{#2}%
  \def\draftangle{#3}\def\draftgray{#4}\def\draftmargins{#5}}

%%% The inclusion magic starts here.  No user-serviceable parts inside!

% TeX MAGIC:
% this makes sure \dr@ftmagic is invoked just before each page is shipped out.

\output=\expandafter{\expandafter\dr@ftmagic \the\output}

% \dr@ftmagic invokes \dr@ftspecial (q.v.) with appropriate arguments
% and makes further invocations of itself be no-ops.

\def\dr@ftmagic{%
  \dr@ftspecial{\drafttext}{\draftfont}{\draftscaling}{\draftangle}%
               {\draftgray}{\draftmargins}%
               {\ifdraftoutline false charpath stroke\else show\fi}%
  \global\def\dr@ftmagic{}}

% \dr@ftspecial{TEXT}{FONT}{SIZE}{ANGLE}{GRAY}{MARGIN} generates a \special
% with specified parameters.  Variables aren't used directly because that
% would cause spaces after variable reference to get gobbled.

\def\dr@ftspecial#1#2#3#4#5#6#7{
% No blank lines allowed inside \special (or TeX will insert \par)!
\special{!userdict /bop-hook {
gsave #5 setgray hsize 2 div vsize 2 div translate
/#2 findfont #3 scalefont setfont			%
newpath 0 0 moveto (#1) false charpath pathbbox	newpath	% l  d  r  u
4 copy							% ldru ldru
3 -1 roll sub 3 1 roll sub neg exch	% ldru  w=r-l  h=u-d
2 copy					% ldru  w  h  w  h
exch atan neg dup			% ldru  w  h  box-angle box-angle
#4 add					% ldru  w  h  box-angle diag-angle
cos exch cos div			% ldru  w  h  width-factor
hsize #6 2 mul sub exch div		% ldru  w  h  scaled-width
3 -1 roll div exch pop			% ldru  scaling
dup scale #4 rotate			% ldru
% center the text around origin
3 -1 roll add -2 div 3 1 roll add -2 div exch moveto
(#1) #7
grestore} put}}
