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

\unprotect

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

\newdimension\paperheight          \paperheight          297mm
\newdimension\paperwidth           \paperwidth           210mm
\newdimension\paperoffset          \paperoffset          \zeropoint
\newdimension\paperbleed           \paperbleed           \zeropoint
\newdimension\spinewidth           \spinewidth           \zeropoint

\newdimension\printpaperheight     \printpaperheight     \paperheight
\newdimension\printpaperwidth      \printpaperwidth      \paperwidth

\newdimension\makeupheight                               % calculated
\newdimension\makeupwidth                                % calculated

\newdimension\textheight                                 % calculated
\newdimension\textwidth                                  % calculated

\newdimension\topspace             \topspace             2cm
\newdimension\backspace            \backspace            \topspace
\newdimension\cutspace             \cutspace             \zeropoint
\newdimension\bottomspace          \bottomspace          \zeropoint

\newdimension\headerheight         \headerheight         2cm
\newdimension\footerheight         \footerheight         \headerheight

\newdimension\topoffset            \topoffset            \zeropoint
\newdimension\backoffset           \backoffset           \topoffset

\newdimension\leftmarginwidth      \leftmarginwidth      3cm
\newdimension\rightmarginwidth     \rightmarginwidth     \leftmarginwidth

\newdimension\leftedgewidth        \leftedgewidth        3cm
\newdimension\rightedgewidth       \rightedgewidth       \leftedgewidth

\newdimension\topheight            \topheight            \zeropoint
\newdimension\bottomheight         \bottomheight         \topheight

\newinteger  \layoutlines          \layoutlines          \zerocount

\newinteger  \layoutcolumns        \layoutcolumns        \zerocount
\newdimension\layoutcolumndistance \layoutcolumndistance \zeropoint
\newdimension\layoutcolumnwidth    \layoutcolumnwidth    \zeropoint

\newinteger  \layoutrows            \layoutrows          \zerocount
\newdimension\layoutrowdistance     \layoutrowdistance   \zeropoint
\newdimension\layoutrowheight       \layoutrowheight     \zeropoint

\newdimension\totaltextwidth

\newdimension\textadaptive

% \permanent\protected\def\snappedtextheight
%   {\dimexpr
%      \ifcase\layoutlines
%        \numexpr
%          \dimexpr\textheight+\roundingeps+\openlineheight:\plustwo\relax:\openlineheight
%        \relax
%      \else
%        \layoutlines
%      \fi\openlineheight
%    \relax}

% \permanent\protected\def\xgetrawnoflines#1%
%   {\numexpr
%      % \dimexpr#1+\roundingeps+.5\openlineheight\relax:\openlineheight
%        \dimexpr#1+\roundingeps+\openlineheight:2\relax:\openlineheight
%    \relax}

\permanent\protected\def\snappedtextheight
  {\dimexpr
     \ifcase\layoutlines
       ((\textheight+\roundingeps+\openlineheight:\plustwo):\openlineheight)*%
     \else
       \layoutlines
     \fi
     \openlineheight
   \relax}

%D \starttyping
%D \setuplayout
%D   [columns=4,
%D    columndistance=1em,
%D    rows=5,
%D    rowdistance=2lh]
%D \stoptyping

\newuserunit \lineheight        lh
\newuserunit \layoutcolumnwidth lc
\newuserunit \layoutrowheight   lr

\newuserunit \globalbodyfontlineheight gl

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

\newdimension\margindistance
\newdimension\edgedistance
\newdimension\marginwidth
\newdimension\edgewidth

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

%D The horizontal distances are:

\newdimension\leftedgedistance
\newdimension\rightedgedistance
\newdimension\leftmargindistance
\newdimension\rightmargindistance

%D The vertical distances are:

\newdimension\topdistance
\newdimension\headerdistance
\newdimension\textdistance
\newdimension\footerdistance
\newdimension\bottomdistance

\newdimension\textovershoot % available in pagebuilder

%D We need to calculate the extra distances:

\newdimension\naturalmarginwidth
\newdimension\naturaledgewidth
\newdimension\naturalmargindistance
\newdimension\naturaledgedistance
\newdimension\naturalleftedgedistance
\newdimension\naturalrightedgedistance
\newdimension\naturalleftmargindistance
\newdimension\naturalrightmargindistance
\newdimension\naturaltopdistance
\newdimension\naturalheaderdistance
\newdimension\naturaltextdistance
\newdimension\naturalfooterdistance
\newdimension\naturalbottomdistance

%D Now we've come to the setup and definition commands.

\installcorenamespace{layout}
\installcorenamespace{layoutlocation}
\installcorenamespace{layoutalternative}
\installcorenamespace{layoutcolumn}
\installcorenamespace{layoutrow}
\installcorenamespace{layoutmethod}

\installswitchcommandhandler \??layout {layout} \??layout

