metafun-styles.tex /size: 13 Kb    last modification: 2023-12-21 09:43
1% language=us runpath=texruns:manuals/metafun
2
3\startcomponent metafun-styles
4
5\environment metafun-environment
6
7\startchapter[title={Defining styles}]
8
9\index {layout}
10\index {styles}
11
12\startintro
13
14Since the integration of \METAPOST\ into \CONTEXT, a complete new range of layout
15fetaures became available. In this document we have introduced several ways to
16include graphics in your document definition. In this chapter we go one step
17further and make dynamic graphics part of a document style.
18
19\stopintro
20
21\startsection[title={Adaptive buttons}]
22
23So far we have seen a lot of graphic ingredients that you can use to make your
24documents more attractive. In this chapter we will define a simple document
25style. This style was written for the \PDFTEX\ presentations at the TUG 2000
26conference in Oxford (UK).
27
28This style exploits a few tricks, like graphics calculated using positional
29information. It also demonstrates how you can make menu buttons that dynamically
30adapt their shapes to the rest of the page layout.
31
32\startbuffer[screens]
33\startlinecorrection[blank]
34\setupcombinations[distance=.0125\textwidth]
35\startcombination[5*1]
36  {\typesetfile[mfun-77\ScrNum.tex][page=1,width=.19\textwidth]} {page 1}
37  {\typesetfile[mfun-77\ScrNum.tex][page=2,width=.19\textwidth]} {page 2}
38  {\typesetfile[mfun-77\ScrNum.tex][page=3,width=.19\textwidth]} {page 3}
39  {\typesetfile[mfun-77\ScrNum.tex][page=4,width=.19\textwidth]} {page 4}
40  {\typesetfile[mfun-77\ScrNum.tex][page=5,width=.19\textwidth]} {page 5}
41\stopcombination
42\stoplinecorrection
43\stopbuffer
44
45\def\ScrNum{1} \getbuffer[screens]
46
47Later we will see an instance with some more randomness in the graphics. While
48writing this style, the random alternative made me think of those organic
49buildings with non equal windows |<|we have a few of those in The Netherlands|>|,
50so I decided to label this style as \type {pre-organic}. If you use \CONTEXT, you
51can load this style with:
52
53\starttyping
54\usemodule[pre-organic]
55\stoptyping
56
57At the end of this file, there is a small test file, so when you process the file
58\type {s-pre-19.tex} \footnote {This style is the 19\high{th} presentation style.
59Those numbered styles are internally mapped onto more meaningful names like in
60this case \type {pre-organic}.} with the options \type {--mode=demo} and \type
61{--pdf}, you will get a demo document.
62
63We use one of the standard screen \quote {paper} sizes, and map it onto the same
64size, so that we get a nicely cropped page. Other screen sizes are \type {S4} and
65\type {S5}.
66
67\starttyping
68\setuppapersize[S6][S6]
69\stoptyping
70
71Like in this \METAFUN\ manual, we use the Palatino as main bodyfont. This font is
72quite readable on even low resolution screens, although I admit that this style
73is developed using an $1400\times1050$ pixel \kap {lcd} screen, so the author may
74be a little biased.
75
76\starttyping
77\setupbodyfont[ppl]
78\stoptyping
79
80The layout specification sets up a text area and a right edge area where the
81menus will go (see \in {chapter} [sec:page] for a more in depth discussion on the
82layout areas). Watch how we use a rather large edge distance. By setting the
83header and footer dimensions to zero, we automatically get rid of page body
84ornaments, like the page number.
85
86\starttyping
87\setuplayout
88  [topspace=48pt,
89   backspace=48pt,
90   cutspace=12pt,
91   width=400pt,
92   margin=0cm,
93   rightedge=88pt,
94   rightedgedistance=48pt,
95   header=0cm,
96   footer=0cm,
97   height=middle]
98\stoptyping
99
100We use a moderate, about a line height, inter||paragraph white space.
101
102\starttyping
103\setupwhitespace[big]
104\stoptyping
105
106Of course we use colors, since on computer displays they come for free.
107
108\starttyping
109\setupcolors[state=start]
110
111\definecolor [red]    [r=.75]
112\definecolor [yellow] [r=.75,g=.75]
113\definecolor [gray]   [s=.50]
114\definecolor [white]  [s=.85]
115\stoptyping
116
117Because it is an interactive document, we have to enable hyperlinks and alike.
118However, in this style, we disable the viewer's \quote {highlight a hyperlink
119when it's clicked on} feature. We will use a menu, so we enable menus. Later we
120will see the contrast color |<|hyperlinks gets that color when we are already on
121the location|>| in action.
122
123\starttyping
124\setupinteraction
125  [state=start,
126   click=off,
127   color=red,
128   contrastcolor=gray,
129   menu=on]
130\stoptyping
131
132The menu itself is set up as follows. Because we will calculate menu buttons
133based on their position on the page, we have to keep track of the positions.
134Therefore, we set the \type {position} variable to \type {yes}.
135
136\starttyping
137\setupinteractionmenu
138  [right]
139  [frame=off,
140   position=yes,
141   align=middle,
142   topoffset=-.75cm,
143   bottomoffset=-.75cm,
144   color=gray,
145   contrastcolor=gray,
146   style=bold,
147   before=,
148   after=]
149\stoptyping
150
151The menu content is rather sober: just a list of topics and a close button. Later
152we will define the command that generates topic entries. The alternative \type
153{right} lets the topic list inherit its characteristics from the menu.
154
155\starttyping
156\startinteractionmenu[right]
157  \placelist[Topic][alternative=right]
158  \vfill
159  \but [CloseDocument] close \\
160\stopinteractionmenu
161\stoptyping
162
163We have now arrived at the more interesting part of the style definition: the
164graphic that goes in the page background. Because this graphic will change, we
165define a useable \METAPOST\ graphic. Page backgrounds are recalculated each page,
166opposite to the other backgrounds that are calculated when a new background is
167defined, or when repetitive calculation is turned on.
168
169\starttyping
170\setupbackgrounds [page] [background=page]
171\defineoverlay    [page] [\useMPgraphic{page}]
172\setupMPvariables [page] [alternative=3]
173\stoptyping
174
175We will implement three alternative backgrounds. First we demonstrate the
176relatively simple super ellipsed one. The main complication is that we want the
177button shapes to follow the right edge of the curve that surrounds the text. We
178don't know in advance how many lines of text there will be in a button, and we
179also don't know at what height it will end up. Therefore, we need to calculate
180each button shape independently and for that purpose we need to know its position
181(see \in {chapter} [sec:positioning]). In \in {figure} [fig:style] you can see
182what lines we need in order to be calculate the button shapes.
183
184\startpostponing
185
186\startmode[screen]
187  \placefigure
188    [here][fig:style]
189    {The lines used to calculate the button shapes.}
190    {\typesetfile[mfun-774.tex][page=1,height=.85\textheight]}
191\stopmode
192
193\startnotmode[screen]
194  \placefigure
195    [here][fig:style]
196    {The auxiliary lines used to calculate the button shapes.}
197    {\rotate{\typesetfile[mfun-774.tex][page=1,height=\textwidth]}}
198\stopnotmode
199
200\page
201
202\stoppostponing
203
204We separate the calculation of the button shape from the rest by embedding it in
205its own usable graphic container. The \type {StartPage}||\type {StopPage} pair
206takes care of proper placement of the whole graphic.
207
208\starttyping
209\startuseMPgraphic{page}
210
211  \includeMPgraphic{rightsuperbutton}
212
213  StartPage ;
214
215    path p, q ; pickup pencircle scaled 3pt ;
216
217    p := Field[Text][Text] enlarged 36pt superellipsed .90 ;
218
219    fill Page withcolor \MPcolor{yellow} ;
220    fill p    withcolor \MPcolor{white} ;
221    draw p    withcolor \MPcolor{red} ;
222
223    p := Field[Text][Text] enlarged 48pt superellipsed .90 ;
224
225    def right_menu_button (expr nn, rr, pp, xx, yy, ww, hh, dd) =
226      if (pp>0) and (rr>0) :
227        q := rightsuperbutton(p,xx,yy,RightEdgeWidth,hh) ;
228        fill q withcolor \MPcolor{white} ;
229        draw q withcolor if rr=2 : \MPcolor{gray}
230                         else    : \MPcolor{red}  fi ;
231      fi ;
232    enddef ;
233
234    \MPmenubuttons{right}
235
236  StopPage ;
237\stopuseMPgraphic
238\stoptyping
239
240The \TEX\ macro \type {\MPmenubuttons} expands into a list of (in this case four)
241calls to the \METAPOST\ macro \type {right_menu_button}. This list is generated
242by \CONTEXT\ when it generates the menu. Because the page background is applied
243last, this list is available at that moment.
244
245\starttyping
246... (expr nn, rr, pp, xx, yy, ww, hh, dd) ...
247\stoptyping
248
249This rather long list of arguments represents the following variables: number,
250referred page, current page, x coordinate, y coordinate, width, height and depth.
251The last six variables originate from the positioning mechanism. Because the
252variables are only available after a second \TEX\ pass, we only draw a button
253shape when the test for the page numbers succeeds.
254
255\starttyping
256\startuseMPgraphic{rightsuperbutton}
257  vardef rightsuperbutton (expr pat, xpos, ypos, wid, hei) =
258
259    save p, ptop, pbot, t, b, edge, shift, width, height ;
260    path p, ptop, pbot ; pair t, b ;
261    numeric edge, shift, width, height ;
262
263    edge  := xpos + wid ; shift := ypos + hei ;
264
265    p := rightpath pat ;
266
267    ptop := ((-infinity,shift)--(edge,shift)) ;
268    pbot := ((-infinity,shift-hei)--(edge,shift-hei)) ;
269
270    t := p intersectionpoint ptop ;
271    b := p intersectionpoint pbot ;
272
273    p := subpath(0,xpart (p intersectiontimes ptop)) of p ;
274    p := subpath(xpart (p intersectiontimes pbot),length(p)) of p ;
275
276    (p --               t -- point 1 of ptop &
277          point 1 of ptop -- point 1 of pbot &
278          point 1 of pbot -- b
279       -- cycle)
280
281  enddef ;
282\stopuseMPgraphic
283\stoptyping
284
285The calculation of the button itself comes down to combining segments of the main
286shape and auxiliary lines. The \type {rightpath} macro returns the right half of
287the path provided. This half is shown as a non dashed line.
288
289Topics are identified with \type {\Topic}, which is an instance of chapter
290headings. The number is made invisible. Since it still is a numbered section
291header, \CONTEXT\ will write the header to the table of contents.
292
293\starttyping
294\definehead [Topic] [chapter]
295\setuphead  [Topic] [number=no]
296\stoptyping
297
298We will use a bold font in the table of contents. We also force a complete list.
299
300\starttyping
301\setuplist
302  [Topic]
303  [criterium=all,
304   style=bold,
305   before=,
306   after=]
307\stoptyping
308
309The \type {\TitlePage} macro looks horrible, because we want to keep the
310interface simple: a list of small sentences, separated by \type {\\}.
311
312\starttyping
313\def\TitlePage#1%
314  {\startstandardmakeup
315     \switchtobodyfont[big]
316     \def\\{\vfill\bfb\let\\=\par}
317     \bfd\setupinterlinespace\gray
318     \vskip.5cm#1\\\vskip.5cm  % \\ is really needed -)
319   \stopstandardmakeup}
320\stoptyping
321
322A presentation that uses this style, may look like the one below. You can choose
323among three alternatives.
324
325\starttyping
326\useenvironment[pre-organic] \setupoutput[pdftex]
327
328\setupMPvariables[page][alternative=1]
329
330\starttext
331
332\TitlePage
333  {A Few Nice Quotes\\
334   A Simple Style Demo\\
335   Hans Hagen, August 2000}
336
337\Topic {Douglas R. Hofstadter} \input douglas \page
338\Topic {Donald E. Knuth}       \input knuth   \page
339\Topic {Edward R. Tufte}       \input tufte   \page
340\Topic {Hermann Zapf}          \input zapf    \page
341
342\stoptext
343\stoptyping
344
345We will not implement the two other alternative shapes: squeezed and randomized.
346
347\def\ScrNum{2} \getbuffer[screens]
348\def\ScrNum{3} \getbuffer[screens]
349
350We combine all alternatives into one page graphic. The alternative is chosen by
351setting the \type {alternative} variable, as we demonstrated in the example.
352
353\starttyping
354\startuseMPgraphic{page}
355
356  \includeMPgraphic{rightsuperbutton}
357
358  StartPage ;
359
360    numeric alternative, seed, superness, squeezeness, randomness ;
361    path p, q ; transform t ;
362\stoptyping
363
364This is one of those cases where a transform variable is useful. We need to store
365the random seed value because we want the larger path that is used in the
366calculations to have the same shape.
367
368\starttyping
369    alternative := \MPvar{alternative} ;
370    seed        := uniformdeviate 100 ;
371
372    if alternative > 10 :
373      superness   := .85  + ((\realfolio-1)/max(\lastpage,1)) * .25 ;
374      squeezeness := 12pt - ((\realfolio-1)/max(\lastpage,1)) * 10pt ;
375    else :
376      superness   := .90 ;
377      squeezeness := 12pt ;
378    fi ;
379
380    randomness := squeezeness ;
381
382    alternative := alternative mod 10 ;
383\stoptyping
384
385If you read closely, you will notice that when we add 10 to the alternative, we
386get a page dependant graphic. So, in fact we have five alternatives. We use
387\CONTEXT\ macros to fetch the (real) page number and the number of the last page.
388In further calculations we use the lower alternative numbers, which is why we
389apply a \type {mod}.
390
391The rest of the code is not so much different from the previous definition. The
392hard coded point sizes match the page dimensions (600pt by 450pt) quite well.
393
394\starttyping
395    t := identity if alternative=3: shifted (9pt,-9pt) fi ;
396
397    randomseed := seed ;
398
399    p := Field[Text][Text] enlarged if
400      alternative = 1 : 36pt superellipsed superness   elseif
401      alternative = 2 : 36pt squeezed      squeezeness elseif
402      alternative = 3 : 36pt randomized    randomness  else
403                      : 36pt                           fi ;
404
405    pickup pencircle scaled 3pt ;
406
407    fill Page withcolor \MPcolor{yellow} ;
408    fill p    withcolor \MPcolor{white} ;
409    draw p    withcolor \MPcolor{red} ;
410
411    randomseed := seed ;
412
413    p := ( Field[Text][Text] enlarged if
414      alternative = 1 : 48pt superellipsed superness   elseif
415      alternative = 2 : 48pt squeezed      squeezeness elseif
416      alternative = 3 : 36pt randomized    randomness  else
417                      : 48pt fi ) transformed t ;
418
419    def right_menu_button (expr nn, rr, pp, xx, yy, ww, hh, dd) =
420      if (pp>0) and (rr>0) :
421        q := rightsuperbutton(p,xx,yy,RightEdgeWidth,hh) ;
422        fill q withcolor \MPcolor{white} ;
423        draw q withcolor if rr=2 : \MPcolor{gray}
424                         else    : \MPcolor{red}  fi ;
425      fi ;
426    enddef ;
427
428    \MPmenubuttons{right}
429
430  StopPage ;
431\stopuseMPgraphic
432\stoptyping
433
434When we choose the alternatives~21 and~22 we get this result:
435
436\def\ScrNum{5} \getbuffer[screens]
437\def\ScrNum{6} \getbuffer[screens]
438
439\stopsection
440
441\stopchapter
442
443\stopcomponent
444