summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/metafun/metafun-styles.tex
blob: f82359117a763efae22da24d1a1b0354ee7b245f (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
% language=uk
%
% copyright=pragma-ade readme=readme.pdf licence=cc-by-nc-sa

\startcomponent metafun-styles

\environment metafun-environment

\startchapter[title={Defining styles}]

\index {layout}
\index {styles}

\startintro

Since the integration of \METAPOST\ into \CONTEXT, a complete new range of layout
fetaures became available. In this document we have introduced several ways to
include graphics in your document definition. In this chapter we go one step
further and make dynamic graphics part of a document style.

\stopintro

\startsection[title={Adaptive buttons}]

So far we have seen a lot of graphic ingredients that you can use to make your
documents more attractive. In this chapter we will define a simple document
style. This style was written for the \PDFTEX\ presentations at the TUG 2000
conference in Oxford (UK).

This style exploits a few tricks, like graphics calculated using positional
information. It also demonstrates how you can make menu buttons that dynamically
adapt their shapes to the rest of the page layout.

\startbuffer[screens]
\startlinecorrection[blank]
\setupcombinations[distance=.0125\textwidth]
\startcombination[5*1]
  {\typesetfile[mfun-77\ScrNum.tex][page=1,width=.19\textwidth]} {page 1}
  {\typesetfile[mfun-77\ScrNum.tex][page=2,width=.19\textwidth]} {page 2}
  {\typesetfile[mfun-77\ScrNum.tex][page=3,width=.19\textwidth]} {page 3}
  {\typesetfile[mfun-77\ScrNum.tex][page=4,width=.19\textwidth]} {page 4}
  {\typesetfile[mfun-77\ScrNum.tex][page=5,width=.19\textwidth]} {page 5}
\stopcombination
\stoplinecorrection
\stopbuffer

\def\ScrNum{1} \getbuffer[screens]

Later we will see an instance with some more randomness in the graphics. While
writing this style, the random alternative made me think of those organic
buildings with non equal windows |<|we have a few of those in The Netherlands|>|,
so I decided to label this style as \type {pre-organic}. If you use \CONTEXT, you
can load this style with:

\starttyping
\usemodule[pre-organic]
\stoptyping

At the end of this file, there is a small test file, so when you process the file
\type {s-pre-19.tex} \footnote {This style is the 19\high{th} presentation style.
Those numbered styles are internally mapped onto more meaningful names like in
this case \type {pre-organic}.} with the options \type {--mode=demo} and \type
{--pdf}, you will get a demo document.

We use one of the standard screen \quote {paper} sizes, and map it onto the same
size, so that we get a nicely cropped page. Other screen sizes are \type {S4} and
\type {S5}.

\starttyping
\setuppapersize[S6][S6]
\stoptyping

Like in this \METAFUN\ manual, we use the Palatino as main bodyfont. This font is
quite readable on even low resolution screens, although I admit that this style
is developed using an $1400\times1050$ pixel \kap {lcd} screen, so the author may
be a little biased.

\starttyping
\setupbodyfont[ppl]
\stoptyping

The layout specification sets up a text area and a right edge area where the
menus will go (see \in {chapter} [sec:page] for a more in depth discussion on the
layout areas). Watch how we use a rather large edge distance. By setting the
header and footer dimensions to zero, we automatically get rid of page body
ornaments, like the page number.

\starttyping
\setuplayout
  [topspace=48pt,
   backspace=48pt,
   cutspace=12pt,
   width=400pt,
   margin=0cm,
   rightedge=88pt,
   rightedgedistance=48pt,
   header=0cm,
   footer=0cm,
   height=middle]
\stoptyping

We use a moderate, about a line height, inter||paragraph white space.

\starttyping
\setupwhitespace[big]
\stoptyping

Of course we use colors, since on computer displays they come for free.

\starttyping
\setupcolors[state=start]

\definecolor [red]    [r=.75]
\definecolor [yellow] [r=.75,g=.75]
\definecolor [gray]   [s=.50]
\definecolor [white]  [s=.85]
\stoptyping

Because it is an interactive document, we have to enable hyperlinks and alike.
However, in this style, we disable the viewer's \quote {highlight a hyperlink
when it's clicked on} feature. We will use a menu, so we enable menus. Later we
will see the contrast color |<|hyperlinks gets that color when we are already on
the location|>| in action.

\starttyping
\setupinteraction
  [state=start,
   click=off,
   color=red,
   contrastcolor=gray,
   menu=on]
\stoptyping

The menu itself is set up as follows. Because we will calculate menu buttons
based on their position on the page, we have to keep track of the positions.
Therefore, we set the \type {position} variable to \type {yes}.

\starttyping
\setupinteractionmenu
  [right]
  [frame=off,
   position=yes,
   align=middle,
   topoffset=-.75cm,
   bottomoffset=-.75cm,
   color=gray,
   contrastcolor=gray,
   style=bold,
   before=,
   after=]
\stoptyping

The menu content is rather sober: just a list of topics and a close button. Later
we will define the command that generates topic entries. The alternative \type
{right} lets the topic list inherit its characteristics from the menu.

\starttyping
\startinteractionmenu[right]
  \placelist[Topic][alternative=right]
  \vfill
  \but [CloseDocument] close \\
\stopinteractionmenu
\stoptyping

We have now arrived at the more interesting part of the style definition: the
graphic that goes in the page background. Because this graphic will change, we
define a useable \METAPOST\ graphic. Page backgrounds are recalculated each page,
opposite to the other backgrounds that are calculated when a new background is
defined, or when repetitive calculation is turned on.

\starttyping
\setupbackgrounds [page] [background=page]
\defineoverlay    [page] [\useMPgraphic{page}]
\setupMPvariables [page] [alternative=3]
\stoptyping

We will implement three alternative backgrounds. First we demonstrate the
relatively simple super ellipsed one. The main complication is that we want the
button shapes to follow the right edge of the curve that surrounds the text. We
don't know in advance how many lines of text there will be in a button, and we
also don't know at what height it will end up. Therefore, we need to calculate
each button shape independently and for that purpose we need to know its position
(see \in {chapter} [sec:positioning]). In \in {figure} [fig:style] you can see
what lines we need in order to be calculate the button shapes.

\startpostponing

\startmode[screen]
  \placefigure
    [here][fig:style]
    {The lines used to calculate the button shapes.}
    {\externalfigure[mfun-774.pdf][page=1,height=.85\textheight]}
\stopmode

\startnotmode[screen]
  \placefigure
    [here][fig:style]
    {The auxiliary lines used to calculate the button shapes.}
    {\rotate{\externalfigure[mfun-774.pdf][page=1,height=\textwidth]}}
\stopnotmode

\page

\stoppostponing

We separate the calculation of the button shape from the rest by embedding it in
its own usable graphic container. The \type {StartPage}||\type {StopPage} pair
takes care of proper placement of the whole graphic.

\starttyping
\startuseMPgraphic{page}

  \includeMPgraphic{rightsuperbutton}

  StartPage ;

    path p, q ; pickup pencircle scaled 3pt ;

    p := Field[Text][Text] enlarged 36pt superellipsed .90 ;

    fill Page withcolor \MPcolor{yellow} ;
    fill p    withcolor \MPcolor{white} ;
    draw p    withcolor \MPcolor{red} ;

    p := Field[Text][Text] enlarged 48pt superellipsed .90 ;

    def right_menu_button (expr nn, rr, pp, xx, yy, ww, hh, dd) =
      if (pp>0) and (rr>0) :
        q := rightsuperbutton(p,xx,yy,RightEdgeWidth,hh) ;
        fill q withcolor \MPcolor{white} ;
        draw q withcolor if rr=2 : \MPcolor{gray}
                         else    : \MPcolor{red}  fi ;
      fi ;
    enddef ;

    \MPmenubuttons{right}

  StopPage ;
\stopuseMPgraphic
\stoptyping

The \TEX\ macro \type {\MPmenubuttons} expands into a list of (in this case four)
calls to the \METAPOST\ macro \type {right_menu_button}. This list is generated
by \CONTEXT\ when it generates the menu. Because the page background is applied
last, this list is available at that moment.

\starttyping
... (expr nn, rr, pp, xx, yy, ww, hh, dd) ...
\stoptyping

This rather long list of arguments represents the following variables: number,
referred page, current page, x coordinate, y coordinate, width, height and depth.
The last six variables originate from the positioning mechanism. Because the
variables are only available after a second \TEX\ pass, we only draw a button
shape when the test for the page numbers succeeds.

\starttyping
\startuseMPgraphic{rightsuperbutton}
  vardef rightsuperbutton (expr pat, xpos, ypos, wid, hei) =

    save p, ptop, pbot, t, b, edge, shift, width, height ;
    path p, ptop, pbot ; pair t, b ;
    numeric edge, shift, width, height ;

    edge  := xpos + wid ; shift := ypos + hei ;

    p := rightpath pat ;

    ptop := ((-infinity,shift)--(edge,shift)) ;
    pbot := ((-infinity,shift-hei)--(edge,shift-hei)) ;

    t := p intersectionpoint ptop ;
    b := p intersectionpoint pbot ;

    p := subpath(0,xpart (p intersectiontimes ptop)) of p ;
    p := subpath(xpart (p intersectiontimes pbot),length(p)) of p ;

    (p --               t -- point 1 of ptop &
          point 1 of ptop -- point 1 of pbot &
          point 1 of pbot -- b
       -- cycle)

  enddef ;
\stopuseMPgraphic
\stoptyping

The calculation of the button itself comes down to combining segments of the main
shape and auxiliary lines. The \type {rightpath} macro returns the right half of
the path provided. This half is shown as a non dashed line.

Topics are identified with \type {\Topic}, which is an instance of chapter
headings. The number is made invisible. Since it still is a numbered section
header, \CONTEXT\ will write the header to the table of contents.

\starttyping
\definehead [Topic] [chapter]
\setuphead  [Topic] [number=no]
\stoptyping

We will use a bold font in the table of contents. We also force a complete list.

\starttyping
\setuplist
  [Topic]
  [criterium=all,
   style=bold,
   before=,
   after=]
\stoptyping

The \type {\TitlePage} macro looks horrible, because we want to keep the
interface simple: a list of small sentences, separated by \type {\\}.

\starttyping
\def\TitlePage#1%
  {\startstandardmakeup
     \switchtobodyfont[big]
     \def\\{\vfill\bfb\let\\=\par}
     \bfd\setupinterlinespace\gray
     \vskip.5cm#1\\\vskip.5cm  % \\ is really needed -)
   \stopstandardmakeup}
\stoptyping

A presentation that uses this style, may look like the one below. You can choose
among three alternatives.

\starttyping
\useenvironment[pre-organic] \setupoutput[pdftex]

\setupMPvariables[page][alternative=1]

\starttext

\TitlePage
  {A Few Nice Quotes\\
   A Simple Style Demo\\
   Hans Hagen, August 2000}

\Topic {Douglas R. Hofstadter} \input douglas \page
\Topic {Donald E. Knuth}       \input knuth   \page
\Topic {Edward R. Tufte}       \input tufte   \page
\Topic {Hermann Zapf}          \input zapf    \page

\stoptext
\stoptyping

We will not implement the two other alternative shapes: squeezed and randomized.

\def\ScrNum{2} \getbuffer[screens]
\def\ScrNum{3} \getbuffer[screens]

We combine all alternatives into one page graphic. The alternative is chosen by
setting the \type {alternative} variable, as we demonstrated in the example.

\starttyping
\startuseMPgraphic{page}

  \includeMPgraphic{rightsuperbutton}

  StartPage ;

    numeric alternative, seed, superness, squeezeness, randomness ;
    path p, q ; transform t ;
\stoptyping

This is one of those cases where a transform variable is useful. We need to store
the random seed value because we want the larger path that is used in the
calculations to have the same shape.

\starttyping
    alternative := \MPvar{alternative} ;
    seed        := uniformdeviate 100 ;

    if alternative > 10 :
      superness   := .85  + ((\realfolio-1)/max(\lastpage,1)) * .25 ;
      squeezeness := 12pt - ((\realfolio-1)/max(\lastpage,1)) * 10pt ;
    else :
      superness   := .90 ;
      squeezeness := 12pt ;
    fi ;

    randomness := squeezeness ;

    alternative := alternative mod 10 ;
\stoptyping

If you read closely, you will notice that when we add 10 to the alternative, we
get a page dependant graphic. So, in fact we have five alternatives. We use
\CONTEXT\ macros to fetch the (real) page number and the number of the last page.
In further calculations we use the lower alternative numbers, which is why we
apply a \type {mod}.

The rest of the code is not so much different from the previous definition. The
hard coded point sizes match the page dimensions (600pt by 450pt) quite well.

\starttyping
    t := identity if alternative=3: shifted (9pt,-9pt) fi ;

    randomseed := seed ;

    p := Field[Text][Text] enlarged if
      alternative = 1 : 36pt superellipsed superness   elseif
      alternative = 2 : 36pt squeezed      squeezeness elseif
      alternative = 3 : 36pt randomized    randomness  else
                      : 36pt                           fi ;

    pickup pencircle scaled 3pt ;

    fill Page withcolor \MPcolor{yellow} ;
    fill p    withcolor \MPcolor{white} ;
    draw p    withcolor \MPcolor{red} ;

    randomseed := seed ;

    p := ( Field[Text][Text] enlarged if
      alternative = 1 : 48pt superellipsed superness   elseif
      alternative = 2 : 48pt squeezed      squeezeness elseif
      alternative = 3 : 36pt randomized    randomness  else
                      : 48pt fi ) transformed t ;

    def right_menu_button (expr nn, rr, pp, xx, yy, ww, hh, dd) =
      if (pp>0) and (rr>0) :
        q := rightsuperbutton(p,xx,yy,RightEdgeWidth,hh) ;
        fill q withcolor \MPcolor{white} ;
        draw q withcolor if rr=2 : \MPcolor{gray}
                         else    : \MPcolor{red}  fi ;
      fi ;
    enddef ;

    \MPmenubuttons{right}

  StopPage ;
\stopuseMPgraphic
\stoptyping

When we choose the alternatives~21 and~22 we get this result:

\def\ScrNum{5} \getbuffer[screens]
\def\ScrNum{6} \getbuffer[screens]

\stopsection

\stopchapter

\stopcomponent