%D \module %D [ file=mp-luas.lmtx, %D version=2019.06.23, %D title=\CONTEXT\ \METAPOST\ graphics, %D subtitle=\LUA, %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. % This is an experimental module where I test some new interface methods; % for real advanced graphics use the luapost module. if known context_lmtx : endinput ; fi ; boolean context_lmtx ; context_lmtx := true ; presetparameters "grid" [ nx = 1, dx = 1, ny = 1, dy = 1, ] ; def lmt_grid = applyparameters "grid" "lmt_do_grid" enddef ; vardef lmt_do_grid = image ( save nx; nx := getparameter "grid" "nx" ; save ny; ny := getparameter "grid" "ny" ; save dx; dx := getparameter "grid" "dx" ; save dy; dy := getparameter "grid" "dy" ; for i = 0 step dx until nx : draw ((0,0) -- (0,ny)) shifted (i,0) ; endfor ; for i = 0 step dy until ny : draw ((0,0) -- (nx,0)) shifted (0,i) ; endfor ; ) enddef ; def lmt_axis = applyparameters "axis" "lmt_do_axis" enddef ; presetparameters "axis" [ nx = 1, dx = 1, tx = 0, sx = 1, startx = 0, ny = 1, dy = 1, ty = 0, sy = 1, starty = 0, samples = { }, list = { }, connect = false, list = [ close = false ], samplecolors = { "" }, axiscolor = "", textcolor = "", ] ; % vardef lmt_do_axis = % image ( % save nx; nx := getparameter "axis" "nx" ; % save ny; ny := getparameter "axis" "ny" ; % save dx; dx := getparameter "axis" "dx" ; % save dy; dy := getparameter "axis" "dy" ; % save tx; tx := getparameter "axis" "tx" ; % save ty; ty := getparameter "axis" "ty" ; % save c, startx, starty ; string c ; % c := getparameter "axis" "axiscolor" ; % startx := getparameter "axis" "startx" ; % starty := getparameter "axis" "starty" ; % draw (startx,starty) -- (startx,ny) withcolor c ; % draw (startx,starty) -- (nx,starty) withcolor c ; % for i = 0 step dx until nx : % if (i > startx) or (startx = 0) : % draw ((0,0) -- (0,-2)) shifted (i,starty) withcolor c ; % fi ; % endfor ; % for i = 0 step dy until ny : % if (i > starty) or (starty = 0) : % draw ((0,0) -- (-2,0)) shifted (startx,i) withcolor c ; % fi ; % endfor ; % if tx <> 0 : % c := getparameter "axis" "textcolor" ; % for i = 0 step tx until nx : % if (i > startx) or (startx = 0) : % draw % textext("\strut " & decimal i) ysized 2 shifted (i,-4+starty) % withcolor c; % fi ; % endfor ; % fi ; % if ty <> 0 : % c := getparameter "axis" "textcolor" ; % for i = 0 step ty until ny : % if (i > starty) or (starty = 0) : % draw % textext.lft("\strut " & decimal i) ysized 2 shifted (-3+startx,i) % withcolor c; % fi ; % endfor ; % fi ; % % if (getparametercount "axis" "samples") > 0 : % if getparameter "axis" "connect" : % for s = 1 upto getparametercount "axis" "samples" : % c := getparameter "axis" "samplecolors" s ; % draw for i = 1 upto getparametercount "axis" "samples" s : % if (i > 1) : -- fi (i, getparameter "axis" "samples" s i) % endfor % withcolor c ; % endfor ; % else : % for s = 1 upto getparametercount "axis" "samples" : % c := getparameter "axis" "samplecolors" s ; % for i = 1 upto getparametercount "axis" "samples" s : % draw (i, getparameter "axis" "samples" s i) % withcolor c ; % endfor ; % endfor ; % fi ; % fi ; % % if (getparametercount "axis" "list") > 0 : % % path p ; numeric ts ; pair a, d ; % % ts := (getparameter "axis" "sy") / 20 ; % % for s = 1 upto getparametercount "axis" "list" : % % c := getparameter "axis" "list" s "color" ; % % p := for i = 1 upto getparametercount "axis" "list" s "points" : % if (i > 1) : -- fi (getparameter "axis" "list" s "points" i) % endfor % if (getparameterdefault "axis" "list" s "close" false) : -- cycle fi ; % % draw p withcolor c ; % % if (getparametercount "axis" "list" s "labels") > 0 : % for i = 1 upto getparametercount "axis" "list" s "labels" : % draw % textext(getparameterdefault "axis" "list" s "labels" i "") % ysized ts % shifted unitvector(direction (i-eps) of p) % todo % shifted point i of p ; % endfor ; % fi ; % % % if (getparametercount "axis" "list" s "texts") > 0 : % % for i = 1 upto getparametercount "axis" "list" s "texts" : % % n := i + 0.5 ; % % a := point n of p ; % % d := direction n of p ; % % draw textext.d(getparameterdefault "axis" "list" s "texts" i "") % % if d < left : rotated 180 shifted (0,-5) else : shifted (0,5) fi % % ysized ts % % shifted a % % rotatedaround(a,angle(d)) ; % % endfor ; % % fi ; % % pushparameters "axis" "list" s "texts" ; % if (getparametercount) > 0 : % for i = 1 upto getparametercount : % n := i + 0.5 ; % a := point n of p ; % d := direction n of p ; % draw textext.d(getparameterdefault i "") % if d < left : rotated 180 shifted (0,-5) else : shifted (0,5) fi % ysized ts % shifted a % rotatedaround(a,angle(d)) ; % endfor ; % fi ; % popparameters ; % % endfor ; % fi ; % % ) % xyscaled(getparameter "axis" "sx",getparameter "axis" "sy") % enddef ; vardef lmt_do_axis = image ( pushparameters "axis" ; save nx; nx := getparameter "nx" ; save ny; ny := getparameter "ny" ; save dx; dx := getparameter "dx" ; save dy; dy := getparameter "dy" ; save tx; tx := getparameter "tx" ; save ty; ty := getparameter "ty" ; save c, startx, starty ; string c ; c := getparameter "axiscolor" ; startx := getparameter "startx" ; starty := getparameter "starty" ; draw (startx,starty) -- (startx,ny) withcolor c ; draw (startx,starty) -- (nx,starty) withcolor c ; for i = 0 step dx until nx : if (i > startx) or (startx = 0) : draw ((0,0) -- (0,-2)) shifted (i,starty) withcolor c ; fi ; endfor ; for i = 0 step dy until ny : if (i > starty) or (starty = 0) : draw ((0,0) -- (-2,0)) shifted (startx,i) withcolor c ; fi ; endfor ; if tx <> 0 : c := getparameter "textcolor" ; for i = 0 step tx until nx : if (i > startx) or (startx = 0) : draw textext("\strut " & decimal i) ysized 2 shifted (i,-4+starty) withcolor c; fi ; endfor ; fi ; if ty <> 0 : c := getparameter "textcolor" ; for i = 0 step ty until ny : if (i > starty) or (starty = 0) : draw textext.lft("\strut " & decimal i) ysized 2 shifted (-3+startx,i) withcolor c; fi ; endfor ; fi ; if (getparametercount "samples") > 0 : if getparameter "connect" : for s = 1 upto getparametercount "samples" : c := getparameter "samplecolors" s ; draw for i = 1 upto getparametercount "samples" s : if (i > 1) : -- fi (i, getparameter "samples" s i) endfor withcolor c ; endfor ; else : for s = 1 upto getparametercount "samples" : c := getparameter "samplecolors" s ; for i = 1 upto getparametercount "samples" s : draw (i, getparameter "samples" s i) withcolor c ; endfor ; endfor ; fi ; fi ; if (getparametercount "list") > 0 : path p ; numeric ts ; pair a, d ; ts := (getparameter "sy") / 20 ; pushparameters "list" ; for s = 1 upto getparametercount : pushparameters s ; c := getparameter "color" ; % p := for i = 1 upto getparametercount "points": % if (i > 1) : -- fi (getparameter "points" i) % endfor % if (getparameterdefault "close" false) : -- cycle fi ; % this can become: % p := if (getparameterdefault "close" false) : % % getparameterpath "points" "--" true ; % getparameterpath "points" true ; % else : % % getparameterpath "points" "--" false ; % getparameterpath "points" ; % fi ; % p := getparameterpath "points" if (getparameterdefault "close" false) : true fi ; p := getparameterpath "points" (getparameterdefault "close" false) ; % p := getparameterpath "points" getparameterdefault "close" false ; draw p withcolor c ; pushparameters "labels" ; if (getparametercount) > 0 : for i = 1 upto getparametercount: n := i - 1 ; a := point n of p ; d := direction n of p ; draw textext(getparametertext i true) ysized ts shifted (a + .5 * unitvector(d) rotated 90) ; endfor ; fi ; popparameters ; pushparameters "texts" ; if (getparametercount) > 0 : for i = 1 upto getparametercount : n := i + 0.5 ; a := point n of p ; d := direction n of p ; draw textext.d(getparametertext i true) if d < left : rotated 180 shifted (0,-5) else : shifted (0,5) fi ysized ts shifted a rotatedaround(a,angle(d)) ; endfor ; fi ; popparameters ; popparameters ; endfor ; popparameters ; fi ; popparameters ; ) xyscaled(getparameter "axis" "sx",getparameter "axis" "sy") enddef ; presetparameters "outline" [ content = "", kind = "draw", fillcolor = "", drawcolor = "", rulethickness = 1/10, align = "", style = "", width = 0, ] ; def lmt_outline = applyparameters "outline" "lmt_do_outline" enddef ; vardef lmt_do_outline = image ( normaldraw image ( save kind ; string kind ; kind := getparameter "outline" "kind" ; save align ; string align ; align := getparameter "outline" "align" ; save style ; string style ; style := getparameter "outline" "style" ; save width ; numeric width ; width := getparameter "outline" "width" ; if kind = "draw" : kind := "d" ; elseif kind = "fill" : kind := "f" ; elseif kind = "both" : kind := "b" ; elseif kind = "reverse" : kind := "r" ; fi ; currentoutlinetext := currentoutlinetext + 1 ; lua.mp.mf_outline_text( currentoutlinetext, if align = "" : getparameter "outline" "content", else : "\framed[align={" & align & "}" if width > 0 : & ",width=" & decimal width & "bp" fi if style <> "" : & ",foregroundstyle=" & style fi & ",offset=none,frame=off]{" & (getparameter "outline" "content") & "}", fi, kind ) ; save currentpen; pen currentpen ; pickup pencircle scaled getparameter "outline" "rulethickness" ; if kind = "f" : mfun_do_outline_text_set_f ( withcolor getparameter "outline" "fillcolor" ); elseif kind = "d" : mfun_do_outline_text_set_d ( withcolor getparameter "outline" "drawcolor" ); elseif kind = "b" : mfun_do_outline_text_set_b ( withcolor getparameter "outline" "fillcolor" ) ( withcolor getparameter "outline" "drawcolor" ); elseif kind = "u" : mfun_do_outline_text_set_f ( withcolor getparameter "outline" "fillcolor" ); elseif kind = "r" : mfun_do_outline_text_set_r ( withcolor getparameter "outline" "drawcolor" ) ( withcolor getparameter "outline" "fillcolor" ) ; elseif kind = "p" : mfun_do_outline_text_set_p ; else : mfun_do_outline_text_set_n ( % what to use here ); fi ; lua.mp.mf_get_outline_text(currentoutlinetext) ; ) ) enddef ; presetparameters "followtext" [ content = "", spread = true, trace = false, reverse = false, autoscaleup = "no", autoscaledown = "no", path = (fullcircle), ] ; def lmt_followtext = applyparameters "followtext" "lmt_do_followtext" enddef ; vardef lmt_do_followtext = image ( save s_u ; string s_u ; s_u := getparameter "followtext" "autoscaleup" ; save s_d ; string s_d ; s_d := getparameter "followtext" "autoscaledown" ; save followtextalternative ; followtextalternative := if getparameter "followtext" "spread" : 1 else : 0 fi ; save tracingfollowtext ; tracingfollowtext := if getparameter "followtext" "trace" : 1 else : 0 fi ; save autoscaleupfollowtext ; autoscaleupfollowtext := if s_u = "yes" : 1 elseif s_u = "max" : 2 else : 0 fi ; save autoscaledownfollowtext ; autoscaledownfollowtext := if s_d = "yes" : 1 elseif s_d = "max" : 2 else : 0 fi ; draw followtext ( if (getparameter "followtext" "reverse") : reverse fi getparameter "followtext" "path", getparameter "followtext" "content" ) ; ) enddef ; presetparameters "arrow" [ path = origin, kind = "fill", dimple = 1/5, scale = 3/4, length = 4, angle = 45, location = "end", % middle both alternative = "normal", % dimpled curved percentage = 50, headonly = false, ] ; def lmt_arrow = applyparameters "arrow" "lmt_do_arrow" enddef ; vardef lmt_do_arrow = image ( save a ; string a ; a := getparameter "arrow" "alternative" ; save l ; string l ; l := getparameter "arrow" "location" ; save k ; string k ; k := getparameter "arrow" "kind" ; save p ; path p ; p := getparameter "arrow" "path" ; save ahvariant ; ahvariant := if a = "dimpled" : 1 elseif a = "curved" : 2 else : 0 fi ; save ahdimple ; ahdimple := getparameter "arrow" "dimple" ; save ahscale ; ahscale := getparameter "arrow" "scale" ; save ahangle ; ahangle := getparameter "arrow" "angle" ; save ahlength ; ahlength := getparameter "arrow" "length" ; if not getparameter "arrow" "headonly" : draw p ; fi ; if k = "draw" : draw elseif k = "both" : filldraw else : fill fi if l = "middle" : midarrowhead p ; elseif l = "percentage" : arrowheadonpath (p, (getparameter "arrow" "percentage")/100) ; elseif l = "both" : arrowhead p ; if k = "draw" : draw elseif k = "both" : filldraw else : fill fi arrowhead reverse p ; else : arrowhead p ; fi ; ) enddef ; % from dum presetparameters "placeholder" [ color = "red", width = 1, height = 1, reduction = 0, alternative = "circle", ] ; def lmt_placeholder = applyparameters "placeholder" "lmt_do_placeholder" enddef ; def lmt_do_placeholder = begingroup ; save w, h, d, r, p, c, b, s, q, a ; numeric w, h, d, r ; path p ; string s, a ; s := getparameter "placeholder" "color" ; w := getparameter "placeholder" "width" ; h := getparameter "placeholder" "height" ; r := getparameter "placeholder" "reduction" ; a := getparameter "placeholder" "alternative" ; d := max(w,h) ; if cmykcolor resolvedcolor(s) : cmykcolor c, b ; b := (0,0,0,0) else : color c, b ; b := (1,1,1) fi ; c := resolvedcolor(s) ; p := unitsquare xyscaled (w,h) ; fill p withcolor r[.5c,b] ; if a = "square" : vardef q = fullsquare enddef ; elseif a = "triangle" : vardef q = fulltriangle rotated (90 * round(uniformdeviate(4))) enddef ; else : vardef q = fullcircle enddef ; fi ; for i := 1 upto 60 : fill q scaled (d/5 randomized (d/5)) shifted (center p randomized (d)) withcolor r[c randomized(.3,.9),b] ; endfor ; clip currentpicture to p ; endgroup ; enddef ; % maybe: vardef lmt_connected(text t) = save p ; path p ; p := origin t ; subpath (1,length(p)) of p enddef; def lmt_connection expr t = -- t enddef;