%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\dodosetcollector[#1][#2]% todo: keep reference point 
  {\getparameters[\??cb#1][#2]%
   \@@layerxsiz\wd\csname\@@collectorbox#1\endcsname
   \@@layerysiz\ht\csname\@@collectorbox#1\endcsname
   \doifvaluesomething{\??cb#1\c!rotatie}
     {\setbox\nextbox\hbox
        {\rotate
           [\c!plaats=\v!hoog,
            \c!rotatie=\getvalue{\??cb#1\c!rotatie}]
           {\flushnextbox}}}%
   \advance\@@layerysiz\dp\csname\@@collectorbox#1\endcsname
   \@@layerxpos\getvalue{\??cb#1\c!x}%
   \advance\@@layerxpos\getvalue{\??cb#1\c!hoffset}%
   \@@layerypos\getvalue{\??cb#1\c!y}%
   \advance\@@layerypos\getvalue{\??cb#1\c!voffset}%
   \ExpandBothAfter\doifinset{\v!onder}{\getvalue{\??cb#1\c!hoek}}
     {\ifdim\@@layerysiz>\zeropoint
        \advance\@@layerypos-\@@layerysiz
        \@@layerypos-\@@layerypos
      \fi}%
   \ExpandBothAfter\doifinset{\v!rechts}{\getvalue{\??cb#1\c!hoek}}
     {\ifdim\@@layerxsiz>\zeropoint
        \advance\@@layerxpos-\@@layerxsiz
        \@@layerxpos-\@@layerxpos
      \fi}%
   \setbox\nextbox\hbox
     {\alignedbox[\getvalue{\??cb#1\c!plaats}]\vbox{\flushnextbox}}%
   \boxmaxdepth\zeropoint % really needed, nice example 
   \global\advance\boxhdisplacement\@@layerxpos
   \ifdim\boxhdisplacement<\zeropoint
     \global\setbox\csname\@@collectorbox#1\endcsname\hbox
       {\hskip-\boxhdisplacement
        \box\csname\@@collectorbox#1\endcsname}%
   \fi
   \global\advance\boxvdisplacement\@@layerypos
   \ifdim\boxvdisplacement<\zeropoint
     \global\setbox\csname\@@collectorbox#1\endcsname\hbox
       {\lower-\boxvdisplacement
        \box\csname\@@collectorbox#1\endcsname}%
   \fi
   \@@layerxsiz\wd\csname\@@collectorbox#1\endcsname
   \@@layerysiz\ht\csname\@@collectorbox#1\endcsname
   \advance\@@layerysiz\dp\csname\@@collectorbox#1\endcsname
   \global\setbox\csname\@@collectorbox#1\endcsname\hbox
     {\box\csname\@@collectorbox#1\endcsname
      \hskip-\@@layerxsiz
      \hskip\@@layerxpos\relax
      \ifdim\boxhdisplacement<\zeropoint
        \hskip-\boxhdisplacement
      \fi
      \lower\@@layerypos\hbox
        {\ifdim\boxvdisplacement<\zeropoint
           \lower-\boxvdisplacement\flushnextbox
         \else
           \flushnextbox
         \fi}}%
   \ifdim\wd\csname\@@collectorbox#1\endcsname<\@@layerxsiz
     \global\wd\csname\@@collectorbox#1\endcsname\@@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}

%\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\stelinterliniein
         \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
      \setlayer
        [caption]
        [\c!breedte=\!!widtha,\c!hoogte=\!!heighta,#1]
        {\getparameters[\??du][#2]%
         \dosetfontattribute\??du\c!letter\stelinterliniein
         \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,#2]%
      \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!voffset=\@@bgoffset,
   \c!hoffset=\@@bgoffset]

\def\bleed
  {\dosingleempty\dobleed}

\def\dobleed[#1]#2%
  {\hbox\bgroup  
   \def\bleedwidth {\hsize}%
   \def\bleedheight{\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
   \edef\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
   \edef\bleedheight{\the\scratchdimen}%
   \hsize\bleedwidth
   \vsize\bleedheight
   \setbox\scratchbox\hbox{#2}%
   \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]

% \placefloat[left]{none}
%   {\bleed[width=5cm,height=3cm,location=lt]{\externalfigure[koe][bleed]}}
% 
% \input tufte 
% 
% \placefloat[left]{none}
%   {\bleed[width=5cm,height=3cm,location=l]{\externalfigure[koe][bleed]}}
% 
% \input tufte 
% 
% \placesomefloat[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 

% \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{\steluitlijnenin[\@@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]     [location=rb]
% \definelayerpreset [righttop]    [corner={right,top},location={left,bottom}]
% \definelayerpreset [leftbottom]  [corner={left,bottom},location={right,top}]
% \definelayerpreset [rightbottom] [corner={right,bottom},location={left,top}]
% 
% \setlayer[pagefigures][preset=lefttop]
% \setlayer[pagefigures][preset=righttop]
% \setlayer[pagefigures][preset=leftbottom]
% \setlayer[pagefigures][preset=rightbottom]

\protect \endinput 
