page-lay.mkii / last modification: 2020-01-30 14:15
%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 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 / Layout Specification}

%D This module is now etex dependent.

% 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\topoffset            \topoffset            = 0pt
\newdimen\backoffset           \backoffset           = \topoffset

\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%
  {\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}

%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}

% these are wrong in the running text, assumes some swapping, needs a cleanup

\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
%   non etex calculations used to go here
% \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\definepapersize
  {\dotripleempty\dodefinepapersize}

\def\dodefinepapersize[#1][#2][#3]%
  {\ifsecondargument
     \doifsomething{#1} % to be sure
       {\doifassignmentelse{#2}
          {\getparameters
             [\??pp#1] % geen \c!scale, scheelt hash ruimte
             [\c!width=\@@ppwidth,\c!height=\@@ppheight,\c!offset=\@@ppoffset,#2]}
          {\setvalue{\??pp:1:#1}{#2}%
           \setvalue{\??pp:2:#1}{#3}}}%
   \else
     \getparameters[\??pp][#1]%
     \setuppapersize % hm. this will freeze !
   \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 Yet undocumented, let's see if it gets noticed.
%D
%D \starttyping
%D \definepapersize[main] [A4]          [A4]
%D \definepapersize[extra][A4,landscape][A4,landscape]
%D
%D \starttext
%D     \setuppapersize[main]
%D     Page 1. \page
%D     Page 2. \page
%D     \setuppapersize[extra]
%D     Page 2 \page
%D     \setuppapersize[main]
%D     Page 3. \page
%D     Page 4. \page
%D     \adaptpapersize[extra]
%D     Page 5. \page
%D     Page 6. \page
%D \stoptext
%D \stoptyping

%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 % beware: later no chardef
\chardef\paperreverse    =0  \chardef\printreverse    =0
\chardef\paperlandscape  =0  \chardef\printlandscape  =0

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

\def\paperscale{1} \newif\ifnegateprintbox

\def\setuppaper[#1]%
  {\getparameters[\??pp][\c!paper=,\c!page=,#1]%
   \edef\@@ppxy{\the\numexpr\@@ppnx*\@@ppny\relax}%
   \doifelsenothing\@@pppage
     {\doifelsenothing\@@pppaper
        {} % {\setuppapersize} % added, removed, no need for, too tricky, mag-01 (fixed by freezing pp's)
        {\dodosetuppapersize[\papersize][\@@pppaper]}}
     {\doifelsenothing\@@pppaper
        {\dodosetuppapersize[\@@pppage][\printpapersize]}
        {\dodosetuppapersize[\@@pppage][\@@pppaper]}}}

\def\setuppapersize
  {\dodoubleempty\dosetuppapersize}

\def\dosetuppapersize[#1][#2]%
  {\doifassignmentelse{#1}
     {\setuppaper[#1]}
     {\doifelsenothing{#2}
        {\expanded{\dodosetuppapersize
           [\executeifdefined{\??pp:1:#1}{#1}]%
           [\executeifdefined{\??pp:2:#1}{\v!default}]}}
        {\doifassignmentelse{#2}
           {\getparameters[\??pp\executeifdefined{\??pp:1:#1}{#1}][#2]}
           {\expanded{\dodosetuppapersize
              [\executeifdefined{\??pp:1:#1}{#1}]%
              [\executeifdefined{\??pp:1:#2}{#2}]}}}}}

\let\reinstatepapersize\relax

\def\adaptpapersize
  {\global\let\reinstatepapersize\restorepapersize
   \setuppapersize}

\appendtoks
  \reinstatepapersize
  \global\let\reinstatepapersize\relax
\to \everyaftershipout

\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}

% dimen freeze is a bit tricky, but catches local redundant calls
% with values where e.g width is defined in terms of \paperwidth

\def\dododosetuppapersize[#1][#2]%
  {\xdef\restorepapersize{\noexpand\setuppapersize[#1][#2]}%
   \dosetuppaperorientation{#1}\paperlandscape\paperorientation\paperreverse\papermirror
   \dosetuppaperorientation{#2}\printlandscape\printorientation\printreverse\printmirror
   \def\docommand##1%
     {\doifsomething{##1}{\doifdefined{\??pp##1\c!width}
        {\global\paperwidth \getvalue{\??pp##1\c!width}%
         \global\paperheight\getvalue{\??pp##1\c!height}%
\ifinpagebody
  \setevalue{\??pp##1\c!height}{\the\paperheight}%
  \setevalue{\??pp##1\c!width }{\the\paperwidth }%
\fi
         \calculatepaperoffsets{##1}%
         \xdef\papersize{##1}}}}%
   \processcommacommand[#1]\docommand
   \doifdefinedelse{\??pp#1\c!scale}
     {\edef\paperscale{\getvalue{\??pp#1\c!scale}}}
     {\edef\paperscale{1}}%
   \def\docommand##1%
     {\doifsomething{##1}{\doifdefined{\??pp##1\c!width}
        {\global\printpaperwidth \getvalue{\??pp##1\c!width}%
         \global\printpaperheight\getvalue{\??pp##1\c!height}%
\ifinpagebody
  \setevalue{\??pp##1\c!height}{\the\printpaperheight}%
  \setevalue{\??pp##1\c!width }{\the\printpaperwidth}%
\fi
         \xdef\printpapersize{##1}}}}%
   \processcommacommand[#2]\docommand
   \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
      \ifcase\paperorientation\else
        \doifinset\paperorientation{90,270}{\swapdimens\paperwidth\paperheight}%
      \fi
      \ifcase\printorientation\else
        \doifinset\printorientation{90,270}{\swapdimens\printpaperwidth\printpaperheight}%
      \fi
      \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\docommand##1%
     {\beforesplitstring##1\at em\to\asciia
      \doifnot\asciia{##1}
        {\aftersplitstring\asciia\at=\to\asciia
         \doifsomething\asciia
           {\showmessage\m!systems{10}{##1}}}}%
   \processcommalist[#1]\docommand}

\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\dimexpr\paperwidth-\backspace-\cutspace\relax}
     {\doifelse{\layoutparameter\c!width}\v!fit
        {\ifdim\cutspace=\zeropoint
           \global\cutspace\backspace
         \fi
         \global\makeupwidth\dimexpr\paperwidth-\cutspace\relax
         \scratchdimen\dimexpr\backspace
           -\leftedgewidth  -\leftedgedistance
           -\leftmarginwidth-\leftmargindistance\relax
         \ifdim\scratchdimen<\zeropoint
           \scratchdimen\zeropoint
         \fi
         \global\advance\makeupwidth\dimexpr
           -\rightmargindistance-\rightmarginwidth
           -\rightedgedistance  -\rightedgewidth
           -\scratchdimen\relax}
        {\global\makeupwidth\layoutparameter\c!width\relax
         \ifdim\cutspace=\zeropoint
           \global\cutspace\dimexpr\paperwidth-\makeupwidth-\backspace\relax
        % \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\dimexpr\paperheight-\topspace-\bottomspace\relax}
       {\doifelse{\layoutparameter\c!height}\v!fit
          {\ifdim\bottomspace=\zeropoint
            \global\bottomspace\topspace
           \fi
           \global\makeupheight\dimexpr\paperheight-\bottomspace\relax
           \scratchdimen\dimexpr\topspace-\topheight-\topdistance\relax
           \ifdim\scratchdimen<\zeropoint
             \scratchdimen\zeropoint
           \fi
           \global\advance\makeupheight\dimexpr-\bottomdistance-\bottomheight-\scratchdimen\relax}
          {\global\makeupheight\layoutparameter\c!height\relax
           \ifdim\bottomspace=\zeropoint
             \global\bottomspace\dimexpr\paperheight-\makeupheight-\topspace\relax
           \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\dimexpr
        \layoutparameter\c!lines\lineheight-\strutheight+\topskip+
        \headerdistance+\headerheight+\footerdistance+\footerheight\relax
   \fi
   \backoffset\layoutparameter\c!horoffset
   \topoffset \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\dimexpr\makeupwidth-\layoutcolumns\layoutcolumndistance+\layoutcolumndistance\relax
   \global\divide\layoutcolumnwidth\layoutcolumns
   \dorecurse\layoutcolumns
     {\setxvalue{\??ly:c:\recurselevel}%
        {\the\numexpr\recurselevel-\plusone\relax\dimexpr\layoutcolumnwidth+\layoutcolumndistance\relax}}}

%\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\changetolayout#1%
  {%\writestatus\m!layouts{changing to layout #1}%
   \xdef\currentlayout{#1}\recalculatelayout}

\def\checkcurrentoddlayout
  {\ifcsname\??ly\v!odd\c!state\endcsname
     \doifvalue{\??ly\v!odd\c!state}\v!start{\changetolayout\v!odd}%
   \fi}

\def\checkcurrentevenlayout
  {\ifcsname\??ly\v!even\c!state\endcsname
     \doifvalue{\??ly\v!even\c!state}\v!start{\changetolayout\v!even}%
   \fi}

\ifx\lastpage\undefined \def\lastpage{1} \fi

\def\reverserealfolio
  {\ifnum\lastpage>\plusone
     \ifnum\lastpage=\realfolio
       \v!last
     \else\ifnum\plusone=\realfolio
       \v!first
     \else
       \the\numexpr\realfolio-\lastpage\relax
     \fi\fi
   \else
     \!!zerocount
   \fi}

\def\checkcurrentlayout % public and used in naw, so keep this name
  {\ifcsname\??ly\realfolio\c!state\endcsname
     \doifvalue{\??ly\realfolio\c!state}\v!start{\changetolayout\realfolio}%
   \else\ifcsname\??ly\reverserealfolio\c!state\endcsname
     \doifvalue{\??ly\reverserealfolio\c!state}\v!start{\changetolayout\reverserealfolio}%
   \else\ifcsname\??ly\v!current\c!state\endcsname
     \changetolayout\v!current % no start test ?
   \else
     \doifoddpageelse\checkcurrentoddlayout\checkcurrentevenlayout
   \fi\fi\fi}

% testcase
%
% \setuppagenumbering[alternative=doublesided]
%
% \setuplayout         [width=11cm]
% \definelayout [odd]  [backspace=1cm]
% \definelayout [even] [backspace=4cm]
% \definelayout [5]    [backspace=5cm]
% \definelayout [6]    [backspace=5cm]
% \definelayout [-2]   [backspace=0cm,cutspace=0cm]
% \definelayout [last] [backspace=0cm,cutspace=0cm]
%
% \checkcurrentlayout \showframe
%
% \starttext
%     \dorecurse{20} {\input knuth \endgraf \input tufte \endgraf}
% \stoptext

%appendtoks \checkcurrentlayout \to \everyaftershipout % no
\appendtoks \checkcurrentlayout \to \everystarttext

\appendtoks
  \dochecknextlayout
\to \everyaftershipout

% \def\dochecknextlayout
%   {\ifx\currentlayout\v!current
%      % prevent redundant calculations
%    \else
%      \globallet\currentlayout\empty
%      \global\letbeundefined{\??ly\v!current\c!state}%
%      \checkcurrentlayout
%    \fi}
%
% this breaks after a standardmakeup

\def\dochecknextlayout{\checkcurrentlayout}

\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]}}

% 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\the\dimexpr\@@zaheight\relax\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\docommand##1%
        {\addtocommalist{##1}\adaptedpages
         \setgvalue{\??za##1}{\dodoadaptlayout[#2]}}%
      \processcommalist[#1]\docommand
      \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}
             {\@@ppleft \box#1\@@ppright}
             {\@@ppright\box#1\@@ppleft }%
         \else
              \@@ppleft \box#1\@@ppright
         \fi}%
      \par
      \@@ppbottom}}

\def\offsetprintbox#1%
  {\ifdim\topoffset=\zeropoint % \relax
     \ifdim\backoffset=\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\topoffset
        \doifbothsides{\hskip\backoffset}{\hskip\backoffset}{\hskip-\backoffset}%
        \box#1}%
     \next
   \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{\ifdoublesided\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}

\setvalue{\??ly:n:\c!marking:\v!on}%
  {\makepagecutbox}

\setvalue{\??ly:n:\c!marking:\v!page}% only at outer when nx/ny > 0
  {\makepagecutbox}

\setvalue{\??ly:n:\c!marking:\v!empty}%
  {\let\pagecutmarksymbol\empty
   \let\pagecutmarktoptext\empty
   \let\pagecutmarkbottomtext\empty
   \makepagecutbox}

\setvalue{\??ly:n:\c!marking:\v!text}%
  {\let\pagecutmarksymbol\empty
   \let\pagecutmarkbottomtext\extrapagecutmarkbottomtext
   \makepagecutbox}

\setvalue{\??ly:c:\c!marking:\v!color}%
  {\doaddpagecolormarks\colormarkbox}

\setvalue{\??ly:c:\c!marking:\v!screen}%
  {\doaddpagecolormarks\rastermarkbox}

\def\addpagecutmarks % #1
  {\executeifdefined{\??ly:n:\c!marking:\@@lymarking}\gobbleoneargument}

\def\addpagecolormarks % #1
  {\executeifdefined{\??ly:c:\c!marking:\@@lymarking}\gobbleoneargument}

\def\doaddpagecolormarks#1#2%
  {\makepagecutbox{#2}%
   \ifnum\horizontalcutmarks>2 \chardef\colormarkoffset4 \fi
   \ifnum\verticalcutmarks  >2 \chardef\colormarkoffset4 \fi
   #1{#2}}

% NOG EENS NAGAAN WANNEER NU GLOBAL EN WANNEER NIET

\ifx\doifelselayoutsomeline\undefined % defined in page-txt
    \let\doifelselayoutsomeline\secondofthreearguments
\fi

\def\calculatevsizes % global needed in \recalculatelayoutregel
  {\redoglobal\textheight\makeupheight
   \doifelselayoutsomeline \v!header
     {\redoglobal\advance\textheight-\dimexpr\headerheight+\headerdistance\relax}%
     \donothing
   \doifelselayoutsomeline \v!footer
     {\redoglobal\advance\textheight-\dimexpr\footerheight+\footerdistance\relax}%
     \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
   \doifelselayoutsomeline \v!header
     {\advance\textheight-\dimexpr\headerheight+\headerdistance\relax}%
     {\headerheight\zeropoint
      \setlayoutdistances}%
   \doifelselayoutsomeline \v!footer
     {\advance\textheight-\dimexpr\footerheight+\footerdistance\relax}%
     {\footerheight\zeropoint
      \setlayoutdistances}}

\newdimen\innermakeupwidth  % special purpose
\newdimen\innermakeupmargin % special purpose

\chardef\innermakeupcompensation\plusone

\def\compensatedinnermakeupmargin
  {\dimexpr\ifnum\innermakeupcompensation=\plusone+\innermakeupmargin\else\zeropoint\fi\relax}

\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
   \global\innermakeupwidth\textwidth
   \doifelsenothing{\layoutparameter\c!textmargin}
     {\global\innermakeupmargin\zeropoint}
     {\global\innermakeupmargin\layoutparameter\c!textmargin}%
   \global\advance\innermakeupwidth-\dimexpr\innermakeupmargin+\innermakeupmargin\relax
   \advance\textwidth-\dimexpr\innermakeupmargin+\innermakeupmargin\relax}   % 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#2#3%
%   {\ifdoublesided
%      \doifoddpageelse{#2}{#3}\relax
%    \else
%      #1\relax
%    \fi}

% \def\doifbothsides#1#2#3%
%   {\ifdoublesided
%      \ifsinglesided
%        #1\relax
%      \else
%        \doifoddpageelse{#2}{#3}\relax
%      \fi
%    \else
%      #1\relax
%    \fi}

\def\redoifoddpageelse#1{\doifoddpageelse}

\def\doifbothsidesoverruled
  {\ifdoublesided
     \@EA\redoifoddpageelse
   \else
     \@EA\firstofthreearguments
   \fi}

\def\doifbothsides
  {\ifdoublesided
     \ifsinglesided
       \@EAEAEA\firstofthreearguments
     \else
       \@EAEAEA\redoifoddpageelse
     \fi
   \else
     \@EA\firstofthreearguments
   \fi}

\newdimen\texthoffset

\def\settexthoffset
  {\doifbothsides
     {\texthoffset\backspace}
     {\texthoffset\backspace}
     {\texthoffset\dimexpr\paperwidth-\backspace-\makeupwidth\relax}}

\def\goleftonpage
  {\hskip-\dimexpr\leftmargindistance+\leftmarginwidth+\leftedgedistance+\leftedgewidth\relax}

\def\doifmarginswapelse#1#2%
  {\doifbothsides{#1}{#1}{#2}}

\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
  {\ifdoublesided
     \ifsinglesided
       \@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}

\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}

%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,
   \c!method=\v!normal]

\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 Swedish thesis formats:

\definepapersize [G5]  [\c!width=169mm,\c!height=239mm]
\definepapersize [E5]  [\c!width=155mm,\c!height=220mm]

%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).

% not interfaced !

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

\definepapersize
  [doublesized]
  [ \c!width=\dimexpr \paperwidth \relax,
   \c!height=\dimexpr2\paperheight\relax]

\definepapersize
  [doubleoversized]
  [ \c!width=\dimexpr \paperheight+1.5cm\relax,
   \c!height=\dimexpr2\paperwidth +1.5cm\relax]

\definepapersize
  [\v!default]
  [ \c!width=\paperwidth,
   \c!height=\paperheight]

\definepapersize
  [samesized]
  [ \c!width=\paperwidth,
   \c!height=\paperheight]

\setuppapersize
 [A4][samesized]

%D For orthogonality:

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

\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