page-flt.mkiv /size: 10 Kb    last modification: 2021-10-28 13:50
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
17%D also float 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]
26
27\newdimen   \d_page_floats_inserted_bottom
28\newdimen   \d_page_floats_inserted_top
29\newdimen   \d_page_floats_inserted_page
30\newcount   \c_page_floats_n_of_top         \c_page_floats_n_of_top   \plustwo
31\newcount   \c_page_floats_n_of_bottom      \c_page_floats_n_of_bottom\zerocount
32\newcount   \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_saved
37%   {\showmessage\m!floatblocks2
38%      {\the\totalnoffloats}}
39
40\def\page_floats_report_total
41  {\showmessage\m!floatblocks4%
42     {\the\totalnoffloats
43      \ifx\floatlocationmethod\empty
44        \ifx\floatlocation\empty\else,\floatlocation\fi
45      \else
46        ,\floatlocationmethod
47      \fi}}
48
49\def\page_floats_report_flushed
50  {\showmessage\m!floatblocks3%
51     {\the\numexpr\totalnoffloats-\savednoffloats\relax}}
52
53%D Extra float registers.
54
55\newconditional\c_page_floats_room
56\newconditional\c_page_floats_some_waiting
57\newconditional\c_page_floats_not_permitted
58\newconditional\c_page_floats_flushing
59\newconditional\c_page_floats_center_box          \settrue\c_page_floats_center_box
60\newconditional\c_page_floats_center_box_local
61\newconditional\c_page_floats_center_box_global
62\newconditional\c_page_floats_compress_flushed       \settrue\c_page_floats_compress_flushed
63
64\newdimen      \d_page_floats_compress_distance
65
66%D For the moment we keep this but they will become private too.
67
68\newcount\totalnoffloats           % these will be redone ... handled at the lua end anyway
69\newcount\savednoffloats           % these will be redone ... handled at the lua end anyway
70\newcount\nofcollectedfloats       % communication channel
71\newdimen\maxcollectedfloatstotal  % communication channel
72
73\newcount\noffloatinserts          % these will be redone ... handled at the lua end anyway
74
75\newbox  \floattext
76
77\newdimen\floatwidth
78\newdimen\floatheight
79\newdimen\floatdepth
80\newdimen\floattextwidth
81\newdimen\floattextheight
82
83\newbox  \floatbox
84\newbox  \savedfloatbox
85
86%D From now on we manage the float stack at the \LUA\ end instead of packing
87%D them in a box and splitting off stacked floats. It's not so much less code
88%D but it's cleaner this way. It also opens op some posibilities as we can now
89%D more conveniently cary additional information around.
90
91\newtoks \everyfloatscheck
92
93\appendtoks
94   \ifcase\savednoffloats
95     \global\setfalse\c_page_floats_some_waiting
96   \else
97     \global\settrue\c_page_floats_some_waiting
98   \fi
99\to \everyfloatscheck
100
101\unexpanded\def\page_floats_flush#1#2%
102  {\clf_flushfloat{#1}#2\relax
103   \the\everyfloatscheck}
104
105\unexpanded\def\page_floats_flush_by_label#1#2%
106  {\clf_flushlabeledfloat{#1}{#2}\relax
107   \the\everyfloatscheck}
108
109\unexpanded\def\page_floats_save#1%
110  {\clf_savefloat{#1}\relax
111   \the\everyfloatscheck}
112
113\unexpanded\def\page_floats_resave#1%
114  {\clf_resavefloat{#1}\relax
115   \the\everyfloatscheck}
116
117\unexpanded\def\page_floats_push_saved
118  {\clf_pushfloat
119   \the\everyfloatscheck}
120
121\unexpanded\def\page_floats_pop_saved
122  {\clf_popfloat
123   \the\everyfloatscheck}
124
125\unexpanded\def\page_floats_get_info#1%
126  {\clf_consultfloat{#1}}
127
128\unexpanded\def\page_floats_if_else#1%
129  {\clf_doifelsestackedfloats{#1}}
130
131\unexpanded\def\page_floats_collect#1#2#3%
132  {\clf_collectfloat{#1}\dimexpr#2\relax\dimexpr#3\relax}
133
134\def\nofstackedfloatsincategory#1%
135  {\clf_nofstackedfloats{#1}}
136
137\let\page_floats_column_push_saved\page_floats_push_saved % overloaded in page-mul
138\let\page_floats_column_pop_saved \page_floats_pop_saved  % overloaded in page-mul
139
140\unexpanded\def\page_floats_save_page_float#1#2%
141  {\clf_savespecificfloat{#1}{specification{#2}}\relax}
142
143\unexpanded\def\page_floats_save_somewhere_float#1#2% #1=method
144  {\clf_savespecificfloat{#1}{specification{#2}label{\floatlabel}}\relax}
145
146%D This is an experimental new feature (for Alan Braslau), a prelude to more:
147%D
148%D \starttyping
149%D test \placefigure{}{}
150%D test \placefigure[somewhere:alpha][whatever]{}{}
151%D test \placefigure[somewhere:beta] [whatever]{}{}
152%D test \placefigure[somewhere:gamma][whatever]{}{}
153%D test \placefigure[somewhere:delta][whatever]{}{}
154%D test \placefigure{}{}
155%D
156%D in \in{figure}[whatever] bla bla
157%D
158%D \placenamedfloat[figure][*l*]
159%D \placenamedfloat[figure][gamma]
160%D \placenamedfloat[figure][beta]
161%D \stoptyping
162
163\unexpanded\def\placenamedfloat
164  {\dodoubleargument\page_floats_place_named}
165
166\def\page_floats_place_named[#1][#2]%
167  {\doloop
168     {\page_floats_flush_by_label\s!somewhere{#2}%
169      \ifvoid\floatbox
170        \exitloop
171      \else
172        \def\currentfloat{#1}%
173        \blank[\rootfloatparameter\c!spacebefore]%
174        \box\floatbox
175        \blank[\rootfloatparameter\c!spaceafter]%
176      \fi}}
177
178% \setupcaption [figure]   [align=flushleft]
179% \setupcaption [figure-1] [align=flushleft,leftmargin=10mm]
180% \setupcaption [figure-2] [align=flushleft,leftmargin=10mm,rightmargin=-10mm,width=\textwidth]
181%
182% \startsetups somefigure
183%     \ifdim\floatsetupwidth>\textwidth
184%         \placesetupfloat[figure-2]
185%     \else
186%         \placesetupfloat[figure-1]
187%     \fi
188% \stopsetups
189%
190% \placefloatwithsetups[somefigure]{}{\externalfigure[dummy][width=5cm,height=2cm]}
191
192\unexpanded\def\placefloatwithsetups
193  {\dotripleempty\page_floats_place_with_setups}
194
195\def\page_floats_place_with_setups[#1][#2][#3]#4%
196  {\def\floatsetupcaption   {#4}%
197   \def\floatsetupcontent   {\copy\nextbox}%
198   \def\floatsetupwidth     {\wd\nextbox}%
199   \def\floatsetupheight    {\ht\nextbox}%
200   \def\placesetupfloat[##1]{\placefloat[##1][#2][#3]{#4}{\floatsetupcontent}}% #4 and not \floatsetupcaption (unexpanded)
201   \dowithnextbox{\setups[#1]}\vbox}
202
203%D The following code is in transition as we don't want to break the
204%D current single column, multi column, and columnset mechanism.
205%D
206%D First we reimplement some helpers.
207
208\def\page_floats_get_used_hsize{\hsize}
209
210\unexpanded\def\page_floats_get
211  {\ifconditional\c_page_floats_some_waiting
212     \page_floats_flush\s!text\plusone
213     \ifconditional\c_page_floats_center_box
214       \ifdim\wd\globalscratchbox<\page_floats_get_used_hsize
215         \global\setbox\floatbox\hpack to \page_floats_get_used_hsize{\hss\box\floatbox\hss}%
216       \else
217         % retain special alignments
218         \ifinsidecolumns
219           \ifdim\wd\floatbox>\makeupwidth
220             \wd\floatbox\makeupwidth
221           \fi
222         \fi
223       \fi
224     \fi
225   \else
226     \global\savednoffloats\zerocount
227     \global\setbox\floatbox\emptybox
228   \fi}
229
230\unexpanded\def\page_floats_flush_saved
231  {\doloop
232     {\ifconditional\c_page_floats_some_waiting
233        \page_otr_command_check_if_float_fits
234        \ifconditional\c_page_floats_room
235          \page_floats_get
236          \doplacefloatbox
237        \else
238          \exitloop
239        \fi
240      \else
241      % \ifconditional\c_page_margin_blocks_present % not here, here just as many floats as fit
242      %   \page_otr_command_flush_margin_blocks
243      % \else
244          \exitloop
245      % \fi
246      \fi}}
247
248%D This is a future mechamism that will be integrated once we're sure about it:
249%D
250%D \starttyping
251%D \dorecurse{10}
252%D   {\input thuan
253%D    \placefigure{}{\framed[height=1.5cm]{test}}
254%D    \placefloatplaceholder}
255%D \stoptyping
256
257\unexpanded\def\placefloatplaceholder
258  {\ifconditional\c_page_floats_room \else
259     \ifdim\dimexpr\pagegoal-\pagetotal-3\lineheight\relax>\zeropoint
260       \startlinecorrection[blank]
261       \mhbox{\inframed{\labeltexts{placeholder}{\lastcaptiontag}}}%
262       \stoplinecorrection
263     \else
264       \allowbreak
265     \fi
266   \fi}
267
268\setuplabeltext
269  [placeholder={\Word{\lastplacedfloat}~, moved}]
270
271%D Page floats use different stacks.
272
273\newtoks\everybeforeflushedpagefloat
274
275\def\page_floats_flush_page_floats_indeed#1% future releases can do more clever things
276  {\page_floats_flush{#1}\plusone
277   \edef\floatspecification{\clf_getfloatvariable{specification}}% Is this okay?
278   \the\everybeforeflushedpagefloat
279   \vpack to \textheight
280     {\doifnotinset\v!high\floatspecification\vfill
281      \box\floatbox
282      \doifnotinset\v!low\floatspecification\vfill}%
283   \page_otr_fill_and_eject_page}
284
285\let\m_page_otr_checked_page_float\relax
286
287\unexpanded\def\page_floats_flush_page_floats % used in postpone
288  {\edef\m_page_otr_checked_page_float{\clf_checkedpagefloat}%
289   \ifx\m_page_otr_checked_page_float\empty
290     % nothing
291   \else\ifx\m_page_otr_checked_page_float\v!empty
292      \emptyhbox \page_otr_fill_and_eject_page % why not dummy_page
293   \else
294      \page_floats_flush_page_floats_indeed\m_page_otr_checked_page_float
295   \fi\fi}
296
297% temp hack, needed to prevent floatbox being forgotten during
298% output, this will change to using another box for flushing
299%
300% \dorecurse{700}{text } \placefigure[top][]{First} {\framed{bla 1}}
301%                        \placefigure[top][]{Second}{\framed{bla 2}}
302% \dorecurse {40}{text } \placefigure[top][]{Third} {\framed{bla 3}}
303
304\appendtoks
305    \global\setbox\savedfloatbox\box\floatbox
306\to \everybeforeoutput
307
308\appendtoks
309    \global\setbox\floatbox\box\savedfloatbox
310\to \everyafteroutput
311
312\protect \endinput
313
314% hm, where is this one used (was in save/restore, see old implementation)
315%
316% \unexpanded\def\uncenteredfloatbox
317%   {\ifconditional\c_page_floats_center_box
318%      \ifhbox\floatbox\relax % remove centering
319%        \ifdim\wd\floatbox=\hsize
320%          \ifhbox\floatbox
321%            \setbox\scratchbox\hbox
322%              {\unhbox\floatbox
323%               \unskip\unskip
324%               \global\setbox\globalscratchbox\lastbox}%
325%            \box\globalscratchbox
326%          \else
327%            \box\floatbox
328%          \fi
329%        \else
330%          \box\floatbox
331%        \fi
332%      \else
333%        \box\floatbox
334%      \fi
335%    \else
336%      \box\floatbox
337%    \fi}
338