%%
%%  pageframe.sty for LaTeX 2e by Mark Reed
%%
%%  Generated from pageframe.sty by Cameron Smith
%%  18 Sept 1990, 16 Oct 1990, 21 Nov 1990, 14 Nov 1991
%%  v0.1 -- First release. 18.4.97, M. Reed
%%  v0.2 -- Added \killcropmarks and \nextpagestyle. Updated output
%%          routine for use with LaTeX 2e <1997/12/01>. 23.4.98, M. Reed
%%
%%  By default, draws a frame around the text area to represent
%%  the edge of the paper -- useful when pages for a book of size
%%  7x9 inches (for example) is being proofed on 8 1/2 x 11 inch
%%  paper.  If \pageframefalse is executed, the part of the frame
%%  beside the text is removed, leaving only the corner marks
%%  to use for checking registration and for cropping.
%%  The full frame can be restored by executing \pageframetrue.
%%
%%  This shows the results of
%%
%%       \pageframetrue  and   \pageframefalse
%%
%%        _|__________|_       _|          |_
%%         |          |
%%         |          |
%%         |          |
%%         |          |
%%         |          |
%%         |          |
%%        _|__________|_       _            _
%%         |          |         |          |
%%
%%
%%  This style option also prints (outside the page area) a timestamp,
%%  a job ID, and the page number and a sequence number on each page.
%%  The sequence number advances throughout the document, so even if
%%  there is a page (i) and several pages 1, only the first page
%%  will have sequence number 1.
%%
%%  An oversight in the original version sometimes caused the tag line
%%  to be printed in an unusual font (if that was the last-used font
%%  on the preceding page).  The current version explicitly sets the
%%  font for the tag line so that this won't happen.  The default is
%%  to use cmr10, since it's highly unlikely that there are any TeX
%%  sites that don't have this font available, but the \pffont macro
%%  is provided in case another font is desired.
%%
%%  Two more features have been added: an "inner skeleton", consisting
%%  of rules that demarcate the header, footer, and text area, and a
%%  "text grid" that can be placed inside the text area.  Use
%%  "\innerskeltrue" or "\innerskelfalse" to control the skeleton
%%  and "\textgridtrue" or "\textgridfalse" to control the text grid.
%%  Use (for example) "\gridsize{10pt}{12pt}" to make a grid whose
%%  squares have width 10pt and height 12pt.
%%
%%  To use this style option, include pageframe as an optional argument
%%  in the \documentstyle command, e.g.
%%
%%                 \documentstyle[pageframe]{book}
%%
%%  Also you must tell LaTeX the height of the paper you are using
%%  (it can deduce the width for itself).  You do this by including
%%  a declaration such as
%%                         \paperheight{9in}
%%
%%  in the preamble of your document.  This *must* appear before
%%  the first page of output, because the new output routine
%%  uses this value.  (If you forget to specify it, then two
%%  things will happen: the vertical rules at the sides of the
%%  text will disappear, because LaTeX thinks they have height 0pt,
%%  and the rule that represents the bottom of the paper will be
%%  flush against the bottom of the footer.)
%%
%%  Since this style file modifies (a part of) LaTeX's output routine,
%%  it is exceedingly unlikely to be compatible with any other style
%%  option that also modifies the output routine.
%%
%%  Please direct comments and bug reports by electronic mail
%%  to texline@cup.cam.ac.uk
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[1997/12/01]
\ProvidesPackage{pagefram}[1998/04/23 v0.2 CUP LaTeX 2e package]

\def\vb@xt@{\vbox to}
\let\@themargin=\oddsidemargin

%%
%% This is the timestamp
%%
\@tempcnta\time\@tempcntb\@tempcnta
\divide\@tempcntb by 60\multiply\@tempcntb by 60\advance\@tempcnta-\@tempcntb
\divide\@tempcntb by 60
\edef\@@@timestamp{\vrule \@height.4in \@depth.3in \@width\z@
	   \hskip.5in\today\space\space
	   \ifnum\@tempcntb<10 0\fi
	   \number\@tempcntb:\ifnum\@tempcnta<10 0\fi\number\@tempcnta
	   \space\space\space\space\space\space\jobname
	   \space\space\space\space\space\space
}

