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