% output=pdftex engine=pdftex language=uk
%
% copyright=pragma-ade readme=readme.pdf licence=cc-by-nc-sa

\loadmapfile [texnansi-bh-lucida.map] % currently pdftex does not load map files after page 1

\loadmapfile[qtm-ec.map] % to be sure
\loadmapfile[qhv-ec.map] % to be sure
\loadmapfile[qpl-ec.map] % to be sure

\let\test\relax % hack

\startenvironment mfun-000

% todo : in automp mode, too much bodyfont loading

\setupsystem
  [random=big]

\setupfootertexts
  [section][] % [Preliminary Version \currentdate][]
  [][section] % [][Preliminary Version \currentdate]

\usemodule
  [abr-01,chart,syn-01]

\useMPlibrary
  [clp,txt]

\definepapersize
  [mine]
  [width=20cm,
   height=28cm]

\setuppapersize
  [mine]
  [A4]

\setuplayout
  [topspace=1cm,
   backspace=2cm,
   cutspace=3cm,
   leftmargin=.75cm,
   leftmargindistance=.5cm,
   rightmargin=1.25cm,
   rightmargindistance=1cm,
   header=1cm,
   headerdistance=1cm,
   footer=1cm,
   footerdistance=1cm,
   width=middle,
   height=middle,
   marking=on,
   location=middle]

\setupcolumns
  [distance=1cm]

\setuppagenumbering
  [alternative=doublesided]

\startnotmode[screen]
 \environment o-font
\stopnotmode

%\setupbodyfont % math times is an experiment
%  [mt,ppl,pcr,10pt]

\usetypescriptonce [map]      [times,helvetica,palatino] [texnansi]
\usetypescriptonce [serif]    [times]                    [name,texnansi]
\usetypescriptonce [sans]     [helvetica]                [name,texnansi]
\usetypescriptonce [palatino] [texnansi]

\setupbodyfont [palatino,10pt]

\startbuffer[mpenv]
\startMPenvironment
  \usetypescriptonce[map][times,helvetica,palatino][texnansi]
  \usetypescriptonce[serif][times][name,texnansi]
  \usetypescriptonce[sans][helvetica][name,texnansi]
  \usetypescriptonce[palatino][texnansi]
  \setupbodyfont[palatino,10pt]
  \definefont[RotFont][RegularBold]
\stopMPenvironment
\stopbuffer

\definefont[RotFont][RegularBold]

\getbuffer[mpenv]

\startnotmode[gray]

\setupcolors
  [state=start]

\stopnotmode

\setupindenting
  [medium]

\setuptyping
  [margin=standard,
   blank=halfline]

\definecolor[darkred]    [r=.625]
\definecolor[darkyellow] [r=.625,g=.625] % not: [y=.625]
\definecolor[darkgray]   [s=.625]
\definecolor[lightgray]  [s=.875]

\definecolor[metafun]    [darkred]

\startMPinclusions
  color darkred    ; darkred    := \MPcolor{darkred}    ;
  color darkyellow ; darkyellow := \MPcolor{darkyellow} ;
  color darkgray   ; darkgray   := \MPcolor{darkgray}   ;
  color lightgray  ; lightgray  := \MPcolor{lightgray}  ;
\stopMPinclusions

\setupinteraction % otherwise funny page dimensions due to
  [state=start,   % grouping half way the file in demo text
   style=,
   color=,
   contrastcolor=]

\setuptolerance
  [verytolerant]

\definestartstop
  [intro]
  [style=slanted,
   after=\blank]

\setupquote
  [before=\blank\startnarrower,
   after=\stopnarrower\blank]

\setuplist
  [chapter]
  [after={\blank[line]}]

\setupcombinedlist
  [content]
  [aligntitle=yes,
   alternative=c,
   interaction=all]

\setuptabulate
  [rulecolor=darkyellow,
   rulethickness=1pt]

