summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/metafun/metafun-layout.tex
blob: 2be7e19ff3fc4b38ea843155e8554ff5f326076f (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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
% language=uk
%
% copyright=pragma-ade readme=readme.pdf licence=cc-by-nc-sa

\startcomponent metafun-layout

\environment metafun-environment

\startchapter[title={Enhancing the layout}]

\startintro

One of the most powerful and flexible commands of \CONTEXT\ is \type {\framed}.
We can use the background features of this command to invoke and position
graphics that adapt themselves to the current situation. Once understood,
overlays will become a natural part of the \CONTEXT\ users toolkit.

\stopintro

\startsection[title={Overlays}]

\index{overlays}

Many \CONTEXT\ commands support overlays. The term {\em overlay} is a bit
confusing, since such an overlay in most cases will lay under the text. However,
because there can be many layers on top of each other, the term suits its
purpose.

When we want to put a \METAPOST\ graphic under some text, we go through a three
step process. First we define the graphic itself:

\startbuffer
\startuniqueMPgraphic{demo circle}
  path p ;
  p := fullcircle xscaled \overlaywidth yscaled \overlayheight ;
  fill p withcolor .85white ;
  draw p withpen pencircle scaled 2pt withcolor .625red ;
\stopuniqueMPgraphic
\stopbuffer

\typebuffer

\getbuffer

This graphic will adapt itself to the width and height of the overlay. Both \type
{\overlaywidth} and \type {\overlayheight} are macros that return a dimension
followed by a space. The next step is to register this graphic as an overlay.

\startbuffer
\defineoverlay[demo circle][\uniqueMPgraphic{demo circle}]
\stopbuffer

\typebuffer

\getbuffer

We can now use this overlay in any command that provides the \type {\framed}
functionality. Since this graphic is defined as unique, \CONTEXT\ will try to
reuse already calculated and embedded graphics when possible.

\startbuffer
\framed[background=demo circle]{This text is overlayed.}
\stopbuffer

\typebuffer

The background can be set to \type {color}, \type {screen}, an overlay
identifier, like \typ {demo circle}, or a comma separated list of those.

\startlinecorrection[blank]
\getbuffer
\stoplinecorrection

The \type {\framed} command automatically draws a ruled box, which can be quite
useful when debugging a graphic. However, in this case we want to turn the frame
off.

\startbuffer
\framed
  [background=demo circle,frame=off]
  {This text is overlayed.}
\stopbuffer

\typebuffer

\startlinecorrection[blank]
\getbuffer
\stoplinecorrection

In this case, it would have made sense to either set the \type {offset} to a
larger value, or to set \type {backgroundoffset}. In the latter case, the ellipse
is positioned outside the frame.

The difference between the three offsets \type {offset}, \type {frameoffset} and
\type {backgroundoffset} is demonstrated in \in {figure} [fig:offsets]. While the
\type {offset} is added to the (natural or specified) dimensions of the content
of the box, the other two are applied to the frame and background and don't add
to the dimensions.

In the first row we only set the \type {offset}, while in the second row, the
(text) offset is set to \type {3pt}. When not specified, the \type {offset} has a
comfortable default value of \type {.25ex} (some 25\% of the height of an~x).

\startbuffer
\setupframed
  [width=.3\textwidth,
   background=demo circle]
\startcombination[3*3]
  {\framed[offset=none]         {\TeX}} {\tt offset=none}
  {\framed[offset=overlay]      {\TeX}} {\tt offset=overlay}
  {\framed[offset=0pt]          {\TeX}} {\tt offset=0pt}
  {\framed[offset=1pt]          {\TeX}} {\tt offset=1pt}
  {\framed[offset=2pt]          {\TeX}} {\tt offset=2pt}
  {\framed[offset=4pt]          {\TeX}} {\tt offset=4pt}
  {\framed[offset=3pt]          {\TeX}} {\tt offset=3pt}
  {\framed[frameoffset=3pt]     {\TeX}} {\tt frameoffset=3pt}
  {\framed[backgroundoffset=3pt]{\TeX}} {\tt backgroundoffset=3pt}
\stopcombination
\stopbuffer

\typebuffer

\placefigure
  [here][fig:offsets]
  {The three offsets.}
  {\getbuffer}

As the first row in \in {figure} [fig:offsets] demonstrates, instead of a value,
one can pass a keyword. The \type {overlay} keyword implies that there is no
offset at all and that the lines cover the content. With \type {none} the frame
is drawn tight around the content. When the offset is set to \type {0pt} or more,
the text is automatically set to at least the height of a line. You can turn this
feature off by saying \type {strut=off}. More details can be found in the
\CONTEXT\ manual.

In \in {figure} [fig:all offsets] we have set {offset} to \type {3pt}, \type
{frameoffset} to \type {6pt} and \type {backgroundoffset} to \type {9pt}. Both
the frame and background offset are sort of imaginary, since they don't
contribute to the size of the box.

\startbuffer
\ruledhbox
  {\framed
     [offset=3pt,frameoffset=6pt,backgroundoffset=9pt,
      background=screen,backgroundscreen=.85]
     {Welcome in the hall of frame!}}
\stopbuffer

\typebuffer

\placefigure
  [here][fig:all offsets]
  {The three offsets.}
  {\getbuffer}

\stopsection

\startsection[title={Overlay variables}]

The communication between \TEX\ and embedded \METAPOST\ graphics takes place by
means of some macros.

\starttabulate[|l|p|]
\HL
\NC overlay status macro    \NC meaning                        \NC \NR
\HL
\NC \tex {overlaywidth}     \NC the width of the graphic, as
                                calculated from the actual
                                width and background offset    \NC \NR
\NC \tex {overlayheight}    \NC the height of the graphic, as
                                calculated from the actual
                                height, depth and background
                                offset                         \NC \NR
\NC \tex {overlaydepth}     \NC the depth of the graphic, if
                                available                      \NC \NR
\NC \tex {overlaycolor}     \NC the background color, if given \NC \NR
\NC \tex {overlaylinecolor} \NC the color of the frame         \NC \NR
\NC \tex {overlaylinewidth} \NC the width of the frame         \NC \NR
\HL
\stoptabulate

The dimensions of the overlay are determined by dimensions of the background,
which normally is the natural size of a \type {\framed}. When a background offset
is specified, it is added to \type {overlayheight} and \type {overlaywidth}.

Colors can be converted by \type {\MPcolor} and in addition to the macros
mentioned, you can use all macros that expand into a dimension or dimen register
prefixed by the \TEX\ primitive \type {\the} (this and other primitives are
explained in \quotation {The \TeX book}, by Donald Knuth).

\stopsection

\startsection[title={Stacking overlays}]

A background can be a gray scale (\type {screen}), a color (\type {color}), a
previously defined overlay identifier, or any combination of these. The next
assignments are therefore valid:

\starttyping
\framed[background=color,backgroundcolor=red]{...}
\framed[background=screen,backgroundscreen=.8]{...}
\framed[background=circle]{...}
\framed[background={color,cow},backgroundcolor=red]{...}
\framed[background={color,cow,grid},backgroundcolor=red]{...}
\stoptyping

In the last three cases of course you have to define \type {circle}, \type {cow}
and \type {grid} as overlay. These items are packed in a comma separated list,
which has to be surrounded by \type {{}}.

\stopsection

\startsection[title={Foregrounds}]

\startbuffer[a]
\startuniqueMPgraphic{backfore}
  draw fullcircle
    xscaled \overlaywidth yscaled \overlayheight
    withpen pencircle scaled 2pt
    withcolor .625yellow ;
\stopuniqueMPgraphic

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

\startbuffer[b]
\framed
  [background=backfore,backgroundoffset=4pt]
  {one, two, three, \unknown}
\stopbuffer

\startbuffer[c]
\framed
  [background={foreground,backfore},backgroundoffset=4pt]
  {one, two, three, \unknown}
\stopbuffer

The overlay system is actually a system of layers. Sometimes we are confronted
with a situation in which we want the text behind another layer. This can be
achieved by explicitly placing the foreground layer, as in \in {figure}
[fig:foreground].

\getbuffer[a]

\placefigure
  [here][fig:foreground]
  {Foreground material moved backwards.}
  {\setupframed[linewidth=1pt]%
   \startcombination
     {\getbuffer[b]} {frame on top layer}
     {\getbuffer[c]} {frame on bottom layer}
   \stopcombination}

The graphic layer is defined as follows:

\typebuffer[a]

The two framed texts have a slightly different definition. The leftmost graphic
is defined as:

\typebuffer[b]

The rightmost graphic is specified as:

\typebuffer[c]

The current values of the frame color and frame width are passed to the overlay.
It often makes more sense to use colors defined at the document level, if only to
force consistency.

\startbuffer
\startuniqueMPgraphic{super ellipse}
  path p ; p := unitsquare
    xscaled \overlaywidth yscaled \overlayheight
    superellipsed .85 ;
  pickup pencircle scaled \overlaylinewidth ;
  fill p withcolor \MPcolor{\overlaycolor} ;
  draw p withcolor \MPcolor{\overlaylinecolor} ;
\stopuniqueMPgraphic

\defineoverlay[super ellipse][\uniqueMPgraphic{super ellipse}]
\stopbuffer

\typebuffer \getbuffer

This background demonstrates that a super ellipse is rather well suited as frame.

\startbuffer
\framed
  [background=super ellipse,
   frame=off,
   width=3cm,
   align=middle,
   framecolor=darkyellow,
   rulethickness=2pt,
   backgroundcolor=darkgray]
  {\white This is a\\Super Ellipsed\\sentence.}
\stopbuffer

\typebuffer

Such a super ellipse looks quite nice and is a good candidate for backgrounds,
for which the superness should be at least~.85.

\startlinecorrection[blank]
\getbuffer
\stoplinecorrection

\stopsection

\startsection[title={Typesetting graphics}]

I have run into people who consider it kind of strange when you want to use \TEX\
for non||mathematical typesetting. If you agree with them, you may skip this
section with your eyes closed.

One of the \CONTEXT\ presentation styles (number 15, tagged as balls) stepwise
builds screens full of sentences, quotes or concepts, packages in balloons and
typesets them as a paragraph. We will demonstrate that \TEX\ can typeset graphics
using the following statement.

% \let\processword\relax

\startbuffer[lions]
\processwords{As you may know, \TEX's ambassador is a lion, while {\METAFONT}
is represented by a lioness. It is still unclear if they have a relationship,
but if so, and if a cub is born, may it enjoy \METAFUN.}
\stopbuffer

\startquotation
\def\processwords#1{#1}\getbuffer[lions]
\stopquotation

The low level \CONTEXT\ macro \type {\processwords} provides a mechanism to treat
the individual words of its argument. The macro is called as follows:

\typebuffer[lions]

In order to perform a task, you should also define a macro \type {\processword},
which takes one argument. The previous quote was typeset with the following
definition in place:

\starttyping
\def\processword#1{#1}
\stoptyping

A slightly more complicated definition is the following:

\startbuffer
\def\processword#1{\noindent\framed{#1}\space}
\stopbuffer

\typebuffer \getbuffer

We now get:

\blank\getbuffer[lions]\blank

If we can use \type {\framed}, we can also use backgrounds.

\startbuffer
\def\processword#1%
  {\noindent\framed[frame=off,background=lions]{#1} }
\stopbuffer

\typebuffer \getbuffer

We can add a supperellipsed frame using the following definition:

\startbuffer
\startuniqueMPgraphic{lions a}
  path p ; p := fullsquare
   xyscaled (\overlaywidth,\overlayheight) superellipsed .85 ;
  pickup pencircle scaled 1pt ;
  fill p withcolor .850white ; draw p withcolor .625yellow ;
\stopuniqueMPgraphic

\defineoverlay[lions][\uniqueMPgraphic{lions a}]
\stopbuffer

\typebuffer \getbuffer

\bgroup \blank \veryraggedright\getbuffer[lions]\unskip \blank \egroup

\startbuffer
\startuseMPgraphic{lions b}
  path p ; p := fullsquare
    xyscaled (\overlaywidth,\overlayheight) randomized 5pt ;
  pickup pencircle scaled 1pt ;
  fill p withcolor .850white ; draw p withcolor .625yellow ;
\stopuseMPgraphic

\defineoverlay[lions][\uniqueMPgraphic{lions b}]
\stopbuffer

\typebuffer \getbuffer

\bgroup \blank \veryraggedcenter\getbuffer[lions]\unskip \blank \egroup

\startbuffer
\startuniqueMPgraphic{lions c}
  path p ; p := fullsquare
    xyscaled (\overlaywidth,\overlayheight) squeezed 2pt ;
  pickup pencircle scaled 1pt ;
  fill p withcolor .850white ; draw p withcolor .625yellow ;
\stopuniqueMPgraphic

\defineoverlay[lions][\uniqueMPgraphic{lions c}]
\stopbuffer

\typebuffer \getbuffer

\bgroup \blank \veryraggedleft\getbuffer[lions]\unskip \blank \egroup

These paragraphs were typeset with the following settings.

\starttyping
\setupalign[broad, right]  % == \veryraggedright
\setupalign[broad, middle] % == \veryraggedcenter
\setupalign[broad, left]   % == \veryraggedleft
\stoptyping

The \type {broad} increases the raggedness. We defined three different graphics
(a, b and c) because we want some to be unique, which saves some processing. Of
course we don't reuse the random graphics. In the definition of \type
{\processword} we have to use \type {\noindent} because otherwise \TEX\ will put
each graphic on a line of its own. Watch the space at the end of the macro.

\stopsection

\startsection[title={Graphics and macros}]

Because \TEX's typographic engine and \METAPOST's graphic engine are separated,
interfacing between them is not as natural as you may expect. In \CONTEXT\ we
have tried to integrate them as much as possible, but using the interface is not
always as convenient as it should be. What method you follow, depends on the
problem at hand.

The official \METAPOST\ way to embed \TEX\ code into graphics is to use \typ
{btex ... etex}. As soon as \CONTEXT\ writes the graphic data to the intermediate
\METAPOST\ file, it looks for these commands. When it has encountered an \type
{etex}, \CONTEXT\ will make sure that the text that is to be typeset by \TEX\ is
{\em not} expanded. This is what you may expect, because when you would embed
those commands in a stand||alone graphic, they would also not be expanded, if
only because \METAPOST\ does not know \TEX. With expansion we mean that \TEX\
commands are replaced by their meaning (which can be quite extensive).

{\em Users of \CONTEXT\ MKIV\ can skip the next paragraph}.

When \METAPOST\ sees a \type {btex} command, it will consult a so called \type
{mpx} file. This file holds the \METAPOST\ representation of the text typeset by
\TEX. Before \METAPOST\ processes a graphic definition file, it first calls
another program that filters the \type {btex} commands from the source file, and
generates a \TEX\ file from them. This file is then processed by \TEX, and after
that converted to a \type {mpx} file. In \CONTEXT\ we let \TEXEXEC\ take care of
this whole process.

Because the \typ {btex ... etex} commands are filtered from the raw \METAPOST\
source code, they cannot be part of macro definitions and loop constructs. When
used that way, only one instance would be found, while in practice multiple
instances may occur.

This drawback is overcome by \METAFUN's \type {textext} command. This command
still uses \typ {btex ... etex} but writes these commands to a separate job
related file each time it is used. \footnote {It took the author a while to find
out that there is a \METAPOST\ module called \type {tex.mp} that provides a
similar feature, but with the disadvantage that each text results in a call to
\TEX. Each text goes into a temporary file, which is then included and results in
\METAPOST\ triggering \TEX.} After the first \METAPOST\ run, this file is merged
with the original file, and \METAPOST\ is called again. So, at the cost of an
additional run, we can use text typeset by \TEX\ in a more versatile way. Because
\METAPOST\ runs are much faster than \TEX\ runs, the price to pay in terms of run
time is acceptable. Unlike \typ {btex ... etex}, the \TEX\ code in \type
{textext} command is expanded, but as long as \CONTEXT\ is used this is seldom a
problem, because most commands are somewhat protected.

If we define a graphic with text to be typeset by \TEX, there is a good chance
that this text is not frozen but passes as argument. A \TEX||like solution for
passing arbitrary content to such a graphic is the following: \footnote {The \type
{\unexpanded} prefix makes the command robust for being passed as argument. It is not
to be confused with the primitive. We had this feature already when the primitive
showed up and it was considered to be inconvenient for other macro packages to adapt to
the \CONTEXT\ situation. So keep that in mind when you mix macro packages.}

\startbuffer[def]
\unexpanded\def\RotatedText#1#2%
  {\startuseMPgraphic{RotatedText}
     draw btex #2 etex rotated #1 ;
   \stopuseMPgraphic
   \useMPgraphic{RotatedText}}
\stopbuffer

\typebuffer[def] \getbuffer[def]

This macro takes two arguments (the \type {#} identifies an
argument):

\startbuffer[exa]
\RotatedText{15}{Some Rotated Text}
\stopbuffer

\typebuffer[exa]

The text is rotated over 15 degrees about the origin in a counterclockwise
direction.

\startlinecorrection[blank]
\getbuffer[exa]
\stoplinecorrection

In \CONTEXT\ we seldom pass settings like the angle of rotation in this manner.
You can use \type {\setupMPvariables} to set up graphic||specific variables. Such
a variable can be accessed with \type {\MPvar}.

\startbuffer[def]
\setupMPvariables[RotatedText][rotation=90]

\startuseMPgraphic{RotatedText}
  draw textext{Some Text} rotated \MPvar{rotation} ;
\stopuseMPgraphic
\stopbuffer

\typebuffer[def] \getbuffer[def]

An example:

\startbuffer[exa]
\RotatedText{-15}{Some Rotated Text}
\stopbuffer

\typebuffer[exa]

\startlinecorrection[blank]
\getbuffer[exa]
\stoplinecorrection

In a similar fashion we can isolate the text. This permits us to use the same
graphics with different settings.

\startbuffer[def]
\setupMPvariables[RotatedText][rotation=270]

\setMPtext{RotatedText}{Some Text}

\startuseMPgraphic{RotatedText}
  draw \MPbetex{RotatedText} rotated \MPvar{rotation} ;
\stopuseMPgraphic
\stopbuffer

\typebuffer[def] \getbuffer[def]

This works as expected:

\startbuffer[exa]
\RotatedText{165}{Some Rotated Text}
\stopbuffer

\typebuffer[exa]

\startlinecorrection[blank]
\getbuffer[exa]
\stoplinecorrection

It is now a small step towards an encapsulating macro (we assume that you are
familiar with \TEX\ macro definitions).

\startbuffer[def]
\def\RotatedText[#1]#2%
  {\setupMPvariables[RotatedText][#1]%
   \setMPtext{RotatedText}{#2}%
   \useMPgraphic{RotatedText}}

\setupMPvariables[RotatedText][rotation=90]

\startuseMPgraphic{RotatedText}
  draw \MPbetex{RotatedText} rotated \MPvar{rotation} ;
\stopuseMPgraphic
\stopbuffer

\typebuffer[def] \getbuffer[def]

Again, we default to a 90 degrees rotation, and pass both the settings and text
in an indirect way. This method permits you to build complicated graphics and
still keep macros readable.

\startbuffer[exa]
\RotatedText[rotation=240]{Some Rotated Text}
\stopbuffer

\typebuffer[exa]

\startlinecorrection[blank]
\getbuffer[exa]
\stoplinecorrection

You may wonder why we don't use the variable mechanism to pass the text. The main
reason is that the text mechanism offers a few more features, one of which is
that it passes the text straight on, without the danger of unwanted expansion of
embedded macros. Using \type {\setMPtext} also permits you to separate \TEX\ and
\METAPOST\ code and reuse it multiple times (imagine using the same graphic in a
section head command).

There are three ways to access a text defined with \type {\setMPtext}. Imagine
that we have the following definitions:

\startbuffer
\setMPtext {1} {Now is this \TeX\ or not?}
\setMPtext {2} {See what happens here.}
\setMPtext {3} {Text streams become pictures.}
\stopbuffer

\typebuffer \getbuffer

The \type {\MPbetex} macro returns a \typ {btex ... etex} construct. The \type
{\MPstring} returns the text as a \METAPOST\ string, between quotes. The raw text
can be fetched with \type {\MPtext}.

\startbuffer
\startMPcode
  picture p ; p :=           \MPbetex {1}          ;
  picture q ; q :=  textext( \MPstring{2}        ) ;
  picture r ; r := thelabel("\MPtext  {3}",origin) ;

  for i=p, boundingbox p : draw i withcolor .625red    ; endfor ;
  for i=q, boundingbox q : draw i withcolor .625yellow ; endfor ;
  for i=r, boundingbox r : draw i withcolor .625white  ; endfor ;

  currentpicture := currentpicture scaled 2 ;
  draw origin
    withpen pencircle scaled 5.0mm withcolor white ;
  draw origin
    withpen pencircle scaled 2.5mm withcolor black ;
  draw boundingbox currentpicture
    withpen pencircle scaled .1mm
    dashed evenly ;
\stopMPcode
\stopbuffer

\typebuffer

The first two lines return text typeset by \TEX, while the last line leaves this
to \METAPOST.

\startlinecorrection[blank]
\getbuffer
\stoplinecorrection

If you watch closely, you will notice that the first (red) alternative is
positioned with the baseline on the origin.

\startbuffer
\startMPcode
  picture p ; p :=                  \MPbetex {1}          ;
  picture q ; q :=  textext.origin( \MPstring{2}        ) ;
  picture r ; r := thelabel.origin("\MPtext  {3}",origin) ;

  for i=p, boundingbox p : draw i withcolor .625red    ; endfor ;
  for i=q, boundingbox q : draw i withcolor .625yellow ; endfor ;
  for i=r, boundingbox r : draw i withcolor .625white  ; endfor ;

  currentpicture := currentpicture scaled 2 ;
  draw origin withpen pencircle scaled 5.0mm
    withcolor white ;
  draw origin withpen pencircle scaled 2.5mm
    withcolor black ;
  draw boundingbox currentpicture
    withpen pencircle scaled .1mm
    dashed evenly ;
\stopMPcode
\stopbuffer

\typebuffer

This draws:

\startlinecorrection[blank]
\getbuffer
\stoplinecorrection

This picture demonstrates that we can also position \type {textext}'s and \type
{label}'s on the baseline. For this purpose the repertoire of positioning
directives (\type {top}, \type {lft}, etc.) is extended with an \type {origin}
directive. Another extra positioning directive is \type {raw}. This one does not
do any positioning at all.

\starttyping
picture q ; q :=  textext.origin( \MPstring{2}        ) ;
picture r ; r := thelabel.origin("\MPtext  {3}",origin) ;
\stoptyping

We will now apply this knowledge of text inclusion in graphics to a more advanced
example. The next definitions are the answer to a question on the \CONTEXT\
mailinglist with regards to framed texts with titles.

\startbuffer[a]
\defineoverlay[FunnyFrame][\useMPgraphic{FunnyFrame}]

\defineframedtext[FunnyText][frame=off,background=FunnyFrame]

\def\StartFrame{\startFunnyText}
\def\StopFrame {\stopFunnyText }

\def\FrameTitle#1%
  {\setMPtext{FunnyFrame}{\hbox spread 1em{\hss\strut#1\hss}}}

\setMPtext{FunnyFrame}{} % initialize the text variable
\stopbuffer

\startbuffer[b]
\startuseMPgraphic{FunnyFrame}
  picture p ; numeric w, h, o ;
  p := textext.rt(\MPstring{FunnyFrame}) ;
  w := OverlayWidth ; h := OverlayHeight ; o := BodyFontSize ;
  p := p shifted (2o,h-ypart center p) ; draw p ;
  drawoptions (withpen pencircle scaled 1pt withcolor .625red) ;
  draw (2o,h)--(0,h)--(0,0)--(w,0)--(w,h)--(xpart urcorner p,h) ;
  draw boundingbox p ;
  setbounds currentpicture to unitsquare xyscaled(w,h) ;
\stopuseMPgraphic
\stopbuffer

\startbuffer[c1]
\FrameTitle{Zapf (1)}

\StartFrame
Coming back to the use of typefaces in electronic
publishing: many of the new typographers receive their
knowledge and information about the rules of typography from
books, from computer magazines or the instruction manuals
which they get with the purchase of a PC or software.
\StopFrame
\stopbuffer

\getbuffer[a,b,c1]

In this example, the title is positioned on top of the frame. Title and text are
entered as:

\typebuffer[c1]

The implementation is not that complicated and uses the frame commands that are
built in \CONTEXT. Instead of letting \TEX\ draw the frame, we use \METAPOST,
which we also use for handling the title. The graphic is defined as follows:

\typebuffer[b]

Because the framed title is partly positioned outside the main frame, and because
the main frame will be combined with the text, we need to set the boundingbox
explicitly. This is a way to create so called free figures, where part of the
figure lays beyond the area that is taken into account when positioning the
graphic. The shift

\starttyping
... shifted (2o,h-ypart center p)
\stoptyping

ensures that the title is vertically centered over the top line of the main box.

The macros that use this graphic combine some techniques of defining macros,
using predefined \CONTEXT\ classes, and passing information to graphics.

\typebuffer[a]

There is a little bit of low level \TEX\ code involved, like a horizontal box
(\type {\hbox}) that stretches one em||space beyond its natural size (\type
{spread 1em}) with a centered text (two times \type {\hss}). Instead of applying
this spread, we could have enlarged the frame on both sides.

\startbuffer[b]
\startuseMPgraphic{FunnyFrame}
  picture p ; numeric o ; path a, b ; pair c ;
  p := textext.rt(\MPstring{FunnyFrame}) ;
  a := unitsquare xyscaled(OverlayWidth,OverlayHeight) ;
  o := BodyFontSize ;
  p := p shifted (2o,OverlayHeight-ypart center p) ;
  drawoptions (withpen pencircle scaled 1pt withcolor .625red) ;
  b := a randomized (o/2) ;
  fill b withcolor .85white ; draw b ;
  b := (boundingbox p) randomized (o/8) ;
  fill b withcolor .85white ; draw b ;
  draw p withcolor black;
  setbounds currentpicture to a ;
 \stopuseMPgraphic
\stopbuffer

In the previous graphic we calculated the big rectangle taking the small one into
account. This was needed because we don't use a background fill. The next
definition does, so there we can use a more straightforward approach by just
drawing (and filling) the small rectangle on top of the big one.

\typebuffer[b] \getbuffer[b]

\startbuffer[c2]
\FrameTitle{Zapf (2)}

\StartFrame
There is not so much basic instruction, as of now, as there
was in the old days, showing the differences between good
and bad typographic design.
\StopFrame
\stopbuffer

\getbuffer[c2]

Because we use a random graphic, we cannot guarantee beforehand that the left and
right edges of the small shape touch the horizontal lines in a nice way. The next
alternative displaces the small shape plus text so that its center lays on the
line. On the average, this looks better.

\startbuffer[b]
\startuseMPgraphic{FunnyFrame}
  picture p ; numeric o ; path a, b ; pair c ;
  p := textext.rt(\MPstring{FunnyFrame}) ;
  a := unitsquare xyscaled(OverlayWidth,OverlayHeight) ;
  o := BodyFontSize ;
  p := p shifted (2o,OverlayHeight-ypart center p) ;
  drawoptions (withpen pencircle scaled 1pt withcolor .625red) ;
  b := a randomized (o/2) ;
  fill b withcolor .85white ; draw b ;
  c := center p ;
  c := b intersectionpoint (c shifted (0,-o)--c shifted(0,o)) ;
  p := p shifted (c-center p) ;
  b := (boundingbox p) randomized (o/8) ;
  fill b withcolor .85white ; draw b ;
  draw p withcolor black;
  setbounds currentpicture to a ;
 \stopuseMPgraphic
\stopbuffer

\typebuffer[b] \getbuffer[b]

\getbuffer[c2]

Yet another definition uses super ellipsed shapes instead of random ones. We need
a high degree of superness (.95) in order to make sure that the curves don't
touch the texts.

\startbuffer[b]
\startuseMPgraphic{FunnyFrame}
  picture p ; numeric o ; path a, b ; pair c ;
  p := textext.rt(\MPstring{FunnyFrame}) ;
  o := BodyFontSize ;
  a := unitsquare xyscaled(OverlayWidth,OverlayHeight) ;
  p := p shifted (2o,OverlayHeight-ypart center p) ;
  drawoptions (withpen pencircle scaled 1pt withcolor .625red) ;
  b := a superellipsed .95 ;
  fill b withcolor .85white ; draw b ;
  b := (boundingbox p) superellipsed .95 ;
  fill b withcolor .85white ; draw b ;
  draw p withcolor black ;
  setbounds currentpicture to a ;
\stopuseMPgraphic
\stopbuffer

\typebuffer[b] \getbuffer[b]

\startbuffer[c3]
\FrameTitle{Zapf (3)}

\StartFrame
Many people are just fascinated by their PC's tricks, and
think that a widely||praised program, called up on the
screen, will make everything automatic from now on.
\StopFrame
\stopbuffer

\getbuffer[c3]

There are quite some hard coded values in these graphics, like the linewidths,
offsets and colors. Some of these can be fetched from the \type {\framed}
environment either by using \TEX\ macros or dimensions, or by using their
\METAFUN\ counterparts. In the following table we summarize both the available
\METAPOST\ variables and their \TEX\ counterparts. They may be used
interchangeably.

\starttabulate[|l|Tl|l|]
\HL
\NC \bf \METAPOST\ variable \NC \rm \bf \TEX\ command \NC \bf meaning \NC\NR
\HL
\NC OverlayWidth     \NC \string\overlaywidth
                     \NC current width \NC\NR
\NC OverlayHeight    \NC \string\overlayheight
                     \NC current height \NC\NR
\NC OverlayDepth     \NC \string\overlayheight
                     \NC current depth (often zero) \NC\NR
\NC OverlayColor     \NC \string\MPcolor\string{\string\overlaycolor\string}
                     \NC background color \NC\NR
\NC OverlayLineWidth \NC \string\overlaylinewidth
                     \NC width of the frame \NC\NR
\NC OverlayLineColor \NC \string\MPcolor\string{\overlaylinecolor\string}
                     \NC color of the frame \NC\NR
\NC BaseLineSkip     \NC \string\the\string\baselineskip
                     \NC main line distance \NC\NR
\NC LineHeight       \NC \string\the\string\baselineskip
                     \NC idem \NC\NR
\NC BodyFontSize     \NC \string\the\string\bodyfontsize
                     \NC font size of the running text \NC\NR
\NC StrutHeight      \NC \string\strutheight
                     \NC space above the baseline \NC\NR
\NC StrutDepth       \NC \string\strutdepth
                     \NC space below the baseline \NC\NR
\NC ExHeight         \NC 1ex
                     \NC height of an x \NC \NR
\NC EmWidth          \NC 1em
                     \NC width of an m-dash \NC \NR
\HL
\stoptabulate

\startbuffer[b]
\startuseMPgraphic{FunnyFrame}
  picture p ; numeric o ; path a, b ; pair c ;
  p := textext.rt(\MPstring{FunnyFrame}) ;
  o := BodyFontSize ;
  a := unitsquare xyscaled(OverlayWidth,OverlayHeight) ;
  p := p shifted (2o,OverlayHeight-ypart center p) ;
  pickup pencircle scaled OverlayLineWidth ;
  b := a superellipsed .95 ;
  fill b withcolor OverlayColor ;
  draw b withcolor OverlayLineColor ;
  b := (boundingbox p) superellipsed .95 ;
  fill b withcolor OverlayColor ;
  draw b withcolor OverlayLineColor ;
  draw p withcolor black ;
  setbounds currentpicture to a ;
\stopuseMPgraphic
\stopbuffer

\typebuffer[b] \getbuffer[b]

\startbuffer[d]
\setupframedtexts
  [FunnyText]
  [backgroundcolor=lightgray,
   framecolor=darkred,
   rulethickness=2pt,
   offset=\bodyfontsize,
   before={\blank[big,medium]},
   after={\blank[big]},
   width=\textwidth]
\stopbuffer

\getbuffer[d,c3]

We used the following command to pass the settings:

\typebuffer[d]

In a real implementation, we should also take care of some additional spacing
before the text, which is why we have added more space before than after the
framed text.

We demonstrated that when defining graphics that are part of the layout, you need
to have access to information known to the typesetting engine. Take \in {figure}
[fig:penalty]. The line height needs to match the font and the two thin
horizontal lines should match the {\em x}||height. We also need to position the
baseline, being the lowest one of a pair of lines, in such a way that it suits
the proportions of the line as specified by the strut. A strut is an imaginary
large character with no width. You should be aware of the fact that while \TEX\
works its way top||down, in \METAPOST\ the origin is in the lower left corner.

\startmode[screen]

\placefigure
  [page][fig:penalty]
  {Penalty lines.}
  {\typesetfile[mfun-902.tex][page=1,frame=on,height=.85\textheight]}

\stopmode

\startnotmode[screen]

\placefigure
  [here][fig:penalty]
  {Penalty lines.}
  {\typesetfile[mfun-902.tex][page=1,frame=on,height=.50\textheight]}

\stopnotmode

\typebuffer[handwrit]

This code demonstrates the use of \type {LineHeight}, \type {ExHeight}, \type
{StrutHeight} and \type {StrutDepth}. We set the interline spacing to 1.5 so that
we get a bit more loose layout. The variables mentioned are set each time a
graphic is processed and thereby match the current font settings.

\stopsection

\stopchapter

\stopcomponent