page-bck.mkxl /size: 28 Kb    last modification: 2024-01-16 09: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  {\ifcsname\??layoutbackgrounds#1\endcsname
213     \expandafter\firstoftwoarguments
214   \else
215     \expandafter\secondoftwoarguments
216   \fi}
217
218\aliased\let\doifsomebackgroundelse\doifelsesomebackground
219
220\permanent\def\doifsomebackground#1%
221  {\ifcsname\??layoutbackgrounds#1\endcsname
222     \expandafter\firstofoneargument
223   \else
224     \expandafter\gobbleoneargument
225   \fi}
226
227%D The background mechanism falls back on the \type {\framed} macro. This means
228%D that all normal frame and overlay features can be used.
229
230\def\page_backgrounds_add_to_box#1% area
231  {\ifcsname\??layoutbackgrounds#1\endcsname
232     \expandafter\page_backgrounds_add_to_box_indeed
233   \else
234     \expandafter\gobblefourarguments
235   \fi#1}
236
237%D We don't need the dimensions here as this is a real framed but the question is:
238%D do we indeed need a real framed or can we use a fake (i.e. no foreground, only
239%D for hidden).
240
241\def\page_backgrounds_add_to_box_indeed#1#2#3#4% area box width height / always non zero
242  {\edef\p_page_backgrounds_setups{\namedframedparameter{\??layoutbackgrounds#1}\c!setups}%
243   \ifempty\p_page_backgrounds_setups \else
244     \doprocesslocalsetups\p_page_backgrounds_setups % should not produce funny spaces !
245   \fi
246   % #2 has the right dimensions already
247   \setbox#2\hpack{\localbackgroundframed{\??layoutbackgrounds#1}#1#2}}% a real framed (including foreground)
248
249
250%D There are quite some backgrounds. At the bottom layer, there is the {\em paper}
251%D background. This one is only used for special purposes, like annotations to
252%D documents.
253
254\def\page_backgrounds_add_to_print#1%
255  {\page_backgrounds_add_to_box\v!paper#1\printpaperwidth\printpaperheight}
256
257%D The page backgrounds can be put behind the {\em left page}, the {\em right page}
258%D or {\em each page}. As with the paper background, these are calculated on each
259%D page.
260
261\def\page_backgrounds_add_to_paper#1%
262  {\doifbothsidesoverruled
263     {\page_backgrounds_add_to_box\v!rightpage#1\paperwidth\paperheight}%
264     {\page_backgrounds_add_to_box\v!rightpage#1\paperwidth\paperheight}%
265     {\page_backgrounds_add_to_box\v!leftpage #1\paperwidth\paperheight}%
266   \page_backgrounds_add_to_box\v!page#1\paperwidth\paperheight}
267
268%D Then there are the 25 areas that make up the layout: {\em top, header, text,
269%D footer, bottom} times {\em left edge, left margin, text, right margin, right
270%D edge}. These are only recalculated when they change or when the \type {status}
271%D is set to \type {repeat}.
272
273\newbox\leftbackground  % todo: rename
274\newbox\rightbackground % todo: rename
275
276%D Finaly there is an aditional {\em text} background, again useful for special
277%D purposes only. This one is calculated each time. The hidden backgrounds are not
278%D meant for users!
279
280\newconditional\c_page_backgrounds_hidden_enabled
281
282\def\page_backgrounds_add_to_text#1%
283  {\ifconditional\c_page_backgrounds_hidden_enabled
284     \page_backgrounds_add_to_box\v!hidden#1\makeupwidth\textheight % mine !
285   \fi
286   \page_backgrounds_add_to_box\v!text#1\makeupwidth\textheight}
287
288%D The next couple of macros implement the area backgrounds. As said, these are
289%D cached in dedicated boxes. The offsets and depth of the page are used for
290%D alignment purposes.
291
292%newdimension\pageoffset % bleed
293%newdimension\pagedepth  % built-in
294
295%D We need a bit more clever mechanism in order to handle layers well. This means
296%D that we cannot calculate both background at the same time since something may
297%D have changed halfway a page.
298
299%D Margin swapping has been simplified: see mkii code in case of regression.
300%D Calculation is delayed till the page anyway so the state is known.
301
302\def\page_backgrounds_recalculate
303  {\global\c_page_backgrounds_new\conditionaltrue}
304
305\def\page_backgrounds_set_boxes
306  {\ifconditional\c_page_backgrounds_new
307     \page_backgrounds_set_boxes_r
308   \fi
309   \doifbothsides
310     \page_backgrounds_set_boxes_a
311     \page_backgrounds_set_boxes_b
312     \page_backgrounds_set_boxes_c
313   \ifx\p_page_backgrounds_state\v!repeat\else
314     \global\c_page_backgrounds_new\conditionalfalse
315   \fi}
316
317\def\page_backgrounds_set_boxes_r
318  {\global\c_page_backgrounds_new_right\conditionaltrue
319   \global\c_page_backgrounds_new_left\conditionaltrue
320   \global\setbox\leftbackground\emptybox
321   \global\setbox\rightbackground\emptybox}
322
323\def\page_backgrounds_set_boxes_a
324  {\ifconditional\c_page_backgrounds_new_left
325     % \showmessage\m!layouts8\empty
326     \page_backgrounds_set_box\leftbackground % \conditionalfalse
327     \global\c_page_backgrounds_new_left\conditionalfalse
328     \global\c_page_backgrounds_new_right\conditionalfalse
329   \fi}
330
331\def\page_backgrounds_set_boxes_b
332  {\ifconditional\c_page_backgrounds_new_left
333     % \showmessage\m!layouts8\empty
334     \page_backgrounds_set_box\leftbackground
335     \global\c_page_backgrounds_new_left\conditionalfalse
336   \fi}
337
338\def\page_backgrounds_set_boxes_c
339  {\ifconditional\c_page_backgrounds_new_right
340     % \showmessage\m!layouts8\empty
341     \page_backgrounds_set_box\rightbackground
342     \global\c_page_backgrounds_new_right\conditionalfalse
343   \fi}
344
345\def\page_backgrounds_add_to_main#1% todo: dimension spec
346  {\ifconditional\c_page_backgrounds_some
347     \page_backgrounds_set_boxes
348     \setbox#1\vpack
349       {\offinterlineskip
350        \doifelsemarginswap{\copy\leftbackground}{\copy\rightbackground}%
351        \box#1}%
352   \fi}
353
354\newdimension\pagebackgroundhoffset  % THESE WILL BECOME OBSOLETE
355\newdimension\pagebackgroundvoffset
356\newdimension\pagebackgroundoffset   % used elsewhere
357\newdimension\pagebackgrounddepth
358\newdimension\pagebackgroundcompensation
359
360\def\page_backgrounds_set_offsets % used in menus (we can use ifcsname's here)
361  {\ifconditional\c_page_backgrounds_some \ifconditional\c_page_backgrounds_new
362     \page_backgrounds_set_offsets_indeed % indirect, less tracing
363   \fi \fi}
364
365\def\page_backgrounds_set_offsets_indeed
366  {\ifcsname\??layoutbackgrounds\v!text\v!text\endcsname
367     \page_backgrounds_set_offsets_yes
368   \orelse\ifcsname\??layoutbackgrounds\v!text\endcsname
369     \page_backgrounds_set_offsets_yes
370   \else
371     \page_backgrounds_set_offsets_nop
372   \fi}
373
374\def\page_backgrounds_set_offsets_nop
375  {\global\pagebackgroundhoffset     \zeropoint
376   \global\pagebackgroundvoffset     \zeropoint
377   \global\pagebackgrounddepth       \zeropoint
378   \global\pagebackgroundcompensation\zeropoint}
379
380\def\page_backgrounds_set_offsets_yes
381  {\global\pagebackgroundoffset      \d_page_backgrounds_depth
382   \global\pagebackgroundcompensation\d_page_backgrounds_offset\relax
383   \ifzeropt\pagebackgroundcompensation
384     \page_backgrounds_set_offsets_nop
385   \else
386     \ifcsname\??layoutbackgrounds\v!top\v!text\endcsname
387       \global\pagebackgroundhoffset\zeropoint
388     \orelse\ifcsname\??layoutbackgrounds\v!bottom\v!text\endcsname
389       \global\pagebackgroundhoffset\zeropoint
390     \else
391       \global\pagebackgroundhoffset\pagebackgroundcompensation
392     \fi
393     \ifcsname\??layoutbackgrounds\v!text\v!rightedge\endcsname
394       \global\pagebackgroundvoffset\zeropoint
395       \global\pagebackgrounddepth  \zeropoint
396     \orelse\ifcsname\??layoutbackgrounds\v!text\v!leftedge\endcsname
397       \global\pagebackgroundvoffset\zeropoint
398       \global\pagebackgrounddepth  \zeropoint
399     \else
400       \global\pagebackgroundvoffset\pagebackgroundcompensation
401       \global\pagebackgrounddepth  \d_page_backgrounds_depth
402     \fi
403   \fi}
404
405\appendtoks
406    \page_backgrounds_set_offsets
407\to \everybeforepagebody
408
409\newconditional\swapbackgroundmargins \swapbackgroundmargins\conditionaltrue
410
411%D The swapping here is pretty nasty! Maybe we should find another way some day but
412%D for now we tweak.
413
414\def\page_backgrounds_set_box#1% #2%
415  {\global\setbox#1\vpack
416     {\dontcomplain
417      \page_layouts_swap_margins\v!background
418      \ifconditional\swapbackgroundmargins
419        \doifelsemarginswap \donothing
420          {\enforced\swapcsvalues\v!rightmargin\v!leftmargin
421           \enforced\swapcsvalues\v!rightedge  \v!leftedge}%
422      \fi
423      \calculatereducedvsizes
424      \offinterlineskip
425      \ifdim\topheight>\zeropoint
426        \kern\dimexpr-\topheight-\topdistance\relax
427        \page_backgrounds_set_box_row\v!top\topheight
428        \kern\topdistance
429      \fi
430      \ifdim\headerheight>\zeropoint
431        \page_backgrounds_set_box_row\v!header\headerheight
432        \kern\headerdistance
433      \fi
434      \ifdim\textheight>\zeropoint
435        \page_backgrounds_set_box_row\v!text\textheight
436      \fi
437      \ifdim\footerheight>\zeropoint
438        \kern\footerdistance
439        \page_backgrounds_set_box_row\v!footer\footerheight
440      \fi
441      \ifdim\bottomheight>\zeropoint
442        \kern\bottomdistance
443        \page_backgrounds_set_box_row\v!bottom\bottomheight
444      \fi
445      \vfilll}%
446   \smashbox#1}
447
448\def\page_backgrounds_set_box_row#1#2% maybe helper
449  {\setbox\scratchbox\vpack to #2
450     \bgroup\hpack\bgroup
451       \goleftonpage
452       \ifdim\leftedgewidth>\zeropoint
453         \ifcsname\??layoutbackgrounds#1\v!leftedge\endcsname
454           \page_backgrounds_set_box_cell#1\v!leftedge\leftedgewidth#2%
455         \else
456           \kern\leftedgewidth
457         \fi
458         \kern\leftedgedistance
459       \fi
460       \ifdim\leftmarginwidth>\zeropoint
461         \ifcsname\??layoutbackgrounds#1\v!leftmargin\endcsname
462           \page_backgrounds_set_box_cell#1\v!leftmargin\leftmarginwidth#2%
463         \else
464           \kern\leftmarginwidth
465         \fi
466         \kern\leftmargindistance
467       \fi
468       \ifcsname\??layoutbackgrounds#1\v!text\endcsname
469         \page_backgrounds_set_box_cell#1\v!text\makeupwidth#2%
470       \else
471         \kern\makeupwidth
472       \fi
473       \ifdim\rightmarginwidth>\zeropoint
474         \kern\rightmargindistance
475         \ifcsname\??layoutbackgrounds#1\v!rightmargin\endcsname
476           \page_backgrounds_set_box_cell#1\v!rightmargin\rightmarginwidth#2%
477         \else
478           \kern\rightmarginwidth
479         \fi
480       \fi
481       \ifdim\rightedgewidth>\zeropoint
482         \kern\rightedgedistance
483         \ifcsname\??layoutbackgrounds#1\v!rightedge\endcsname
484           \page_backgrounds_set_box_cell#1\v!rightedge\rightedgewidth#2%
485         \else
486           \kern\rightedgewidth
487         \fi
488       \fi
489     \egroup\egroup
490   \wd\scratchbox\zeropoint
491   \box\scratchbox\relax}
492
493% these are fake framed .. maybe it's nicer to honor foreground here as well
494% but it's probably a slow downer
495
496\def\page_backgrounds_set_box_cell#1#2#3#4% pos pos width height
497  {\begingroup
498   \edef\p_page_backgrounds_setups{\namedframedparameter{\??layoutbackgrounds#1#2}\c!setups}%
499   \ifempty\p_page_backgrounds_setups \else
500     \doprocesslocalsetups\p_page_backgrounds_setups % should not produce funny spaces !
501   \fi
502   \edef\p_page_backgrounds_command{\namedframedparameter{\??layoutbackgrounds#1#2}\c!command}%
503   \ifempty\p_page_backgrounds_command
504     \expandafter\page_backgrounds_set_box_cell_nop
505   \else
506     \expandafter\page_backgrounds_set_box_cell_yes
507   \fi#3#4%
508   \localbackgroundframed{\??layoutbackgrounds#1#2}{#1:#2}\scratchbox
509   \endgroup}
510
511\def\page_backgrounds_set_box_cell_nop#1#2%
512  {\setbox\scratchbox\emptyvbox
513   \wd\scratchbox#1%
514   \ht\scratchbox#2}
515
516\def\page_backgrounds_set_box_cell_yes#1#2%
517  {\setbox\scratchbox\vpack to #2{\vss\hpack to#1{\hss\p_page_backgrounds_command\hss}\vss}%
518   \dp\scratchbox\zeropoint}
519
520%D The background mechanism is quite demanding in terms or resources. We used to
521%D delay these definitions till runtime usage, but since today's \TEX's are large,
522%D we now do the work on forehand.
523%D
524%D \starttyping
525%D \setupbackgrounds [settings]
526%D \setupbackgrounds [paper,page,text,..] [settings]
527%D \setupbackgrounds [top,...] [leftedge,...] [settings]
528%D \stoptyping
529%D
530%D \showsetup{setupbackgrounds}
531%D
532%D Because the number of arguments runs from one to three, we need to check
533%D for it.
534
535\newtoks\everybackgroundssetup
536
537\permanent\tolerant\protected\def\setupbackgrounds[#S#1]#*[#S#2]#*[#S#3]%
538  {\ifarguments
539     \expandafter\gobblethreeoptionals
540   \or
541     \expandafter\page_backgrounds_setup_basics
542   \or
543     \expandafter\page_backgrounds_setup_single
544   \or
545     \expandafter\page_backgrounds_setup_double
546   \fi[#1][#2][#3]}
547
548\newdimension\d_page_backgrounds_offset
549\newdimension\d_page_backgrounds_depth
550
551\appendtoks
552   \edef\p_page_backgrounds_offset{\namedframedparameter{\??layoutbackgrounds\v!page}\c!offset}%
553   \edef\p_page_backgrounds_depth {\namedframedparameter{\??layoutbackgrounds\v!page}\c!depth }%
554   \edef\p_page_backgrounds_state {\namedframedparameter{\??layoutbackgrounds       }\c!state }%
555   \global\d_page_backgrounds_offset\ifempty\p_offset\zeropoint\else\p_page_backgrounds_offset\fi
556   \global\d_page_backgrounds_depth \ifempty\p_depth \zeropoint\else\p_page_backgrounds_depth \fi
557   \global\pagebackgroundoffset\d_page_backgrounds_offset\relax
558   \global\pagebackgrounddepth \d_page_backgrounds_depth \relax
559   \ifx\p_page_backgrounds_state\v!stop
560     \global\c_page_backgrounds_new\conditionalfalse
561   \else
562     \global\c_page_backgrounds_new\conditionaltrue
563   \fi
564\to \everybackgroundssetup
565
566\def\v_page_backgrounds_double_set{\v!paper,\v!page,\v!leftpage,\v!rightpage}
567\def\v_page_backgrounds_single_set{\v!text,\v!hidden,\v!paper,\v!page,\v!leftpage,\v!rightpage}
568\def\v_page_backgrounds_common_set{\v!leftedge,\v!leftmargin,\v!text,\v!rightmargin,\v!rightedge}
569
570\let\page_backgrounds_setup_step_nested\relax
571
572\protected\def\page_backgrounds_setup_double[#1][#2][#S#3]% if needed we can speed this up
573  {\global\c_page_backgrounds_some\conditionaltrue
574   \def\page_backgrounds_setup_step##1%
575     {\ifinset{##1}\v_page_backgrounds_double_set
576        \page_backgrounds_setup_and_check{##1}{#3}%
577      \else
578        \def\page_backgrounds_setup_step_nested####1{\page_backgrounds_setup_and_check{##1####1}{#3}}%
579        \processcommacommand[#2]\page_backgrounds_setup_step_nested
580      \fi}%
581   \processcommacommand[#1]\page_backgrounds_setup_step
582   \expand\everybackgroundssetup}
583
584\protected\def\page_backgrounds_setup_single[#1][#S#2][#-]%
585  {\global\c_page_backgrounds_some\conditionaltrue
586   \ifcommon{#1}\v_page_backgrounds_single_set
587     \def\page_backgrounds_setup_step##1{\page_backgrounds_setup_and_check{##1}{#2}}%
588     \processcommacommand[#1]\page_backgrounds_setup_step
589     \expand\everybackgroundssetup
590   \else
591     \page_backgrounds_setup_double[#1][\v_page_backgrounds_common_set][#2]%
592   \fi}
593
594\protected\def\page_backgrounds_setup_basics[#S#1][#-][#-]%
595  {\setupframed[\??layoutbackgrounds][#1]%
596   \expand\everybackgroundssetup}
597
598\protected\def\page_backgrounds_setup_and_check#1#2% tag settings
599  {\cdef\currentotrbackground{\??layoutbackgrounds#1}%
600   \setupframed[\currentotrbackground][#2]%
601   \page_backgrounds_check_background}
602
603%D Each areas (currently there are $1+3+25+1=30$ of them) has its own low level
604%D framed object associated.
605
606\definesimplifiedframed[\??layoutbackgrounds\v!paper]
607\definesimplifiedframed[\??layoutbackgrounds\v!page]
608\definesimplifiedframed[\??layoutbackgrounds\v!leftpage]
609\definesimplifiedframed[\??layoutbackgrounds\v!rightpage]
610
611\definesimplifiedframed[\??layoutbackgrounds\v!text]
612\definesimplifiedframed[\??layoutbackgrounds\v!hidden]
613
614\definesimplifiedframed[\??layoutbackgrounds\v!top\v!leftedge]
615\definesimplifiedframed[\??layoutbackgrounds\v!top\v!leftmargin]
616\definesimplifiedframed[\??layoutbackgrounds\v!top\v!text]
617\definesimplifiedframed[\??layoutbackgrounds\v!top\v!rightmargin]
618\definesimplifiedframed[\??layoutbackgrounds\v!top\v!rightedge]
619
620\definesimplifiedframed[\??layoutbackgrounds\v!header\v!leftedge]
621\definesimplifiedframed[\??layoutbackgrounds\v!header\v!leftmargin]
622\definesimplifiedframed[\??layoutbackgrounds\v!header\v!text]
623\definesimplifiedframed[\??layoutbackgrounds\v!header\v!rightmargin]
624\definesimplifiedframed[\??layoutbackgrounds\v!header\v!rightedge]
625
626\definesimplifiedframed[\??layoutbackgrounds\v!text\v!leftedge]
627\definesimplifiedframed[\??layoutbackgrounds\v!text\v!leftmargin]
628\definesimplifiedframed[\??layoutbackgrounds\v!text\v!text]
629\definesimplifiedframed[\??layoutbackgrounds\v!text\v!rightmargin]
630\definesimplifiedframed[\??layoutbackgrounds\v!text\v!rightedge]
631
632\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!leftedge]
633\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!leftmargin]
634\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!text]
635\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!rightmargin]
636\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!rightedge]
637
638\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!leftedge]
639\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!leftmargin]
640\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!text]
641\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!rightmargin]
642\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!rightedge]
643
644\setupbackgrounds
645  [\v!page]
646  [\c!offset=\zeropoint, % hm, so we need to force overlay elsewhere
647   \c!depth=\zeropoint]
648
649%D General setup:
650
651\setupbackgrounds
652  [\c!state=\c!start]
653
654%D The hidden layer can be populated by extending the following comma separated
655%D list. This only happens in core modules.
656
657% todo page-2   .. page+2   achter pagina -> bleed
658%      spread-2 .. spread+2 achter spread -> spread (repeat 2 times)
659
660\permanent\protected\def\enablehiddenbackground
661  {\global\c_page_backgrounds_hidden_enabled\conditionaltrue
662   \global\c_page_backgrounds_some\conditionaltrue
663   \page_backgrounds_recalculate}
664
665\permanent\protected\def\disablehiddenbackground
666  {\global\c_page_backgrounds_hidden_enabled\conditionalfalse}
667
668\immutable\def\hiddenbackgroundlist
669  {\v!text-2,\v!text-1,\v!foreground,\v!text+1,\v!text+2}
670
671\permanent\protected\def\prependhiddenbackground#1{\enforced\edef\hiddenbackgroundlist{#1,\hiddenbackgroundlist}}
672\permanent\protected\def\appendhiddenbackground #1{\enforced\edef\hiddenbackgroundlist{\hiddenbackgroundlist,#1}}
673
674\defineoverlay[\v!text-2][\positionregionoverlay\textanchor{\v!text-2}] % no new anchor, we share text
675\defineoverlay[\v!text-1][\positionregionoverlay\textanchor{\v!text-1}]
676\defineoverlay[\v!text+1][\positionregionoverlay\textanchor{\v!text+1}]
677\defineoverlay[\v!text+2][\positionregionoverlay\textanchor{\v!text+2}]
678
679\setupbackgrounds
680  [\v!hidden]
681  [\c!background=\hiddenbackgroundlist]
682
683%D Because we haven't really set up backgrounds yet, we set the main efficiency
684%D switch to false.
685
686\c_page_backgrounds_some\conditionalfalse
687
688%D Sometimes you have a document wide (page) background but need to overload it
689%D locally. In such case (at least in my experience) the only values that get set
690%D are the background and backgroundcolor (if set at all). A full inheritance chain
691%D would complicate things because then we need to use named backgrounds which in
692%D turn will make this mechanism slower. I considered independent local backgrounds
693%D but that also complicates the code (not that much) but isolation means that we
694%D need to set more parameters each time. The following simple approach proabbly
695%D suits most usage.
696%D
697%D \starttyping
698%D \starttext
699%D     \setupbackgrounds[page][background=color,backgroundcolor=red]
700%D     \input tufte \page
701%D     \setupbackgrounds[page][background=,backgroundcolor=]
702%D     \input tufte \page
703%D     \setupbackgrounds[page][background=color,backgroundcolor=red]
704%D     \input tufte \page
705%D     \pushbackground[page]
706%D         \setupbackgrounds[page][background=color,backgroundcolor=green]
707%D         \input tufte \page
708%D     \popbackground
709%D     \input tufte \page
710%D \stoptext
711%D \stoptyping
712
713\permanent\protected\def\pushbackground[#1]%
714  {\pushmacro\popbackground
715   \cdef\currentotrbackground{\??layoutbackgrounds#1}%
716   \enforced\permanent\protected\edef\popbackground
717     {\setupframed
718        [\currentotrbackground]
719        [\c!background=\namedframedparameter{\currentotrbackground}\c!background,
720         \c!backgroundcolor=\namedframedparameter{\currentotrbackground}\c!backgroundcolor]%
721      \page_backgrounds_check_background
722      \popmacro\popbackground}%
723   \setupframed
724     [\currentotrbackground]
725     [\c!background=,\c!backgroundcolor=]%
726   \page_backgrounds_check_background}
727
728\permanent\let\popbackground\relax
729
730\stopcontextdefinitioncode
731
732\protect \endinput
733
734% %D The next series is used in local (for instance floating) backgrounds.
735%
736% \installsimplifiedframed{\??layoutbackgrounds\v!local}
737%
738% \getparameters
739%   [\??layoutbackgrounds\v!local]
740%   [\c!component=local,
741%    \c!background=\localbackgroundlist]
742%
743% \def\localbackgroundlist
744%   {\v!local-2,\v!local-1,\v!foreground,\v!local+1,\v!local+2}
745%
746% \defineoverlay[\v!local-2][\positionoverlay{\v!local-2}] % todo share
747% \defineoverlay[\v!local-1][\positionoverlay{\v!local-1}]
748% \defineoverlay[\v!local+1][\positionoverlay{\v!local+1}]
749% \defineoverlay[\v!local+2][\positionoverlay{\v!local+2}]
750%
751% \def\page_backgrounds_add_local_to_box
752%   {\ifconditional\c_page_backgrounds_hidden_enabled
753%      \expandafter\page_backgrounds_add_local_to_box_indeed
754%    \else
755%      \expandafter\gobbleoneargument
756%    \fi}
757%
758% \def\page_backgrounds_add_local_to_box_indeed#1%
759%   {\setbox#1\hbox{\localbackgroundframed{\??layoutbackgrounds\v!local}\v!local#1}%
760%    \global\advanceby\localpositionnumber\plusone} % afterwards !
761%
762% \let\page_backgrounds_add_local_to_box\gobbleoneargument
763
764% Test how previous macro behaves with depth:
765%
766% \startcolumnset
767% \input tufte
768% \placefigure{none}{\framed[lines=5]{xxx}}
769% \input tufte
770% \placefigure{none}{\starttabulate\NC test\nc test\NC\NR\stoptabulate}
771% \input tufte
772% \stopcolumnset
773