page-txt.mkvi /size: 38 Kb    last modification: 2021-10-28 13:50
1% macros=mkvi
2
3%D \module
4%D   [       file=page-txt, % copied from main-001,
5%D        version=1997.03.31,
6%D          title=\CONTEXT\ Page Macros,
7%D       subtitle=Texts,
8%D         author=Hans Hagen,
9%D           date=\currentdate,
10%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
11%C
12%C This module is part of the \CONTEXT\ macro||package and is
13%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
14%C details.
15
16\writestatus{loading}{ConTeXt Page Macros / Texts}
17
18\unprotect
19
20\newtoks\toptextcontent     \newtoks\leftedgetextcontent
21\newtoks\headertextcontent  \newtoks\leftmargintextcontent
22\newtoks\footertextcontent  \newtoks\rightmargintextcontent
23\newtoks\bottomtextcontent  \newtoks\rightedgetextcontent
24
25\newtoks\texttextcontent
26
27%D \macros
28%D  {setuptop, setupheader, setuptext,setupfooter, setupbottom}
29%D
30%D The macros in this module sometimes look a bit more complicated than needed,
31%D which is a direct result of the fact that their ancestors are quite old and
32%D upward compatibility is a must.
33%D
34%D \showsetup{setuptop}
35%D \showsetup{setupheader}
36%D \showsetup{setuptext}
37%D \showsetup{setupfooter}
38%D \showsetup{setupbottom}
39
40\installcorenamespace{layouttexts}
41\installcorenamespace{layouttextsline}
42\installcorenamespace{layouttextsreset}
43\installcorenamespace{layouttextssynchronize}
44\installcorenamespace{layouttextstrut}
45\installcorenamespace{layouttextspecial}
46\installcorenamespace{layouttextcontent}
47
48\installcommandhandler \??layouttexts {layoutelement} \??layouttexts
49
50% \appendtoks
51%     \resetlayoutelementparameter\c!lefttext   % resolves better
52%     \resetlayoutelementparameter\c!middletext
53%     \resetlayoutelementparameter\c!righttext
54% \to \everydefinelayoutelement
55
56\definelayoutelement[\v!top   ]
57\definelayoutelement[\v!header]
58\definelayoutelement[\v!text  ]
59\definelayoutelement[\v!footer]
60\definelayoutelement[\v!bottom]
61
62\definelayoutelement[\v!top   :\v!text]  [\v!top   ][\c!lefttext=,\c!middletext=,\c!righttext=]
63\definelayoutelement[\v!header:\v!text]  [\v!header][\c!lefttext=,\c!middletext=,\c!righttext=]
64\definelayoutelement[\v!text  :\v!text]  [\v!text  ][\c!lefttext=,\c!middletext=,\c!righttext=]
65\definelayoutelement[\v!footer:\v!text]  [\v!footer][\c!lefttext=,\c!middletext=,\c!righttext=]
66\definelayoutelement[\v!bottom:\v!text]  [\v!bottom][\c!lefttext=,\c!middletext=,\c!righttext=]
67
68\definelayoutelement[\v!top   :\v!margin][\v!top   ][\c!lefttext=,\c!middletext=,\c!righttext=]
69\definelayoutelement[\v!header:\v!margin][\v!header][\c!lefttext=,\c!middletext=,\c!righttext=]
70\definelayoutelement[\v!text  :\v!margin][\v!text  ][\c!lefttext=,\c!middletext=,\c!righttext=]
71\definelayoutelement[\v!footer:\v!margin][\v!footer][\c!lefttext=,\c!middletext=,\c!righttext=]
72\definelayoutelement[\v!bottom:\v!margin][\v!bottom][\c!lefttext=,\c!middletext=,\c!righttext=]
73
74\definelayoutelement[\v!top   :\v!edge]  [\v!top   ][\c!lefttext=,\c!middletext=,\c!righttext=]
75\definelayoutelement[\v!header:\v!edge]  [\v!header][\c!lefttext=,\c!middletext=,\c!righttext=]
76\definelayoutelement[\v!text  :\v!edge]  [\v!text  ][\c!lefttext=,\c!middletext=,\c!righttext=]
77\definelayoutelement[\v!footer:\v!edge]  [\v!footer][\c!lefttext=,\c!middletext=,\c!righttext=]
78\definelayoutelement[\v!bottom:\v!edge]  [\v!bottom][\c!lefttext=,\c!middletext=,\c!righttext=]
79
80\unexpanded\def\setuplayouttext
81  {\dotripleempty\page_layouts_setup_text}
82
83\def\page_layouts_setup_text[#vertical][#horizontal][#settings]%
84  {\ifthirdargument
85     \setuplayoutelement[#vertical:#horizontal][#settings]%
86   \else
87     \setuplayoutelement[#vertical][#horizontal]%
88   \fi}
89
90\appendtoks
91    \ifx\currentlayoutelement\empty\else
92        \page_layouts_synchronize_element\currentlayoutelement % brr, can be vertical:horizontal
93    \fi
94\to \everysetuplayoutelement
95
96\def\page_layouts_reset_element_status#vertical%
97  {\expandafter\normalgdef\csname\??layouttextsreset#vertical\endcsname{\page_layouts_set_element_status_normal#vertical}}
98
99\def\page_layouts_set_element_status_normal#vertical%
100  {\expandafter\glet\csname\namedlayoutelementhash#vertical\c!state\endcsname\v!normal
101   \expandafter\glet\csname\??layouttextsreset#vertical\endcsname\relax
102   \page_layouts_synchronize_element{#vertical}}
103
104\def\page_layouts_synchronize_element#vertical%
105  {\xdef\previoustextstate{\csname\??layouttextssynchronize#vertical\endcsname}% can be a let
106   \edef\currenttextstate {\namedlayoutelementparameter{#vertical}\c!state}%
107  %\writestatus{>>}{[#vertical:\currenttextstate/\previoustextstate]}%
108   \ifx\currenttextstate\previoustextstate \else
109     \page_layouts_synchronize_element_indeed{#vertical}%
110   \fi}
111
112\def\page_layouts_synchronize_element_indeed#vertical%
113  {\ifx\currenttextstate \v!high \calculatevsizes\page_backgrounds_recalculate \else
114   \ifx\previoustextstate\v!high \calculatevsizes\page_backgrounds_recalculate \else
115   \ifx\currenttextstate \v!none \calculatevsizes\page_backgrounds_recalculate \else
116   \ifx\previoustextstate\v!none \calculatevsizes\page_backgrounds_recalculate \fi\fi\fi\fi
117   \letgvalue{\??layouttextssynchronize#vertical}\currenttextstate}
118
119\unexpanded\def\setuptop   {\dotripleempty\page_layouts_setup_text[\v!top   ]}
120\unexpanded\def\setupheader{\dotripleempty\page_layouts_setup_text[\v!header]}
121\unexpanded\def\setuptext  {\dotripleempty\page_layouts_setup_text[\v!text  ]}
122\unexpanded\def\setupfooter{\dotripleempty\page_layouts_setup_text[\v!footer]}
123\unexpanded\def\setupbottom{\dotripleempty\page_layouts_setup_text[\v!bottom]}
124
125%D We inherit some settings:
126
127\setuplayoutelement
128  [ \c!leftstyle=\layoutelementparameter\c!style,
129   \c!rightstyle=\layoutelementparameter\c!style,
130    \c!leftcolor=\layoutelementparameter\c!color,
131   \c!rightcolor=\layoutelementparameter\c!color,
132    \c!leftwidth=\layoutelementparameter\c!width,
133   \c!rightwidth=\layoutelementparameter\c!width]
134
135%D \macros
136%D  {noheaderandfooterlines,notopandbottomlines}
137%D
138%D Although not really needed, the following shortcuts sometimes come in handy.
139%D
140%D \showsetup{noheaderandfooterlines}
141%D \showsetup{notopandbottomlines}
142
143\unexpanded\def\noheaderandfooterlines
144  {\setuplayoutelement[\v!header][\c!state=\v!empty]%
145   \setuplayoutelement[\v!footer][\c!state=\v!empty]}
146
147\unexpanded\def\notopandbottomlines
148  {\setuplayoutelement[\v!top   ][\c!state=\v!empty]%
149   \setuplayoutelement[\v!bottom][\c!state=\v!empty]}
150
151%D \macros
152%D  {setuptoptexts,setupheadertexts,setuptexttexts,setupfootertexts,setupbottomtexts}
153%D
154%D The next macros take one or more arguments. The exact setup depends on the number
155%D of arguments. Although not that intuitive, the current scheme evolved out of the
156%D original. When margin and edge texts as well as middle texts showed up, the
157%D current odd|/|even scheme surfaced.
158%D
159%D \showsetup{setuptoptexts}
160%D \showsetup{setupheadertexts}
161%D \showsetup{setuptexttexts}
162%D \showsetup{setupfootertexts}
163%D \showsetup{setupbottomtexts}
164
165\unexpanded\def\setuptoptexts   {\dosixtupleempty\page_layouts_setup_texts[\v!top    ]}
166\unexpanded\def\setupheadertexts{\dosixtupleempty\page_layouts_setup_texts[\v!header ]}
167\unexpanded\def\setuptexttexts  {\dosixtupleempty\page_layouts_setup_texts[\v!text   ]}
168\unexpanded\def\setupfootertexts{\dosixtupleempty\page_layouts_setup_texts[\v!footer ]}
169\unexpanded\def\setupbottomtexts{\dosixtupleempty\page_layouts_setup_texts[\v!bottom ]}
170
171\unexpanded\def\page_layouts_setup_text_six#vertical#horizontal#a#b#c#d%
172  {\edef\currentlayoutelement{#vertical:#horizontal}%
173   \setlayoutelementparameter\c!lefttext
174     {\page_layouts_process_element_double
175        \c!leftstyle \c!leftcolor \c!leftwidth {#a}%
176        \c!rightstyle\c!rightcolor\c!rightwidth{#d}}%
177   \setlayoutelementparameter\c!righttext
178     {\page_layouts_process_element_double
179        \c!rightstyle\c!rightcolor\c!rightwidth{#b}%
180        \c!leftstyle \c!leftcolor \c!leftwidth {#c}}}
181
182\unexpanded\def\page_layouts_setup_text_five#vertical#horizontal#a#b#c%
183  {\edef\currentlayoutelement{#vertical:\v!text}%
184   \setlayoutelementparameter\c!lefttext
185     {\page_layouts_process_element_double
186        \c!leftstyle \c!leftcolor \c!leftwidth {#horizontal}%
187        \c!rightstyle\c!rightcolor\c!rightwidth{#c}}%
188   \setlayoutelementparameter\c!righttext
189     {\page_layouts_process_element_double
190        \c!rightstyle\c!rightcolor\c!rightwidth{#a}%
191        \c!leftstyle \c!leftcolor \c!leftwidth {#b}}}
192
193\unexpanded\def\page_layouts_setup_text_four#vertical#horizontal#a#b%
194  {\edef\currentlayoutelement{#vertical:#horizontal}%
195   \doifelsenothing{\detokenize{#a}}
196     {\resetlayoutelementparameter\c!lefttext}
197     {\setlayoutelementparameter\c!lefttext
198        {\page_layouts_process_element_double
199           \c!leftstyle\c!leftcolor\c!leftwidth{#a}%
200           \c!leftstyle\c!leftcolor\c!leftwidth{#a}}}%
201   \doifelsenothing{\detokenize{#b}}
202     {\resetlayoutelementparameter\c!righttext}
203     {\setlayoutelementparameter\c!righttext
204        {\page_layouts_process_element_double
205           \c!rightstyle\c!rightcolor\c!rightwidth{#b}%
206           \c!rightstyle\c!rightcolor\c!rightwidth{#b}}}}
207
208\unexpanded\def\page_layouts_setup_text_three#vertical#horizontal#a%
209  {\edef\currentlayoutelement{#vertical:\v!text}%
210   \doifelsenothing{\detokenize{#horizontal}}
211     {\resetlayoutelementparameter\c!lefttext}
212     {\setlayoutelementparameter\c!lefttext
213        {\page_layouts_process_element_double
214           \c!leftstyle\c!leftcolor\c!leftwidth{#horizontal}%
215           \c!leftstyle\c!leftcolor\c!leftwidth{#horizontal}}}%
216   \doifelsenothing{\detokenize{#a}}
217     {\resetlayoutelementparameter\c!righttext}
218     {\setlayoutelementparameter\c!righttext
219        {\page_layouts_process_element_double
220           \c!rightstyle\c!rightcolor\c!rightwidth{#a}%
221           \c!rightstyle\c!rightcolor\c!rightwidth{#a}}}}
222
223\unexpanded\def\page_layouts_setup_text_two#vertical#horizontal%
224  {\edef\currentlayoutelement{#vertical:\v!text}%
225   \resetlayoutelementparameter\c!lefttext
226   \resetlayoutelementparameter\c!righttext
227   \doifelsenothing{\detokenize{#horizontal}}
228     {\resetlayoutelementparameter\c!middletext}
229     {\setlayoutelementparameter\c!middletext
230        {\page_layouts_process_element_single\c!style\c!color\c!width{#horizontal}}}}
231
232\unexpanded\def\page_layouts_setup_text_one#vertical%
233  {\edef\currentlayoutelement{#vertical:\v!text}%
234   \resetlayoutelementparameter\c!lefttext
235   \resetlayoutelementparameter\c!righttext
236   \resetlayoutelementparameter\c!middletext
237   \edef\currentlayoutelement{#vertical:\v!margin}%
238   \resetlayoutelementparameter\c!lefttext
239   \resetlayoutelementparameter\c!righttext
240   \resetlayoutelementparameter\c!middletext
241   \edef\currentlayoutelement{#vertical:\v!edge}%
242   \resetlayoutelementparameter\c!lefttext
243   \resetlayoutelementparameter\c!righttext
244   \resetlayoutelementparameter\c!middletext}
245
246\unexpanded\def\page_layouts_setup_texts[#vertical][#horizontal][#a][#b][#c][#d]%
247  {\ifsixthargument \page_layouts_setup_text_six  {#vertical}{#horizontal}{#a}{#b}{#c}{#d}\else
248   \iffifthargument \page_layouts_setup_text_five {#vertical}{#horizontal}{#a}{#b}{#c}\else
249   \iffourthargument\page_layouts_setup_text_four {#vertical}{#horizontal}{#a}{#b}\else
250   \ifthirdargument \page_layouts_setup_text_three{#vertical}{#horizontal}{#a}\else
251   \ifsecondargument\page_layouts_setup_text_two  {#vertical}{#horizontal}\else
252                    \page_layouts_setup_text_one  {#vertical}\fi\fi\fi\fi\fi}
253
254%D Left and right texts are swapped on odd and even pages, but only when double
255%D sided typesetting is enabled.
256
257\unexpanded\def\page_layouts_process_element_double
258  {\doifelseoddpage
259     \page_layouts_process_element_double_odd
260     \page_layouts_process_element_double_even}
261
262\def\page_layouts_process_element_double_odd #lstyle#lcolor#lwidth#lcontent#rstyle#rcolor#rwidth#rcontent%
263  {\page_layouts_process_element_single#lstyle#lcolor#lwidth{#lcontent}}
264
265\def\page_layouts_process_element_double_even#lstyle#color#lwidth#lcontent#rstyle#rcolor#rwidth#rcontent%
266  {\page_layouts_process_element_single#rstyle#rcolor#rwidth{#rcontent}}
267
268%D The next macro will be cleaned up and made less messy and dependent.
269
270\let\m_page_layouts_element_content\empty
271
272\unexpanded\def\page_layouts_process_element_single#style#color#width#content%
273  {\edef\m_page_layouts_element_content{\detokenize{#content}}% so no \v!xxx
274   \ifx\m_page_layouts_element_content\empty
275     % should not happen too often
276   \else
277     \page_layouts_process_element_single_indeed#style#color#width{#content}%
278   \fi}
279
280\setvalue{\??layouttextstrut\v!yes}{\setstrut\strut} % maybe more variants
281
282\def\page_layouts_process_element_single_indeed#style#color#width#content%
283  {\begingroup
284   \uselayoutelementstyleandcolor#style#color%
285   \csname\??layouttextstrut\layoutelementparameter\c!strut\endcsname
286   \ifcsname\??layouttextspecial\m_page_layouts_element_content\endcsname
287     \lastnamedcs
288   \else
289     \edef\currentlayoutelementwidth{\layoutelementparameter#width}%
290     \ifx\currentlayoutelementwidth\empty
291       \expandafter\page_layouts_process_element_single_normal
292     \else
293       \expandafter\page_layouts_process_element_single_limited
294     \fi{#content}%
295   \fi
296   \endgroup}
297
298% {}{}{} prevents lookahead issues ... this will go away
299
300\def\page_layouts_process_element_single_normal#content%
301  {\doifelsemarking\m_page_layouts_element_content
302     {\getmarking[\m_page_layouts_element_content][\v!first]}
303     {\ignorecrlf#content{}{}{}}}
304
305\def\page_layouts_process_element_single_limited#content%
306  {\doifelsemarking\m_page_layouts_element_content
307     {\limitatetext{\getmarking[\m_page_layouts_element_content][\v!first]}\currentlayoutelementwidth\unknown}
308     {\ignorecrlf\limitatetext{#content{}{}{}}\currentlayoutelementwidth\unknown}}
309
310\setvalue{\??layouttextspecial\v!pagenumber}{\page_layouts_place_page_number}
311\setvalue{\??layouttextspecial\v!date      }{\currentdate}
312
313%D When specified, the texts are automatically limited in length.
314
315\appendtoks \page_layouts_place_text_line\v!top   \topheight    \to \toptextcontent
316\appendtoks \page_layouts_place_text_line\v!header\headerheight \to \headertextcontent
317\appendtoks \page_layouts_place_text_line\v!text  \textheight   \to \texttextcontent
318\appendtoks \page_layouts_place_text_line\v!footer\footerheight \to \footertextcontent
319\appendtoks \page_layouts_place_text_line\v!bottom\bottomheight \to \bottomtextcontent
320
321%D Texts can be disabled, moved up and ignored, depending in the \type {status}
322%D variable. This is handled by the next couple of macros.
323
324\newcount\c_page_layouts_element_state_n
325
326\def\page_layouts_set_element_status#vertical%
327  {\c_page_layouts_element_state_n=0\namedlayoutelementparameter#vertical\c!n\relax
328   \ifcase\c_page_layouts_element_state_n
329     \edef\textlinestatus{\namedlayoutelementparameter#vertical\c!state}%
330   \else
331     \setxvalue{\namedlayoutelementhash#vertical\c!n}{\the\numexpr\c_page_layouts_element_state_n+\minusone}%
332     \let\textlinestatus\v!stop
333   \fi}
334
335\appendtoks
336   \doifinset\v!header\floatspecification{\setxvalue{\namedlayoutelementhash\v!header\c!n}{1}}%
337   \doifinset\v!footer\floatspecification{\setxvalue{\namedlayoutelementhash\v!footer\c!n}{1}}%
338\to \everybeforeflushedpagefloat
339
340\unexpanded\def\page_layouts_place_text_line#vertical%
341  {\page_layouts_set_element_status#vertical\relax
342   \ifcsname\??layouttextsline\textlinestatus\endcsname
343     \expandafter\lastnamedcs
344   \else
345     \expandafter\page_layouts_place_text_line_unknown
346   \fi#vertical}
347
348\unexpanded\def\doifelselayouttextline#vertical% shown or not
349  {\edef\currentlayoutelementstate{\namedlayoutelementparameter{#vertical}\c!state}%
350   \ifx\currentlayoutelementstate\v!normal
351      \expandafter\firstoftwoarguments
352   \else\ifx\currentlayoutelementstate\v!start
353      \doubleexpandafter\firstoftwoarguments
354   \else
355      \doubleexpandafter\secondoftwoarguments
356   \fi\fi}
357
358\unexpanded\def\doifelselayoutsomeline#vertical% present or not
359  {\edef\currentlayoutelementstate{\namedlayoutelementparameter{#vertical}\c!state}%
360   \ifx\currentlayoutelementstate\v!none
361     \expandafter\secondoftwoarguments
362   \else\ifx\currentlayoutelementstate\v!high
363     \doubleexpandafter\secondoftwoarguments
364   \else
365     \doubleexpandafter\firstoftwoarguments
366   \fi\fi}
367
368\let\doiflayouttextlineelse\doifelselayouttextline
369\let\doiflayoutsomelineelse\doifelselayoutsomeline
370
371\newconditional\resyncaftertextline
372
373\setvalue{\??layouttextsline\v!normal}{\page_layouts_place_text_line_indeed}
374\setvalue{\??layouttextsline\empty   }{\page_layouts_place_text_line_indeed}
375
376\letvalue{\??layouttextsline\v!none}\gobbletwoarguments
377\letvalue{\??layouttextsline\v!stop}\gobbletwoarguments
378
379\setvalue{\??layouttextsline\v!high}#vertical#height%
380  {\global\settrue\resyncaftertextline
381   \page_layouts_reset_element_status#vertical}
382
383\setvalue{\??layouttextsline\v!empty}#vertical#height%
384  {\page_layouts_reset_element_status#vertical}
385
386\setvalue{\??layouttextsline\v!start}#vertical#height%
387  {\page_layouts_reset_element_status#vertical%
388   \page_layouts_place_text_line_indeed#vertical#height}
389
390\setvalue{\??layouttextsline\v!nomarking}#vertical#height%
391  {\bgroup
392   \page_layouts_reset_element_status#vertical%
393   \settrue\inhibitgetmarking
394   \page_layouts_place_text_line_indeed#vertical#height%
395   \egroup}
396
397% \setupheadertexts            [11]
398% \definetext [title] [header] [aa]
399% \setupheadertexts            [11] [22]
400% \definetext [title] [header] [aa] [bb]
401% \setupheadertexts            [text] [11] [22]
402% \definetext [title] [header] [text] [aa] [bb]
403% \setupheadertexts            [11] [22] [33] [44]
404% \definetext [title] [header] [aa] [bb] [cc] [dd]
405% \setupheadertexts            [text] [11] [22] [33] [44]
406% \definetext [title] [header] [text] [aa] [bb] [cc] [dd]
407
408\def\page_layouts_place_text_line_unknown#vertical#height%
409  {\global\settrue\resyncaftertextline
410   \begingroup % new
411   \page_layouts_reset_element_status#vertical%
412   \begincsname\namedlayoutelementhash{#vertical}\textlinestatus\endcsname
413   \begincsname\namedlayoutelementhash{#vertical:\v!text}\textlinestatus\endcsname
414   \begincsname\namedlayoutelementhash{#vertical:\v!margin}\textlinestatus\endcsname
415   \begincsname\namedlayoutelementhash{#vertical:\v!edge}\textlinestatus\endcsname
416   \page_layouts_place_text_line_indeed#vertical#height%
417   \endgroup}
418
419\letvalue{\??layouttextsline\s!unknown}\page_layouts_place_text_line_unknown
420
421%D The following macro has to be called after a page is flushed.
422
423\unexpanded\def\resetlayouttextlines % public
424  {\begincsname\??layouttextsreset\v!top   \endcsname
425   \begincsname\??layouttextsreset\v!header\endcsname
426   \begincsname\??layouttextsreset\v!text  \endcsname
427   \begincsname\??layouttextsreset\v!footer\endcsname
428   \begincsname\??layouttextsreset\v!bottom\endcsname
429   \ifconditional\resyncaftertextline
430     \calculateglobalvsizes
431     \page_backgrounds_recalculate
432     \global\setfalse\resyncaftertextline
433   \fi}
434
435\def\getspecificlayouttext#vertical#horizontal#what%
436  {\begincsname\namedlayoutelementhash{#vertical:#horizontal}#what\endcsname}
437
438% \settext[header][text][middle][xxx][yyy]
439
440\unexpanded\def\settextcontent
441  {\doquintupleempty\page_layouts_set_text_content}
442
443\def\page_layouts_set_text_content[#vertical][#horizontal][#one][#two][#three]% header text middle text/text
444  {\iffifthargument
445     \setvalue{\namedlayoutelementhash{#vertical:#horizontal}\executeifdefined{\??layouttextcontent\v!text:#one}\c!middletext}%
446       {\page_layouts_process_element_double
447          \c!leftstyle \c!leftcolor \c!leftwidth {#two}%
448          \c!rightstyle\c!rightcolor\c!rightwidth{#three}}%
449   \else\iffourthargument
450     \setvalue{\namedlayoutelementhash{#vertical:#horizontal}\executeifdefined{\??layouttextcontent\v!text:#one}\c!middletext}%
451       {\page_layouts_process_element_double
452          \c!leftstyle \c!leftcolor \c!leftwidth {#two}%
453          \c!rightstyle\c!rightcolor\c!rightwidth{#two}}%
454   \else\ifthirdargument
455     \setvalue{\namedlayoutelementhash{#vertical:#horizontal}\c!middletext}%
456       {\page_layouts_process_element_double
457          \c!leftstyle \c!leftcolor \c!leftwidth {#one}%
458          \c!rightstyle\c!rightcolor\c!rightwidth{#one}}%
459   \fi\fi\fi}
460
461\let\currentlayoutelement\relax
462
463\unexpanded\def\resettextcontent
464  {\dotripleempty\page_layouts_reset_text_content}
465
466% \def\page_layouts_reset_text_content[#vertical][#horizontal][#tag]% header text middle
467%   {\edef\currentlayoutelement{#vertical:#horizontal}%
468%    \ifthirdargument
469%      \letvalueempty{\layoutelementhash\executeifdefined{\??layouttextcontent\v!text:#tag}\c!middletext}%
470%    \else\ifsecondargument
471%      \resetlayoutelementparameter\c!lefttext
472%      \resetlayoutelementparameter\c!middletext
473%      \resetlayoutelementparameter\c!righttext
474%    \fi\fi}
475
476\def\page_layouts_reset_text_content[#vertical][#horizontal][#tag]% header text middle
477  {\ifthirdargument
478     \letvalueempty{\namedlayoutelementhash{#vertical:#horizontal}\executeifdefined{\??layouttextcontent\v!text:#tag}\c!middletext}%
479   \else\ifsecondargument
480     \resetlayoutelementparameter\c!lefttext
481     \resetlayoutelementparameter\c!middletext
482     \resetlayoutelementparameter\c!righttext
483   \fi\fi}
484
485\letvalue{\??layouttextcontent\v!text:\c!middle}\c!middletext
486\letvalue{\??layouttextcontent\v!text:\c!left  }\c!lefttext
487\letvalue{\??layouttextcontent\v!text:\c!right }\c!righttext
488
489%D The placement of a whole line is handled by the next two macros. These are hooked
490%D into the general purpose token list registers mentioned before.
491
492\let\currentlayouttextline\relax
493
494\def\page_layouts_place_text_line_indeed#vertical#height%
495  {\let\currentlayouttextline#vertical%
496   \ifdim#height>\zeropoint\relax  % prevents pagenumbers when zero height
497     \page_layouts_place_text_line_left_or_right{#height}%
498   \fi}
499
500\def\page_layouts_place_text_line_left_or_right#height%
501  {\goleftonpage
502   \setbox\b_page_layouts_element\vbox to #height
503     {\vsize#height\relax
504     %\hsize\zeropoint % hack so that e.g. after=\hairline gives predictable results
505      \hsize\totaltextwidth
506      \normalbaselines
507      \let\\\ignoredlinebreak
508      \let\crlf\ignoredlinebreak
509      \namedlayoutelementparameter\currentlayouttextline\c!before
510      \doifbothsidesoverruled
511        \page_layouts_place_text_line_right
512        \page_layouts_place_text_line_right
513        \page_layouts_place_text_line_left
514      \namedlayoutelementparameter\currentlayouttextline\c!after
515      \vkern\zeropoint}% keep the \dp, beware of \vtops, never change this!
516   \dp\b_page_layouts_element\zeropoint
517   \box\b_page_layouts_element
518   \vkern-#height\relax}
519
520\let\page_layouts_extra_at_margin_left \plusone
521\let\page_layouts_extra_at_margin_right\plustwo
522
523\let\page_layouts_place_extra_text_left \relax % historic
524\let\page_layouts_place_extra_text_right\relax % historic
525
526\def\page_layouts_place_text_line_right
527  {\hpack
528     {\ifdim\leftedgewidth>\zeropoint
529        \page_layouts_left_edge_element\c!lefttext
530      \fi
531      \ifdim\leftmarginwidth>\zeropoint
532       %\page_layouts_left_margin_element\c!lefttext\page_layouts_extra_at_margin_left
533        \page_layouts_left_margin_element\c!lefttext\page_layouts_extra_at_margin_right
534      \fi
535      \ifdim\makeupwidth>\zeropoint
536        \page_layouts_text_body_element\c!lefttext\c!middletext\c!righttext\page_layouts_extra_at_margin_left
537      \fi
538      \ifdim\rightmarginwidth>\zeropoint
539       %\page_layouts_right_margin_element\c!righttext\page_layouts_extra_at_margin_left
540        \page_layouts_right_margin_element\c!righttext\page_layouts_extra_at_margin_right
541      \fi
542      \ifdim\rightedgewidth>\zeropoint
543       %\page_layouts_right_edge_element\c!lefttext
544        \page_layouts_right_edge_element\c!righttext
545      \fi}}
546
547\def\page_layouts_place_text_line_left
548  {\hpack
549     {\ifdim\leftedgewidth>\zeropoint
550        \page_layouts_left_edge_element\c!righttext
551      \fi
552      \ifdim\leftmarginwidth>\zeropoint
553       %\page_layouts_left_margin_element\c!righttext\page_layouts_extra_at_margin_right
554        \page_layouts_left_margin_element\c!righttext\page_layouts_extra_at_margin_left
555      \fi
556      \ifdim\makeupwidth>\zeropoint
557        \page_layouts_text_body_element\c!righttext\c!middletext\c!lefttext\page_layouts_extra_at_margin_right
558      \fi
559      \ifdim\rightmarginwidth>\zeropoint
560       %\page_layouts_right_margin_element\c!lefttext\page_layouts_extra_at_margin_right
561        \page_layouts_right_margin_element\c!lefttext\page_layouts_extra_at_margin_left
562      \fi
563      \ifdim\rightedgewidth>\zeropoint
564       %\page_layouts_right_edge_element\c!righttext
565        \page_layouts_right_edge_element\c!lefttext
566      \fi}}
567
568\def\page_layouts_left_edge_element#parameter%
569  {\edef\currentlayoutelement{\currentlayouttextline:\v!edge}%
570   \page_layouts_place_element_indeed\leftedgewidth
571     {\hss\layoutelementparameter#parameter}%
572   \kern\leftedgedistance}
573
574\def\page_layouts_left_margin_element#parameter#extrastate%
575  {\edef\currentlayoutelement{\currentlayouttextline:\v!margin}%
576   \page_layouts_place_element_indeed\leftmarginwidth
577     {\hbox to \leftmarginwidth{\hss\layoutelementparameter#parameter}%
578      \ifnum#extrastate=\page_layouts_extra_at_margin_left
579        \kern-\leftmarginwidth
580        \hbox to \leftmarginwidth{\hss\layoutelementparameter\c!margintext}%
581      \fi}%
582   \kern\leftmargindistance}
583
584\def\page_layouts_text_body_element#left#middle#right#extrastate%
585  {\edef\currentlayoutelement{\currentlayouttextline:\v!text}%
586   \page_layouts_place_element_indeed\makeupwidth
587     {\hbox to \makeupwidth{\ifnum#extrastate=\page_layouts_extra_at_margin_left\page_layouts_place_extra_text_left\fi\layoutelementparameter#left\hss}%
588      \kern-\makeupwidth
589      \hbox to \makeupwidth{\hss\layoutelementparameter#middle\hss}%
590      \kern-\makeupwidth
591      \hbox to \makeupwidth{\hss\layoutelementparameter#right\ifnum#extrastate=\page_layouts_extra_at_margin_right\page_layouts_place_extra_text_right\fi}}}
592
593\def\page_layouts_right_margin_element#parameter#extrastate%
594  {\edef\currentlayoutelement{\currentlayouttextline:\v!margin}%
595   \kern\rightmargindistance
596   \page_layouts_place_element_indeed\rightmarginwidth
597     {\hbox to \rightmarginwidth{\layoutelementparameter#parameter\hss}%
598      \ifnum#extrastate=\page_layouts_extra_at_margin_right
599        \kern-\rightmarginwidth
600        \hbox to \rightmarginwidth{\layoutelementparameter\c!margintext\hss}%
601      \fi}}
602
603\def\page_layouts_right_edge_element#parameter%
604  {\edef\currentlayoutelement{\currentlayouttextline:\v!edge}%
605   \kern\rightedgedistance
606   \page_layouts_place_element_indeed\rightedgewidth
607     {\layoutelementparameter#parameter\hss}}
608
609\def\page_layouts_place_element_indeed#width#content%
610  {\vbox % to \vsize
611     {\hsize#width\relax
612      \layoutelementparameter\c!before
613      \setlayoutcomponentattribute\currentlayoutelement
614      \hbox \layoutcomponentboxattribute to #width{#content}%
615      \layoutelementparameter\c!after}}
616
617%D Although it is far better to use backgrounds for this purpose, one can add a rule
618%D in the following way. This method makes the rules disappear in case of an empty
619%D text line. Consider this a feature.
620%D
621%D \starttyping
622%D \setupheadertexts[left][right]
623%D
624%D \setupheader[text][after=\hrule,style=bold]
625%D
626%D \starttext
627%D   \input tufte \page
628%D   \setupheader[state=empty]
629%D   \input tufte \page
630%D \stoptext
631%D \stoptyping
632
633%D This code will move to \type {page-flt.tex}.
634
635\appendtoks \placerightmarginblock \kern-\rightmarginwidth \to \rightmargintextcontent
636\appendtoks \placeleftmarginblock  \kern-\leftmarginwidth  \to \leftmargintextcontent
637
638%D \macros
639%D   {definetext}
640%D
641%D Some macros ago, we implemented the \type {status} option \type {unknown}. This
642%D one is used to take care of symbolic texts handlers.
643%D
644%D \showsetup{definetext}
645%D
646%D The next example demonstrates how we can use this mechanism to provide page
647%D (event) dependent text lines.
648%D
649%D \starttyping
650%D \definetext[chapter][footer][pagenumber]
651%D \setuphead[chapter][header=high,footer=chapter]
652%D \setupheadertexts[pagenumber]
653%D \setupfootertexts[left][right]
654%D \chapter{eerste} \dorecurse{20}{\input tufte \relax}
655%D \chapter{tweede} \dorecurse{20}{\input tufte \relax}
656%D \stoptyping
657
658\unexpanded\def\definetext
659  {\doseventupleempty\page_layouts_define_text}
660
661\def\page_layouts_define_text[#tag][#vertical][#horizontal][#a][#b][#c][#d]%
662  {\ifseventhargument
663     \setvalue{\namedlayoutelementhash{#vertical:#horizontal}#tag}{\page_layouts_setup_text_six  {#vertical}{#horizontal}{#a}{#b}{#c}{#d}}%
664   \else\ifsixthargument
665     \setvalue{\namedlayoutelementhash            {#vertical}#tag}{\page_layouts_setup_text_five {#vertical}{#horizontal}{#a}{#b}{#c}}%
666   \else\iffifthargument
667     \setvalue{\namedlayoutelementhash{#vertical:#horizontal}#tag}{\page_layouts_setup_text_four {#vertical}{#horizontal}{#a}{#b}}%
668   \else\iffourthargument
669     \setvalue{\namedlayoutelementhash            {#vertical}#tag}{\page_layouts_setup_text_three{#vertical}{#horizontal}{#a}}%
670   \else
671     \setvalue{\namedlayoutelementhash            {#vertical}#tag}{\page_layouts_setup_text_two  {#vertical}{#horizontal}}%
672   \fi\fi\fi\fi}
673
674%D A few more page breakers:
675
676\installpagebreakmethod \v!empty
677  {\page_otr_flush_all_floats
678   \page_otr_command_next_page
679   \doifnot{\namedlayoutelementparameter\v!header\c!state}\v!stop{\setuplayoutelement[\v!header][\c!state=\v!empty]}%
680   \doifnot{\namedlayoutelementparameter\v!footer\c!state}\v!stop{\setuplayoutelement[\v!footer][\c!state=\v!empty]}%
681   \page_otr_insert_dummy_page}
682
683\installpagebreakmethod \v!header
684  {\doifnot{\namedlayoutelementparameter\v!header\c!state}\v!stop{\setuplayoutelement[\v!header][\c!state=\v!empty]}}
685
686\installpagebreakmethod \v!footer
687  {\doifnot{\namedlayoutelementparameter\v!footer\c!state}\v!stop{\setuplayoutelement[\v!footer][\c!state=\v!empty]}}
688
689%D While the header and footer lines are moved away from the main text, the top and
690%D bottom lines are centered.
691
692\setuplayoutelement[\v!top   ][\c!state=\v!normal,\c!n=0,\c!before=\vss,\c!after=\vss,\c!strut=]
693\setuplayoutelement[\v!header][\c!state=\v!normal,\c!n=0,\c!before=,    \c!after=\vss,\c!strut=\v!yes]
694\setuplayoutelement[\v!text  ][\c!state=\v!normal,\c!n=0,\c!before=\vss,\c!after=\vss,\c!strut=]
695\setuplayoutelement[\v!footer][\c!state=\v!normal,\c!n=0,\c!before=\vss,\c!after=,    \c!strut=\v!yes]
696\setuplayoutelement[\v!bottom][\c!state=\v!normal,\c!n=0,\c!before=\vss,\c!after=\vss,\c!strut=]
697
698%D Moved here from strc-pag:
699%D
700%D We reset a previous location but only when it has a pagenumber associated. This
701%D is a rather messy test but better than the MkII way where we use states and keep
702%D settings.
703
704\let\m_page_layouts_page_number_location  \relax
705\let\m_page_layouts_page_number_location_v\relax
706\let\m_page_layouts_page_number_location_h\relax
707\let\m_page_layouts_page_number_location_x\relax
708
709\def\page_layouts_place_page_number   % also elsewhere .. beware, not \unexpanded else
710  {\strc_pagenumbers_place_location}  % test below fails
711
712\def\page_layouts_reset_page_number_location
713  {\ifx\m_page_layouts_page_number_location_v\relax\else
714     \edef\currentlayoutelement{\m_page_layouts_page_number_location_v:\m_page_layouts_page_number_location_h}%
715     \edef\page_layouts_previous_page_number_locator{\detokenizedlayoutelementparameter\m_page_layouts_page_number_location_x}%
716     \doif{\meaning\page_layouts_previous_page_number_locator}{\meaning\page_layouts_place_page_number}
717       {\resetlayoutelementparameter\m_page_layouts_page_number_location_x}%
718   \fi}
719
720\def\page_layouts_set_page_number_location
721  {\edef\currentlayoutelement{\m_page_layouts_page_number_location_v:\m_page_layouts_page_number_location_h}%
722   \letlayoutelementparameter\m_page_layouts_page_number_location_x\page_layouts_place_page_number
723   \ifx\m_page_layouts_page_number_location_x\c!marginedgetext
724     \let\page_layouts_place_extra_text_left \page_layouts_place_page_number_left
725     \let\page_layouts_place_extra_text_right\page_layouts_place_page_number_right
726   \else
727     \let\page_layouts_place_extra_text_left \relax
728     \let\page_layouts_place_extra_text_right\relax
729   \fi}
730
731\def\page_layouts_identify_page_number_location
732  {\let\m_page_layouts_page_number_location_v\v!footer
733   \let\m_page_layouts_page_number_location_h\v!text
734   \let\m_page_layouts_page_number_location_x\c!middletext
735   \processallactionsinset[\directpagenumberingparameter\c!location]
736     [    \v!header=>\let\m_page_layouts_page_number_location_v\v!header,
737          \v!footer=>\let\m_page_layouts_page_number_location_v\v!footer,
738          \v!middle=>\let\m_page_layouts_page_number_location_h\v!text
739                     \let\m_page_layouts_page_number_location_x\c!middletext,
740            \v!left=>\let\m_page_layouts_page_number_location_h\v!text
741                     \let\m_page_layouts_page_number_location_x\c!lefttext,
742           \v!right=>\let\m_page_layouts_page_number_location_h\v!text
743                     \let\m_page_layouts_page_number_location_x\c!righttext,
744          \v!inleft=>\let\m_page_layouts_page_number_location_h\v!margin
745                     \let\m_page_layouts_page_number_location_x\c!lefttext,
746         \v!inright=>\let\m_page_layouts_page_number_location_h\v!margin
747                     \let\m_page_layouts_page_number_location_x\c!righttext,
748        \v!inmargin=>\let\m_page_layouts_page_number_location_h\v!margin
749                     \def\m_page_layouts_page_number_location_x{\ifdoublesided\c!margintext\else\c!righttext\fi},
750          \v!margin=>\let\m_page_layouts_page_number_location_h\v!margin
751                     \def\m_page_layouts_page_number_location_x{\ifdoublesided\c!margintext\else\c!righttext\fi},
752        \v!atmargin=>\let\m_page_layouts_page_number_location_h\v!text
753                     \let\m_page_layouts_page_number_location_x\c!marginedgetext,
754      \v!marginedge=>\let\m_page_layouts_page_number_location_h\v!text
755                     \let\m_page_layouts_page_number_location_x\c!marginedgetext]}
756
757\unexpanded\def\strc_pagenumbers_set_location
758  {\edef\p_strc_pagenumbers_location{\directpagenumberingparameter\c!location}%
759   \ifx\p_strc_pagenumbers_location\m_page_layouts_page_number_location
760      % unchanged
761   \else
762     \let\m_page_layouts_page_number_location\p_strc_pagenumbers_location
763     \page_layouts_reset_page_number_location
764     \ifx\p_strc_pagenumbers_location\empty
765       % set otherwise
766     \else\ifx\p_strc_pagenumbers_location\v!none
767       % set otherwise
768     \else
769       \page_layouts_identify_page_number_location
770       \page_layouts_set_page_number_location
771     \fi\fi
772   \fi}
773
774\def\page_layouts_place_page_number_left % historic
775  {\begingroup
776   \setbox\scratchbox\hbox{\ignorespaces\layoutelementparameter\c!marginedgetext\removeunwantedspaces}%
777   \ifzeropt\wd\scratchbox\else
778     \edef\p_strc_pagenumbers_width{\directpagenumberingparameter\c!width}%
779     \ifx\p_strc_pagenumbers_width\empty
780       \box\scratchbox\tfskip
781     \else
782       \hpack to \p_strc_pagenumbers_width{\box\scratchbox\hss}%
783     \fi
784   \fi
785   \endgroup}
786
787\def\page_layouts_place_page_number_right % historic
788  {\begingroup
789   \setbox\scratchbox\hbox{\ignorespaces\layoutelementparameter\c!marginedgetext\removeunwantedspaces}%
790   \ifzeropt\wd\scratchbox\else
791     \edef\p_strc_pagenumbers_width{\directpagenumberingparameter\c!width}%
792     \ifx\p_strc_pagenumbers_width\empty
793       \tfskip\box\scratchbox
794     \else
795       \hpack to \p_strc_pagenumbers_width{\hss\box\scratchbox}%
796     \fi
797   \fi
798   \endgroup}
799
800\strc_pagenumbers_set_location % initializes
801
802\newbox\b_page_layouts_element
803
804\def\page_layouts_insert_elements
805  {\ifcase\pageornamentstate
806     \page_layouts_place_elements_indeed % we could have a special flag for always ignored
807   \fi}
808
809\def\page_layouts_place_elements_indeed
810  {\setbox\b_page_layouts_element\vpack
811     {\dontcomplain
812      \calculatereducedvsizes
813      \swapmargins
814      \offinterlineskip
815      \vkern\dimexpr-\topheight-\topdistance\relax
816      \the\toptextcontent
817      \vkern\dimexpr\topheight+\topdistance\relax
818      \the\headertextcontent
819      \vkern\dimexpr\headerheight+\headerdistance+\textdistance\relax
820      \anch_positions_place_anchors
821      \vkern\dimexpr-\textdistance-\textheight\relax
822      \the\texttextcontent
823      \vkern\textheight
824      \the\everyendoftextbody
825      \vkern\footerdistance
826      \the\footertextcontent
827      \vkern\dimexpr\footerheight+\bottomdistance\relax
828      \the\bottomtextcontent
829      \vkern\bottomheight
830      \vfilll}%
831  \smashbox\b_page_layouts_element
832  \box\b_page_layouts_element}
833
834% \ifdefined\page_prepare_backgrounds\else
835%     \let\page_prepare_backgrounds\gobbleoneargument
836% \fi
837
838% only for very special controlled cases or experiments:
839
840\let\page_scale_text_box\gobbleoneargument
841
842\def\page_insert_body#1#2%
843  {\setbox\b_page_layouts_element\vpack
844     {\offinterlineskip
845      \calculatereducedvsizes
846      \calculatehsizes
847      \swapmargins
848      \vkern\dimexpr\headerheight+\headerdistance+\textdistance\relax
849      \dontleavehmode
850     %\page_prepare_backgrounds{#2}%
851      \hpack to \makeupwidth
852        {\begingroup
853           \swapmargins
854           \goleftonpage
855           \ifdim\leftedgewidth>\zeropoint
856             \the\leftedgetextcontent
857             \kern\dimexpr\leftedgewidth+\leftedgedistance\relax
858           \fi
859           \ifdim\leftmarginwidth>\zeropoint
860             \the\leftmargintextcontent
861             \kern\dimexpr\leftmarginwidth+\leftmargindistance\relax
862           \fi
863         \endgroup
864         \page_apply_postprocessors_page{#2}%
865         \settextpagecontent\b_page_layouts_element{#1}{#2}%
866         \page_backgrounds_add_to_text\b_page_layouts_element
867         \page_grids_add_to_box\b_page_layouts_element
868         \page_scale_text_box\b_page_layouts_element
869         \box\b_page_layouts_element
870         \begingroup
871           \ifdim\rightmarginwidth>\zeropoint
872             \kern\rightmargindistance
873             \the\rightmargintextcontent
874             \kern\rightmarginwidth
875           \fi
876           \ifdim\rightedgewidth>\zeropoint
877             \kern\rightedgedistance
878             \the\rightedgetextcontent
879             \kern\rightedgewidth
880           \fi
881         \endgroup
882         \hss}}%
883   \smashbox\b_page_layouts_element
884   \box\b_page_layouts_element}
885
886%D The main text area has to be combined with some additional (tracing) information.
887%D
888%D This will be stored as normal and overloaded in page-lyr and later in page-spr we
889%D overload the the stored version .. evenatually i will clear up the experimental
890%D mess.
891
892\def\settextpagecontent#1#2#3% #2 and #3 will disappear / is overloaded
893  {\setbox#1\hpack to \makeupwidth
894     {\hss                     % so don't change this
895      \setlayoutcomponentattribute{\v!page:\v!text}%
896      \vpack \layoutcomponentboxattribute to \textheight
897        {\offinterlineskip
898         \freezetextwidth
899         \hsize\textwidth      % local variant of \sethsize  <<< in columns?
900         \boxmaxdepth\maxdepth
901         \noindent             % content can be < \hsize
902         \page_otr_command_package_contents#2#3}% this will vbox
903      \hss}%
904   \dp#1\zeropoint}
905
906\protect \endinput
907