%D \module
%D   [       file=page-mrk,
%D        version=2011.05.23, % moved from page-lay
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Cutmarks and Colorbars,
%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.

\writestatus{loading}{ConTeXt Page Macros / Cutmarks and Colorbars}

%D This module depends on \METAPOST.

\unprotect

% \def\pagecutmarksymbol    {\the\realpageno}
% \let\pagecutmarktoptext    \empty
% \let\pagecutmarkbottomtext \empty

\def\pagecutmarklength {5mm}
\def\pagecutmarkoffset {3mm} % slightly larger than before, and now a fixed size
\def\pagecutmarkmargin{10cm}

\newconditional\c_page_marks_add_more_color
\newconditional\c_page_marks_add_more_marking
\newconditional\c_page_marks_add_more_lines
\newconditional\c_page_marks_add_page_lines
\newconditional\c_page_marks_add_more_number

\newcount\c_page_marks_nx
\newcount\c_page_marks_ny

\startuniqueMPgraphic{print:color}{w,h,l,o}
    if unknown context_crop : input mp-crop.mpiv ; fi ;
    page_marks_add_color(\MPvar w,\MPvar h,\MPvar l,\MPvar o) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{print:marking}{w,h,l,o}
    if unknown context_crop : input mp-crop.mpiv ; fi ;
    page_marks_add_marking(\MPvar w,\MPvar h,\MPvar l,\MPvar o) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{print:lines}{w,h,l,o,x,y}
    if unknown context_crop : input mp-crop.mpiv ; fi ;
    page_marks_add_lines(\MPvar w,\MPvar h,\MPvar l,\MPvar o,\MPvar x,\MPvar y) ;
\stopuniqueMPgraphic

\startuseMPgraphic{print:number}{w,h,l,o,n}
    if unknown context_crop : input mp-crop.mpiv ; fi ;
    page_marks_add_number(\MPvar w,\MPvar h,\MPvar l,\MPvar o,\MPvar n) ;
\stopuseMPgraphic

\def\page_marks_add_color
  {\setlayoutcomponentattribute{\v!print:\v!color}%
   \setbox\scratchbox\hbox \layoutcomponentboxattribute\bgroup
     \uniqueMPgraphic
       {print:color}%
       {w=\the\scratchwidth,h=\the\scratchheight,%
        l=\pagecutmarklength,o=\pagecutmarkoffset}%
   \egroup
   \wd\scratchbox\scratchwidth\ht\scratchbox\scratchheight\dp\scratchbox\scratchdepth
   \hskip-\scratchwidth\box\scratchbox}

\def\page_marks_add_marking
  {\setlayoutcomponentattribute{\v!print:\v!marking}%
   \setbox\scratchbox\hbox \layoutcomponentboxattribute\bgroup
     \uniqueMPgraphic
       {print:marking}%
       {w=\the\scratchwidth,h=\the\scratchheight,%
        l=\pagecutmarklength,o=\pagecutmarkoffset}%
   \egroup
   \wd\scratchbox\scratchwidth\ht\scratchbox\scratchheight\dp\scratchbox\scratchdepth
   \hskip-\scratchwidth\box\scratchbox}

\def\page_marks_add_lines
  {\setlayoutcomponentattribute{\v!print:\v!lines}%
   \setbox\scratchbox\hbox \layoutcomponentboxattribute\bgroup
     \uniqueMPgraphic
       {print:lines}%
       {w=\the\scratchwidth,h=\the\scratchheight,%
        l=\pagecutmarklength,o=\pagecutmarkoffset,%
        x=\the\c_page_marks_nx,y=\the\c_page_marks_ny}%
   \egroup
   \wd\scratchbox\scratchwidth\ht\scratchbox\scratchheight\dp\scratchbox\scratchdepth
   \hskip-\scratchwidth\box\scratchbox}

\def\page_marks_add_number
  {\setlayoutcomponentattribute{\v!print:\v!number}%
   \setbox\scratchbox\hbox \layoutcomponentboxattribute\bgroup
     \useMPgraphic
       {print:number}%
       {w=\the\scratchwidth ,h=\the\scratchheight,%
        l=\pagecutmarklength,o=\pagecutmarkoffset,%
        n=\number\ifcase\arrangeno\realpageno\else\arrangeno\fi}%
   \egroup
   \wd\scratchbox\scratchwidth\ht\scratchbox\scratchheight\dp\scratchbox\scratchdepth
   \hskip-\scratchwidth\box\scratchbox}

\def\page_marks_add_page_indeed#1%
  {\setbox#1\hbox\bgroup
     \resetvisualizers
     \let\objectoffset\pagecutmarkmargin
    %\infofont
     \scratchwidth \wd#1%
     \scratchheight\ht#1%
     \scratchdepth \dp#1%
     \box#1\relax
     \ifconditional\c_page_marks_add_page_lines
       \page_marks_add_lines
     \fi
   \egroup}

\def\page_marks_add_more_indeed#1%
  {\setbox#1\hbox\bgroup
     \resetvisualizers
     \let\objectoffset\pagecutmarkmargin
     \infofont
     \scratchwidth \wd#1% these can move out
     \scratchheight\ht#1%
     \scratchdepth \dp#1%
     \box#1\relax
     \ifconditional\c_page_marks_add_more_color
       \page_marks_add_color
     \fi
     \ifconditional\c_page_marks_add_more_marking
       \page_marks_add_marking
     \fi
     \ifconditional\c_page_marks_add_more_lines
       \page_marks_add_lines
     \fi
     \ifconditional\c_page_marks_add_more_number
       \page_marks_add_number
     \fi
   \egroup}

\let\page_marks_add_page\gobbleoneargument
\let\page_marks_add_more\gobbleoneargument

\installcorenamespace{layoutmarking}

\def\installpagecutmark#1#2%
  {\setvalue{\??layoutmarking#1}{#2}}

% \installpagecutmark\v!off
%   {}

% \installpagecutmark\v!text
%   {\settrue\c_page_marks_add_more_lines
%    \settrue\c_page_marks_add_more_number
%    \let\pagecutmarksymbol\empty
%    \let\pagecutmarkbottomtext\extrapagecutmarkbottomtext}

\installpagecutmark\v!on
  {\settrue\c_page_marks_add_page_lines
   \settrue\c_page_marks_add_more_lines
   \settrue\c_page_marks_add_more_number}

\installpagecutmark\v!page  % todo
  {\settrue\c_page_marks_add_more_lines
   \settrue\c_page_marks_add_more_number}

\installpagecutmark\v!empty
  {\settrue\c_page_marks_add_page_lines
   \settrue\c_page_marks_add_more_lines}

\installpagecutmark\v!color
  {\settrue\c_page_marks_add_page_lines
   \settrue\c_page_marks_add_more_lines
   \settrue\c_page_marks_add_more_color
   \settrue\c_page_marks_add_more_marking
   \settrue\c_page_marks_add_more_number}

\appendtoks
    \setfalse\c_page_marks_add_page_lines
    \setfalse\c_page_marks_add_more_color
    \setfalse\c_page_marks_add_more_marking
    \setfalse\c_page_marks_add_more_lines
    \setfalse\c_page_marks_add_more_number
    \csname\??layoutmarking\layoutparameter\c!marking\endcsname
    \ifconditional\c_page_marks_add_page_lines
      \let\page_marks_add_page\page_marks_add_page_indeed
    \else
      \let\page_marks_add_page\gobbleoneargument
    \fi
    \ifconditional\c_page_marks_add_more_lines
      \let\page_marks_add_more\page_marks_add_more_indeed
    \else\ifconditional\c_page_marks_add_more_color
      \let\page_marks_add_more\page_marks_add_more_indeed
    \else\ifconditional\c_page_marks_add_more_marking
      \let\page_marks_add_more\page_marks_add_more_indeed
    \else\ifconditional\c_page_marks_add_more_number
      \let\page_marks_add_more\page_marks_add_more_indeed
    \else
      \let\page_marks_add_more\gobbleoneargument
    \fi\fi\fi\fi
\to \everysetuplayout

\setuplayout
  [\c!marking=\v!off]

\protect \endinput
