summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/anch-box.mkxl
blob: d480b221483f71438f271d55f714bb889b928ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
%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}

\tolerant\permanent\protected\def\connectboxanchors[#1]#*[#2]#*[#3]#*[#4]#*#:#5#6%
  {\begingroup
   %
   \edef\boxanchorone{\boxanchoringclass:#5}%
   \edef\boxanchortwo{\boxanchoringclass:#6}%
%    \edef\boxanchorone{#5}%
%    \edef\boxanchortwo{#6}%
   %
   \doifnotanchorbox{\boxanchorone}{\defineboxanchor[\boxanchorone]}%
   \doifnotanchorbox{\boxanchortwo}{\defineboxanchor[\boxanchortwo]}%
   %
   \edef\boxanchorposone{#1:#2:\namespacedboxanchor{\boxanchorone}}%
   \edef\boxanchorpostwo{#1:#2:\namespacedboxanchor{\boxanchortwo}}%
% \edef\boxanchorposone{#1:#2:\number\namedboxanchor{\boxanchorone}}%
% \edef\boxanchorpostwo{#1:#2:\number\namedboxanchor{\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 ?
   %
   \normalexpanded{%
     \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
     \normalexpanded{%
       \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]#*[#3]#*[#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