page-ffl.mkxl /size: 11 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=page-ffl,
3%D        version=2018.01.04,
4%D          title=\CONTEXT\ Page Macros,
5%D       subtitle=Facing floats,
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 / Facing floats}
15
16%D The code below comes from a module made for Thomas Schmitz and is now part of the
17%D core. A simple example is given here:
18%D
19%D \starttyping
20%D \definefacingfloat
21%D   [whatever]
22%D
23%D \setupfacingfloat
24%D   [whatever]
25%D   [style=bold,
26%D    width=frame,
27%D    offset=10pt,
28%D    color=white]
29%D
30%D \setupfacingfloat
31%D   [whatever:left]
32%D   [background=color,
33%D    backgroundcolor=red]
34%D
35%D \setupfacingfloat
36%D   [whatever:right]
37%D   [background=color,
38%D    backgroundcolor=green]
39%D
40%D \startfacingfloat[whatever]
41%D     {\dorecurse{10}{\samplefile{tufte} }}
42%D     {\dorecurse{10}{\samplefile{ward}  }}
43%D     {\dorecurse{10}{\samplefile{tufte} }}
44%D     {\dorecurse{10}{\samplefile{ward}  }}
45%D \stopfacingfloat
46%D
47%D \startfacingfloat[whatever]
48%D     \startcontent \dorecurse{10}{\samplefile{tufte} } \stopcontent
49%D     \startcontent \dorecurse{10}{\samplefile{ward}  } \stopcontent
50%D     \startcontent \dorecurse{10}{\samplefile{tufte} } \stopcontent
51%D     \startcontent \dorecurse{10}{\samplefile{ward}  } \stopcontent
52%D \stopfacingfloat
53%D
54%D \flushfacingfloats[whatever]
55%D
56%D \dorecurse{10}{\samplefile{sapolsky} }
57%D
58%D \flushpendingtopcontent
59%D
60%D \stoptyping
61%D
62%D The idea is to flush related floats more or less in parallel.
63
64\unprotect
65
66%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67
68% \starttext
69%
70% \registertopcontent[3]\vbox{\framed[align=normal,width=\textwidth]{\samplefile{ward}}}
71% \registertopcontent[3]\vbox{\framed[align=normal,width=\textwidth]{\samplefile{davis}}}
72% \registertopcontent[5]\vbox{\framed[align=normal,width=\textwidth]{\samplefile{ward}}}
73%
74% \dorecurse{20}{\samplefile{tufte}\par}
75%
76% \stoptext
77
78\newdimension\d_page_adapts_preroll
79\newinteger  \c_page_adapts_pushed
80
81% this assumes a constant textheight and no adaptations otherwise so we
82% should block these for already set pages in adaptheight .. or we can
83% make these independent (so not use adaptheight)
84
85\def\page_adapts_layout_preroll#1%
86  {\begingroup
87   \global\d_page_adapts_preroll\zeropoint
88   \def\page_adapts_layout_indeed##1%
89     {\setupcurrentadaptlayout[\c!top=\zeropoint,##1]%
90      \global\d_page_adapts_preroll\dimexpr\adaptlayoutparameter\c!top\relax}
91   \begincsname\??pageadaptations\number#1\endcsname
92   \endgroup}
93
94\permanent\protected\def\registertopcontent[#1]%
95  {\begingroup
96   \dowithnextbox
97     {\page_adapts_layout_preroll{#1}%
98      \scratchdimen\dimexpr
99        \htdp\nextbox+\d_page_adapts_preroll
100        \ifzeropt\d_page_adapts_preroll
101          +\lineheight
102        \fi
103      \relax
104      \putboxincache{\v!page:\number#1}{+}\nextbox
105      \normalexpanded{\adaptlayout[\number#1][\c!top=\the\scratchdimen]}%
106      \global\advanceby\c_page_adapts_pushed\plusone
107      \endgroup}}
108
109\def\page_otr_flush_top_content
110  {\scratchcounter\getboxcountfromcache{\v!page:\the\realpageno}\relax
111   \ifcase\scratchcounter\else
112      \dorecurse\scratchcounter
113        {\directboxfromcache{\v!page:\the\realpageno}\recurselevel
114         \nointerlineskip
115         \par}%
116      \disposeboxesincache{\v!page:\the\realpageno}%
117      \global\advanceby\c_page_adapts_pushed\minusone
118      \nointerlineskip
119   \fi}
120
121\permanent\protected\def\flushpendingtopcontent
122  {\ifcase\c_page_adapts_pushed\else
123     \null
124     \page
125     \expandafter\flushpendingtopcontent
126  \fi}
127
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129
130% \let\page_check_weird_page\relax
131
132\def\page_check_weird_page_indeed % for now only when facing floats
133  {\ifdim\vsize>\zeropoint\else
134      %\showmessage\m!layouts9{}%
135      \writestatus\m!layouts{forcing zero height page}%
136      \emptyhbox\page
137   \fi}
138
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141% width:
142%
143% -- fit       : do nothing
144% -- dimension : use that
145% -- frame     : use hsize minus frame offsets (based on preroll)
146
147\installcorenamespace {facingfloat}
148
149\installframedcommandhandler \??facingfloat {facingfloat} \??facingfloat
150
151\setupfacingfloat
152  [\c!spaceinbetween=\v!big,
153   \c!inbetween={\blank[\v!big]},
154  %\c!page=\v!left, % not used
155  %\c!style,
156  %\c!color,
157   \c!width=\v!fit]
158
159\newinteger\c_defining_facing_float
160
161\appendtoks
162  \advanceby\c_defining_facing_float\plusone
163  \ifcase\c_defining_facing_float\or
164      \normalexpanded{
165        \definefacingfloat[\currentfacingfloat:\v!left ][\currentfacingfloat]%
166        \definefacingfloat[\currentfacingfloat:\v!right][\currentfacingfloat]%
167      }%
168    \fi
169  \advanceby\c_defining_facing_float\minusone
170\to \everydefinefacingfloat
171
172\newinteger\c_strc_floats_facing_saved
173\newinteger\c_strc_floats_facing_flushed
174\newbox    \b_strc_floats_facing_l
175\newbox    \b_strc_floats_facing_r
176
177\let\m_strc_floats_state\relax
178
179\installcorenamespace {facingfloatflusher}
180
181\def\strc_floats_facing_flush_indeed_step#1#2#3%
182  {\ifnum#2<#3\relax
183     \advanceby#2\plusone
184     \donetrue
185     \writestatus
186       {facing}
187       {page: \the\scratchcounterone, location: #1, blob: \the#2, max: \the#3}%
188     \registertopcontent
189       [\scratchcounterone]
190       \hbox{\directboxfromcache{\currentfacingfloat:#1}{\the#2}}%
191   \fi}
192
193\def\strc_floats_facing_flush_indeed#1%
194  {\begingroup
195   \cdef\currentfacingfloat{#1}%
196   \glet\page_check_weird_page\page_check_weird_page_indeed % for now only when facing floats
197   \scratchcounterone  \realpageno
198   \scratchcounterthree\getboxcountfromcache{\currentfacingfloat:\v!left }\relax
199   \scratchcounterfour \getboxcountfromcache{\currentfacingfloat:\v!right}\relax
200   \scratchcounterfive \zerocount
201   \scratchcountersix  \zerocount
202   % find first empty spread i.e. odd (left) and even (right) empty
203   \ifdim\pagetotal>\zeropoint
204     \advanceby\scratchcounterone\plusone
205   \fi
206   \ifodd\scratchcounterone
207     \advanceby\scratchcounterone\plusone
208   \fi
209   \writestatus
210     {facing}
211     {page: \the\scratchcounterone, start checking}%
212   \doloop{%
213       \page_adapts_layout_preroll\scratchcounterone
214       \ifzeropt\d_page_adapts_preroll
215         % left empty
216         \advanceby\scratchcounterone\plusone
217         \page_adapts_layout_preroll\scratchcounterone
218         \ifzeropt\d_page_adapts_preroll
219           % right empty
220           \advanceby\scratchcounterone\minusone
221           \exitloop
222         \fi
223       \else
224         \advanceby\scratchcounterone\plustwo
225       \fi
226   }
227   \writestatus
228     {facing}
229     {page: \the\scratchcounterone, start flushing}%
230   \doloop{%
231     \ifodd\scratchcounterone
232       \strc_floats_facing_flush_indeed_step\v!right\scratchcountersix \scratchcounterfour
233     \else
234       \strc_floats_facing_flush_indeed_step\v!left \scratchcounterfive\scratchcounterthree
235     \fi
236     \ifnum\scratchcountersix<\scratchcounterfour
237        % more
238     \orelse\ifnum\scratchcounterfive<\scratchcounterthree
239        % more
240     \else
241       \exitloop
242     \fi
243     \advanceby\scratchcounterone\plusone
244   }
245   \disposeboxesincache{\currentfacingfloat:\v!right}%
246   \disposeboxesincache{\currentfacingfloat:\v!left}%
247   \page_check_weird_page_indeed
248   \endgroup}
249
250\permanent\protected\tolerant\def\flushfacingfloats[#1]%
251  {\processcommalist[#1]\strc_floats_facing_flush_indeed}
252
253\protected\def\strc_floats_facing_setup
254  {\cdef\currentfacingfloat{\currentfacingfloat:\m_strc_floats_state}%
255   \usefacingfloatstyleandcolor\c!style\v!color}
256
257\protected\def\strc_floats_facing_collect
258  {\ifx\m_strc_floats_state\v!left
259     \ifvoid\nextbox\orelse\ifzeropt\wd\nextbox\else
260       \ifvoid\b_strc_floats_facing_l
261         \setbox\b_strc_floats_facing_l\box\nextbox
262       \else
263         \setbox\b_strc_floats_facing_l\vbox\bgroup
264           \unvbox\b_strc_floats_facing_l
265           \facingfloatparameter\c!inbetween
266           \unvbox\nextbox
267         \egroup
268       \fi
269     \fi
270     \let\m_strc_floats_state\v!right
271   \orelse\ifx\m_strc_floats_state\v!right
272     \ifvoid\nextbox\orelse\ifzeropt\wd\nextbox\else
273       \ifvoid\b_strc_floats_facing_r
274         \setbox\b_strc_floats_facing_r\box\nextbox
275       \else
276         \setbox\b_strc_floats_facing_r\vbox\bgroup
277           \unvbox\b_strc_floats_facing_r
278           \facingfloatparameter\c!inbetween
279           \unvbox\nextbox
280         \egroup
281       \fi
282     \fi
283     \let\m_strc_floats_state\v!left
284   \else
285     \let\m_strc_floats_state\v!left
286   \fi}
287
288\protected\def\strc_floats_facing_handle
289  {\doifnextbgroupelse
290     \strc_floats_facing_handle_indeed
291     \strc_floats_facing_wrap_up}
292
293\protected\def\strc_floats_facing_handle_indeed
294  {\dowithnextboxcontent
295     \strc_floats_facing_setup
296     {\strc_floats_facing_collect\strc_floats_facing_handle}
297     \vbox}
298
299\permanent\protected\def\startfacingfloat[#1]%
300  {\begingroup
301   \cdef\currentfacingfloat{#1}%
302   \edef\p_width{\facingfloatparameter\c!width}%
303   \letfacingfloatparameter\c!width\v!fit
304   \ifx\p_width\v!frame
305      \setbox\scratchbox\hpack{\inheritedfacingfloatframed{}}%
306      \advanceby\hsize-\wd\scratchbox
307   \orelse\ifx\p_width\v!fit
308      % whatever
309   \else
310      \hsize\p_width
311   \fi
312   \enforced\let\startcontent\bgroup
313   \enforced\let\stopcontent\egroup
314   \let\m_strc_floats_state\v!left
315   \strc_floats_facing_handle}
316
317\permanent\protected\def\stopfacingfloat
318  {\endgroup}
319
320\protected\def\strc_floats_facing_wrap_up
321  {\edef\p_spaceinbetween{\facingfloatparameter\c!spaceinbetween}%
322   \ifempty\p_spaceinbetween
323     \scratchdimen\zeropoint
324   \else
325     \setbox\scratchbox\vbox{\directvspacing\p_spaceinbetween}%
326     \scratchdimen\htdp\scratchbox
327   \fi
328   \ifvoid\b_strc_floats_facing_l\else
329     \page_postprocessors_linenumbers_box\b_strc_floats_facing_l
330   \fi
331   \ifvoid\b_strc_floats_facing_r\else
332     \page_postprocessors_linenumbers_box\b_strc_floats_facing_r
333   \fi
334   \doloop{%
335     \strc_floats_facing_flush_wrap\b_strc_floats_facing_l\v!left
336     \strc_floats_facing_flush_wrap\b_strc_floats_facing_r\v!right
337     \global\advanceby\c_strc_floats_facing_saved\plusone
338     \ifvoid\b_strc_floats_facing_l\relax\ifvoid\b_strc_floats_facing_r\relax
339       \exitloop
340     \fi\fi}}
341
342\def\strc_floats_facing_flush_wrap#1#2%
343  {\ifvoid#1\relax
344     % todo
345   \else
346     \begingroup
347     \setbox\scratchboxone\hpack\bgroup
348       \cdef\currentfacingfloat{\currentfacingfloat:#2}%
349       \inheritedfacingfloatframed{\strut}%
350     \egroup
351     \scratchdimenone\dimexpr\textheight-\htdp\scratchboxone+\lineheight\relax
352     \dontcomplain
353     \splittopskip\zeroskip
354     \setbox\scratchbox\vsplit#1 upto \scratchdimenone
355     \setsplitlisthtdp\scratchbox\strutht\strutdp
356     \setbox\scratchbox\hpack\bgroup
357       \cdef\currentfacingfloat{\currentfacingfloat:#2}%
358       \inheritedfacingfloatframed{\box\scratchbox}%
359     \egroup
360     \setbox\scratchbox\vbox
361       \ifdim\ht\scratchbox<\dimexpr\textheight-\scratchdimen\relax
362         {\box\scratchbox\directvspacing\p_spaceinbetween}%
363       \else
364         to \textheight{\box\scratchbox\vss}%
365       \fi
366     \putboxincache{\currentfacingfloat:#2}{+}\scratchbox
367     \endgroup
368   \fi}
369
370\protect \endinput
371