%D \module
%D   [       file=meta-mat,
%D        version=2013.07.19,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Math,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% A few accents:
%
% / for cambria

%D We need this for Alan, who nests math in \METAPOST:

\unprotect

\setupmathstackers
  [\c!mp=minifun::math:stacker:\number\scratchunicode]

\protect

\startMPextensions
    vardef math_stacker_bracket_shape(expr delta, rotate) =
        image (
            draw
                (0,OverlayOffset) --
                (0,delta-OverlayOffset) --
                (OverlayWidth,delta-OverlayOffset) --
                (OverlayWidth,OverlayOffset)
            withcolor
                OverlayLineColor ;
            setbounds currentpicture to boundingbox currentpicture bottomenlarged OverlayOffset ;
            if rotate :
                currentpicture := currentpicture rotated 180 ;
            fi ;
        )
    enddef ;
    vardef math_stacker_parent_shape(expr delta, rotate) =
        image (
            draw
                (0,OverlayOffset) ...
                (OverlayWidth/2,delta-OverlayOffset) ...
                (OverlayWidth,OverlayOffset)
            withcolor
                OverlayLineColor ;
            setbounds currentpicture to boundingbox currentpicture bottomenlarged OverlayOffset ;
            if rotate :
                currentpicture := currentpicture rotated 180 ;
            fi ;
        )
    enddef ;
    vardef math_stacker_brace_shape(expr delta, rotate) =
        image (
            draw
                (0,OverlayOffset) ...
                (OverlayWidth/4-OverlayOffset,delta-OverlayOffset) ...
                (OverlayWidth/2-OverlayOffset,delta-OverlayOffset) ...
                (OverlayWidth/2,delta) &
                (OverlayWidth/2,delta) ...
                (OverlayWidth/2+OverlayOffset,delta-OverlayOffset) ...
                (3OverlayWidth/4+OverlayOffset,delta-OverlayOffset) ...
                (OverlayWidth,OverlayOffset)
            withcolor
                OverlayLineColor ;
            setbounds currentpicture to boundingbox currentpicture bottomenlarged OverlayOffset ;
            if rotate :
                currentpicture := currentpicture rotated 180 ;
            fi ;
        )
    enddef ;
    vardef math_stacker_bar_shape(expr rotate) =
        image (
            draw
                (0,OverlayOffset) -- (OverlayWidth,OverlayOffset)
            withcolor
                OverlayLineColor ;
            setbounds currentpicture to boundingbox currentpicture bottomenlarged OverlayOffset ;
            if rotate :
                currentpicture := currentpicture rotated 180 ;
            fi ;
        )
    enddef ;
    % arrows
    vardef math_stacker_arrow_shape(expr axis, ex, em, rotate) =
        image (
            interim ahlength := ex/2;
            drawarrow
                ((OverlayWidth,ex/2) -- (0,ex/2))
            withcolor
                OverlayLineColor ;
            setbounds currentpicture to unitsquare xysized(OverlayWidth,ex) ;
            if rotate :
                currentpicture := currentpicture rotated 180 ;
            fi ;
        )
    enddef ;
    vardef math_stacker_leftrightarrow_shape(expr axis, ex, em, rotate) =
        image (
            interim ahlength := ex/2;
            drawdblarrow
                ((OverlayWidth,ex/2) -- (0,ex/2))
            withcolor
                OverlayLineColor ;
            setbounds currentpicture to unitsquare xysized(OverlayWidth,ex) ;
            if rotate :
                currentpicture := currentpicture rotated 180 ;
            fi ;
        )
    enddef ;
    vardef math_stacker_rightoverleftarrow_shape(expr axis, ex, em, rotate) =
        image (
            interim ahlength := ex/2;
            drawdoublearrows
                ((OverlayWidth,ex/2) -- (0,ex/2))
            withcolor
                OverlayLineColor ;
            setbounds currentpicture to unitsquare xysized(OverlayWidth,ex) ;
            if rotate :
                currentpicture := currentpicture rotated 180 ;
            fi ;
        )
    enddef ;

    % main handler

    def math_stacker_draw_accent(expr p) =
%         draw p withpen pencircle scaled OverlayLineWidth xyscaled (2,3) rotated 45 ;
%         draw p withpen pencircle xscaled (2OverlayLineWidth) yscaled (3OverlayLineWidth/4) rotated 45 ;
        draw p withpen pencircle scaled OverlayLineWidth ;
    enddef ;

    def math_stacker_draw_arrow(expr p) =
        draw p withpen pencircle scaled OverlayLineWidth ;
    enddef ;
\stopMPextensions

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE3B4\else23B4\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_bracket_shape(OverlayHeight,false)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE3B5\else23B5\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_bracket_shape(OverlayDepth,true)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE3DC\else23DC\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_parent_shape(OverlayHeight,false)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE3DD\else23DD\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_parent_shape(OverlayDepth,true)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE3DE\else23DE\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_brace_shape(OverlayHeight,false)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE3DF\else23DF\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_brace_shape(OverlayDepth,true)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE33E\else203E\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_bar_shape(false)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"\ifcase\contextlmtxmode FE33F\else203F\fi}{axis,ex,em}
    math_stacker_draw_accent(math_stacker_bar_shape(true)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"2190}{axis,ex,em}
    math_stacker_draw_arrow(math_stacker_arrow_shape(\MPvar{axis},\MPvar{ex},\MPvar{em},false)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"2192}{axis,ex,em}
    math_stacker_draw_arrow(math_stacker_arrow_shape(\MPvar{axis},\MPvar{ex},\MPvar{em},true)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"2194}{axis,ex,em}
    math_stacker_draw_arrow(math_stacker_leftrightarrow_shape(\MPvar{axis},\MPvar{ex},\MPvar{em},false)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"27F7}{axis,ex,em}
    math_stacker_draw_arrow(math_stacker_leftrightarrow_shape(\MPvar{axis},\MPvar{ex},\MPvar{em},false)) ;
\stopuniqueMPgraphic

\startuniqueMPgraphic{minifun::math:stacker:\number"21C4}{axis,ex,em}
    math_stacker_draw_arrow(math_stacker_rightoverleftarrow_shape(\MPvar{axis},\MPvar{ex},\MPvar{em},false)) ;
\stopuniqueMPgraphic

%D Radicals:

\startMPextensions
    vardef math_radical_simple(expr w,h,d,o) =
        (-h/2-o,h/2-o) --
        (-h/4-o,-d-o)  --
        (-o,h+o)       --
        (w+o,h+o)      --
        (w+o,h-h/10+o)
    enddef ;
\stopMPextensions

\startuniqueMPgraphic{minifun::math:radical:default}{axis,ex,em}
    draw
        math_radical_simple(OverlayWidth,OverlayHeight,OverlayDepth,OverlayOffset)
     %  withpen pencircle xscaled (2OverlayLineWidth) yscaled (3OverlayLineWidth/4) rotated 30
        withpen pencircle scaled OverlayLineWidth
      % dashed evenly
        withcolor OverlayLineColor ;
\stopuniqueMPgraphic

% already setup:
%
% definemathradical [sqrt]   [mp=minifun::math:radical:default]

\continueifinputfile{meta-imp-mat.mkiv}

\setupmathstackers [both]   [color=darkred,alternative=mp]
\setupmathstackers [top]    [color=darkred,alternative=mp]
\setupmathstackers [bottom] [color=darkred,alternative=mp]

\startTEXpage[offset=10pt]

$
    \overbracket   {a+b+c+d} \quad
    \underbracket  {a+b+c+d} \quad
    \doublebracket {a+b+c+d}
$
\blank[2*big]
$
    \overparent   {a+b+c+d} \quad
    \underparent  {a+b+c+d} \quad
    \doubleparent {a+b+c+d}
$
\blank[2*big]
$
    \overbrace   {a+b+c+d} \quad
    \underbrace  {a+b+c+d} \quad
    \doublebrace {a+b+c+d}
$
\blank[2*big]
$
    \overbar   {a+b+c+d} \quad
    \underbar  {a+b+c+d} \quad
    \doublebar {a+b+c+d}
$
\blank[2*big]
$
    \overleftarrow  {a+b+c+d} \quad
    \overrightarrow {a+b+c+d}
$
\blank[2*big]
$
    \underleftarrow  {a+b+c+d} \quad
    \underrightarrow {a+b+c+d}
$
\blank[2*big]
\setupmathradical[sqrt][alternative=mp,color=darkgreen]
$
    \sqrt[2]{a+b+c+d} \quad
$

\stopTEXpage
