page-flt.mkxl /size: 11 Kb    last modification: 2023-12-21 09:44
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]
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}\dimexpr#2\relax\dimexpr#3\relax}
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\protected\def\page_floats_save_page_float#1#2%
136  {\clf_savespecificfloat{#1}{specification{#2}}\relax}
137
138\protected\def\page_floats_save_somewhere_float#1#2% #1=method
139  {\clf_savespecificfloat{#1}{specification{#2}label{\floatlabel}}\relax}
140
141%D This is an experimental new feature (for Alan Braslau), a prelude to more:
142%D
143%D \starttyping
144%D test \placefigure{}{}
145%D test \placefigure[somewhere:alpha][whatever]{}{}
146%D test \placefigure[somewhere:beta] [whatever]{}{}
147%D test \placefigure[somewhere:gamma][whatever]{}{}
148%D test \placefigure[somewhere:delta][whatever]{}{}
149%D test \placefigure{}{}
150%D
151%D in \in{figure}[whatever] bla bla
152%D
153%D \placenamedfloat[figure][*l*]
154%D \placenamedfloat[figure][gamma]
155%D \placenamedfloat[figure][beta]
156%D \stoptyping
157
158\permanent\tolerant\protected\def\placenamedfloat[#1]#*[#2]%
159  {\doloop
160     {\page_floats_flush_by_label\s!somewhere{#2}%
161      \ifvoid\floatbox
162        \exitloop
163      \else
164        \cdef\currentfloat{#1}%
165        \blank[\rootfloatparameter\c!spacebefore]%
166        \box\floatbox
167        \blank[\rootfloatparameter\c!spaceafter]%
168      \fi}}
169
170%D Not the best name for a command:
171%D
172%D \starttyping
173%D \samplefile{tufte}
174%D \placefigure[somewhere:a]{}{\externalfigure[dummy-001]}
175%D \samplefile{tufte}
176%D \placefigure{}{\externalfigure[dummy-002]}
177%D \samplefile{tufte}
178%D \startplacefigure[location={somewhere:b,tight}]
179%D     \externalfigure[dummy-003]
180%D \stopplacefigure
181%D \samplefile{tufte}
182%D \placefigure{}{\externalfigure[dummy-004]}
183%D \page
184%D \placesavedfloat[figure][b][location={none,left}] \samplefile{tufte}
185%D \placesavedfloat[figure][a]
186%D % \placenamedfloat[figure][*]
187%D % \placenamedfloat[figure][b]
188%D \stoptyping
189
190\newbox\b_strc_float_saved
191
192\permanent\tolerant\protected\def\placesavedfloat[#1]#*[#2]#*[#S#3]% experiment for Alan B
193  {\clf_flushlabeledfloat\s!somewhere{#2}\relax
194  %\cdef\currentfloat{#1}%
195   \setbox\b_strc_float_saved\vpack{\box\floatbox}%
196   \startplacefloat[#1][\c!location=\v!none,#3]%
197     \box\b_strc_float_saved
198   \stopplacefloat}
199
200% \setupcaption [figure]   [align=flushleft]
201% \setupcaption [figure-1] [align=flushleft,leftmargin=10mm]
202% \setupcaption [figure-2] [align=flushleft,leftmargin=10mm,rightmargin=-10mm,width=\textwidth]
203%
204% \startsetups somefigure
205%     \ifdim\floatsetupwidth>\textwidth
206%         \placesetupfloat[figure-2]
207%     \else
208%         \placesetupfloat[figure-1]
209%     \fi
210% \stopsetups
211%
212% \placefloatwithsetups[somefigure]{}{\externalfigure[dummy][width=5cm,height=2cm]}
213
214\mutable\lettonothing\floatsetupcaption
215\mutable\lettonothing\floatsetupcontent
216\mutable\lettonothing\floatsetupwidth
217\mutable\lettonothing\floatsetupheight
218
219\aliased\let\placesetupfloat\gobbleoneoptional
220
221\permanent\tolerant\protected\def\placefloatwithsetups[#1]#*[#S#2]#*[#S#3]#:#4%
222  {\def\floatsetupcaption{#4}%
223   \def\floatsetupcontent{\copy\nextbox}%
224   \def\floatsetupwidth  {\wd\nextbox}%
225   \def\floatsetupheight {\ht\nextbox}%
226   \enforced\permanent\protected\def\placesetupfloat[##1]%
227     {\placefloat[##1][#2][#3]{#4}{\floatsetupcontent}}% #4 and not \floatsetupcaption (protected)
228   \dowithnextbox{\setups[#1]}\vbox}
229
230%D The following code is in transition as we don't want to break the current single
231%D column, multi column, and columnset mechanism.
232%D
233%D First we reimplement some helpers.
234
235\def\page_floats_get_used_hsize{\hsize}
236
237\protected\def\page_floats_get
238  {\ifconditional\c_page_floats_some_waiting
239     \page_floats_flush\s!text\plusone
240     \ifconditional\c_page_floats_center_box
241       \ifdim\wd\globalscratchbox<\page_floats_get_used_hsize
242         \global\setbox\floatbox\hpack to \page_floats_get_used_hsize{\hss\box\floatbox\hss}%
243       \orelse\ifinsidecolumns
244         % retain special alignments
245         \ifdim\wd\floatbox>\makeupwidth
246           \wd\floatbox\makeupwidth
247         \fi
248       \fi
249     \fi
250   \else
251     \global\savednoffloats\zerocount
252     \global\setbox\floatbox\emptybox
253   \fi}
254
255\protected\def\page_floats_flush_saved
256  {\doloop
257     {\ifconditional\c_page_floats_some_waiting
258        \page_otr_command_check_if_float_fits
259        \ifconditional\c_page_floats_room
260          \page_floats_get
261          \doplacefloatbox
262        \else
263          \exitloop
264        \fi
265      \else
266      % \ifconditional\c_page_margin_blocks_present % not here, here just as many floats as fit
267      %   \page_otr_command_flush_margin_blocks
268      % \else
269          \exitloop
270      % \fi
271      \fi}}
272
273%D This is a future mechamism that will be integrated once we're sure about it:
274%D
275%D \starttyping
276%D \dorecurse{10}
277%D   {\input thuan
278%D    \placefigure{}{\framed[height=1.5cm]{test}}
279%D    \placefloatplaceholder}
280%D \stoptyping
281
282\permanent\protected\def\placefloatplaceholder
283  {\ifconditional\c_page_floats_room \else
284     \ifdim\dimexpr\pagegoal-\pagetotal-3\lineheight\relax>\zeropoint
285       \startlinecorrection[blank]
286       \mhbox{\inframed{\labeltexts{placeholder}{\m_strc_floats_last_caption_tag}}}%
287       \stoplinecorrection
288     \else
289       \allowbreak
290     \fi
291   \fi}
292
293\setuplabeltext
294  [placeholder={\Word{\lastplacedfloat}~, moved}]
295
296%D Page floats use different stacks.
297
298\newtoks\everybeforeflushedpagefloat
299
300\let\m_page_otr_checked_page_float\relax
301
302\newconditional\c_page_floats_flushed
303
304\def\page_floats_flush_page_floats_inject#1% future releases can do more clever things
305  {\page_floats_flush{#1}\plusone
306   \edef\floatspecification{\clf_getfloatvariable{specification}}% Is this okay?
307   \expand\everybeforeflushedpagefloat
308   \vpack to \textheight
309     {\ifinset\v!high\floatspecification\else\vfill\fi
310      \box\floatbox
311      \ifinset\v!low \floatspecification\else\vfill\fi}%
312   \page_otr_fill_and_eject_page % needed
313   \global\c_page_floats_flushed\conditionaltrue}
314
315\protected\def\page_floats_flush_page_floats % used in postpone
316  {\global\c_page_floats_flushed\conditionalfalse
317   \page_floats_flush_page_floats_indeed
318   \ifconditional\c_page_floats_flushed
319     \page_otr_fill_and_eject_page
320   \fi}
321
322\protected\def\page_floats_flush_page_floats_indeed % used in postpone
323  {\edef\m_page_otr_checked_page_float{\clf_checkedpagefloat}%
324   \ifempty\m_page_otr_checked_page_float
325     % nothing
326   \orelse\ifx\m_page_otr_checked_page_float\v!empty
327      \emptyhbox \page_otr_fill_and_eject_page % why not dummy_page
328      \expandafter\page_floats_flush_page_floats
329   \else
330      \page_floats_flush_page_floats_inject\m_page_otr_checked_page_float
331      \expandafter\page_floats_flush_page_floats
332   \fi}
333
334% temp hack, needed to prevent floatbox being forgotten during
335% output, this will change to using another box for flushing
336%
337% \dorecurse{700}{text } \placefigure[top][]{First} {\framed{bla 1}}
338%                        \placefigure[top][]{Second}{\framed{bla 2}}
339% \dorecurse {40}{text } \placefigure[top][]{Third} {\framed{bla 3}}
340
341\appendtoks
342    \global\setbox\savedfloatbox\box\floatbox
343\to \everybeforeoutput
344
345\appendtoks
346    \global\setbox\floatbox\box\savedfloatbox
347\to \everyafteroutput
348
349\protect \endinput
350
351% hm, where is this one used (was in save/restore, see old implementation)
352%
353% \permanent\protected\def\uncenteredfloatbox
354%   {\ifconditional\c_page_floats_center_box
355%      \ifhbox\floatbox\relax % remove centering
356%        \ifdim\wd\floatbox=\hsize
357%          \ifhbox\floatbox
358%            \setbox\scratchbox\hbox
359%              {\unhbox\floatbox
360%               \unskip\unskip
361%               \global\setbox\globalscratchbox\lastbox}%
362%            \box\globalscratchbox
363%          \else
364%            \box\floatbox
365%          \fi
366%        \else
367%          \box\floatbox
368%        \fi
369%      \else
370%        \box\floatbox
371%      \fi
372%    \else
373%      \box\floatbox
374%    \fi}
375