\startuseMPgraphic{cover page}

  numeric w ; w := 20cm ;
  numeric h ; h := 28cm ;

  drawoptions(withcolor (.6+uniformdeviate.35)*white) ;

  for i=0cm step 1cm until w-1cm  :
    for j=0cm step 1cm until h-1cm :
      fill unitsquare scaled 1cm shifted (i,j) ;
    endfor ;
  endfor ;

  drawoptions(withpen pencircle scaled .5mm withcolor .625yellow) ;

  for i=0cm step 1cm until w : draw (i,0) -- (i,h) ; endfor ;
  for i=0cm step 1cm until h : draw (0,i) -- (w,i) ; endfor ;

  drawoptions(withcolor .625red) ;

\stopuseMPgraphic

\startuseMPgraphic{title page}

  \includeMPgraphic{cover page}

  picture p ; p := "metafun" infont "\truefontname{Helvetica}" rotated 90 ;
  p := p scaled ((h-2cm)/(abs(ypart urcorner p - ypart lrcorner p))) ;
  addto currentpicture also p shifted (w-1cm,1cm) withcolor .625red ;
  picture p ; p := "Hans Hagen" infont "\truefontname{Helvetica}" ;
  p := p scaled (12cm/(xpart lrcorner p - xpart llcorner p)) ;
  addto currentpicture also p shifted (2cm,2cm) withcolor .625red ;

\stopuseMPgraphic

\startuseMPgraphic{back page}

  \includeMPgraphic{cover page}

\stopuseMPgraphic

\startuseMPgraphic{small grid}

  numeric w ; w := \overlaywidth ;
  numeric h ; h := \overlayheight ;
  numeric d ; d := .25cm ;

  drawoptions(withcolor (.6+uniformdeviate.35)*white) ;

  for i=0cm step d until w  :
    for j=0cm step d until h :
      fill unitsquare scaled d shifted (i,j) ;
    endfor ;
  endfor ;

  drawoptions(withpen pencircle scaled .125mm withcolor .625yellow) ;

  for i=0 step d until w+d : draw (i,0) -- (i,h) ; endfor ;
  for i=0 step d until h+d : draw (0,i) -- (w,i) ; endfor ;

\stopuseMPgraphic

\defineoverlay[title page][\useMPgraphic{title page}]

\startnotmode[proof]
  \defineoverlay[back  page][\useMPgraphic{back  page}]
  \defineoverlay[small grid][\useMPgraphic{small grid}]
\stopnotmode

