summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/metafun/metafun-backgrounds.tex
blob: 200620cae4ccb92d4ace70d4a65b428f2def56b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
% language=uk
%
% copyright=pragma-ade readme=readme.pdf licence=cc-by-nc-sa

\startcomponent metafun-backgrounds

\environment metafun-environment

\startchapter[reference=sec:page,title={Page backgrounds}]

\startintro

Especially in interactive documents, adding backgrounds to the page and text
areas not only enhances readability, but also makes it more convenient to
identify header, footers and navigational areas. In this chapter we will
demonstrate that with \METAPOST\ we can go beyond the \TEX\ based features
present in \CONTEXT. One section is dedicated to graphics and printing,
especially bleeding.

\stopintro

\startsection[title={The basic layout}]

\index {layout}

In the \CONTEXT\ manual you can find many details on the composition of the page.
When \TEX\ typesets text, crossing the page boundary triggers \TEX's output
routine. This routine is responsible for pasting the body text that goes onto a
page in the correct area. A simple representation of such a page is:

\startbuffer[a]
\startuseMPgraphic{layout 1}
  pickup pencircle scaled 1mm ;
  fill unitsquare xyscaled (7cm,8cm)
    withcolor .85white ;
  fill unitsquare xyscaled (5cm,5cm) shifted (1cm,1.5cm)
    withcolor .625yellow ;
  fill unitsquare xyscaled (5cm,1cm) shifted (1cm,.5cm)
    withcolor .625red ;
  fill unitsquare xyscaled (5cm,1cm) shifted (1cm,6.5cm)
    withcolor .625red ;
  draw unitsquare xyscaled (5cm,7cm) shifted (1cm,.5cm)
    withcolor .25white ;
  drawarrow (2cm,8cm) -- top (2cm,7.5cm) ;
  drawarrow (0cm,7cm) -- lft (1cm,7cm) ;
  clip currentpicture to unitsquare xyscaled (7cm,8cm) ;
\stopuseMPgraphic
\stopbuffer

\startbuffer[b]
\startuseMPgraphic{layout 2}
  \includeMPgraphic{layout 1}
  clip currentpicture to unitsquare scaled 3cm shifted (0,5cm) ;
  currentpicture := currentpicture scaled 2 shifted (0,-8cm) ;
  setbounds currentpicture to unitsquare xyscaled (6cm,8cm) ;
\stopuseMPgraphic
\stopbuffer

\getbuffer[a,b]

\startbuffer[c]
\startlinecorrection[blank]
\hbox
  {\useMPgraphic{layout 1}\hskip1cm
   \useMPgraphic{layout 2}}
\stoplinecorrection
\stopbuffer

\getbuffer[c]

The red areas are the header and footer, while the yellow areas contains the text
flow. We can turn headers on and off and|/|or hide them. For this reason, the
header, text and footer areas together make up the height of the text.

A close look at the left picture will reveal that the two arrows point to the
center of the lines. This is achieved by the \type {top} and \type {lft}
directives. If we would not have clipped the picture, the arrow would have stuck
half a line width outside the gray area that represents the page. When
constructing such pictures, one should really pay attention to such details,
since it pays off in the overall look and feel of the document.

The vertical arrow represents the top space, while the horizontal arrow denotes
the distance to the back of the cover (back space). By changing their values, you
can shift the main body text on the page. In a double sided layout scheme, the
back space is automatically mirrored on even pages.

An advanced \METAPOST\ user may wonder why we hard code the dimensions, and avoid
\METAPOST's powerful mechanisms for defining relations. Our experience has taught
us that in pictures like this, providing a general solution seldom pays large
dividents or savings in time.

\typebuffer[a]

As you can see, the left graphic is defined as a series of rectangles. The \type
{xyscaled} macro is part of the \CONTEXT\ files, and saves some typing and space.
It is defined as a primary, requiring both left and right operands.

\starttyping
primarydef p xyscaled q =
  p xscaled (xpart q) yscaled (ypart q)
enddef ;
\stoptyping

