%D \module
%D   [       file=page-imp, % was: core-pag,
%D        version=1998.01.15,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Pagebody Building (Imposition),
%D         author=Hans Hagen & Willi Egger,
%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.

% much of this can more to run time loading !

\writestatus{loading}{ConTeXt Page Macros / Pagebody Building}

\unprotect

%D \macros
%D   {starttextdata}
%D
%D This is a user macro (appending to every last shipout is not
%D really user friendly).

\newtoks\t_page_text_data

\unexpanded\def\starttextdata#1\stoptextdata
  {\global\let\page_shipouts_flush_text_data\page_shipouts_flush_text_data_indeed
   \globaladdtotoks\t_page_text_data{#1}}

\let\stoptextdata\relax

\def\page_shipouts_flush_text_data_indeed
  {\vsmashed{\the\t_page_text_data}%
   \global\t_page_text_data\emptytoks
   \global\let\page_shipouts_flush_text_data\relax}

\let\page_shipouts_flush_text_data\relax

\prependtoks
    \page_shipouts_flush_text_data
\to \everylastshipout

% Problem: we need to apply the finalizers to a to be shipped out page (as
% we can have positioning involved). However, we can also add stuff in the
% imposition, like cropmarks. Fortunately we do that with metapost so
% colors etc are dealt with at that end.

% \starttypen
% \def\pagestoshipout{1,3,5}
% \stoptypen

\installcorenamespace{shipoutmethod}
\installcorenamespace{layoutarranger}
\installcorenamespace{layoutarrangeoption}

\newcount      \shippedoutpages
\newcount      \combinedpagescounter

\let           \pagestoshipout\empty    % {1,3,6}
\newconstant   \whichpagetoshipout      % 0=all 1=odd 2=even

\newbox        \shipoutscratchbox

\setnewconstant\shipoutfinalizemethod\plusone % this will be default (we will have two finalizers)

\unexpanded\def\installshipoutmethod#1#2%                   % a handler takes one argument: something to be boxed
  {\setgvalue{\??shipoutmethod#1}##1{#2{##1}}} % and shipped out (don't depend on the exact package)

\let\installpagehandler\installshipoutmethod % will go

\unexpanded\def\invokepagehandler#1%
  {\expandnamespacevalue\??shipoutmethod{#1}\v!normal}

\def\page_shipouts_handle
  {\csname\??shipoutmethod\ifcsname\??shipoutmethod\v_page_target_method\endcsname
     \v_page_target_method
   \else
     \v!none
   \fi\endcsname}

\installshipoutmethod \v!normal
  {\ifarrangingpages
     \expandafter\page_shipouts_arrange
   \else
     \expandafter\page_shipouts_normal
   \fi}

\installshipoutmethod \v!none
  {\page_shipouts_ignore}

% extension mechanism

\newcount\c_page_boxes_flush_n % set at the lua end

\let\page_boxes_flush_before\relax
\let\page_boxes_flush_after \relax

% used here:

\def\page_boxes_shipout#1% or: \page_shipouts_apply
  {\dontcomplain         % redundant
   \ifcase\c_page_boxes_flush_n\else
     \page_boxes_flush_before
   \fi
   \the\everybeforeshipout
   \ifcase\shipoutfinalizemethod
     \page_shipouts_handle{#1}%
   \else
     \setbox\shipoutscratchbox\hbox{#1}% just in case there are objects there, hook for testing (will go away)
     \finalizeshipoutbox\shipoutscratchbox
     \page_shipouts_handle{\box\shipoutscratchbox}%
   \fi
   \setnextrealpageno       % so this comes before \everyaftershipout so in fact:
   \the\everyaftershipout   % at this point we're already on the next realpage
   \ifcase\c_page_boxes_flush_n\else
     \page_boxes_flush_after
   \fi}

\def\page_shipouts_ignore#1%
  {\begingroup
   \message
     {[\ifarrangingpages arranged \fi page
       \ifarrangingpages\the\arrangeno\else\the\realpageno\fi\normalspace
       not flushed]}%
   \setbox\scratchbox\hbox{#1}%
   \deadcycles\zerocount
   \endgroup}

\def\page_otr_flush_every_stuff
  {\begingroup
   \setbox\scratchbox\hbox
     {% before the main one !
      \ifcase\realfolio \or
        \the\everyfirstshipout
        \global\everyfirstshipout\emptytoks
      \fi
      % the main one
      \the\everyshipout\relax
      % always last (and after the main one)
      \ifnum\realpageno=\lastpage\relax
        \the\everylastshipout
        \global\everylastshipout\emptytoks
      \fi}%
   \smashbox\scratchbox
   \box\scratchbox
   \endgroup}

\newdimen\d_page_shipouts_offset \d_page_shipouts_offset=-1in

\def\page_shipouts_normal#1%
  {\global\advance\shippedoutpages\plusone
   % this is not resource safe!
   \ifx\pagestoshipout\empty
     \ifcase\whichpagetoshipout\relax
       \donetrue
     \or % 1
       \ifodd\shippedoutpages\relax\donetrue\else\donefalse\fi
     \or % 2
       \ifodd\shippedoutpages\relax\donefalse\else\donetrue\fi
     \else
       \donetrue
     \fi
   \else % testen, aangepast / expanded nodig ?
     \normalexpanded{\doifinsetelse{\the\shippedoutpages}{\pagestoshipout}}\donetrue\donefalse
   \fi
   \ifdone
     \setbox\shipoutscratchbox\hbox{#1}%
     \ifcase\shipoutfinalizemethod
       \finalizeshipoutbox\shipoutscratchbox
     \fi
     \normalshipout\vbox
       {\offinterlineskip
        \vskip\d_page_shipouts_offset
        \hskip\d_page_shipouts_offset
        \hbox
          {\page_otr_flush_every_stuff
           \page_otr_flush_special_content
           \box\shipoutscratchbox}}%
   \else
     \page_shipouts_ignore{#1}%
   \fi}

\def\page_shipouts_arrange#1%
  {% \global\advance\shippedoutpages\plusone
   \begingroup
   \setbox\scratchbox\hbox
     {\page_otr_flush_every_stuff
      \page_otr_flush_special_content
      \box\shipoutscratchbox}%
   \pusharrangedpage\scratchbox
   \deadcycles\zerocount
   \endgroup}

%D We need a couple of boxes for duplex printing \unknown

\newbox\arrangedpageA \newbox\arrangedpageB
\newbox\arrangedpageC \newbox\arrangedpageD
\newbox\arrangedpageE \newbox\arrangedpageF
\newbox\arrangedpageG \newbox\arrangedpageH

\newconditional\arrangedswapstate
\newconditional\arrangednegatestate
\newconditional\arrangedmirrorstate
\newconditional\arrangeddoublestate
\newconditional\arrangingdisabledstate
\newconditional\arrangedbackgroundstate

\def\arrangedrotationO{0}
\def\arrangedrotationE{0}

\newcount\arrangedpageN
\newcount\arrangedpageM

\newcount\arrangedpageT \arrangedpageT\plusone
\newcount\arrangedpageX \arrangedpageX\plusone
\newcount\arrangedpageY \arrangedpageY\plusone

\def\page_paper_set_offsets
  {\global\paperoffset\v_page_target_offset
   \global\advance\paperwidth -2\dimexpr\paperoffset/\arrangedpageX\relax
   \global\advance\paperheight-2\dimexpr\paperoffset/\arrangedpageY\relax}

\def\doinstallarrangedoption#1#2%
  {\setvalue{\??layoutarrangeoption#1}{#2}}

\def\doinstalledarrangedoption#1%
  {\ifcsname\??layoutarrangeoption#1\endcsname
     \csname\??layoutarrangeoption#1\endcsname
   \else
     \checkinstalledpagearrangement{#1}% this installs the arranger
   \fi}

\doinstallarrangedoption\empty
  {} % no default and check if empty, we can have ,,,

\doinstallarrangedoption\v!disable
  {\global\settrue\arrangingdisabledstate}

\doinstallarrangedoption\v!mirrored
  {\global\settrue\arrangedmirrorstate}

\doinstallarrangedoption\v!doublesided
  {\global\settrue\arrangeddoublestate}

\doinstallarrangedoption\v!negative
  {\global\settrue\arrangednegatestate}

\doinstallarrangedoption\v!rotated
  {\gdef\arrangedrotationO {90}%
   \gdef\arrangedrotationE{270}%
   \swapcounts\c_page_marks_nx\c_page_marks_ny}

\doinstallarrangedoption{90}
  {\gdef\arrangedrotationO {90}%
   \gdef\arrangedrotationE{270}%
   \swapcounts\c_page_marks_nx\c_page_marks_ny}

\doinstallarrangedoption{180}
  {\gdef\arrangedrotationO{180}%
   \gdef\arrangedrotationE  {0}}

\doinstallarrangedoption{270}
  {\gdef\arrangedrotationO{270}%
   \gdef\arrangedrotationE {90}%
   \swapcounts\c_page_marks_nx\c_page_marks_ny}

\doinstallarrangedoption\s!reset
  {\global\arrangingpagesfalse}

\doinstallarrangedoption\v!background
  {\global\settrue\arrangedbackgroundstate}

\unexpanded\def\setuparranging[#1]%
  {\ifconditional\arrangingdisabledstate \else
     %global\setfalse\arrangingdisabledstate
     \global\arrangingpagestrue % will be conditional
     \global\setfalse\arrangednegatestate
     \global\setfalse\arrangedmirrorstate
     \global\setfalse\arrangeddoublestate
     \global\setfalse\arrangedswapstate
     \gdef\arrangedrotationO{0}%
     \gdef\arrangedrotationE{180}%
     \processcommalist[#1]\doinstalledarrangedoption
     \ifx\handlearrangedpage\undefined
       \global\arrangingpagesfalse
     \fi
     \setuppapersize
     \ifarrangingpages
       \ctxlua{job.disablesave()}%
       %\disabledirective[job.save]%
     \fi
   \fi}

\def\installpagearrangement #1 % will lchange, no space
  {\setgvalue{\??layoutarranger#1}}

\def\checkinstalledpagearrangement#1% can be empty: aaa,,bbb
  {\executeifdefined{\??layoutarranger#1}\donothing}

\let\poparrangedpages\relax
\let\pusharrangedpage\relax

\def\dosetuparrangement#1#2#3#4#5#6#7#8%
  {\global\arrangedpageX         #1%
   \global\arrangedpageY         #2%
   \global\arrangedpageT         #3%
   \global\c_page_marks_nx       #4%
   \global\c_page_marks_ny       #5%
   \global\let\pusharrangedpage  #6%
   \global\let\poparrangedpages  #7%
   \global\let\handlearrangedpage#8}

\installpagearrangement {\v!normal}
  {\global\arrangingpagesfalse}

\installpagearrangement 2*16
  {\dosetuparrangement{4}{4}{16}{5}{5}%
     \pusharrangedpageTHIRTYTWO\poparrangedpagesAB\relax}

\installpagearrangement 2*8
  {\dosetuparrangement{4}{2}{8}{5}{3}%
     \pusharrangedpageSIXTEEN\poparrangedpagesAB\relax}

\installpagearrangement 2*4
  {\dosetuparrangement{2}{2}{4}{3}{3}%
     \pusharrangedpageEIGHT\poparrangedpagesAB\relax}

\installpagearrangement 2*2
  {\dosetuparrangement{2}{1}{2}{3}{2}%
     \pusharrangedpageFOURA\poparrangedpagesAB\relax}

\installpagearrangement 2**2
  {\dosetuparrangement{2}{1}{2}{3}{2}%
     \pusharrangedpageFOURB\poparrangedpagesAB\relax}

\installpagearrangement 2SIDE
  {\dosetuparrangement{2}{1}{2}{3}{2}%
     \pusharrangedpageSIDETOP\poparrangedpagesTWO\handlearrangedpageSIDE}

\installpagearrangement 2TOP
  {\dosetuparrangement{1}{2}{2}{2}{3}%
     \pusharrangedpageSIDETOP\poparrangedpagesTWO\handlearrangedpageTOP}

\installpagearrangement 2UP
  {\dosetuparrangement{2}{1}{4}{3}{2}%
     \pusharrangedpageTWO\poparrangedpagesTWO\handlearrangedpageTWOUP}

\installpagearrangement 2DOWN
  {\dosetuparrangement{1}{2}{4}{2}{3}%
     \pusharrangedpageTWO\poparrangedpagesTWO\handlearrangedpageTWODOWN}

\installpagearrangement 2*4*2 % one defined by Willy Egger:
  {\dosetuparrangement{2}{2}{4}{3}{3}%
     \pusharrangedpageSIXTEENTWO\poparrangedpagesAtoD\relax}

\installpagearrangement 2*2*4 % onother one of Willy Egger
  {\dosetuparrangement{2}{1}{8}{3}{2}%
     \pusharrangedpageSIXTEENFOUR\poparrangedpagesAtoH\relax}

\installpagearrangement 2TOPSIDE
  {\dosetuparrangement{1}{2}{4}{2}{3}%
     \pusharrangedpageTWOTOPSIDE\poparrangedpagesTWOTOPSIDE\handlearrangedpageTOP}

\def\filluparrangedpages % beware: \realpageno is 1 ahead
  {\ifarrangingpages
     \scratchcounter\numexpr\realpageno-\plusone\relax
     \dosetmodulo\scratchcounter\arrangedpageT\scratchcounter
     \ifcase\scratchcounter\else
       \advance\scratchcounter \plusone
       \dostepwiserecurse\scratchcounter\arrangedpageT\plusone
         {\noheaderandfooterlines\page_otr_insert_dummy_page}%
     \fi
   \fi}

\def\handlearrangedpageXandY#1#2#3#4#5%
  {\global\setbox#5\hbox to \arrangedpageX\paperwidth
     {\setbox\scratchbox\vbox to \arrangedpageY\paperheight
        {\offinterlineskip
         \vskip#4\paperheight
         \hskip#3\paperwidth
         \dorotatebox{\ifcase#2 0\else180\fi}\hbox{\box#1}%
         \vfill}%
      \wd\scratchbox\zeropoint
      \box\scratchbox\box#5\hss}}

\def\gotonextarrangepage
  {\global\advance\arrangeno \plusone
   \def\pagecutmarksymbol{\the\arrangeno}}

\def\outputarrangedbox#1%
  {\begingroup
   \forgetall % somehow we're back and need to redo this
   \gotonextarrangepage
   \ifnum\arrangedrotationO\arrangedrotationE>\zerocount
     \setbox#1\vbox
       {\ifconditional\arrangeddoublestate
          \ifodd\arrangeno
            \dorotatebox\arrangedrotationO\hbox{\box#1}%
          \else
            \dorotatebox\arrangedrotationE\hbox{\box#1}%
          \fi
        \else
          \dorotatebox\arrangedrotationO\hbox{\box#1}%
        \fi}%
   \fi
   \ifconditional\arrangedmirrorstate
     \page_boxes_apply_mirror{#1}%
   \fi
   \ifconditional\arrangednegatestate
     \page_boxes_apply_negate{#1}%
   \fi
   \page_marks_add_more             #1%
   \page_boxes_apply_center         #1%
   \page_boxes_apply_mirror_print   #1%
   \page_boxes_apply_orientate_print#1%
   \page_boxes_apply_offset         #1%
   \page_boxes_apply_negate_print   #1%
   %
   \ifconditional\arrangedbackgroundstate
     \page_backgrounds_add_to_print#1%
   \fi
   %
   \page_shipouts_normal{\box#1}%
   \endgroup}

%D The format file can be 16K smaller when we postpone the
%D real arrangments. Some day ...

\def\reportarrangedpage#1%
  {\showmessage\m!system{23}{\the\realpageno.\the\pageno\ifnum\subpageno>0 .\the\subpageno\fi,\number#1}}

\def\advancearrangedpageN
  {\global\advance\arrangedpageN\plusone}

% TOP

% 32/16/8/4/SIDE

\def\poparrangedpagesAB
  {\ifnum\arrangedpageN>\zerocount
     \paperwidth\arrangedpageX\paperwidth
     \paperheight\arrangedpageY\paperheight
     \outputarrangedbox\arrangedpageA
     \outputarrangedbox\arrangedpageB
     \global\arrangedpageN\zerocount
   \fi}

\def\pusharrangedpageTHIRTYTWO#1% taco's challenge
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}033\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}003\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}130\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}130\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}033\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}003\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}102\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}132\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}031\arrangedpageB % 11
   \or \handlearrangedpageXandY{#1}001\arrangedpageA % 12
   \or \handlearrangedpageXandY{#1}031\arrangedpageA % 13
   \or \handlearrangedpageXandY{#1}001\arrangedpageB % 14
   \or \handlearrangedpageXandY{#1}102\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}132\arrangedpageA % 16
   \or \handlearrangedpageXandY{#1}122\arrangedpageA % 17
   \or \handlearrangedpageXandY{#1}112\arrangedpageB % 18
   \or \handlearrangedpageXandY{#1}011\arrangedpageB % 19
   \or \handlearrangedpageXandY{#1}021\arrangedpageA % 20
   \or \handlearrangedpageXandY{#1}011\arrangedpageA % 21
   \or \handlearrangedpageXandY{#1}021\arrangedpageB % 22
   \or \handlearrangedpageXandY{#1}122\arrangedpageB % 23
   \or \handlearrangedpageXandY{#1}112\arrangedpageA % 24
   \or \handlearrangedpageXandY{#1}013\arrangedpageA % 25
   \or \handlearrangedpageXandY{#1}023\arrangedpageB % 26
   \or \handlearrangedpageXandY{#1}120\arrangedpageB % 27
   \or \handlearrangedpageXandY{#1}110\arrangedpageA % 28
   \or \handlearrangedpageXandY{#1}120\arrangedpageA % 29
   \or \handlearrangedpageXandY{#1}110\arrangedpageB % 30
   \or \handlearrangedpageXandY{#1}013\arrangedpageB % 31
   \or \handlearrangedpageXandY{#1}023\arrangedpageA % 32
     \poparrangedpages
   \fi}

\def\pusharrangedpageSIXTEEN#1% changed to match the official way of doing
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}031\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}001\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}031\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}130\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}130\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}120\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}110\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}120\arrangedpageB % 11
   \or \handlearrangedpageXandY{#1}110\arrangedpageA % 12
   \or \handlearrangedpageXandY{#1}011\arrangedpageA % 13
   \or \handlearrangedpageXandY{#1}021\arrangedpageB % 14
   \or \handlearrangedpageXandY{#1}011\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}021\arrangedpageA % 16
     \poparrangedpages
   \fi}

\def\pusharrangedpageEIGHT#1% changed to match the official way of doing
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}011\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}001\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}110\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}110\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}011\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  8
     \poparrangedpages
   \fi}

\def\pusharrangedpageFOURA{\pusharrangedpageFOURdo01}
\def\pusharrangedpageFOURB{\pusharrangedpageFOURdo10}

\def\pusharrangedpageFOURdo#1#2#3%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#3}010\arrangedpageA    %  1
   \or \handlearrangedpageXandY{#3}0{#1}0\arrangedpageB %  2/3 not {1}
   \or \handlearrangedpageXandY{#3}0{#2}0\arrangedpageB %  3/2 not {1}
   \or \handlearrangedpageXandY{#3}000\arrangedpageA    %  4
     \poparrangedpages
   \fi}

\def\pusharrangedpageSIDETOP#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
     \poparrangedpages
   \fi}

\def\handlearrangedpageSIDE
  {\wd\arrangedpageA\paperwidth
   \wd\arrangedpageB\paperwidth
   \global\setbox\arrangedpageA\hbox
     {\box\arrangedpageA\box\arrangedpageB}%
   \ht\arrangedpageA\paperheight}

\def\handlearrangedpageTOP
  {\ht\arrangedpageA\paperheight
   \ht\arrangedpageB\paperheight
   \global\setbox\arrangedpageA\vbox
     {\offinterlineskip\vskip\paperheight
      \box\arrangedpageA\box\arrangedpageB}%
   \global\setbox\arrangedpageB\box\scratchbox} % ?

% 2UP/2DOWN / 1pt prevents overflow

\def\splitoffarrangedpagesTWO
  {\splittopskip\zeropoint
   \global\setbox\arrangedpageA\vsplit\arrangedpageB to \onepoint
   \scratchdimen\dimexpr\ht\arrangedpageB-\onepoint\relax
   \ifdim\scratchdimen>\onepoint
     \setbox\scratchbox\vsplit\arrangedpageB to \scratchdimen
   \fi}

\def\handlearrangedpageTWOUP
  {\splitoffarrangedpagesTWO
   \ht\arrangedpageA\paperheight
   \ht\arrangedpageB\paperheight
   \ifconditional\arrangedswapstate
     \global\setbox\arrangedpageA\hbox
       {\page_boxes_apply_clip_print_left \arrangedpageA
        \box\arrangedpageA
        \page_boxes_apply_clip_print_right\arrangedpageB
        \box\arrangedpageB}%
     \setfalse\arrangedswapstate
   \else
     \global\setbox\arrangedpageA\hbox
       {\page_boxes_apply_clip_print_left \arrangedpageB
        \box\arrangedpageB
        \page_boxes_apply_clip_print_right\arrangedpageA
        \box\arrangedpageA}%
     \settrue\arrangedswapstate
   \fi
   \global\setbox\arrangedpageB\box\scratchbox}

\def\handlearrangedpageTWODOWN
  {\splitoffarrangedpagesTWO
   \ht\arrangedpageA\paperheight
   \ht\arrangedpageB\paperheight
   \ifconditional\arrangedswapstate
     \global\setbox\arrangedpageA\vbox
       {\offinterlineskip
        \vskip\paperheight
        \box\arrangedpageA
        \box\arrangedpageB}%
     \setfalse\arrangedswapstate
   \else
     \global\setbox\arrangedpageA\vbox
       {\offinterlineskip
        \vskip\paperheight
        \box\arrangedpageB
        \box\arrangedpageA}%
     \settrue\arrangedswapstate
   \fi
   \global\setbox\arrangedpageB\box\scratchbox}

\def\poparrangedpagesTWO
  {\ifnum\arrangedpageN>\zerocount
     \setfalse\arrangedswapstate
     \doloop
       {\handlearrangedpage
        \bgroup
        \paperwidth \arrangedpageX\paperwidth
        \paperheight\arrangedpageY\paperheight
        \ht\arrangedpageA\paperheight
        \wd\arrangedpageA\paperwidth
        \outputarrangedbox\arrangedpageA
        \egroup
        \ifdim\ht\arrangedpageB=\zeropoint
          \exitloop
        \fi}%
     \global\arrangedpageN\zerocount
   \fi}

\def\pusharrangedpageTWO#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \global\setbox\arrangedpageB\vbox
     {\offinterlineskip
      \unvbox\arrangedpageB
      \allowbreak
      \setbox#1\vbox{\box#1}% really needed in order to keep real dimensions
      \ht#1\onepoint
      \dp#1\zeropoint
      \vbox{\box#1}}}

\def\poparrangedpagesTWOTOPSIDE
  {\ifnum\arrangedpageN>\zerocount
     \bgroup
     \global\arrangedpageN\plustwo
     \poparrangedpagesTWO
     \let\arrangedpageA\arrangedpageC
     \let\arrangedpageB\arrangedpageD
     \global\arrangedpageN\plustwo
     \poparrangedpagesTWO
     \global\arrangedpageN\zerocount
     \egroup
  \fi}

\def\pusharrangedpageTWOTOPSIDE#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}000\arrangedpageC %  2
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  1
   \or \handlearrangedpageXandY{#1}000\arrangedpageD %  2
     \poparrangedpages
   \fi}

%D Willy Egger's sheet simulations:

\def\poparrangedpagesAtoH
  {\ifnum\arrangedpageN>\zerocount
     \paperwidth \arrangedpageX\paperwidth
     \paperheight\arrangedpageY\paperheight
     \outputarrangedbox\arrangedpageA
     \outputarrangedbox\arrangedpageB
     \outputarrangedbox\arrangedpageC
     \outputarrangedbox\arrangedpageD
     \outputarrangedbox\arrangedpageE
     \outputarrangedbox\arrangedpageF
     \outputarrangedbox\arrangedpageG
     \outputarrangedbox\arrangedpageH
     \global\arrangedpageN\zerocount
   \fi}

% to arrange 16 pages on 2 sheets to form one booklet

\def\poparrangedpagesAtoD
  {\ifnum\arrangedpageN>\zerocount
     \paperwidth\arrangedpageX\paperwidth
     \paperheight\arrangedpageY\paperheight
     \outputarrangedbox\arrangedpageA
     \outputarrangedbox\arrangedpageB
     \outputarrangedbox\arrangedpageC
     \outputarrangedbox\arrangedpageD
     \global\arrangedpageN\zerocount
   \fi}

% to arrange 16 pages on 4 sheets to form one booklet

\def\pusharrangedpageSIXTEENFOUR#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageC %  3
   \or \handlearrangedpageXandY{#1}000\arrangedpageD %  4
   \or \handlearrangedpageXandY{#1}010\arrangedpageE %  5
   \or \handlearrangedpageXandY{#1}000\arrangedpageF %  6
   \or \handlearrangedpageXandY{#1}010\arrangedpageG %  7
   \or \handlearrangedpageXandY{#1}000\arrangedpageH %  8
   \or \handlearrangedpageXandY{#1}010\arrangedpageH %  9
   \or \handlearrangedpageXandY{#1}000\arrangedpageG % 10
   \or \handlearrangedpageXandY{#1}010\arrangedpageF % 11
   \or \handlearrangedpageXandY{#1}000\arrangedpageE % 12
   \or \handlearrangedpageXandY{#1}010\arrangedpageD % 13
   \or \handlearrangedpageXandY{#1}000\arrangedpageC % 14
   \or \handlearrangedpageXandY{#1}010\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}000\arrangedpageA % 16
     \poparrangedpages
   \fi}

% to arrange 16 pages on 2 sheets to form one booklet

\def\pusharrangedpageSIXTEENTWO#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}011\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}001\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}011\arrangedpageC %  3
   \or \handlearrangedpageXandY{#1}001\arrangedpageD %  4
   \or \handlearrangedpageXandY{#1}100\arrangedpageD %  5
   \or \handlearrangedpageXandY{#1}110\arrangedpageC %  6
   \or \handlearrangedpageXandY{#1}100\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}110\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}110\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}100\arrangedpageC % 11
   \or \handlearrangedpageXandY{#1}110\arrangedpageD % 12
   \or \handlearrangedpageXandY{#1}011\arrangedpageD % 13
   \or \handlearrangedpageXandY{#1}001\arrangedpageC % 14
   \or \handlearrangedpageXandY{#1}011\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}001\arrangedpageA % 16
     \poparrangedpages
   \fi}

%D Might be used if a printer is printing from a rol or creating mini-books from A4:
%D This section has 16 pages. The folding scheme is first a Z-fold and at the end
%D a final fold in the spine.
%D Coding: [2*8*Z]

\installpagearrangement 2*8*Z
   {\dosetuparrangement{2}{4}{8}{3}{5}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageSIXTEENZ\poparrangedpagesAB\relax}


\def\pusharrangedpageSIXTEENZ#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  1
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}101\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}111\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}012\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}002\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}103\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}113\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}103\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}113\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}012\arrangedpageB % 11
   \or \handlearrangedpageXandY{#1}002\arrangedpageA % 12
   \or \handlearrangedpageXandY{#1}101\arrangedpageA % 13
   \or \handlearrangedpageXandY{#1}111\arrangedpageB % 14
   \or \handlearrangedpageXandY{#1}010\arrangedpageB % 15
   \or \handlearrangedpageXandY{#1}000\arrangedpageA % 16
     \poparrangedpages
   \fi}

%D Another Z-folded section with 12 pages
%D Coding: [2*6*Z]

\installpagearrangement 2*6*Z
   {\dosetuparrangement{2}{3}{6}{3}{4}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageTWELVEZ\poparrangedpagesAB\relax}

\def\pusharrangedpageTWELVEZ#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  1: rotation (0=upright),x (0=first column),y (0=first row)
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}101\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}111\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}012\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}002\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}012\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}002\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}101\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}111\arrangedpageB % 10
   \or \handlearrangedpageXandY{#1}010\arrangedpageB % 11
   \or \handlearrangedpageXandY{#1}000\arrangedpageA % 12
     \poparrangedpages
   \fi}

%D For Heinz' special greeting cards folding. This scheme is also used for the PocketDiary (module):
%D Coding: [1*8]

\installpagearrangement 1*8
   {\dosetuparrangement{4}{2}{8}{5}{3}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageEIGHTSINGLESIDEDFOLDED\poparrangedpagesTWO\relax}

\def\pusharrangedpageEIGHTSINGLESIDEDFOLDED#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  1 rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  2
   \or \handlearrangedpageXandY{#1}030\arrangedpageA %  3
   \or \handlearrangedpageXandY{#1}131\arrangedpageA %  4
   \or \handlearrangedpageXandY{#1}121\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}111\arrangedpageA %  6
   \or \handlearrangedpageXandY{#1}101\arrangedpageA %  7
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  8
     \poparrangedpages
   \fi}

%D This is not a section. \CONTEXT\ places 4 pages on a sheet of paper, singlesided
%D Coding: [1*4]

\installpagearrangement 1*4
   {\dosetuparrangement{2}{2}{4}{3}{3}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageFOURSINGLESIDEDFOLDED\poparrangedpagesTWO\relax}

\def\pusharrangedpageFOURSINGLESIDEDFOLDED#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  1 rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  2
   \or \handlearrangedpageXandY{#1}011\arrangedpageA %  3
   \or \handlearrangedpageXandY{#1}110\arrangedpageA %  4
     \poparrangedpages
   \fi}

%D This imposition scheme was requested by Hraban Ramm, by Willi Egger 21-07-2003
%D Coding: [3SIDE]

\installpagearrangement 3SIDE
   {\dosetuparrangement{3}{1}{3}{4}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageTHREESIDE\poparrangedpagesAB\relax}

\def\pusharrangedpageTHREESIDE#1% Willi's approach
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  2
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  3
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  4
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  5
   \or \handlearrangedpageXandY{#1}020\arrangedpageB %  6
       \poparrangedpages
   \fi}

%D FLYER in three parts and 6 pages 22-10-2010
%D Coding: [TRYPTICHON]

\installpagearrangement TRYPTICHON
   {\dosetuparrangement{3}{1}{3}{4}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageFLYERSIX\poparrangedpagesAB\relax}

\def\pusharrangedpageFLYERSIX#1% Willi's approach
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}020\arrangedpageB %  4
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  5
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  6
       \poparrangedpages
   \fi}

%D FLYER in Z-fold with 8 pages 22-01-2010
%D Coding: [ZFLYER-8]

\installpagearrangement ZFLYER-8
   {\dosetuparrangement{4}{1}{4}{5}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageZFLYEREIGHT\poparrangedpagesAB\relax}

\def\pusharrangedpageZFLYEREIGHT#1% Willi's approach
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}030\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}020\arrangedpageB %  4
   \or \handlearrangedpageXandY{#1}030\arrangedpageB %  5
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  6
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  7
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  8
       \poparrangedpages
   \fi}

%D FLYER in Z-fold with 10 pages 04-08-2010
%D Coding: [ZFLYER-10]

\installpagearrangement ZFLYER-10
   {\dosetuparrangement{5}{1}{5}{6}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageZFLYERTEN\poparrangedpagesAB\relax}

\def\pusharrangedpageZFLYERTEN#1% Willi's approach
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}040\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}020\arrangedpageB %  4
   \or \handlearrangedpageXandY{#1}030\arrangedpageB %  5
   \or \handlearrangedpageXandY{#1}040\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  7
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}030\arrangedpageA %  10
       \poparrangedpages
   \fi}

%D FLYER in Z-fold with 12 pages 04-08-2010
%D Coding: [ZFLYER-12]

\installpagearrangement ZFLYER-12
   {\dosetuparrangement{6}{1}{6}{7}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageZFLYERTWELVE\poparrangedpagesAB\relax}

\def\pusharrangedpageZFLYERTWELVE#1% Willi's approach
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}050\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}020\arrangedpageB %  4
   \or \handlearrangedpageXandY{#1}030\arrangedpageB %  5
   \or \handlearrangedpageXandY{#1}040\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}050\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  10
   \or \handlearrangedpageXandY{#1}030\arrangedpageA %  11
   \or \handlearrangedpageXandY{#1}040\arrangedpageA %  12
       \poparrangedpages
   \fi}

%D FLYER folded as a map with 6 pages per side.
%D Coding: [MAPFLYER-12]

\installpagearrangement MAPFLYER-12
   {\dosetuparrangement{3}{2}{6}{4}{3}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageMFLYERTWELVE\poparrangedpagesAB\relax}

\def\pusharrangedpageMFLYERTWELVE#1% Willi's approach
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}001\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  4
   \or \handlearrangedpageXandY{#1}011\arrangedpageB %  5
   \or \handlearrangedpageXandY{#1}020\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}021\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  8
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  9
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  10
   \or \handlearrangedpageXandY{#1}011\arrangedpageA %  11
   \or \handlearrangedpageXandY{#1}021\arrangedpageA %  12
       \poparrangedpages
   \fi}

%D FLYER folded as double window with 4 pages per side.
%D Coding: [DOUBLEWINDOW]

\installpagearrangement DOUBLEWINDOW
   {\dosetuparrangement{4}{1}{4}{5}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageDOUBLEWINDOWEIGHT\poparrangedpagesAB\relax}

\def\pusharrangedpageDOUBLEWINDOWEIGHT#1% Willi's approach
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}020\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}030\arrangedpageA %  2
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  4
   \or \handlearrangedpageXandY{#1}020\arrangedpageB %  5
   \or \handlearrangedpageXandY{#1}030\arrangedpageB %  6
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  7
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  8
       \poparrangedpages
   \fi}

%D Imposition as requested by Jan Pohanka 26-08-2010, 4 pages, two verso, two recto,
%D uneven pages upright and down, even pages top and rotated 180.
%D Implementation with 2 pages for conference-name-display
%D Coding: [1*2-Conference]

\installpagearrangement 1*2-Conference
   {\dosetuparrangement{1}{2}{4}{3}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageCONFERENCE2\poparrangedpagesAB\relax}

\def\pusharrangedpageCONFERENCE2#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  2
       \poparrangedpages
   \fi}

%D Implementation with 4 pages for conference-name-display
%D Coding: [1*4-Conference]

\installpagearrangement 1*4-Conference
   {\dosetuparrangement{1}{2}{4}{3}{2}% X,Y,Total,hcutmarks,vcutmarks
        \pusharrangedpageCONFERENCE4\poparrangedpagesAB\relax}

\def\pusharrangedpageCONFERENCE4#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}001\arrangedpageA %  1  rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}100\arrangedpageA %  2
   \or \handlearrangedpageXandY{#1}011\arrangedpageB %  3
   \or \handlearrangedpageXandY{#1}110\arrangedpageB %  4
       \poparrangedpages
   \fi}

%D There should be arrangements for sections made of heavy and thick paper. i.e. the heavier the paper
%D the fewer pages per section:
%D Section with 8 pages put on to sheets of paper. Each sheet carries recto 2 and verso 2 pages.
%D Coding: [2*2*2]

\installpagearrangement 2*2*2
  {\dosetuparrangement{2}{1}{2}{3}{2}% X,Y,Total,hcutmarks,vcutmarks
     \pusharrangedpageEIGHTTWO\poparrangedpagesAtoD\relax}

\def\pusharrangedpageEIGHTTWO#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  1 rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageC %  3
   \or \handlearrangedpageXandY{#1}000\arrangedpageD %  4
   \or \handlearrangedpageXandY{#1}010\arrangedpageD %  5
   \or \handlearrangedpageXandY{#1}000\arrangedpageC %  6
   \or \handlearrangedpageXandY{#1}010\arrangedpageB %  7
   \or \handlearrangedpageXandY{#1}000\arrangedpageA %  8
       \poparrangedpages
   \fi}

%D Section with 12 pages, built from three sheets of paper.
%D Each sheet carries 2 pages recto and verso.
%D Coding: [2*2*3]

\def\poparrangedpagesAtoF
  {\ifnum\arrangedpageN>\zerocount
     \paperwidth \arrangedpageX\paperwidth
     \paperheight\arrangedpageY\paperheight
     \outputarrangedbox\arrangedpageA
     \outputarrangedbox\arrangedpageB
     \outputarrangedbox\arrangedpageC
     \outputarrangedbox\arrangedpageD
     \outputarrangedbox\arrangedpageE
     \outputarrangedbox\arrangedpageF
     \global\arrangedpageN\zerocount
   \fi}

\installpagearrangement 2*2*3
  {\dosetuparrangement{2}{1}{2}{3}{2}% X,Y,Total,hcutmarks,vcutmarks
     \pusharrangedpageTWELVETWO\poparrangedpagesAtoF\relax}

\def\pusharrangedpageTWELVETWO#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifcase\arrangedpageN
   \or \handlearrangedpageXandY{#1}010\arrangedpageA %  1 rot,hskip,vskip
   \or \handlearrangedpageXandY{#1}000\arrangedpageB %  2
   \or \handlearrangedpageXandY{#1}010\arrangedpageC %  3
   \or \handlearrangedpageXandY{#1}000\arrangedpageD %  4
   \or \handlearrangedpageXandY{#1}010\arrangedpageE %  5
   \or \handlearrangedpageXandY{#1}000\arrangedpageF %  6
   \or \handlearrangedpageXandY{#1}010\arrangedpageF %  7
   \or \handlearrangedpageXandY{#1}000\arrangedpageE %  8
   \or \handlearrangedpageXandY{#1}010\arrangedpageD %  9
   \or \handlearrangedpageXandY{#1}000\arrangedpageC % 10
   \or \handlearrangedpageXandY{#1}010\arrangedpageB % 11
   \or \handlearrangedpageXandY{#1}000\arrangedpageA % 12
       \poparrangedpages
   \fi}

% % From Wolfgang for Mari (mail on list) ... yes or no in core .. time for
% % delayed loading ...
% %
% % http://stamphenge.wordpress.com/minibooks/meander-accordion-folded-book/
%
% \installpagearrangement MEANDER16
%   {\dosetuparrangement{4}{4}{16}{5}{5}%
%      \pusharrangedpageMEANDERSIXTEEN\poparrangedpagesXY\relax}
%
% \def\pusharrangedpageMEANDERSIXTEEN#1%
%   {\advancearrangedpageN
%    \reportarrangedpage\arrangedpageN
%    \ifcase\arrangedpageN
%    \or \handlearrangedpageXandY{#1}000\arrangedpageA
%    \or \handlearrangedpageXandY{#1}010\arrangedpageA
%    \or \handlearrangedpageXandY{#1}020\arrangedpageA
%    \or \handlearrangedpageXandY{#1}030\arrangedpageA
%    \or \handlearrangedpageXandY{#1}131\arrangedpageA
%    \or \handlearrangedpageXandY{#1}121\arrangedpageA
%    \or \handlearrangedpageXandY{#1}111\arrangedpageA
%    \or \handlearrangedpageXandY{#1}101\arrangedpageA
%    \or \handlearrangedpageXandY{#1}002\arrangedpageA
%    \or \handlearrangedpageXandY{#1}012\arrangedpageA
%    \or \handlearrangedpageXandY{#1}022\arrangedpageA
%    \or \handlearrangedpageXandY{#1}032\arrangedpageA
%    \or \handlearrangedpageXandY{#1}133\arrangedpageA
%    \or \handlearrangedpageXandY{#1}123\arrangedpageA
%    \or \handlearrangedpageXandY{#1}113\arrangedpageA
%    \or \handlearrangedpageXandY{#1}103\arrangedpageA
%      \poparrangedpages
%    \fi}
%
% % \definepapersize[small][width=6cm,height=6cm]
% % \definepapersize[big][width=30cm,height=30cm]
% % \setuppapersize[small][big]
% % \setuppagenumbering[location=]
% % \setuparranging[MEANDER16]
% % \setuplayout
% %   [location=middle,
% %    marking=on]
% % \starttext
% % \dorecurse{32}{\centerbox{\ssd\recurselevel}}
% % \stoptext
%
% % By Willi:
%
% \installpagearrangement MEANDER9
%  {\dosetuparrangement{3}{3}{9}{4}{4}%
%     \pusharrangedpageMEANDERNINE\poparrangedpagesXY\relax}
%
% \def\pusharrangedpageMEANDERNINE#1%
%  {\advancearrangedpageN
%   \reportarrangedpage\arrangedpageN
%   \ifcase\arrangedpageN
%   \or \handlearrangedpageXandY{#1}000\arrangedpageA
%   \or \handlearrangedpageXandY{#1}010\arrangedpageA
%   \or \handlearrangedpageXandY{#1}020\arrangedpageA
%   \or \handlearrangedpageXandY{#1}121\arrangedpageA
%   \or \handlearrangedpageXandY{#1}111\arrangedpageA
%   \or \handlearrangedpageXandY{#1}101\arrangedpageA
%   \or \handlearrangedpageXandY{#1}002\arrangedpageA
%   \or \handlearrangedpageXandY{#1}012\arrangedpageA
%   \or \handlearrangedpageXandY{#1}022\arrangedpageA
%   \poparrangedpages
%   \fi}

% % handy for stickers etc, this way we can treat them as page
%
% \setuppapersize [XY][A4]
% \setuppaper     [topspace=5mm,backspace=5mm,dx=1mm,dy=1mm,nx=2,ny=6]
% \setuplayout    [page] [topspace=5mm,backspace=5mm]
% \setuplayout    [page]
% \setuplayout    [location=middle]
% \setuparranging [XY]
% \showframe
%
% \starttext \dorecurse{30}{test \recurselevel \page} \stoptext

\def\pusharrangedpageXY#1%
  {\advancearrangedpageN
   \global\advance\arrangedpageM\plusone
   \reportarrangedpage\arrangedpageN
   \global\setbox\arrangedpageB\hbox \ifdim\v_page_target_width>\zeropoint to \v_page_target_width \fi
      {\ifvoid\arrangedpageB\else
         \unhbox\arrangedpageB
         \ifdim\v_page_target_dx>\zeropoint
           \hskip\v_page_target_dx
         \else
           \hss
           \hskip\v_page_target_dx
           \hss
         \fi
       \fi
       \box#1}%
    \ifnum\arrangedpageM<\arrangedpageX\else
      \global\setbox\arrangedpageA\vbox \ifdim\v_page_target_height>\zeropoint to \v_page_target_height \fi
        {\offinterlineskip
         \ifvoid\arrangedpageA\else
           \unvbox\arrangedpageA
           \ifdim\v_page_target_dy>\zeropoint
             \vskip\v_page_target_dy
           \else
             \vss
             \vskip\v_page_target_dy
             \vss
           \fi
         \fi
         \box\arrangedpageB}%
      \global\arrangedpageM\zerocount
    \fi
    \ifnum\arrangedpageN<\arrangedpageT\else
      \poparrangedpages
    \fi}

\def\poparrangedpagesXY
  {\ifnum\arrangedpageN>\zerocount
     \paperwidth \arrangedpageX\paperwidth
     \paperheight\arrangedpageY\paperheight
     \outputarrangedbox\arrangedpageA
     \global\arrangedpageN\zerocount
     \global\arrangedpageM\zerocount
   \fi}

\installpagearrangement XY
  {\dosetuparrangement
     \v_page_target_nx
     \v_page_target_ny
     \v_page_target_xy
     \zerocount
     \zerocount
     \pusharrangedpageXY\poparrangedpagesXY\relax}

%D A crazy definition, don't guess who pushed me for the landscape option.

\def\page_imposition_xy_height
  {\dimexpr
     \dimexpr
       \v_page_target_height
      -\numexpr\v_page_target_ny-1\relax\v_page_target_dy
     \relax
    /\v_page_target_ny
   \relax}

\def\page_imposition_xy_width
  {\dimexpr
     \dimexpr
       \v_page_target_width
      -\numexpr\v_page_target_nx-1\relax\v_page_target_dx
     \relax
    /\v_page_target_nx
   \relax}

\definepapersize
  [XY]
  [\c!height=\page_imposition_xy_height,
   \c!width =\page_imposition_xy_width]

\setuppaper
  [\c!width =\dimexpr\printpaperwidth -2\v_page_target_backspace\relax,
   \c!height=\dimexpr\printpaperheight-2\v_page_target_topspace \relax]

% \definepageshift[test][horizontal][10pt,20pt,30pt,40pt,50pt]
% \definepageshift[test][vertical]  [10pt,20pt,30pt,40pt,50pt]
%
% \setuppageshift[test]
% \setuppageshift[test][test]
% \setuppageshift[test][none]
% \setuppageshift[none][test]
% \setuppageshift[paper][test][test] % arrange only
% \setuppageshift[paper][test]       % arrange only
% \setuppageshift[print][test][test]
%
% \showframe \dorecurse{100}{\input tufte \par}

% #1=name #2=horizontal|vertical #3=shiftlist

% this will move to lua

\installcorenamespace {pageshift}

\unexpanded\def\definepageshift
  {\dotripleargument\page_boxes_define_shift}

\def\page_boxes_define_shift[#1][#2][#3]%
  {\setvalue{\??pageshift#2:#1}{#3}}

\let\page_boxes_h_shifts\empty
\let\page_boxes_v_shifts\empty

\newcount\c_page_boxes_h_shifts
\newcount\c_page_boxes_v_shifts

\newdimen\d_page_boxes_h_shifts
\newdimen\d_page_boxes_v_shifts

\let\page_boxes_apply_shift_print\gobbleoneargument
\let\page_boxes_apply_shift_paper\gobbleoneargument

\def\page_boxes_filter_shift#1#2#3% #1=\dimenx #2=\xpageshifts #3=\nofxpageshifts
  {#1\zeropoint
   \ifx#2\empty \else
     \global\advance#3\plusone
     \getfromcommacommand[#2][#3]%
     \ifx\commalistelement\empty
       \global#3\plusone % we cycle
       \getfromcommacommand[#2][#3]%
     \fi
     \ifx\commalistelement\empty \else
       #1\commalistelement\relax % the relax is really needed as there is an \if later on
     \fi
   \fi}

\def\page_boxes_apply_shift#1%
  {\page_boxes_filter_shift\d_page_boxes_h_shifts\page_boxes_h_shifts\c_page_boxes_h_shifts
   \page_boxes_filter_shift\d_page_boxes_v_shifts\page_boxes_v_shifts\c_page_boxes_v_shifts
   \ifzeropt\d_page_boxes_h_shifts
     \ifzeropt\d_page_boxes_v_shifts
       % nothing to shift
     \else
       \page_boxes_apply_shift_indeed#1%
     \fi
   \else
     \page_boxes_apply_shift_indeed#1%
   \fi}

\def\page_boxes_apply_shift_indeed#1%
  {\edef\next{\wd#1\the\wd#1\ht#1\the\ht#1\dp#1\the\dp#1}%
   \setbox#1\vbox
     {\offinterlineskip
      \vskip\d_page_boxes_v_shifts
      \hskip\d_page_boxes_h_shifts
      \box#1}%
   \next}

\unexpanded\def\setuppageshift
  {\dotripleempty\page_boxes_setup_shift}

\def\page_boxes_setup_shift[#1][#2][#3]% page|paper horizontal vertical
  {\ifthirdargument                    % paper=arrange
     \edef\page_boxes_h_shifts{\ifcsname\??pageshift\v!horizontal:#2\endcsname\csname\??pageshift\v!horizontal:#2\endcsname\fi}%
     \edef\page_boxes_v_shifts{\ifcsname\??pageshift\v!vertical  :#3\endcsname\csname\??pageshift\v!vertical  :#3\endcsname\fi}%
     \doifelse{#1}\v!page {\let\page_boxes_apply_shift_print\page_boxes_apply_shift}{\let\page_boxes_apply_shift_print\gobbleoneargument}%
     \doifelse{#1}\v!paper{\let\page_boxes_apply_shift_paper\page_boxes_apply_shift}{\let\page_boxes_apply_shift_paper\gobbleoneargument}%
   \else\ifsecondargument
     \doifinsetelse{#1}{\v!page,\v!paper}
       {\setuppageshift[#1][#2][#2]}
       {\setuppageshift[\v!page][#1][#2]}%
   \else\iffirstargument
     \setuppageshift[\v!page][#1][#1]%
   \fi\fi\fi}

\protect \endinput
