%D \module
%D   [       file=core-box,
%D        version=2002.04.12,
%D          title=\CONTEXT\ Box Macros,
%D       subtitle=New Macros,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This module contains all kind of macros for moving content
%D around. Many macros here come from other modules, but
%D depencies made it more clear to isolate them.

% \placeornament

\unprotect

% \definelayer[\v!tekst-2][\c!positie=\v!ja]
% \definelayer[\v!tekst-1][\c!positie=\v!ja]
% \definelayer[\v!tekst+1][\c!positie=\v!ja]
% \definelayer[\v!tekst+2][\c!positie=\v!ja]

% we need to set the size, else we get dimensions depending
% on the content, which in itsel fis ok, but can lead to loops
% due to rounding errors (happened in demo-obv)

\definelayer[\v!tekst-2][\c!positie=\v!ja,\c!breedte=\overlaywidth,\c!hoogte=\overlayheight]
\definelayer[\v!tekst-1][\c!positie=\v!ja,\c!breedte=\overlaywidth,\c!hoogte=\overlayheight]
\definelayer[\v!tekst+1][\c!positie=\v!ja,\c!breedte=\overlaywidth,\c!hoogte=\overlayheight]
\definelayer[\v!tekst+2][\c!positie=\v!ja,\c!breedte=\overlaywidth,\c!hoogte=\overlayheight]

\def\internaltextoverlay#1% will become more generic and installable
  {\startoverlay          % i.e. probably an overlay by itself
     {\positionoverlay{\v!tekst#1}} % see later
     {\composedlayer  {\v!tekst#1}}
   \stopoverlay}

%\def\internaltextoverlay#1%
%  {\hbox to \zeropoint{\positionoverlay{\v!tekst#1}\hss}%
%   \composedlayer{\v!tekst#1}}

% todo: share info, so that tuo will be smaller

\defineoverlay[\v!tekst-2][\internaltextoverlay{-2}]
\defineoverlay[\v!tekst-1][\internaltextoverlay{-1}]
\defineoverlay[\v!tekst+1][\internaltextoverlay{+1}]
\defineoverlay[\v!tekst+2][\internaltextoverlay{+2}]

% to be documented

\definelayer[anchor]

\def\anchor
  {\dosingleargument\doanchor}

\def\doanchor[#1]%
  {\ifundefined{\??an#1}\@EA\dodoanchor\else\@EA\nonoanchor\fi[#1]}

\def\nonoanchor[#1]%
  {\getvalue{\??an#1}}

\def\dodoanchor[#1]%
  {\dotripleempty\dododoanchor[#1]}

\def\dododoanchor
  {\ifthirdargument
     \expandafter\dodoanchorT
   \else
     \expandafter\dodoanchorS
   \fi}

\def\dodoanchorS[#1][#2][#3]%
  {\dodoanchorT[#1][#2][#2]}

\def\dodoanchorT[#1][#2][#3]%
  {\dowithnextbox
     {\bgroup
      \checktextbackgrounds
      \setbox\scratchbox\null
      \wd\scratchbox\nextboxwd
      \ht\scratchbox\nextboxht
      \dp\scratchbox\nextboxdp
      \setlayer
        [anchor]
        [\c!breedte=\wd\scratchbox,
         \c!hoogte=\ht\scratchbox,
         \c!offset=\!!zeropoint,
         #2,#3]
        {\setlayer[#1]{\flushnextbox}}%
      \framed
        [#2,
         \c!achtergrond=anchor,
         \c!offset=\v!overlay,
         \c!kader=\v!uit,
         #3]
        {\box\scratchbox}%
     \egroup}%
     \vbox}

\def\defineanchor
  {\doquadrupleempty\dodefineanchor}

\def\dodefineanchor[#1][#2][#3][#4]%
  {\setvalue{\??an#1}{\dodefinedanchor[#2][#3][#4]}}

\def\dodefinedanchor[#1][#2][#3]%
  {\def\docommando[##1][##2]%
     {\ifsecondargument
        \def\next{\dodoanchorT[#1][#2,##1][#3,##2]}%
      \else\iffirstargument
        \def\next{\dodoanchorT[#1][#2,##1][#2,##1]}%
      \else
        \def\next{\dodoanchorT[#1][#2][#3]}%
      \fi\fi
      \next}%
   \dodoubleempty\docommando}

\def\@@collectorbox{@@collectorbox}

\def\definecollector
  {\dodoubleargument\dodefinecollector}

\def\dodefinecollector[#1][#2]%
  {\ifundefined{\@@collectorbox#1}%
     \expandafter\newbox\csname\@@collectorbox#1\endcsname
   \fi
   \resetcollector[#1]%
   \setupcollector
     [#1]
     [\c!status=\v!start,
      \c!x=\!!zeropoint,\c!y=\!!zeropoint,
      \c!offset=\!!zeropoint,\c!rotatie=, % geen 0 !
      \c!hoffset=\!!zeropoint,\c!voffset=\!!zeropoint,
      \c!plaats=rb,\c!hoek=,#2]}

\def\setupcollector
  {\dodoubleargument\dosetupcollector}

\def\dosetupcollector[#1][#2]%
  {\def\docommando##1{\getparameters[\??cb##1][#2]}%
   \processcommalist[#1]\docommando}

\def\setcollector
  {\dodoubleargument\dosetcollector}

\def\dosetcollector[#1][#2]%
  {\bgroup
   \forgetall
   \dontcomplain
   \dowithnextbox
     {\ifundefined{\@@collectorbox#1}%
        \writestatus{collector}{unknown layer #1}%
      \else
        \dodosetcollector[#1][#2]%
      \fi
      \egroup}
     \hbox}

\def\collectorparameter#1{\csname\??cb\currentcollector#1\endcsname}

\def\dodosetcollector[#1][#2]% todo: keep reference point
  {\def\currentcollector{#1}%
   \chardef\collectorbox\csname\@@collectorbox#1\endcsname
   \getparameters[\??cb#1][#2]%
   \@@layerxsiz\wd\collectorbox
   \@@layerysiz\ht\collectorbox
   \doifvaluesomething{\??cb#1\c!rotatie}
     {\setbox\nextbox\hbox
        {\rotate
           [\c!plaats=\v!hoog,
            \c!rotatie=\collectorparameter\c!rotatie]
           {\flushnextbox}}}%
   \advance\@@layerysiz\dp\collectorbox
   \@@layerxpos\collectorparameter\c!x
   \advance\@@layerxpos\collectorparameter\c!hoffset
   \@@layerypos\collectorparameter\c!y
   \advance\@@layerypos\collectorparameter\c!voffset
   \doifelse\v!midden{\collectorparameter\c!hoek}
     {\ifdim\@@layerxsiz>\zeropoint
        \advance\@@layerxpos.5\@@layerxsiz
      \fi
      \ifdim\@@layerysiz>\zeropoint
        \advance\@@layerypos.5\@@layerysiz
      \fi}%
     {\ExpandBothAfter\doifinset\v!onder{\collectorparameter\c!hoek}
        {\ifdim\@@layerysiz>\zeropoint
           \advance\@@layerypos-\@@layerysiz
           \@@layerypos-\@@layerypos
         \fi}%
      \ExpandBothAfter\doifinset\v!rechts{\collectorparameter\c!hoek}
        {\ifdim\@@layerxsiz>\zeropoint
           \advance\@@layerxpos-\@@layerxsiz
           \@@layerxpos-\@@layerxpos
         \fi}}%
   \setbox\nextbox\hbox
     {\alignedbox[\collectorparameter\c!plaats]\vbox{\flushnextbox}}%
   \boxmaxdepth\zeropoint % really needed, nice example
   \global\advance\boxhdisplacement\@@layerxpos
   \ifdim\boxhdisplacement<\zeropoint
     \global\setbox\collectorbox\hbox
       {\hskip-\boxhdisplacement
        \box\collectorbox}%
   \fi
   \global\advance\boxvdisplacement\@@layerypos
   \ifdim\boxvdisplacement<\zeropoint
     \global\setbox\collectorbox\hbox
       {\lower-\boxvdisplacement
        \box\collectorbox}%
   \fi
   \@@layerxsiz\wd\collectorbox
   \@@layerysiz\ht\collectorbox
   \advance\@@layerysiz\dp\collectorbox
   \global\setbox\collectorbox\hbox
     {\box\collectorbox
      \hskip-\@@layerxsiz
      \hskip\@@layerxpos\relax
      \ifdim\boxhdisplacement<\zeropoint
        \hskip-\boxhdisplacement
      \fi
      \lower\@@layerypos\hbox
        {\ifdim\boxvdisplacement<\zeropoint
           \lower-\boxvdisplacement\flushnextbox
         \else
           \flushnextbox
         \fi}}%
   % combine height and depth into depth only (later flushed as height)
   \global\setbox\collectorbox\hbox
     {\lower\ht\collectorbox\box\collectorbox}%
   % just to be sure
   \ifdim\wd\collectorbox<\@@layerxsiz
     \global\wd\collectorbox\@@layerxsiz
   \fi}

\def\flushcollector[#1]%
  {\ifundefined{\@@collectorbox#1}%
     \writestatus{collector}{unknown collector #1}%
   \else
     \doifnotvalue{\??cb#1\c!status}\v!stop
       {\vbox
          {\hbox
             {\doifelsevalue{\??cb#1\c!status}\v!herhaal
                {\let\next\copy}{\let\next\box}%
              \raise\dp\csname\@@collectorbox#1\endcsname
              \next\csname\@@collectorbox#1\endcsname}}}%
   \fi}

\def\composedcollector#1{\flushcollector[#1]}

\def\resetcollector[#1]%
  {\ifundefined{\@@collectorbox#1}\else
     \global\setbox\csname\@@collectorbox#1\endcsname\emptybox
   \fi}

\def\adaptcollector
  {\dodoubleargument\doadaptcollector}

\def\doadaptcollector[#1][#2]%
  {\bgroup
   \def\currentcollector{#1}%
   \chardef\collectorbox\csname\@@collectorbox#1\endcsname
   \getparameters
     [\??cb#1][\c!voffset=\zeropoint,\c!hoffset=\zeropoint,#2]%
   \scratchdimen\wd\collectorbox
   \advance\scratchdimen\collectorparameter\c!hoffset
   \global\wd\collectorbox\scratchdimen
   \scratchdimen\ht\collectorbox
   \advance\scratchdimen\collectorparameter\c!voffset
   \global\ht\collectorbox\scratchdimen
   \egroup}


%\definecollector[test]
%\setcollector[test]
%  [location=rb]
%  {\externalfigure[koe][frame=on,width=3cm]}
%\setcollector[test]
%  [corner={right,bottom},location={left,top}]
%  {\framed{gans}}
%\composedcollector{test}

\definecollector
  [caption]

\def\collectedtext
  {\dodoubleempty\docollectedtext}

\def\docollectedtext[#1][#2]#3%
  {\bgroup
   \dowithnextbox
     {\setcollector
        [caption]
        {\flushnextbox}%
      \setcollector
        [caption][#1]
        {\getparameters[\??du][#2]%
         \dosetfontattribute\??du\c!letter\setupinterlinespace
         \framed % watch the special setting of kader/overlay
           [\c!kader=\v!overlay,#2]
           {\doattributes\??du\c!letter\c!kleur{#3}}}%
      \composedcollector{caption}%
      \egroup}%
     \hbox}

% \collectedtext
%   [corner={right,bottom},location={left,top}]
%   [background=color,backgroundcolor=white,offset=0pt]
%   {gans}
%   {\externalfigure[koe][width=3cm]}
%
% \collectedtext
%   [rotation=90,corner={right,bottom},location={right,top}]
%   [frame=on,offset=0pt]
%   {gans}
%   {\externalfigure[koe][width=3cm]}
%
% \collectedtext
%   [rotation=90,corner={left,bottom},location={left,top}]
%   [frame=on,offset=0pt]
%   {gans}
%   {\externalfigure[koe][width=3cm]}

\definelayer
  [caption]

\def\layeredtext
  {\dodoubleempty\dolayeredtext}

\def\dolayeredtext[#1][#2]#3%
  {\bgroup
   \dowithnextbox
     {\!!widtha \nextboxwd
      \!!heighta\nextboxht
      \bgroup % preserve \nextbox
      \setuplayer
        [caption]
        [\c!breedte=\!!widtha,\c!hoogte=\!!heighta]%
      \setlayer
        [caption]
        [#1]
        {\getparameters[\??du][#2]%
         \dosetfontattribute\??du\c!letter\setupinterlinespace
         \framed
           [\c!kader=\v!overlay,,#2]
           {\doattributes\??du\c!letter\c!kleur{#3}}}%
      \egroup
      \framed
        [\c!offset=\v!overlay,
         \c!kader=\v!uit,
         \c!achtergrond={\v!voorgrond,caption},
         \c!breedte=\!!widtha,
         \c!hoogte=\!!heighta]
        {\flushnextbox}%
      \egroup}%
     \hbox}

% \layeredtext
%   [corner={right,bottom},location={left,top}]
%   [background=color,backgroundcolor=white,offset=0pt]
%   {gans}
%   {\externalfigure[koe][width=3cm]}
%
% \layeredtext
%   [rotation=90,corner={right,bottom},location={right,top}]
%   [frame=on,offset=0pt]
%   {gans}
%   {\externalfigure[koe][width=3cm]}
%
% \layeredtext
%   [rotation=90,corner={left,bottom},location={left,top}]
%   [frame=on,offset=0pt]
%   {gans}
%   {\externalfigure[koe][width=3cm]}

\def\ornamenttext
  {\dodoubleempty\doornamenttext}

\def\doornamenttext[#1][#2]%
  {\bgroup
   \doifassignmentelse{#1}
     {\getparameters[\s!dummy][\c!variant=\v!a,#1]%
      \doifelse\dummyvariant\v!a
        {\egroup\collectedtext}%
        {\egroup\layeredtext  }%
      [#1][#2]}%
     {\egroup\getvalue{#1}}}

\def\defineornament
  {\dotripleempty\dodefineornament}

\def\dodefineornament[#1][#2][#3]%
  {\setvalue{#1}{\doornamenttext[#2][#3]}}

% \defineornament
%   [affiliation]
%   [rotation=90,corner={right,bottom},location={right,top},
%    hoffset=-.25ex]
%   [frame=on,background=color,backgroundcolor=red,offset=0pt]
%
% \ruledhbox{\affiliation{gans}{\externalfigure[koe][width=3cm]}}
%
% \defineornament
%   [affiliation]
%   [rotation=90,corner={right,bottom},location={right,top},
%    hoffset=-.25ex,alternative=b]
%   [frame=on,background=color,backgroundcolor=red,offset=0pt]
%
% \ruledhbox{\affiliation{gans}{\externalfigure[koe][width=3cm]}}
%
% \defineornament
%   [affiliation]
%   [rotation=90,corner={right,bottom},location={left,top},
%    hoffset=.25ex,voffset=.25ex,alternative=a]
%   [background=color,style=\ss\tfxx,backgroundcolor=white,offset=0pt]
%
% \affiliation{photo}{\externalfigure[molen][width=3cm]}
%
% \defineornament
%   [affiliation]
%   [rotation=90,corner={right,bottom},location={left,top},
%    hoffset=.25ex,voffset=.25ex,alternative=b]
%   [background=color,style=\ss\tfxx,backgroundcolor=white,offset=0pt]
%
% \affiliation{drawing}{\externalfigure[hakker][width=3cm]}

% pas op: aanpassen aan nieuwe layer hoek ankers en columnset

\newcounter\nofbleeds % per pag

\def\setupbleeding
  {\dodoubleempty\getparameters[\??bg]}

\setupbleeding
  [\c!plaats=l,
   \c!rek=\v!ja,
   \c!breedte=3cm,
   \c!hoogte=3cm,
   \c!offset=2mm,
   \c!pagina=\v!nee,
   \c!voffset=\@@bgoffset,
   \c!hoffset=\@@bgoffset]

\def\bleed
  {\dosingleempty\dobleed}


\def\bleedwidth {\the\hsize}%
\def\bleedheight{\the\vsize}%

\def\dobleed[#1]#2%
  {\hbox\bgroup
   \xdef\bleedwidth {\the\hsize}%
   \xdef\bleedheight{\the\vsize}%
   \doglobal\increment\nofbleeds
   \getparameters[\??bg][#1]%
   \!!doneafalse % left
   \!!donebfalse % right
   \!!donecfalse % top
   \!!donedfalse % bottom
   % replace this part ! todo: default location
   \processaction
     [\@@bgplaats]
     [ t=>\!!donectrue\let\@@bghoffset\!!zeropoint,
       b=>\!!donedtrue\let\@@bghoffset\!!zeropoint,
       l=>\!!doneatrue\let\@@bgvoffset\!!zeropoint,
       r=>\!!donebtrue\let\@@bgvoffset\!!zeropoint,
      bl=>\!!doneatrue\!!donedtrue,
      lb=>\!!doneatrue\!!donedtrue,
      br=>\!!donebtrue\!!donedtrue,
      rb=>\!!donebtrue\!!donedtrue,
      tl=>\!!doneatrue\!!donectrue,
      lt=>\!!doneatrue\!!donectrue,
      tr=>\!!donebtrue\!!donectrue,
      rt=>\!!donebtrue\!!donectrue]%
   \doifelse\@@bgrek\v!ja\donetrue\donefalse
   \scratchdimen\@@bgbreedte
   \ifdone
     \if!!donea
       \advance\scratchdimen\MPx{\??bg:\nofbleeds}%
     \else\if!!doneb
       \scratchdimen\papierbreedte
       \advance\scratchdimen-\MPx{\??bg:\nofbleeds}%
     \fi\fi
   \fi
   \advance\scratchdimen\@@bghoffset
   \xdef\bleedwidth{\the\scratchdimen}%
   \scratchdimen\@@bghoogte
   \ifdone
     \if!!donec
       \scratchdimen\papierhoogte
       \advance\scratchdimen-\MPy{\??bg:\nofbleeds}%
     \else\if!!doned
       \advance\scratchdimen\MPy{\??bg:\nofbleeds}%
     \fi\fi
   \fi
   \advance\scratchdimen\@@bgvoffset
   \xdef\bleedheight{\the\scratchdimen}%
   \hsize\bleedwidth
   \vsize\bleedheight
   \setbox\scratchbox\hbox{#2}%
   \doif\@@bgpagina\v!ja
     {\setbox\scratchbox\topskippedbox{\box\scratchbox}}%
   \setbox\scratchbox\hbox to \@@bgbreedte
     {\if!!donea\hss\fi\box\scratchbox\if!!doneb\hss\fi}%
   \if!!doned
     \setbox\scratchbox\hbox
       {\lower\bleedheight\hbox{\raise\@@bghoogte\box\scratchbox}}%
   \fi
   \wd\scratchbox\@@bgbreedte
   \ht\scratchbox\@@bghoogte
   \dp\scratchbox\zeropoint
   \ifdone
     \hpos{\??bg:\nofbleeds}{\box\scratchbox}%
   \else
     \box\scratchbox
   \fi
   \egroup}

\setupbleeding[\c!rek=\v!ja]

\defineexternalfigure[bleed][\c!breedte=\bleedwidth,\c!hoogte=\bleedheight]

% \placefigure[left]{none}
%   {\bleed[width=5cm,height=3cm,location=lt]{\externalfigure[koe][bleed]}}
%
% \input tufte
%
% \placefigure[left]{none}
%   {\bleed[width=5cm,height=3cm,location=l]{\externalfigure[koe][bleed]}}
%
% \input tufte
%
% \placefigure[right]{none}
%   {\bleed[width=5cm,height=3cm,location=r]{\externalfigure[koe][bleed]}}
%
% \input tufte
%
% \placesomefloat[right]{none}
%   {\bleed[width=5cm,height=3cm,location=rb]{\externalfigure[koe][bleed]}}
%
% \input tufte
%
% \placefigure
%   [top,none]
%   {} % no caption
%   {\bleed
%      [hoffset=-\backspace,
%       voffset=3mm,
%       width=0cm,
%       height=6\lineheight,
%       page=yes, % correct for topskip
%       location=lt]
%      {\externalfigure[koe][bleed][frame=on]}}

% \setlayerframed[layer id][layer settings][framed setting]{data}
% \setlayerframed[layer id][combined settings]{data}

\def\setlayerframed
  {\dotripleempty\dosetlayerframed}

\def\dosetlayerframed
  {\ifthirdargument
     \expandafter\dosetlayerframedT
   \else
     \expandafter\dosetlayerframedS
   \fi}

\def\dosetlayerframedT[#1][#2][#3]%
  {\dowithnextbox{\setlayer[#1][#2]{\flushnextbox}}%
   \hbox\framed[#3]}

\def\dosetlayerframedS[#1][#2][#3]%
  {\dowithnextbox
     {\setlayer
        [#1]
        [\c!breedte=\nextboxwd,\c!hoogte=\nextboxht,
         \c!offset=\!!zeropoint,#2]
        {\flushnextbox}}%
   \hbox\framed[\c!plaats=\v!normaal,#2]}

\def\setlayertext
  {\dotripleempty\dosetlayertext}

\def\dosetlayertext[#1][#2][#3]%
  {\bgroup
   \getparameters
     [\??lx]
     [\c!uitlijnen=,
      \c!breedte=\hsize,
      \c!kleur=,
      \c!letter=,
      #3]%
   \dowithnextboxcontent
     {\forgetall
      \hsize\@@lxbreedte
      \expanded{\setupalign[\@@lxuitlijnen]}%
      \dosetfontattribute\??lx\c!letter}
     {\setlayer[#1][#2]{\strut\color[\@@lxkleur]{\flushnextbox}}%
      \egroup}%
   \vtop}

% \setupbackgrounds
%   [page]
%   [background=pagefigures]
%
% \definelayer
%   [pagefigures]
%   [x=-2mm,
%    y=-2mm,
%    width=\paperwidth,
%    height=\paperheight]
%
% \definelayerpreset [lefttop]     [corner={left,top},location={right,bottom}]
% \definelayerpreset [righttop]    [corner={right,top},location={left,bottom}]
% \definelayerpreset [leftbottom]  [corner={left,bottom},location={right,top}]
% \definelayerpreset [rightbottom] [corner={right,bottom},location={left,top}]
% \definelayerpreset [middle]      [corner=middle,location=middle]
%
% \setlayer[pagefigures][preset=lefttop]
% \setlayer[pagefigures][preset=righttop]
% \setlayer[pagefigures][preset=leftbottom]
% \setlayer[pagefigures][preset=rightbottom]

\definelayerpreset
  [\v!links\v!boven]
  [\c!hoek={\v!links,\v!boven},\c!plaats={\v!rechts,\v!onder}]

\definelayerpreset
  [\v!rechts\v!boven]
  [\c!hoek={\v!rechts,\v!boven},\c!plaats={\v!links,\v!onder}]

\definelayerpreset
  [\v!links\v!onder]
  [\c!hoek={\v!links,\v!onder},\c!plaats={\v!rechts,\v!boven}]

\definelayerpreset
  [\v!rechts\v!onder]
  [\c!hoek={\v!rechts,\v!onder},\c!plaats={\v!links,\v!boven}]

\definelayerpreset
  [\v!midden]
  [\c!hoek=\v!midden,\c!plaats=\v!midden]

\definelayerpreset
  [\v!midden\v!boven]
  [\c!plaats=\v!onder,\c!hoffset=.5\layerwidth]

\definelayerpreset
  [\v!midden\v!onder]
  [\c!plaats=\v!boven,\c!hoffset=.5\layerwidth,\c!voffset=\layerheight]

\definelayerpreset
  [\v!midden\v!links]
  [\c!plaats=\v!rechts,\c!voffset=.5\layerwidth]

\definelayerpreset
  [\v!midden\v!rechts]
  [\c!plaats=\v!links,\c!hoffset=\layerwidth,\c!voffset=.5\layerheight]
  
\def\alignedbox
  {\dodoubleempty\doalignedbox[]}

% \def\doalignedbox[#1][#2]%
%   {\bgroup
%   %\let\iftraceboxplacement\iftracelayers % ugly
%    \dowithnextbox
%      {\let\next\middlebox
%       \processaction
%         [#2]
%         [ t=>\let\next\topbox       , b=>\let\next\bottombox     ,
%           l=>\let\next\leftbox      , r=>\let\next\rightbox      ,
%          bl=>\let\next\bottomleftbox,br=>\let\next\bottomrightbox,
%          tl=>\let\next\topleftbox   ,tr=>\let\next\toprightbox   ,
%          lt=>\let\next\lefttopbox   ,lb=>\let\next\leftbottombox ,
%          rt=>\let\next\righttopbox  ,rb=>\let\next\rightbottombox]%
%       \next{\flushnextbox}%
%       \egroup}#1}

\def\doalignedbox[#1][#2]%
  {\bgroup
  %\let\iftraceboxplacement\iftracelayers % ugly
   \dowithnextbox
     {\serializecommalist[#2]%
      \executeifdefined{\??ab\??ab\serializedcommalist}\middlebox{\flushnextbox}%
      \egroup}#1}

\setvalue{\??ab\??ab         }{\middlebox}
\setvalue{\??ab\??ab\v!midden}{\middlebox}
\setvalue{\??ab\??ab\v!links }{\leftbox  }
\setvalue{\??ab\??ab\v!rechts}{\rightbox }
\setvalue{\??ab\??ab\v!onder }{\bottombox}
\setvalue{\??ab\??ab\v!boven }{\topbox   }

\setvalue{\??ab\??ab\v!midden\v!midden}{\middlebox}
\setvalue{\??ab\??ab\v!links \v!boven }{\lefttopbox}
\setvalue{\??ab\??ab\v!links \v!onder }{\leftbottombox}
\setvalue{\??ab\??ab\v!rechts\v!boven }{\righttopbox}
\setvalue{\??ab\??ab\v!rechts\v!onder }{\rightbottombox}
\setvalue{\??ab\??ab\v!boven \v!links }{\topleftbox}
\setvalue{\??ab\??ab\v!onder \v!links }{\bottomleftbox}
\setvalue{\??ab\??ab\v!boven \v!rechts}{\toprightbox}
\setvalue{\??ab\??ab\v!onder \v!rechts}{\bottomrightbox}

\setvalue{\??ab\??ab  c}{\middlebox}
\setvalue{\??ab\??ab  l}{\leftbox}
\setvalue{\??ab\??ab  r}{\rightbox}
\setvalue{\??ab\??ab  o}{\bottombox}
\setvalue{\??ab\??ab  b}{\topbox}

\setvalue{\??ab\??ab lt}{\lefttopbox}
\setvalue{\??ab\??ab lb}{\leftbottombox}
\setvalue{\??ab\??ab rt}{\righttopbox}
\setvalue{\??ab\??ab rb}{\rightbottombox}
\setvalue{\??ab\??ab tl}{\topleftbox}
\setvalue{\??ab\??ab bl}{\bottomleftbox}
\setvalue{\??ab\??ab tr}{\toprightbox}
\setvalue{\??ab\??ab br}{\bottomrightbox}

\def\offsetbox
  {\dodoubleempty\dooffsetbox[]}

% left/right/top/bottomoffset -> dimensions change
% x/y                         -> dimensions don't change

\def\dooffsetbox[#1][#2]%
  {\bgroup
   \dowithnextbox
     {\getparameters[\??ox]
        [\c!x=\zeropoint,
         \c!y=\zeropoint,
         \c!breedte=\nextboxwd,
         \c!hoogte=\nextboxht,
         \c!diepte=\nextboxdp,
         \c!plaats=,
         \c!linkeroffset=\zeropoint,
         \c!rechteroffset=\zeropoint,
         \c!bovenoffset=\zeropoint,
         \c!onderoffset=\zeropoint,
         #2]%
      \donefalse
      \ifdim\@@oxlinkeroffset >\zeropoint\donetrue\fi
      \ifdim\@@oxrechteroffset>\zeropoint\donetrue\fi
      \ifdim\@@oxbovenoffset  >\zeropoint\donetrue\fi
      \ifdim\@@oxonderoffset  >\zeropoint\donetrue\fi
      \ifdone
        \setbox\nextbox\vbox
          {\forgetall\offinterlineskip
           \vskip\@@oxbovenoffset
           \hbox
             {\hskip\@@oxlinkeroffset
              \flushnextbox
              \hskip\@@oxrechteroffset}%
           \vskip\@@oxonderoffset}%
        \scratchdimen\nextboxht
        \advance\scratchdimen\nextboxdp
        \nextboxht\scratchdimen
        \nextboxdp\zeropoint
      \fi
      \freezedimenmacro\@@oxbreedte
      \freezedimenmacro\@@oxhoogte
      \freezedimenmacro\@@oxdiepte
      \setbox\nextbox\hbox
        {\hskip\@@oxx\lower\@@oxy\hbox
           {\doifelsenothing\@@oxplaats
              {\flushnextbox}
              {\alignedbox[\@@oxplaats]\hbox{\flushnextbox}}}}%
      \nextboxwd\@@oxbreedte
      \nextboxht\@@oxhoogte
      \nextboxdp\@@oxdiepte
      \flushnextbox
      \egroup}#1}

% \useMPlibrary[pre] \setupbackgrounds[page][background=pagegrid]
%
% \placefigure[left,none]{}{\offset[leftoffset=1cm]{\externalfigure[koe][breedte=3cm]}}
% \input tufte
% \placefigure[left,none]{}{\offset[rightoffset=1cm]{\externalfigure[koe][breedte=3cm]}}
% \input tufte
% \placefigure[left,none]{}{\offset[topoffset=1cm]{\externalfigure[koe][breedte=3cm]}}
% \input tufte
% \placefigure[left,none]{}{\offset[bottomoffset=1cm]{\externalfigure[koe][breedte=3cm]}}
% \input tufte

\def\offset {\dodoubleempty\dooffsetbox [\hbox]} % yes or no
\def\aligned{\dosingleempty\doalignedbox[\hbox]} % yes or no

%\ruledhbox{\offsetbox[x=-1cm,y=-1cm,location=c]
%  {\framed[width=4cm,height=4cm]{x}}}

\def\dotabbed#1#2#3#4%
  {\dontleavehmode
   \bgroup
   \setbox\scratchbox\hbox{#3}%
   \hbox to \wd\scratchbox{#1#4#2}%
   \egroup}

\def\ltabbed{\dotabbed\relax\hss}
\def\rtabbed{\dotabbed\hss  \relax}
\def\ctabbed{\dotabbed\hss  \hss}      \let\mtabbed\ctabbed

% \ltabbed{\romeins{3}}{\romeins{1}} test \endgraf
% \ltabbed{\romeins{3}}{\romeins{2}} test \endgraf
% \ltabbed{\romeins{3}}{\romeins{3}} test \endgraf
%
% \rtabbed{\romeins{3}}{\romeins{1}} test \endgraf
% \rtabbed{\romeins{3}}{\romeins{2}} test \endgraf
% \rtabbed{\romeins{3}}{\romeins{3}} test \endgraf
%
% \ctabbed{\romeins{3}}{\romeins{1}} test \endgraf
% \ctabbed{\romeins{3}}{\romeins{2}} test \endgraf
% \ctabbed{\romeins{3}}{\romeins{3}} test \endgraf

% alternative, if done, then other name
%
% \def\dotabbed#1#2#3#4%
%   {\dontleavehmode
%    \bgroup
%    \scratchdimen\zeropoint
%    \def\docommando##1%
%      {\setbox\scratchbox\hbox{##1}%
%       \ifdim\wd\scratchbox>\scratchdimen
%         \scratchdimen\wd\scratchbox
%       \fi}%
%    \processcommalist[#3]\docommando
%    \hbox to \scratchdimen{#1#4#2}%
%    \egroup}
%
% \def\ltabbed{\dotabbed\relax\hss}
% \def\rtabbed{\dotabbed\hss  \relax}
% \def\ctabbed{\dotabbed\hss  \hss}      \let\mtabbed\ctabbed
%
% \ltabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{10}}   test \endgraf
% \ltabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{15}}   test \endgraf
% \ltabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{2000}} test \endgraf
%
% \rtabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{10}}   test \endgraf
% \rtabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{15}}   test \endgraf
% \rtabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{2000}} test \endgraf
%
% \ctabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{10}}   test \endgraf
% \ctabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{15}}   test \endgraf
% \ctabbed{\romeins{10},\romeins{2000},\romeins{15}}{\romeins{2000}} test \endgraf

% to be documented

\def\phantombox[#1]%
  {\hbox\bgroup
   \getparameters
     [\??ol]
     [\c!breedte=\zeropoint,%
      \c!hoogte=\zeropoint,%
      \c!diepte=\zeropoint,#1]%
   \setbox\scratchbox\null
   \wd\scratchbox\@@olbreedte
   \ht\scratchbox\@@olhoogte
   \dp\scratchbox\@@oldiepte
   \box\scratchbox
   \egroup}

\protect \endinput
