page-flt.mkxl /size: 13 Kb    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=page-flt,
3%D        version=2010.04.08,
4%D          title=\CONTEXT\ Page Macros,
5%D       subtitle=Float Management,
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 Page Macros / Float Management}
15
16%D This module has code that previously was in other modules. There is also float
17%D related code in \type {strc-flt.mkiv}.
18
19\registerctxluafile{page-flt}{}
20
21\unprotect
22
23\defineinsertion[\s!topfloat]
24\defineinsertion[\s!bottomfloat]
25\defineinsertion[\s!pagefloat]    % not used
26
27\newdimension\d_page_floats_inserted_bottom
28\newdimension\d_page_floats_inserted_top
29\newdimension\d_page_floats_inserted_page
30\newinteger  \c_page_floats_n_of_top           \c_page_floats_n_of_top   \plustwo
31\newinteger  \c_page_floats_n_of_bottom        \c_page_floats_n_of_bottom\zerocount
32\newinteger  \c_page_floats_n_of_page          \c_page_floats_n_of_page  \plustwo
33
34\newconstant\c_page_floats_insertions_topskip_mode  % 1 = no topskip
35
36\def\page_floats_report_total
37  {\showmessage\m!floatblocks4%
38     {\the\totalnoffloats
39      \ifempty\floatlocationmethod
40        \ifempty\floatlocation\else,\floatlocation\fi
41      \else
42        ,\floatlocationmethod
43      \fi}}
44
45\def\page_floats_report_flushed
46  {\showmessage\m!floatblocks3%
47     {\the\numexpr\totalnoffloats-\savednoffloats\relax}}
48
49%D Extra float registers.
50
51\newconditional\c_page_floats_room
52\newconditional\c_page_floats_some_waiting
53\newconditional\c_page_floats_not_permitted
54\newconditional\c_page_floats_flushing
55\newconditional\c_page_floats_center_box          \c_page_floats_center_box\conditionaltrue
56\newconditional\c_page_floats_center_box_local
57\newconditional\c_page_floats_center_box_global
58\newconditional\c_page_floats_compress_flushed    \c_page_floats_compress_flushed\conditionaltrue
59
60\newdimension  \d_page_floats_compress_distance
61
62%D For the moment we keep this but they will become private too.
63
64\newinteger  \totalnoffloats           % these will be redone ... handled at the lua end anyway
65\newinteger  \savednoffloats           % these will be redone ... handled at the lua end anyway
66\newinteger  \nofcollectedfloats       % communication channel
67\newdimension\maxcollectedfloatstotal  % communication channel
68\newinteger  \noffloatinserts          % these will be redone ... handled at the lua end anyway
69
70\newbox      \floattext
71
72\newdimension\floatwidth
73\newdimension\floatheight
74\newdimension\floatdepth
75\newdimension\floattextwidth
76\newdimension\floattextheight
77
78\newbox  \floatbox
79\newbox  \savedfloatbox
80
81%D From now on we manage the float stack at the \LUA\ end instead of packing
82%D them in a box and splitting off stacked floats. It's not so much less code
83%D but it's cleaner this way. It also opens op some posibilities as we can now
84%D more conveniently cary additional information around.
85
86\newtoks \everyfloatscheck
87
88\appendtoks
89   \ifcase\savednoffloats
90     \global\c_page_floats_some_waiting\conditionalfalse
91   \else
92     \global\c_page_floats_some_waiting\conditionaltrue
93   \fi
94\to \everyfloatscheck
95
96\protected\def\page_floats_flush#1#2%
97  {\clf_flushfloat{#1}#2\relax
98   \expand\everyfloatscheck}
99
100\protected\def\page_floats_flush_by_label#1#2%
101  {\clf_flushlabeledfloat{#1}{#2}\relax
102   \expand\everyfloatscheck}
103
104\protected\def\page_floats_save#1%
105  {\clf_savefloat{#1}\relax
106   \expand\everyfloatscheck}
107
108\protected\def\page_floats_resave#1%
109  {\clf_resavefloat{#1}\relax
110   \expand\everyfloatscheck}
111
112\protected\def\page_floats_push_saved
113  {\clf_pushfloat
114   \expand\everyfloatscheck}
115
116\protected\def\page_floats_pop_saved
117  {\clf_popfloat
118   \expand\everyfloatscheck}
119
120\protected\def\page_floats_get_info#1%
121  {\clf_consultfloat{#1}}
122
123\protected\def\page_floats_if_else#1%
124  {\clf_doifelsestackedfloats{#1}}
125
126\protected\def\page_floats_collect#1#2#3%
127  {\clf_collectfloat{#1}{#2}{#3}}
128
129\permanent\def\nofstackedfloatsincategory#1% was singular
130  {\clf_nofstackedfloats{#1}}
131
132\let\page_floats_column_push_saved\page_floats_push_saved % overloaded in page-mul
133\let\page_floats_column_pop_saved \page_floats_pop_saved  % overloaded in page-mul
134
135%D \starttyping
136%D \starttext
137%D
138%D \samplefile{tufte}
139%D \startplacefigure[location=page] \externalfigure[dummy] \stopplacefigure
140%D \samplefile{tufte}
141%D \dorecurse{6}{
142%D     \startplacefigure[location={page,append}] \externalfigure[dummy] \stopplacefigure
143%D }
144%D \startplacefigure[location=page] \externalfigure[dummy] \stopplacefigure
145%D \samplefile{tufte}
146%D
147%D \page
148%D
149%D \samplefile{tufte}
150%D \startplacefigure[location=page] \externalfigure[dummy] \stopplacefigure
151%D \samplefile{tufte}
152%D \dorecurse{5}{
153%D     \startplacefigure[location={page,append}] \externalfigure[dummy] \stopplacefigure
154%D }
155%D \startplacefigure[location=page] \externalfigure[dummy] \stopplacefigure
156%D \samplefile{tufte}
157%D
158%D \stoptext
159%D \stoptyping
160
161\protected\def\page_floats_save_page_float#1#2%
162  {\ifconditional\c_floats_append_to_stored
163     \scratchcounter\nofstackedfloatsincategory{#1}\relax
164     \ifcase\scratchcounter\else
165       \setbox\scratchboxtwo\box\floatbox
166       \clf_flushfloat{#1}\scratchcounter
167       \setbox\scratchboxone\box\floatbox
168       \setbox\scratchboxthree\vbox\bgroup
169          \blank[\rootfloatparameter\c!spaceafter ]% a bit of a hack
170          \blank[\rootfloatparameter\c!spacebefore]%
171       \egroup
172       \scratchdimen\ifconditional\c_floats_force\maxdimen\else\textheight\fi
173       \ifdim{\htdp\scratchboxone+\htdp\scratchboxtwo+\htdp\scratchboxthree}>\scratchdimen
174         \setbox\floatbox\box\scratchboxone
175         \clf_savespecificfloat{#1}{specification{#2}}\relax
176         \setbox\floatbox\box\scratchboxtwo
177       \else
178         \setbox\floatbox\vbox\bgroup
179            \box   \scratchboxone
180            \unvbox\scratchboxthree
181            \box   \scratchboxtwo
182         \egroup
183       \fi
184     \fi
185   \fi
186   \clf_savespecificfloat{#1}{specification{#2}}\relax}
187
188\protected\def\page_floats_save_somewhere_float#1#2% #1=method
189  {\clf_savespecificfloat{#1}{specification{#2}label{\floatlabel}}\relax}
190
191%D This is an experimental new feature (for Alan Braslau), a prelude to more:
192%D
193%D \starttyping
194%D test \placefigure{}{}
195%D test \placefigure[somewhere:alpha][whatever]{}{}
196%D test \placefigure[somewhere:beta] [whatever]{}{}
197%D test \placefigure[somewhere:gamma][whatever]{}{}
198%D test \placefigure[somewhere:delta][whatever]{}{}
199%D test \placefigure{}{}
200%D
201%D in \in{figure}[whatever] bla bla
202%D
203%D \placenamedfloat[figure][*l*]
204%D \placenamedfloat[figure][gamma]
205%D \placenamedfloat[figure][beta]
206%D \stoptyping
207
208\permanent\tolerant\protected\def\placenamedfloat[#1]#*[#2]%
209  {\doloop
210     {\page_floats_flush_by_label\s!somewhere{#2}%
211      \ifvoid\floatbox
212        \exitloop
213      \else
214        \cdef\currentfloat{#1}%
215        \blank[\rootfloatparameter\c!spacebefore]%
216        \box\floatbox
217        \blank[\rootfloatparameter\c!spaceafter]%
218      \fi}}
219
220%D Not the best name for a command:
221%D
222%D \starttyping
223%D \samplefile{tufte}
224%D \placefigure[somewhere:a]{}{\externalfigure[dummy-001]}
225%D \samplefile{tufte}
226%D \placefigure{}{\externalfigure[dummy-002]}
227%D \samplefile{tufte}
228%D \startplacefigure[location={somewhere:b,tight}]
229%D     \externalfigure[dummy-003]
230%D \stopplacefigure
231%D \samplefile{tufte}
232%D \placefigure{}{\externalfigure[dummy-004]}
233%D \page
234%D \placesavedfloat[figure][b][location={none,left}] \samplefile{tufte}
235%D \placesavedfloat[figure][a]
236%D % \placenamedfloat[figure][*]
237%D % \placenamedfloat[figure][b]
238%D \stoptyping
239
240\newbox\b_strc_float_saved
241
242\permanent\tolerant\protected\def\placesavedfloat[#1]#*[#2]#*[#S#3]% experiment for Alan B
243  {\clf_flushlabeledfloat\s!somewhere{#2}\relax
244  %\cdef\currentfloat{#1}%
245   \setbox\b_strc_float_saved\vpack{\box\floatbox}%
246   \startplacefloat[#1][\c!location=\v!none,#3]%
247     \box\b_strc_float_saved
248   \stopplacefloat}
249
250% \setupcaption [figure]   [align=flushleft]
251% \setupcaption [figure-1] [align=flushleft,leftmargin=10mm]
252% \setupcaption [figure-2] [align=flushleft,leftmargin=10mm,rightmargin=-10mm,width=\textwidth]
253%
254% \startsetups somefigure
255%     \ifdim\floatsetupwidth>\textwidth
256%         \placesetupfloat[figure-2]
257%     \else
258%         \placesetupfloat[figure-1]
259%     \fi
260% \stopsetups
261%
262% \placefloatwithsetups[somefigure]{}{\externalfigure[dummy][width=5cm,height=2cm]}
263
264\mutable\lettonothing\floatsetupcaption
265\mutable\lettonothing\floatsetupcontent
266\mutable\lettonothing\floatsetupwidth
267\mutable\lettonothing\floatsetupheight
268
269\aliased\let\placesetupfloat\gobbleoneoptional
270
271\permanent\tolerant\protected\def\placefloatwithsetups[#1]#*[#S#2]#*[#S#3]#:#4%
272  {\def\floatsetupcaption{#4}%
273   \def\floatsetupcontent{\copy\nextbox}%
274   \def\floatsetupwidth  {\wd\nextbox}%
275   \def\floatsetupheight {\ht\nextbox}%
276   \enforced\permanent\protected\def\placesetupfloat[##1]%
277     {\placefloat[##1][#2][#3]{#4}{\floatsetupcontent}}% #4 and not \floatsetupcaption (protected)
278   \dowithnextbox{\setups[#1]}\vbox}
279
280%D The following code is in transition as we don't want to break the current single
281%D column, multi column, and columnset mechanism.
282%D
283%D First we reimplement some helpers.
284
285\def\page_floats_get_used_hsize{\hsize}
286
287\protected\def\page_floats_get
288  {\ifconditional\c_page_floats_some_waiting
289     \page_floats_flush\s!text\plusone
290     \ifconditional\c_page_floats_center_box
291       \ifdim\wd\globalscratchbox<\page_floats_get_used_hsize
292         \global\setbox\floatbox\hpack to \page_floats_get_used_hsize{\hss\box\floatbox\hss}%
293       \orelse\ifinsidecolumns
294         % retain special alignments
295         \ifdim\wd\floatbox>\makeupwidth
296           \wd\floatbox\makeupwidth
297         \fi
298       \fi
299     \fi
300   \else
301     \global\savednoffloats\zerocount
302     \global\setbox\floatbox\emptybox
303   \fi}
304
305\protected\def\page_floats_flush_saved
306  {\doloop
307     {\ifconditional\c_page_floats_some_waiting
308        \page_otr_command_check_if_float_fits
309        \ifconditional\c_page_floats_room
310          \page_floats_get
311          \doplacefloatbox
312        \else
313          \exitloop
314        \fi
315      \else
316      % \ifconditional\c_page_margin_blocks_present % not here, here just as many floats as fit
317      %   \page_otr_command_flush_margin_blocks
318      % \else
319          \exitloop
320      % \fi
321      \fi}}
322
323%D This is a future mechamism that will be integrated once we're sure about it:
324%D
325%D \starttyping
326%D \dorecurse{10}
327%D   {\input thuan
328%D    \placefigure{}{\framed[height=1.5cm]{test}}
329%D    \placefloatplaceholder}
330%D \stoptyping
331
332\permanent\protected\def\placefloatplaceholder
333  {\ifconditional\c_page_floats_room \else
334     \ifdim{\pagegoal-\pagetotal-3\lineheight}>\zeropoint
335       \startlinecorrection[blank]
336       \mhbox{\inframed{\labeltexts{placeholder}{\m_strc_floats_last_caption_tag}}}%
337       \stoplinecorrection
338     \else
339       \allowbreak
340     \fi
341   \fi}
342
343\setuplabeltext
344  [placeholder={\Word{\lastplacedfloat}~, moved}]
345
346%D Page floats use different stacks.
347
348\newtoks\everybeforeflushedpagefloat
349
350\let\m_page_otr_checked_page_float\relax
351
352\newconditional\c_page_floats_flushed
353
354\def\page_floats_flush_page_floats_inject#1% future releases can do more clever things
355  {\page_floats_flush{#1}\plusone
356   \edef\floatspecification{\clf_getfloatvariable{specification}}% Is this okay?
357   \expand\everybeforeflushedpagefloat
358   \vpack to \textheight
359     {\ifinset\v!high\floatspecification\else\vfill\fi
360      \box\floatbox
361      \ifinset\v!low \floatspecification\else\vfill\fi}%
362   \page_otr_fill_and_eject_page % needed
363   \global\c_page_floats_flushed\conditionaltrue}
364
365\protected\def\page_floats_flush_page_floats % used in postpone
366  {\global\c_page_floats_flushed\conditionalfalse
367   \page_floats_flush_page_floats_indeed
368   \ifconditional\c_page_floats_flushed
369     \page_otr_fill_and_eject_page
370   \fi}
371
372\protected\def\page_floats_flush_page_floats_indeed % used in postpone
373  {\edef\m_page_otr_checked_page_float{\clf_checkedpagefloat}%
374   \ifempty\m_page_otr_checked_page_float
375     % nothing
376   \orelse\ifx\m_page_otr_checked_page_float\v!empty
377      \emptyhbox \page_otr_fill_and_eject_page % why not dummy_page
378      \expandafter\page_floats_flush_page_floats
379   \else
380      \page_floats_flush_page_floats_inject\m_page_otr_checked_page_float
381      \expandafter\page_floats_flush_page_floats
382   \fi}
383
384% temp hack, needed to prevent floatbox being forgotten during
385% output, this will change to using another box for flushing
386%
387% \dorecurse{700}{text } \placefigure[top][]{First} {\framed{bla 1}}
388%                        \placefigure[top][]{Second}{\framed{bla 2}}
389% \dorecurse {40}{text } \placefigure[top][]{Third} {\framed{bla 3}}
390
391\appendtoks
392    \global\setbox\savedfloatbox\box\floatbox
393\to \everybeforeoutput
394
395\appendtoks
396    \global\setbox\floatbox\box\savedfloatbox
397\to \everyafteroutput
398
399\protect \endinput
400
401% hm, where is this one used (was in save/restore, see old implementation)
402%
403% \permanent\protected\def\uncenteredfloatbox
404%   {\ifconditional\c_page_floats_center_box
405%      \ifhbox\floatbox\relax % remove centering
406%        \ifdim\wd\floatbox=\hsize
407%          \ifhbox\floatbox
408%            \setbox\scratchbox\hbox
409%              {\unhbox\floatbox
410%               \unskip\unskip
411%               \global\setbox\globalscratchbox\lastbox}%
412%            \box\globalscratchbox
413%          \else
414%            \box\floatbox
415%          \fi
416%        \else
417%          \box\floatbox
418%        \fi
419%      \else
420%        \box\floatbox
421%      \fi
422%    \else
423%      \box\floatbox
424%    \fi}
425