summaryrefslogtreecommitdiff
path: root/tex/context/base/mkii/tabl-tsp.mkii
blob: 59e9185e81dd2da62f9db352d957fa1746c5e477 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
%D \module
%D   [       file=tabl-tsp,
%D        version=2000.10.20,
%D          title=\CONTEXT\ Table Macros,
%D       subtitle=Splitting,
%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 Table Macros / Splitting}

%D The code in this file is move here from other places.

\unprotect

% only to be used with single tokens (will be prim)

\ifx\htdp\undefined \def\htdp#1{\dimexpr\ht#1+\dp#1\relax} \fi

%D Although the name resembles floats, and therefore this should be
%D a page module, we decided to make it core functionality because the
%D table code depends on it. Othrwise there would be too much
%D overloading afterwards involved. Actually, the float part is rather
%D generic and not that related to floats.

% \splitfloat [settings] {\placetable[optional args]{test}} {content}

\definenumber
  [\??si]
  [\c!way=\v!by\v!text,
   \c!conversion=\@@siconversion]

\def\setupfloatsplitting
  {\dodoubleargument\getparameters[\??si]}

\newif\ifinsidesplitfloat % will become chardef

\newtoks \everysplitfloatsetup

\def\splitfloat
  {\dosingleempty\dosplitfloat}

\ifx\floatcaptionsuffix\undefined \else
  \let\floatcaptionsuffix\empty % will become \splitfloatcaptionsuffix
\fi

\def\extrasplitfloatlines{0}