%%
%%  \pffont lets us specify the font to use for the tag line
%%  By default we use whatever font is current when this file is
%%  being loaded.  This will normally be cmr10; in any case we're
%%  selecting a font which is (a) certainly available on the system
%%  were running on and (b) probably suitable for this purpose
%%  (normal-sized roman text).
%%
\def\pffont#1{\global\font\@@@pffont=#1\relax}
\def\@@@pffont{\global\let\@@@pffont}
\expandafter\@@@pffont\the\font

\newcount\@@@sheetcount

%%
%% This is the code that makes the "inner skeleton".
%%
\newif\ifinnerskel
\newif\iftextgrid
\textgridtrue
\newdimen\@@@gridwd\newdimen\@@@gridht
\def\gridsize#1#2{\global\@@@gridwd#1\global\@@@gridht#2}
\@@@gridwd 10\p@ \@@@gridht 10\p@

\def\@@@innerskel{\vb@xt@\z@{%
\vskip \topmargin
\vb@xt@\z@{\vss\hrule \@width\textwidth}%
\vskip \headheight
\vb@xt@\z@{\vss\hrule \@width\textwidth}%
\vskip \headsep
\vb@xt@\z@{\vss\hrule \@width\textwidth}%
\iftextgrid
\vb@xt@\z@{\vb@xt@ \textheight{%
\leaders\vb@xt@ \@@@gridht{\vss\hrule \@width\textwidth}\vfil
}\vss}%
\fi
\hb@xt@ \textwidth{\llap{\vrule \@height\textheight}%
\iftextgrid\leaders\hb@xt@\@@@gridwd{\hfil\vrule \@height\textheight}\fi\hfil
\vrule \@height\textheight}%
\vb@xt@\z@{\vss\hrule \@width\textwidth}%
\vskip \footskip
\vb@xt@\z@{\vss\hrule \@width\textwidth}%
\vskip -\footheight
\vb@xt@\z@{\vss\hrule \@width\textwidth}%
\vss
}}

%%
%%  The paper width can be deduced from the text width and margins,
%%  but the paper height must be specified.
%%
\newdimen\@@@pageheight
\def\paperheight#1{\global\@@@pageheight#1}
\newdimen\@@@pagewidth

%%
%%  The part of the output routine that puts the frame on the
%%  page to represent the edge of the paper is surrounded by
%%  \ifpageframe...\fi so that it can be suppressed.
%%
\newif\ifpageframe
\pageframetrue

