mp-blob.mpiv /size: 4126 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=mp-blob.mpiv,
3%D        version=2018.04.08,
4%D          title=\CONTEXT\ \METAPOST\ graphics,
5%D       subtitle=Blobs,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D This is a follow up on good old \type {meta-imp-txt}.
15
16if known context_blob : endinput ; fi ;
17
18boolean context_blob ; context_blob := true ;
19
20numeric mfun_blob_n ; mfun_blob_n := 0 ;
21picture mfun_blob_c ;
22color   mfun_blob_b ;
23
24def mfun_reset_tex_blobs =
25    mfun_blob_n := 0 ;
26    mfun_blob_c := nullpicture ;
27enddef ;
28
29extra_endfig := extra_endfig & "mfun_reset_tex_blobs ; " ;
30
31vardef mfun_inject_blob(expr n) =
32    mfun_blob_c := nullpicture ;
33    mfun_blob_b := lua.mp.mf_blob_dimensions(mfun_blob_n,n) ;
34    addto mfun_blob_c doublepath unitsquare
35        xscaled redpart mfun_blob_b
36        yscaled (greenpart mfun_blob_b + bluepart mfun_blob_b)
37        shifted (0,- bluepart mfun_blob_b)
38        withprescript "mf_object=texblob"
39        withprescript "tb_blob=" & decimal lua.mp.mf_blob_index(mfun_blob_n,n) ;
40    mfun_blob_c
41enddef ;
42
43% An example of usage:
44
45newinternal followtextalternative   ; followtextalternative   := 1 ;
46newinternal tracingfollowtext       ; tracingfollowtext       := 0 ;
47newinternal autoscaleupfollowtext   ; autoscaleupfollowtext   := 2 ;
48newinternal autoscaledownfollowtext ; autoscaledownfollowtext := 0 ;
49
50vardef followtext(expr pth, txt) =
51    image (
52        mfun_blob_n := mfun_blob_n + 1 ;
53        lua.mp.mf_inject_blob(mfun_blob_n,txt);
54        save pat, al, at, pl, pc, wid, pos, ap, ad, pic, len, n, b, sc ;
55        path pat, b ; pat := pth ;
56        numeric al, at, pl, pc, wid, pos, len[], n, sc ;
57        pair ap, ad ;
58        picture pic[] ;
59        len[0] := 0 ;
60        n := lua.mp.mf_blob_size(mfun_blob_n) ;
61        sc := 0 ;
62        for i=1 upto n :
63            pic[i] := mfun_inject_blob(i) ;
64            pic[i] := pic[i] shifted - llcorner pic[i] ;
65            len[i] := len[i-1] + lua.mp.mf_blob_width(mfun_blob_n,i) ;
66        endfor ;
67        al := arclength pth ;
68        if al = 0 :
69            al := len[n] ;
70            pat := origin -- (al,0) ;
71        fi ;
72        if ((al < len[n]) and (autoscaleupfollowtext   > 0)) or
73           ((al > len[n]) and (autoscaledownfollowtext > 0)) :
74            sc := len[n] /al ;
75            pat := pat scaled sc ;
76            al := arclength pat ;
77        fi ;
78        if followtextalternative = 1 :
79            pl := (al-len[n])/(if n>1 : (n-1) else : 1 fi) ;
80            pc := 0 ;
81        else : % centered / MP
82            pl := 0 ;
83            pc := arclength pat/2 - len[n]/2 ;
84        fi ;
85        if tracingfollowtext = 1 :
86            draw pat withpen pencircle scaled 1pt withcolor blue ;
87        fi ;
88        for i=1 upto n :
89            wid := lua.mp.mf_blob_width(mfun_blob_n,i) ;
90            pos := len[i]-wid/2 + (i-1)*pl + pc ;
91            at := arctime   pos of pat ;
92            ap := point     at  of pat ;
93            ad := direction at  of pat ;
94            pic[i] := pic[i]
95                shifted (-wid/2,0)
96                if ad <> origin : rotated(angle(ad)) fi
97                shifted ap ;
98            draw pic[i] ;
99            if tracingfollowtext = 1 :
100                draw boundingbox pic[i] withpen pencircle scaled .25pt withcolor red ;
101                draw ap withpen pencircle scaled .50pt withcolor green ;
102            fi ;
103        endfor ;
104        if ((autoscaleupfollowtext = 2) or (autoscaledownfollowtext = 2)) and (sc <> 0) and (sc <> 1):
105            currentpicture := currentpicture scaled (1/sc) ;
106        fi ;
107        b := boundingbox currentpicture ;
108        if tracingfollowtext = 1 :
109            draw b withpen pencircle scaled .25pt withcolor blue ;
110        fi ;
111        draw fullcircle scaled 100bp
112            withprescript "mf_object=followtext"
113            withprescript "ft_category=" & decimal mfun_blob_n ;
114        setbounds currentpicture to b ;
115    )
116enddef ;
117