summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/page-otr.mkvi
blob: 19c308c32affc893caf89dc7d0e5b996afd8574e (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
%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
%D same time we 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?

\unprotect

\def\m!otr{otr} % todo

\installcorenamespace{outputroutine}

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

\newtoks\t_page_otr_commands
\newtoks\t_page_otr_tracers

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

\unexpanded\def\page_otr_commands_define#name%
  {\ifcsname#name\endcsname \else
     \expandafter\let\csname#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

\unexpanded\def\page_otr_specifics_preset#1%
  {\edef\page_otr_specifics_command{\strictoutputroutineparameter{#1}}% no inheritance of commands
   \ifx\page_otr_specifics_command\empty
     \writestatus{\currentoutputroutine}{- \expandafter\strippedcsname\csname#1\endcsname}%
     \expandafter\let\csname#1\endcsname\relax
   \else
     \writestatus{\currentoutputroutine}{+ \expandafter\strippedcsname\csname#1\endcsname}%
     \expandafter\let\csname#1\expandafter\endcsname\page_otr_specifics_command
   \fi}

\unexpanded\def\page_otr_specifics_preset_normal#1%
  {\edef\page_otr_specifics_command{\strictoutputroutineparameter{#1}}% no inheritance of commands
   \ifx\page_otr_specifics_command\empty
     \expandafter\let\csname#1\endcsname\relax
   \else
     \expandafter\let\csname#1\expandafter\endcsname\page_otr_specifics_command
   \fi}

\unexpanded\def\page_otr_specifics_preset_traced#1%
  {\edef\page_otr_specifics_command{\strictoutputroutineparameter{#1}}% no inheritance of commands
   \ifx\page_otr_specifics_command\empty
     \writestatus{\currentoutputroutine}{preset: - \expandafter\strippedcsname\csname#1\endcsname}%
     \expandafter\let\csname#1\endcsname\relax
   \else
     \writestatus{\currentoutputroutine}{preset: + \expandafter\strippedcsname\csname#1\endcsname}%
     \expandafter\let\csname#1\expandafter\endcsname\page_otr_specifics_command
   \fi}

\let\page_otr_specifics_preset\page_otr_specifics_preset_normal

\unexpanded\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
%D the single column routine. Then there is a multicolumn variant
%D that can be used mixed, and a columnset variant that is more
%D 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_otf_trigger_penalty \c_page_otf_trigger_penalty -100010

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

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

\unexpanded\def\page_otr_message_s#1#2%
  {\writestatus
    \currentoutputroutine
    {#1\space             \space
     #2\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
     \ifdim\pagetotal>\pagegoal
       d:\the\dimexpr\pagetotal-\pagegoal\relax
     \fi}}

\unexpanded\def\page_otr_trigger#1%
  {\begingroup
   \par
   \penalty#1%
   \endgroup}

\unexpanded\def\installoutputroutine#1#2% \invoke \action
  {\global\advance\c_page_otf_trigger_penalty\minusone
   \edef#1{\page_otr_trigger{\number\c_page_otf_trigger_penalty}}%
   \setvalue{\??otrtriggers\number\c_page_otf_trigger_penalty}{#2}}

\unexpanded\def\page_otr_triggered_output_routine_traced
  {\ifcsname\??otrtriggers\the\outputpenalty\endcsname
     \page_otr_message_b{special}%
     \csname\??otrtriggers\the\outputpenalty\endcsname
     \page_otr_message_e{special}%
   \else
     \page_otr_message_b{normal}%
     \page_otr_command_routine
     \page_otr_message_e{normal}%
   \fi}

\unexpanded\def\page_otr_triggered_output_routine_normal
  {\ifcsname\??otrtriggers\the\outputpenalty\endcsname
     \csname\??otrtriggers\the\outputpenalty\endcsname
   \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_otf_set_engine_output_routine#1%
  {\global\output
     {\inotrtrue
      \the\everybeforeoutput
      #1\relax
      \the\everyafteroutput}}

\page_otf_set_engine_output_routine\page_otr_triggered_output_routine

\installoutputroutine\synchronizeoutput
  {\ifvoid\normalpagebox\else
     \unvbox\normalpagebox
     \pagediscards
   \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
   \hbox 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
%D macro:

\unexpanded\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
%D and (2) is handier when aligning definitions (checks nicer).

\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_margin_blocks}

\definesystemconstant{singlecolumn}
\definesystemconstant{multicolumn}
\definesystemconstant{columnset}

\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_margin_blocks]

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

\protect \endinput