%D \module
%D   [       file=anch-box,
%D        version=2022.03.25,
%D          title=\CONTEXT\ Anchoring Macros,
%D       subtitle=Boxes,
%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 Anchoring Macros / Boxes}

\unprotect

\startMPpositiongraphic{mypos:boxanchor:arrow}{}% from,to,linecolor,text,alternative,leftedge,rightedge,distance}
    anch_box_arrows_draw ;
\stopMPpositiongraphic

\def\anch_box_connect_parameters#1#2%
  {span=no,
   from=\boxanchorposone,%
   to=\boxanchorpostwo,%
   distance={\namedboxanchorcontentparameter{#1}\c!distance},%
   dashtype={\namedboxanchorcontentparameter{#1}\c!dash},%
   arrow={\namedboxanchorcontentparameter{#1}\c!arrow},%
   alternative={\namedboxanchorcontentparameter{#1}\c!alternative},%
   text={\namedboxanchorcontentparameter{#1}\c!text},%
   rulethickness={\namedboxanchorcontentparameter{#1}\c!rulethickness},%
   linecolor=\namedboxanchorcontentparameter{#1}{\c!rulecolor},
   #2}

\mutable\lettonothing\boxanchorone
\mutable\lettonothing\boxanchortwo
\mutable\lettonothing\boxanchorposone
\mutable\lettonothing\boxanchorpostwo

\tolerant\permanent\protected\def\connectboxanchors[#1]#*[#2]#*[#S#3]#*[#S#4]#*#:#5#6%
  {\begingroup
   %
   \edef\boxanchorone{\boxanchoringclass:#5}%
   \edef\boxanchortwo{\boxanchoringclass:#6}%
   %
   \doifnotanchorbox{\boxanchorone}{\defineboxanchor[\boxanchorone]}%
   \doifnotanchorbox{\boxanchortwo}{\defineboxanchor[\boxanchortwo]}%
   %
   \edef\boxanchorposone{#1:#2:\namespacedboxanchor{\boxanchorone}}%
   \edef\boxanchorpostwo{#1:#2:\namespacedboxanchor{\boxanchortwo}}%
   %
   \setboxanchor[\boxanchorone][#1][#3]\hpack{\xypos{\boxanchorposone}}%
   \setboxanchor[\boxanchortwo][#2][#4]\hpack{\xypos{\boxanchorpostwo}}%
   %
   \ifparameter#3\or\setupboxanchorcontent[#1][#3]\fi
   \ifparameter#4\or\setupboxanchorcontent[#2][#4]\fi % used ?
   %
   \expanded{%
     \startpositionoverlay{text+1}% will become configurable region
       \setMPpositiongraphic
         {\boxanchorposone}%
         {\namedboxanchorcontentparameter{#1}\c!mp}%
          {\anch_box_connect_parameters{#1}{#3}}%
    \stoppositionoverlay
   }%
   \ifcstok{\namedboxanchorcontentparameter{#1}\c!page}\v!yes
     \expanded{%
       \startpositionoverlay{text+1}% will become configurable region
           \setMPpositiongraphic
             {\boxanchorpostwo}%
             {\namedboxanchorcontentparameter{#1}\c!mp}%
             {\anch_box_connect_parameters{#1}{#3}}%
       \stoppositionoverlay
     }%
   \fi
   \endgroup}

% dedicated or not

\tolerant\permanent\protected\def\connectmatrixanchors[#1]#*[#2]#*[#S#3]#*[#S#4]#*#:#5#6%
  {\begingroup
   \doifelseinset{#5}{\m_anch_matrix_list}\donetrue\donefalse
   \ifdone\else
     \defineboxanchor[#5]%
     \setboxanchor[#5][#1][#3]\hpack{\xypos{\namespacedboxanchor{#5}}}%
     \xdef\m_anch_matrix_list{\m_anch_matrix_list,#5}%
   \fi
   \ifparameter#3\or\setupboxanchorcontent[#1][#3]\fi
   \doifelseinset{#6}{\m_anch_matrix_list}\donetrue\donefalse
   \ifdone\else
     \defineboxanchor[#6]%
     \setboxanchor[#6][#2][#4]\hpack{\xypos{\namespacedboxanchor{#6}}}%
     \xdef\m_anch_matrix_list{\m_anch_matrix_list,#6}%
   \fi
   \ifparameter#4\or\setupboxanchorcontent[#1][#4]\fi
   \startpositionoverlay{text-1}% will become configurable region
     \setMPpositiongraphic
       {\namespacedboxanchor{#5}}%
       {\namedboxanchorcontentparameter{#1}\c!mp}%
       {from=\namespacedboxanchor{#5},%
        to=\namespacedboxanchor{#6},%
        distance={\namedboxanchorcontentparameter{#1}\c!distance},%
        dashtype={\namedboxanchorcontentparameter{#1}\c!dash},%
        arrow={\namedboxanchorcontentparameter{#1}\c!arrow},%
        alternative={\namedboxanchorcontentparameter{#1}\c!alternative},%
        text={\namedboxanchorcontentparameter{#1}\c!text},%
        rulethickness={\namedboxanchorcontentparameter{#1}\c!rulethickness},%
        linecolor=\namedboxanchorcontentparameter{#1}{\c!rulecolor},%
        % matrix specific:
        leftedge=\namespacedboxanchor\c!leftedge,%
        rightedge=\namespacedboxanchor\c!rightedge}%
   \stoppositionoverlay
   \endgroup}

\lettonothing\m_anch_matrix_list

\tolerant\permanent\protected\def\startmatrixanchors
  {\glet\m_anch_matrix_list\empty
   \defineboxanchor[\v!leftedge]%
   \setboxanchor[\v!leftedge][\v!leftedge]\hpack{\xypos{\namespacedboxanchor\v!leftedge}}%
   \defineboxanchor[\v!rightedge]%
   \setboxanchor[\v!rightedge][\v!rightedge]\hpack{\xypos{\namespacedboxanchor\v!rightedge}}%
   \setupmathfence[\c!leftsource=\v!leftedge,\c!rightsource=\v!rightedge]}

\tolerant\permanent\protected\def\stopmatrixanchors
  {\setupmathfence[\c!leftsource=,\c!rightsource=]%
   \glet\m_anch_matrix_list\empty}

\defineboxanchorcontent
  [arrow]
  [\c!mp=mypos:boxanchor:arrow,
   \c!distance=1.5\exheight,
   \c!rulethickness=\linewidth,
   \c!rulecolor=textcolor]

\defineboxanchorcontent
  [\v!top]
  [arrow]
  [\c!corner=\v!height,
   \c!location=\v!depth,
   \c!yoffset=.25\exheight,
   \c!alternative=\v!top]

\defineboxanchorcontent
  [\v!bottom]
  [arrow]
  [\c!corner=\v!depth,
   \c!location=\v!height,
   \c!yoffset=-\namedboxanchorcontentparameter{top}\c!yoffset,
   \c!alternative=\v!bottom]

\defineboxanchorcontent
  [\v!left]
  [arrow]
  [\c!corner={\v!middle,\v!left},
   \c!xoffset=-1.25\emwidth,
   \c!alternative=\v!left]

\defineboxanchorcontent
  [\v!right]
  [arrow]
  [\c!corner={\v!middle,\v!right},
   \c!xoffset=-\namedboxanchorcontentparameter{left}\c!xoffset,
   \c!alternative=\v!right]

\defineboxanchorcontent
  [\v!leftedge]
  [\v!left]
  [\c!xoffset=-.25\exheight]

\defineboxanchorcontent
  [\v!rightedge]
  [\v!right]
  [\c!xoffset=0.25\exheight] % we are centered

\defineboxanchorcontent[\v!middle:\v!bottom][\v!bottom][\c!alternative=\v!middle,\c!yoffset=-.5\exheight]
\defineboxanchorcontent[\v!middle:\v!top   ][\v!top   ][\c!alternative=\v!middle,\c!yoffset=.5\exheight]
\defineboxanchorcontent[\v!middle:\v!left  ][\v!left  ][\c!alternative=\v!middle,\c!xoffset=-.5\exheight]
\defineboxanchorcontent[\v!middle:\v!right ][\v!right ][\c!alternative=\v!middle,\c!xoffset=.5\exheight]

\defineboxanchorcontent
  [\v!left:\v!bottom]
  [\v!bottom]
  [\c!alternative=\v!middle,
   \c!corner={\v!left,\v!depth},
   \c!xoffset=-.25\exheight,
   \c!yoffset=-.25\exheight]

\defineboxanchorcontent
  [\v!right:\v!bottom]
  [\v!bottom]
  [\c!alternative=\v!middle,
   \c!corner={\v!right,\v!depth},
   \c!xoffset=.25\exheight,
   \c!yoffset=-.25\exheight]

\defineboxanchorcontent
  [\v!left:\v!top]
  [\v!top]
  [\c!alternative=\v!middle,
   \c!corner={\v!left,\v!height},
   \c!xoffset=-.25\exheight,
   \c!yoffset=.25\exheight]

\defineboxanchorcontent
  [\v!right:\v!top]
  [\v!top]
  [\c!alternative=\v!middle,
   \c!corner={\v!right,\v!height},
   \c!xoffset=.25\exheight,
   \c!yoffset=.25\exheight]

% \setupboxanchorcontent  [top]   [rulecolor=darkyellow]
% \setupboxanchorcontent  [left]  [rulecolor=darkred]
% \setupboxanchorcontent  [bottom][rulecolor=darkblue]
% \setupboxanchorcontent  [right] [rulecolor=darkgreen]
%
% \startbuffer
% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 1}]{a1}{a2}
% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 2}]{b1}{b2}
% \connectboxanchors[top]   [top]   [text={\small\small\strut\bf var 3}]{a1}{b1}
% \connectboxanchors[bottom][bottom][text={\small\small\strut\bf var 4}]{b1}{b2}
% \connectboxanchors[bottom][bottom][text={\small\small\strut\bf var 6}]{a1}{a2}
% \connectboxanchors[bottom][bottom][rulecolor=darkgreen,distance=4ex,text={\small\small\strut\bf var 7}]{c1}{d1}
%
% % \start
%
% $\showboxes
% % \mathboxanchored{a1}{a}^{2^x} +
% % \mathboxanchored[nucleus]{a1}{a}^{2^x} +
% % \mathboxanchored{a1}{g}^2 +
% % \mathboxanchored{a1}{a} +
% a^{\mathboxanchored{a1}{2}} +
% % a^{\mathboxanchored{a1}{2}}_3 +
% \mathboxanchored{b1}{b}
% =
% \mathboxanchored{c1}{c}^2 +
% \mathboxanchored[nucleus]{d1}{d}^2
% =
% b_{\mathboxanchored{b2}{3}} +
% \mathboxanchored{a2}{a}
% $
% \stopbuffer
%
% \starttext
%   \startTEXpage[offset=10mm]\startboxanchoring[a]\getbuffer\stopboxanchoring\stopTEXpage
%   \startTEXpage[offset=11mm]\startboxanchoring[b]\getbuffer\stopboxanchoring\stopTEXpage
%   \startTEXpage[offset=12mm]\startboxanchoring[+]\getbuffer\stopboxanchoring\stopTEXpage
% \stoptext

\protect
