page-ffl.mkiv /size: 7468 b    last modification: 2020-07-01 14:35
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 \dorecurse{10}{\samplefile{sapolsky} }
55%D \stoptyping
56%D
57%D The idea is to flush related floats more or less in parallel.
58
59\unprotect
60
61% width:
62%
63% -- fit       : do nothing
64% -- dimension : use that
65% -- frame     : use hsize minus frame offsets (based on preroll)
66
67\installcorenamespace {facingfloat}
68
69\installframedcommandhandler \??facingfloat {facingfloat} \??facingfloat
70
71\setupfacingfloat
72  [\c!spaceinbetween=\v!big,
73   \c!inbetween={\blank[\v!big]},
74   \c!width=\v!fit,
75  %\c!style,
76  %\c!color,
77   \c!page=\v!left]
78
79\appendtoks
80    \ifx\currentfacingfloatparent\empty
81        \normalexpanded{\definefacingfloat[\currentfacingfloat:\v!left ][\currentfacingfloat]}%
82        \normalexpanded{\definefacingfloat[\currentfacingfloat:\v!right][\currentfacingfloat]}%
83    \fi
84\to \everydefinefacingfloat
85
86\newcount\c_strc_floats_facing_saved
87\newcount\c_strc_floats_facing_flushed
88
89\newbox\b_strc_floats_facing_l
90\newbox\b_strc_floats_facing_r
91
92\let\m_strc_floats_state\relax
93
94\unexpanded\def\strc_floats_facing_flush
95  {\ifnum\c_strc_floats_facing_flushed<\c_strc_floats_facing_saved
96     \ifodd\c_strc_floats_facing_flushed
97       \ifodd\realpageno
98         \strc_floats_facing_flush_indeed
99         \doifelsependingpagecontent\relax{\null\page}%
100       \else
101       \fi
102     \else
103       \ifodd\realpageno
104       \else
105         \strc_floats_facing_flush_indeed
106         \doifelsependingpagecontent\relax{\null\page}%
107       \fi
108     \fi
109   \fi}
110
111\def\strc_floats_facing_flush_indeed
112  {\global\advance\c_strc_floats_facing_flushed\plusone
113   \floatingpenalty\zerocount
114   \insert\namedinsertionnumber\s!topfloat\bgroup
115     \forgetall
116     \ifconditional\c_page_one_top_of_insert
117       \ifconditional\c_page_one_correct_top_insert
118         \topskipcorrection % [xx] new: see icare topbleed
119         \kern-\lineskip
120         \par
121         \prevdepth\maxdimen
122       \fi
123     \fi
124     \directboxfromcache{\currentfacingfloat}{\number\c_strc_floats_facing_flushed}%
125     \vkern\s_page_one_between_top_insert
126   \egroup
127   \ifnum\c_strc_floats_facing_saved=\c_strc_floats_facing_flushed
128     \global\c_strc_floats_facing_saved  \zerocount
129     \global\c_strc_floats_facing_flushed\zerocount
130     \resetboxesincache{\currentfacingfloat}%
131   \fi}
132
133\let\flushfacingfloats\strc_floats_facing_flush
134
135\unexpanded\def\strc_floats_facing_setup
136  {\edef\currentfacingfloat{\currentfacingfloat:\m_strc_floats_state}%
137   \usefacingfloatstyleandcolor\c!style\v!color}
138
139\unexpanded\def\strc_floats_facing_collect
140  {\ifx\m_strc_floats_state\v!left
141     \ifvoid\nextbox\else\ifzeropt\wd\nextbox\else
142       \ifvoid\b_strc_floats_facing_l
143         \setbox\b_strc_floats_facing_l\box\nextbox
144       \else
145         \setbox\b_strc_floats_facing_l\vbox\bgroup
146           \unvbox\b_strc_floats_facing_l
147           \facingfloatparameter\c!inbetween
148           \unvbox\nextbox
149         \egroup
150       \fi
151     \fi\fi
152     \let\m_strc_floats_state\v!right
153   \else\ifx\m_strc_floats_state\v!right
154     \ifvoid\nextbox\else\ifzeropt\wd\nextbox\else
155       \ifvoid\b_strc_floats_facing_r
156         \setbox\b_strc_floats_facing_r\box\nextbox
157       \else
158         \setbox\b_strc_floats_facing_r\vbox\bgroup
159           \unvbox\b_strc_floats_facing_r
160           \facingfloatparameter\c!inbetween
161           \unvbox\nextbox
162         \egroup
163       \fi
164     \fi\fi
165     \let\m_strc_floats_state\v!left
166   \else
167     \let\m_strc_floats_state\v!left
168   \fi\fi}
169
170\unexpanded\def\strc_floats_facing_handle
171  {\doifnextbgroupelse
172     \strc_floats_facing_handle_indeed
173     \strc_floats_facing_wrap_up}
174
175\unexpanded\def\strc_floats_facing_handle_indeed
176  {\dowithnextboxcontent
177     \strc_floats_facing_setup
178     {\strc_floats_facing_collect\strc_floats_facing_handle}
179     \vbox}
180
181\unexpanded\def\startfacingfloat[#1]%
182  {\begingroup
183   % todo: \usepageparameter
184%    \edef\p_page{\facingfloatparameter\c!page}%
185%    \ifx\p_page\empty\else
186%      \page[\p_page]%
187%    \fi
188   %
189   \edef\currentfacingfloat{#1}%
190   \edef\p_width{\facingfloatparameter\c!width}%
191   \letfacingfloatparameter\c!width\v!fit
192   \ifx\p_width\v!frame
193      \setbox\scratchbox\hpack{\inheritedfacingfloatframed{}}%
194      \advance\hsize-\wd\scratchbox
195   \else\ifx\p_width\v!fit
196      % whatever
197   \else
198      \hsize\p_width
199   \fi\fi
200   \let\startcontent\bgroup
201   \let\stopcontent\egroup
202   \let\m_strc_floats_state\v!left
203   \strc_floats_facing_handle}
204
205\unexpanded\def\stopfacingfloat
206  {\endgroup}
207
208\unexpanded\def\strc_floats_facing_wrap_up
209  {\edef\p_spaceinbetween{\facingfloatparameter\c!spaceinbetween}%
210   \ifx\p_spaceinbetween\empty
211     \scratchdimen\zeropoint
212   \else
213     \setbox\scratchbox\vbox{\directvspacing\p_spaceinbetween}%
214     \scratchdimen\htdp\scratchbox
215   \fi
216   \ifvoid\b_strc_floats_facing_l\else
217     \page_postprocessors_linenumbers_box\b_strc_floats_facing_l
218   \fi
219   \ifvoid\b_strc_floats_facing_r\else
220     \page_postprocessors_linenumbers_box\b_strc_floats_facing_r
221   \fi
222   \doloop{%
223     \strc_floats_facing_flush_wrap\b_strc_floats_facing_l\v!left
224     \strc_floats_facing_flush_wrap\b_strc_floats_facing_r\v!right
225     \ifvoid\b_strc_floats_facing_l\relax\ifvoid\b_strc_floats_facing_r\relax
226       \exitloop
227     \fi\fi}}
228
229\def\strc_floats_facing_flush_wrap#1#2%
230  {\ifvoid#1\relax
231     % todo
232   \else
233     \begingroup
234     \setbox\scratchbox\vsplit#1 upto \textheight
235     \setbox\scratchbox\hpack\bgroup
236       \edef\currentfacingfloat{\currentfacingfloat:#2}%
237       \inheritedfacingfloatframed{\box\scratchbox}%
238     \egroup
239     \ifdim\ht\scratchbox<\dimexpr\textheight-\scratchdimen\relax
240        \setbox\scratchbox\vbox{\box\scratchbox\directvspacing\p_spaceinbetween}%
241     \else
242        \setbox\scratchbox\vbox to \textheight{\box\scratchbox\vss}%
243     \fi
244     \global\advance\c_strc_floats_facing_saved\plusone
245     \putboxincache{\currentfacingfloat}{\number\c_strc_floats_facing_saved}\scratchbox
246     \endgroup
247   \fi}
248
249\protect \endinput
250