\pushoverloadmode

    % don't set [1] because it interferes with initializing the text and one can just
    % start explicitly with \setuplayout[xxx] or so

    \let\page_layouts_define\definelayout % ugly hack for MS

    \permanent\protected\def\definelayout[#1]% intercept relative pages
      {\expanded{\page_layouts_define[\ifhastoks{+}{#1}\tointeger{\realpageno#1}\else#1\fi]}}

\popoverloadmode

\appendtoks
    \ifempty{\directlayoutparameter\c!state}%
      \letlayoutparameter\c!state\v!start
    \fi
\to \everydefinelayout

\newinteger\c_page_layouts_changed

\appendtoks
    \ifcase\layoutsetupmode
        % can't happen
    \or % \setuplayout[name][key=value]
      \ifx\previouslayout\currentlayout
        \letlayoutparameter\c!state\v!normal % global ? still needed ?
        \let\currentlayout\currentlayout
        \page_layouts_synchronize
        \page_layouts_check_next
      \fi
    \or % \setuplayout[key=value]
      % we can inherit so we always synchronize
      \letlayoutparameter\c!state\v!normal % global ? still needed ?
      \let\currentlayout\previouslayout
      \page_layouts_synchronize
      \page_layouts_check_next
    \or % \setuplayout[name]
      \ifx\currentlayout\v!reset
        \lettonothing\currentlayout
      \fi
      \letlayoutparameter\c!state\v!normal % global ? still needed ?
      \glet\currentlayout\currentlayout % global
      \page_layouts_synchronize
      \page_layouts_check_next
    \or % \setuplayout (reverts to main layout)
      \letlayoutparameter\c!state\v!normal % global ? still needed ?
      \glettonothing\currentlayout % global
      \page_layouts_synchronize
      \page_layouts_check_next
    \fi
    \global\c_page_layouts_changed\realpageno
\to \everysetuplayout

\permanent\def\doifelselayoutdefined#1%
  {\ifcsname\namedlayouthash{#1}\c!state\endcsname % maybe a helper
      \expandafter\firstoftwoarguments
   \else
      \expandafter\secondoftwoarguments
   \fi}

\aliased\let\doiflayoutdefinedelse\doifelselayoutdefined

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

\appendtoks
    \global\pageextragoal\zeropoint
\to \everysetuplayout

\appendtoks
    \global\pageextragoal \zeropoint
\to \everysetupoutputroutine

\permanent\integerdef\lasttextadaptive\zerocount

\prependtoks
	\lasttextadaptive\plusone
\to \everybeforeoutput

% \newconditional\reverselayout

\def\page_layouts_set_dimensions
  {\global\naturalmarginwidth        {\layoutparameter\c!margin}%
   \global\naturaledgewidth          {\layoutparameter\c!edge}%
   \global\naturalmargindistance     {\layoutparameter\c!margindistance}%
   \global\naturaledgedistance       {\layoutparameter\c!edgedistance}%
   \global\naturalleftedgedistance   {\layoutparameter\c!leftedgedistance}%
   \global\naturalrightedgedistance  {\layoutparameter\c!rightedgedistance}%
   \global\naturalleftmargindistance {\layoutparameter\c!leftmargindistance}%
   \global\naturalrightmargindistance{\layoutparameter\c!rightmargindistance}%
   \global\naturaltopdistance        {\layoutparameter\c!topdistance}%
   \global\naturalheaderdistance     {\layoutparameter\c!headerdistance}%
   \global\naturaltextdistance       {\layoutparameter\c!textdistance}%
   \global\naturalfooterdistance     {\layoutparameter\c!footerdistance}%
   \global\naturalbottomdistance     {\layoutparameter\c!bottomdistance}%
   %
   \global\marginwidth               \naturalmarginwidth
   \global\edgewidth                 \naturaledgewidth
   \global\margindistance            \naturalmargindistance
   \global\edgedistance              \naturaledgedistance
   %
   \global\leftedgedistance          \layoutdistance\leftedgewidth   \naturalleftedgedistance
   \global\rightedgedistance         \layoutdistance\rightedgewidth  \naturalrightedgedistance
   \global\leftmargindistance        \layoutdistance\leftmarginwidth \naturalleftmargindistance
   \global\rightmargindistance       \layoutdistance\rightmarginwidth\naturalrightmargindistance
   \global\topdistance               \layoutdistance\topheight       \naturaltopdistance
   \global\headerdistance            \layoutdistance\headerheight    \naturalheaderdistance
   \global\textdistance                                              \naturaltextdistance
   \global\footerdistance            \layoutdistance\footerheight    \naturalfooterdistance
   \global\bottomdistance            \layoutdistance\bottomheight    \naturalbottomdistance}

\def\page_layouts_set_distances % local in \setreducedvsize
  {\headerdistance\layoutdistance\headerheight\naturalheaderdistance
   \footerdistance\layoutdistance\footerheight\naturalfooterdistance}

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

\newtoks\extralayoutcalculations

\def\page_layouts_calculate_extras
  {\expand\extralayoutcalculations}

\newtoks\everyswapmargins % watch the order !

\newconditional\marginsareswapped

\appendtoks
    \ifconditional\marginsareswapped
       \marginsareswapped\conditionalfalse
    \else
       \marginsareswapped\conditionaltrue
    \fi
    %
    \enforced\swapcsvalues\leftmargindistance\rightmargindistance
    \enforced\swapcsvalues\leftedgedistance  \rightedgedistance
    \enforced\swapcsvalues\leftmarginwidth   \rightmarginwidth
    \enforced\swapcsvalues\leftedgewidth     \rightedgewidth
    %
    \enforced\swapcsvalues\backspace         \cutspace
    %
    \enforced\swapcsvalues\leftmargintotal   \rightmargintotal
    \enforced\swapcsvalues\leftedgetotal     \rightedgetotal
    \enforced\swapcsvalues\leftsidetotal     \rightsidetotal
    \enforced\swapcsvalues\leftcombitotal    \rightcombitotal
    \enforced\swapcsvalues\innermargintotal  \outermargintotal
    \enforced\swapcsvalues\inneredgetotal    \outeredgetotal
    \enforced\swapcsvalues\innercombitotal   \outercombitotal
    \enforced\swapcsvalues\innersidetotal    \outersidetotal
\to \everyswapmargins

%D The papersize macros have a long history and we don't want to change the commands
%D so they keep looking a bit complex.

%D \macros
%D   {definepapersize}
%D
%D Before we start calculating layout dimensions, we will first take care of paper
%D sizes. The first argument can be either an assignment (for defaults) or an
%D identifier, in which case the second argument is an assignment.
%D
%D \showsetup{definepapersize}
%D
%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
%D \macros
%D   {setuppaper,setuppapersize}
%D
%D When setting up the papersize on which to typeset and print, we can also
%D determine some more characteristics.
%D
%D \showsetup{setuppapersize}
%D
%D We keep track of these features with the following
%D variables.

\installcorenamespace{layouttarget}
\installcorenamespace{layoutpaper}
\installcorenamespace{layoutprint}
\installcorenamespace{layoutcurrent}

\installcommandhandler \??layouttarget {layouttarget} \??layouttarget % so this is a sort of mix, not really a user command / maybe switch handler

\newconditional\c_page_target_paper_mirror
\newconstant   \c_page_target_paper_orientation
\newconstant   \c_page_target_paper_reverse
\newconditional\c_page_target_paper_landscape
\newconditional\c_page_target_paper_negate

\newconditional\c_page_target_print_mirror
\newconstant   \c_page_target_print_orientation
\newconstant   \c_page_target_print_reverse
\newconditional\c_page_target_print_landscape
\newconditional\c_page_target_print_negate
\newconditional\c_page_target_print_doublesided

\let\v_page_target_left_fill  \relax
\let\v_page_target_right_fill \relax
\let\v_page_target_top_fill   \relax
\let\v_page_target_bottom_fill\relax

\lettonothing\papersize
\lettonothing\printpapersize

\def\v_page_target_top      {\namedlayouttargetparameter\papersize\c!top}
\def\v_page_target_bottom   {\namedlayouttargetparameter\papersize\c!bottom}
\def\v_page_target_left     {\namedlayouttargetparameter\papersize\c!left}
\def\v_page_target_right    {\namedlayouttargetparameter\papersize\c!right}
\def\v_page_target_method   {\rootlayouttargetparameter\c!method}
\def\v_page_target_scale    {\rootlayouttargetparameter\c!scale}

\def\v_page_target_nx       {\numexpr\rootlayouttargetparameter\c!nx\relax}
\def\v_page_target_ny       {\numexpr\rootlayouttargetparameter\c!ny\relax}
\def\v_page_target_dx       {\dimexpr\rootlayouttargetparameter\c!dx\relax}
\def\v_page_target_dy       {\dimexpr\rootlayouttargetparameter\c!dy\relax}
\def\v_page_target_width    {\dimexpr\rootlayouttargetparameter\c!width\relax}
\def\v_page_target_height   {\dimexpr\rootlayouttargetparameter\c!height\relax}
\def\v_page_target_topspace {\dimexpr\rootlayouttargetparameter\c!topspace\relax}
\def\v_page_target_backspace{\dimexpr\rootlayouttargetparameter\c!backspace\relax}
\def\v_page_target_offset   {\dimexpr\rootlayouttargetparameter\c!offset\relax}

\def\v_page_target_xy       {\numexpr\v_page_target_nx*\v_page_target_ny\relax}

%D Normally we will not use this command directly but for now it works out okay. In
%D the future we might use more of the related commands.

\setuplayouttarget
  [% these are rather special
   \c!nx=1,
   \c!ny=1,
   \c!dx=\zeropoint,
   \c!dy=\zeropoint,
   \c!topspace=\zeropoint,
   \c!backspace=\zeropoint,
   \c!width=\zeropoint,
   \c!height=\zeropoint,
   %
   \c!method=\v!normal,
   \c!option=\v!max,
   \c!alternative=\v!normal,
   %
   \c!scale=1,
   \c!offset=\zeropoint,
   %
   \c!top=,
   \c!bottom=,
   \c!left=,
   \c!right=,
   %
   \c!location=]

% \definepapersize[name][A4][A3]
% \definepapersize[name][settings]

\permanent\tolerant\protected\def\definepapersize[#1]#*[#S#2]#*[#S#3]%
  {\cdef\currentlayouttarget{#1}%
   \ifempty\currentlayouttarget
     % invalid target
   \orelse\ifhastok={#2}%
     \definelayouttarget[#1][#2]%
   \else
     \edefcsname\??layoutpaper#1\endcsname{#2}%
     \ifparameter#3\or
       \edefcsname\??layoutprint#1\endcsname{#3}%
     \else
       \edefcsname\??layoutprint#1\endcsname{#2}%
     \fi
   \fi}

\appendtoks
    \letcsname\??layoutcurrent\currentlayouttarget\endcsname\relax
\to \everydefinelayouttarget

\def\page_paper_the_paper_size#1{\ifcsname\??layoutpaper#1\endcsname\lastnamedcs\else#1\fi}
\def\page_paper_the_print_size#1{\ifcsname\??layoutprint#1\endcsname\lastnamedcs\else#1\fi}

% \setuppaper    [page=A4,paper=A3] % the k/v variant, changes the current page mapping
% \setuppapersize[A4][a=b,c=d]      % the k/v variant, changes nothing, just settings
% \setuppapersize[A4][A3]           % changes the current page mapping

\let\page_paper_reinstate\relax
\let\page_paper_restore  \relax

\permanent\tolerant\protected\def\setuppapersize[#S#1]#*[#S#2]%
  {\ifarguments
     \page_paper_restore
   \orelse\ifhastok={#1}%
     \page_paper_setup_size_settings[#1]%
   \orelse\ifhastok={#2}%
     \page_paper_setup_size_settings_by_name[#1][#2]%
   \else
     \page_paper_setup_size_change_size[#1][#2]%
   \fi}

\lettonothing\m_layouttarget_paper
\lettonothing\m_layouttarget_print

\lettonothing\m_layouttarget_paper_saved
\lettonothing\m_layouttarget_print_saved

\protected\def\page_paper_setup_size_settings[#S#1]% sometimes used to set paper/print size
  {\lettonothing\currentlayouttarget
   \edef\m_layouttarget_paper_saved{\page_paper_the_paper_size{\layouttargetparameter\c!page }}%
   \edef\m_layouttarget_print_saved{\page_paper_the_print_size{\layouttargetparameter\c!paper}}%
   \setupcurrentlayouttarget[#1]%
   \edef\m_layouttarget_paper{\page_paper_the_paper_size{\layouttargetparameter\c!page }}%
   \edef\m_layouttarget_print{\page_paper_the_print_size{\layouttargetparameter\c!paper}}%
   \ifx\m_layouttarget_paper_saved\m_layouttarget_paper
     \ifx\m_layouttarget_print_saved\m_layouttarget_print
       % we didn't change the size, maybe only sx or sy or so
     \else
       \page_paper_setup_size_settings_recalibrate
     \fi
   \else
     \page_paper_setup_size_settings_recalibrate
   \fi}

\def\page_paper_setup_size_settings_recalibrate
  {\ifempty\m_layouttarget_paper
     % forget about it
   \else
     \ifempty\m_layouttarget_print
        \let\m_layouttarget_print\m_layouttarget_paper
     \fi
     \page_paper_set_current[\m_layouttarget_paper][\m_layouttarget_print]%
   \fi
   \letlayouttargetparameter\c!page \papersize
   \letlayouttargetparameter\c!paper\printpapersize}

\protected\def\page_paper_setup_size_settings_by_name[#1][#S#2]%
  {\cdef\currentlayouttarget{\page_paper_the_paper_size{#1}}%
   \setuplayouttarget[#2]}

\protected\def\page_paper_setup_size_change_size[#1][#2]%
  {\ifempty{#2}%
     \page_paper_set_current[#1][#1]%
   \else
     \page_paper_set_current[#1][#2]%
   \fi}

\permanent\let\setuppaper\page_paper_setup_size_settings

\permanent\protected\def\adaptpapersize
  {\glet\page_paper_reinstate\page_paper_restore
   \setuppapersize}

\appendtoks
    \page_paper_reinstate
    \glet\page_paper_reinstate\relax
\to \everyaftershipout

\protected\def\page_paper_set_restore#1#2%
  {\xdef\page_paper_restore{\page_paper_set_current_indeed[#1][#2]}}

\protected\def\page_paper_set_current[#1][#2]%
  {\expanded{\page_paper_set_current_indeed
     [\page_paper_the_paper_size{#1}]%
     [\page_paper_the_print_size{#2}]}}

\protected\def\page_paper_reset_paper
  {\global\c_page_target_paper_landscape\conditionalfalse
   \global\c_page_target_paper_mirror\conditionalfalse
   \global\c_page_target_paper_negate\conditionalfalse
   \global\c_page_target_paper_orientation\uprotationangle
   \global\c_page_target_paper_reverse\uprotationangle}

\protected\def\page_paper_reset_print
  {\global\c_page_target_print_landscape\conditionalfalse
   \global\c_page_target_print_mirror\conditionalfalse
   \global\c_page_target_print_negate\conditionalfalse
   \global\c_page_target_print_orientation\uprotationangle
   \global\c_page_target_print_reverse\uprotationangle}

\letcsname\??layoutpaper\v!reset\endcsname\page_paper_reset_paper
\letcsname\??layoutprint\v!reset\endcsname\page_paper_reset_print

\defcsname\??layoutpaper\v!landscape\endcsname
  {\global\c_page_target_paper_landscape\conditionaltrue}

\defcsname\??layoutpaper\v!mirrored\endcsname
  {\global\c_page_target_paper_mirror\conditionaltrue}

\defcsname\??layoutpaper\v!negative\endcsname
  {\global\c_page_target_paper_negate\conditionaltrue}

\defcsname\??layoutpaper\v!rotated\endcsname
  {\global\c_page_target_paper_orientation\rightrotationangle
   \global\c_page_target_paper_reverse\leftrotationangle}

\defcsname\??layoutpaper\the\rightrotationangle\endcsname
  {\global\c_page_target_paper_orientation\rightrotationangle
   \global\c_page_target_paper_reverse\leftrotationangle}

\defcsname\??layoutpaper\the\downrotationangle\endcsname
  {\global\c_page_target_paper_orientation\downrotationangle
   \global\c_page_target_paper_reverse\zerocount}

\defcsname\??layoutpaper\the\leftrotationangle\endcsname
  {\global\c_page_target_paper_orientation\leftrotationangle
   \global\c_page_target_paper_reverse\rightrotationangle}

\defcsname\??layoutprint\v!landscape\endcsname
  {\global\c_page_target_print_landscape\conditionaltrue}

\defcsname\??layoutprint\v!mirrored\endcsname
  {\global\c_page_target_print_mirror\conditionaltrue}

\defcsname\??layoutprint\v!negative\endcsname
  {\global\c_page_target_print_negate\conditionaltrue}

\defcsname\??layoutprint\v!rotated\endcsname
  {\global\c_page_target_print_orientation\rightrotationangle
   \global\c_page_target_print_reverse\leftrotationangle}

\defcsname\??layoutprint\the\rightrotationangle\endcsname
  {\global\c_page_target_print_orientation\rightrotationangle
   \global\c_page_target_print_reverse\leftrotationangle}

\defcsname\??layoutprint\the\downrotationangle \endcsname
  {\global\c_page_target_print_orientation\downrotationangle
   \global\c_page_target_print_reverse\zerocount}

\defcsname\??layoutprint\the\leftrotationangle \endcsname
  {\global\c_page_target_print_orientation\leftrotationangle
   \global\c_page_target_print_reverse\rightrotationangle}

\def\page_paper_handle_page_option #1{\begincsname\??layoutpaper#1\endcsname}
\def\page_paper_handle_print_option#1{\begincsname\??layoutprint#1\endcsname}

\protected\def\page_paper_identify_target#1%
  {\ifcsname\??layoutcurrent#1\endcsname
     \cdef\currentlayouttarget{#1}%
   \fi}

\newdimension\d_page_minimum_paper_size \d_page_minimum_paper_size\luaexpr{math.pi}\onebasepoint

\lettonothing\m_page_asked_paper
\lettonothing\m_page_asked_print

\protected\def\page_paper_set_current_indeed[#1][#2]%
  {\edef\m_page_asked_paper{#1}% can be the restores
   \edef\m_page_asked_print{#2}%
   %
   \page_paper_set_restore\m_page_asked_paper\m_page_asked_print
   %
   % locate page target
   \lettonothing\currentlayouttarget
   \page_paper_reset_paper
   \processcommacommand[\m_page_asked_paper]\page_paper_identify_target
   \ifempty\currentlayouttarget
     \let\currentlayouttarget\currentpage
   \fi
   \glet\papersize\currentlayouttarget
   \page_paper_reset_paper
   \processcommacommand[\m_page_asked_paper]\page_paper_handle_page_option
   \global\paperwidth {\layouttargetparameter\c!width}%
   \global\paperheight{\layouttargetparameter\c!height}%
   \ifdim\paperwidth<\d_page_minimum_paper_size
     \global\paperwidth\d_page_minimum_paper_size
   \fi
   \ifdim\paperheight<\d_page_minimum_paper_size
     \global\paperheight\d_page_minimum_paper_size
   \fi
   \ifconditional\c_page_target_paper_landscape
     \enforced\global\swapcsvalues\paperwidth\paperheight
   \fi
   \ifinpagebody % local freeze
     \expanded{\setlayouttargetparameter\c!height{\the\paperheight}}%
     \expanded{\setlayouttargetparameter\c!width {\the\paperwidth }}%
   \fi
   %
   \page_paper_set_offsets
   %  locate paper target
   \page_paper_reset_print
   \processcommacommand[\m_page_asked_print]\page_paper_identify_target
   \glet\printpapersize\currentlayouttarget
   \page_paper_reset_print
   \processcommacommand[\m_page_asked_print]\page_paper_handle_print_option
   \global\printpaperwidth {\layouttargetparameter\c!width}%
   \global\printpaperheight{\layouttargetparameter\c!height}%
   \ifdim\printpaperwidth<\onepoint
     \global\printpaperwidth\paperwidth
   \fi
   \ifdim\printpaperheight<\onepoint
     \global\printpaperheight\paperheight
   \fi
   \ifconditional\c_page_target_print_landscape
     \enforced\global\swapcsvalues\printpaperwidth\printpaperheight
   \fi
   % this check can be confusing, so we've added the possibility
   % to bypass this test: \setuppapersize[option=fit]
   \ifcstok{\rootlayouttargetparameter\c!option}\v!max % \v!fit is
      \begingroup
      % we need to pre-swap else we get the wrong paper size
      \ifnum\c_page_target_paper_orientation=\rightrotationangle
        \enforced\swapcsvalues\paperwidth\paperheight
      \orelse\ifnum\c_page_target_paper_orientation=\leftrotationangle
        \enforced\swapcsvalues\paperwidth\paperheight
      \fi
      \ifnum\c_page_target_print_orientation=\rightrotationangle
        \enforced\swapcsvalues\printpaperwidth\printpaperheight
      \orelse\ifnum\c_page_target_print_orientation=\leftrotationangle
        \enforced\swapcsvalues\printpaperwidth\printpaperheight
      \fi
      \ifdim\paperheight>\printpaperheight
        \global\printpaperheight\paperheight
        \writestatus\m!system{print height forced to paper height}%
      \fi
      \ifdim\paperwidth>\printpaperwidth
        \global\printpaperwidth\paperwidth
        \writestatus\m!system{print width forced to paper width}%
      \fi
      \endgroup
   \fi
  %\writestatus{layout target}{(\the\paperwidth,\the\paperheight) -> (\the\printpaperwidth,\the\printpaperheight)}%
   \page_layouts_synchronize
   % new but we assume \setuplayout
   \scrn_canvas_synchronize_only}

\ifdefined\scrn_canvas_synchronize_only \else
    \let\scrn_canvas_synchronize_only\relax
\fi

\ifdefined\page_paper_set_offsets \else

  \def\page_paper_set_offsets % will move
    {\global\paperoffset\v_page_target_offset
     \global\advanceby\paperwidth -2\paperoffset
     \global\advanceby\paperheight-2\paperoffset}

\fi

%D The fit options were used for situations like this:
%D
%D \starttyping
%D \setuplayout
%D   [topspace=15pt,header=30pt,footer=30pt,bottomdistance=2.5pt,bottom=17.5pt,height=fit,
%D    backspace=60pt,margin=30pt,margindistance=15pt,width=fit]
%D \stoptyping

\lettonothing\p_page_layouts_height
\lettonothing\p_page_layouts_scale
\lettonothing\p_page_layouts_sx
\lettonothing\p_page_layouts_sy
\lettonothing\p_page_layouts_width

\def\page_layouts_synchronize_width_fit
  {\ifzeropt\cutspace
     \global\cutspace\backspace
   \fi
   \global\makeupwidth{%
     \paperwidth-\cutspace
   }%
   \scratchdimen{%
      \backspace
     -\leftedgewidth  -\leftedgedistance
     -\leftmarginwidth-\leftmargindistance
   }%
   \ifdim\scratchdimen<\zeropoint
     \scratchdimen\zeropoint
   \fi
   \global\advanceby\makeupwidth{%
     -\rightmargindistance-\rightmarginwidth
     -\rightedgedistance  -\rightedgewidth
     -\scratchdimen
   }}

\def\page_layouts_synchronize_width_middle
  {\ifzeropt\cutspace
     \global\cutspace\backspace
   \fi
   \global\makeupwidth{%
     \paperwidth-\backspace-\cutspace
   }}

\def\page_layouts_synchronize_width_size
  {\global\makeupwidth{\p_page_layouts_width}%
   \ifzeropt\cutspace
     \global\cutspace{%
       \paperwidth-\makeupwidth-\backspace
     }%
   \fi}

\def\page_layouts_synchronize_height_fit
  {\ifzeropt\bottomspace
     \global\bottomspace\topspace
   \fi
   \global\makeupheight{%
     \paperheight-\bottomspace
   }%
   \scratchdimen{%
     \topspace-\topheight-\topdistance
   }%
   \ifdim\scratchdimen<\zeropoint
     \scratchdimen\zeropoint
   \fi
   \global\advanceby\makeupheight{%
     -\bottomdistance-\bottomheight-\scratchdimen
   }}

\def\page_layouts_synchronize_height_middle
  {\ifzeropt\bottomspace
     \global\bottomspace\topspace
   \fi
   \global\makeupheight{%
     \paperheight-\topspace-\bottomspace
   }}

\def\page_layouts_synchronize_height_size
  {\global\makeupheight{\p_page_layouts_height}}


%D Beware, when the bodyfont changes (switched) this will change as well;
%D implementing a global lineheight is tricky: should we take the bodyfont
%D interlinespace or the one set independent of the bodyfont (before or after a
%D layout spec); way too fuzzy, so we stick to the current method (after a night of
%D experimenting ...2003/10/13)

\def\page_layouts_synchronize_height_lines
  {\global\makeupheight{%
      (\layoutparameter\c!lines)*\lineheight
     -\strutheight
     +\topskip
     +\headerdistance
     +\headerheight
     +\footerdistance
     +\footerheight
%    \relax}
   }}

\def\page_layouts_synchronize
  {\setups[\layoutparameter\c!preset]\relax
   %
   \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\cutspace        {\layoutparameter\c!cutspace}%    % moved here
   \global\topspace        {\layoutparameter\c!topspace}%
   \global\bottomspace     {\layoutparameter\c!bottomspace}% % moved here
   \global\textadaptive    {\layoutparameter\c!adaptive}%
   %
   \global\layoutlines\ifchknumexpr\layoutparameter\c!lines\or\lastchknumber\else\zerocount\fi
   %
   \ifcase\layoutlines\else
     \checkbodyfontstate
   \fi
   \page_layouts_set_dimensions
   \synchronizegridsnapping
   \usesetupsparameter\layoutparameter % depends on gridsnapping !
   \synchronizewhitespace
   \synchronizeblank
   \setupinterlinespace[\v!reset]% \synchronizegloballinespecs
   %
  %\global\cutspace\layoutparameter\c!cutspace\relax % moved up
   \edef\p_page_layouts_width{\layoutparameter\c!width}%
   \ifx\p_page_layouts_width\v!middle
     \page_layouts_synchronize_width_middle
   \orelse\ifx\p_page_layouts_width\v!fit
     \page_layouts_synchronize_width_fit
   \else
     \page_layouts_synchronize_width_size
   \fi
  %\global\bottomspace\layoutparameter\c!bottomspace\relax % moved up
   \ifcase\layoutlines
     \edef\p_page_layouts_height{\layoutparameter\c!height}%
     \ifx\p_page_layouts_height\v!middle
       \page_layouts_synchronize_height_middle
     \orelse\ifx\p_page_layouts_height\v!fit
       \page_layouts_synchronize_height_fit
     \else
       \page_layouts_synchronize_height_size
     \fi
   \else
     \page_layouts_synchronize_height_lines
   \fi
   \ifzeropt\bottomspace
     \global\bottomspace{\paperheight-\makeupheight-\topspace}%
   \fi
   \backoffset{\layoutparameter\c!horoffset}%
   \topoffset {\layoutparameter\c!veroffset}%
   \ifdim\makeupwidth<\onepoint
     \global\makeupwidth\onepoint
   \fi
   \ifdim\makeupheight<\onepoint
     \global\makeupheight\onepoint
   \fi
   % handy in page builder
   \global\totaltextwidth{%
      \leftedgetotal
     +\leftmargintotal
     +\makeupwidth
     +\rightmargintotal
     +\rightedgetotal
   }%
   % \page_layouts_check_next % here ?
   \page_layouts_check_direction
   \page_layouts_calculate_extras
   \page_target_check_centering
   \calculatehsizes
   \calculatevsizes
   \page_layouts_check_pseudo_columns
   \page_layouts_check_pseudo_rows
   \page_backgrounds_recalculate}

\def\page_layouts_check_direction
  {\ifcstok{\layoutparameter\c!direction}\v!reverse
     \enforced\global\swapcsvalues\naturalleftedgedistance  \naturalrightedgedistance
     \enforced\global\swapcsvalues\naturalleftmargindistance\naturalrightmargindistance
     \enforced\global\swapcsvalues\leftmargindistance       \rightmargindistance
     \enforced\global\swapcsvalues\leftmarginwidth          \rightmarginwidth
     \enforced\global\swapcsvalues\leftedgedistance         \rightedgedistance
     \enforced\global\swapcsvalues\leftedgewidth            \rightedgewidth
     \enforced\global\swapcsvalues\backspace                \cutspace
     \expandafter\setsystemmode
   \else
     \expandafter\resetsystemmode
   \fi{reverselayout}}

%D Pseudo columns:

\def\page_layouts_check_pseudo_columns
  {\global\layoutcolumns       {\layoutparameter\c!columns}%
   \global\layoutcolumndistance{\layoutparameter\c!columndistance}%
   \global\layoutcolumnwidth   {%
      \makeupwidth
     -\layoutcolumns\layoutcolumndistance
     +\layoutcolumndistance
   }%
   \ifnum\layoutcolumns>\plusone
     \global\divideby\layoutcolumnwidth\layoutcolumns
     \dorecurse\layoutcolumns\page_layouts_check_pseudo_column
   \fi}

\def\page_layouts_check_pseudo_column
  {\xdefcsname\??layoutcolumn\recurselevel\endcsname
     {\the\numexpr\recurselevel-\plusone\relax\dimexpr\layoutcolumnwidth+\layoutcolumndistance}}

\aliased\letcsname\??layoutcolumn0\endcsname\zeropoint

\permanent\def\layoutcolumnoffset#1% can travel around so we can't use \lastnamedcs
  {\csname\??layoutcolumn\ifcsname\??layoutcolumn#1\endcsname#1\else0\fi\endcsname}

%D Pseudo rows:

\def\page_layouts_check_pseudo_rows
  {\global\layoutrows\layoutparameter\c!rows
   \global\layoutrowdistance{\layoutparameter\c!rowdistance}%
   \global\layoutrowheight{%
      \makeupheight
     -\layoutrows\layoutrowdistance
     +\layoutrowdistance
  }%
   \ifnum\layoutrows>\plusone
     \global\divideby\layoutrowheight\layoutrows
     \dorecurse\layoutrows\page_layouts_check_pseudo_row
   \fi}

\def\page_layouts_check_pseudo_row
  {\xdefcsname\??layoutrow\recurselevel\endcsname
     {\the\numexpr\recurselevel-\plusone\relax\dimexpr\layoutrowheight+\layoutrowdistance}}

\aliased\letcsname\??layoutrow0\endcsname\zeropoint

\untraced\permanent\def\layoutrowoffset#1% can travel around so we can't use \lastnamedcs
  {\csname\??layoutrow\ifcsname\??layoutrow#1\endcsname#1\else0\fi\endcsname}

%D Offset:

\untraced\permanent\protected\def\layoutlineoffset#1%
  {\todimension{\topskip-\strutht+#1\lineheight-\lineheight}}

\def\page_layouts_synchronize_at_start
  {\ifdim\makeupheight=\layoutlines\lineheight \else % weird check
     \page_layouts_synchronize
   \fi}

\appendtoks
    \page_layouts_synchronize_at_start % still needed?
\to \everystarttext

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

\ifdefined\lastpage \else \let\lastpage\!!plusone \fi

\permanent\protected\def\page_layouts_change#1%
  {%\writestatus\m!layouts{changing to layout #1}%
   \xdef\currentlayout{#1}%
   \page_layouts_synchronize}

\aliased\let\changetolayout\page_layouts_change % also public

\def\v_real_page_normal {\the\realpageno}
\def\v_real_page_reverse{-\the\numexpr\lastpage-\realpageno\relax}

\def\v_real_page_odd_or_even
  {\ifodd\pagenoshift
     \ifodd\realpageno\v!even\else\v!odd \fi
   \else
     \ifodd\realpageno\v!odd \else\v!even\fi
   \fi}

\let\v_real_page_current\v!current

\installcorenamespace{unrealpage}

\def\v_real_page_named
  {\ifnum\lastpage=\realpageno
     \v!last
   \orelse\ifnum\plusone=\realpageno
     \v!first
   \else
     \??unrealpage
   \fi}

\let\v_page_layouts_pre_check\relax % we could use push / pop

\lettonothing\m_page_check

% \def\page_layouts_check_default_indeed#1%
%   {\edef\m_page_check{#1}
%    \edef\p_state{\namedlayoutparameter\m_page_check\c!state}%
%    \ifx\p_state\v!start
%      \glet\v_page_layouts_pre_check\currentlayout
%      \glet\currentlayout\m_page_check
%      \page_layouts_synchronize
%    \orelse\ifx\p_state\v!repeat
%      \glet\v_page_layouts_pre_check\relax
%      \glet\currentlayout\m_page_layouts_check
%      \page_layouts_synchronize
%    \fi}

\def\page_layouts_check_default_indeed#1%
  {\edef\m_page_check{#1}
   \ifcstok{\namedlayoutparameter\m_page_check\c!state}\v!start
     \glet\v_page_layouts_pre_check\currentlayout
     \glet\currentlayout\m_page_check
     \page_layouts_synchronize
   \orelse\iflastnamedcs\v!repeat
     \glet\v_page_layouts_pre_check\relax
     \glet\currentlayout\m_page_check
     \page_layouts_synchronize
   \fi}

\def\page_layouts_check_revert
  {\glet\currentlayout\v_page_layouts_pre_check
   \glet\v_page_layouts_pre_check\relax
   \page_layouts_synchronize}

% \showframe
%
% \setuplayout         [width=100mm,height=200mm]
% \definelayout[shorta][height=80mm]
% \definelayout[shortb][height=130mm]
%
% \starttext
%     \definelayout[2][shorta] \definelayout[4][shortb]
%     \definelayout[7][shorta] \definelayout[8][shortb] % we need to check before
%     \dorecurse{20}{#1: \samplefile{knuth}\par}
% \stoptext

\def\page_layouts_check_default
  {\ifrelax\v_page_layouts_pre_check\else
     \page_layouts_check_revert
   \fi
   \ifcsname\namedlayouthash\v_real_page_normal\c!state\endcsname
     \page_layouts_check_default_indeed\v_real_page_normal
   \orelse\ifcsname\namedlayouthash\v_real_page_reverse\c!state\endcsname
     \page_layouts_check_default_indeed\v_real_page_reverse
   \orelse\ifcsname\namedlayouthash\v_real_page_named\c!state\endcsname
     \page_layouts_check_default_indeed\v_real_page_named
   \orelse\ifcsname\namedlayouthash\v_real_page_current\c!state\endcsname
     \page_layouts_check_default_indeed\v_real_page_current
   \orelse\ifcsname\namedlayouthash\v_real_page_odd_or_even\c!state\endcsname
     \page_layouts_check_default_indeed\v_real_page_odd_or_even
 % \orelse\ifrelax\v_page_layouts_pre_check
 %   % okay
 % \else
 %   \page_layouts_check_revert
   \fi}

\permanent\protected\def\installlayoutmethod#1#2%
  {\gdefcsname\??layoutmethod#1\endcsname{#2}}

\installlayoutmethod\v!default{\page_layouts_check_default}
\installlayoutmethod\v!normal {\page_layouts_check_default}

\permanent\protected\def\page_layouts_check_next
  {\ifcsname\??layoutmethod\layoutparameter\c!method\endcsname
     \lastnamedcs
   \else
     \page_layouts_check_default
   \fi}

\aliased\let\checkcurrentlayout\page_layouts_check_next % public and used in naw, so this synonym will stay

% inheritance
%
% \definelayout
%   [test]
%   [width=12cm,
%    height=10cm]
%
% \definelayout
%   [more]
%   [test]
%   [height=12cm]
%
% 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

% Because we want to keep the set parameters as they are we use some
% helpers to communicate between the several page building related
% mechanism.

\appendtoks \page_layouts_check_next \to \everystarttext
\appendtoks \page_layouts_check_next \to \everyaftershipout

\newconditional\c_page_layouts_location_is_set
\newconditional\c_page_layouts_location_is_middle

\def\page_layouts_location_reset % we start in the left top and mirror right pages
  {\c_page_layouts_location_is_set\conditionalfalse
   \c_page_layouts_location_is_middle\conditionalfalse
   \let\v_page_target_left_fill  \relax
   \let\v_page_target_right_fill \hss  % ? \relax
   \let\v_page_target_top_fill   \relax
   \let\v_page_target_bottom_fill\vss} % \relax}

\defcsname\??layoutlocation\v!right\endcsname
  {\c_page_layouts_location_is_set\conditionaltrue
   \c_page_layouts_location_is_middle\conditionalfalse
   \let\v_page_target_left_fill \hss
   \let\v_page_target_right_fill\relax}

\defcsname\??layoutlocation\v!left\endcsname
  {\c_page_layouts_location_is_set\conditionaltrue
   \c_page_layouts_location_is_middle\conditionalfalse
   \let\v_page_target_left_fill \relax
   \let\v_page_target_right_fill\hss}

\defcsname\??layoutlocation\v!bottom\endcsname
  {\c_page_layouts_location_is_set\conditionaltrue
   \c_page_layouts_location_is_middle\conditionalfalse
   \let\v_page_target_top_fill   \vss
   \let\v_page_target_bottom_fill\relax}

\defcsname\??layoutlocation\v!top\endcsname
  {\c_page_layouts_location_is_set\conditionaltrue
   \c_page_layouts_location_is_middle\conditionalfalse
   \let\v_page_target_top_fill    \relax
   \let\v_page_target_bottom_fill\vss}

\defcsname\??layoutlocation\v!middle\endcsname
  {\c_page_layouts_location_is_set\conditionaltrue
   \c_page_layouts_location_is_middle\conditionaltrue
   \let\v_page_target_left_fill  \hss
   \let\v_page_target_right_fill \hss
   \let\v_page_target_top_fill   \vss
   \let\v_page_target_bottom_fill\vss}

\defcsname\??layoutlocation\empty\endcsname
  {\c_page_layouts_location_is_set\conditionalfalse % default also signal to scrn_
   \c_page_layouts_location_is_middle\conditionalfalse
   \let\v_page_target_right_fill \hss
   \let\v_page_target_bottom_fill\hss}

\defcsname\??layoutlocation\v!doublesided\endcsname
  {\c_page_target_print_doublesided\conditionaltrue}

\defcsname\??layoutlocation\v!singlesided\endcsname
  {\c_page_target_print_doublesided\conditionalfalse}

\def\page_target_check_centering_indeed#1%
  {\begincsname\??layoutlocation#1\endcsname}

\protected\def\page_target_check_centering
  {\c_page_target_print_doublesided\conditionalfalse
   \page_layouts_location_reset
   \processcommacommand[\layoutparameter\c!location]\page_target_check_centering_indeed}

% installers

\permanent\protected\def\installlayoutalternative#1#2%
  {\gdefcsname\??layoutalternative#1\endcsname{#2}}

\def\page_boxes_construct_content_default#1#2#3% targetbox flusher box
  {\setbox#1\vpack % was \vbox
     {\offinterlineskip
      \begingroup % needed ?
      \uselayoutstyleandcolor\c!style\c!color
      \offinterlineskip
      \page_layouts_insert_elements % zero size
      \endgroup
      \page_insert_body#2#3}}% including footnotes

\installlayoutalternative\v!default{\page_boxes_construct_content_default}
\installlayoutalternative\v!normal {\page_boxes_construct_content_default}

% \def\page_boxes_construct_content % targetbox flusher box
%   {\ifcsname\??layoutalternative\layoutparameter\c!alternative\endcsname
%      \expandafter\lastnamedcs
%    \else
%      \expandafter\page_boxes_construct_content_default
%    \fi}
%
% see below

%D For Mikael:
%D
%D \starttyping
%d \starttext
%D
%D \definecolumnset[mine][n=2]
%D
%D \startbuffer[whatever-2]
%D     \setupbackgrounds[page][background=color,backgroundcolor=red]%
%D     \setupheader[state=empty]%
%D \stopbuffer
%D \startbuffer[whatever-3]
%D     \setupbackgrounds[page][background=color,backgroundcolor=green]%
%D     \setupheader[state=empty]%
%D \stopbuffer
%D \startbuffer[whatever-4]
%D     \setupbackgrounds[page][background=color,backgroundcolor=yellow]%
%D     \setupheader[state=empty]%
%D \stopbuffer
%D
%D \startcolumnset[mine]
%D     \dorecurse{10}{
%D         \startsection[title=Show #1.1]
%D             \dorecurse{40}{\samplefile{knuthmath}}
%D         \stopsection
%D         \startsection[title=Show #1.2,extradata=\atthispage{\getbuffer[whatever-2]}]
%D             \dorecurse{50}{\samplefile{knuthmath}}
%D         \stopsection
%D         \startsection[title=Show #1.3,extradata=\atthispage{\getbuffer[whatever-3]}]
%D             \dorecurse{30}{\samplefile{knuthmath}}
%D         \stopsection
%D         \startsection[title=Show #1.4,extradata=\atthispage{\getbuffer[whatever-4]}]
%D             \dorecurse{20}{\samplefile{knuthmath}}
%D         \stopsection
%D     }
%D \stopcolumnset
%D
%D \stoptext
%D \stoptyping

\installcorenamespace{thispage}

\definemarking[\??thispage]

\lettonothing\page_marks_flush_this_page

\permanent\def\page_marks_flush_this_page_indeed
  {\firstmarks\csname\??markingclass\??thispage\endcsname
   \clearmarks\csname\??markingclass\??thispage\endcsname}

\permanent\protected\def\atthispage#1%
  {\glet\page_marks_flush_this_page\page_marks_flush_this_page_indeed
   \marks\csname\??markingclass\??thispage\endcsname{#1}}

\def\page_boxes_construct_content % targetbox flusher box
  {\page_marks_flush_this_page
   \ifcsname\??layoutalternative\layoutparameter\c!alternative\endcsname
     \expandafter\lastnamedcs
   \else
     \expandafter\page_boxes_construct_content_default
   \fi}

%D \macros
%D   {adaptlayout}

% this will move to lua

\installcorenamespace{adaptlayout}
\installcorenamespace{pageadaptations}

\installsetuponlycommandhandler \??adaptlayout {adaptlayout}

\newdimension\d_page_adapts_pushed_text_height
\newdimension\d_page_adapts_pushed_footer_height
\newdimension\d_page_adapts_height
\newdimension\d_page_adapts_delta

\appendtoks \global\d_page_adapts_delta\zeropoint \to \everyshipout

\permanent\protected\tolerant\def\adaptlayout[#1]#*[#S#2]%
  {\ifparameter#2\or
     \processcommalist[#1]{\page_adapts_layout_register{#2}}%
     \page_adapts_check
   \else
     \page_adapts_layout_indeed{#1}%
   \fi}

\def\page_adapts_layout_register#1#2%
  {\gdefcsname\??pageadaptations#2\endcsname{\page_adapts_layout_indeed{#1}}}

\let\p_adapts_height\zeropoint
\let\p_adapts_lines \zerocount

\def\page_adapts_layout_indeed#1% called often
  {\setupcurrentadaptlayout[\c!height=\zeropoint,\c!lines=\zerocount,\c!top=\zeropoint,#1]%
   \page_adapts_push
   \edef\p_adapts_height{\adaptlayoutparameter\c!height}%
   \edef\p_adapts_lines {\adaptlayoutparameter\c!lines}%
   \ifx\p_adapts_height\v!max
     \global\d_page_adapts_height\footerheight
   \else
     \global\d_page_adapts_height{\ifnum\p_adapts_lines=\zerocount\p_adapts_height\else\p_adapts_lines\openlineheight\fi}%
     \ifdim\d_page_adapts_height>\footerheight
       \global\d_page_adapts_height\footerheight
     \fi
   \fi
   %
   \global\d_page_adapts_delta{-(\adaptlayoutparameter\c!top)}%
   %
   \global\advanceby\textheight   \d_page_adapts_height
   \global\advanceby\footerheight-\d_page_adapts_height
   \showmessage\m!layouts1{\the\d_page_adapts_height,\the\realpageno}%
   % this will become a better one (do we need insert correction?)
   \page_otr_command_set_vsize
   %
   \page_backgrounds_recalculate
   \glet\page_adapts_push\relax
   \glet\page_adapts_pop\page_adapts_pop_indeed}

\def\page_adapts_check
  {\begincsname\??pageadaptations\the\realpageno\endcsname}

\def\page_adapts_reset
  {\ifcsname\??pageadaptations\the\realpageno\endcsname
     \gletcsname\??pageadaptations\the\realpageno\endcsname\relax
   \fi}

\def\page_adapts_push_indeed
  {\global\d_page_adapts_pushed_text_height  \textheight
   \global\d_page_adapts_pushed_footer_height\footerheight}

\def\page_adapts_pop_indeed
  {\global\textheight  \d_page_adapts_pushed_text_height
   \global\footerheight\d_page_adapts_pushed_footer_height
   \page_layouts_synchronize
   \glet\page_adapts_push\page_adapts_push_indeed
   \glet\page_adapts_pop\relax}

\appendtoks \page_adapts_check \to \everystarttext
\appendtoks \page_adapts_reset \to \everyshipout

\let\page_adapts_pop \relax
\let\page_adapts_push\page_adapts_push_indeed

\protected\def\page_adapts_synchronize % used elsewhere
  {\page_adapts_pop
   \page_adapts_check}

\def\page_adapts_status_info % used elsewhere
  {\ifrelax\page_adapts_pop \else
     \space(adept: \the\d_page_adapts_height)\space
   \fi
   \the\realpageno}

%D \macros
%D   {startlayout,stoplayout}

\permanent\protected\def\startlayout[#S#1]%
  {\page
   \globalpushmacro\currentlayout
   \doifelselayoutdefined{#1}{\setuplayout[#1]}\donothing} % {\setuplayout[\currentlayout]}}

\permanent\protected\def\stoplayout
  {\page
   \globalpopmacro\currentlayout
   \setuplayout[\currentlayout]}

% NOG EENS NAGAAN WANNEER NU GLOBAL EN WANNEER NIET

\ifdefined\doifelselayoutsomeline \else % eventually defined in page-txt
    \let\doifelselayoutsomeline\secondofthreearguments
\fi

% todo

\permanent\def\compensatevsizeheader             {\textheight{\textheight-\headerheight-\headerdistance}}
\permanent\def\compensatevsizefooter             {\textheight{\textheight-\footerheight-\footerdistance}}
\permanent\def\globalcompensatevsizeheader{\global\textheight{\textheight-\headerheight-\headerdistance}}
\permanent\def\globalcompensatevsizefooter{\global\textheight{\textheight-\footerheight-\footerdistance}}

\permanent\def\compensatevsizeheaderzero{\headerheight\zeropoint\page_layouts_set_distances}
\permanent\def\compensatevsizefooterzero{\footerheight\zeropoint\page_layouts_set_distances}

\def\page_layouts_set_modes
  {\ifzeropt\headerheight
     \resetsystemmode\v!header
   \else
     \setsystemmode\v!header
   \fi
   \ifzeropt\footerheight
     \resetsystemmode\v!footer
   \else
     \setsystemmode\v!footer
   \fi}

\permanent\protected\def\calculatevsizes % name will change
  {\textheight\makeupheight
   \doifelselayoutsomeline\v!header\compensatevsizeheader\donothing
   \doifelselayoutsomeline\v!footer\compensatevsizefooter\donothing
   \page_layouts_set_modes
   \resetglobal
   \page_otr_command_set_vsize}

\permanent\protected\def\calculateglobalvsizes % name will change
  {\global\textheight\makeupheight
   \doifelselayoutsomeline\v!header\globalcompensatevsizeheader\donothing
   \doifelselayoutsomeline\v!footer\globalcompensatevsizefooter\donothing
   \page_layouts_set_modes
   \page_otr_command_set_vsize}

\permanent\protected\def\calculatereducedvsizes % name will change
  {\textheight\makeupheight
   \doifelselayoutsomeline\v!header\compensatevsizeheader\compensatevsizeheaderzero
   \doifelselayoutsomeline\v!footer\compensatevsizefooter\compensatevsizefooterzero}

\newdimension\innermakeupwidth  % special purpose
\newdimension\innermakeupmargin % special purpose

\newconditional\innermakeupcompensation \innermakeupcompensation\conditionaltrue

\permanent\def\compensatedinnermakeupmargin
  {\dimexpr\ifconditional\innermakeupcompensation+\innermakeupmargin\else\zeropoint\fi\relax}

\permanent\protected\def\freezetextwidth
  {\textwidth\ifchkdimexpr\layoutparameter\c!textwidth\or
     \lastchkdimension
   \else
     \makeupwidth
   \fi
   \global\innermakeupwidth\textwidth
   \global\innermakeupmargin\ifchkdimexpr\layoutparameter\c!textmargin\or
     \lastchkdimension
   \else
     \zeropoint
   \fi
   \global\advanceby\innermakeupwidth-2\innermakeupmargin
   \advanceby\textwidth-2\innermakeupmargin}   % local

\permanent\protected\def\calculatehsizes % name will change
  {\freezetextwidth
   \page_otr_command_set_hsize}

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

% #single #left #right

\permanent\def\doifelseoddpage
  {\ifodd\pagenoshift
     \expandafter\page_layouts_if_odd_else_yes
   \else
     \expandafter\page_layouts_if_odd_else_nop
   \fi}

\aliased\let\doifoddpageelse\doifelseoddpage

\def\page_layouts_if_odd_else_yes
  {\ifodd\realpageno
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

\def\page_layouts_if_odd_else_nop
  {\ifodd\realpageno
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

\def\page_layouts_if_odd_else_again#1{\doifelseoddpage}

\permanent\def\doifbothsidesoverruled
  {\ifdoublesided
     \expandafter\page_layouts_if_odd_else_again
   \else
     \expandafter\firstofthreearguments
   \fi}

\permanent\def\doifbothsides% #1 #2 #3
  {\ifdoublesided
     \expandafter\page_layouts_if_both_sides
   \else
     \expandafter\firstofthreearguments
   \fi}

\def\page_layouts_if_both_sides
  {\ifsinglesided
     \expandafter\firstofthreearguments
   \else
     \expandafter\page_layouts_if_odd_else_again
   \fi}

\newdimension\texthoffset

\permanent\protected\def\settexthoffset % name will change
  {\texthoffset\doifbothsides\backspace\backspace{\dimexpr\paperwidth-\backspace-\makeupwidth\relax}}

\permanent\protected\def\goleftonpage % name will change (we could cache)
  {\hkern{-(\leftmargindistance+\leftmarginwidth+\leftedgedistance+\leftedgewidth)}}

\permanent\def\doifelsemarginswap#1#2% protected?
  {\doifbothsides{#1}{#1}{#2}}

\aliased\let\doifmarginswapelse\doifelsemarginswap

\protected\def\page_layouts_swap_margins % name will change, frozen?
  {\doifelsemarginswap\gobbleoneargument\page_layouts_swap_margins_indeed}

\protected\def\page_layouts_swap_margins_indeed#1%
  {\let\page_layouts_swap_margins\gobbleoneargument % to prevent local swapping
  %\writestatus\m!layouts{swapping margins for #1}%
   \expand\everyswapmargins}

\permanent\protected\def\swapmargindimensions
  {\page_layouts_swap_margins\v!page}

\permanent\def\rightorleftpageaction
  {\ifdoublesided
     \expandafter\page_layouts_right_or_left_page_action
   \else
     \expandafter\firstoftwoarguments
   \fi}

% \def\page_layouts_right_or_left_page_action
%   {\ifsinglesided
%      \expandafter\firstoftwoarguments
%    \else
%      \expandafter\doifelseoddpage
%    \fi}

\def\page_layouts_right_or_left_page_action
  {\ifsinglesided
     \expandafter\firstoftwoarguments
   \orelse\ifodd\pagenoshift
     \expandafter\page_layouts_if_odd_else_yes
   \else
     \expandafter\page_layouts_if_odd_else_nop
   \fi}

% swapping needs frozen

% maybe \protected so that the don't expand prematurely

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

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

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

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

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

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

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

%D \macros
%D   {startlocallayout}
%D
%D These macros should be used with care. They permit local layouts (as used in
%D fitting pages, see \type {page-app.tex}). This is kind of obsolete now that we
%D have \type {\definelayout}, so this hack will disappear in future versions.

\permanent\protected\def\startlocallayout
  {\globalpushmacro\page_paper_restore
   \globalpushmacro\currentlayout}

\permanent\protected\def\stoplocallayout
  {\globalpopmacro\currentlayout
   \globalpopmacro\page_paper_restore
   \page_paper_restore
   \setuplayout[\currentlayout]\relax} % explicit !

%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}
%D %showsetup{showgrid}
%D %showsetup{showusage}

\fetchmodulecommand \showprint    \f!page_run
\fetchmodulecommand \showframe    \f!page_run
\fetchmodulecommand \showlayout   \f!page_run
\fetchmodulecommand \showsetups   \f!page_run
\fetchmodulecommand \showmargins  \f!page_run
\fetchmodulecommand \showgrid     \f!page_run
\fetchmodulecommand \showcentering\f!page_run

\glet\page_grids_add_to_box\gobbleoneargument
\glet\page_grids_add_to_one\gobbleoneargument
\glet\page_grids_add_to_mix\gobbleoneargument

%D Play safe:

\appendtoks
    \ifnum\realpageno>\plusone
        \page[\v!odd]%
        \setuplayout
    \fi
\to \everysetuppagenumbering

%D The default dimensions are quite old and will not change. The funny fractions
%D were introduced when we went from fixed dimensions to relative ones. Since
%D \CONTEXT\ is a dutch package, the dimensions are based on the metric system. The
%D asymmetrical layout is kind of handy for short quick||and||dirty stapled
%D documents.
%D
%D Although valid, it is not a real good idea to use dimensions based on the \type
%D {em} unit. First of all, since there are no fonts loaded yet, this dimension
%D makes no sense, and second, you would loose track of values, since they could
%D change while going to a new page, 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 \dimexpr\
     \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 unset is signal (no other default, spoils arrange)
                   \c!state=\v!start,
                   \c!scale=1,
                      \c!sx=1,
                      \c!sy=1,
                      \c!nx=1,
                      \c!ny=1,
                      \c!dx=\zeropoint,
                      \c!dy=\zeropoint,
                    \c!grid=\v!no,
                \c!snapping=\v!normal, % when \c!grid=\v!snapping
               \c!direction=\v!normal,
                  \c!preset=,
                  \c!setups=\systemsetupsprefix\s!default,
              \c!clipoffset=\zeropoint,
                   \c!lines=0,
                   \c!paper=,
                    \c!page=,
                \c!adaptive=\zeropoint, % new: experiment
            \c!limitstretch=\v!auto, % catches \setupalign[depth] without setting (2 lineheight)
                 \c!columns=\plusone,
          \c!columndistance=\zeropoint,
                    \c!rows=\plusone,
             \c!rowdistance=\zeropoint,
          ]

\pushoverloadmode
    \newuserunit \textwidth   tw
    \newuserunit \textheight  th
    \newuserunit \paperwidth  pw
    \newuserunit \paperheight ph
    \newuserunit \hsize       hs % just for fun
    \newuserunit \vsize       vs % just for fun
\popoverloadmode

% \appendtoks
%     \layoutunit\dimexpr\textwidth/100\relax
% \to \everysetuplayout

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

\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 [A4/2][\c!width=\dimexpr297mm/2\relax,\c!height=210mm] % 148.5mm
%definepapersize [2A5] [\c!width=296mm,\c!height=210mm] % doublewide

\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 were corrected top the
%D latest DIN specifications. Peter Rolf came up with these and a few more missing
%D sizes. 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 a bunch of sizes with
%D $4:3$ ratios. The \type {S6} size is nearly as wide as a sheet of \type {A4}
%D 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 [SL]  [\c!width=640pt,\c!height=400pt] % low
\definepapersize [SM]  [\c!width=720pt,\c!height=450pt] % medium
\definepapersize [SW]  [\c!width=800pt,\c!height=450pt] % wide
\definepapersize [HD]  [\c!width=1920pt,\c!height=1080pt]
\definepapersize [HD+] [\c!width=1920pt,\c!height=1200pt]
\definepapersize [HD-] [\c!width=960pt,\c!height=540pt]

%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 \CDROM\ covers, but it
%D does not hurt to have the paper size ready.

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

%D The next series is for our English speaking friends who decided to stick to non
%D metric values. Thanks to Nelson 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 For Alan:

\definepapersize [business]   [\c!width=85mm,\c!height=55mm]
\definepapersize [businessUS] [\c!width=3.5in,\c!height=2in]

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

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

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

\setuppapersize
  [\c!distance=1.5cm] % offset is already taken

\definepapersize
  [oversized]
  [ \c!width=\paperwidth+\layouttargetparameter\c!distance,
   \c!height=\paperheight+\layouttargetparameter\c!distance]

\definepapersize
  [undersized]
  [ \c!width=\paperwidth-\layouttargetparameter\c!distance,
   \c!height=\paperheight-\layouttargetparameter\c!distance]

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

\definepapersize
  [doubleoversized]
  [ \c!width=\paperwidth+\layouttargetparameter\c!distance,
   \c!height=2\paperheight+\layouttargetparameter\c!distance]

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

% \setuppapersize
%   [A4][A4]

\setuppapersize
  [A4] % [samesized]

%D A few goodies, first a full page layout:

\definelayout
  [\v!page]

\setuplayout
  [\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]

%D A quick and dirty one:

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

%D One for testing examples (5mm is often the non printable area):

\definelayout
  [\v!tight]

\definelayout
  [\v!tight]
  [\v!page]
  [\c!backspace=5mm,
   \c!cutspace=5mm,
   \c!topspace=5mm,
   \c!bottomspace=5mm]

\protect \endinput
