%D \module
%D   [       file=page-lay,
%D        version=2000.10.20, % copied from main-001
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Layout Specification,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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 / Layout Specification}

% to be translated into english

% hoofdhoogte wordt bij status=hoog niet aangepast op outer
% level, wel binnen bepaalde berekeningen

%D Before you start wondering why some of the page related
%D modules skip upward or left in order to place elements, you
%D must realize that the reference point is the top left
%D corner of the main typesetting area. One reason for this
%D choice is that it suited some viewers that displayed page
%D areas. Another reason is that margins, edges and top and
%D bottom areas are kind of virtual, while the header, text
%D and footer areas normally determine the text flow.

\unprotect

%D First we get rid of the funny \TEX\ offset defaults of one
%D inch by setting them to zero.

\voffset = 0pt % setting this to -1in let's go metapost crazy
\hoffset = 0pt % setting this to -1in let's go metapost crazy

%D The dimensions related to layout areas are represented by
%D real dimensions.

\newdimen\paperheight          \paperheight          = 297mm
\newdimen\paperwidth           \paperwidth           = 210mm

\newdimen\printpaperheight     \printpaperheight     = \paperheight
\newdimen\printpaperwidth      \printpaperwidth      = \paperwidth

\newdimen\makeupheight                               % calculated
\newdimen\makeupwidth                                % calculated

\newdimen\textheight                                 % calculated
\newdimen\textwidth                                  % calculated

\newdimen\topspace             \topspace             = 2cm
\newdimen\backspace            \backspace            = \topspace
\newdimen\cutspace             \cutspace             = 0pt
\newdimen\bottomspace          \bottomspace          = 0pt

\newdimen\headerheight         \headerheight         = 2cm
\newdimen\footerheight         \footerheight         = \headerheight

\newdimen\kopoffset            \kopoffset            = 0pt
\newdimen\rugoffset            \rugoffset            = \kopoffset

\newdimen\leftmarginwidth      \leftmarginwidth      = 3cm
\newdimen\rightmarginwidth     \rightmarginwidth     = \leftmarginwidth

\newdimen\leftedgewidth        \leftedgewidth        = 3cm
\newdimen\rightedgewidth       \rightedgewidth       = \leftedgewidth

\newdimen\topheight            \topheight            = 0cm
\newdimen\bottomheight         \bottomheight         = \topheight

\newcount\layoutlines          \layoutlines          = 0
\newcount\layoutcolumns        \layoutcolumns        = 0
\newdimen\layoutcolumndistance \layoutcolumndistance = 0pt
\newdimen\layoutcolumnwidth    \layoutcolumnwidth    = 0pt

%D We can save some tokens and fuzzy parameters by using a
%D symbolic name for the current set of layout parameters.

\let\currentlayout\empty