%%
%%  \@@@cropmark should be followed by two letters, the first of which
%%  is t or b (for top or bottom) and the second l or r (left or right).
%%  It makes a box of height, width, and depth 0pt, but with a
%%  crop mark extending above or below and left or right of the base point.
%%
%%  The arms of the crop mark do not quite meet, but are positioned
%%  so that if they were extended to meet, the meeting point would be
%%  the corner of the paper.  This is to allow a little leeway for
%%  positioning and cutting the paper.
%%
\def\@@@cropmark#1#2{%
\csname#2lap\endcsname{%
\vb@xt@\z@{%
\if#1t\vss\else
\hb@xt@ .5in{\if#2r\hfil\fi
\vrule \@width.45in \@height.4\p@ \@depth\z@\if#2l\hfil\fi}%
\vglue.05in\fi%
\hb@xt@ .5in{\if#2l\hfil\fi\vrule \@height.45in \@depth\z@\if#2r\hfil\fi}%
\if#1b\vss\else
\vglue.05in%
\hb@xt@ .5in{\if#2r\hfil\fi
\vrule \@width.45in \@height.4\p@ \@depth\z@\if#2l\hfil\fi}%
\fi%
}}%
}

\newcommand\killcropmarks{\global\let\@@@cropmark\@gobbletwo}

\let\@theothermargin\@themargin

\newif\ifnoinfo  \noinfotrue % turns of timestamp etc. at top of page

\newif\if@NextPageStyle
\newcount\@NextCount
\newcount\@NextLimit

\newcommand\nextpagestyle{\@ifnextchar[{\@xnextpstyle}{\@xnextpstyle[0]}}
\def\@xnextpstyle[#1]#2{%
  \global\@NextCount=\z@
  \ifnum#1=\z@
    \global\@NextLimit=\tw@
    \global\@NextPageStyletrue
    \gdef\@NextStyle{#2}%
  \else
    \ifnum#1<\tw@
      \errmessage{The [n] argument in the \string\nextpagestyle
                  \space command must be >1.^^J
                  \space The command has been ignored}%
    \else
      \global\@NextLimit=#1\relax
      \global\advance\@NextLimit \@ne
      \global\@NextPageStyletrue
      \gdef\@NextStyle{#2}%
    \fi
  \fi
}

% Adapted from LATEX.LTX [1997/12/01]

\ifx\normalsfcodes\undefined \let\normalsfcodes\relax\fi

\def\@outputpage{%
  \begingroup           % the \endgroup is put in by \aftergroup
    \let \protect \noexpand
    \@resetactivechars
    \@parboxrestore
    \global\advance\@@@sheetcount \@ne
    \shipout \vbox{%
      \set@typeset@protect
      \aftergroup \endgroup
      \aftergroup \set@typeset@protect
                                % correct? or just restore by ending
                                % the group?
      \if@specialpage
        \global\@specialpagefalse\@nameuse{ps@\@specialstyle}%
      \fi
      \if@NextPageStyle
        \ifnum\@NextCount>\z@
          \ifnum\@NextCount<\@NextLimit
            \@nameuse{ps@\@NextStyle}%
          \else
            \global\@NextPageStylefalse
          \fi
        \fi
      \fi
      \if@twoside
        \ifodd\count\z@ \let\@thehead\@oddhead \let\@thefoot\@oddfoot
          \let\@themargin\oddsidemargin
          \let\@theothermargin\evensidemargin
        \else \let\@thehead\@evenhead
          \let\@thefoot\@evenfoot \let\@themargin\evensidemargin
          \let\@theothermargin\oddsidemargin
        \fi
      \fi
      \reset@font
      \normalsize
      \normalsfcodes
      \let\label\@gobble
      \let\index\@gobble
      \let\glossary\@gobble
      \baselineskip\z@skip \lineskip\z@skip \lineskiplimit\z@
      \@begindvi
      \vb@xt@\z@{\vss
        \hbox{\ifnoinfo \else
          \@@@pffont\@@@timestamp Sheet number \number\@@@sheetcount
          \space\space Page number \thepage\fi}%
        \hrule \@height\z@ \@depth\z@
      }%
      \@@@pagewidth\@themargin \advance\@@@pagewidth\textwidth
      \advance\@@@pagewidth\@theothermargin
      \hbox{\@@@cropmark tl\hskip\@@@pagewidth\@@@cropmark tr}%
      \ifpageframe \vb@xt@\z@{\vss\hrule \@width\@@@pagewidth}\fi
      \hbox{%
        \ifpageframe
          \llap{\vrule \@height\z@ \@depth\@@@pageheight}%
        \else
          \vrule \@height\z@ \@depth\@@@pageheight \@width\z@
        \fi
        \hskip\@themargin
        \vtop{%
          \ifinnerskel\@@@innerskel\fi
          \vskip \topmargin
%%%          \moveright\@themargin
          \vbox {%
            \setbox\@tempboxa \vbox to\headheight{%
              \vfil
              \color@hbox
              \normalcolor
              \hb@xt@\textwidth {\@thehead}%
              \color@endbox
            }%                        %% 22 Feb 87
            \dp\@tempboxa \z@
            \box\@tempboxa
            \vskip \headsep
            \box\@outputbox
            \baselineskip \footskip
            \color@hbox
            \normalcolor
            \hb@xt@\textwidth{\@thefoot}%
            \color@endbox
          }%
        }%
        \ifpageframe
          \hskip\@theothermargin
          \rlap{\vrule \@height\z@ \@depth\@@@pageheight}%
        \fi
      }%
      \ifpageframe \vb@xt@\z@{\hrule \@width\@@@pagewidth\vss}\fi
      \hbox{\@@@cropmark bl\hskip\@@@pagewidth\@@@cropmark br}%
    }%
    \global \@colht \textheight
    \stepcounter{page}%
    \global\advance\@NextCount \@ne
    \let\firstmark\botmark
}

\endinput

% End of file pagefram.sty
