page-bck.mkxl /size: 28 Kb    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=page-bck, % copied from main-001
3%D        version=1997.03.31,
4%D          title=\CONTEXT\ Page Macros,
5%D       subtitle=Backgrounds,
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% Currently the text cells are fakes and no (foreground) frames which makes sense
15% as order might matter e.g. is text sticks in other cells. The page, text etc
16% areas do support foreground order change.
17
18\writestatus{loading}{ConTeXt Page Macros / Backgrounds}
19
20\unprotect
21
22\startcontextdefinitioncode
23
24% maybe use \currentframedhash here
25
26%D For special purposes, users can question the \type {*background} mode. This mode
27%D is only available when typesetting the pagebody.
28%D
29%D \starttyping
30%D \startmode[*background] ...
31%D \stoptyping
32
33\newconditional\c_page_backgrounds_new
34\newconditional\c_page_backgrounds_new_right
35\newconditional\c_page_backgrounds_new_left
36\newconditional\c_page_backgrounds_some
37
38\appendtoks
39    \ifconditional\c_page_backgrounds_some
40        \ifconditional\c_page_backgrounds_new
41            \setsystemmode\v!background
42        \fi
43    \fi
44\to \everybeforepagebody
45
46\permanent\protected\def\initializepagebackgrounds
47  {\c_page_backgrounds_new\conditionalfalse
48   \c_page_backgrounds_new_right\conditionalfalse
49   \c_page_backgrounds_new_left\conditionalfalse
50   \c_page_backgrounds_some\conditionalfalse}
51
52%D We keep calculations and checks to a minimum and also try to minimize
53%D the amount of tracing due to expansion.
54
55\mutable\lettonothing\currentotrbackground
56
57%D This is the only spot where we have a low level dependency on the way
58%D parent chains are defined but we want the speed.
59
60% \protected\def\page_backgrounds_check_background
61%   {\ifcsname\??framed\currentotrbackground:\c!background\endcsname
62%     %\edef\page_background_temp{\csname\??framed\currentotrbackground:\c!background\endcsname}%
63%      \edef\page_background_temp{\lastnamedcs}%
64%      \ifempty\page_background_temp
65%        \expandafter\expandafter\expandafter\page_backgrounds_check_frame
66%      \else
67%        \page_backgrounds_set_yes
68%      \fi
69%    \else
70%      \expandafter\page_backgrounds_check_frame
71%    \fi}
72%
73% \def\page_backgrounds_check_frame
74%   {\ifcsname\??framed\currentotrbackground:\c!frame\endcsname
75%     %\edef\page_background_temp{\csname\??framed\currentotrbackground:\c!frame\endcsname}%
76%      \edef\page_background_temp{\lastnamedcs}%
77%      \ifx\page_background_temp\v!on
78%        \page_backgrounds_set_yes
79%      \else
80%        \expandafter\expandafter\expandafter\page_backgrounds_check_leftframe
81%      \fi
82%    \else
83%      \expandafter\page_backgrounds_check_leftframe
84%    \fi}
85%
86% \def\page_backgrounds_check_leftframe
87%   {\ifcsname\??framed\currentotrbackground:\c!leftframe\endcsname
88%     %\edef\page_background_temp{\csname\??framed\currentotrbackground:\c!leftframe\endcsname}%
89%      \edef\page_background_temp{\lastnamedcs}%
90%      \ifx\page_background_temp\v!on
91%        \page_backgrounds_set_yes
92%      \else
93%        \expandafter\expandafter\expandafter\page_backgrounds_check_rightframe
94%      \fi
95%    \else
96%      \expandafter\page_backgrounds_check_rightframe
97%    \fi}
98%
99% \def\page_backgrounds_check_rightframe
100%   {\ifcsname\??framed\currentotrbackground:\c!rightframe\endcsname
101%     %\edef\page_background_temp{\csname\??framed\currentotrbackground:\c!rightframe\endcsname}%
102%      \edef\page_background_temp{\lastnamedcs}%
103%      \ifx\page_background_temp\v!on
104%        \page_backgrounds_set_yes
105%      \else
106%        \expandafter\expandafter\expandafter\page_backgrounds_check_topframe
107%      \fi
108%    \else
109%      \expandafter\page_backgrounds_check_topframe
110%    \fi}
111%
112% \def\page_backgrounds_check_topframe
113%   {\ifcsname\??framed\currentotrbackground:\c!topframe\endcsname
114%     %\edef\page_background_temp{\csname\??framed\currentotrbackground:\c!topframe\endcsname}%
115%      \edef\page_background_temp{\lastnamedcs}%
116%      \ifx\page_background_temp\v!on
117%        \page_backgrounds_set_yes
118%      \else
119%        \expandafter\expandafter\expandafter\page_backgrounds_check_bottomframe
120%      \fi
121%    \else
122%      \expandafter\page_backgrounds_check_bottomframe
123%    \fi}
124%
125% \def\page_backgrounds_check_bottomframe
126%   {\ifcsname\??framed\currentotrbackground:\c!bottomframe\endcsname
127%     %\edef\page_background_temp{\csname\??framed\currentotrbackground:\c!bottomframe\endcsname}%
128%      \edef\page_background_temp{\lastnamedcs}%
129%      \ifx\page_background_temp\v!on
130%        \page_backgrounds_set_yes
131%      \else
132%        \page_backgrounds_set_nop
133%      \fi
134%    \else
135%      \page_backgrounds_set_nop
136%    \fi}
137
138\protected\def\page_backgrounds_check_background
139  {\unless\ifcsname\??framed\currentotrbackground:\c!background\endcsname
140     \page_backgrounds_check_frame
141   \orelse\ifempty{\lastnamedcs}%
142%    \orelse\iflastnamedcs\v!on
143     \page_backgrounds_check_frame
144   \else
145     \page_backgrounds_set_yes
146   \fi}
147
148\def\page_backgrounds_check_frame
149  {\unless\ifcsname\??framed\currentotrbackground:\c!frame\endcsname
150     \page_backgrounds_check_leftframe
151   \orelse\ifcstok{\lastnamedcs}\v!on
152%    \orelse\iflastnamedcs\v!on
153     \page_backgrounds_set_yes
154   \else
155     \page_backgrounds_check_leftframe
156   \fi}
157
158\def\page_backgrounds_check_leftframe
159  {\unless\ifcsname\??framed\currentotrbackground:\c!leftframe\endcsname
160     \page_backgrounds_check_rightframe
161   \orelse\ifcstok{\lastnamedcs}\v!on
162%    \orelse\iflastnamedcs\v!on
163     \page_backgrounds_set_yes
164   \else
165     \page_backgrounds_check_rightframe
166   \fi}
167
168\def\page_backgrounds_check_rightframe
169  {\unless\ifcsname\??framed\currentotrbackground:\c!rightframe\endcsname
170     \page_backgrounds_check_topframe
171   \orelse\ifcstok{\lastnamedcs}\v!on
172%    \orelse\iflastnamedcs\v!on
173     \page_backgrounds_set_yes
174   \else
175     \page_backgrounds_check_topframe
176   \fi}
177
178\def\page_backgrounds_check_topframe
179  {\unless\ifcsname\??framed\currentotrbackground:\c!topframe\endcsname
180     \page_backgrounds_check_bottomframe
181   \orelse\ifcstok{\lastnamedcs}\v!on
182%    \orelse\iflastnamedcs\v!on
183     \page_backgrounds_set_yes
184   \else
185     \page_backgrounds_check_bottomframe
186   \fi}
187
188\def\page_backgrounds_check_bottomframe
189  {\unless\ifcsname\??framed\currentotrbackground:\c!bottomframe\endcsname
190     \page_backgrounds_set_nop
191   \orelse\ifcstok{\lastnamedcs}\v!on
192%    \orelse\iflastnamedcs\v!on
193     \page_backgrounds_set_yes
194   \else
195     \page_backgrounds_set_nop
196   \fi}
197
198%D We don't use the commandhandler code as we want these multitude of backgrounds to be
199%D as fast as possible.
200
201\installcorenamespace{layoutbackgrounds}
202\installcorenamespace{layoutbackgroundcheck} % we need another hash as \??layoutbackgrounds<...> gets defined
203
204\def\page_backgrounds_set_yes{\letcsname\currentotrbackground\endcsname\relax    }
205\def\page_backgrounds_set_nop{\letcsname\currentotrbackground\endcsname\undefined}
206
207\protected\def\page_backgrounds_check#1%
208  {\cdef\currentotrbackground{\??layoutbackgrounds#1}%
209   \page_backgrounds_check_background}
210
211\permanent\def\doifelsesomebackground#1%
212  {\ifexporting
213     \expandafter\secondoftwoarguments
214  \orelse\ifcsname\??layoutbackgrounds#1\endcsname
215     \expandafter\firstoftwoarguments
216   \else
217     \expandafter\secondoftwoarguments
218   \fi}
219
220\aliased\let\doifsomebackgroundelse\doifelsesomebackground
221
222\permanent\def\doifsomebackground#1%
223  {\ifexporting
224     \expandafter\gobbleoneargument
225   \orelse\ifcsname\??layoutbackgrounds#1\endcsname
226     \expandafter\firstofoneargument
227   \else
228     \expandafter\gobbleoneargument
229   \fi}
230
231%D The background mechanism falls back on the \type {\framed} macro. This means
232%D that all normal frame and overlay features can be used.
233
234\def\page_backgrounds_add_to_box#1% area
235  {\ifexporting
236     \expandafter\gobblefourarguments
237   \orelse\ifcsname\??layoutbackgrounds#1\endcsname
238     \expandafter\page_backgrounds_add_to_box_indeed
239   \else
240     \expandafter\gobblefourarguments
241   \fi#1}
242
243%D We don't need the dimensions here as this is a real framed but the question is:
244%D do we indeed need a real framed or can we use a fake (i.e. no foreground, only
245%D for hidden).
246
247\def\page_backgrounds_add_to_box_indeed#1#2#3#4% area box width height / always non zero
248  {\edef\p_page_backgrounds_setups{\namedframedparameter{\??layoutbackgrounds#1}\c!setups}%
249   \ifempty\p_page_backgrounds_setups \else
250     \doprocesslocalsetups\p_page_backgrounds_setups % should not produce funny spaces !
251   \fi
252   % #2 has the right dimensions already
253   \setbox#2\hpack{\localbackgroundframed{\??layoutbackgrounds#1}#1#2}}% a real framed (including foreground)
254
255
256%D There are quite some backgrounds. At the bottom layer, there is the {\em paper}
257%D background. This one is only used for special purposes, like annotations to
258%D documents.
259
260\def\page_backgrounds_add_to_print#1%
261  {\page_backgrounds_add_to_box\v!paper#1\printpaperwidth\printpaperheight}
262
263%D The page backgrounds can be put behind the {\em left page}, the {\em right page}
264%D or {\em each page}. As with the paper background, these are calculated on each
265%D page.
266
267\def\page_backgrounds_add_to_paper#1%
268  {\doifbothsidesoverruled
269     {\page_backgrounds_add_to_box\v!rightpage#1\paperwidth\paperheight}%
270     {\page_backgrounds_add_to_box\v!rightpage#1\paperwidth\paperheight}%
271     {\page_backgrounds_add_to_box\v!leftpage #1\paperwidth\paperheight}%
272   \page_backgrounds_add_to_box\v!page#1\paperwidth\paperheight}
273
274%D Then there are the 25 areas that make up the layout: {\em top, header, text,
275%D footer, bottom} times {\em left edge, left margin, text, right margin, right
276%D edge}. These are only recalculated when they change or when the \type {status}
277%D is set to \type {repeat}.
278
279\newbox\leftbackground  % todo: rename
280\newbox\rightbackground % todo: rename
281
282%D Finaly there is an aditional {\em text} background, again useful for special
283%D purposes only. This one is calculated each time. The hidden backgrounds are not
284%D meant for users!
285
286\newconditional\c_page_backgrounds_hidden_enabled
287
288\def\page_backgrounds_add_to_text#1%
289  {\ifconditional\c_page_backgrounds_hidden_enabled
290     \page_backgrounds_add_to_box\v!hidden#1\makeupwidth\textheight % mine !
291   \fi
292   \page_backgrounds_add_to_box\v!text#1\makeupwidth\textheight}
293
294%D The next couple of macros implement the area backgrounds. As said, these are
295%D cached in dedicated boxes. The offsets and depth of the page are used for
296%D alignment purposes.
297
298%newdimension\pageoffset % bleed
299%newdimension\pagedepth  % built-in
300
301%D We need a bit more clever mechanism in order to handle layers well. This means
302%D that we cannot calculate both background at the same time since something may
303%D have changed halfway a page.
304
305%D Margin swapping has been simplified: see mkii code in case of regression.
306%D Calculation is delayed till the page anyway so the state is known.
307
308\def\page_backgrounds_recalculate
309  {\global\c_page_backgrounds_new\conditionaltrue}
310
311\def\page_backgrounds_set_boxes
312  {\ifconditional\c_page_backgrounds_new
313     \page_backgrounds_set_boxes_r
314   \fi
315   \doifbothsides
316     \page_backgrounds_set_boxes_a
317     \page_backgrounds_set_boxes_b
318     \page_backgrounds_set_boxes_c
319   \ifx\p_page_backgrounds_state\v!repeat\else
320     \global\c_page_backgrounds_new\conditionalfalse
321   \fi}
322
323\def\page_backgrounds_set_boxes_r
324  {\global\c_page_backgrounds_new_right\conditionaltrue
325   \global\c_page_backgrounds_new_left\conditionaltrue
326   \global\setbox\leftbackground\emptybox
327   \global\setbox\rightbackground\emptybox}
328
329\def\page_backgrounds_set_boxes_a
330  {\ifconditional\c_page_backgrounds_new_left
331     % \showmessage\m!layouts8\empty
332     \page_backgrounds_set_box\leftbackground % \conditionalfalse
333     \global\c_page_backgrounds_new_left\conditionalfalse
334     \global\c_page_backgrounds_new_right\conditionalfalse
335   \fi}
336
337\def\page_backgrounds_set_boxes_b
338  {\ifconditional\c_page_backgrounds_new_left
339     % \showmessage\m!layouts8\empty
340     \page_backgrounds_set_box\leftbackground
341     \global\c_page_backgrounds_new_left\conditionalfalse
342   \fi}
343
344\def\page_backgrounds_set_boxes_c
345  {\ifconditional\c_page_backgrounds_new_right
346     % \showmessage\m!layouts8\empty
347     \page_backgrounds_set_box\rightbackground
348     \global\c_page_backgrounds_new_right\conditionalfalse
349   \fi}
350
351\def\page_backgrounds_add_to_main#1% todo: dimension spec
352  {\ifconditional\c_page_backgrounds_some
353     \page_backgrounds_set_boxes
354     \setbox#1\vpack
355       {\offinterlineskip
356        \doifelsemarginswap{\copy\leftbackground}{\copy\rightbackground}%
357        \box#1}%
358   \fi}
359
360\newdimension\pagebackgroundhoffset  % THESE WILL BECOME OBSOLETE
361\newdimension\pagebackgroundvoffset
362\newdimension\pagebackgroundoffset   % used elsewhere
363\newdimension\pagebackgrounddepth
364\newdimension\pagebackgroundcompensation
365
366\def\page_backgrounds_set_offsets % used in menus (we can use ifcsname's here)
367  {\ifconditional\c_page_backgrounds_some \ifconditional\c_page_backgrounds_new
368     \page_backgrounds_set_offsets_indeed % indirect, less tracing
369   \fi \fi}
370
371\def\page_backgrounds_set_offsets_indeed
372  {\ifcsname\??layoutbackgrounds\v!text\v!text\endcsname
373     \page_backgrounds_set_offsets_yes
374   \orelse\ifcsname\??layoutbackgrounds\v!text\endcsname
375     \page_backgrounds_set_offsets_yes
376   \else
377     \page_backgrounds_set_offsets_nop
378   \fi}
379
380\def\page_backgrounds_set_offsets_nop
381  {\global\pagebackgroundhoffset     \zeropoint
382   \global\pagebackgroundvoffset     \zeropoint
383   \global\pagebackgrounddepth       \zeropoint
384   \global\pagebackgroundcompensation\zeropoint}
385
386\def\page_backgrounds_set_offsets_yes
387  {\global\pagebackgroundoffset      \d_page_backgrounds_depth
388   \global\pagebackgroundcompensation\d_page_backgrounds_offset\relax
389   \ifzeropt\pagebackgroundcompensation
390     \page_backgrounds_set_offsets_nop
391   \else
392     \ifcsname\??layoutbackgrounds\v!top\v!text\endcsname
393       \global\pagebackgroundhoffset\zeropoint
394     \orelse\ifcsname\??layoutbackgrounds\v!bottom\v!text\endcsname
395       \global\pagebackgroundhoffset\zeropoint
396     \else
397       \global\pagebackgroundhoffset\pagebackgroundcompensation
398     \fi
399     \ifcsname\??layoutbackgrounds\v!text\v!rightedge\endcsname
400       \global\pagebackgroundvoffset\zeropoint
401       \global\pagebackgrounddepth  \zeropoint
402     \orelse\ifcsname\??layoutbackgrounds\v!text\v!leftedge\endcsname
403       \global\pagebackgroundvoffset\zeropoint
404       \global\pagebackgrounddepth  \zeropoint
405     \else
406       \global\pagebackgroundvoffset\pagebackgroundcompensation
407       \global\pagebackgrounddepth  \d_page_backgrounds_depth
408     \fi
409   \fi}
410
411\appendtoks
412    \page_backgrounds_set_offsets
413\to \everybeforepagebody
414
415\newconditional\swapbackgroundmargins \swapbackgroundmargins\conditionaltrue
416
417%D The swapping here is pretty nasty! Maybe we should find another way some day but
418%D for now we tweak.
419
420\def\page_backgrounds_set_box#1% #2%
421  {\global\setbox#1\vpack
422     {\dontcomplain
423      \page_layouts_swap_margins\v!background
424      \ifconditional\swapbackgroundmargins
425        \doifelsemarginswap \donothing
426          {\enforced\swapcsvalues\v!rightmargin\v!leftmargin
427           \enforced\swapcsvalues\v!rightedge  \v!leftedge}%
428      \fi
429      \calculatereducedvsizes
430      \offinterlineskip
431      \ifdim\topheight>\zeropoint
432        \kern{-\topheight-\topdistance}%
433        \page_backgrounds_set_box_row\v!top\topheight
434        \kern\topdistance
435      \fi
436      \ifdim\headerheight>\zeropoint
437        \page_backgrounds_set_box_row\v!header\headerheight
438        \kern\headerdistance
439      \fi
440      \ifdim\textheight>\zeropoint
441        \page_backgrounds_set_box_row\v!text\textheight
442      \fi
443      \ifdim\footerheight>\zeropoint
444        \kern\footerdistance
445        \page_backgrounds_set_box_row\v!footer\footerheight
446      \fi
447      \ifdim\bottomheight>\zeropoint
448        \kern\bottomdistance
449        \page_backgrounds_set_box_row\v!bottom\bottomheight
450      \fi
451      \vfilll}%
452   \smashbox#1}
453
454\def\page_backgrounds_set_box_row#1#2% maybe helper
455  {\setbox\scratchbox\vpack to #2
456     \bgroup\hpack\bgroup
457       \goleftonpage
458       \ifdim\leftedgewidth>\zeropoint
459         \ifcsname\??layoutbackgrounds#1\v!leftedge\endcsname
460           \page_backgrounds_set_box_cell#1\v!leftedge\leftedgewidth#2%
461         \else
462           \kern\leftedgewidth
463         \fi
464         \kern\leftedgedistance
465       \fi
466       \ifdim\leftmarginwidth>\zeropoint
467         \ifcsname\??layoutbackgrounds#1\v!leftmargin\endcsname
468           \page_backgrounds_set_box_cell#1\v!leftmargin\leftmarginwidth#2%
469         \else
470           \kern\leftmarginwidth
471         \fi
472         \kern\leftmargindistance
473       \fi
474       \ifcsname\??layoutbackgrounds#1\v!text\endcsname
475         \page_backgrounds_set_box_cell#1\v!text\makeupwidth#2%
476       \else
477         \kern\makeupwidth
478       \fi
479       \ifdim\rightmarginwidth>\zeropoint
480         \kern\rightmargindistance
481         \ifcsname\??layoutbackgrounds#1\v!rightmargin\endcsname
482           \page_backgrounds_set_box_cell#1\v!rightmargin\rightmarginwidth#2%
483         \else
484           \kern\rightmarginwidth
485         \fi
486       \fi
487       \ifdim\rightedgewidth>\zeropoint
488         \kern\rightedgedistance
489         \ifcsname\??layoutbackgrounds#1\v!rightedge\endcsname
490           \page_backgrounds_set_box_cell#1\v!rightedge\rightedgewidth#2%
491         \else
492           \kern\rightedgewidth
493         \fi
494       \fi
495     \egroup\egroup
496   \wd\scratchbox\zeropoint
497   \box\scratchbox\relax}
498
499% these are fake framed .. maybe it's nicer to honor foreground here as well
500% but it's probably a slow downer
501
502\def\page_backgrounds_set_box_cell#1#2#3#4% pos pos width height
503  {\begingroup
504   \edef\p_page_backgrounds_setups{\namedframedparameter{\??layoutbackgrounds#1#2}\c!setups}%
505   \ifempty\p_page_backgrounds_setups \else
506     \doprocesslocalsetups\p_page_backgrounds_setups % should not produce funny spaces !
507   \fi
508   \edef\p_page_backgrounds_command{\namedframedparameter{\??layoutbackgrounds#1#2}\c!command}%
509   \ifempty\p_page_backgrounds_command
510     \expandafter\page_backgrounds_set_box_cell_nop
511   \else
512     \expandafter\page_backgrounds_set_box_cell_yes
513   \fi#3#4%
514   \localbackgroundframed{\??layoutbackgrounds#1#2}{#1:#2}\scratchbox
515   \endgroup}
516
517\def\page_backgrounds_set_box_cell_nop#1#2%
518  {\setbox\scratchbox\emptyvbox
519   \wd\scratchbox#1%
520   \ht\scratchbox#2}
521
522\def\page_backgrounds_set_box_cell_yes#1#2%
523  {\setbox\scratchbox\vpack to #2{\vss\hpack to#1{\hss\p_page_backgrounds_command\hss}\vss}%
524   \dp\scratchbox\zeropoint}
525
526%D The background mechanism is quite demanding in terms or resources. We used to
527%D delay these definitions till runtime usage, but since today's \TEX's are large,
528%D we now do the work on forehand.
529%D
530%D \starttyping
531%D \setupbackgrounds [settings]
532%D \setupbackgrounds [paper,page,text,..] [settings]
533%D \setupbackgrounds [top,...] [leftedge,...] [settings]
534%D \stoptyping
535%D
536%D \showsetup{setupbackgrounds}
537%D
538%D Because the number of arguments runs from one to three, we need to check
539%D for it.
540
541\newtoks\everybackgroundssetup
542
543\permanent\tolerant\protected\def\setupbackgrounds[#S#1]#*[#S#2]#*[#S#3]%
544  {\ifarguments
545     \expandafter\gobblethreeoptionals
546   \or
547     \expandafter\page_backgrounds_setup_basics
548   \or
549     \expandafter\page_backgrounds_setup_single
550   \or
551     \expandafter\page_backgrounds_setup_double
552   \fi[#1][#2][#3]}
553
554\newdimension\d_page_backgrounds_offset
555\newdimension\d_page_backgrounds_depth
556
557\appendtoks
558   \edef\p_page_backgrounds_offset{\namedframedparameter{\??layoutbackgrounds\v!page}\c!offset}%
559   \edef\p_page_backgrounds_depth {\namedframedparameter{\??layoutbackgrounds\v!page}\c!depth }%
560   \edef\p_page_backgrounds_state {\namedframedparameter{\??layoutbackgrounds       }\c!state }%
561   \global\d_page_backgrounds_offset\ifempty\p_offset\zeropoint\else{\p_page_backgrounds_offset}\fi
562   \global\d_page_backgrounds_depth \ifempty\p_depth \zeropoint\else{\p_page_backgrounds_depth }\fi
563   \global\pagebackgroundoffset\d_page_backgrounds_offset\relax
564   \global\pagebackgrounddepth \d_page_backgrounds_depth \relax
565   \ifx\p_page_backgrounds_state\v!stop
566     \global\c_page_backgrounds_new\conditionalfalse
567   \else
568     \global\c_page_backgrounds_new\conditionaltrue
569   \fi
570\to \everybackgroundssetup
571
572\def\v_page_backgrounds_double_set{\v!paper,\v!page,\v!leftpage,\v!rightpage}
573\def\v_page_backgrounds_single_set{\v!text,\v!hidden,\v!paper,\v!page,\v!leftpage,\v!rightpage}
574\def\v_page_backgrounds_common_set{\v!leftedge,\v!leftmargin,\v!text,\v!rightmargin,\v!rightedge}
575
576\let\page_backgrounds_setup_step_nested\relax
577
578\protected\def\page_backgrounds_setup_double[#1][#2][#S#3]% if needed we can speed this up
579  {\global\c_page_backgrounds_some\conditionaltrue
580   \def\page_backgrounds_setup_step##1%
581     {\ifinset{##1}\v_page_backgrounds_double_set
582        \page_backgrounds_setup_and_check{##1}{#3}%
583      \else
584        \def\page_backgrounds_setup_step_nested####1{\page_backgrounds_setup_and_check{##1####1}{#3}}%
585        \processcommacommand[#2]\page_backgrounds_setup_step_nested
586      \fi}%
587   \processcommacommand[#1]\page_backgrounds_setup_step
588   \expand\everybackgroundssetup}
589
590\protected\def\page_backgrounds_setup_single[#1][#S#2][#-]%
591  {\global\c_page_backgrounds_some\conditionaltrue
592   \ifcommon{#1}\v_page_backgrounds_single_set
593     \def\page_backgrounds_setup_step##1{\page_backgrounds_setup_and_check{##1}{#2}}%
594     \processcommacommand[#1]\page_backgrounds_setup_step
595     \expand\everybackgroundssetup
596   \else
597     \page_backgrounds_setup_double[#1][\v_page_backgrounds_common_set][#2]%
598   \fi}
599
600\protected\def\page_backgrounds_setup_basics[#S#1][#-][#-]%
601  {\setupframed[\??layoutbackgrounds][#1]%
602   \expand\everybackgroundssetup}
603
604\protected\def\page_backgrounds_setup_and_check#1#2% tag settings
605  {\cdef\currentotrbackground{\??layoutbackgrounds#1}%
606   \setupframed[\currentotrbackground][#2]%
607   \page_backgrounds_check_background}
608
609%D Each areas (currently there are $1+3+25+1=30$ of them) has its own low level
610%D framed object associated.
611
612\definesimplifiedframed[\??layoutbackgrounds\v!paper]
613\definesimplifiedframed[\??layoutbackgrounds\v!page]
614\definesimplifiedframed[\??layoutbackgrounds\v!leftpage]
615\definesimplifiedframed[\??layoutbackgrounds\v!rightpage]
616
617\definesimplifiedframed[\??layoutbackgrounds\v!text]
618\definesimplifiedframed[\??layoutbackgrounds\v!hidden]
619
620\definesimplifiedframed[\??layoutbackgrounds\v!top\v!leftedge]
621\definesimplifiedframed[\??layoutbackgrounds\v!top\v!leftmargin]
622\definesimplifiedframed[\??layoutbackgrounds\v!top\v!text]
623\definesimplifiedframed[\??layoutbackgrounds\v!top\v!rightmargin]
624\definesimplifiedframed[\??layoutbackgrounds\v!top\v!rightedge]
625
626\definesimplifiedframed[\??layoutbackgrounds\v!header\v!leftedge]
627\definesimplifiedframed[\??layoutbackgrounds\v!header\v!leftmargin]
628\definesimplifiedframed[\??layoutbackgrounds\v!header\v!text]
629\definesimplifiedframed[\??layoutbackgrounds\v!header\v!rightmargin]
630\definesimplifiedframed[\??layoutbackgrounds\v!header\v!rightedge]
631
632\definesimplifiedframed[\??layoutbackgrounds\v!text\v!leftedge]
633\definesimplifiedframed[\??layoutbackgrounds\v!text\v!leftmargin]
634\definesimplifiedframed[\??layoutbackgrounds\v!text\v!text]
635\definesimplifiedframed[\??layoutbackgrounds\v!text\v!rightmargin]
636\definesimplifiedframed[\??layoutbackgrounds\v!text\v!rightedge]
637
638\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!leftedge]
639\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!leftmargin]
640\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!text]
641\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!rightmargin]
642\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!rightedge]
643
644\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!leftedge]
645\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!leftmargin]
646\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!text]
647\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!rightmargin]
648\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!rightedge]
649
650\setupbackgrounds
651  [\v!page]
652  [\c!offset=\zeropoint, % hm, so we need to force overlay elsewhere
653   \c!depth=\zeropoint]
654
655%D General setup:
656
657\setupbackgrounds
658  [\c!state=\c!start]
659
660%D The hidden layer can be populated by extending the following comma separated
661%D list. This only happens in core modules.
662
663% todo page-2   .. page+2   achter pagina -> bleed
664%      spread-2 .. spread+2 achter spread -> spread (repeat 2 times)
665
666\permanent\protected\def\enablehiddenbackground
667  {\global\c_page_backgrounds_hidden_enabled\conditionaltrue
668   \global\c_page_backgrounds_some\conditionaltrue
669   \page_backgrounds_recalculate}
670
671\permanent\protected\def\disablehiddenbackground
672  {\global\c_page_backgrounds_hidden_enabled\conditionalfalse}
673
674\immutable\def\hiddenbackgroundlist
675  {\v!text-2,\v!text-1,\v!foreground,\v!text+1,\v!text+2}
676
677\permanent\protected\def\prependhiddenbackground#1{\enforced\edef\hiddenbackgroundlist{#1,\hiddenbackgroundlist}}
678\permanent\protected\def\appendhiddenbackground #1{\enforced\edef\hiddenbackgroundlist{\hiddenbackgroundlist,#1}}
679
680\defineoverlay[\v!text-2][\positionregionoverlay\textanchor{\v!text-2}] % no new anchor, we share text
681\defineoverlay[\v!text-1][\positionregionoverlay\textanchor{\v!text-1}]
682\defineoverlay[\v!text+1][\positionregionoverlay\textanchor{\v!text+1}]
683\defineoverlay[\v!text+2][\positionregionoverlay\textanchor{\v!text+2}]
684
685\setupbackgrounds
686  [\v!hidden]
687  [\c!background=\hiddenbackgroundlist]
688
689% \setupbackgrounds
690%   [\v!hidden]
691%   [\c!depth=\ifnum\bottomraggednessmode=\plusthree\strutdp\else\zeropoint\fi]
692
693%D Because we haven't really set up backgrounds yet, we set the main efficiency
694%D switch to false.
695
696\c_page_backgrounds_some\conditionalfalse
697
698%D Sometimes you have a document wide (page) background but need to overload it
699%D locally. In such case (at least in my experience) the only values that get set
700%D are the background and backgroundcolor (if set at all). A full inheritance chain
701%D would complicate things because then we need to use named backgrounds which in
702%D turn will make this mechanism slower. I considered independent local backgrounds
703%D but that also complicates the code (not that much) but isolation means that we
704%D need to set more parameters each time. The following simple approach proabbly
705%D suits most usage.
706%D
707%D \starttyping
708%D \starttext
709%D     \setupbackgrounds[page][background=color,backgroundcolor=red]
710%D     \input tufte \page
711%D     \setupbackgrounds[page][background=,backgroundcolor=]
712%D     \input tufte \page
713%D     \setupbackgrounds[page][background=color,backgroundcolor=red]
714%D     \input tufte \page
715%D     \pushbackground[page]
716%D         \setupbackgrounds[page][background=color,backgroundcolor=green]
717%D         \input tufte \page
718%D     \popbackground
719%D     \input tufte \page
720%D \stoptext
721%D \stoptyping
722
723\permanent\protected\def\pushbackground[#1]%
724  {\pushmacro\popbackground
725   \cdef\currentotrbackground{\??layoutbackgrounds#1}%
726   \enforced\permanent\protected\edef\popbackground
727     {\setupframed
728        [\currentotrbackground]
729        [\c!background=\namedframedparameter{\currentotrbackground}\c!background,
730         \c!backgroundcolor=\namedframedparameter{\currentotrbackground}\c!backgroundcolor]%
731      \page_backgrounds_check_background
732      \popmacro\popbackground}%
733   \setupframed
734     [\currentotrbackground]
735     [\c!background=,\c!backgroundcolor=]%
736   \page_backgrounds_check_background}
737
738\permanent\let\popbackground\relax
739
740\stopcontextdefinitioncode
741
742\protect \endinput
743
744% %D The next series is used in local (for instance floating) backgrounds.
745%
746% \installsimplifiedframed{\??layoutbackgrounds\v!local}
747%
748% \getparameters
749%   [\??layoutbackgrounds\v!local]
750%   [\c!component=local,
751%    \c!background=\localbackgroundlist]
752%
753% \def\localbackgroundlist
754%   {\v!local-2,\v!local-1,\v!foreground,\v!local+1,\v!local+2}
755%
756% \defineoverlay[\v!local-2][\positionoverlay{\v!local-2}] % todo share
757% \defineoverlay[\v!local-1][\positionoverlay{\v!local-1}]
758% \defineoverlay[\v!local+1][\positionoverlay{\v!local+1}]
759% \defineoverlay[\v!local+2][\positionoverlay{\v!local+2}]
760%
761% \def\page_backgrounds_add_local_to_box
762%   {\ifconditional\c_page_backgrounds_hidden_enabled
763%      \expandafter\page_backgrounds_add_local_to_box_indeed
764%    \else
765%      \expandafter\gobbleoneargument
766%    \fi}
767%
768% \def\page_backgrounds_add_local_to_box_indeed#1%
769%   {\setbox#1\hbox{\localbackgroundframed{\??layoutbackgrounds\v!local}\v!local#1}%
770%    \global\advanceby\localpositionnumber\plusone} % afterwards !
771%
772% \let\page_backgrounds_add_local_to_box\gobbleoneargument
773
774% Test how previous macro behaves with depth:
775%
776% \startcolumnset
777% \input tufte
778% \placefigure{none}{\framed[lines=5]{xxx}}
779% \input tufte
780% \placefigure{none}{\starttabulate\NC test\nc test\NC\NR\stoptabulate}
781% \input tufte
782% \stopcolumnset
783