summaryrefslogtreecommitdiff
path: root/tex/context/base/page-flt.mkiv
blob: 370b40e892bfcc1aeabbf6facbfcc261146a640f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
%D \module
%D   [       file=page-flt,
%D        version=2010.04,08,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Float Management,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Page Macros / Float Management}

%D This module has code that previously was in other modules. There is
%D also float related code in \type {strc-flt.mkiv}.

\registerctxluafile{page-flt}{1.001}

\unprotect

%D To be checked and removed

%def\dosavefloatinfo   {\showmessage\m!floatblocks2{\the\totalnoffloats}}
\def\doinsertfloatinfo {\showmessage\m!floatblocks4{\the\totalnoffloats}}
\def\dofloatflushedinfo{\showmessage\m!floatblocks3{\the\numexpr\totalnoffloats-\savednoffloats\relax}}

%D Extra float registers.

\newif\ifsomefloatwaiting     \somefloatwaitingfalse
\newif\ifroomforfloat         \roomforfloattrue
\newif\ifnofloatpermitted     \nofloatpermittedfalse
\newif\ifcenterfloatbox       \centerfloatboxtrue
\newif\iflocalcenterfloatbox  \localcenterfloatboxfalse
\newif\ifglobalcenterfloatbox \globalcenterfloatboxfalse
\newif\ifflushingfloats       \flushingfloatsfalse
\newif\ifpackflushedfloats    \packflushedfloatstrue % for the moment
\newif\ifpackcolumnfloats     \packcolumnfloatstrue

\newcount\totalnoffloats
\newcount\savednoffloats
\newcount\noffloatinserts
\newcount\nofcollectedfloats % communication channel

\newbox  \floatlist
\newbox  \savedfloatlist
\newbox  \floattext

\newdimen\floatwidth
\newdimen\floatheight
\newdimen\floatdepth
\newdimen\floattextwidth
\newdimen\floattextheight

\newbox  \floatbox
\newbox  \savedfloatbox

%D From now on we manage the float stack at the \LUA\ end instead of packing
%D them in a box and splitting off stacked floats. It's not so much less code
%D but it's cleaner this way. It also opens op some posibilities as we can now
%D more conveniently cary additional information around.

\newtoks \everyfloatscheck

\appendtoks
   \ifcase\savednoffloats
     \global\somefloatwaitingfalse
   \else
     \global\somefloatwaitingtrue
   \fi
\to \everyfloatscheck