Zooming in on the top left corner only takes a few lines. First we clip the
correct part, next we scale it up, and finally we let the bounding box suit the
left picture.

\typebuffer[b]

This code demonstrates how you can reuse a graphic inside another one. This
strategy can easily be used to stepwise build (or extend) graphics. The two
graphics were put side by side with the following command. Watch the use of line
correction commands. They optimize the white space around the graphic.

\typebuffer[c]

As soon as you want to make an electronic document, you will want to use
different areas of the screen for different purposes: text, menus, buttons, etc.
For this reason, \CONTEXT\ provides not only left and right margins, but also
additional left and right edge areas and top and bottom margins. These areas are
shown in the figure on the next page.

\startbuffer[aa]
pickup pencircle scaled 2pt ;

numeric w[], h[], x[], y[], u ; u := .5cm ;

numeric width  ; width  := \the\textwidth  ;
numeric height ; height := \the\textheight ;
\stopbuffer

\startbuffer[bb]
w[1] = 2u ; w[2] = 3u ; w[4] = 3u ; w[5] = 2u ;
h[1] = 1u ; h[2] = 1u ; h[4] = 1u ; h[5] = 1u ;

w[1]+w[2]+w[3]+w[4]+w[5]+4u = width  ;
h[1]+h[2]+h[3]+h[4]+h[5]+4u = height ;

x[1] = 1u ;                y[1] = 1u ;
x[2] = x[1] + w[1] + .5u ; y[2] = y[1] + h[1] + .5u ;
x[3] = x[2] + w[2] + .5u ; y[3] = y[2] + h[2] + .5u ;
x[4] = x[3] + w[3] + .5u ; y[4] = y[3] + h[3] + .5u ;
x[5] = x[4] + w[4] + .5u ; y[5] = y[4] + h[4] + .5u ;
\stopbuffer

\startbuffer[cc]
def do_it (expr xx, yy, cc) =
  draw unitsquare
    xyscaled (w[xx],h[yy]) shifted (x[xx],y[yy])
    withcolor if cc : .625red else : .625yellow fi ;
enddef ;

fill unitsquare xyscaled (width,height) withcolor .85white;

do_it (1,1,false) ; do_it (5,1,false) ;
do_it (2,1,false) ; do_it (3,1,false) ; do_it (4,1,false) ;

do_it (1,2,false) ; do_it (5,2,false) ;
do_it (2,2,true ) ; do_it (3,2,true ) ; do_it (4,2,true ) ;

do_it (1,3,false) ; do_it (5,3,false) ;
do_it (2,3,true ) ; do_it (3,3,true ) ; do_it (4,3,true ) ;

do_it (1,4,false) ; do_it (5,4,false) ;
do_it (2,4,true ) ; do_it (3,4,true ) ; do_it (4,4,true ) ;

do_it (1,5,false) ; do_it (5,5,false) ;
do_it (2,5,false) ; do_it (3,5,false) ; do_it (4,5,false) ;
\stopbuffer

\startbuffer[dd]
def do_it (expr yy, tt) =
  path p ;
  p := unitsquare xyscaled (w[1],h[yy]) shifted (x[1],y[yy]) ;
  label.lft(tt, center p shifted (-w[1]/2-u-.25cm,0)) ;
enddef ;

do_it (1,btex bottom etex) ;
do_it (2,btex footer etex) ;
do_it (3,btex text   etex) ;
do_it (4,btex header etex) ;
do_it (5,btex top    etex) ;
\stopbuffer

\startbuffer[ee]
def do_it (expr xx, tt) =
  path p ;
  p := unitsquare xyscaled (w[xx],h[1]) shifted (x[xx],y[1]) ;
  label(tt, center p shifted (0,height-h[1]/2)) ;
enddef ;

do_it (1,btex edge   etex) ;
do_it (2,btex margin etex) ;
do_it (3,btex text   etex) ;
do_it (4,btex margin etex) ;
do_it (5,btex edge   etex) ;
\stopbuffer

