%D \module
%D   [       file=pack-cut, % comes from core-vis/trac-vis
%D        version=1996.06.01,
%D          title=\CONTEXT\ Packaging Macros,
%D       subtitle=Cut boxes,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\unprotect

%D \macros
%D   {makecutbox, cuthbox, cutvbox, cutvtop}
%D
%D Although mainly used for marking the page, these macros can
%D also serve local use.
%D
%D \startbuffer
%D \setbox0=\vbox{a real \crlf vertical box} \makecutbox0
%D \stopbuffer
%D
%D \typebuffer
%D
%D This marked \type{\vbox} shows up as:
%D
%D \startlinecorrection
%D \getbuffer
%D \stoplinecorrection
%D
%D The alternative macros are used as:
%D
%D \startbuffer
%D \cuthbox{a made cut box}
%D \stopbuffer
%D
%D \typebuffer
%D
%D This is typeset as:
%D
%D \startlinecorrection
%D \getbuffer
%D \stoplinecorrection
%D
%D By setting the next macros one can influence the length of
%D the marks as well as the horizontal and vertical divisions.

\newdimen\d_pack_cutmarks_width
\newdimen\d_pack_cutmarks_height
\newdimen\d_pack_cutmarks_depth

\newcount\horizontalcutmarks \horizontalcutmarks \plustwo
\newcount\verticalcutmarks   \verticalcutmarks   \plustwo
\newcount\cutmarkoffset      \cutmarkoffset      \plusone

\let\cutmarksymbol       \relax
\let\cutmarktoptext      \empty
\let\cutmarkbottomtext   \empty
\let\cutmarkhoffset      \empty
\let\cutmarkvoffset      \empty
\def\cutmarklength       {2\bodyfontsize}
\def\cutmarkrulethickness{\onepoint}

\unexpanded\def\horizontalcuts
  {\hpack to \d_pack_cutmarks_width
     {\dorecurse\horizontalcutmarks{\vrule\s!width\cutmarkrulethickness\s!height\cutmarklength\hfill}%
      \unskip}}

\unexpanded\def\verticalcuts
  {\vpack to \dimexpr\d_pack_cutmarks_height+\d_pack_cutmarks_depth\relax
     {\hsize\cutmarklength
      \dorecurse\verticalcutmarks{\vrule\s!height\cutmarkrulethickness\s!width\hsize\vfill}%
      \unskip}}

\unexpanded\def\baselinecuts
  {\ifdim\d_pack_cutmarks_depth>\zeropoint
     \vpack to \dimexpr\d_pack_cutmarks_height+\d_pack_cutmarks_depth\relax
       {\hsize\dimexpr\cutmarklength/2\relax
        \vskip\zeropoint\s!plus\d_pack_cutmarks_height
        \vrule\s!height\cutmarkrulethickness\s!width\hsize
        \vskip\zeropoint\s!plus\d_pack_cutmarks_depth}%
   \fi}

\unexpanded\def\cutmarksymbols#1%
  {\hpack to \d_pack_cutmarks_width
     {\setbox\scratchbox\hbox to \cutmarklength
        {\hss\infofont\cutmarksymbol\hss}%
      \hss
      \vpack to \cutmarklength
        {\scratchdimen\dimexpr\cutmarklength/2\relax
         \scratchskip \ifx\cutmarkhoffset\empty\cutmarkoffset\scratchdimen\else\cutmarkhoffset\fi
         \vss
         \hbox to \d_pack_cutmarks_width
           {\llap{\copy\scratchbox\hskip\scratchskip}%
            \hskip\scratchdimen\hss
            \infofont#1%
            \hss\hskip\scratchdimen
            \rlap{\hskip\scratchskip\copy\scratchbox}}%
         \vss}%
      \hss}}

\unexpanded\def\makecutbox#1%
  {\bgroup
   \d_pack_cutmarks_height\ht#1%
   \d_pack_cutmarks_depth \dp#1%
   \d_pack_cutmarks_width \wd#1%
   \setbox#1\hpack
     {\dontcomplain
      \forgetall
      \boxmaxdepth\maxdimen
      \offinterlineskip
      \scratchdimen\dimexpr\cutmarklength/2\relax
      \hsize\d_pack_cutmarks_width
      \setbox\scratchbox\vpack
        {\setbox\scratchbox\hpack{\horizontalcuts}%
         \scratchskip\ifx\cutmarkvoffset\empty\cutmarkoffset\scratchdimen\else\cutmarkvoffset\fi
         \tlap{\copy\scratchbox\vskip\scratchskip}%
         \hpack to \d_pack_cutmarks_width
           {\scratchskip\ifx\cutmarkhoffset\empty\cutmarkoffset\scratchdimen\else\cutmarkhoffset\fi
            \setbox\scratchbox\hpack{\verticalcuts}%
            \llap{\copy\scratchbox\hskip\scratchskip}%
            \ifdim\d_pack_cutmarks_depth=\zeropoint
              \hfill
            \else
              \bgroup
              \setbox\scratchbox\hpack{\baselinecuts}%
              \llap{\copy\scratchbox\hskip\scratchskip}%
              \hfill
              \rlap{\hskip\scratchskip\copy\scratchbox}%
              \egroup
            \fi
            \rlap{\hskip\scratchskip\copy\scratchbox}}%
         \blap{\vskip\scratchskip\copy\scratchbox}}%
      \ht\scratchbox\d_pack_cutmarks_height
      \dp\scratchbox\d_pack_cutmarks_depth
      \wd\scratchbox\zeropoint
      \startcolor[\defaulttextcolor]%
      \box\scratchbox
      \ifx\cutmarksymbol\relax \else
        \setbox\scratchbox\vpack
          {\scratchskip\ifx\cutmarkvoffset\empty\cutmarkoffset\scratchdimen\else\cutmarkvoffset\fi
           \vskip-\dimexpr\scratchskip+\cutmarklength\relax
           \hpack{\cutmarksymbols\cutmarktoptext}%
           \vskip\dimexpr\scratchskip+\d_pack_cutmarks_height+\d_pack_cutmarks_depth+\scratchskip\relax
           \hpack{\cutmarksymbols\cutmarkbottomtext}}%
        \ht\scratchbox\d_pack_cutmarks_height
        \dp\scratchbox\d_pack_cutmarks_depth
        \wd\scratchbox\zeropoint
        \box\scratchbox
      \fi
      \stopcolor
      \box#1}%
   \wd#1\d_pack_cutmarks_width
   \ht#1\d_pack_cutmarks_height
   \dp#1\d_pack_cutmarks_depth
   \egroup}

\unexpanded\def\cuthbox{\hpack\bgroup\dowithnextbox{\makecutbox\nextbox\flushnextbox\egroup}\hbox}
\unexpanded\def\cutvbox{\vpack\bgroup\dowithnextbox{\makecutbox\nextbox\flushnextbox\egroup}\vbox}
\unexpanded\def\cutvtop{\tpack\bgroup\dowithnextbox{\makecutbox\nextbox\flushnextbox\egroup}\vtop}

\protect \endinput
