%D \module
%D   [       file=mp-text.mpiv,
%D        version=2000.07.10,
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=text support,
%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 licen-en.pdf for
%C details.

%D This one is only used in metafun so it will become a module.

if known metafun_loaded_text : endinput ; fi ;

newinternal boolean metafun_loaded_text ; metafun_loaded_text := true ; immutable metafun_loaded_text ;

% This is still mostly the same as the one discussed in the good old \METAFUN\
% example code but modernized abit to suit \LMTX. We can actually use \hsplit
% now!

newscriptindex mfid_setparshapeproperty ; mfid_setparshapeproperty := scriptindex "setparshapeproperty" ;

% this is the old name

presetparameters "parshape" [
  % offset      = 0,
  % path        = fullsquare,
  % dx          = 0,
  % dy          = 0,
  % strutheight = StrutHeight,
  % strutdepth  = StutDepth,
  % lineheight  = LineHeight,
  % topskip     = StrutHeight,
  % trace       = false,
] ;

def lmt_parshape = applyparameters "parshape" "lmt_do_parshape" enddef ;

def lmt_do_parshape = % todo: check and improve this rather oldie

    begingroup ; pushparameters "parshape" ;

    save
        p, q, l, r, line, tt, bb,
        dx, dy, baselineskip, strutheight, strutdepth, topskip, bottomskip, offset, trace,
        n, hsize, vsize, vvsize, voffset, hoffset, width, indent,
        ll, lll, rr, rrr, cp, cq, t, b,
        found_point ;

    path
        p, q, l, r, line, tt, bb ;
    numeric
        dx, dy, baselineskip, strutheight, strutdepth, topskip, offset,
        n, hsize, vsize, vvsize, voffset, hoffset, width[], indent[] ;
    pair
        ll, lll, rr, rrr, cp, cq, t, b ;
    boolean
        trace ;

    % specification:

    p            := getparameterdefault "path"         fullsquare ;
    dx           := getparameterdefault "dx"           0 ;
    dy           := getparameterdefault "dy"           0 ;
    baselineskip := getparameterdefault "baselineskip" LineHeight ;
    strutheight  := getparameterdefault "strutheight"  StrutHeight ;
    strutdepth   := getparameterdefault "strutdepth"   StrutDepth ;
    topskip      := getparameterdefault "topskip"      StrutHeight ;
    bottomskip   := getparameterdefault "bottomskip"   0 ;
    offset       := getparameterdefault "offset"       0 ;
    trace        := getparameterdefault "trace"        false ;

    n  := 0 ;
    cp := center p ;

    if hasparameter "offsetpath" :
        q       := getparameter "offsetpath" ;
        voffset := dy ;
        hoffset := dx ;
    else :
        q       := p ;
        hoffset := offset + dx ;
        voffset := offset + dy ;
    fi ;

    cq := center q ;

    hsize := xpart lrcorner q - xpart llcorner q ;
    vsize := ypart urcorner q - ypart lrcorner q ;

    q := p shifted - cp ;

    runscript mfid_setparshapeproperty "voffset" voffset ;
    runscript mfid_setparshapeproperty "hoffset" hoffset ;
    runscript mfid_setparshapeproperty "width"   hsize ;
    runscript mfid_setparshapeproperty "height"  vsize ;

    if hasparameter "offsetpath" :
        q := q xscaled ((hsize-2hoffset)/hsize) yscaled ((vsize-2voffset)/vsize) ;
    fi ;

    hsize := xpart lrcorner q - xpart llcorner q ;
    vsize := ypart urcorner q - ypart lrcorner q ;

    t := (ulcorner q -- urcorner q) intersection_point q ;
    b := (llcorner q -- lrcorner q) intersection_point q ;

    if xpart directionpoint t of q < 0 :
        q := reverse q ;
    fi ;

    l := q cutbefore t ;
    l := l if xpart point 0 of q < 0 : & q fi cutafter b ;

    r := q cutbefore b ;
    r := r if xpart point 0 of q > 0 : & q fi cutafter t ;

    vardef found_point (expr lin, pat, sig) =
        save a, b; pair a, b ;
        a := pat intersection_point (lin shifted (0,strutheight)) ;
        if intersection_found :
            a := a shifted (0,-strutheight) ;
        else :
            a := pat intersection_point lin ;
        fi ;
        b := pat intersection_point (lin shifted (0,-strutdepth)) ;
        if intersection_found :
            if sig :
                if xpart b > xpart a : a := b shifted (0,strutdepth) fi ;
            else :
                if xpart b < xpart a : a := b shifted (0,strutdepth) fi ;
            fi ;
        fi ;
        a
    enddef ;

    if (strutheight + strutdepth < baselineskip) :
        vvsize := vsize ;
    else :
        vvsize := (vsize div baselineskip) * baselineskip ;
    fi ;

    runscript mfid_setparshapeproperty "first" false ;

    for i = topskip step baselineskip until (vvsize + bottomskip) :

        line := (ulcorner q -- urcorner q) shifted (0,-i-eps) ;

        ll := found_point(line,l,true ) ;
        rr := found_point(line,r,false) ;

        if trace :
            fill (ll -- rr -- rr shifted (0,strutheight) -- ll shifted (0,strutheight) -- cycle) shifted cp withcolor .6white ;
            fill (ll -- rr -- rr shifted (0,-strutdepth) -- ll shifted (0,-strutdepth) -- cycle) shifted cp withcolor .8white ;
            draw ll shifted cp withpen pencircle scaled 2pt ;
            draw rr shifted cp withpen pencircle scaled 2pt ;
            draw (ll -- rr) shifted cp  withpen pencircle scaled .5pt ;
        fi ;

        n := n + 1 ;
        indent[n] := abs(xpart ll - xpart llcorner q) ;
        width[n]  := abs(xpart rr - xpart ll) ;

        if (i = strutheight) and (width[n] < baselineskip) :
            n := n - 1 ;
            runscript mfid_setparshapeproperty "first" true ;
        fi ;

    endfor ;

    if trace :
        drawarrow p            withpen pencircle scaled 2pt withcolor red ;
        drawarrow l shifted cp withpen pencircle scaled 1pt withcolor green ;
        drawarrow r shifted cp withpen pencircle scaled 1pt withcolor blue ;
    fi ;

    runscript mfid_setparshapeproperty "lines" n ;

    for i=1 upto n:
        runscript mfid_setparshapeproperty "line" i (indent[i]) (width[i]) ;
    endfor ;

    popparameters ; endgroup ;

enddef ;

def build_parshape (expr p, offset_or_path, dx, dy, baselineskip, strutheight, strutdepth, topskip) =
    if path offset_or_path :
        lmt_parshape [
            path        = p,
            offsetpath  = offset_or_path,
            dx          = dx,
            dy          = dy,
            strutheight = strutheight,
            strutdepth  = strutdepth,
            lineheight  = lineheight,
            topskip     = topskip,
            trace       = (if unknown trace_parshape : false else : trace_parshape fi),
        ]
    else :
        lmt_parshape [
            path        = p,
            offset      = offset_or_path,
            dx          = dx,
            dy          = dy,
            strutheight = strutheight,
            strutdepth  = strutdepth,
            lineheight  = lineheight,
            topskip     = topskip,
            trace       = (if unknown trace_parshape : false else : trace_parshape fi),
        ]
    fi ;
enddef ;