\def\dofloatsflush#1#2%
  {\ctxlua{floats.flush("#1",\number#2)}%
   \the\everyfloatscheck}

\def\dofloatssave#1%
  {\ctxlua{floats.save("#1")}%
   \the\everyfloatscheck}

\def\dofloatsresave#1%
  {\ctxlua{floats.resave("#1")}%
   \the\everyfloatscheck}

\def\dopushsavedfloats
  {\ctxlua{floats.push()}%
   \the\everyfloatscheck}

\def\dopopsavedfloats
  {\ctxlua{floats.pop()}%
   \the\everyfloatscheck}

\def\dofloatsgetinfo#1%
  {\ctxlua{floats.consult("#1")}}

\def\doifsavedfloatelse#1%
  {\ctxlua{commands.doifsavedfloatelse("#1")}}

\def\dofloatscollect#1#2#3%
  {\ctxlua{floats.collect("#1",\number\dimexpr#2,\number\dimexpr#3)}}

\def\dofloatsnofstacked#1%
  {\ctxlua{floats.thestacked("#1")}}

\def\dofloatsgetvariable#1%
  {\ctxlua{floats.thevar("specification")}}

\let\dopushcolumnfloats\dopushsavedfloats
\let\dopopcolumnfloats \dopopsavedfloats

\def\dofloatssavepagefloat#1#2%
  {\ctxlua{floats.save("#1", { specification = "#2" })}}

%D The following code is in transition as we don't want to break the
%D current single column, multi column, and columnset mechanism.
%D
%D First we reimplement some helpers.

\def\dogetfloat
  {\ifsomefloatwaiting
     \dofloatsflush{text}{1}%
     \ifcenterfloatbox
       \ifdim\wd\globalscratchbox<\hsize
         \global\setbox\floatbox\hbox to \hsize{\hss\box\floatbox\hss}%
       \else
         % retain special alignments
         \ifinsidecolumns
           \ifdim\wd\floatbox>\makeupwidth
             \global\wd\floatbox\makeupwidth
           \fi
         \fi
       \fi
     \fi
   \else
     \global\savednoffloats\zerocount
     \global\setbox\floatbox\emptybox
   \fi}

\def\dosavefloat
  {\dofloatssave{text}%
   \nonoindentation} % brrr nonoindentation here

\def\doresavefloat
  {\dofloatsresave{text}}

\def\doreversesavefloat
  {\dofloatsresave{text}}

\def\doflushsavedfloats % simplified \OTRONEdodoflushfloats
  {\doloop
     {\ifsomefloatwaiting
        \docheckiffloatfits
        \ifroomforfloat
          \dogetfloat
          \doplacefloatbox
        \else
          \exitloop
        \fi
      \else
        \exitloop
      \fi}}

%D Page floats use different stacks.

\newtoks \everybeforeflushedpagefloat

\def\doflushsomepagefloat#1% future releases can do more clever things
  {\dofloatsflush{#1}{1}%
   \edef\floatspecification{\dofloatsgetvariable{specification}}%
   \the\everybeforeflushedpagefloat
   \vbox to \textheight
     {\doifnotinset\v!high\floatspecification\vfill
      \box\floatbox
      \doifnotinset\v!low\floatspecification\vfill}%
   \vfill\eject}

% \def\doflushpagefloats
%   {\doifoddpageelse
%      {\ifnum\dofloatsnofstacked{rightpage}>\zerocount
%         \doflushsomepagefloat{rightpage}%
%       \else\ifnum\dofloatsnofstacked{page}>\zerocount
%         \doflushsomepagefloat{page}
%       \else\ifnum\dofloatsnofstacked{leftpage}>\zerocount
%         \null\vfill\eject
%        %\doflushsomepagefloat{leftpage}%
%       \fi\fi\fi}
%      {\ifnum\dofloatsnofstacked{leftpage}>\zerocount
%         \doflushsomepagefloat{leftpage}%
%       \else\ifnum\dofloatsnofstacked{page}>\zerocount
%         \doflushsomepagefloat{page}
%       \else\ifnum\dofloatsnofstacked{rightpage}>\zerocount
%         \null\vfill\eject
%        %\doflushsomepagefloat{rightpage}%
%       \fi\fi\fi}}

\def\doflushpagefloats
  {\edef\checkedpagefloat{\ctxlua{floats.thecheckedpagefloat()}}% (true) for packed
   \ifx\checkedpagefloat\empty
     % nothing
   \else\ifx\checkedpagefloat\v!empty
      \null\vfill\eject
   \else
      \doflushsomepagefloat\checkedpagefloat
   \fi\fi}

\def\uncenteredfloatbox
  {\ifcenterfloatbox
     \ifhbox\floatbox\relax % remove centering
       \ifdim\wd\floatbox=\hsize
         \ifhbox\floatbox
           \setbox\scratchbox\hbox
             {\unhbox\floatbox
              \unskip\unskip
              \global\setbox\globalscratchbox\lastbox}%
           \box\globalscratchbox
         \else
           \box\floatbox
         \fi
       \else
         \box\floatbox
       \fi
     \else
       \box\floatbox
     \fi
   \else
     \box\floatbox
   \fi}

\ifdefined\doflushfloats\else \let\doflushfloats\relax \fi
\ifdefined\flushfloatbox\else \let\flushfloatbox\relax \fi

% temp hack, needed to prevent floatbox being forgotten during
% output, this will change to using another box for flushing
%
% \dorecurse{700}{text } \placefigure[top][]{First} {\framed{bla 1}}
%                        \placefigure[top][]{Second}{\framed{bla 2}}
% \dorecurse {40}{text } \placefigure[top][]{Third} {\framed{bla 3}}

\newbox\savedfloatbox

\appendtoks
    \global\setbox\savedfloatbox\box\floatbox
\to \everybeforeoutput
\appendtoks
    \global\setbox\floatbox\box\savedfloatbox
\to \everyafteroutput

\protect \endinput