\def\dosplitfloat[#1]#2% nog dubbele refs
  {\bgroup
   \global\setfalse\splitfloatdone
   \aftergroup\checksplitfloat
   \insidefloattrue
   \insidesplitfloattrue
   \getparameters[\??si][#1]%
   \resetnumber[\??si]%
   \def\floatcaptionsuffix{\convertednumber[\??si]}%
   \let\extrasplitfloatlines\@@silines
   \the\everysplitfloatsetup
   \def\splitfloatcommand{#2}%
   \global\settrue \onlyonesplitofffloat
   \global\setfalse\somenextplitofffloat
   \dopushsavedfloats
   \@@sibefore
   \let\next} % \bgroup

\def\checksplitfloat
  {\ifconditional\splitfloatdone\else
      \blank{\tttf \getmessage\m!floatblocks{13}\empty}\blank
      \showmessage\m!floatblocks{13}\empty
   \fi}

\settrue \onlyonesplitofffloat
\setfalse\somenextplitofffloat

%D When \type {inbetween} is made empty instead of the
%D default \type {\page}, we will get delayed flushing
%D and text may continue below the graphic.
%D
%D \starttyping
%D \dorecurse{2}{\input tufte }
%D
%D \splitfloat[lines=auto,inbetween=]
%D   {\placetable{\dorecurse{5}{test\recurselevel\endgraf}}}
%D   {\bTABLE[split=yes]
%D    \bTR \bTD 11 \eTD \bTD \input tufte \eTD \eTR
%D    \bTR \bTD 12 \eTD \bTD \input zapf \eTD \eTR
%D    \bTR \bTD 13 \eTD \bTD \input bryson \eTD \eTR
%D    \bTR \bTD 14 \eTD \bTD test  \eTD \eTR
%D    \bTR \bTD 21 \eTD \bTD \input tufte \eTD \eTR
%D    \bTR \bTD 22 \eTD \bTD \input zapf \eTD \eTR
%D    \bTR \bTD 23 \eTD \bTD \input bryson \eTD \eTR
%D    \bTR \bTD 24 \eTD \bTD test  \eTD \eTR
%D    \bTR \bTD 31 \eTD \bTD \input tufte \eTD \eTR
%D    \bTR \bTD 32 \eTD \bTD \input zapf \eTD \eTR
%D    \bTR \bTD 33 \eTD \bTD \input bryson \eTD \eTR
%D    \bTR \bTD 34 \eTD \bTD test  \eTD \eTR
%D    \eTABLE}
%D
%D \dorecurse{10}{\input tufte }
%D \stoptyping

\newconditional\splitfloatdone

\def\dodowithsplitofffloat
  {\dowithnextbox
     {\forgetall
      \dontcomplain
      \global\settrue\splitfloatdone
      \chardef\nodelocationmode\zerocount % bypass auto-renumbering
      \incrementnumber[\??si]%
      \ifcase\rawnumber[\??si]\or \ifconditional\onlyonesplitofffloat
        \let\floatcaptionsuffix\empty
      \fi \fi
      \bgroup
      \ifconditional\somenextplitofffloat
        \settrue\retainfloatnumber
\notesenabledfalse % best here, experimental, brrr; test with note in caption
      \else
        \setfalse\retainfloatnumber
      \fi
      \splitfloatcommand{\box\nextbox}%
      \egroup
      \ifconditional\somenextplitofffloat
        \doifelsenothing\@@siinbetween
          {\ifconditional\splitfloatfirstdone\else\page\fi}
          \@@siinbetween
      \else
        \@@siafter
        \dopopsavedfloats
        \doflushsavedfloats
      \fi
      \global\settrue\splitfloatfirstdone}%
   \vbox}

\def\nodowithsplitofffloat
  {\dowithnextbox
     {\forgetall
      \dontcomplain
      \box\nextbox % maybe an option to unvbox
      \global\settrue\splitfloatfirstdone}%
   \vbox}

\def\dochecksplitofffloat#1% box
  {\ifinsidesplitfloat
     \ifdim\ht#1=\zeropoint
       \global\setfalse\somenextplitofffloat
     \else
       \global\settrue \somenextplitofffloat
       \global\setfalse\onlyonesplitofffloat
     \fi
   \fi}

\def\analyzesplitfloatcaption#1% depends on page-flt
  {\doif\extrasplitfloatlines\v!auto
     {\bgroup
      \settrue\retainfloatnumber
      \chardef\nodelocationmode\zerocount
      \forcelocalfloats
      \setuplocalfloats[\c!before=,\c!after=,\c!inbetween=]%
      \splitfloatcommand{\hbox to \wd#1{\strut}}% dummy line
      \setbox\scratchbox\vbox{\flushlocalfloats}%
      \getnoflines{\ht\scratchbox}%
      \resetlocalfloats
      \advance\noflines\minusone % compensate dummy line
      \expanded{\egroup\noexpand\edef\noexpand\extrasplitfloatlines{\the\noflines}}}}

% \def\analyzesplitfloatcaption#1%
%   {\edef\extrasplitfloatlines{11}}

\def\dowithsplitofffloat % nextbox
  {\ifinsidesplitfloat
     \expandafter\dodowithsplitofffloat
   \else
     \expandafter\nodowithsplitofffloat
   \fi}

\def\doifnotinsidesplitfloat
  {\ifinsidesplitfloat\expandafter\gobbleoneargument\fi}

%D Some defaults:

\setupfloatsplitting
  [\c!conversion=\v!character, % \v!romannumerals
   \c!lines=3,
   \c!before=,
   \c!inbetween=\page,
   \c!after=]

%D Table splitter, on top of previous code:

\newbox\tsplitcontent
\newbox\tsplitresult
\newbox\tsplithead
\newbox\tsplitnext
\newbox\tsplittail

\def\resettsplit{% only \def's starting a a new line are seen by the dep checker
   \def\tsplitminimumfreelines{0}%
   \def\tsplitminimumfreespace{0pt}%
   \setbox\tsplitcontent  \vbox{}%
   \setbox\tsplitresult   \vbox{}%
   \setbox\tsplithead     \vbox{}%
   \setbox\tsplitnext     \vbox{}%
   \setbox\tsplittail     \vbox{}%
   \let\tsplitbeforeresult\donothing
   \let\tsplitafterresult \donothing
   \let\tsplitinbetween   \donothing
   \let\tsplitbefore      \donothing
   \let\tsplitafter       \donothing
   \let\postprocesstsplit \donothing
}

\resettsplit

% todo: keep tail to rest, so we need a lookahead

\newconditional\splitfloatfirstdone

\def\handletsplit
  {\analyzesplitfloatcaption\tsplitcontent
   \global\setfalse\splitfloatfirstdone
   \testpagesync % new, sync, but still tricky
     [\tsplitminimumfreelines]
     [\dimexpr\tsplitminimumfreespace+\extrasplitfloatlines\lineheight\relax]%
   \setbox\scratchbox\vbox{\tsplitinbetween}%
   \edef\tsplitinbetweenheight{\the\htdp\scratchbox}% etex
   \!!doneafalse
   \doloop
     {\ifinsidecolumns
        % brrr, assumes empty columns
        \global\setfalse\splitfloatfirstdone
        \scratchdimen\textheight
        \!!donectrue
      \else
        \ifconditional\splitfloatfirstdone
          \scratchdimen\textheight
          \!!donectrue
        \else\ifdim\pagegoal<\maxdimen
          \scratchdimen\dimexpr\pagegoal-\pagetotal\relax
          \!!donecfalse
        \else
          \scratchdimen\textheight
          \!!donectrue
        \fi\fi
      \fi
      \scratchdimen\dimexpr\scratchdimen-\tsplitinbetweenheight-\tsplitminimumfreespace-\extrasplitfloatlines\lineheight\relax
      \ifdim\htdp\tsplittail>\zeropoint
        \advance\scratchdimen-\htdp\tsplittail
      \fi
      \setbox\tsplitresult\vbox
        {\ifdim\ht\tsplithead>\zeropoint
           \unvcopy\tsplithead
           \tsplitinbetween
         \fi}%
      \if!!donea\else\ifdim\ht\tsplitnext>\zeropoint
        \setbox\tsplithead\box\tsplitnext
      \fi\fi
      \!!doneatrue
      \ifdim\ht\tsplitresult>\zeropoint
        \!!donedtrue  % table head
      \else
        \!!donedfalse % no tablehead
      \fi
      \splittopskip\zeropoint
      \doloop
        {\setbox\scratchbox\vsplit\tsplitcontent to \onepoint % \lineheight
         \setbox\scratchbox\vbox{\unvbox\scratchbox}%
         \ifdim\dimexpr\scratchdimen-\htdp\scratchbox-\htdp\tsplitresult\relax>\zeropoint
           \setbox\tsplitresult\vbox
             {\unvbox\tsplitresult
              \tsplitinbetween
              \unvbox\scratchbox}%
           \ifvoid\tsplitcontent \exitloop \fi
         \else\if!!doned
           % we only have a tablehead so far
           \setbox\tsplitresult\vbox{\unvbox\tsplitresult\unvbox\scratchbox}%
           \exitloop
         \else\if!!donec
           % we have text height available, but the (one) cell is too
           % large to fit, so, in order to avoid loops/deadcycles we do:
           \setbox\tsplitresult\vbox
             {\unvbox\tsplitresult
              \tsplitinbetween
              \unvbox\scratchbox}%
           \exitloop
         \else
           \setbox\tsplitcontent\vbox
             {\unvbox\scratchbox
              \tsplitinbetween
              \ifvoid\tsplitcontent\else\unvbox\tsplitcontent\fi}%
           \exitloop
         \fi\fi\fi
         \!!donedfalse
         \!!donecfalse}%
      \postprocesstsplit
      \dochecksplitofffloat\tsplitcontent
      \ifvoid\tsplitcontent
        \setbox\tsplitresult\vbox
          {\unvbox\tsplitresult
           \tsplitinbetween
           \unvcopy\tsplittail}%
        \dowithsplitofffloat{\tsplitbeforeresult\box\tsplitresult\tsplitafterresult}%
        \doifnotinsidesplitfloat\tsplitafter
        \endgraf
        \exitloop
      \else
        % hack
        \ifdim\pagegoal<\maxdimen
          \global\pagegoal\dimexpr\pagegoal+\lineheight\relax % etex
        \fi
        % brrr
        \ifdim\ht\tsplitresult>\zeropoint
          \setbox\tsplitresult\vbox
            {\unvbox\tsplitresult
             \tsplitinbetween
             \unvcopy\tsplittail}%
          \dowithsplitofffloat{\tsplitbeforeresult\box\tsplitresult\tsplitafterresult}%
          \doifnotinsidesplitfloat\tsplitafter
          \endgraf
        \fi
        \ifinsidecolumns
          \doifnotinsidesplitfloat\goodbreak
        \else
          \doifnotinsidesplitfloat\page
        \fi
      \fi}%
   \global\setfalse\splitfloatfirstdone} % we can use this one for tests

\protect \endinput

% test cases

% \setupTABLE[split=repeat]
%
% \input tufte \endgraf
% \splitfloat[lines=11]
%   {\placetable{\dorecurse{10}{test\recurselevel\endgraf}}}
%   {\bTABLE\dorecurse{100}{\bTR \bTD test \eTD \eTR}\eTABLE}
% \input tufte \page
%
% \input tufte \endgraf
% \splitfloat[lines=0]
%   {}
%   {\bTABLE\dorecurse{100}{\bTR \bTD test \eTD \eTR}\eTABLE}
% \input tufte \endgraf \page
%
% \input tufte \endgraf
% \bTABLE\dorecurse{100}{\bTR \bTD test \eTD \eTR}\eTABLE
% \input tufte \page

% \setuptabulate[split=yes]
%
% \input tufte \endgraf
% \splitfloat[lines=11]
%   {\placetable{\dorecurse{10}{test\recurselevel\endgraf}}}
%   {\starttabulate\dorecurse{200}{\NC test \NC test \NC \NR}\stoptabulate}
% \input tufte \page
%
% \input tufte \endgraf
% \splitfloat[lines=0]
%   {}
%   {\starttabulate\dorecurse{200}{\NC test \NC test \NC \NR}\stoptabulate}
% \input tufte \page
%
% \input tufte \endgraf
% \starttabulate\dorecurse{200}{\NC test \NC test \NC \NR}\stoptabulate
% \input tufte \page

% \setuptables[split=yes]
%
% \newtoks\TestToks
%
% \TestToks\emptytoks
% \appendtoks\starttablehead\to\TestToks
% \dorecurse{3}{\appendtoks\VL head \VL head \VL \SR\to\TestToks}
% \appendtoks\stoptablehead\to\TestToks
% \appendtoks\starttabletail\to\TestToks
% \dorecurse{3}{\appendtoks\VL tail \VL tail \VL \SR\to\TestToks}
% \appendtoks\stoptabletail\to\TestToks
% \appendtoks\starttables[|c|c|]\to\TestToks
% \dorecurse{100}{\appendtoks\VL test \VL test \VL \SR\to\TestToks}
% \appendtoks\stoptables\to\TestToks
%
% \input tufte \endgraf
% \splitfloat[lines=auto] % [lines=11]
%   {\placetable{\dorecurse{10}{test\recurselevel\endgraf}}}
%   {\the\TestToks}
% \input tufte \page
%
% \input tufte \endgraf
% \splitfloat[lines=0]
%   {}
%   {\the\TestToks}
% \input tufte \page
%
% \input tufte \endgraf
% \the\TestToks
% \input tufte \page
%
% multiple floats
%
% \starttext
%   \dorecurse{3}{\input tufte } \endgraf
%   \dorecurse{5}{\placefigure{}{\framed[height=.5\textheight]{}}}
%   \splitfloat[lines=auto,inbetween=]
%     {\placetable{\dorecurse{5}{test\recurselevel\endgraf}}}
%     {\bTABLE[split=yes]
%      \bTR \bTD 11 \eTD \bTD \input tufte \eTD \eTR
%      \bTR \bTD 12 \eTD \bTD \input zapf \eTD \eTR
%      \bTR \bTD 13 \eTD \bTD \input bryson \eTD \eTR
%      \bTR \bTD 14 \eTD \bTD test  \eTD \eTR
%      \bTR \bTD 21 \eTD \bTD \input tufte \eTD \eTR
%      \bTR \bTD 22 \eTD \bTD \input zapf \eTD \eTR
%      \bTR \bTD 23 \eTD \bTD \input bryson \eTD \eTR
%      \bTR \bTD 24 \eTD \bTD test  \eTD \eTR
%      \bTR \bTD 31 \eTD \bTD \input tufte \eTD \eTR
%      \bTR \bTD 32 \eTD \bTD \input zapf \eTD \eTR
%      \bTR \bTD 33 \eTD \bTD \input bryson \eTD \eTR
%      \bTR \bTD 34 \eTD \bTD test  \eTD \eTR
%      \eTABLE}
%   \dorecurse{10}{\input tufte }
% \stoptext