mp-luas.mpiv /size: 6824 b    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=mp-luas.mpiv,
3%D        version=2014.04.14,
4%D          title=\CONTEXT\ \METAPOST\ graphics,
5%D       subtitle=\LUA,
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
14if known context_luas : endinput ; fi ;
15
16% When I prototyped the runscript primitive I was just thinking of a usage like
17% the original \directlua primitive in luatex: genererate something and pipe
18% that back to metapost, and have access to some internals. Instead of compiling
19% the code a the metapost end here we delegate that to the lua end. Only strings
20% get passed. Of course in the end the real usage got a bit beyong the intended
21% usage. So, in addition to some definitions here there are and will be use in
22% other metafun modules too. Of course in retrospect I should have done this five
23% years earlier. Older version sof this module show the way it evolved.
24
25boolean context_luas ; context_luas := true ;
26
27string mfun_lua_bs ; mfun_lua_bs := "[===[" ;
28string mfun_lua_es ; mfun_lua_es := "]===]" ;
29
30vardef mlib_luas_luacall(text t) =
31    runscript("" for s = t :
32        if string s :
33            & s
34          % & mfun_lua_bs & s & mfun_lua_es
35        elseif numeric s :
36            & decimal s
37        elseif boolean s :
38            & if s : "true" else : "false" fi
39        elseif pair s :
40            & mfun_pair_to_table(s)
41        elseif path s :
42            & mfun_path_to_table(s)
43        elseif rgbcolor s :
44            & mfun_rgb_to_table(s)
45        elseif cmykcolor s :
46            & mfun_cmyk_to_table(s)
47        else :
48            & ditto & tostring(s) & ditto
49        fi endfor
50    )
51enddef ;
52
53newinternal mfun_luas_b ;
54
55def mlib_luas_luadone =
56    exitif numeric begingroup mfun_luas_b := 1 ; endgroup ;
57enddef ;
58
59vardef mlib_luas_lualist(expr c)(text t) = % we could use mlib_luas_s instead of c
60    interim mfun_luas_b := 0 ;
61    runscript(c & for s = t :
62        if mfun_luas_b = 0 :
63            "("
64          % hide(mfun_luas_b := 1)
65            mlib_luas_luadone
66        else :
67            ","
68        fi
69        &
70        if string s :
71            mfun_lua_bs & s & mfun_lua_es
72        elseif numeric s :
73            decimal s
74        elseif boolean s :
75            if s : "true" else : "false" fi
76        elseif pair s :
77            mfun_pair_to_table(s)
78        elseif path s :
79            mfun_path_to_table(s)
80        elseif rgbcolor s :
81            mfun_rgb_to_table(s)
82        elseif cmykcolor s :
83            mfun_cmyk_to_table(s)
84        else :
85            ditto & tostring(s) & ditto
86        fi & endfor if mfun_luas_b = 0 : "()" else : ")" fi
87    )
88enddef ;
89
90def luacall = mlib_luas_luacall enddef ; % why no let
91
92vardef lualist@#(text t) = mlib_luas_lualist(str @#)(t) enddef ;
93
94string mlib_luas_s ; % saves save/restore
95
96vardef lua@#(text t) =
97    mlib_luas_s := str @# ;
98    if length(mlib_luas_s) > 0 :
99        mlib_luas_lualist(mlib_luas_s,t)
100    else :
101        mlib_luas_luacall(t)
102    fi
103enddef ;
104
105vardef MP@#(text t) =
106    mlib_luas_lualist("MP." & str @#,t)
107enddef ;
108
109def message expr t =
110    lua.mp.report(tostring(t)) ;
111enddef ;
112
113% Color:
114
115% We do a low level runscript:
116%
117% lua.mp.namedcolor(s)       % conflicts with macro namedcolor
118% lua.mp.mf_named_color(s)   % okay but, can also be
119% lua.mp("mf_named_color",s) % which gives expansion mess
120
121def resolvedcolor primary s = % no vardef
122    if string s :
123        runscript("mp.mf_named_color('" & s & "')") % faster anyway
124    else :
125        s
126    fi
127enddef ;
128
129% Modes:
130
131vardef texmode   (expr s) = lua.mp("mode",      s) enddef ;
132vardef systemmode(expr s) = lua.mp("systemmode",s) enddef ;
133
134% A few helpers
135
136vardef isarray   suffix a = lua.mp.isarray  (str a) enddef ;
137vardef prefix    suffix a = lua.mp.prefix   (str a) enddef ;
138vardef dimension suffix a = lua.mp.dimension(str a) enddef ;
139
140% More access
141
142vardef getmacro(expr k)   = lua.mp._get_macro_(k)   enddef ;
143vardef getdimen(expr k)   = lua.mp._get_dimen_(k)   enddef ;
144vardef getcount(expr k)   = lua.mp._get_count_(k)   enddef ;
145vardef gettoks (expr k)   = lua.mp._get_toks_ (k)   enddef ;
146
147def setmacro(expr k,v) = lua.mp._set_macro_(k,v) enddef ;
148def setdimen(expr k,v) = lua.mp._set_dimen_(k,v) enddef ;
149def setcount(expr k,v) = lua.mp._set_count_(k,v) enddef ;
150def settoks (expr k,v) = lua.mp._set_toks_ (k,v) enddef ;
151
152def setglobalmacro(expr k,v) = lua.mp._set_global_macro_(k,v) enddef ;
153def setglobaldimen(expr k,v) = lua.mp._set_global_dimen_(k,v) enddef ;
154def setglobalcount(expr k,v) = lua.mp._set_global_count_(k,v) enddef ;
155def setglobaltoks (expr k,v) = lua.mp._set_global_toks_ (k,v) enddef ;
156
157vardef positionpath  (expr name) = lua.mp.positionpath  (name) enddef ;
158vardef positioncurve (expr name) = lua.mp.positioncurve (name) enddef ;
159vardef positionxy    (expr name) = lua.mp.positionxy    (name) enddef ;
160vardef positionpxy   (expr name) = lua.mp.positionpxy   (name) enddef ;
161vardef positionwhd   (expr name) = lua.mp.positionwhd   (name) enddef ;
162vardef positionpage  (expr name) = lua.mp.positionpage  (name) enddef ;
163vardef positionregion(expr name) = lua.mp.positionregion(name) enddef ;
164vardef positionbox   (expr name) = lua.mp.positionbox   (name) enddef ;
165vardef positionanchor            = lua.mp.positionanchor()     enddef ;
166
167let wdpart = redpart ;
168let htpart = greenpart ;
169let dppart = bluepart ;
170
171vardef positioninregion =
172    currentpicture := currentpicture shifted - positionxy(positionanchor) ;
173enddef ;
174
175vardef positionatanchor(expr name) =
176    currentpicture := currentpicture shifted - positionxy(name) ;
177enddef ;
178
179vardef texvar(expr name) = lua.mp.texvar(name) enddef ;
180vardef texstr(expr name) = lua.mp.texstr(name) enddef ;
181
182%D New experimental feature for Alan-The-Number-Cruncher:
183
184def inpath suffix p = % permits p[0]
185    = 1 step 1 until
186    begingroup
187    save mfun_inpath_r,mfun_inpath_n ; path mfun_inpath_r ;
188    mfun_inpath_r = p ;
189    mfun_inpath_n := lua.mp.mf_path_length(str mfun_inpath_r) ;
190    if mfun_inpath_n = 0 : 1 else : mfun_inpath_n fi
191    endgroup
192enddef ;
193
194% vardef pointof primary i = lua.mp.mf_path_point(i) enddef ;
195% vardef leftof  primary i = lua.mp.mf_path_left (i) enddef ;
196% vardef rightof primary i = lua.mp.mf_path_right(i) enddef ;
197
198vardef pointof primary i = runscript("mp.mf_path_point(" & decimal i & ")") enddef ;
199vardef leftof  primary i = runscript("mp.mf_path_left("  & decimal i & ")") enddef ;
200vardef rightof primary i = runscript("mp.mf_path_right(" & decimal i & ")") enddef ;
201
202extra_endfig := extra_endfig & " lua.mp.mf_path_reset() ; " ;
203
204vardef utflen(expr s)     = lua.mp.utflen(s)     enddef ;
205vardef utfsub(expr s,f,t) = lua.mp.utfsub(s,f,t) enddef ;
206