\startbuffer[ff]
setbounds currentpicture to
  unitsquare xyscaled (width,height) ;
\stopbuffer

% We use two chars for buffer names, otherwise we can get
% get in conflict with the next buffers with similar names.

\startpostponing
\centerbox{\processMPbuffer[aa,bb,cc,dd,ee,ff]}
\stoppostponing

When defining this graphic, all areas have related dimensions. Here it makes
sense to let \METAPOST\ calculate these dimensions as much as possible. First we
define the five by five matrix of areas. We pass the width and height of the main
text area. Because they are stored in \TEX\ dimension registers, we have to
prefix them by \type {\the}.

\typebuffer[aa]

We now specify the lower left corners using \type {=} instead of the \type {:=},
which means that \METAPOST\ will calculate \type {w[3]} and \type {h[3]} for us.

\typebuffer[bb]

Because we are going to repeat ourselves, we draw the areas using a macro.
Depending on its importance, we color it red or yellow.

\typebuffer[cc]

This picture in itself is not yet explanatory, so we add some labels. Again, we
use a macro, which we feed with a picture generated by \TEX. Since these pictures
are filtered from the source and pre||processed, we cannot embed the \type
{btex}||\type {etex} in the macro \type {do_it} and pass a string. It has to be
done this way. \footnote {This is true only in a regular \METAPOST\ run. In
\CONTEXT\ \MKIV\ we follow a different route.}

\typebuffer[dd]

In the horizontal direction we have edges, margins and text. There are left and
right edges and margins, which are swapped on even pages when you typeset a
double sided document.

\typebuffer[ee]

Since we want the graphic to match the dimensions of the text area of the current
page, we have to make sure that the bounding box is adapted accordingly. By this
action, the labels will fall outside the bounding box. When we directly embed a
graphic, this works ok, but when we start scaling and reusing, due to the object
reuse mechanism the graphic will be clipped to the bounding box.

\typebuffer[ff]

In the following sections we will demonstrate how you can put graphics behind
these 25~areas, as well as behind the (left and right) page.

\stopsection

\startsection[title={Setting up backgrounds}]

\index {overlays}
\index {backgrounds}

One way of protecting a document for unwanted usage is to put an annoying word in
the background. If you like this, you may try the following. The macro \type
{ysized} is part of the macros that come with \CONTEXT\ and scales a picture to a
specific size.

\startbuffer[a]
\startuniqueMPgraphic{concept}
  draw btex \colored[s=.8]{\bf CONCEPT} etex rotated 60 ;
  currentpicture := currentpicture
    ysized (\overlayheight-.5cm) ;
\stopuniqueMPgraphic

\defineoverlay[concept][\uniqueMPgraphic{concept}]
\stopbuffer

\typebuffer[a]

You can now put this graphic in the page background by
saying:

\starttyping
\setupbackgrounds[page][background=concept]
\stoptyping

You may consider the next alternative a bit better, but still it renders the text
unreadable. Like \type {xysized}, the macro \type {enlarged} is not part of
standard \METAPOST, but comes with \CONTEXT.

\startbuffer[b]
\startuniqueMPgraphic{copyright}
  picture p ; p := btex \colored[s=.8]{COPYRIGHT} etex
    rotated 90 ;
  setbounds p to boundingbox p enlarged 1pt ;
  draw p ;
  currentpicture := currentpicture
    xysized (\overlaywidth,\overlayheight) ;
\stopuniqueMPgraphic

\defineoverlay[copyright][\uniqueMPgraphic{copyright}]
\stopbuffer

\typebuffer[b]

Again, we put this graphic in the background. By using a unique graphic, we make
sure that it's rendered only once and reused when possible.

\startbuffer[c]
\setupbackgrounds[text][rightmargin][background=copyright]
\stopbuffer

\typebuffer[c]

\doifnotmode{screen}{\getbuffer[b,c]}

