anch-tab.mkxl /size: 12 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=anch-pgr, % moved from anch-pgr (1999.08.01)
3%D        version=2011.12.19, % 1999.08.01,
4%D          title=\CONTEXT\ Anchoring Macros,
5%D       subtitle=Table Extensions,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Anchoring Macros / Table Extensions}
15
16%D This is just a playground and functionality might change or even dissappear in
17%D favour of better solutions. Anyway, it now has been aroudn too long to bother.
18
19\unprotect
20
21\newinteger\c_anch_tabs
22
23\protected\def\tabl_tabulate_hook_b
24  {\iftrialtypesetting\else
25     \ifpositioning
26       \ifconditional\tablehaspositions
27         \ifcase\c_tabl_tabulate_noflines % \ifnum\c_tabl_tabulate_noflines=\c_tabl_tabulate_totalnoflines
28           \tabl_tabulate_hook_b_first
29         \else
30           \tabl_tabulate_hook_b_next
31         \fi
32        \fi
33     \fi
34   \fi}
35
36\protected\def\tabl_tabulate_hook_e
37  {\iftrialtypesetting\else
38     \ifpositioning
39       \ifconditional\tablehaspositions
40         \ifcase\c_tabl_tabulate_noflines % \ifnum\c_tabl_tabulate_noflines=\c_tabl_tabulate_totalnoflines
41           \tabl_tabulate_hook_e_first
42         \else
43           \tabl_tabulate_hook_e_next
44         \fi
45       \fi
46     \fi
47   \fi}
48
49\protected\def\tabl_tabulate_hook_b_first{\clf_bposcolumnregistered{tabulate:\the\c_anch_tabs:\the\c_tabl_tabulate_column}}
50\protected\def\tabl_tabulate_hook_b_next {\clf_bposcolumn{tabulate:\the\c_anch_tabs:\the\c_tabl_tabulate_column}}
51\protected\def\tabl_tabulate_hook_e_first{\clf_eposcolumnregistered}
52\protected\def\tabl_tabulate_hook_e_next {\clf_eposcolumn}
53
54% \appendtoks \registerparoptions \to \everypar
55
56\appendtoks
57    \global\advanceby\c_anch_tabs\plusone
58\to \everytabulate
59
60% for text backgrounds
61
62\appendtoks
63    \c_anch_backgrounds_pos_no_shape\conditionaltrue
64\to \everytabulate
65
66%D Beware, the following code is somewhat weird and experimental and might be
67%D dropped or become a loadable module.
68
69%D \macros
70%D   {GFC, GTC, GSC}
71%D
72%D The next macros extend tables and tabulation with backgrounds and position
73%D related features. Areas are specified with symbolic names, and symbolic
74%D references to the graphics involved. Each table has its own namespace.
75
76\newconditional\tablehaspositions
77\newinteger    \noftabpositions
78\newtoks       \posXCtoks
79
80\protected\def\anch_tabulate_bpos{\bpos}
81\protected\def\anch_tabulate_epos{\epos}
82
83\installcorenamespace{positiontables}
84
85\permanent\def\tbPOSprefix{\??positiontables\number\noftabpositions:}
86
87\permanent\protected\def\tablepos
88  {\normalexpanded{\global\posXCtoks\emptytoks\the\posXCtoks}}
89
90\permanent\protected\def\tbXC {\anch_table_checked\anch_tables_indeed_XC }
91\permanent\protected\def\tbGSC{\anch_table_checked\anch_tables_indeed_GSC}
92\permanent\protected\def\tbGFC{\anch_table_checked\anch_tables_indeed_GFC}
93\permanent\protected\def\tbGTC{\anch_table_checked\anch_tables_indeed_GTC}
94
95\tolerant\def\anch_table_checked#1[#2]%
96  {\iftrialtypesetting
97     \global\tablehaspositions\conditionaltrue
98     \expandafter\anch_tables_indeed_NC
99   \orelse\ifparameter#2\or
100     \expandafter#1%
101   \else
102     \expandafter\anch_tables_indeed_NC
103   \fi[#2]}
104
105\def\anch_tables_indeed_NC[#1]%
106  {\NC}
107
108\def\anch_tables_indeed_XC[#1]%
109  {{\enforced\let\NC\relax\processcommalist[#1]\anch_tables_step_XC}}
110
111\def\anch_tables_step_XC#1%
112  {\anch_tables_step_indeed_XC[#1]}
113
114\def\anch_tables_step_indeed_XC[#1#2]%
115  {\if#1>\anch_tables_indeed_GFC[#2:#2]\orelse
116   \if#1+\anch_tables_indeed_GFC[#2:#2]\orelse
117   \if#1<\anch_tables_indeed_GTC[#2:#2]\orelse
118   \if#1-\anch_tables_indeed_GTC[#2:#2]\orelse
119   \if#1=\anch_tables_indeed_GSC[#2:#2]\orelse
120         \anch_tables_indeed_GSC[#1#2:#1#2]\fi}
121
122\def\anch_tables_indeed_GSC[#1]%
123  {\processcommalist[#1]\anch_tables_step_GSC}
124
125\def\anch_tables_step_GSC#1%
126  {\anch_tables_append_GSC[#1:#1]}
127
128\def\anch_tables_append_GSC[#1:#2:#3]%
129% {\doglobal\appendtoks\anch_tables_process_GSC[#1:#2]\to\posXCtoks\NC}
130  {\gtoksapp\posXCtoks{\anch_tables_process_GSC[#1:#2]}\NC}
131
132\def\anch_tables_process_GSC[#1:#2]%
133  {\remappositionframed{#2}{\tbPOSprefix#1}%
134   \anch_tabulate_bpos{\tbPOSprefix#1}%
135%  \doglobal\appendtoks\expandafter\anch_tabulate_epos\expandafter{\tbPOSprefix#1}\to\posXCtoks}
136   \xtoksapp\posXCtoks{\anch_tabulate_epos{\tbPOSprefix#1}}}
137
138\def\anch_tables_indeed_GFC[#1]%
139% {\doglobal\appendtoks\anch_tables_delayed_GFC[#1]\to\posXCtoks\NC}
140  {\gtoksapp\posXCtoks{\anch_tables_delayed_GFC[#1]}\NC}
141
142\def\anch_tables_delayed_GFC[#1]%
143  {\processcommalist[#1]\anch_tables_step_GFC}
144
145\def\anch_tables_step_GFC#1%
146  {\anch_tables_process_GFC[#1:#1]}
147
148\def\anch_tables_process_GFC[#1:#2:#3]%
149  {\remappositionframed{#2}{\tbPOSprefix#1}%
150   \anch_tabulate_bpos{\tbPOSprefix#1}}
151
152\def\anch_tables_indeed_GTC[#1]%
153% {\doglobal\appendtoks\anch_tables_delayed_GTC[#1]\to\posXCtoks\NC}
154  {\gtoksapp\posXCtoks{\anch_tables_delayed_GTC[#1]}\NC}
155
156\def\anch_tables_delayed_GTC[#1]%
157% {\doglobal\appendtoks\anch_tables_process_GTC[#1]\to\posXCtoks}
158  {\gtoksapp\posXCtoks{\anch_tables_process_GTC[#1]}\NC}
159
160\def\anch_tables_process_GTC[#1]%
161  {\processcommalist[#1]\anch_tables_step_GTC}
162
163\def\anch_tables_step_GTC#1%
164  {\anch_tables_step_indeed_GTC[#1:#1]}
165
166\def\anch_tables_step_indeed_GTC[#1:#2]%
167  {\anch_tabulate_epos{\tbPOSprefix#1}}
168
169%D The amount of code to support tables and tabulation is rather minimalistic.
170
171\aliased\let\tabulatepos\tablepos
172
173\permanent\protected\def\tabulatenormalpos
174  {\iftrialtypesetting
175     % nothing
176   \orelse\ifconditional\tablehaspositions
177     \hss\tabulatepos\hss
178   \else
179     % nothing
180   \fi}
181
182\permanent\protected\def\tabulateequalpos
183  {\iftrialtypesetting
184     \tabulateEQ
185   \orelse\ifconditional\tablehaspositions
186     \tabulateEQpos
187   \else
188     \tabulateEQ
189   \fi}
190
191\permanent\protected\def\tabulateEQpos
192  {\setbox\scratchbox\hbox{\tabulateEQ}%
193   \hbox to \wd\scratchbox{\hss\kern\zeropoint\tabulatepos\hss}% hpack
194   \kern-\wd\scratchbox
195   \box\scratchbox}
196
197\appendtoks
198    \global\advanceby\noftabpositions\plusone
199    \global\tablehaspositions\conditionalfalse
200\to \everytabulate
201
202% We need to handle paragraphs as well.
203
204\let\anch_tabulate_flush_epos\relax
205
206\protected\def\anch_tabulate_bpos_indeed
207  {\bpos}
208
209\protected\def\anch_tabulate_epos_indeed#1%
210  {\ifvoid\b_tabl_tabulate_current\c_tabl_tabulate_column
211     \epos{#1}%
212     \glet\anch_tabulate_flush_epos\relax
213   \else
214     \gdef\anch_tabulate_flush_epos{\epos{#1}}%
215   \fi}
216
217\permanent\def\flushtabulatesplitbox
218  {\box\b_tabl_tabulate
219   \iftrialtypesetting\orelse\ifconditional\tablehaspositions\anch_tabulate_flush_epos\fi}
220
221\appendtoks
222    \let\anch_tabulate_bpos\anch_tabulate_bpos_indeed % ?
223    \let\anch_tabulate_epos\anch_tabulate_epos_indeed % ?
224    \glet\anch_tabulate_flush_epos\relax
225\to \everytabulate
226
227%D In order to prevent potential clashes with abbreviations, postpone the mapping and
228%D make them mutable.
229
230\mutable\lettonothing\GSC
231\mutable\lettonothing\GFC
232\mutable\lettonothing\GTC
233
234\appendtoks
235    \enforced\let\GSC\tbGSC
236    \enforced\let\GFC\tbGFC
237    \enforced\let\GTC\tbGTC
238    \enforced\let\XC \tbXC
239\to \everytabulate
240
241%D \macros
242%D   {definepositionframed}
243%D
244%D The next example show how to provide backgrounds to table cells. First we define
245%D some framed backgrounds.
246%D
247%D \startbuffer
248%D \definepositionframed[x][background=color,backgroundcolor=red]
249%D \definepositionframed[y][background=color,backgroundcolor=green]
250%D \definepositionframed[z][background=color,backgroundcolor=blue]
251%D \stopbuffer
252%D
253%D \typebuffer
254%D
255%D % \getbuffer
256%D
257%D \startbuffer
258%D \starttabulate[|c|c|c|]
259%D \GFC[f:x] this is a small  \NC       table            \NC     in which we \NC \FR
260%D \NC       will demonstrate \GFC[g:z] that this        \GTC[g] positioning \NC \MR
261%D \GSC[e:y] mechanism also   \GTC[f]   works quite well \NC     in tables   \NC \LR
262%D \stoptabulate
263%D \stopbuffer
264%D
265%D The table itself defines three areas (a, b and~c) using these frames.
266%D
267%D \typebuffer
268%D % \getbuffer
269%D
270%D Tables (based on \TABLE) are supported by:
271
272\permanent\def\normalTABLEsimplebar {\unskip\!ttRightGlue&\tablepos&}                       % |
273\permanent\def\normalTABLEcomplexbar{\unskip\!ttRightGlue&\omit\tablepos\!ttAlternateVrule} % \|
274\permanent\def\normalTABLEquote     {\unskip\!ttRightGlue&\omit\tablepos&}                  % "
275
276\appendtoks
277    \global\advanceby\noftabpositions\plusone
278    \global\tablehaspositions\conditionalfalse
279\to \everytable
280
281%D Since we don't want nameclashes:
282
283\appendtoks
284    \enforced\let\GSC\tbGSC
285    \enforced\let\GFC\tbGFC
286    \enforced\let\GTC\tbGTC
287    \enforced\let\XC \tbXC
288\to \everytable
289
290%D In the previous example, we could have provided an overlay to the framed definition.
291%D A more direct approach is demonstrated below:
292%D
293%D \startbuffer
294%D \def\cw#1{\color[white]{#1}}
295%D
296%D \startMPpositiongraphic{tableshade}
297%D   initialize_area(\MPpos{\MPvar{from}},\MPpos{\MPvar{to}}) ;
298%D   color c ; c := \MPvar{color} ;
299%D   linear_shade(pxy,0,.4c,.9c) ;
300%D   anchor_area(\MPanchor{\MPvar{from}}) ;
301%D \stopMPpositiongraphic
302%D
303%D \setMPpositiongraphic{b:x}{tableshade}{from=b:x,to=e:x,color=red}
304%D \setMPpositiongraphic{b:y}{tableshade}{from=b:y,to=e:y,color=green}
305%D \setMPpositiongraphic{b:z}{tableshade}{from=b:z,to=e:z,color=blue}
306%D \stopbuffer
307%D
308%D \typebuffer \getbuffer
309%D
310%D The definition of the table looks about the same as the previous one:
311%D
312%D \startbuffer
313%D \starttable[|c|c|c|]
314%D \GFC[b:z] \cw{this is a small}  \NC       \cw{table}            \NC     in which we      \NC \FR
315%D \NC       \cw{will demonstrate} \GFC[c:y] \cw{that this}        \GTC[c] \cw{positioning} \NC \MR
316%D \GSC[a:x] \cw{mechanism also}   \GTC[b]   \cw{works quite well} \NC     in tables        \NC \LR
317%D \stoptable
318%D \stopbuffer
319%D
320%D \typebuffer
321%D
322%D \getbuffer
323
324% \definepositionframed[w][background=color,backgroundcolor=yellow]
325% \definepositionframed[x][background=color,backgroundcolor=red]
326% \definepositionframed[y][background=color,backgroundcolor=green]
327% \definepositionframed[z][background=color,backgroundcolor=blue]
328%
329% \starttabulate[|c|c|c|]
330% \NC this is a small  \NC table            \NC in which we \NC \FR
331% \NC will demonstrate \NC that this        \NC positioning \NC \MR
332% \NC mechanism also   \NC works quite well \NC in tables   \NC \LR
333% \stoptabulate
334%
335% \starttabulate[|c|c|c|]
336% \GFC[f:x] this is a small      \GTC      table            \NC     in which we \NC \FR
337% \NC       will demonstrate     \GFC[g:z] that this        \GTC[g] positioning \NC \MR
338% \GSC[e:y] mechanism also       \GTC[f]   works quite well \NC     in tables   \NC \LR
339% \stoptabulate
340%
341% \starttabulate[|c|c|c|]
342% \GFC[f:x,d:w] this is a small  \GTC[d]   table            \NC     in which we \NC \FR
343% \NC           will demonstrate \GFC[g:z] that this        \GTC[g] positioning \NC \MR
344% \GSC[e:y]     mechanism also   \GTC[f]   works quite well \NC     in tables   \NC \LR
345% \stoptabulate
346%
347% \starttabulate[|c|c|c|]
348% \XC[+f:x] this is a small  \XC      table            \NC     in which we \NC \FR
349% \NC      will demonstrate  \XC[+g:z] that this       \XC[-g] positioning \NC \MR
350% \XC[=e:y] mechanism also   \XC[-f]  works quite well \NC     in tables   \NC \LR
351% \stoptabulate
352%
353% \starttabulate[|c|c|c|]
354% \XC[+f:x,+d:w] this is a small  \XC[-d]   table            \NC     in which we \NC \FR
355% \NC            will demonstrate \XC[+g:z] that this        \XC[-g] positioning \NC \MR
356% \XC[=e:y]       mechanism also  \XC[-f]   works quite well \NC     in tables   \NC \LR
357% \stoptabulate
358
359% evt                  [b:x]
360%
361% \definepositionframed[x][background=color,fillcolor=red]
362% \definepositionframed[y][background=color,fillcolor=green]
363% \definepositionframed[z][background=color,fillcolor=blue]
364
365\permanent\protected\def\remappositionframed#1#2% from to
366  {\copyposition{b:#1}{b:#2}%
367   \copyposition{e:#1}{e:#2}%
368   \anch_positions_set_action{b:#2}{\dopositionaction{b:#1}}}
369
370\permanent\tolerant\protected\def\definepositionframed[#1]#*[#S#2]%
371  {\anch_positions_set_action{b:#1}{\anch_framed_handle[#1][#2]}}
372
373\protected\def\anch_framed_handle
374  {\bgroup
375   \positionframed}
376
377\permanent\tolerant\protected\def\positionframed[#1]#*[#S#2]%
378  {\setbox\scratchbox\hpack
379     {\scratchwidth \dimexpr\MPx{e:#1}-\MPx{b:#1}\relax
380      \scratchdepth \dimexpr\MPy{b:#1}-\MPy{e:#1}+\MPd{e:#1}\relax
381      \scratchheight\dimexpr\scratchdepth+\MPh{b:#1}\relax
382      \lower\scratchdepth\hpack
383        {\framed[\c!width=\scratchwidth,\c!height=\scratchheight,\c!offset=\v!overlay,#2]{}}}%
384   \smashedbox\scratchbox
385   \egroup}
386
387\protect \endinput
388