% \iffalse [this is a meta-comment for a -*- LaTeX -*- package source]
%
%  draftdoc: package to display the word ``Draft'' on every page of a document.
%  This file is designed to work with dvips, and will probably fail with
%  other formatters.
%% $Id: draftdoc.dtx,v 1.1 1998/02/20 02:50:20 bert Exp bert $
%%
%  Copyright (c) 1997--1998, Albert Dvornik <bert@mit.edu>.
%  Idea derived from texdraft.ps by Jim Rees, 1992.
%
%  You are permitted to use, copy, distribute and modify this package
%  under the following conditions:
%    * This and other copyright notices must be left intact.
%    * You must distribute both ``draftdoc.dtx'' and ``draftdoc.ins''.
%    * If you modify any file, you *must* change the file name and
%      document the changes.
%    * You may not charge for it in any way, shape or form.
%
%  If you use this and like it, please send an e-mail note to <bert@mit.edu>.
% \fi
%
% \iffalse ***** title information ***** \fi
%
% \title{The \texttt{draftdoc} package for \LaTeXe\ and \texttt{dvips}}
% \author{Albert Dvornik, \texttt{<bert@mit.edu>}}
% \date{Version 0.10b\\ February 14, 1998}
% \maketitle
% \tableofcontents
%
% \begin{center}\bf
% This documentation is still essentially non-existent.  (I should fix that.)
% \end{center}
%
% \section{Introduction}
%
% \section{Source code}
%
% \iffalse ***** please ignore the man hiding behind the curtain. ***** \fi
%
% {\catcode`\@=\catcode`\\ \catcode`\\=\catcode`\a
%  @global@def@bsl#1{@texttt{\#1}}}
%
% \iffalse
% \subsection{Driver placement hack (start)}
%
% \newcommand{\IF}{\bsl{if}}
% \newcommand{\IFFALSE}{\bsl{iffalse}}
% \newcommand{\FI}{\bsl{fi}}
%
% This is a rather ugly hack that enables us to put the driver at the
% end of this file, and thus at the end of the documentation.  In all
% likelyhood, you don't need to know about it, so it isn't included in
% the normally generated documentation (by virtue of being surrounded
% by an {\IFFALSE--\FI} pair).
%
%    \begin{macrocode}
%<*hack_to_put_the_driver_at_the_end>
\iffalse
% \fi
%</hack_to_put_the_driver_at_the_end>
%    \end{macrocode}
%
% The {\IFFALSE} prevents the uncommented package source code from
% being included when the \verb|draftdoc.dtx| file is being processed
% by \LaTeX.  This would be unnecessary if I were willing to place the
% driver \emph{before} the source code in the documentation{\ldots} but
% I'm not.
%
% Since this is not normally included in the documentation, we have an
% additional problem: the {\IFFALSE} will confuse \LaTeX's normal
% {\IF--\FI} matching mechanism when this file is being processed for
% documentation.  The ``\texttt{\% \FI}'' fixes this: it will balance
% the {\IFFALSE} when documentation is being produced (and this section
% is {\IFFALSE}'d out so that \texttt{macrocode} isn't doing its job),
% but not when it is being first eaten by LaTeX.
% \fi
%
% \subsection{Preamble}
%
% \iffalse ***** Require LaTeX 2e (any date) ***** \fi
%
% This package requires \LaTeXe.
% It's unclear what is the earliest version that will work, so we don't
% specify the earliest allowed version.  If you notice a version of
% \LaTeXe\ this \emph{doesn't} work with, please let me know.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
%    \end{macrocode}
%
% \iffalse ***** Specify package ***** \fi

% We also specify the name of the package, and a message to be
% displayed when it is invoked:
%    \begin{macrocode}
\ProvidesPackage{draftdoc}[1998/02/14 v0.10b <bert@mit.edu>]
%    \end{macrocode}
%
% \iffalse ***** Specify modifiable parameters ***** \fi
% \subsection{Parameters}
%
% \newcommand{\brk}{\discretionary{}{}{}}
%
% There are several parameters that may be changed either using a
% command, by package options,\footnote{Package options are the
% arguments specified between the \texttt{[]}'s on the
% \bsl{usepackage\brk[\textrm{\emph{options}}]\brk\{draftdoc\}} line.}
% or both.
%
% \subsubsection{Banner text}
% The text printed across the page defaults to ``Draft''.  This can be
% changed by specifying \verb+\drafttext{+\emph{new-text}\verb+}+, or
% by specifying the \texttt{text=}\emph{new-text} option to the
% package.  Please note that the option may not contain whitespace; to
% specify a banner containing whitespace, use \verb+\drafttext+.
%    \begin{macrocode}
\newcommand\dr@fttext{Draft}
\newcommand\drafttext[1]{\def\dr@fttext{#1}}
%    \end{macrocode}
% (The option is handled by \verb+\DeclareOption*+, below.)
%
% \subsubsection{Filled vs. outlined characters}
%    \begin{macrocode}
\newcommand\dr@ftshow{}
\DeclareOption{outline}%
  {\draftgray{.85}\def\dr@ftshow{false charpath stroke}}
\DeclareOption{fill}%
  {\draftgray{.95}\def\dr@ftshow{show}}
%
\newcommand\dr@ftfont{}
\newcommand\draftfont[1]{\def\dr@ftfont{#1}}
\DeclareOption{times}{\draftfont{Times-Roman}}
\DeclareOption{times-italic}{\draftfont{Times-Italic}}
\DeclareOption{helvetica}{\draftfont{Helvetica}}
\DeclareOption{helvetica-bold}{\draftfont{Helvetica-Bold}}
\DeclareOption{ncs}{\draftfont{NewCenturySchlbk}}
%
\newcommand\dr@ftangle{0}
\newcommand\draftangle[1]{\def\dr@ftangle{#1}}
\DeclareOption{45deg}{\draftangle{45}}
\DeclareOption{diagonal}{\draftangle{vsize hsize atan}}
%    \end{macrocode}
%
% Nominal size (has effect only on fonts which do not scale linearly)
%    \begin{macrocode}
\newcommand\dr@ftscaling{100}
\newcommand\draftscaling[1]{\def\dr@ftscaling{#1}}
%    \end{macrocode}
%
% Level of grayness (higher==lighter)
%    \begin{macrocode}
\newcommand\dr@ftgray{0.9}
\newcommand\draftgray[1]{\def\dr@ftgray{#1}}
%
\newcommand\dr@ftmargins{18}
\newcommand\draftmargins[1]{\def\dr@ftmargins{#1}}
\DeclareOption{wide}{\draftmargins{18}}
\DeclareOption{narrow}{\draftmargins{72}}
%
\newcommand\dr@fttwoside{}
\DeclareOption{twoside}%
  {\def\dr@fttwoside{page-parity mul exch}}
\DeclareOption{noflip}%
  {\def\dr@fttwoside{}}
%
\def\draft@noopt#1,{\PackageError{draftdoc}%
  {Unknown option `\CurrentOption'}%
  {You specified `\CurrentOption' as a local option to the `draftdoc'
   package.\MessageBreak This option isn't declared; did you spell its
   name right?\MessageBreak You can try typing \space <return> \space
   to ignore this option and continue.}}
\def\draft@novalue,{\PackageError{draftdoc}%
  {Missing value for option `\CurrentOption'}%
  {You specified `\CurrentOption' as a local option to the `draftdoc'
   package.\MessageBreak This option requires an argument, as in
   `\CurrentOption=<some data>'.\MessageBreak You can try typing \space
   <return> \space to ignore this option and continue.}}
\def\draft@assign#1={%
	\@ifundefined{draft#1}{\draft@noopt}{\draft@checkarg{draft#1}}}
\def\draft@checkarg#1{\@ifnextchar,{\draft@novalue}{\draft@set{#1}}}
\def\draft@set#1#2=,{\@nameuse{#1}{#2}}
\DeclareOption*{\expandafter\draft@assign\CurrentOption=,}
%
\ExecuteOptions{times,wide,diagonal,fill,noflip}
\ProcessOptions\relax
%    \end{macrocode}
%
% When the document starts, invoke \verb+\dr@ftspecial+ (q.v.), and
% make further invocations of setting commands be an error.
%    \begin{macrocode}
\AtBeginDocument{\begingroup \def\ { }\dr@ftspecial\endgroup}
%    \end{macrocode}
%
% \verb+\dr@ftspecial+ generates a \verb+\special+ with specified parameters.
%
% Assumptions about the (PostScript) universe:
% \begin{enumerate}
% \item \verb+/bop-hook+ is invoked just after the beginning of every
%       page if it's defined in userdict.  The top item on the stack
%       when \verb+/bop-hook+ is invoked is the physical page number,
%       which is numbered starting from zero.
% \item No other dvips hacks will override \verb+/bop-hook+.  [We hope.]
% \item Postscript symbols \texttt{vsize} and \texttt{hsize} refer to
%       page width and height in PostScript points (ie, \TeX's ``bp'' units).
% \end{enumerate}
% All of these assumptions are true for dvipsk 5.66a.
%
% It is worth remembering that no blank lines should occur inside a
% \verb+\special+.  (\TeX\ would replace them with a \verb+\par+ in the
% body of the special!)
%    \begin{macrocode}
\def\dr@ftspecial{%
  \special{!%
    userdict /bop-hook {
%    \end{macrocode}
%
%    \begin{macrocode}
    gsave \dr@ftgray\ setgray hsize 2 div vsize 2 div translate
%    \end{macrocode}
%
%    \begin{macrocode}
    dup 2 mod 2 mul 1 exch sub /page-parity exch def
%    \end{macrocode}
%
%    \begin{macrocode}
    /\dr@ftfont\ findfont \dr@ftscaling\ scalefont setfont
%    \end{macrocode}
%
%    \begin{macrocode}
    newpath 0 0 moveto (\dr@fttext) false charpath pathbbox	newpath	
%    \end{macrocode}
% l  d  r  u
%    \begin{macrocode}
    4 copy								
%    \end{macrocode}
% l d r u l d r u
%    \begin{macrocode}
    3 -1 roll sub 3 1 roll sub neg exch	
%    \end{macrocode}
% l d r u w=r-l  h=u-d
%    \begin{macrocode}
    2 copy					
%    \end{macrocode}
% l d r u w  h  w  h
%    \begin{macrocode}
    exch atan neg dup			
%    \end{macrocode}
% l d r u w  h  box-angle box-angle
%    \begin{macrocode}
    \dr@ftangle\ add			
%    \end{macrocode}
% l d r u w  h  box-angle diag-angle
%    \begin{macrocode}
    cos exch cos div			
%    \end{macrocode}
% l d r u w  h  width-factor
%    \begin{macrocode}
    hsize \dr@ftmargins\ 2 mul sub exch div	
%    \end{macrocode}
% l d r u w  h  scaled-width
%    \begin{macrocode}
    3 -1 roll div exch pop			
%    \end{macrocode}
% l d r u scaling
%    \begin{macrocode}
    dup \dr@fttwoside\ scale		
%    \end{macrocode}
% l d r u
%    \begin{macrocode}
    \dr@ftangle\ rotate			
%    \end{macrocode}
% l d r u
%
% {\tt\em \verb+\dr@ftangle+}
%
% Center the text around origin
%    \begin{macrocode}
    3 -1 roll add -2 div 3 1 roll add -2 div exch moveto
    (\dr@fttext) \dr@ftshow\
    grestore} put}}
%    \end{macrocode}
%
% \iffalse ***** oh, look, there is a man hiding behind the curtain! ***** \fi
%
% \iffalse [The explanation of this is included above!]
%<*hack_to_put_the_driver_at_the_end>
% \iffalse
\fi
%</hack_to_put_the_driver_at_the_end>
% \fi
%
% \iffalse ***** documentation generation ***** \fi
%
% \appendix{The driver for this documentation}
%
%    \begin{macrocode}
%<*docstrip>
\begingroup
\input docstrip
\preamble
You are permitted to use, copy, distribute and modify this package
under the following conditions:
 * This and other copyright notices must be left intact.
 * You must distribute both ``draftdoc.dtx'' and ``draftdoc.ins''.
 * If you modify any file, you *must* change the file name and
   document the changes.
 * You may not charge for it in any way, shape or form.
If you use this and like it, please send an e-mail note to <bert@mit.edu>.
\endpreamble\keepsilent\Msg{*** Generating `draftdoc' package style ***}
\generate{\file{draftdoc.sty}{\from{draftdoc.dtx}{draftdoc,dvips}}}
\ifToplevel{
\Msg{***********************************************************}
\Msg{* To finish the installation you have to move the file}
\Msg{* `draftdoc.sty' into a directory searched by TeX.}
\Msg{***********************************************************}
}
\endgroup
%</docstrip>
%<*driver>
\message{*** Generating documentation for `draftdoc' ***}
\documentclass{ltxdoc}
\usepackage{verbatim}
\begin{document}
\DocInput{draftdoc.dtx}
\end{document}
%</driver>
%    \end{macrocode}