In both cases, we slightly scale down the graphic. We do so because otherwise a
small portion of the text is clipped off. This is unrelated to \TEX\ or
\METAPOST, but a characteristic of the font. Compare the following Pagella, Latin
Modern and Termes gi's (the Pagella is the body font of this text).

\startbuffer
\hbox \bgroup
    \hbox{\definedfont[file:texgyrepagella-regular at 6cm]gi}%
    \hbox{\definedfont[file:lmroman10-regular      at 6cm]gi}%
    \hbox{\definedfont[file:texgyretermes-regular  at 6cm]gi}%
\egroup
\stopbuffer

\typebuffer

\startlinecorrection[blank]
{\showboxes \getbuffer}
\stoplinecorrection

Watch how the bounding boxes differ and sometimes cross the shape. So, in order
not to loose part of a glyph when clipping, you need to add a bit of space. \in
{Figure} [fig:annoying] shows the two backgrounds in action.

\startbuffer
\getbuffer[a,b]
\def\ShowPage#1% % (yet) no image as background to image
  {\framed       % possible due to nested file problems
      [background=#1,offset=overlay]
      {\typesetfile[mfun-900.tex][page=1,width=.4\textwidth]}}
\startcombination
  {\ShowPage{concept}}   {concept}
  {\ShowPage{copyright}} {copyright}
\stopcombination
\stopbuffer

\placefigure
  [here][fig:annoying]
  {Two examples of annoying backgrounds.}
  {\getbuffer}

If you really want to add such texts to a document, in \CONTEXT\ we don't have to
use the page background, but can use one of the layout areas instead (like \type
{[text][text]} or \type {[text][leftmargin]})

\typebuffer[d]

There is one drawback: when your left and right margin have different dimensions,
the text will be scaled differently on odd and even pages. Normally this is no
problem for a draft.

As an alternative you can use the \type {\setuptexts} command and wrap the
graphic in a box with the right dimensions, using code like:

\starttyping
\startuniqueMPgraphic{copyright}
  picture p ; p := btex COPYRIGHT etex rotated 90 ;
  setbounds p to boundingbox p enlarged 1pt ;
  draw p withcolor .8white ;
  xyscale_currentpicture(\the\leftmarginwidth,\the\textheight) ;
\stopuniqueMPgraphic

\setuptexttexts [margin] [] [\uniqueMPgraphic{copyright}]
\stoptyping

The graphic goes into the outer margin. The second argument can be used to put
something in the inner margin.

\stopsection

\startsection[title={Multiple overlays}]

\index{overlays+stack}

\setupbackgrounds[text][rightmargin][background=]

You can stack overlays. Consider the next case, where we assume that you have
enabled interaction support using \type {\setupinteraction[state=start]}:

\starttyping
\setupbackgrounds
  [page]
  [background={color,nextpage},
   backgroundcolor=darkyellow]
\stoptyping

Here, the page gets a colored background and a hyperlink to the next page,
previously defined by:

\starttyping
\defineoverlay[nextpage][\overlaybutton{nextpage}]
\stoptyping

An \type {\overlaybutton} is just a button, with all attributes (color, frame,
etc) set to nothing, having the dimensions of the overlay. The argument is one of
the permitted destinations, like \type {nextpage}, \type {firstpage}, \type
{SearchDocument} and alike.

For efficiency reasons, the background areas (like \type {[text][text]}) are
calculated only when their definition has changed. When a background changes per
page, we have to recalculate it on each page. In the next example, the macro
\type {\overlaybutton} generates a different button on each page. But, since we
don't explicitly set the background at each page, there is no way the background
drawing mechanism can know that this button has changed. Therefore, we must force
recalculation with:

\starttyping
\setupbackgrounds[state=repeat]
\stoptyping

You can test this concept yourself with the following code. Here we assume that
you have a file called \type {tufte.tex} on your system, which is the case if you
have \CONTEXT\ installed. However, you can just as easily use any file having a
paragraph of two of text.

\starttyping
\starttext
\setupinteraction[state=start]
\setupbackgrounds[state=repeat]
\defineoverlay[nextpage][\overlaybutton{nextpage}]
\setupbackgrounds[text][text][background=nextpage]
\dorecurse{20}{\input tufte \par}
\stoptext
\stoptyping

Note that you can move forward from page to page in the resulting \PDF\ file by
clicking on each page with the mouse. Now compile this file without setting the
background state to \type {repeat} and note the difference as you click pages
with the mouse.

Setting the state was not needed when we used the page background:

\starttyping
\setupbackgrounds[page][background=nextpage]
\stoptyping

The \type {\dorecurse} macro is handy for testing since it saves us typing. One
can nest this macro as in:

\starttyping
\dorecurse{20}{\dorecurse{10}{Hello World! }\par}
\stoptyping

The current step is available in \type {\recurselevel} and the depth (nesting
level) in \type {\recursedepth}.

\stopsection

\startsection[title={Crossing borders}]

\index{backgrounds}

In many cases, the previously mentioned background areas will suffice, but in the
case of more complicated backgrounds, you may wish to use \METAPOST\ to draw
graphics that combine or span these areas.

At runtime \CONTEXT\ saves information on the layout that can be picked up by
\METAPOST. The framework for a page graphic is:

\starttyping
StartPage;
  % all kind of commands
StopPage ;
\stoptyping

Between the \type {StartPage} and \type {StopPage} command you have access to a
wide range of variables:

\starttabulate[|l|Tp|]
\HL
\NC page       \NC PaperHeight PaperWidth               \NC \NR
\NC            \NC PrintPaperHeight PrintPaperWidth     \NC \NR
\NC            \NC PageOffset PageDepth                 \NC \NR
\NC margins    \NC TopSpace BackSpace                   \NC \NR
\NC text       \NC MakeupHeight MakeupWidth             \NC \NR
\NC vertical   \NC TopHeight TopDistance                \NC \NR
\NC            \NC HeaderHeight HeaderDistance          \NC \NR
\NC            \NC TextHeight                           \NC \NR
\NC            \NC FooterDistance FooterHeight          \NC \NR
\NC            \NC BottomDistance BottomHeight          \NC \NR
\NC horizontal \NC LeftEdgeWidth LeftEdgeDistance       \NC \NR
\NC            \NC LeftMarginWidth LeftMarginDistance   \NC \NR
\NC            \NC TextWidth                            \NC \NR
\NC            \NC RightMarginDistance RightMarginWidth \NC \NR
\NC            \NC RightEdgeDistance RightEdgeWidth     \NC \NR
\HL
\stoptabulate

Since using these variables to construct paths is not that handy because the
areas are available as predefined paths, which we will demonstrate here.

\placefigure
  [here][fig:back 1]
  {A background with combined areas.}
  {\startcombination
     {\typesetfile[mfun-900.tex][page=2,width=.4\textwidth]}{even}
     {\typesetfile[mfun-900.tex][page=3,width=.4\textwidth]}{odd}
   \stopcombination}

In \in {figure} [fig:back 1] you see two pages (odd and even) with a background
spanning the outer margin and the text area. You can access an area in two ways.
The area itself is available as \type {Area}.

\starttyping
StartPage ;
  fill Area[Text][Text] withcolor .85white ;
StopPage ;
\stoptyping

If you use an area this way, you will notice that it is not positioned at the
right place. An \type {Area} is just a rectangle. If you want a positioned area,
you should use the \type {Field} array:

\starttyping
StartPage ;
  fill Field[Text][Text] withcolor .85white ;
StopPage ;
\stoptyping

The location of an area is available in \type {Location}, so the previous
definition is the same as:

\starttyping
StartPage ;
  fill Area[Text][Text] shifted Location[Text][Text]
    withcolor .85white ;
StopPage ;
\stoptyping

The following definition fills and draws the margin and text areas.

\typebuffer[back-2]

This background is assigned to the page layer by saying:

\typebuffer[back-0]

As you can see in \in {figure} [fig:back 2], the text is typeset rather tightly
between the left and right margins.

\placefigure
  [here][fig:back 2]
  {A background with split areas.}
  {\startcombination
     {\typesetfile[mfun-900.tex][page=4,width=.4\textwidth]}{even}
     {\typesetfile[mfun-900.tex][page=5,width=.4\textwidth]}{odd}
   \stopcombination}

This can easily be solved by enlarging the areas a bit. The next example
demonstrates this on the text area, which is shown in \in {figure} [fig:back 3].

\typebuffer[back-3]

\placefigure
  [here][fig:back 3]
  {A background with enlarged text area.}
  {\startcombination
     {\typesetfile[mfun-900.tex][page=6,width=.4\textwidth]}{even}
     {\typesetfile[mfun-900.tex][page=7,width=.4\textwidth]}{odd}
   \stopcombination}

The \type {enlarged} macro can be used like \type {shifted} and accepts either a
numeric or a pair.

How do we define a background as in \in {figure} [fig:back 1]? Because \type
{Field} provides us the positioned areas, we can use the corners of those.

\typebuffer[back-1]

In this definition we calculate a different path for odd and even pages. When
done, we enlarge the path a bit. If you want to use different offsets in all
directions, you can use moved corner points.

\typebuffer[back-4]

Here we displace the corners randomly which leads to backgrounds like \in
{figure} [fig:back 4]. The following definition would have worked as well:

\typebuffer[back-4x]

\placefigure
  [here][fig:back 4]
  {A random text area.}
  {\startcombination
     {\typesetfile[mfun-900.tex][page=8,width=.4\textwidth]}{even}
     {\typesetfile[mfun-900.tex][page=9,width=.4\textwidth]}{odd}
   \stopcombination}

The previous graphics are defined as usable ones, which means that they will be
recalculated each page. This is rather inefficient when the shapes don't change.
But, using a reusable graphic instead, would result in only one graphic for both
pages. Since the layout for the left and right page differs, another method is
needed.

Instead of putting the same graphic on the page layer, we put two different ones
on the left and right page layer.

\starttyping
\defineoverlay[left page] [\useMPgraphic{left page}]
\defineoverlay[right page][\useMPgraphic{right page}]

\setupbackgrounds[leftpage] [background=left page]
\setupbackgrounds[rightpage][background=right page]
\stoptyping

Now we only have to split the previously defined graphic into two parts. In order
to force consistency, we isolate the code that fills and draws. The left page
code looks like:

\starttyping
\startreusableMPgraphic{left page}
  StartPage ;
    path Main ; Main :=
      llcorner Field[OuterMargin][Text] --
      lrcorner Field[Text]       [Text] --
      urcorner Field[Text]       [Text] --
      ulcorner Field[OuterMargin][Text] -- cycle ;
    \includeMPgraphic{draw page}
  StopPage ;
\stopreusableMPgraphic
\stoptyping

The right page text looks similar:

\starttyping
\startreusableMPgraphic{right page}
  StartPage ;
    path Main ; Main :=
      lrcorner Field[OuterMargin][Text] --
      llcorner Field[Text]       [Text] --
      ulcorner Field[Text]       [Text] --
      urcorner Field[OuterMargin][Text] -- cycle ;
    \includeMPgraphic{draw page}
  StopPage ;
\stopreusableMPgraphic
\stoptyping

Watch how we used a reusable graphic first and a simple usable one next.
Actually, the next graphic is not a stand alone graphic.

\starttyping
\startuseMPgraphic{draw page}
  Main := Main enlarged 6pt ;
  pickup pencircle scaled 2pt ;
  fill Page withcolor .625white ;
  fill Main withcolor .850white ;
  draw Main withcolor .625red ;
\stopuseMPgraphic
\stoptyping

We have seen some predefined paths and locations. Apart from the \type {Page}
path, they take two arguments that specify their position on the layout grid.

\starttabulate[|lT|l|]
\HL
\NC path Area     [][] \NC an area similar to a \CONTEXT\ one    \NC \NR
\NC pair Location [][] \NC the position of this area             \NC \NR
\NC path Field    [][] \NC the area positioned at the right place \NC \NR
\NC path Page          \NC the page itself \NC \NR
\HL
\stoptabulate

Some less used and more obscure variables are the following.

\starttabulate[|lT|l|]
\HL
\NC numeric Hstep [] \NC the horizontal distance to the previous area \NC \NR
\NC numeric Vstep [] \NC the vertical distance to the previous area \NC \NR
\NC numeric Hsize [] \NC the width of an area  \NC \NR
\NC numeric Vsize [] \NC the height of an area \NC \NR
\HL
\stoptabulate

The array variables are accessed by using constants:

\starttabulate[|l|l|]
\HL
\NC horizontal           \NC vertical        \NC \NR
\HL
\NC LeftEdge             \NC Top             \NC \NR
\NC LeftEdgeSeparator    \NC TopSeparator    \NC \NR
\NC LeftMargin           \NC Header          \NC \NR
\NC LeftMarginSeparator  \NC HeaderSeparator \NC \NR
\NC Text                 \NC Text            \NC \NR
\NC RightMarginSeparator \NC FooterSeparator \NC \NR
\NC RightMargin          \NC Footer          \NC \NR
\NC RightEdgeSeparator   \NC BottomSeparator \NC \NR
\NC RightEdge            \NC Bottom          \NC \NR
\HL
\stoptabulate

In addition to these, there are \type {Margin}, \type {InnerMargin} and \type
{OuterMargin} which adapt themselves to the current odd or even page. The same is
true for \type {Edge}, \type {InnerEdge} and \type {OuterEdge}, although these
will seldom be used, since interactive documents are always single sided.

We started this chapter with spending a lot of code to simulate the page areas.
It will be clear now that in practice this is much easier using the mechanism
described here.

\placefigure
  [here][fig:back 5]
  {A quick way to draw all used areas.}
  {\setupexternalfigures[background=color,backgroundcolor=white]%
   \startcombination
     {\typesetfile[mfun-900.tex][page=10,width=.4\textwidth]}{even}
     {\typesetfile[mfun-900.tex][page=11,width=.4\textwidth]}{odd}
   \stopcombination}

In \in {figure} [fig:back 5] we see all used areas. Areas that are not used are
not drawn (which saves some testing). This background was defined as:

\typebuffer[back-5]

We use two nested \type {for} loops to step over the areas. A \type {for} loop
with a step of~1 will fail, because the indices are defined in a rather special
way. On the other hand, the mechanism is rather tolerant, in the sense that \type
{[i][j]} and \type {[j][i]} are both accepted.

\stopsection

\startsection[title={Bleeding}]

\index {bleeding}

If you want to share your document all over the world, it makes sense to use a
paper format like {\em letter} or {\em A4}. In that case, the layout often
matches the paper size.

\startlinecorrection[blank]
\startMPcode
  path p ; p := fullcircle xyscaled (21mm,29.7mm) ;
  path q ; q := boundingbox p ;
  fill q withcolor .625white ;
  fill p withcolor .625yellow ;
  currentpicture := currentpicture shifted (-31mm,0) ;
  fill q withcolor .625white ;
  fill p xsized (bbwidth(p)-2mm) withcolor .625yellow ;
  currentpicture := currentpicture shifted (-31mm,0) ;
  fill q withcolor .625white ;
  fill p withcolor .625yellow ;
  draw q enlarged -1mm withpen pencircle scaled 2mm withcolor .625white ;
\stopMPcode
\stoplinecorrection

The left picture demonstrates what happens when you have a printer that is
capable of printing from edge to edge. If you have such a printer, you're lucky.
The middle picture demonstrates what happens if you have a properly set up
printing program and|/|or printer: the page is scaled down so that the content
fits into the non printable area of the printer. One reason why printers don't
print from edge to edge is that the engine is not that happy when toner or ink
ends up next to the page. The third picture shows what happens when a printer
simply ignores content that runs over the non printable area. In many cases it's
best to make sure that the content leaves a margin of 5mm from the edges.

Books and magazines seldom use the popular desk||top paper sizes. Here the
designer determined the paper size and layout more or less independent from the
size of the sheet on which the result is printed. Instead of one page per sheet,
arrangements of 2 upto 32 or more pages per sheet are made. The process of
arranging pages in such a way that these sheets can be folded and combined into
books is called page imposition. \CONTEXT\ supports a wide range of page
imposition schemes. More information on this can be found in the \CONTEXT\
manuals.

The fact that the sheet on which a page is printed is larger than the page itself
opens the possibility to use the full page for content. In that case, especially
when you use background graphics, you need to make sure that indeed the page is
covered completely. Where in desk top printing you can get away with imperfection
simply because the printing engines have their limitations, in professional
output you need to be more considerate.

\startlinecorrection[blank]
\startMPcode
  path p ; p := fullsquare xyscaled (4cm,5cm) ;
  path q ; q := fullsquare xyscaled (3cm,4cm) ;
  path r ; r := fullsquare xyscaled (2cm,3cm) shifted (-.5cm,.5cm) ;
  fill p withcolor .625white ;
  fill q withcolor .850white ;
  currentpicture := currentpicture shifted (-45mm,0) ;
  fill p withcolor .625white ;
  fill q withcolor .850white ;
  fill r withcolor transparent(1,.5,.625yellow) ;
  currentpicture := currentpicture shifted (-45mm,0) ;
  fill p withcolor .625white ;
  fill q withcolor .850white ;
  r := r topenlarged 2mm leftenlarged 2mm ;
  fill r withcolor transparent(1,.5,.625yellow) ;
\stopMPcode
\stoplinecorrection

Slightly enlarging a graphic so that it exceeds the natural page limits is called
bleeding. Because quite often layout elements have a rectangular nature,
\METAFUN\ provides a couple of operations that can save you some work in defining
bleeding boxes.

\startbuffer
path p, q ;
def ShowPath =
  fill p withcolor transparent(1,.5,.625yellow) ;
  fill q withcolor transparent(1,.5,.625yellow) ;
  currentpicture := currentpicture shifted (-25mm,0) ;
enddef ;
p := q := fullsquare xyscaled (2cm,3cm) ; ShowPath ;
p := p leftenlarged   2mm ; ShowPath ;
p := p topenlarged    2mm ; ShowPath ;
p := p rightenlarged  2mm ; ShowPath ;
p := p bottomenlarged 2mm ; ShowPath ;
\stopbuffer

\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection

This graphic is generated as follows:

\typebuffer

The trick is in the last couple of lines. In addition to the general \type
{enlarged} operator, we have 4~operators that enlarge a rectangle in a certain
direction. This means that we can define the original path using dimensions
related to the layout, and add bleed strips independently.

\startbuffer
path p ; p := fullsquare xyscaled (4cm,1cm) ;
path q ; q := p leftenlarged 2mm topenlarged 2mm ;
fill p withcolor transparent(1,.5,.625yellow) ;
fill q withcolor transparent(1,.5,.625yellow) ;
draw boundingbox currentpicture withcolor .625red ;
\stopbuffer

\typebuffer

\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection

This example demonstrates that when we enlarge a graphic, the bounding box also
gets larger. Because this can interfere with the placement of such a graphic, we
need to make sure that the bleeding is there but not seen.

\startbuffer
path p ; p := fullsquare xyscaled (4cm,1cm) ;
path q ; q := p leftenlarged 2mm topenlarged 2mm ;
fill p withcolor transparent(1,.5,.625yellow) ;
fill q withcolor transparent(1,.5,.625yellow) ;
setbounds currentpicture to p ;
draw boundingbox currentpicture withcolor .625red ;
\stopbuffer

\typebuffer

\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection

There are two more operators: \type {innerenlarged} and \type {outerenlarged}.
These expand to either \type {leftenlarged} or \type {rightenlarged}, depending
on the page being left or right hand.

\stopsection

\stopchapter

\stopcomponent