1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
%D \module
%D [ file=mp-luas.mpiv,
%D version=2014.04.14,
%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.
if known context_luas : endinput ; fi ;
% When I prototyped the runscript primitive I was just thinking of a usage like
% the original \directlua primitive in luatex: genererate something and pipe
% that back to metapost, and have access to some internals. Instead of compiling
% the code a the metapost end here we delegate that to the lua end. Only strings
% get passed. Of course in the end the real usage got a bit beyong the intended
% usage. So, in addition to some definitions here there are and will be use in
% other metafun modules too. Of course in retrospect I should have done this five
% years earlier.
boolean context_luas ; context_luas := true ;
% First variant:
%
% let lua = runscript ;
%
% Second variant:
%
% vardef lua (text t) =
% runscript(for s = t : s & endfor "")
% enddef;
%
% Third variant:
%
% vardef lua (text t) =
% runscript("" for s = t :
% if string s :
% & s
% elseif numeric s :
% & decimal s
% elseif boolean s :
% & if s : "true" else "false" fi
% fi endfor)
% enddef;
%
% Fourth variant:
string mfun_lua_bs ; mfun_lua_bs := "[===[" ;
string mfun_lua_es ; mfun_lua_es := "]===]" ;
vardef mlib_luas_luacall(text t) =
runscript("" for s = t :
if string s :
& s
% & mfun_lua_bs & s & mfun_lua_es
elseif numeric s :
& decimal s
elseif boolean s :
& if s : "true" else : "false" fi
elseif pair s :
& mfun_pair_to_table(s)
elseif path s :
& mfun_path_to_table(s)
elseif rgbcolor s :
& mfun_rgb_to_table(s)
elseif cmykcolor s :
& mfun_cmyk_to_table(s)
else :
& ditto & tostring(s) & ditto
fi endfor
)
enddef ;
vardef mlib_luas_lualist(expr c)(text t) =
save b ; boolean b ; b := false ;
runscript(c & "(" for s = t :
if b :
& ","
else :
hide(b := true)
fi
if string s :
% & ditto & s & ditto
& mfun_lua_bs & s & mfun_lua_es
elseif numeric s :
& decimal s
elseif boolean s :
& if s : "true" else : "false" fi
elseif pair s :
& mfun_pair_to_table(s)
elseif path s :
& mfun_path_to_table(s)
elseif rgbcolor s :
& mfun_rgb_to_table(s)
elseif cmykcolor s :
& mfun_cmyk_to_table(s)
else :
& ditto & tostring(s) & ditto
fi endfor & ")"
)
enddef ;
def luacall = mlib_luas_luacall enddef ; % why no let
vardef lualist@#(text t) = mlib_luas_lualist(str @#)(t) enddef ;
string mlib_luas_s ; % saves save/restore
vardef lua@#(text t) =
mlib_luas_s := str @# ;
if length(mlib_luas_s) > 0 :
mlib_luas_lualist(mlib_luas_s,t)
else :
mlib_luas_luacall(t)
fi
enddef ;
vardef MP@#(text t) =
mlib_luas_lualist("MP." & str @#,t)
enddef ;
def message expr t =
if t <> "" : lua.mp.report(t) fi ;
enddef ;
% A few helpers
vardef isarray suffix a = lua.mp.isarray (str a) enddef ;
vardef prefix suffix a = lua.mp.prefix (str a) enddef ;
vardef dimension suffix a = lua.mp.dimension(str a) enddef ;
% More access
def getdimen(expr k) = lua.mp._get_dimen_(k) enddef ;
def getcount(expr k) = lua.mp._get_count_(k) enddef ;
def gettoks (expr k) = lua.mp._get_toks_ (k) enddef ;
def setdimen(expr k, v) = lua.mp._set_dimen_(k,v) enddef ;
def setcount(expr k, v) = lua.mp._set_count_(k,v) enddef ;
def settoks (expr k, v) = lua.mp._set_toks_ (k,v) enddef ;
% vardef getdimen(expr k) = save getdimen ; lua.mp.getdimen(k) enddef ;
% vardef getcount(expr k) = save getcount ; lua.mp.getcount(k) enddef ;
% vardef gettoks (expr k) = save gettoks ; lua.mp.gettoks (k) enddef ;
% vardef setdimen(expr k,v) = save setdimen ; lua.mp.setdimen(k,v) enddef ;
% vardef setcount(expr k,v) = save setcount ; lua.mp.setcount(k,v) enddef ;
% vardef settoks (expr k,v) = save settoks ; lua.mp.settoks (k,v) enddef ;
vardef positionpath (expr name) = lua.mp.positionpath (name) enddef ;
vardef positioncurve (expr name) = lua.mp.positioncurve (name) enddef ;
vardef positionxy (expr name) = lua.mp.positionxy (name) enddef ;
vardef positionpxy (expr name) = lua.mp.positionpxy (name) enddef ;
vardef positionwhd (expr name) = lua.mp.positionwhd (name) enddef ;
vardef positionpage (expr name) = lua.mp.positionpage (name) enddef ;
vardef positionregion(expr name) = lua.mp.positionregion(name) enddef ;
vardef positionbox (expr name) = lua.mp.positionbox (name) enddef ;
vardef positionanchor = lua.mp.positionanchor() enddef ;
let wdpart = redpart ;
let htpart = greenpart ;
let dppart = bluepart ;
vardef positioninregion =
currentpicture := currentpicture shifted - positionxy(positionanchor) ;
enddef ;
vardef positionatanchor(expr name) =
currentpicture := currentpicture shifted - positionxy(name) ;
enddef ;
vardef texvar(expr name) = lua.mp.texvar(name) enddef ;
vardef texstr(expr name) = lua.mp.texstr(name) enddef ;
%D New experimental feature for Alan-The-Number-Cruncher:
% \startMPcode{doublefun}
% path p ; p := (
% for i=1 upto 20000 :
% origin randomized 10000 ..
% endfor cycle
% ) xysized (TextWidth,TextHeight) ;
% draw for i inpath p:
% pointof i .. controls (leftof i) and (rightof i) ..
% endfor cycle ;
% \stopMPcode
def inpath suffix p =
= 1 step 1 until lua.mp.mfun_path_length(str p)
enddef ;
vardef pointof primary i = lua.mp.mfun_path_point(i) enddef ;
vardef leftof primary i = lua.mp.mfun_path_left (i) enddef ;
vardef rightof primary i = lua.mp.mfun_path_right(i) enddef ;
extra_endfig := extra_endfig & " lua.mp.mfun_path_reset() ; " ;
|