summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/page-otr.mklx
blob: adca25eb57fb0bdb1c0a5c45ec719023cff0cac9 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
%D \module
%D   [       file=page-otr,
%D        version=2012.01.25,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Output Routines,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 / Output Routines}

%D This module will get some of the code from other modules. At the same time we
%D provide a bit more control.

% When issuing two \par\penalty-\plustenthousand's, only the first triggers the
% otr. Is this an obscure feature or an optimization?

\registerctxluafile{page-otr}{autosuffix}

\unprotect

% triggerpagebuilder % defined at the lua end

\installcorenamespace{outputroutine}

\installswitchcommandhandler \??outputroutine {outputroutine} \??outputroutine

\newtoks\t_page_otr_commands
\newtoks\t_page_otr_tracers

\permanent\protected\def\defineoutputroutinecommand[#name]% doing multiple on one go saves syncing
  {\processcommalist[#name]\page_otr_commands_define}

\protected\def\page_otr_commands_define#name%
  {\ifcsname#name\endcsname \else
     \letcsname#name\endcsname\relax
     \normalexpanded{\t_page_otr_commands{\the\t_page_otr_commands\noexpand\page_otr_commands_process{#name}}}%
   \fi}

\let\page_otr_commands_process\gobbleoneargument

\appendtoks
    \let\page_otr_commands_process\page_otr_specifics_preset
    \the\t_page_otr_commands
    \let\page_otr_commands_process\gobbleoneargument
\to \everyswitchoutputroutine

\protected\def\page_otr_specifics_preset#name%
  {\edef\page_otr_specifics_command{\directoutputroutineparameter{#name}}% no inheritance of commands
   \ifempty\page_otr_specifics_command
     \writestatus{\currentoutputroutine}{- \expandafter\strippedcsname\csname#name\endcsname}%
     \letcsname#name\endcsname\relax
   \else
     \writestatus{\currentoutputroutine}{+ \expandafter\strippedcsname\csname#name\endcsname}%
     \letcsname#name\expandafter\endcsname\page_otr_specifics_command
   \fi}

\protected\def\page_otr_specifics_preset_normal#name%
  {\edef\page_otr_specifics_command{\directoutputroutineparameter{#name}}% no inheritance of commands
   \ifempty\page_otr_specifics_command
     \letcsname#name\endcsname\relax
   \else
     \letcsname#name\expandafter\endcsname\page_otr_specifics_command
   \fi}

\protected\def\page_otr_specifics_preset_traced#name%
  {\edef\page_otr_specifics_command{\directoutputroutineparameter{#name}}% no inheritance of commands
   \ifempty\page_otr_specifics_command
     \writestatus{\currentoutputroutine}{preset: - \expandafter\strippedcsname\csname#name\endcsname}%
     \letcsname#name\endcsname\relax
   \else
     \writestatus{\currentoutputroutine}{preset: + \expandafter\strippedcsname\csname#name\endcsname}%
     \letcsname#name\expandafter\endcsname\page_otr_specifics_command
   \fi}

\let\page_otr_specifics_preset\page_otr_specifics_preset_normal

\permanent\protected\def\traceoutputroutines
  {\the\t_page_otr_tracers}

\appendtoks
    \let\page_otr_specifics_preset\page_otr_specifics_preset_traced
\to \t_page_otr_tracers

%D We have a couple of output routines and the default one is the single column
%D routine. Then there is a multicolumn variant that can be used mixed, and a
%D columnset variant that is more exclusive.

\installcorenamespace{otrtriggers}

\newconstant\c_page_otr_eject_penalty   \c_page_otr_eject_penalty   -\plustenthousand
\newconstant\c_page_otr_super_penalty   \c_page_otr_super_penalty   -\plustwentythousand
\newcount   \c_page_otr_trigger_penalty \c_page_otr_trigger_penalty -100010

\newcount   \c_page_otr_columns % we will share this one

\newif      \ifinotr % we keep this (name) for old times sake

% \def\page_otr_update_page_goal#1#2%
%   {\global\c_page_otr_columns#2\relax
%    \pagegoal\dimexpr\vsize-\c_page_otr_columns\insertheights\relax}

\appendtoks
    \insertheights\zeropoint
\to \everyaftershipout

\protected\def\page_otr_message_b{\page_otr_message_s+}
\protected\def\page_otr_message_e{\page_otr_message_s-}

\protected\def\page_otr_message_s#sign#what%
  {\writestatus
    \currentoutputroutine
    {#sign\space          \space
     #what\space          \space
     p:\the\outputpenalty,\space
     r:\the\realpageno   ,\space
     c:\number\mofcolumns,\space
     v:\the\vsize        ,\space
     g:\the\pagegoal     ,\space
     t:\the\pagetotal    ,\space
     i:\the\insertheights
     \ifdim\pagetotal>\pagegoal
       ,\space
       d:\the\dimexpr\pagetotal-\pagegoal\relax
     \fi}}

\protected\def\page_otr_trigger#penalty%
  {\begingroup
   \par
   \penalty#penalty%
   \endgroup}

\permanent\protected\def\installoutputroutine#invoke#action% \invoke \action
  {\global\advance\c_page_otr_trigger_penalty\minusone
   \frozen\protected\edef#invoke{\page_otr_trigger{\number\c_page_otr_trigger_penalty}}%
   \setvalue{\??otrtriggers\number\c_page_otr_trigger_penalty}{#action}}

\protected\def\page_otr_triggered_output_routine_traced
  {\ifcsname\??otrtriggers\the\outputpenalty\endcsname
     \page_otr_message_b{special}%
     \csname\??otrtriggers\the\outputpenalty\endcsname % \lastnamedcs can be gone
     \page_otr_message_e{special}%
   \else
     \page_otr_message_b{normal}%
     \page_otr_command_routine
     \page_otr_message_e{normal}%
   \fi}

\protected\def\page_otr_triggered_output_routine_normal
  {\ifcsname\??otrtriggers\the\outputpenalty\endcsname
     \lastnamedcs
   \else
     \page_otr_command_routine
   \fi}

\let\page_otr_triggered_output_routine\page_otr_triggered_output_routine_normal

\appendtoks
    \let\page_otr_triggered_output_routine\page_otr_triggered_output_routine_traced
\to \t_page_otr_tracers

%D The real routine handler:

\ifdefined\everybeforeoutput \else \newtoks\everybeforeoutput \fi
\ifdefined\everyafteroutput  \else \newtoks\everyafteroutput  \fi

\def\page_otr_set_engine_output_routine#content%
  {\global\output
     {\inotrtrue
      \the\everybeforeoutput
      #content\relax
      \the\everyafteroutput}}

\page_otr_set_engine_output_routine\page_otr_triggered_output_routine

\installoutputroutine\synchronizeoutput % use \triggerpagebuilder instead
  {\ifvoid\normalpagebox\else
     \unvbox\normalpagebox
     % not \pagediscards as it does more harm than good
   \fi}

\installoutputroutine\discardpage
  {\setbox\scratchbox\box\normalpagebox}

% todo: \resetpagebreak -> everyejectpage

\def\page_otr_trigger_output_routine
  {\par
   \ifvmode
     \penalty\c_page_otr_eject_penalty
   \fi
   \resetpagebreak}

\def\page_otr_fill_and_eject_page
  {\par
   \ifvmode
     \vfill
     \penalty\c_page_otr_eject_penalty
   \fi
   \resetpagebreak}

\def\page_otr_eject_page
  {\par
   \ifvmode
     \ifdim\pagetotal>\pagegoal \else
       \normalvfil
     \fi
     \penalty\c_page_otr_eject_penalty
   \fi
   \resetpagebreak}

\def\page_otr_eject_page_and_flush_inserts % can be an installed one
  {\par
   \ifvmode
     \ifdim\pagetotal>\pagegoal \else
       \normalvfil
     \fi
     \penalty\c_page_otr_super_penalty
   \fi
   \resetpagebreak}

\def\page_otr_check_for_pending_inserts
  {\ifnum\outputpenalty>\c_page_otr_super_penalty \else
     \ifnum\insertpenalties>\zerocount
       % something is being held over so we force a new page
       \page_otr_force_another_page
     \fi
   \fi}

\def\page_otr_force_another_page
  {% we should actually remove the dummy line in the otr
   \hpack to \hsize{}%
   \kern-\topskip
   \nobreak
   \vfill
   \penalty\c_page_otr_super_penalty
   \resetpagebreak}

%D For those who've read the plain \TEX\ book, we provide the next macro:

\permanent\protected\def\bye
  {\writestatus\m!system{Sorry, you're not done yet, so no goodbye!}}

%D We define a few constants because that (1) provides some checking and (2) is
%D handier when aligning definitions (checks nicer). Most routines will use ard
%D codes names but sometimes we want to adapt, which is why we have these:

\definesystemconstant{page_otr_command_routine}
\definesystemconstant{page_otr_command_package_contents}
\definesystemconstant{page_otr_command_set_vsize}
\definesystemconstant{page_otr_command_set_hsize}
\definesystemconstant{page_otr_command_synchronize_hsize}
\definesystemconstant{page_otr_command_next_page}
\definesystemconstant{page_otr_command_next_page_and_inserts}
\definesystemconstant{page_otr_command_set_top_insertions}
\definesystemconstant{page_otr_command_set_bottom_insertions}
\definesystemconstant{page_otr_command_flush_top_insertions}
\definesystemconstant{page_otr_command_flush_bottom_insertions}
\definesystemconstant{page_otr_command_check_if_float_fits}
\definesystemconstant{page_otr_command_set_float_hsize}
\definesystemconstant{page_otr_command_flush_float_box}
\definesystemconstant{page_otr_command_side_float_output}
\definesystemconstant{page_otr_command_synchronize_side_floats}
\definesystemconstant{page_otr_command_flush_floats}
\definesystemconstant{page_otr_command_flush_side_floats}
\definesystemconstant{page_otr_command_flush_saved_floats}
\definesystemconstant{page_otr_command_flush_all_floats}
\definesystemconstant{page_otr_command_flush_margin_blocks}
\definesystemconstant{page_otr_command_test_column}

\definesystemconstant{singlecolumn}
\definesystemconstant{multicolumn}   % will move
\definesystemconstant{columnset}     % will move
\definesystemconstant{pagecolumn}    % will move

\defineoutputroutinecommand
  [\s!page_otr_command_routine,
   \s!page_otr_command_package_contents,
   \s!page_otr_command_set_vsize,
   \s!page_otr_command_set_hsize,
   \s!page_otr_command_synchronize_hsize, % for columns of different width
   \s!page_otr_command_next_page,
   \s!page_otr_command_next_page_and_inserts,
   \s!page_otr_command_set_top_insertions,
   \s!page_otr_command_set_bottom_insertions,
   \s!page_otr_command_flush_top_insertions,
   \s!page_otr_command_flush_bottom_insertions,
   \s!page_otr_command_check_if_float_fits,
   \s!page_otr_command_set_float_hsize,
   \s!page_otr_command_flush_float_box,
   \s!page_otr_command_side_float_output, % name will change as will hooks
   \s!page_otr_command_synchronize_side_floats,
   \s!page_otr_command_flush_floats,
   \s!page_otr_command_flush_side_floats,
   \s!page_otr_command_flush_saved_floats,
   \s!page_otr_command_flush_all_floats,
   \s!page_otr_command_flush_margin_blocks,
   \s!page_otr_command_test_column]

\appendtoks
    \setupoutputroutine[\s!singlecolumn]%
\to \everydump

\protect \endinput