%D \module
%D   [      file=s-pre-69,
%D        version=2010.04.28,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Presentation Environment 69,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA ADE]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\setuppapersize[S6][S6]
\setuppapersize[SM][SM]

\usemodule
  [abr-01,pre-60]

\setupinteraction
  [state=start,
   contrastcolor=white,
   color=white,
   click=no]

\setuplayout
  [location=middle,
   topspace=60pt,
   bottomspace=80pt,
   backspace=80pt,
   header=0pt,
   footer=0pt,
   width=middle,
   height=middle]

\setupcolors
  [textcolor=white]

\setupbodyfont[euler]

\definecolor[maincolor] [blue]
\definecolor[extracolor][green]

% \definecolor[maincolor] [red]
% \definecolor[extracolor][blue]

\startMPinitializations
    if unknown MyColor[1] :
        color MyColor[] ;
        MyColor[1] := transparent(1,.25,\MPcolor{maincolor}) ;
        MyColor[2] := transparent(1,.25,\MPcolor{extracolor}) ;

        picture MySoFar ; MySoFar := nullpicture ;
        path MyLastOne ; MyLastOne := origin -- cycle ;
        color MyPageColor ; MyPageColor := MyColor[1] ;
        path MyLeftSteps, MyRightSteps ;
        boolean MyPageDone ; MyPageDone := false ;

        vardef MySmallShape(expr parent) =
            path p ; p := boundingbox parent ;
            p := boundingbox parent ;
            numeric w, h ; w := bbwidth(p) ; h := bbheight(p) ;
            urcorner p shifted (-uniformdeviate w/4,0) --
            lrcorner p shifted (0,uniformdeviate h/4) --
            llcorner p shifted (uniformdeviate w/4,0) --
            ulcorner p shifted (0,-uniformdeviate h/4) -- cycle
        enddef ;

        vardef MyShape(expr parent) =
            path p ; p := boundingbox parent ;
            if MyPageDone :
                MyPageDone := false ;
                urcorner p shifted (-EmWidth + -uniformdeviate CutSpace/2,0) --
                lrcorner p shifted (0,EmWidth + uniformdeviate BottomSpace/2) --
                llcorner p shifted (EmWidth + uniformdeviate BackSpace/2,0) --
                ulcorner p shifted (0,-EmWidth + -uniformdeviate TopSpace/2) -- cycle
            else :
                MyPageDone := true ;
                urcorner p shifted (0,-EmWidth + -uniformdeviate TopSpace/2) --
                lrcorner p shifted (-EmWidth + -uniformdeviate CutSpace/2,0) --
                llcorner p shifted (0,EmWidth + uniformdeviate BottomSpace/2) --
                ulcorner p shifted (EmWidth + uniformdeviate BackSpace/2,0) -- cycle
            fi
        enddef ;

        vardef MyMakeOne =
            MyLastOne := MyShape(Page) ;
        enddef ;

        vardef MyAddOne =
            addto MySoFar also image(fill MyLastOne withcolor MyPageColor ; ) ;
        enddef ;

        vardef MyDrawOne =
            fill MyLastOne withcolor black ;
            fill MyLastOne withcolor MyPageColor ;
        enddef ;

        vardef MyDrawPage =
            draw MySoFar ;
        enddef ;

        vardef MySetSteps =
            path l, r ; numeric s ; path ll[], rr[] ; path t ;
            l := point 2 of MyLastOne -- point 3 of MyLastOne ;
            r := point 0 of MyLastOne -- point 1 of MyLastOne ;
            t := topboundary Field[Text][Text] rightenlarged TextWidth leftenlarged TextWidth ;
            s := bbheight(Field[Text][Text])/LineHeight + 2 ;
            t := t shifted (0,-TopSkip) ;
            for i=1 upto s :
                ll[i] := t intersectionpoint l ;
                rr[i] := t intersectionpoint r ;
                t := t shifted (0,-LineHeight) ;
            endfor ;
            MyLeftSteps  := for i=1 upto s : ll[i] -- endfor cycle ;
            MyRightSteps := for i=1 upto s : rr[i] -- endfor cycle ;
        enddef ;

        vardef MyDrawText(expr txt) =
            pair a ; a := (point 1 of MyLastOne) - (point 2 of MyLastOne) ;
            picture p ; p := txt ;
            p := p
                shifted (-EmWidth,EmWidth)
                shifted ulcorner txt
                shifted point 1 of MyLastOne ;
            p := p rotatedaround(lrcorner p, radian * tan(ypart a/xpart a)) ;
            setbounds p to origin -- cycle ;
            draw p ;
        enddef ;

        vardef MyDrawTitle(expr txt) =
        %   pair a ; a := (point 2 of MyLastOne) - (point 3 of MyLastOne) ;
            pair a ; a := (point 3 of MyLastOne) - (point 4 of MyLastOne) ;
            picture p ;
            if bbheight(txt) > bbwidth(txt) :
                p := txt ysized(0.8*TextHeight) ;
            else :
                p := txt xsized(0.8*TextWidth) ;
            fi ;
            numeric d ; d := arclength(point 2 of MyLastOne -- point 3 of MyLastOne) - bbheight(p) ;
            p := p
                shifted (BackSpace,-d/2)
                shifted -ulcorner p
                shifted point 3 of MyLastOne ;
        %   p := p rotatedaround(ulcorner p, - radian * tan(xpart a/ypart a)) ;
        %   p := p rotatedaround(ulcorner p,  radian * tan(ypart a/xpart a)) ;
            setbounds p to origin -- cycle ;
            draw p ;
        enddef ;

        vardef MyDrawSteps =
            s := bbheight(Field[Text][Text])/LineHeight + 2 ;
            for i=1 upto s :
                draw ll[i] withpen pencircle scaled 1mm ;
                draw rr[i] withpen pencircle scaled 1mm ;
                draw ll[i] -- rr[i] ;
            endfor ;
            draw Field[Text][Text] ;
        enddef ;

    fi ;
