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\startcontextdefinitioncode
23
24
25
26
27
28
29
30
31
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
53
54
55\mutable\lettonothing\currentotrbackground
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138\protected\def\page_backgrounds_check_background
139 {\unless\ifcsname\??framed\currentotrbackground:\c!background\endcsname
140 \page_backgrounds_check_frame
141 \orelse\ifempty{\lastnamedcs}
142
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
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
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
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
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
193 \page_backgrounds_set_yes
194 \else
195 \page_backgrounds_set_nop
196 \fi}
197
198
199
200
201\installcorenamespace{layoutbackgrounds}
202\installcorenamespace{layoutbackgroundcheck}
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
228
229
230\def\page_backgrounds_add_to_box#1
231 {\ifcsname\??layoutbackgrounds#1\endcsname
232 \expandafter\page_backgrounds_add_to_box_indeed
233 \else
234 \expandafter\gobblefourarguments
235 \fi#1}
236
237
238
239
240
241\def\page_backgrounds_add_to_box_indeed#1#2#3#4
242 {\edef\p_page_backgrounds_setups{\namedframedparameter{\??layoutbackgrounds#1}\c!setups}
243 \ifempty\p_page_backgrounds_setups \else
244 \doprocesslocalsetups\p_page_backgrounds_setups
245 \fi
246
247 \setbox#2\hpack{\localbackgroundframed{\??layoutbackgrounds#1}#1#2}}
248
249
250
251
252
253
254\def\page_backgrounds_add_to_print#1
255 {\page_backgrounds_add_to_box\v!paper#1\printpaperwidth\printpaperheight}
256
257
258
259
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
269
270
271
272
273\newbox\leftbackground
274\newbox\rightbackground
275
276
277
278
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
285 \fi
286 \page_backgrounds_add_to_box\v!text#1\makeupwidth\textheight}
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
326 \page_backgrounds_set_box\leftbackground
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
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
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
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
355\newdimension\pagebackgroundvoffset
356\newdimension\pagebackgroundoffset
357\newdimension\pagebackgrounddepth
358\newdimension\pagebackgroundcompensation
359
360\def\page_backgrounds_set_offsets
361 {\ifconditional\c_page_backgrounds_some \ifconditional\c_page_backgrounds_new
362 \page_backgrounds_set_offsets_indeed
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
412
413
414\def\page_backgrounds_set_box#1
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
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
494
495
496\def\page_backgrounds_set_box_cell#1#2#3#4
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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
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]
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
599 {\cdef\currentotrbackground{\??layoutbackgrounds#1}
600 \setupframed[\currentotrbackground][#2]
601 \page_backgrounds_check_background}
602
603
604
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,
647 \c!depth=\zeropoint]
648
649
650
651\setupbackgrounds
652 [\c!state=\c!start]
653
654
655
656
657
658
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!text2,\v!text1,\v!foreground,\v!text1,\v!text2}
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!text2][\positionregionoverlay\textanchor{\v!text2}]
675\defineoverlay[\v!text1][\positionregionoverlay\textanchor{\v!text1}]
676\defineoverlay[\v!text1][\positionregionoverlay\textanchor{\v!text1}]
677\defineoverlay[\v!text2][\positionregionoverlay\textanchor{\v!text2}]
678
679\setupbackgrounds
680 [\v!hidden]
681 [\c!background=\hiddenbackgroundlist]
682
683
684
685
686\c_page_backgrounds_some\conditionalfalse
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773 |