\def\OnGrid#1%
  {\hbox to\hsize
     {\ifodd\realpageno\hss\fi
      \setbox0=\hbox{\color[darkred]{#1}}%
      \dimen0=\wd0
      \ifdim\dimen0>0pt
        \advance\dimen0 by .125cm
        \dimen2=.25cm
        \divide\dimen0 by \dimen2
        \multiply\dimen0 by \dimen2
        \advance\dimen0 by .5cm
      \else
        \dimen0=2cm
      \fi
      \dp0=.25cm
      \ht0=.50cm
      \framed
        [background=small grid,frame=off,offset=overlay]
        {\hbox to \dimen0{\hss\box0\hss}}%
      \ifodd\realpageno\else\hss\fi}}

\setupfootertexts[margin][][\OnGrid{\getmarking[chapter]}]

\startuseMPgraphic{circled}
  pickup pencircle scaled 1mm ;
  drawoptions(withcolor (.6+uniformdeviate.35)*white) ;
  fill fullcircle xscaled 1.5cm yscaled 1cm ;
  drawoptions(withcolor .625yellow) ;
  draw fullcircle xscaled 1.5cm yscaled 1cm ;
\stopuseMPgraphic

\startnotmode[proof]
  \defineoverlay[circled][\useMPgraphic{circled}]
\stopnotmode

\def\Circled#1%
  {\framed
     [background=circled,frame=off,offset=overlay]
     {\color[darkred]{#1}}}

\setuppagenumbering[location=]
\setupheadertexts[margin][][\hbox to \hsize{\hss\Circled\pagenumber\hss}]

\startuniqueMPgraphic{titled}
  path p ; p := unitsquare xscaled \overlaywidth yscaled \overlayheight ;
  pickup pencircle scaled 1mm ;
  drawoptions(withcolor .625yellow) ;
  draw llcorner p -- lrcorner p ;
  setbounds currentpicture to p ;
\stopuniqueMPgraphic

\defineoverlay[titled][\uniqueMPgraphic{titled}]

\def\ChapterCommand#1#2%
  {\setbox0=\hbox to \leftmarginwidth{#1\hss}%
   \hskip-\leftmarginwidth
   \hskip-\leftmargindistance
   \framed
     [background=titled,frame=off,offset=0pt]
     {\box0
      \hskip\leftmargindistance
      #2}}

\def\TitleCommand#1#2%
  {\framed
     [background=titled,frame=off,offset=0pt]
     {#2}}

\def\IntroTitleCommand#1#2%
  {\framed
     [background=titled,frame=off,offset=0pt,width=\textwidth]
     {\hfill#2}}

\def\ExtroTitleCommand#1#2%
  {\framed
     [background=titled,frame=off,offset=0pt,width=\textwidth]
     {\hss#2\hss}}

\setuphead
  [chapter,section,subsection]
  [command=\ChapterCommand,
   color=darkred]

\setuphead
  [title,subject,sububject]
  [command=\TitleCommand,
   color=darkred]

\definehead [introsubject] [subsubject]
\definehead [extrosubject] [subsubject]

\setuphead [introsubject] [command=\IntroTitleCommand]
\setuphead [extrosubject] [command=\ExtroTitleCommand]

\setuphead
  [subsection,subsubject]
  [before=\blank,after=\blank]

\setupFLOWcharts
  [offset=overlay,
   breedte=6\bodyfontsize,
   hoogte=3\bodyfontsize,
   dx=\bodyfontsize,
   dy=\bodyfontsize]

\startFLOWchart[metatex]
  \setupFLOWshapes
    [framecolor=darkred]
  \setupFLOWlines
    [color=darkyellow]
  \startFLOWcell
    \name       {texexec 1}
    \location   {1,1}
    \shape      {action}
    \text       {\TEXEXEC}
    \connection [rl] {context 1}
  \stopFLOWcell
  \startFLOWcell
    \name       {context 1}
    \location   {2,1}
    \shape      {action}
    \text       {\CONTEXT}
    \connection [bt] {metapost 1}
    \connection [rl] {texexec  2}
  \stopFLOWcell
  \startFLOWcell
    \name       {metapost 1}
    \location   {2,2}
    \shape      {action}
    \text       {\METAPOST}
  \stopFLOWcell
  \startFLOWcell
    \name       {texexec 2}
    \location   {3,1}
    \shape      {action}
    \text       {\TEXEXEC}
    \connection [rl] {context  2}
    \connection [bt] {metapost 2}
  \stopFLOWcell
  \startFLOWcell
    \name       {context 2}
    \location   {4,1}
    \shape      {action}
    \text       {\CONTEXT}
  \stopFLOWcell
  \startFLOWcell
    \name       {metapost 2}
    \location   {3,2}
    \shape      {action}
    \text       {\METAPOST}
  \stopFLOWcell
\stopFLOWchart

% the incomplete way:

\startbuffer[a-arrow]
\startMPpositiongraphic{mypos:arrow}
  path pa, pb, pab ; numeric na, nb ;
  initialize_box(\MPpos{\MPvar{from}}) ;
  na := nxy ; pa := llxy..lrxy..urxy..ulxy..cycle ;
  initialize_box(\MPpos{\MPvar{to}}) ;
  nb := nxy ; pb := llxy..lrxy..urxy..ulxy..cycle ;
  if na=nb :
    pickup pencircle scaled 1pt ;
    fill pa withcolor .800white ;
    fill pb withcolor .800white ;
    draw pa withcolor .625yellow ;
    draw pb withcolor .625yellow ;
    pab := center pa  ..
           .5[center pa,center pb] shifted (0,36pt) ..
            center pb ;
    pab := pab cutbefore (pab intersectionpoint pa) ;
    pab := pab cutafter  (pab intersectionpoint pb) ;
    drawarrow pab dashed evenly withcolor .625red ;
    anchor_box(\MPanchor{\MPvar{from}}) ;
  fi ;
\stopMPpositiongraphic
\stopbuffer

% the ugly but better way

\startbuffer[a-arrow]
\startMPpositionmethod{mypos:arrow}
  \startMPpositiongraphic{mypos:arrow}
    path pa, pb, pab ; numeric na, nb ;
    initialize_box(\MPpos{\MPvar{from}}) ;
    na := nxy ; pa := llxy..lrxy..urxy..ulxy..cycle ;
    initialize_box(\MPpos{\MPvar{to}}) ;
    nb := nxy ; pb := llxy..lrxy..urxy..ulxy..cycle ;
    pickup pencircle scaled 1pt ; bboxmargin := 12pt ;
    if na=nb :
      fill pa withcolor .800white ;
      fill pb withcolor .800white ;
      draw pa withcolor .625yellow ;
      draw pb withcolor .625yellow ;
      pab := center pa  ..
             .5[center pa,center pb] shifted (0,36pt) ..
              center pb ;
      pab := pab cutbefore (pab intersectionpoint pa) ;
      pab := pab cutafter  (pab intersectionpoint pb) ;
      drawarrow pab dashed evenly withcolor .625red ;
      anchor_box(\MPanchor{\MPvar{from}}) ;
    elseif nb<na :
      fill pb withcolor .800white ;
      draw pb withcolor .625yellow ;
      pab := center pb {up} .. ulcorner bbox pb ;
      pab := pab cutbefore (pab intersectionpoint pb) ;
      drawarrow pab dashed evenly withcolor .625red ;
      anchor_box(\MPanchor{\MPvar{to}}) ;
    else :
      fill pa withcolor .800white ;
      draw pa withcolor .625yellow ;
      pab := center pa {up} .. urcorner bbox pa ;
      pab := pab cutbefore (pab intersectionpoint pa) ;
      drawarrow pab dashed evenly withcolor .625red ;
      anchor_box(\MPanchor{\MPvar{from}}) ;
    fi ;
  \stopMPpositiongraphic
  \MPpositiongraphic{mypos:arrow}
  \ifnum\MPp{\MPvar{from}}<\MPp{\MPvar{to}}\relax
    \expanded{\setMPpositiongraphic{\MPvar{to}}{mypos:arrow}{to=\MPvar{from}}}
  \fi
\stopMPpositionmethod
\stopbuffer

\startbuffer[g-circle]
\startMPpositiongraphic{mypos:circle}
  initialize_box(\MPpos{\MPvar{self}}) ;
  path p ; p := llxy..lrxy..urxy..ulxy..cycle ;
  pickup pencircle scaled 1pt ;
  fill p withcolor .800white ;
  draw p withcolor .625yellow ;
  anchor_box(\MPanchor{\MPvar{self}}) ;
\stopMPpositiongraphic
\stopbuffer

\startbuffer[g-line]
\startMPpositiongraphic{mypos:line}
  path pa, pb, pab ; numeric na, nb ;
  initialize_box(\MPpos{\MPvar{from}}) ;
  na := nxy ; pa := llxy..lrxy..urxy..ulxy..cycle ;
  initialize_box(\MPpos{\MPvar{to}}) ;
  nb := nxy ; pb := llxy..lrxy..urxy..ulxy..cycle ;
  if na=nb :
    pab := center pa -- center pb ;
    pab := pab cutbefore (pab intersectionpoint pa) ;
    pab := pab cutafter  (pab intersectionpoint pb) ;
    pickup pencircle scaled 1pt ;
    draw pab withcolor .625yellow ;
    anchor_box(\MPanchor{\MPvar{from}}) ;
  fi ;
\stopMPpositiongraphic
\stopbuffer

\getbuffer[a-arrow,g-circle,g-line]

% hm, slows down the whole doc

\setupbackgrounds
  [page]
  [background={backgraphics,foreground,foregraphics}]

\defineoverlay [backgraphics] [\positionoverlay{backgraphics}]
\defineoverlay [foregraphics] [\positionoverlay{foregraphics}]

\startbuffer[shape-a]
\startuniqueMPgraphic{meta:hash}{linewidth,linecolor,angle,gap}
  if unknown context_back : input mp-back ; fi ;
  some_hash ( \overlaywidth, \overlayheight ,
              \MPvar{linewidth}, \MPvar{linecolor} ,
              \MPvar{angle}, \MPvar{gap} ) ;
\stopuniqueMPgraphic
\stopbuffer

\startbuffer[shape-b]
\setupMPvariables
  [meta:hash]
  [gap=.25\bodyfontsize,
   angle=45,
   linewidth=\overlaylinewidth,
   linecolor=\overlaylinecolor]
\stopbuffer

\startbuffer[shape-c]
\def\metahashoverlay#1{\uniqueMPgraphic{meta:hash}{angle=#1}}

\defineoverlay[meta:hash:right]     [\metahashoverlay{ +45}]
\defineoverlay[meta:hash:left]      [\metahashoverlay{ -45}]
\defineoverlay[meta:hash:horizontal][\metahashoverlay{+180}]
\defineoverlay[meta:hash:vertical]  [\metahashoverlay{ -90}]
\stopbuffer

\startbuffer[symb-a]
\startuniqueMPgraphic{meta:button}{type,size,linecolor,fillcolor}
  if unknown context_butt : input mp-butt ; fi ;
  some_button ( \MPvar{type},
                \MPvar{size},
                \MPvar{linecolor},
                \MPvar{fillcolor} ) ;
\stopuniqueMPgraphic
\stopbuffer

\startbuffer[symb-b]
\setupMPvariables
  [meta:button]
  [type=1,
   size=2\bodyfontsize,
   fillcolor=gray,
   linecolor=darkred]
\stopbuffer

\startbuffer[symb-c]
\def\metabuttonsymbol#1{\uniqueMPgraphic{meta:button}{type=#1}}

\definesymbol[menu:left]  [\metabuttonsymbol{101}]
\definesymbol[menu:right] [\metabuttonsymbol{102}]
\definesymbol[menu:list]  [\metabuttonsymbol{103}]
\definesymbol[menu:index] [\metabuttonsymbol{104}]
\definesymbol[menu:person][\metabuttonsymbol{105}]
\definesymbol[menu:stop]  [\metabuttonsymbol{106}]
\definesymbol[menu:info]  [\metabuttonsymbol{107}]
\definesymbol[menu:down]  [\metabuttonsymbol{108}]
\definesymbol[menu:up]    [\metabuttonsymbol{109}]
\definesymbol[menu:print] [\metabuttonsymbol{110}]
\stopbuffer

\hyphenation{tool-kit}

\startbuffer[pagetext]
\subject{Edward  R. Tufte}      \input tufte   \par
\subject{Donald  E. Knuth}      \input knuth   \par
\subject{Douglas R. Hostadter}  \input douglas \page
\stopbuffer

\startbuffer[back-0]
\defineoverlay[page][\useMPgraphic{page}]
\setupbackgrounds[page][background=page]
\stopbuffer

\startbuffer[back-1]
\startuseMPgraphic{page}
  StartPage ;
    path Main ;
    if OnRightPage :
      Main := lrcorner Field[OuterMargin][Text] --
              llcorner Field[Text]       [Text] --
              ulcorner Field[Text]       [Text] --
              urcorner Field[OuterMargin][Text] -- cycle ;
    else :
      Main := llcorner Field[OuterMargin][Text] --
              lrcorner Field[Text]       [Text] --
              urcorner Field[Text]       [Text] --
              ulcorner Field[OuterMargin][Text] -- cycle ;
    fi ;
    Main := Main enlarged 6pt ;
    pickup pencircle scaled 2pt ;
    fill Page withcolor .625white ;
    fill Main withcolor .850white ;
    draw Main withcolor .625red ;
  StopPage ;
\stopuseMPgraphic
\stopbuffer

\startbuffer[back-2]
\startuseMPgraphic{page}
  StartPage ;
    pickup pencircle scaled 2pt ;
    fill Page                     withcolor .625white ;
    fill Field[OuterMargin][Text] withcolor .850white ;
    fill Field[Text]       [Text] withcolor .850white ;
    draw Field[OuterMargin][Text] withcolor .625red ;
    draw Field[Text]       [Text] withcolor .625red ;
  StopPage ;
\stopuseMPgraphic
\stopbuffer

\startbuffer[back-3]
\startuseMPgraphic{page}
  StartPage ;
    pickup pencircle scaled 2pt ;
    fill Page                            withcolor .625white ;
    fill Field[Text][Text] enlarged .5cm withcolor .850white ;
    draw Field[Text][Text] enlarged .5cm withcolor .625red ;
  StopPage ;
\stopuseMPgraphic
\stopbuffer

\startbuffer[back-4]
\startuseMPgraphic{page}
  StartPage ;
    def somewhere =
      (uniformdeviate 1cm,uniformdeviate 1cm)
    enddef ;
    path Main ;
    Main := Field[Text][Text] lrmoved somewhere --
            Field[Text][Text] llmoved somewhere --
            Field[Text][Text] ulmoved somewhere --
            Field[Text][Text] urmoved somewhere -- cycle ;
    pickup pencircle scaled 2pt ;
    fill Page withcolor .625white ;
    fill Main withcolor .850white ;
    draw Main withcolor .625red ;
  StopPage ;
\stopuseMPgraphic
\stopbuffer

\startbuffer[back-4x]
\startuseMPgraphic{page}
  StartPage ;
    path Main ; Main := Field[Text][Text] randomized 1cm ;
    pickup pencircle scaled 2pt ;
    fill Page withcolor .625white ;
    fill Main withcolor .850white ;
    draw Main withcolor .625red ;
  StopPage ;
\stopuseMPgraphic
\stopbuffer

\startbuffer[back-5]
\startuseMPgraphic{page}
  StartPage
    for i=Top,Header,Text,Footer,Bottom :
      for j=LeftEdge,LeftMargin,Text,RightMargin,RightEdge :
        draw Field[i][j] withpen pencircle scaled 2pt withcolor .625red ;
      endfor ;
    endfor ;
  StopPage
\stopuseMPgraphic
\stopbuffer

\def\Literature#1#2#3%
  {\noindentation#1\space{\bf#2}\space#3\par}

% sample macros

\startuseMPgraphic{sample setup}
  numeric Scale ; Scale := 2cm ;
  numeric Size ; Size := 2.5mm/Scale ;
  numeric Pen ; Pen := .25mm/Scale ;
  path Path ;
  pickup pencircle scaled (4Pen) ;
  def InRed    = withcolor .625red    enddef ;
  def InYellow = withcolor .625yellow enddef ;
  def InGray   = withpen pencircle scaled   Pen  withcolor .625white enddef ;
  def InBetween= withpen pencircle scaled (4Pen) withcolor .800white enddef ;
  def InBlack  = withpen pencircle scaled   Pen                      enddef ;
  def InWhite  = withpen pencircle scaled   Pen  withcolor     white enddef ;
  def DrawArrow text t =
    draw      t ; Path := boundingbox currentpicture ;
    drawarrow t ; setbounds currentpicture to Path ;
  enddef ;
  def ColorCircle =
    fill llcircle withcolor .625red    ;
    fill lrcircle withcolor .625green  ;
    fill urcircle withcolor .625blue   ;
    fill ulcircle withcolor .625yellow ;
  enddef ;
  evenly   := dashpattern(on (3mm/Scale) off (3mm/Scale)) ;
  withdots := dashpattern(off (2.5mm/Scale) on 0 off (2.5mm/Scale)) ;
  ahlength := 4mm/Scale ; ahangle := 30 ;
  draw (origin shifted (0,Size)--origin shifted (0,-Size)) ;
  draw (origin shifted (Size,0)--origin shifted (-Size,0)) ;
  picture Origin ; Origin := currentpicture ;
  setbounds Origin to origin--cycle ;
  currentpicture := nullpicture ;
\stopuseMPgraphic

\startuseMPgraphic{sample finish}
  draw boundingbox currentpicture InBlack ;
  draw Origin InGray ;
  currentpicture := currentpicture scaled Scale ;
\stopuseMPgraphic

% fm = metafun  macro
% fv = metafun  variable
% mc = metapost concept
% mm = metapost macro
% mp = metapost primitive
% mv = metapost variable

\unexpanded\def\DoDoSampleHead#1#2#3%
  {\setbox\scratchbox=\vbox % \ruledvbox
     {\tabskip0pt\tt\tfx\halign
        {\strut\hss##\unskip\unskip\cr#3\cr#1\cr}}%
   \ht\scratchbox=\ht\strutbox
   \dp\scratchbox=\dp\strutbox
   %\ruledhbox to \hsize{
   #2\black\hfill\box\scratchbox\ignorespaces}%}

\unexpanded\def\DoSampleHead#1#2#3%
  {\processaction
     [#1]
     [fm=>\DoDoSampleHead{metafun      macro}{#2}{#3},
      fv=>\DoDoSampleHead{metafun   variable}{#2}{#3},
      mc=>\DoDoSampleHead{metapost   concept}{#2}{#3},
      mm=>\DoDoSampleHead{metapost     macro}{#2}{#3},
      mp=>\DoDoSampleHead{metapost primitive}{#2}{#3},
      mv=>\DoDoSampleHead{metapost  variable}{#2}{#3}]}

\def\SampleHead#1#2#3%
  {\convertargument#3\to\ascii
   \expanded{\extrosubject{\noexpand\DoSampleHead{#1}{#2}{\ascii}}}}

\long\def\StartSample%
  {\doquintuplegroupempty\doStartSample}

\long\def\doStartSample#1#2#3#4#5#6\StopSample
  {\bgroup
   \def\&{\string&}%
   \SampleHead{#1}{#2}{#3}%
   \startuseMPgraphic{dummy}
     \includeMPgraphic{sample setup}
     #6
     \includeMPgraphic{sample finish}
   \stopuseMPgraphic
   \startlinecorrection[blank]
     \useMPgraphic{dummy}
   \stoplinecorrection
   \egroup}

\long\def\ShowSampleZ#1#2#3%
  {\bgroup
   \SampleHead{#1}{#2}{}%
   \blank#3\par
   \egroup}

\def\ShowSampleA#1#2#3%
  {\StartSample{#1}{#2}{#3}
     path p ; p := #3 ; % freeze randomized
     if length(p)>0 :
       DrawArrow  p InRed ;
     fi ;
     drawpoints p InBetween ;
   \StopSample}

\def\ShowSampleB#1#2#3#4%
  {\StartSample{#1}{#2}{#4}
     DrawArrow (#3) InRed ;
     DrawArrow (#4) InYellow ;
     drawpoints (#3) InBetween ;
     drawpoints (#4) InBetween ;
   \StopSample}

\def\ShowSampleC#1#2#3#4%
  {\StartSample{#1}{#2}{#4}
     path bb ; bb := boundingbox #3 ;
     DrawArrow #3 InRed ;
     draw #4 withpen pencircle scaled .15 InYellow ;
     setbounds currentpicture to bb ;
   \StopSample}

\def\ShowSampleD#1#2#3#4%
  {\StartSample{#1}{#2}{#4}
     DrawArrow #3 InRed ;
     setbounds currentpicture to #4 ;
   \StopSample}

\def\ShowSampleDD#1#2#3#4%
  {\StartSample{#1}{#2}{#4}
     DrawArrow #3 InRed ;
     DrawArrow #4 InYellow ;
   \StopSample}

\def\ShowSampleE#1#2#3#4%
  {\StartSample{#1}{#2}{#4}
     fill fullcircle scaled 1cm InRed ;
     currentpicture := currentpicture #3 ;
     Scale := 1 ;
   \StopSample}

\def\ShowSampleF#1#2#3#4%
  {\StartSample{#1}{#2}{#4}
     DrawArrow #3 InRed ;
     drawdot   #4 InYellow ;
   \StopSample}

\def\ShowSampleG#1#2#3#4%
  {\StartSample{#1}{#2}{#4}
     draw    #3 InRed ;
     drawdot #4 InYellow ;
   \StopSample}

\def\ShowSampleH#1#2#3#4#5%
  {\StartSample{#1}{#2}{#5}
     DrawArrow #3 InRed ;
     DrawArrow #4 InYellow ;
     drawdot   #5 InWhite ;
   \StopSample}

\def\ShowSampleI#1#2#3%
  {\StartSample{#1}{#2}{#3}
     draw fullcircle InRed #3 ;
   \StopSample}

\def\ShowSampleII#1#2#3%
  {\StartSample{#1}{#2}{#3}
     draw fullcircle #3 InRed ;
   \StopSample}

\def\ShowSampleJ#1#2#3%
  {\StartSample{#1}{withpen #2}{withpen #3 scaled 2mm}
     draw fullcircle xscaled 2 withpen #3 scaled Pen InRed ;
   \StopSample}

\def\ShowSampleK#1#2#3%
  {\StartSample{#1}{withpen #2}{withpen #3}
     draw fullcircle xscaled 2 withpen #3 InRed ;
   \StopSample}

\def\ShowSampleL#1#2#3%
  {\StartSample{#1}{#2}{#2 #3}
     #2 #3 InRed;
   \StopSample}

\def\ShowSampleM#1#2#3%
  {\StartSample{#1}{#2}{#2 #3}
     fill boundingbox (#3--cycle) InRed ;
     #2 #3 ;
   \StopSample}

\def\ShowSampleN#1#2#3#4%
  {\StartSample{#1}{#2}{#3}
     #3 ;
     draw #4 withpen pencircle scaled 25Pen InRed ;
   \StopSample}

\def\ShowSampleO#1#2#3%
  {\StartSample{#1}{#2}{#3}
     drawdot origin InRed ;
     #3 scaled (2.5/Scale) InYellow ;
   \StopSample}

\def\ShowSampleP#1#2#3%
  {\StartSample{#1}{#2}{#3} % switch still needed ?
     verbatimtex \usetypescript[palatino][ec]\switchtobodyfont[palatino,10pt] etex
     drawdot origin InRed ;
     #3 scaled (2.5/Scale) InYellow ;
   \StopSample}

\def\ShowSampleQ#1#2#3%
  {\StartSample{#1}{#2}{#3}
     #2 #3 withpen pencircle scaled 25Pen InRed ;
   \StopSample}

\def\ShowSampleQQ#1#2#3%
  {\StartSample{#1}{#2}{#3}
     #3 scaled (2.5/Scale) withpen pencircle scaled Pen InYellow ;
   \StopSample}

\def\ShowSampleR#1#2#3%
  {\StartSample{#1}{#2}{#3}
     ColorCircle ;
     addto currentpicture also (#3)
       shifted (bbwidth(currentpicture)+.1,0) ;
   \StopSample}

\def\ShowSampleS#1#2#3%
  {\StartSample{#1}{#2}{#3}
     Pen := Pen * Scale ; Scale := 1 ; #3 ;%
   \StopSample}

\startbuffer[handwrit]
\switchtobodyfont[lbr,hw] % lucida bright, handwriting

\startMPenvironment
  \switchtobodyfont[lbr,hw] % here too
\stopMPenvironment

\startMPpage
StartPage ;
  path p ; numeric l, n ; l := 1.5LineHeight ; n := 0 ;
  p := origin shifted (l,0) -- origin shifted (PaperWidth-l,0) ;
  for i=PaperHeight-l step -l until l :
    n := n + 1 ;
    drawoptions(withcolor .85white) ;
    fill         p shifted (0,i+StrutHeight) --
         reverse p shifted (0,i-StrutDepth ) -- cycle ;
    drawoptions(withpen pencircle scaled .25pt withcolor .5white) ;
    draw p shifted (0,i) ; draw p shifted (0,i+ExHeight) ;
    draw textext.origin("How are those penalty lines called in english?
      I may not steal candies ..." & decimal n) shifted (l,i) ;
  endfor ;
StopPage ;
\stopMPpage
\stopbuffer

\startbuffer[gridpage]
\startMPpage
  StartPage ;
    width  := PaperWidth  ; height := PaperHeight ; unit := cm ;
    drawoptions(withpen pencircle scaled .2pt withcolor .8white) ;
    draw vlingrid(0, width /unit, 1/10, width,  height) ;
    draw hlingrid(0, height/unit, 1/10, height, width ) ;
    drawoptions(withpen pencircle scaled .5pt withcolor .4white) ;
    draw vlingrid(0, width /unit, 1,    width,  height) ;
    draw hlingrid(0, height/unit, 1,    height, width ) ;
  StopPage ;
\stopMPpage
\stopbuffer

% needed to get white backgrounds

\startmode[screen]

\startbuffer[wipe]
picture savedpicture ;
savedpicture := currentpicture ;
currentpicture := nullpicture ;
draw savedpicture withcolor black ;
draw savedpicture ;
\stopbuffer

\stopmode

\startnotmode[screen]

\startbuffer[wipe]
  % nothing to whipe
\stopbuffer

\stopnotmode

\stopenvironment