\stopMPinitializations

\startuseMPgraphic{initialization}
    StartPage ;
        MySoFar := image(fill Page enlarged 12pt withcolor MyPageColor) ;
        MyMakeOne ;
        MySetSteps ;
    StopPage ;
\stopuseMPgraphic

\appendtoks
    \startnointerference
        \useMPgraphic{initialization}
    \stopnointerference
\to \everystarttext

\startuseMPgraphic{page}
    StartPage ;
        MyDrawPage ;
        MyDrawOne ;
        MySetSteps ;
        MyDrawTitle(textext("\getvariable{document}{title}")) ;
        MyDrawText(textext("\getvariable{document}{topic}")) ;
        %
        % we have multiple runs when we have text
        %
%         MyDrawSteps ;
%         MyMakeOne ;
%         MySetSteps ;
    StopPage ;
\stopuseMPgraphic

\appendtoks
    \startnointerference
        \startMPcode
            MyAddOne ;
            MyMakeOne ;
            MySetSteps ;
        \stopMPcode
    \stopnointerference
\to \everyshipout

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

\startuseMPgraphic{symbol}
    color cc ; cc := MyColor[2] ;
    path p ; p := MySmallShape(unitsquare scaled (.6*LineHeight)) ;
    fill p withcolor white ;
    fill p withcolor cc ;
\stopuseMPgraphic

\definesymbol[mysymbol][\struttedbox{\useMPgraphic{symbol}}]

\setupitemgroup[itemize][1][symbol=mysymbol]

\setupbackgrounds
  [page]
  [background=page]

\startluacode
    local texdimen = tex.dimen
    function document.SetParShape()
        local leftpath  = metapost.getclippath("metafun","metafun","clip currentpicture to MyLeftSteps ;")
        local rightpath = metapost.getclippath("metafun","metafun","clip currentpicture to MyRightSteps ;")
        local shape = { }
        for i=1,#leftpath do
            local left  = leftpath[i].x_coord
            local right = rightpath[i].x_coord
            local hsize = right - left - (texdimen.backspace + texdimen.cutspace)*number.dimenfactors.bp
            shape[#shape+1] = string.format("%sbp %sbp",left,hsize)
        end
     -- print(table.serialize(shape))
        tex.sprint(tex.ctxcatcodes,string.format("\\parshape %s %s",#shape,table.concat(shape," ")))
    end
\stopluacode

\nopenalties \dontcomplain

\setupwhitespace[none]

\def\StartText#1#2%
  {\starttext
   \setvariable{document}{title}{\framed[frame=off,offset=0pt,align=flushleft,foregroundstyle=\tfd\setupinterlinespace]{\begstrut#1\endstrut}}
   \setvariable{document}{topic}{\tfb#2}
   \startstandardmakeup
     % dummy page
   \stopstandardmakeup
   \setvariable{document}{title}{}
   \setvariable{document}{topic}{}}

\def\StopText
  {\stoptext}

\def\StartItems#1%
  {\setvariable{document}{topic}{\tfb#1}
   \startstandardmakeup[top=,bottom=\vss]
   \ctxlua{document.SetParShape()}
   \StartSteps}

\def\StopItems
  {\StopSteps
   \stopstandardmakeup}

\def\StartItem
  {\dontleavehmode\llap{\symbol[mysymbol]\quad}\ignorespaces}

\def\StopItem
  {\removeunwantedspaces\nobreak\crlf\crlf\FlushStep}

\def\ShapeParagraph
  {\ctxlua{document.SetParShape()}}

% no parshape yet

\def\StartParagraphs#1%
  {\setvariable{document}{topic}{\tfb#1}
   \startstandardmakeup[top=,bottom=\vss]
  %\ctxlua{document.SetParShape()}
   \StartSteps}

\def\StopParagraphs
  {\StopSteps
   \stopstandardmakeup}

\def\StartParagraph
  {}

\def\StopParagraph
  {\par\FlushStep}

\doifnotmode{demo}{\endinput}

% finetuning: \StartText{\TEX\ and Reality\vskip2exClashing Mindsets?\vskip1ex}{Bacho\TEX, May 1, 2010}

\StartText{Just\\A Demo}{Bacho\TEX, May 1, 2010}

\StartItems{Quote from Tufte and Ward}
    \StartItem
        \input tufte
    \StopItem
    \StartItem
        \input ward
    \StopItem
\StopItems

% \dorecurse{20}{
%     \ctxlua{document.SetParShape()}
%     \input tufte
%     \page
% }

\StopText

