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 14
if
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 25
boolean
context_luas
;
context_luas
:
=
true
;
26 27
string
mfun_lua_bs
;
mfun_lua_bs
:
=
"
[===[
"
;
28
string
mfun_lua_es
;
mfun_lua_es
:
=
"
]===]
"
;
29 30
vardef
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
)
51
enddef
;
52 53
newinternal
mfun_luas_b
;
54 55
def
mlib_luas_luadone
=
56
exitif
numeric
begingroup
mfun_luas_b
:
=
1
;
endgroup
;
57
enddef
;
58 59
vardef
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
)
88
enddef
;
89 90
def
luacall
=
mlib_luas_luacall
enddef
;
% why no let
91 92
vardef
lualist
@#
(
text
t
)
=
mlib_luas_lualist
(
str
@#
)
(
t
)
enddef
;
93 94
string
mlib_luas_s
;
% saves save/restore
95 96
vardef
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
103
enddef
;
104 105
vardef
MP
@#
(
text
t
)
=
106
mlib_luas_lualist
(
"
MP.
"
&
str
@#
,
t
)
107
enddef
;
108 109
def
message
expr
t
=
110
lua.mp.report
(
tostring
(
t
)
)
;
111
enddef
;
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 121
def
resolvedcolor
primary
s
=
% no vardef
122
if
string
s
:
123
runscript
(
"
mp.mf_named_color('
"
&
s
&
"
')
"
)
% faster anyway
124
else
:
125
s
126
fi
127
enddef
;
128 129
% Modes:
130 131
vardef
texmode
(
expr
s
)
=
lua.mp
(
"
mode
"
,
s
)
enddef
;
132
vardef
systemmode
(
expr
s
)
=
lua.mp
(
"
systemmode
"
,
s
)
enddef
;
133 134
% A few helpers
135 136
vardef
isarray
suffix
a
=
lua.mp.isarray
(
str
a
)
enddef
;
137
vardef
prefix
suffix
a
=
lua.mp.prefix
(
str
a
)
enddef
;
138
vardef
dimension
suffix
a
=
lua.mp.dimension
(
str
a
)
enddef
;
139 140
% More access
141 142
vardef
getmacro
(
expr
k
)
=
lua.mp._get_macro_
(
k
)
enddef
;
143
vardef
getdimen
(
expr
k
)
=
lua.mp._get_dimen_
(
k
)
enddef
;
144
vardef
getcount
(
expr
k
)
=
lua.mp._get_count_
(
k
)
enddef
;
145
vardef
gettoks
(
expr
k
)
=
lua.mp._get_toks_
(
k
)
enddef
;
146 147
def
setmacro
(
expr
k
,
v
)
=
lua.mp._set_macro_
(
k
,
v
)
enddef
;
148
def
setdimen
(
expr
k
,
v
)
=
lua.mp._set_dimen_
(
k
,
v
)
enddef
;
149
def
setcount
(
expr
k
,
v
)
=
lua.mp._set_count_
(
k
,
v
)
enddef
;
150
def
settoks
(
expr
k
,
v
)
=
lua.mp._set_toks_
(
k
,
v
)
enddef
;
151 152
def
setglobalmacro
(
expr
k
,
v
)
=
lua.mp._set_global_macro_
(
k
,
v
)
enddef
;
153
def
setglobaldimen
(
expr
k
,
v
)
=
lua.mp._set_global_dimen_
(
k
,
v
)
enddef
;
154
def
setglobalcount
(
expr
k
,
v
)
=
lua.mp._set_global_count_
(
k
,
v
)
enddef
;
155
def
setglobaltoks
(
expr
k
,
v
)
=
lua.mp._set_global_toks_
(
k
,
v
)
enddef
;
156 157
vardef
positionpath
(
expr
name
)
=
lua.mp.positionpath
(
name
)
enddef
;
158
vardef
positioncurve
(
expr
name
)
=
lua.mp.positioncurve
(
name
)
enddef
;
159
vardef
positionxy
(
expr
name
)
=
lua.mp.positionxy
(
name
)
enddef
;
160
vardef
positionpxy
(
expr
name
)
=
lua.mp.positionpxy
(
name
)
enddef
;
161
vardef
positionwhd
(
expr
name
)
=
lua.mp.positionwhd
(
name
)
enddef
;
162
vardef
positionpage
(
expr
name
)
=
lua.mp.positionpage
(
name
)
enddef
;
163
vardef
positionregion
(
expr
name
)
=
lua.mp.positionregion
(
name
)
enddef
;
164
vardef
positionbox
(
expr
name
)
=
lua.mp.positionbox
(
name
)
enddef
;
165
vardef
positionanchor
=
lua.mp.positionanchor
(
)
enddef
;
166 167
let
wdpart
=
redpart
;
168
let
htpart
=
greenpart
;
169
let
dppart
=
bluepart
;
170 171
vardef
positioninregion
=
172
currentpicture
:
=
currentpicture
shifted
-
positionxy
(
positionanchor
)
;
173
enddef
;
174 175
vardef
positionatanchor
(
expr
name
)
=
176
currentpicture
:
=
currentpicture
shifted
-
positionxy
(
name
)
;
177
enddef
;
178 179
vardef
texvar
(
expr
name
)
=
lua.mp.texvar
(
name
)
enddef
;
180
vardef
texstr
(
expr
name
)
=
lua.mp.texstr
(
name
)
enddef
;
181 182
%D New experimental feature for Alan-The-Number-Cruncher:
183 184
def
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
192
enddef
;
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 198
vardef
pointof
primary
i
=
runscript
(
"
mp.mf_path_point(
"
&
decimal
i
&
"
)
"
)
enddef
;
199
vardef
leftof
primary
i
=
runscript
(
"
mp.mf_path_left(
"
&
decimal
i
&
"
)
"
)
enddef
;
200
vardef
rightof
primary
i
=
runscript
(
"
mp.mf_path_right(
"
&
decimal
i
&
"
)
"
)
enddef
;
201 202
extra_endfig
:
=
extra_endfig
&
"
lua.mp.mf_path_reset() ;
"
;
203 204
vardef
utflen
(
expr
s
)
=
lua.mp.utflen
(
s
)
enddef
;
205
vardef
utfsub
(
expr
s
,
f
,
t
)
=
lua.mp.utfsub
(
s
,
f
,
t
)
enddef
;
206