1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18\writestatus{loading}{ConTeXt Page Macros Backgrounds}
19
20\unprotect
21
22
23
24
25
26
27
28
29
30
31\newconditional\c_page_backgrounds_new
32\newconditional\c_page_backgrounds_new_right
33\newconditional\c_page_backgrounds_new_left
34\newconditional\c_page_backgrounds_some
35
36\appendtoks
37 \ifconditional\c_page_backgrounds_some
38 \ifconditional\c_page_backgrounds_new
39 \setsystemmode\v!background
40 \fi
41 \fi
42\to \everybeforepagebody
43
44\unexpanded\def\initializepagebackgrounds
45 {\setfalse\c_page_backgrounds_new
46 \setfalse\c_page_backgrounds_new_right
47 \setfalse\c_page_backgrounds_new_left
48 \setfalse\c_page_backgrounds_some}
49
50
51
52
53\let\currentotrbackground\empty
54
55
56
57
58\unexpanded\def\page_backgrounds_check_background
59 {\ifcsname\??framed\currentotrbackground:\c!background\endcsname
60
61 \edef\page_background_temp{\lastnamedcs}
62 \ifx\page_background_temp\empty
63 \expandafter\expandafter\expandafter\page_backgrounds_check_frame
64 \else
65 \page_backgrounds_set_yes
66 \fi
67 \else
68 \expandafter\page_backgrounds_check_frame
69 \fi}
70
71\def\page_backgrounds_check_frame
72 {\ifcsname\??framed\currentotrbackground:\c!frame\endcsname
73
74 \edef\page_background_temp{\lastnamedcs}
75 \ifx\page_background_temp\v!on
76 \page_backgrounds_set_yes
77 \else
78 \expandafter\expandafter\expandafter\page_backgrounds_check_leftframe
79 \fi
80 \else
81 \expandafter\page_backgrounds_check_leftframe
82 \fi}
83
84\def\page_backgrounds_check_leftframe
85 {\ifcsname\??framed\currentotrbackground:\c!leftframe\endcsname
86
87 \edef\page_background_temp{\lastnamedcs}
88 \ifx\page_background_temp\v!on
89 \page_backgrounds_set_yes
90 \else
91 \expandafter\expandafter\expandafter\page_backgrounds_check_rightframe
92 \fi
93 \else
94 \expandafter\page_backgrounds_check_rightframe
95 \fi}
96
97\def\page_backgrounds_check_rightframe
98 {\ifcsname\??framed\currentotrbackground:\c!rightframe\endcsname
99
100 \edef\page_background_temp{\lastnamedcs}
101 \ifx\page_background_temp\v!on
102 \page_backgrounds_set_yes
103 \else
104 \expandafter\expandafter\expandafter\page_backgrounds_check_topframe
105 \fi
106 \else
107 \expandafter\page_backgrounds_check_topframe
108 \fi}
109
110\def\page_backgrounds_check_topframe
111 {\ifcsname\??framed\currentotrbackground:\c!topframe\endcsname
112
113 \edef\page_background_temp{\lastnamedcs}
114 \ifx\page_background_temp\v!on
115 \page_backgrounds_set_yes
116 \else
117 \expandafter\expandafter\expandafter\page_backgrounds_check_bottomframe
118 \fi
119 \else
120 \expandafter\page_backgrounds_check_bottomframe
121 \fi}
122
123\def\page_backgrounds_check_bottomframe
124 {\ifcsname\??framed\currentotrbackground:\c!bottomframe\endcsname
125
126 \edef\page_background_temp{\lastnamedcs}
127 \ifx\page_background_temp\v!on
128 \page_backgrounds_set_yes
129 \else
130 \page_backgrounds_set_nop
131 \fi
132 \else
133 \page_backgrounds_set_nop
134 \fi}
135
136
137
138
139\installcorenamespace{layoutbackgrounds}
140\installcorenamespace{layoutbackgroundcheck}
141
142\def\page_backgrounds_set_yes{\expandafter\let\csname\currentotrbackground\endcsname\relax }
143\def\page_backgrounds_set_nop{\expandafter\let\csname\currentotrbackground\endcsname\undefined}
144
145\unexpanded\def\page_backgrounds_check#1
146 {\edef\currentotrbackground{\??layoutbackgrounds#1}
147 \page_backgrounds_check_background}
148
149\def\doifelsesomebackground#1
150 {\ifcsname\??layoutbackgrounds#1\endcsname
151 \expandafter\firstoftwoarguments
152 \else
153 \expandafter\secondoftwoarguments
154 \fi}
155
156\let\doifsomebackgroundelse\doifelsesomebackground
157
158\def\doifsomebackground#1
159 {\ifcsname\??layoutbackgrounds#1\endcsname
160 \expandafter\firstofoneargument
161 \else
162 \expandafter\gobbleoneargument
163 \fi}
164
165
166
167
168\def\page_backgrounds_add_to_box#1
169 {\ifcsname\??layoutbackgrounds#1\endcsname
170 \expandafter\page_backgrounds_add_to_box_indeed
171 \else
172 \expandafter\gobblefourarguments
173 \fi#1}
174
175
176
177
178
179\def\page_backgrounds_add_to_box_indeed#1#2#3#4
180 {\edef\p_page_backgrounds_setups{\namedframedparameter{\??layoutbackgrounds#1}\c!setups}
181 \ifx\p_page_backgrounds_setups\empty \else
182 \doprocesslocalsetups\p_page_backgrounds_setups
183 \fi
184
185 \setbox#2\hpack{\localbackgroundframed{\??layoutbackgrounds#1}#1#2}}
186
187
188
189
190
191
192\def\page_backgrounds_add_to_print#1
193 {\page_backgrounds_add_to_box\v!paper#1\printpaperwidth\printpaperheight}
194
195
196
197
198
199\def\page_backgrounds_add_to_paper#1
200 {\doifbothsidesoverruled
201 {\page_backgrounds_add_to_box\v!rightpage#1\paperwidth\paperheight}
202 {\page_backgrounds_add_to_box\v!rightpage#1\paperwidth\paperheight}
203 {\page_backgrounds_add_to_box\v!leftpage #1\paperwidth\paperheight}
204 \page_backgrounds_add_to_box\v!page#1\paperwidth\paperheight}
205
206
207
208
209
210
211\newbox\leftbackground
212\newbox\rightbackground
213
214
215
216
217
218\newconditional\c_page_backgrounds_hidden_enabled
219
220\def\page_backgrounds_add_to_text#1
221 {\ifconditional\c_page_backgrounds_hidden_enabled
222 \page_backgrounds_add_to_box\v!hidden#1\makeupwidth\textheight
223 \fi
224 \page_backgrounds_add_to_box\v!text#1\makeupwidth\textheight}
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240\def\page_backgrounds_recalculate
241 {\global\settrue\c_page_backgrounds_new}
242
243\def\page_backgrounds_set_boxes
244 {\ifconditional\c_page_backgrounds_new
245 \page_backgrounds_set_boxes_r
246 \fi
247 \doifbothsides
248 \page_backgrounds_set_boxes_a
249 \page_backgrounds_set_boxes_b
250 \page_backgrounds_set_boxes_c
251 \ifx\p_page_backgrounds_state\v!repeat\else
252 \global\setfalse\c_page_backgrounds_new
253 \fi}
254
255\def\page_backgrounds_set_boxes_r
256 {\global\settrue\c_page_backgrounds_new_right
257 \global\settrue\c_page_backgrounds_new_left
258 \global\setbox\leftbackground\emptybox
259 \global\setbox\rightbackground\emptybox}
260
261\def\page_backgrounds_set_boxes_a
262 {\ifconditional\c_page_backgrounds_new_left
263
264 \page_backgrounds_set_box\leftbackground
265 \global\setfalse\c_page_backgrounds_new_left
266 \global\setfalse\c_page_backgrounds_new_right
267 \fi}
268
269\def\page_backgrounds_set_boxes_b
270 {\ifconditional\c_page_backgrounds_new_left
271
272 \page_backgrounds_set_box\leftbackground
273 \global\setfalse\c_page_backgrounds_new_left
274 \fi}
275
276\def\page_backgrounds_set_boxes_c
277 {\ifconditional\c_page_backgrounds_new_right
278
279 \page_backgrounds_set_box\rightbackground
280 \global\setfalse\c_page_backgrounds_new_right
281 \fi}
282
283\def\page_backgrounds_add_to_main#1
284 {\ifconditional\c_page_backgrounds_some
285 \page_backgrounds_set_boxes
286 \setbox#1\vpack
287 {\offinterlineskip
288 \doifelsemarginswap{\copy\leftbackground}{\copy\rightbackground}
289 \box#1}
290 \fi}
291
292\newdimen\pagebackgroundhoffset
293\newdimen\pagebackgroundvoffset
294\newdimen\pagebackgroundoffset
295\newdimen\pagebackgrounddepth
296\newdimen\pagebackgroundcompensation
297
298\def\page_backgrounds_set_offsets
299 {\ifconditional\c_page_backgrounds_some \ifconditional\c_page_backgrounds_new
300 \page_backgrounds_set_offsets_indeed
301 \fi \fi}
302
303\def\page_backgrounds_set_offsets_indeed
304 {\ifcsname\??layoutbackgrounds\v!text\v!text\endcsname
305 \page_backgrounds_set_offsets_yes
306 \else\ifcsname\??layoutbackgrounds\v!text\endcsname
307 \page_backgrounds_set_offsets_yes
308 \else
309 \page_backgrounds_set_offsets_nop
310 \fi\fi}
311
312\def\page_backgrounds_set_offsets_nop
313 {\global\pagebackgroundhoffset \zeropoint
314 \global\pagebackgroundvoffset \zeropoint
315 \global\pagebackgrounddepth \zeropoint
316 \global\pagebackgroundcompensation\zeropoint}
317
318\def\page_backgrounds_set_offsets_yes
319 {\global\pagebackgroundoffset \d_page_backgrounds_depth
320 \global\pagebackgroundcompensation\d_page_backgrounds_offset\relax
321 \ifzeropt\pagebackgroundcompensation
322 \page_backgrounds_set_offsets_nop
323 \else
324 \ifcsname\??layoutbackgrounds\v!top\v!text\endcsname
325 \global\pagebackgroundhoffset\zeropoint
326 \else\ifcsname\??layoutbackgrounds\v!bottom\v!text\endcsname
327 \global\pagebackgroundhoffset\zeropoint
328 \else
329 \global\pagebackgroundhoffset\pagebackgroundcompensation
330 \fi\fi
331 \ifcsname\??layoutbackgrounds\v!text\v!rightedge\endcsname
332 \global\pagebackgroundvoffset\zeropoint
333 \global\pagebackgrounddepth \zeropoint
334 \else\ifcsname\??layoutbackgrounds\v!text\v!leftedge\endcsname
335 \global\pagebackgroundvoffset\zeropoint
336 \global\pagebackgrounddepth \zeropoint
337 \else
338 \global\pagebackgroundvoffset\pagebackgroundcompensation
339 \global\pagebackgrounddepth \d_page_backgrounds_depth
340 \fi\fi
341 \fi}
342
343\appendtoks
344 \page_backgrounds_set_offsets
345\to \everybeforepagebody
346
347\newconditional\swapbackgroundmargins \settrue\swapbackgroundmargins
348
349\def\page_backgrounds_set_box#1
350 {\global\setbox#1\vpack
351 {\dontcomplain
352 \swapmargins
353 \ifconditional\swapbackgroundmargins
354 \doifelsemarginswap \donothing
355 {\swapmacros\v!rightmargin\v!leftmargin
356 \swapmacros\v!rightedge \v!leftedge}
357 \fi
358 \calculatereducedvsizes
359 \offinterlineskip
360
361
362
363 \ifdim\topheight>\zeropoint
364 \kern\dimexpr\topheight\topdistance\relax
365 \page_backgrounds_set_box_row\v!top\topheight
366 \kern\topdistance
367 \fi
368 \ifdim\headerheight>\zeropoint
369 \page_backgrounds_set_box_row\v!header\headerheight
370 \kern\headerdistance
371 \fi
372 \ifdim\textheight>\zeropoint
373 \page_backgrounds_set_box_row\v!text\textheight
374 \fi
375 \ifdim\footerheight>\zeropoint
376 \kern\footerdistance
377 \page_backgrounds_set_box_row\v!footer\footerheight
378 \fi
379 \ifdim\bottomheight>\zeropoint
380 \kern\bottomdistance
381 \page_backgrounds_set_box_row\v!bottom\bottomheight
382 \fi
383 \vfilll}
384 \smashbox#1}
385
386\def\page_backgrounds_set_box_row#1#2
387 {\setbox\scratchbox\vpack to #2
388 \bgroup\hpack\bgroup
389 \goleftonpage
390 \ifdim\leftedgewidth>\zeropoint
391 \ifcsname\??layoutbackgrounds#1\v!leftedge\endcsname
392 \page_backgrounds_set_box_cell#1\v!leftedge\leftedgewidth#2
393 \else
394 \kern\leftedgewidth
395 \fi
396 \kern\leftedgedistance
397 \fi
398 \ifdim\leftmarginwidth>\zeropoint
399 \ifcsname\??layoutbackgrounds#1\v!leftmargin\endcsname
400 \page_backgrounds_set_box_cell#1\v!leftmargin\leftmarginwidth#2
401 \else
402 \kern\leftmarginwidth
403 \fi
404 \kern\leftmargindistance
405 \fi
406 \ifcsname\??layoutbackgrounds#1\v!text\endcsname
407 \page_backgrounds_set_box_cell#1\v!text\makeupwidth#2
408 \else
409 \kern\makeupwidth
410 \fi
411 \ifdim\rightmarginwidth>\zeropoint
412 \kern\rightmargindistance
413 \ifcsname\??layoutbackgrounds#1\v!rightmargin\endcsname
414 \page_backgrounds_set_box_cell#1\v!rightmargin\rightmarginwidth#2
415 \else
416 \kern\rightmarginwidth
417 \fi
418 \fi
419 \ifdim\rightedgewidth>\zeropoint
420 \kern\rightedgedistance
421 \ifcsname\??layoutbackgrounds#1\v!rightedge\endcsname
422 \page_backgrounds_set_box_cell#1\v!rightedge\rightedgewidth#2
423 \else
424 \kern\rightedgewidth
425 \fi
426 \fi
427 \egroup\egroup
428 \wd\scratchbox\zeropoint
429 \box\scratchbox\relax}
430
431
432
433
434\def\page_backgrounds_set_box_cell#1#2#3#4
435 {\begingroup
436 \edef\p_page_backgrounds_setups{\namedframedparameter{\??layoutbackgrounds#1#2}\c!setups}
437 \ifx\p_page_backgrounds_setups\empty \else
438 \doprocesslocalsetups\p_page_backgrounds_setups
439 \fi
440 \edef\p_page_backgrounds_command{\namedframedparameter{\??layoutbackgrounds#1#2}\c!command}
441 \ifx\p_page_backgrounds_command\empty
442 \expandafter\page_backgrounds_set_box_cell_nop
443 \else
444 \expandafter\page_backgrounds_set_box_cell_yes
445 \fi#3#4
446 \localbackgroundframed{\??layoutbackgrounds#1#2}{#1:#2}\scratchbox
447 \endgroup}
448
449\def\page_backgrounds_set_box_cell_nop#1#2
450 {\setbox\scratchbox\emptyvbox
451 \wd\scratchbox#1
452 \ht\scratchbox#2}
453
454\def\page_backgrounds_set_box_cell_yes#1#2
455 {\setbox\scratchbox\vpack to #2{\vss\hpack to#1{\hss\p_page_backgrounds_command\hss}\vss}
456 \dp\scratchbox\zeropoint}
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473\newtoks\everybackgroundssetup
474
475\unexpanded\def\setupbackgrounds
476 {\dotripleempty\page_backgrounds_setup}
477
478\def\page_backgrounds_setup
479 {\ifthirdargument
480 \expandafter\page_backgrounds_setup_double
481 \else\ifsecondargument
482 \doubleexpandafter\page_backgrounds_setup_single
483 \else\iffirstargument
484 \tripleexpandafter\page_backgrounds_setup_basics
485 \fi\fi\fi}
486
487\newdimen\d_page_backgrounds_offset
488\newdimen\d_page_backgrounds_depth
489
490\appendtoks
491 \edef\p_page_backgrounds_offset{\namedframedparameter{\??layoutbackgrounds\v!page}\c!offset}
492 \edef\p_page_backgrounds_depth {\namedframedparameter{\??layoutbackgrounds\v!page}\c!depth }
493 \edef\p_page_backgrounds_state {\namedframedparameter{\??layoutbackgrounds }\c!state }
494 \global\d_page_backgrounds_offset\ifx\p_offset\empty\zeropoint\else\p_page_backgrounds_offset\fi
495 \global\d_page_backgrounds_depth \ifx\p_depth \empty\zeropoint\else\p_page_backgrounds_depth \fi
496 \global\pagebackgroundoffset\d_page_backgrounds_offset\relax
497 \global\pagebackgrounddepth \d_page_backgrounds_depth \relax
498 \ifx\p_page_backgrounds_state\v!stop
499 \global\setfalse\c_page_backgrounds_new
500 \else
501 \global\settrue \c_page_backgrounds_new
502 \fi
503\to \everybackgroundssetup
504
505\def\v_page_backgrounds_double_set{\v!paper,\v!page,\v!leftpage,\v!rightpage}
506\def\v_page_backgrounds_single_set{\v!text,\v!hidden,\v!paper,\v!page,\v!leftpage,\v!rightpage}
507\def\v_page_backgrounds_common_set{\v!leftedge,\v!leftmargin,\v!text,\v!rightmargin,\v!rightedge}
508
509\unexpanded\def\page_backgrounds_setup_double[#1][#2][#3]
510 {\global\settrue\c_page_backgrounds_some
511 \def\page_backgrounds_setup_step##1
512 {\doifelseinset{##1}\v_page_backgrounds_double_set
513 {\page_backgrounds_setup_and_check{##1}{#3}}
514 {\def\page_backgrounds_setup_step_nested####1{\page_backgrounds_setup_and_check{##1####1}{#3}}
515 \processcommacommand[#2]\page_backgrounds_setup_step_nested}}
516 \processcommacommand[#1]\page_backgrounds_setup_step
517 \the\everybackgroundssetup}
518
519\unexpanded\def\page_backgrounds_setup_single[#1][#2][#3]
520 {\global\settrue\c_page_backgrounds_some
521 \doifelsecommon{#1}\v_page_backgrounds_single_set
522 {\def\page_backgrounds_setup_step##1{\page_backgrounds_setup_and_check{##1}{#2}}
523 \processcommacommand[#1]\page_backgrounds_setup_step
524 \the\everybackgroundssetup}
525 {\page_backgrounds_setup_double[#1][\v_page_backgrounds_common_set][#2]}}
526
527\unexpanded\def\page_backgrounds_setup_basics[#1][#2][#3]
528 {\setupframed[\??layoutbackgrounds][#1]
529 \the\everybackgroundssetup}
530
531\unexpanded\def\page_backgrounds_setup_and_check#1#2
532 {\edef\currentotrbackground{\??layoutbackgrounds#1}
533 \setupframed[\currentotrbackground][#2]
534 \page_backgrounds_check_background}
535
536
537
538
539\definesimplifiedframed[\??layoutbackgrounds\v!paper]
540\definesimplifiedframed[\??layoutbackgrounds\v!page]
541\definesimplifiedframed[\??layoutbackgrounds\v!leftpage]
542\definesimplifiedframed[\??layoutbackgrounds\v!rightpage]
543
544\definesimplifiedframed[\??layoutbackgrounds\v!text]
545\definesimplifiedframed[\??layoutbackgrounds\v!hidden]
546
547\definesimplifiedframed[\??layoutbackgrounds\v!top\v!leftedge]
548\definesimplifiedframed[\??layoutbackgrounds\v!top\v!leftmargin]
549\definesimplifiedframed[\??layoutbackgrounds\v!top\v!text]
550\definesimplifiedframed[\??layoutbackgrounds\v!top\v!rightmargin]
551\definesimplifiedframed[\??layoutbackgrounds\v!top\v!rightedge]
552
553\definesimplifiedframed[\??layoutbackgrounds\v!header\v!leftedge]
554\definesimplifiedframed[\??layoutbackgrounds\v!header\v!leftmargin]
555\definesimplifiedframed[\??layoutbackgrounds\v!header\v!text]
556\definesimplifiedframed[\??layoutbackgrounds\v!header\v!rightmargin]
557\definesimplifiedframed[\??layoutbackgrounds\v!header\v!rightedge]
558
559\definesimplifiedframed[\??layoutbackgrounds\v!text\v!leftedge]
560\definesimplifiedframed[\??layoutbackgrounds\v!text\v!leftmargin]
561\definesimplifiedframed[\??layoutbackgrounds\v!text\v!text]
562\definesimplifiedframed[\??layoutbackgrounds\v!text\v!rightmargin]
563\definesimplifiedframed[\??layoutbackgrounds\v!text\v!rightedge]
564
565\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!leftedge]
566\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!leftmargin]
567\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!text]
568\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!rightmargin]
569\definesimplifiedframed[\??layoutbackgrounds\v!footer\v!rightedge]
570
571\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!leftedge]
572\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!leftmargin]
573\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!text]
574\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!rightmargin]
575\definesimplifiedframed[\??layoutbackgrounds\v!bottom\v!rightedge]
576
577\setupbackgrounds
578 [\v!page]
579 [\c!offset=\zeropoint,
580 \c!depth=\zeropoint]
581
582
583
584\setupbackgrounds
585 [\c!state=\c!start]
586
587
588
589
590
591
592
593\unexpanded\def\enablehiddenbackground
594 {\global\settrue\c_page_backgrounds_hidden_enabled
595 \global\settrue\c_page_backgrounds_some
596 \page_backgrounds_recalculate}
597
598\unexpanded\def\disablehiddenbackground
599 {\global\setfalse\c_page_backgrounds_hidden_enabled}
600
601\def\hiddenbackgroundlist
602 {\v!text2,\v!text1,\v!foreground,\v!text1,\v!text2}
603
604\defineoverlay[\v!text2][\positionregionoverlay\textanchor{\v!text2}]
605\defineoverlay[\v!text1][\positionregionoverlay\textanchor{\v!text1}]
606\defineoverlay[\v!text1][\positionregionoverlay\textanchor{\v!text1}]
607\defineoverlay[\v!text2][\positionregionoverlay\textanchor{\v!text2}]
608
609\setupbackgrounds
610 [\v!hidden]
611 [\c!background=\hiddenbackgroundlist]
612
613
614
615
616\setfalse\c_page_backgrounds_some
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643\unexpanded\def\pushbackground[#1]
644 {\pushmacro\popbackground
645 \edef\currentotrbackground{\??layoutbackgrounds#1}
646 \unexpanded\edef\popbackground
647 {\setupframed
648 [\currentotrbackground]
649 [\c!background=\namedframedparameter{\currentotrbackground}\c!background,
650 \c!backgroundcolor=\namedframedparameter{\currentotrbackground}\c!backgroundcolor]
651 \page_backgrounds_check_background
652 \popmacro\popbackground}
653 \setupframed
654 [\currentotrbackground]
655 [\c!background=,\c!backgroundcolor=]
656 \page_backgrounds_check_background}
657
658\let\popbackground\relax
659
660\protect \endinput
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702 |