%\def\layoutparameter#1%
%  {\ifundefined{\??ly\currentlayout#1}%
%     \getvalue{\??ly#1}%
%   \else
%     \getvalue{\??ly\currentlayout#1}%
%   \fi}
%
% faster

\def\layoutparameter#1%
  {\csname\??ly
     \ifundefined{\??ly\currentlayout#1}\else\currentlayout\fi
   #1\endcsname}

\def\namedlayoutparameter#1#2%
  {\ifundefined{\??ly#1#2}%
     \layoutparameter{#2}%
   \else
     \getvalue{\??ly#1#2}%
   \fi}

%D Look how ugly a speed up looks:

\beginTEX

\def\layoutparameter#1%
  {\csname\??ly\@EA\ifx\csname
     \??ly\currentlayout#1\endcsname\relax\else\currentlayout
   \fi#1\endcsname}

\def\namedlayoutparameter#1#2%
  {\csname\??ly
     \@EA\ifx\csname\??ly#1#2\endcsname\relax
       \@EA\ifx\csname\??ly\currentlayout#2\endcsname\relax
       \else\currentlayout\fi
     \else#1\fi
   \fi#2\endcsname}

\endTEX

%D Its \ETEX\ counterpart is:

\beginETEX \ifcsname

\def\layoutparameter#1%
  {\csname\??ly\ifcsname
     \??ly\currentlayout#1\endcsname\currentlayout
   \fi#1\endcsname}

\def\namedlayoutparameter#1#2%
  {\csname\??ly\ifcsname\??ly            #1#2\endcsname#1\else
               \ifcsname\??ly\currentlayout#2\endcsname\currentlayout
   \fi\fi#2\endcsname}

\endETEX

%D Beause normal \TEX\ has at most 256 dimensions (of which a
%D substantial part is already in use), we provide a way to
%D generate a format with macro based alternatives. For a long
%D time, this used to be the default case. Beware: only fixed
%D dimensions can be used in calculations! By the way, the
%D gain in speed can hardly be called impressive and is roughly
%D 1 second on a 35 second run of 850 empty pages with a
%D couple of backgrounds only (which is far less than one
%D percent on a normal document).

%D The next series of dimensions are complemented by left
%D and rights ones.

\newdimen \margindistance
\newdimen \edgedistance
\newdimen \marginwidth
\newdimen \edgewidth

%D Because a distance does not really makes sense when there
%D is no area, we use a zero distance in case there is no
%D area.

\def\layoutdistance#1#2%
  {\ifdim\zeropoint<#1\layoutparameter#2\else\zeropoint\fi}

%D The horizontal distances are:

\newdimen \leftedgedistance
\newdimen \rightedgedistance
\newdimen \leftmargindistance
\newdimen \rightmargindistance

%D The vertical distances are:

\newdimen \topdistance
\newdimen \headerdistance
\newdimen \footerdistance
\newdimen \bottomdistance

%D We need to calculate the extra distances:

\def\setlayoutdimensions
  {\global\marginwidth        \layoutparameter\c!margin
   \global\edgewidth          \layoutparameter\c!edge
   \global\margindistance     \layoutparameter\c!margindistance
   \global\edgedistance       \layoutparameter\c!edgedistance
   \global\leftedgedistance   \layoutdistance \leftedgewidth    \c!leftedgedistance
   \global\rightedgedistance  \layoutdistance \rightedgewidth   \c!rightedgedistance
   \global\leftmargindistance \layoutdistance \leftmarginwidth  \c!leftmargindistance
   \global\rightmargindistance\layoutdistance \rightmarginwidth \c!rightmargindistance
   \global\topdistance        \layoutdistance \topheight        \c!topdistance
   \global\headerdistance     \layoutdistance \headerheight     \c!headerdistance
   \global\footerdistance     \layoutdistance \footerheight     \c!footerdistance
   \global\bottomdistance     \layoutdistance \bottomheight     \c!bottomdistance}

\def\setlayoutdistances % local in \setreducedvsize
  {\headerdistance\layoutdistance\headerheight\c!headerdistance
   \footerdistance \layoutdistance\footerheight \c!footerdistance}

\def\outermarginwidth   {\rightorleftpageaction\rightmarginwidth   \leftmarginwidth }
\def\innermarginwidth   {\rightorleftpageaction\leftmarginwidth    \rightmarginwidth}
\def\outermargindistance{\rightorleftpageaction\rightmargindistance\leftmargindistance }
\def\innermargindistance{\rightorleftpageaction\leftmargindistance \rightmargindistance}

\def\outeredgewidth     {\rightorleftpageaction\rightedgewidth   \leftedgewidth }
\def\inneredgewidth     {\rightorleftpageaction\leftedgewidth    \rightedgewidth}
\def\outeredgedistance  {\rightorleftpageaction\rightedgedistance\leftedgedistance }
\def\inneredgedistance  {\rightorleftpageaction\leftedgedistance \rightedgedistance}

\def\outerspacewidth    {\rightorleftpageaction\cutspace\backspace}
\def\innerspacewidth    {\rightorleftpageaction\backspace\cutspace}

\newtoks\extralayoutcalculations \def\calculatelayoutextras{\the\extralayoutcalculations}

\appendtoks
  \scratchdimen\leftmarginwidth  \advance\scratchdimen\leftmargindistance  \xdef\leftmargintotal {\the\scratchdimen}%
  \scratchdimen\rightmarginwidth \advance\scratchdimen\rightmargindistance \xdef\rightmargintotal{\the\scratchdimen}%
  \scratchdimen\leftedgewidth    \advance\scratchdimen\leftedgedistance    \xdef\leftedgetotal   {\the\scratchdimen}%
  \scratchdimen\rightedgewidth   \advance\scratchdimen\rightedgedistance   \xdef\rightedgetotal  {\the\scratchdimen}%
  \scratchdimen\leftmarginwidth  \advance\scratchdimen\leftedgetotal       \xdef\leftsidetotal   {\the\scratchdimen}%
  \scratchdimen\rightmarginwidth \advance\scratchdimen\rightedgetotal      \xdef\rightsidetotal  {\the\scratchdimen}%
  \scratchdimen\leftmargintotal  \advance\scratchdimen\leftedgetotal       \xdef\leftcombitotal  {\the\scratchdimen}%
  \scratchdimen\rightmargintotal \advance\scratchdimen\rightedgetotal      \xdef\rightcombitotal {\the\scratchdimen}%
  %
  \scratchdimen\innermarginwidth \advance\scratchdimen\innermargindistance \xdef\innermargintotal{\the\scratchdimen}%
  \scratchdimen\outermarginwidth \advance\scratchdimen\outermargindistance \xdef\outermargintotal{\the\scratchdimen}%
  \scratchdimen\inneredgewidth   \advance\scratchdimen\inneredgedistance   \xdef\inneredgetotal  {\the\scratchdimen}%
  \scratchdimen\outeredgewidth   \advance\scratchdimen\outeredgedistance   \xdef\outeredgetotal  {\the\scratchdimen}%
  \scratchdimen\innermargintotal \advance\scratchdimen\inneredgetotal      \xdef\innercombitotal {\the\scratchdimen}%
  \scratchdimen\outermargintotal \advance\scratchdimen\outeredgetotal      \xdef\outercombitotal {\the\scratchdimen}%
  \scratchdimen\innermarginwidth \advance\scratchdimen\inneredgetotal      \xdef\innersidetotal  {\the\scratchdimen}%
  \scratchdimen\outermarginwidth \advance\scratchdimen\outeredgetotal      \xdef\outersidetotal  {\the\scratchdimen}%
\to \extralayoutcalculations

\newtoks\everyswapmargins % watch the order !

\appendtoks
   \swapdimens\leftmargindistance\rightmargindistance
   \swapdimens\leftedgedistance  \rightedgedistance
   \swapdimens\leftmarginwidth   \rightmarginwidth
   \swapdimens\leftedgewidth     \rightedgewidth
\to \everyswapmargins

\appendtoks
  \swapmacros\leftmargintotal \rightmargintotal
  \swapmacros\leftedgetotal   \rightedgetotal
  \swapmacros\leftsidetotal   \rightsidetotal
  \swapmacros\leftcombitotal  \rightcombitotal
  \swapmacros\innermargintotal\outermargintotal
  \swapmacros\inneredgetotal  \outeredgetotal
  \swapmacros\innercombitotal \outercombitotal
  \swapmacros\innersidetotal  \outersidetotal
\to \everyswapmargins

%D \macros
%D   {definepapersize}
%D
%D Before we start calculating layout dimensions, we will
%D first take care of paper sizes. The first argument can be
%D either an assignment (for defaults) or an identifier, in
%D which case the second argument is an assignment.
%D
%D \showsetup{definepapersize}

\def\definepapersize
  {\dodoubleempty\dodefinepapersize}

\def\dodefinepapersize[#1][#2]%
  {\ifsecondargument
     \doifsomething{#1} % to be sure
       {\getparameters
          [\??pp#1] % geen \c!scale, scheelt hash ruimte
          [\c!width=\@@ppwidth,\c!height=\@@ppheight,\c!offset=\@@ppoffset,#2]}%
   \else
     \getparameters[\??pp][#1]%
     \setuppapersize
   \fi}

%D For the moment we need to fake this macro.

\ifx\setuppapersize\undefined
  \let\setuppapersize\relax
\fi

%D We set the defaults to the dimensions of an A4 sheet of
%D paper.

\definepapersize
  [\c!width=210mm,\c!height=297mm,\c!offset=\!!zeropoint]

%D \macros
%D   {setuppaper,setuppapersize}
%D
%D When setting up the papersize on which to typeset and
%D print, we can also determine some more characteristics.
%D
%D \showsetup{setuppapersize}
%D
%D We keep track of these features with the following
%D variables.

\chardef\papermirror     =0  \chardef\printmirror     =0
\chardef\paperorientation=0  \chardef\printorientation=0
\chardef\paperreverse    =0  \chardef\printreverse    =0
\chardef\paperlandscape  =0  \chardef\printlandscape  =0

\let\papersize\empty \let\printpapersize\empty

\def\paperscale{1} \newif\ifnegateprintbox

% \def\dosetuppapersize[#1][#2]%
%   {\doifassignmentelse{#1}
%      {\getparameters[\??pp][#1]}
%      {\doifassignmentelse{#2}
%         {\getparameters[\??pp#1][#2]}
%         {\dodosetuppapersize[#1][#2]}}}
%
% The next version is more extensive; here we can
% associate paper and page with key/value like settinsg,
% which is handy in 'example' when we use setups

\def\setuppaper[#1]%
  {\getparameters[\??pp][\c!paper=,\c!page=,#1]%
   \scratchcounter\@@ppnx
   \multiply\scratchcounter\@@ppny
   \edef\@@ppxy{\the\scratchcounter}%
   \doifelsenothing\@@pppage
     {\doifsomething\@@pppaper
        {\dodosetuppapersize[\papersize][\@@pppaper]}}
     {\doifelsenothing\@@pppaper
        {\dodosetuppapersize[\@@pppage][\printpapersize]}
        {\dodosetuppapersize[\@@pppage][\@@pppaper]}}}

\def\setuppapersize
  {\dodoubleempty\dosetuppapersize}

\def\dosetuppapersize[#1][#2]%
  {\doifassignmentelse{#1}
     {\setuppaper[#1]}
     {\doifassignmentelse{#2}
        {\getparameters[\??pp#1][#2]}
        {\dodosetuppapersize[#1][#2]}}}

\def\dodosetuppapersize[#1][#2]%
  {\ifsecondargument
     \expanded{\dododosetuppapersize[#1][#2]}%
     \calculatehsizes
     \calculatevsizes
     \recalculatelogos
     \recalculatebackgrounds
     \recalculatelayout
   \else\iffirstargument
     \setuppapersize[#1][#2]%
   \else\ifx\papersize\undefined\else
     \restorepapersize
   \fi\fi\fi}

\def\dododosetuppapersize[#1][#2]%
  {\xdef\restorepapersize
     {\noexpand\setuppapersize[#1][#2]}%
   \dosetuppaperorientation{#1}\paperlandscape\paperorientation\paperreverse\papermirror
   \dosetuppaperorientation{#2}\printlandscape\printorientation\printreverse\printmirror
   \def\docommando##1%
     {\doifsomething{##1}{\doifdefined{\??pp##1\c!width}
        {\global\paperwidth\getvalue{\??pp##1\c!width}%
         \global\paperheight\getvalue{\??pp##1\c!height}%
         \calculatepaperoffsets{##1}%
         \xdef\papersize{##1}}}}%
   \processcommacommand[#1]\docommando
   \doifdefinedelse{\??pp#1\c!scale}
     {\edef\paperscale{\getvalue{\??pp#1\c!scale}}}
     {\edef\paperscale{1}}%
   \def\docommando##1%
     {\doifsomething{##1}{\doifdefined{\??pp##1\c!width}
        {\global\printpaperwidth\getvalue{\??pp##1\c!width}%
         \global\printpaperheight\getvalue{\??pp##1\c!height}%
         \xdef\printpapersize{##1}}}}%
   \processcommacommand[#2]\docommando
   \global\setdimentoatleast\paperwidth      \onepoint
   \global\setdimentoatleast\paperheight     \onepoint
   \global\setdimentoatleast\printpaperwidth \onepoint
   \global\setdimentoatleast\printpaperheight\onepoint
   \ifcase\paperlandscape\else
     \doglobal\swapdimens\paperwidth\paperheight
   \fi
   \ifcase\printlandscape\else
     \doglobal\swapdimens\printpaperwidth\printpaperheight
   \fi
   % this check can be confusing, so we've added the possibility
   % to bypass this test: \setuppapersize[option=fit]
   \doif\@@ppoption\v!max % \v!fit is
     {\bgroup
      % we need to pre-swap else we get the wrong paper size
      \doifinset\paperorientation{90,270}{\swapdimens\paperwidth\paperheight}%
      \doifinset\printorientation{90,270}{\swapdimens\printpaperwidth\printpaperheight}%
      \ifdim\paperheight>\printpaperheight
        \global\printpaperheight\paperheight
        \writestatus\m!systems{print height forced to paper height}%
      \fi
      \ifdim\paperwidth>\printpaperwidth
        \global\printpaperwidth\paperwidth
        \writestatus\m!systems{print width forced to paper width}%
      \fi
      \egroup}}

\def\dosetuppaperorientation#1#2#3#4#5%
  {\global\chardef#2\zerocount
   \global\chardef#5\zerocount
   \globallet#3\!!zerocount
   \globallet#4\!!zerocount
   \global\negateprintboxfalse
   \processallactionsinset
     [#1]
     [ \v!landscape=>\global\chardef#2\plusone,
        \v!mirrored=>\global\chardef#5\plusone,
         \v!rotated=>\gdef#3{90}\gdef#4{270},
        \v!negative=>\global\negateprintboxtrue,
                 90=>\gdef#3{90}\gdef#4{270},
                180=>\gdef#3{180}\gdef#4{0},
                270=>\gdef#3{270}\gdef#4{90}]}

\ifx\calculatepaperoffsets\undefined

  \def\calculatepaperoffsets#1%
    {\scratchdimen\getvalue{\??pp#1\c!offset}%
     \global\advance\paperwidth -2\scratchdimen
     \global\advance\paperheight-2\scratchdimen}

\fi

\let\restorepapersize\relax

\def\checkforems[#1]%
  {\def\docommando##1%
     {\beforesplitstring##1\at em\to\asciia
      \doifnot\asciia{##1}
        {\aftersplitstring\asciia\at=\to\asciia
         \doifsomething\asciia
           {\showmessage\m!systems{10}{##1}}}}%
   \processcommalist[#1]\docommando}

\ifx\setups\undefined \def\setups[#1]{\setdefaultpenalties} \fi

% \newtoks \everybeforelayout \relax
% \newtoks \everyafterlayout  \relax

\def\recalculatelayout
  {\doifsomething{\layoutparameter\c!page\layoutparameter\c!paper}
     {\expanded{\dododosetuppapersize[\layoutparameter\c!page][\layoutparameter\c!paper]}}%
   \dorecalculatelayout}

\def\dorecalculatelayout
  {%\the\everybeforelayout
   \setups[\layoutparameter\c!preset]%
   \global\leftmarginwidth \layoutparameter\c!leftmargin
   \global\rightmarginwidth\layoutparameter\c!rightmargin
   \global\leftedgewidth   \layoutparameter\c!leftedge
   \global\rightedgewidth  \layoutparameter\c!rightedge
   \global\headerheight    \layoutparameter\c!header
   \global\footerheight    \layoutparameter\c!footer
   \global\bottomheight    \layoutparameter\c!bottom
   \global\topheight       \layoutparameter\c!top
   \global\backspace       \layoutparameter\c!backspace
   \global\topspace        \layoutparameter\c!topspace
   \setlayoutdimensions % the rest of the `dimensions'
   \doifelse\@@lygrid\v!yes\gridsnappingtrue\gridsnappingfalse
   \ifgridsnapping
     \setsystemmode\v!grid
   \else
     \resetsystemmode\v!grid
   \fi
   \setups[\layoutparameter\c!setups]% depends on gridsnapping !
   \simplesetupwhitespace
   \simplesetupblank
   \global\cutspace\layoutparameter\c!cutspace
   \relax
   \doifelse{\layoutparameter\c!width}\v!middle
     {\ifdim\cutspace=\zeropoint
        \global\cutspace\backspace
      \fi
      \global\makeupwidth\paperwidth
      \global\advance\makeupwidth -\backspace
      \global\advance\makeupwidth -\cutspace}
     {\doifelse{\layoutparameter\c!width}\v!fit
        {\ifdim\cutspace=\zeropoint
           \global\cutspace\backspace
         \fi
         \global\makeupwidth\paperwidth
         \global\advance\makeupwidth -\cutspace
         \scratchdimen\backspace
         \advance\scratchdimen -\leftedgewidth
         \advance\scratchdimen -\leftedgedistance
         \advance\scratchdimen -\leftmarginwidth
         \advance\scratchdimen -\leftmargindistance\relax
         \ifdim\scratchdimen<\zeropoint
           \scratchdimen\zeropoint
         \fi
         \global\advance\makeupwidth -\rightmargindistance
         \global\advance\makeupwidth -\rightmarginwidth
         \global\advance\makeupwidth -\rightedgedistance
         \global\advance\makeupwidth -\rightedgewidth
         \global\advance\makeupwidth -\scratchdimen}
        {\global\makeupwidth\layoutparameter\c!width\relax
         \ifdim\cutspace=\zeropoint
           \global\cutspace\paperwidth
           \global\advance\cutspace-\makeupwidth
           \global\advance\cutspace-\backspace
        % \else
            % A kind of inconsistent specification, but used
            % in for instance s-pre-19.tex; the cutspace is
            % used only for determining some kind of right
            % margin; don't use this in doublesided mode
         \fi}}%
   \scratchdimen\layoutparameter\c!bottomspace\relax
  %\ifdim\scratchdimen=\zeropoint
  %  \scratchdimen\topspace
  %\fi
   \global\bottomspace\layoutparameter\c!bottomspace\relax
   \global\layoutlines0\number\layoutparameter\c!lines\relax % may be empty
   \ifcase\layoutlines
     \doifelse{\layoutparameter\c!height}\v!middle
       {\ifdim\bottomspace=\zeropoint
          \global\bottomspace\topspace
        \fi
        \global\makeupheight\paperheight
        \global\advance\makeupheight -\topspace
        \global\advance\makeupheight -\bottomspace}
       {\doifelse{\layoutparameter\c!height}\v!fit
          {\ifdim\bottomspace=\zeropoint
            \global\bottomspace\topspace
           \fi
           \global\makeupheight\paperheight
           \global\advance\makeupheight -\bottomspace
           \scratchdimen\topspace
           \advance\scratchdimen -\topheight
           \advance\scratchdimen -\topdistance\relax
           \ifdim\scratchdimen<\zeropoint
             \scratchdimen\zeropoint
           \fi
           \global\advance\makeupheight -\bottomdistance
           \global\advance\makeupheight -\bottomheight
           \global\advance\makeupheight -\scratchdimen}
          {\global\makeupheight\layoutparameter\c!height\relax
           \ifdim\bottomspace=\zeropoint
             \global\bottomspace\paperheight
             \global\advance\bottomspace-\makeupheight
             \global\advance\bottomspace-\topspace
           \else
             % inconsistent specification
           \fi}}%
   \else
     % beware, when the bodyfont changes (switched) this will change as well; implementing
     % a global lineheight is tricky: should we take the bodyfont interlinespace or the one set
     % independent of the bodyfont (before or after a layout spec); way too fuzzy, so we
     % stick to the current method (after a night of experimenting ...2003/10/13)
     \global\makeupheight\layoutparameter\c!lines\lineheight
     % new, cleaner
     \global\advance\makeupheight-\strutheight
     \global\advance\makeupheight \topskip
     % new, was a bug
     \global\advance\makeupheight \headerdistance
     \global\advance\makeupheight \footerdistance
     % so far
     \global\advance\makeupheight \headerheight
     \global\advance\makeupheight \footerheight
   \fi
   \rugoffset\layoutparameter\c!horoffset
   \kopoffset\layoutparameter\c!veroffset
   \global\setdimentoatleast\makeupwidth\onepoint
   \global\setdimentoatleast\makeupheight \onepoint
   % \checkcurrentlayout % here ?
   % \the\everyafterlayout
   \calculatelayoutextras
   \calculatehsizes
   \calculatevsizes
   \calculatepseudocolumns
   \checkgridsnapping
   \recalculatelogos
   \recalculatebackgrounds}

\def\calculatepseudocolumns
  {\global\layoutcolumns\layoutparameter\c!columns
   \global\layoutcolumndistance\layoutparameter\c!columndistance
   \global\layoutcolumnwidth-\layoutcolumns\layoutcolumndistance
   \global\advance\layoutcolumnwidth\layoutcolumndistance
   \global\advance\layoutcolumnwidth\makeupwidth
   \global\divide\layoutcolumnwidth\layoutcolumns
   \dorecurse\layoutcolumns
     {\scratchdimen\layoutcolumnwidth
      \advance\scratchdimen\layoutcolumndistance
      \scratchcounter\recurselevel
      \advance\scratchcounter\minusone
      \scratchdimen\scratchcounter\scratchdimen
      \setxvalue{\??ly:c:\recurselevel}{\the\scratchdimen}}}

%\dorecurse\layoutcolumns
%  {\setxvalue{\??ly:c:\recurselevel}{\the\dimexpr
%     (\numexpr(\recurselevel-1)\dimexpr(\layoutcolumnwidth+\layoutcolumndistance))}}

\def\layoutcolumnoffset#1%
  {\executeifdefined{\??ly:c:#1}\!!zeropoint}

\def\checklayout
  {\ifdim\makeupheight=\layoutlines\lineheight \else \recalculatelayout \fi}

\appendtoks \checklayout \to \everystarttext

% document:
%
% \setuplayout[odd][state=stop] \setuplayout[even][state=stop] \setuplayout[page]
%
% \startstandardmakeup[page=blank] ... \stopstandardmakeup

\def\checkcurrentlayout % public and used in naw, so keep this name
  {\ifundefined{\??ly\realfolio\c!state}%
     \doifoddpageelse\checkcurrentoddlayout\checkcurrentevenlayout
   \else
     \doifvalue{\??ly\realfolio\c!state}\v!start
       {\xdef\currentlayout{\realfolio}\recalculatelayout}% new, global
   \fi}

\def\checkcurrentoddlayout
  {\ifundefined{\??ly\v!odd\c!state}\else
     \doifvalue{\??ly\v!odd\c!state}\v!start
       {\globallet\currentlayout\v!odd\recalculatelayout}% new, global
   \fi}

\def\checkcurrentevenlayout
  {\ifundefined{\??ly\v!even\c!state}\else
     \doifvalue{\??ly\v!even\c!state}\v!start
       {\globallet\currentlayout\v!even\recalculatelayout}% new, global
   \fi}

\appendtoks \checkcurrentlayout \to \everyaftershipout

\newif\ifdoublesidedprint

\def\presetcenterpagebox % in \setuplayout !!!!!!!!!!!!!!!!
  {\doublesidedprintfalse
   \ExpandFirstAfter\processallactionsinset
     [\@@lylocation]
     [     \v!middle=>{\setuppapersize[\c!left=\hss,\c!right=\hss,\c!top=\vss,\c!bottom=\vss]},
             \v!left=>{\setuppapersize[\c!left=,\c!right=\hss]},
            \v!right=>{\setuppapersize[\c!left=\hss,\c!right=]},
           \v!bottom=>{\setuppapersize[\c!top=\vss,\c!bottom=]},
              \v!top=>{\setuppapersize[\c!top=,\c!bottom=\vss]},%
      \v!doublesided=>\doublesidedprinttrue,
      \v!singlesided=>\doublesidedprintfalse]}

\def\definelayout
  {\dodoubleargument\dodefinelayout}

\def\dodefinelayout[#1][#2]%
  {\getparameters[\??ly#1][\c!state=\v!start,#2]}

\def\dodosetuplayout[#1][#2]%
  {\ConvertToConstant\doifnot{#2}\v!reset % #2 ?
     {\getparameters[\??ly#1][#2]%
      \checkforems[#2]}}

% \def\dosetuplayout[#1][#2]%
%   {\let\currentlayout\empty
%    \ifsecondargument
%      \dodosetuplayout[#1][#2]%
%    \else\iffirstargument
%      \doifassignmentelse{#1}
%        {\dodosetuplayout[][#1]}
%        {\doifnot{#1}\v!reset{\def\currentlayout{#1}}}%
%    \fi\fi
%    \recalculatelayout
%    \checkcurrentlayout % here ?
%    \presetcenterpagebox}

% global needed for non-doublesided standardmakeup

\def\dosetuplayout[#1][#2]%
  {\globallet\currentlayout\empty % new, global
   \ifsecondargument
     \dodosetuplayout[#1][#2]%
   \else\iffirstargument
     \doifassignmentelse{#1}
       {\dodosetuplayout[][#1]}
       {\doifnot{#1}\v!reset{\xdef\currentlayout{#1}}}% new, global
   \fi\fi
   \recalculatelayout
   \checkcurrentlayout % here ?
   \presetcenterpagebox}

\def\setuplayout
  {\dodoubleempty\dosetuplayout}

\let\@@zaheight=\!!zeropoint

\def\dopushpagedimensions
  {\xdef\oldtextheight  {\the\textheight  }%
   \xdef\oldfooterheight{\the\footerheight}%
   \global\let\@@zaheight\@@zaheight}

\def\dopoppagedimensions
  {\global\textheight  \oldtextheight
   \global\footerheight\oldfooterheight
   \recalculatelayout
   \global\let\pushpagedimensions\dopushpagedimensions
   \global\let\poppagedimensions\relax}

\let\poppagedimensions  = \relax
\let\pushpagedimensions = \dopushpagedimensions

% Elke \csname ... \endcsname wordt ook aangemaakt, dus ook
% in een test met \doifdefined. Bij veel bladzijden kan dit
% te veel macro's kosten. Vandaar de set \adaptedpages. Het
% kost tijd, maar scheelt macro's.

\let\adaptedpages\empty

\def\adaptpagedimensions
  {\ifx\adaptedpages\empty\else
     \rawdoifinsetelse\realfolio\adaptedpages
       {\getvalue{\??za\realfolio}%
        \letbeundefined{\??za\realfolio}}
       \donothing
   \fi}

\def\checkpagedimensions
  {\poppagedimensions
   \adaptpagedimensions}

\def\reportpagedimensions
  {\ifx\poppagedimensions\relax \else
     \space\@@zaheight\space-\space
   \fi
   \realfolio}

\def\dodoadaptlayout[#1]%
  {\getparameters[\??za][\c!height=,\c!lines=0,#1]%
   \pushpagedimensions
   \ifcase\@@zalines\relax
     \showmessage\m!layouts1{\@@zaheight,\realfolio}%
   \else
     \showmessage\m!layouts1{\@@zalines\space\v!lines,\realfolio}%
     \def\@@zaheight{\@@zalines\openlineheight}%
   \fi
   \doifelse\@@zaheight\v!max
     {\balancedimensions\textheight\footerheight\footerheight}
     {\balancedimensions\textheight\footerheight\@@zaheight}%
   \ifdim\footerheight<\zeropoint
     \global\advance\textheight \footerheight
     \global\footerheight\zeropoint
     \global\xdef\@@zaheight{\layoutparameter\c!footer\space(\v!max)}%
   \fi
   \setvsize
   \global\pagegoal\vsize  % nog corrigeren voor insertions ?
   \recalculatelogos
   \recalculatebackgrounds
   \global\let\pushpagedimensions\relax
   \global\let\poppagedimensions\dopoppagedimensions}

\def\doadaptlayout[#1][#2]%
  {\doifelsenothing{#2}
     {\dodoadaptlayout[#1]}
     {\def\docommando##1%
        {\addtocommalist{##1}\adaptedpages
         \setgvalue{\??za##1}{\dodoadaptlayout[#2]}}%
      \processcommalist[#1]\docommando
      \adaptpagedimensions}}

\def\adaptlayout
  {\dodoubleempty\doadaptlayout}

% describe interface

%D Centering the paper area on the print area is determined
%D by the \type {top}, \type {bottom}, \type {left} and \type
%D {right} parameters.

\def\centerpagebox#1%
  {\printpaperwidth\paperscale\printpaperwidth
   \printpaperheight \paperscale\printpaperheight
   \setbox#1\vbox to \printpaperheight
     {\@@pptop
      \hbox to \printpaperwidth
        {\ifdoublesidedprint
           \doifbothsides \@@ppleft \box#1\@@ppright
           \orsideone     \@@ppleft \box#1\@@ppright
           \orsidetwo     \@@ppright\box#1\@@ppleft
         \od \else        \@@ppleft \box#1\@@ppright
         \fi}%
      \par
      \@@ppbottom}}

\def\offsetprintbox#1%
  {\ifdim\kopoffset=\zeropoint % \relax
     \ifdim\rugoffset=\zeropoint
       \donefalse
     \else
       \donetrue
     \fi
   \else
     \donetrue
   \fi
   \ifdone
     \edef\next{\wd#1\the\wd#1\ht#1\the\ht#1\dp#1\the\dp#1}%
     \setbox#1\vbox
       {%\forgetall
        \offinterlineskip
        \vskip\kopoffset
        \doifbothsides
          \hskip\rugoffset
        \orsideone
          \hskip\rugoffset
        \orsidetwo
          \hskip-\rugoffset
        \od
        \box#1}%
     \next
   \fi}

% \def\replicatebox#1#2#3%
%   {\setbox#1=\vbox
%      {%\forgetall
%       \offinterlineskip
%       \dorecurse{#3}
%         {\hbox{\dorecurse{#2}{\copy#1\hskip\@@lydx}\unskip}%
%          \vskip\@@lydy}
%       \unskip}}
%
% \def\replicatepagebox#1%
%   {\ifnum\@@lynx>0 \ifnum\@@lyny>0
%      \replicatebox{#1}{\@@lynx}{\@@lyny}%
%    \fi\fi}

\def\replicatepagebox#1%
  {\ifnum\@@lynx>\plusone
     \donetrue
   \else\ifnum\@@lyny>\plusone
     \donetrue
   \else
     \donefalse
   \fi\fi
   \ifdone
     \setbox#1\vbox
       {%\forgetall
        \offinterlineskip
        \dorecurse\@@lyny
          {\hbox{\dorecurse\@@lynx{\copy#1\hskip\@@lydx}\unskip}%
           \vskip\@@lydy}
        \unskip}%
   \fi}

\def\orientpagebodybox#1#2#3%
  {\ifnum#2#3>\zerocount
     \setbox#1\vbox
       {\edef\somerotation{\ifdubbelzijdig\ifodd\realpageno#2\else#3\fi\else#2\fi}%
        \dorotatebox\somerotation\hbox{\box#1}}%
   \fi}

\def\orientpaperbox#1%
  {\orientpagebodybox{#1}\paperorientation\paperreverse}

\def\orientprintbox#1%
  {\orientpagebodybox{#1}\printorientation\printreverse}

\def\mirrorpagebodybox#1#2%
  {\ifcase#2\or
     \setbox#1\vbox
       {\domirrorbox\vbox{\box#1}}%
   \fi}

\def\mirrorpaperbox#1%
  {\mirrorpagebodybox{#1}\papermirror}

\def\mirrorprintbox#1%
  {\mirrorpagebodybox{#1}\printmirror}

\def\scalepagebox#1%
  {\ifdim\@@lyscale\points=\onepoint \else
     \setbox#1\vbox
       {\scale[\c!sx=\@@lyscale,\c!sy=\@@lyscale]{\box#1}}%
     \paperwidth\@@lyscale\paperwidth
     \paperheight \@@lyscale\paperheight
   \fi}

\def\negateprintbox#1%
  {\ifnegateprintbox
     \negatecolorbox{#1}%
   \fi}

\def\pagecutmarksymbol    {\the\realpageno}
\def\pagecutmarklength    {.5cm}
\let\pagecutmarktoptext    \empty
\let\pagecutmarkbottomtext \empty

\def\extrapagecutmarkbottomtext
  {\rlap{\jobname}\hfill\currentdate\space-\space\currenttime\hfill\llap{\realfolio}}

\def\makepagecutbox % #1
  {\let\cutmarksymbol    \pagecutmarksymbol
   \let\cutmarklength    \pagecutmarklength
   \let\cutmarktoptext   \pagecutmarktoptext
   \let\cutmarkbottomtext\pagecutmarkbottomtext
   \makecutbox}

\def\addpagecutmarks % #1
  {\doifelse\@@lymarking\v!on
     \makepagecutbox
     {\doifelse\@@lymarking\v!empty
        {\let\pagecutmarksymbol\empty
         \let\pagecutmarktoptext\empty
         \let\pagecutmarkbottomtext\empty
         \makepagecutbox}
        {\doifelse\@@lymarking\v!text
           {\let\pagecutmarksymbol\empty
            \let\pagecutmarkbottomtext\extrapagecutmarkbottomtext
            \makepagecutbox}
           \gobbleoneargument}}}

\def\addpagecolormarks % #1
  {\doifelse\@@lymarking\v!color
     {\doaddpagecolormarks\colormarkbox}
     {\doifelse\@@lymarking\v!screen
        {\doaddpagecolormarks\rastermarkbox}
        \gobbleoneargument}}

\def\doaddpagecolormarks#1#2%
  {\makepagecutbox{#2}%
   \ifnum\horizontalcutmarks>2 \chardef\colormarkoffset4 \fi
   \ifnum\verticalcutmarks  >2 \chardef\colormarkoffset4 \fi
   #1{#2}}

\newif\ifdubbelzijdig \dubbelzijdigfalse
\newif\ifenkelzijdig  \enkelzijdigtrue

\def\doifsometextlineelse#1%
  {\edef\!!stringa{\csname\??tk#1\c!state\endcsname}%
   \ifx\!!stringa\v!none
     \@EA\secondoftwoarguments
   \else\ifx\!!stringa\v!high
     \@EAEAEA\secondoftwoarguments
   \else
     \@EAEAEA\firstoftwoarguments
   \fi\fi}

% NOG EENS NAGAAN WANNEER NU GLOBAL EN WANNEER NIET

\def\calculatevsizes % global needed in \recalculatelayoutregel
  {\redoglobal\textheight\makeupheight
   \doifsometextlineelse \v!header
     {\redoglobal\advance\textheight -\headerheight
      \redoglobal\advance\textheight -\headerdistance}%
     \donothing
   \doifsometextlineelse \v!footer
     {\redoglobal\advance\textheight -\footerheight
      \redoglobal\advance\textheight -\footerdistance}%
     \donothing
   \ifzeropt\headerheight
     \resetsystemmode\v!header
   \else
     \setsystemmode\v!header
   \fi
   \ifzeropt\footerheight
     \resetsystemmode\v!footer
   \else
     \setsystemmode\v!footer
   \fi
   \resetglobal
   \setvsize}

\def\calculatereducedvsizes
  {\textheight\makeupheight
   \doifsometextlineelse \v!header
     {\advance\textheight -\headerheight
      \advance\textheight -\headerdistance}%
     {\headerheight\zeropoint
      \setlayoutdistances}%
   \doifsometextlineelse \v!footer
     {\advance\textheight -\footerheight
      \advance\textheight -\footerdistance}%
     {\footerheight\zeropoint
      \setlayoutdistances}}

\def\freezetextwidth        % \makeupwidth may be set to \textwidth
  {\textwidth\makeupwidth % which is a tricky but valid value
   \doifsomething{\layoutparameter\c!textwidth}%
     {\textwidth\layoutparameter\c!textwidth}%         % local
   \doifsomething{\layoutparameter\c!textmargin}%
     {\advance\textwidth-\layoutparameter\c!textmargin    % local
      \advance\textwidth-\layoutparameter\c!textmargin}}  % local

\def\calculatehsizes
  {\freezetextwidth
   \sethsize}

% De onderstaande macro voert commando's uit, afhankelijk van
% het karakter van het paginanummer.
%
% \doifoddpageelse{then-commando}{else-commando}

%D When we start at an even page, we need to swap the layout
%D differently. We cannot adapt the real page number, since
%D it is used in cross referencing. The next switch is set
%D when we start at an even page.

\newif\ifshiftedrealpageno

% \def\doifoddpageelse#1#2%
%   {\ifshiftedrealpageno
%      \ifodd\realpageno#2\else#1\fi
%    \else
%      \ifodd\realpageno#1\else#2\fi
%    \fi}

\def\doifoddpageelse
  {\ifshiftedrealpageno
     \ifodd\realpageno
       \@EAEAEA\secondoftwoarguments
     \else
       \@EAEAEA\firstoftwoarguments
     \fi
   \else
     \ifodd\realpageno
       \@EAEAEA\firstoftwoarguments
     \else
       \@EAEAEA\secondoftwoarguments
     \fi
   \fi}

\let\doifonevenpaginaelse\doifoddpageelse

% what are those \relax'es doing there?

\def\doifbothsidesoverruled#1\orsideone#2\orsidetwo#3\od
  {\ifdubbelzijdig
     \doifoddpageelse{#2}{#3}\relax
   \else
     #1\relax
   \fi}

\def\doifbothsides#1\orsideone#2\orsidetwo#3\od
  {\ifdubbelzijdig
     \ifenkelzijdig
       #1\relax
     \else
       \doifoddpageelse{#2}{#3}\relax
     \fi
   \else
     #1\relax
   \fi}

\newdimen\texthoffset

\def\settexthoffset
  {\doifbothsides
     \texthoffset\backspace
   \orsideone
     \texthoffset\backspace
   \orsidetwo
     \texthoffset\paperwidth
     \advance\texthoffset-\backspace
     \advance\texthoffset-\makeupwidth
   \od}

\def\goleftonpage
  {\hskip-\leftmargindistance
   \hskip-\leftmarginwidth
   \hskip-\leftedgedistance
   \hskip-\leftedgewidth}

\def\doifmarginswapelse#1#2%
  {\doifbothsides#1\orsideone#1\orsidetwo#2\od}

\def\swapmargins
  {\doifmarginswapelse\relax\doswapmargins}

\def\doswapmargins
  {\let\swapmargins  \relax % to prevent local swapping
   \let\doswapmargins\relax % to prevent local swapping
   \the\everyswapmargins}

\def\rightorleftpageaction
  {\ifdubbelzijdig
     \ifenkelzijdig
       \@EAEAEA\firstoftwoarguments
     \else
       \@EAEAEA\doifoddpageelse
     \fi
   \else
     \@EA\firstoftwoarguments
   \fi}

\def\outermarginwidth   {\rightorleftpageaction\rightmarginwidth   \leftmarginwidth }
\def\innermarginwidth   {\rightorleftpageaction\leftmarginwidth    \rightmarginwidth}
\def\outermargindistance{\rightorleftpageaction\rightmargindistance\leftmargindistance }
\def\innermargindistance{\rightorleftpageaction\leftmargindistance \rightmargindistance}

\def\outeredgewidth     {\rightorleftpageaction\rightedgewidth   \leftedgewidth }
\def\inneredgewidth     {\rightorleftpageaction\leftedgewidth    \rightedgewidth}
\def\outeredgedistance  {\rightorleftpageaction\rightedgedistance\leftedgedistance }
\def\inneredgedistance  {\rightorleftpageaction\leftedgedistance \rightedgedistance}

\def\outerspacewidth    {\rightorleftpageaction\cutspace \backspace }
\def\innerspacewidth    {\rightorleftpageaction\backspace\cutspace}

\beginETEX \dimexpr

  \def\leftmargintotal {\dimexpr\leftmarginwidth +\leftmargindistance \relax}
  \def\rightmargintotal{\dimexpr\rightmarginwidth+\rightmargindistance\relax}
  \def\leftedgetotal   {\dimexpr\leftedgewidth   +\leftedgedistance   \relax}
  \def\rightedgetotal  {\dimexpr\rightedgewidth  +\rightedgedistance  \relax}

  \def\leftsidetotal   {\dimexpr\leftmarginwidth +\leftedgetotal \relax}
  \def\rightsidetotal  {\dimexpr\rightmarginwidth+\rightedgetotal\relax}
  \def\leftcombitotal  {\dimexpr\leftmargintotal +\leftedgetotal \relax}
  \def\rightcombitotal {\dimexpr\rightmargintotal+\rightedgetotal\relax}

  \def\innermargintotal{\dimexpr\innermarginwidth+\innermargindistance\relax}
  \def\outermargintotal{\dimexpr\outermarginwidth+\outermargindistance\relax}
  \def\inneredgetotal  {\dimexpr\inneredgewidth  +\inneredgedistance  \relax}
  \def\outeredgetotal  {\dimexpr\outeredgewidth  +\outeredgedistance  \relax}

  \def\innercombitotal {\dimexpr\innermargintotal+\inneredgetotal\relax}
  \def\outercombitotal {\dimexpr\outermargintotal+\outeredgetotal\relax}
  \def\innersidetotal  {\dimexpr\innermarginwidth+\inneredgetotal\relax}
  \def\outersidetotal  {\dimexpr\outermarginwidth+\outeredgetotal\relax}

\endETEX

%D \macros
%D   {startlocallayout}
%D
%D These macros should be used with care. They permit local
%D layouts (as used in fitting pages, see \type {page-app.tex}).

%D This is kind of obsolete now that we have \type
%D {\definelayout}, so this hack will disappear in future
%D versions.

\def\startlocallayout
  {\globalpushmacro\restorepapersize
   \globalpushmacro\currentlayout}

\def\stoplocallayout
  {\globalpopmacro\currentlayout
   \globalpopmacro\restorepapersize
   \restorepapersize
   \setuplayout}

%D \macros
%D   {showprint, showframe, showlayout, showsetups}
%D
%D We predefine a couple of tracing macros.
%D
%D \showsetup{showprint}
%D \showsetup{showframe}
%D \showsetup{showlayout}
%D \showsetup{showsetups}
%D %showsetup{showmargins}

\fetchruntimecommand \showprint   {page-run}
\fetchruntimecommand \showframe   {page-run}
\fetchruntimecommand \showlayout  {page-run}
\fetchruntimecommand \showsetups  {page-run}
\fetchruntimecommand \showmargins {page-run}

%D The default dimensions are quite old and will not change.
%D The funny fractions were introduced when we went from fixed
%D dimensions to relative ones. Since \CONTEXT\ is a dutch
%D package, the dimensions are based on the metric system. The
%D asymmetrical layout is kind of handy for short
%D quick||and||dirty stapled documents.
%D
%D Although valid, it is not a real good idea to use
%D dimensions based on the \type {em} unit. First of all,
%D since there are no fonts loaded yet, this dimension makes
%D no sense, and second, you would loose track of values,
%D since they could change while going to a new page,
%D depending on the current font setting.

\setuplayout
  [             \c!topspace=.08417508418\paperheight,  %  2.5cm
                     \c!top=\!!zeropoint,
             \c!topdistance=\!!zeropoint,
                  \c!header=.06734006734\paperheight,  %  2.0cm
          \c!headerdistance=\!!zeropoint,
                  \c!height=.84175084175\paperheight,  % 25.0cm
          \c!footerdistance=\layoutparameter\c!headerdistance,
                  \c!footer=.06734006734\paperheight,  %  2.0cm
          \c!bottomdistance=\layoutparameter\c!topdistance,
                  \c!bottom=\!!zeropoint,
               \c!backspace=.11904761905\paperwidth, %  2.5cm
                    \c!edge=\!!zeropoint,
            \c!edgedistance=\layoutparameter\c!margindistance,
                  \c!margin=.12649983170\paperwidth, %  snijwit-2*afstand
          \c!margindistance=.02008341748\paperwidth, %  12.0pt
                \c!leftedge=\layoutparameter\c!edge,
        \c!leftedgedistance=\layoutparameter\c!edgedistance,
              \c!leftmargin=\layoutparameter\c!margin,
      \c!leftmargindistance=\layoutparameter\c!margindistance,
                   \c!width=.71428571429\paperwidth, %  15.0cm
     \c!rightmargindistance=\layoutparameter\c!margindistance,
             \c!rightmargin=\layoutparameter\c!margin,
       \c!rightedgedistance=\layoutparameter\c!edgedistance,
               \c!rightedge=\layoutparameter\c!edge,
               \c!veroffset=\!!zeropoint,
             \c!bottomspace=\!!zeropoint,
               \c!horoffset=\!!zeropoint,
                \c!cutspace=\!!zeropoint,
               \c!textwidth=, % dangerous option -> centered / local
              \c!textmargin=, % dangerous option -> both sides
            \c!textdistance=\!!zeropoint, % shift down on grid
                   \c!style=,
                   \c!color=,
                 \c!marking=\v!off,
                \c!location=, % \v!singlesided, but empty is signal
                   \c!scale=1,
                      \c!nx=1,
                      \c!ny=1,
                      \c!dx=\!!zeropoint,
                      \c!dy=\!!zeropoint,
                    \c!grid=\v!no,
                  \c!preset=,
                  \c!setups=\systemsetupsprefix\s!default,
              \c!clipoffset=\!!zeropoint,
                   \c!lines=0,
                   \c!paper=, % for foxet
                    \c!page=, % for foxet
                 \c!columns=1,
          \c!columndistance=\!!zeropoint]

%D First we define a whole range of (DIN) papersizes,
%D of which the A-series makes most sense. We enable checking.

%D We also set some of the parameters that will be used when
%D positioning the typeset paper onto the print paper.

\setuppaper % (size) % only used in XY imposition
  [\c!width=\zeropoint,
   \c!height=\zeropoint,
   \c!topspace=\zeropoint,
   \c!backspace=\zeropoint,
   \c!dx=\zeropoint,
   \c!dy=\zeropoint,
   \c!nx=1,
   \c!ny=1]

\setuppapersize
  [\c!option=\v!max,
   \c!top=,
   \c!bottom=\vss,
   \c!left=,
   \c!right=\hss]

\definepapersize [A0]  [\c!width=841mm,\c!height=1189mm]
\definepapersize [A1]  [\c!width=594mm,\c!height=841mm]
\definepapersize [A2]  [\c!width=420mm,\c!height=594mm]
\definepapersize [A3]  [\c!width=297mm,\c!height=420mm]
\definepapersize [A4]  [\c!width=210mm,\c!height=297mm]
\definepapersize [A5]  [\c!width=148mm,\c!height=210mm]
\definepapersize [A6]  [\c!width=105mm,\c!height=148mm]
\definepapersize [A7]  [\c!width=74mm,\c!height=105mm]
\definepapersize [A8]  [\c!width=52mm,\c!height=74mm]
\definepapersize [A9]  [\c!width=37mm,\c!height=52mm]
\definepapersize [A10] [\c!width=26mm,\c!height=37mm]

\definepapersize [B0]  [\c!width=1000mm,\c!height=1414mm]
\definepapersize [B1]  [\c!width=707mm,\c!height=1000mm]
\definepapersize [B2]  [\c!width=500mm,\c!height=707mm]
\definepapersize [B3]  [\c!width=353mm,\c!height=500mm] % [\c!width=354mm,\c!height=500mm]
\definepapersize [B4]  [\c!width=250mm,\c!height=353mm] % [\c!width=250mm,\c!height=354mm]
\definepapersize [B5]  [\c!width=176mm,\c!height=250mm] % [\c!width=177mm,\c!height=250mm]
\definepapersize [B6]  [\c!width=125mm,\c!height=176mm] % [\c!width=125mm,\c!height=177mm]
\definepapersize [B7]  [\c!width=88mm,\c!height=125mm]
\definepapersize [B8]  [\c!width=62mm,\c!height=88mm]   % [\c!width=63mm,\c!height=88mm]
\definepapersize [B9]  [\c!width=44mm,\c!height=62mm]   % [\c!width=44mm,\c!height=63mm]
\definepapersize [B10] [\c!width=31mm,\c!height=44mm]

\definepapersize [C0]  [\c!width=917mm,\c!height=1297mm]
\definepapersize [C1]  [\c!width=648mm,\c!height=917mm] % [\c!width=649mm,\c!height=917mm]
\definepapersize [C2]  [\c!width=458mm,\c!height=648mm] % [\c!width=459mm,\c!height=649mm]
\definepapersize [C3]  [\c!width=324mm,\c!height=458mm] % [\c!width=324mm,\c!height=459mm]
\definepapersize [C4]  [\c!width=229mm,\c!height=324mm]
\definepapersize [C5]  [\c!width=162mm,\c!height=229mm]
\definepapersize [C6]  [\c!width=114mm,\c!height=162mm] % [\c!width=115mm,\c!height=162mm]
\definepapersize [C7]  [\c!width=81mm,\c!height=114mm]  % [\c!width=81mm,\c!height=115mm]
\definepapersize [C8]  [\c!width=57mm,\c!height=81mm]
\definepapersize [C9]  [\c!width=40mm,\c!height=57mm]
\definepapersize [C10] [\c!width=28mm,\c!height=40mm]

%D Per August 2004 the rounding of some (seldom used) sizes
%D were corrected top the latest DIN specifications. Peter
%D Rolf came up with these and a few more missing sizes.
%D Watch out: spaces and slashes!

\definepapersize [4 A0]  [\c!width=1682mm,\c!height=2378mm]
\definepapersize [2 A0]  [\c!width=1189mm,\c!height=1682mm]
\definepapersize [C6/C5] [\c!width=114mm,\c!height=229mm]

%D Because there are no standardized screen sizes, we define
%D a bunch of sizes with $4:3$ ratios. The \type {S6} size is
%D nearly as wide as a sheet of \type {A4} paper.

\definepapersize [S3] [\c!width=300pt,\c!height=225pt]
\definepapersize [S4] [\c!width=400pt,\c!height=300pt]
\definepapersize [S5] [\c!width=500pt,\c!height=375pt]
\definepapersize [S6] [\c!width=600pt,\c!height=450pt]
\definepapersize [S8] [\c!width=800pt,\c!height=600pt]
\definepapersize [SW] [\c!width=800pt,\c!height=450pt]
\definepapersize [SM] [\c!width=720pt,\c!height=450pt]

%D These are handy too:

\definepapersize [S33] [\c!width=300pt,\c!height=300pt]
\definepapersize [S44] [\c!width=400pt,\c!height=400pt]
\definepapersize [S55] [\c!width=500pt,\c!height=500pt]
\definepapersize [S66] [\c!width=600pt,\c!height=600pt]

%D One may wonder if \TEX\ should be used for typesetting
%D \CDROM\ covers, but it does not hurt to have the paper size
%D ready.

\definepapersize [CD] [\c!width=120mm,\c!height=120mm]

%D The next series is for our English speaking friends who
%D decided to stick to non metric values. Thanks to Nelson
%D Beebe for completing the inch based list.

\definepapersize [letter]    [\c!width=8.5in,\c!height=11in]
\definepapersize [ledger]    [\c!width=11in,\c!height=17in]
\definepapersize [tabloid]   [\c!width=17in,\c!height=11in]

\definepapersize [legal]     [\c!width=8.5in,\c!height=14in]
\definepapersize [folio]     [\c!width=8.5in,\c!height=13in]
\definepapersize [executive] [\c!width=7.25in,\c!height=10.5in]

\definepapersize [A] [\c!width=8.5in,\c!height=11in] % 1 sheet
\definepapersize [B] [\c!width=11in,\c!height=17in]  % 2 sheets
\definepapersize [C] [\c!width=17in,\c!height=22in]  % 4 sheets

%D The next set is for Tobias Burnus, who gave me the sizes.

\definepapersize [envelope 9]  [\c!width=8.88in,\c!height=3.88in]
\definepapersize [envelope 10] [\c!width=9.5in,\c!height=4.13in]
\definepapersize [envelope 11] [\c!width=10.38in,\c!height=4.5in]
\definepapersize [envelope 12] [\c!width=11.0in,\c!height=4.75in]
\definepapersize [envelope 14] [\c!width=11.5in,\c!height=5.0in]
\definepapersize [monarch]     [\c!width=7.5in,\c!height=3.88in]
\definepapersize [check]       [\c!width=8.58in,\c!height=3.88in]
\definepapersize [DL]          [\c!width=110mm,\c!height=220mm] % [\c!width=220mm,\c!height=110mm]
\definepapersize [E4]          [\c!width=280mm,\c!height=400mm]

%D The next three sets are supplied by Taco:

\definepapersize [RA0]  [\c!width=860mm,\c!height=1220mm]
\definepapersize [RA1]  [\c!width=610mm,\c!height=860mm]
\definepapersize [RA2]  [\c!width=430mm,\c!height=610mm]
\definepapersize [RA3]  [\c!width=305mm,\c!height=430mm]
\definepapersize [RA4]  [\c!width=215mm,\c!height=305mm]

%D ISO SRA (supplementary raw A) sizes:

\definepapersize [SRA0]  [\c!width=900mm,\c!height=1280mm]
\definepapersize [SRA1]  [\c!width=640mm,\c!height=900mm]
\definepapersize [SRA2]  [\c!width=450mm,\c!height=640mm]
\definepapersize [SRA3]  [\c!width=320mm,\c!height=450mm]
\definepapersize [SRA4]  [\c!width=225mm,\c!height=320mm]

%D Industry invention:

\definepapersize [A3plus] [\c!width=329mm,\c!height=483mm]

%D We can now default to a reasonable size. We match the print
%D paper size with the typeset paper size. This setting should
%D come after the first layout specification (already done).

\setuppapersize
  [A4][A4]

%D A few goodies:

\beginETEX \dimexpr

\definepapersize
  [oversized]
  [\c!width=\dimexpr(\paperwidth+1.5cm),
   \c!height=\dimexpr(\paperheight+1.5cm)]

\definepapersize
  [doublesized]
  [\c!width=\dimexpr(\paperwidth),
   \c!height=\dimexpr(2\paperheight)]

\definepapersize
  [doubleoversized]
  [\c!width=\dimexpr(\paperheight+1.5cm),
   \c!height=\dimexpr(2\paperwidth+1.5cm)]

%D For orthogonality:

\definepapersize
  [undersized]
  [\c!width=\dimexpr(\paperwidth-1.5cm),
   \c!height=\dimexpr(\paperheight-1.5cm)]

\endETEX

\definelayout
  [\v!page]
  [\c!backspace=\zeropoint,
   \c!cutspace=\zeropoint,
   \c!topspace=\zeropoint,
   \c!bottomspace=\zeropoint,
   \c!margin=\zeropoint,
   \c!edge=\zeropoint,
   \c!header=\zeropoint,
   \c!footer=\zeropoint,
   \c!top=\zeropoint,
   \c!bottom=\zeropoint,
   \c!leftmargin=\zeropoint,
   \c!rightmargin=\zeropoint,
   \c!leftedge=\zeropoint,
   \c!rightedge=\zeropoint,
   \c!textdistance=\zeropoint,
   \c!width=\v!middle,
   \c!height=\v!middle,
   \c!lines=0,
   \c!grid=\v!no]

\definelayout
  [\v!middle]
  [\c!width=\v!middle,
   \c!height=\v!middle]

\protect \endinput
