mlib-lmt.lua /size: 4709 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
mlib-lmt
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to mlib-ctx.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
,
7
}
8 9
-- todo: check for possible inject usage
10 11
local
type
=
type
12 13
local
aux
=
mp
.
aux
14
local
mpdirect
=
aux
.
direct
15
local
mppath
=
mp
.
path
16 17
local
scan
=
mp
.
scan
18
local
scannumeric
=
scan
.
numeric
19
local
scanpath
=
scan
.
path
20 21
local
getparameter
=
metapost
.
getparameter
22 23
function
mp
.
lmt_function_x
(
xmin
,
xmax
,
xstep
,
code
,
shape
)
-- experimental
24
local
code
=
"
return function(x) return
"
.
.
code
.
.
"
end
"
25
local
action
=
load
(
code
)
26
local
points
=
{
}
27
local
nofpoints
=
0
28
if
action
then
29
action
=
action
(
)
30
end
31
if
shape
=
=
"
steps
"
then
32
local
halfx
=
xstep
/
2
33
local
lastx
=
xmin
34
local
lasty
=
action
(
xmin
)
35
for
xi
=
xmin
,
xmax
,
xstep
do
36
local
yi
=
action
(
xi
)
37
local
xx
=
lastx
+
halfx
38
nofpoints
=
nofpoints
+
1
;
points
[
nofpoints
]
=
{
xx
,
lasty
}
39
nofpoints
=
nofpoints
+
1
;
points
[
nofpoints
]
=
{
xx
,
yi
}
40
lastx
=
xi
41
lasty
=
yi
42
end
43
if
points
[
nofpoints
]
[
1
]
~
=
xmax
then
44
local
yi
=
action
(
xmax
)
45
local
xx
=
lastx
+
halfx
46
nofpoints
=
nofpoints
+
1
;
points
[
nofpoints
]
=
{
xx
,
lasty
}
47
nofpoints
=
nofpoints
+
1
;
points
[
nofpoints
]
=
{
xx
,
yi
}
48
lastx
=
xi
49
lasty
=
yi
50
end
51
else
52
for
xi
=
xmin
,
xmax
,
xstep
do
53
nofpoints
=
nofpoints
+
1
;
points
[
nofpoints
]
=
{
xi
,
action
(
xi
)
}
54
end
55
if
points
[
nofpoints
]
[
1
]
~
=
xmax
then
56
nofpoints
=
nofpoints
+
1
;
points
[
nofpoints
]
=
{
xmax
,
action
(
xmax
)
}
57
end
58
end
59
mppath
(
points
,
shape
=
=
"
curve
"
and
"
..
"
or
"
--
"
,
false
)
60
end
61 62
function
mp
.
lmt_mesh_set
(
)
63
local
mesh
=
getparameter
{
"
mesh
"
,
"
paths
"
}
64
structures
.
references
.
currentset
.
mesh
=
mesh
65
end
66 67
function
mp
.
lmt_mesh_update
(
)
68
local
mesh
=
getparameter
{
"
paths
"
}
or
getparameter
{
"
mesh
"
,
"
paths
"
}
69
mesh
[
scannumeric
(
)
]
=
scanpath
(
true
)
70
end
71 72
-- moved here
73 74
function
mp
.
lmt_svg_include
(
)
75
local
labelfile
=
metapost
.
getparameter
{
"
labelfile
"
}
76
if
labelfile
and
labelfile
~
=
"
"
then
77
local
labels
=
table
.
load
(
labelfile
)
-- todo: same path as svg file
78
if
type
(
labels
)
=
=
"
table
"
then
79
for
i
=
1
,
#
labels
do
80
metapost
.
remaptext
(
labels
[
i
]
)
81
end
82
end
83
end
84
local
fontname
=
metapost
.
getparameter
{
"
fontname
"
}
85
if
fontname
and
fontname
~
=
"
"
then
86
local
unicode
=
metapost
.
getparameter
{
"
unicode
"
}
87
if
unicode
then
88
mpdirect
(
89
metapost
.
svgglyphtomp
(
fontname
,
math
.
round
(
unicode
)
)
90
)
91
end
92
return
93
end
94
local
colorfile
=
metapost
.
getparameter
{
"
colormap
"
}
95
local
colormap
=
false
96
if
colorfile
and
colorfile
~
=
"
"
then
97
colormap
=
metapost
.
svgcolorremapper
(
colorfile
)
98
end
99
local
filename
=
metapost
.
getparameter
{
"
filename
"
}
100
if
filename
and
filename
~
=
"
"
then
101
mpdirect
(
metapost
.
svgtomp
{
102
data
=
io
.
loaddata
(
filename
)
,
103
remap
=
true
,
104
colormap
=
colormap
,
105
id
=
filename
,
106
}
)
107
else
108
local
buffer
=
metapost
.
getparameter
{
"
buffer
"
}
109
if
buffer
then
110
mpdirect
(
metapost
.
svgtomp
{
111
data
=
buffers
.
getcontent
(
buffer
)
,
112
-- remap = true,
113
colormap
=
colormap
,
114
id
=
buffer
or
"
buffer
"
,
115
}
)
116
else
117
local
code
=
metapost
.
getparameter
{
"
code
"
}
118
if
code
then
119
mpdirect
(
metapost
.
svgtomp
{
120
data
=
code
,
121
colormap
=
colormap
,
122
id
=
"
code
"
,
123
}
)
124
end
125
end
126
end
127
end
128 129 130
function
mp
.
lmt_do_remaptext
(
)
131
local
parameters
=
metapost
.
scanparameters
(
)
132
if
parameters
and
parameters
.
label
then
133
metapost
.
remaptext
(
parameters
)
134
end
135
end
136 137
do
138 139
local
dropins
=
fonts
.
dropins
140
local
registerglyphs
=
dropins
.
registerglyphs
141
local
registerglyph
=
dropins
.
registerglyph
142 143
function
mp
.
lmt_register_glyph
(
)
144
registerglyph
(
metapost
.
getparameterset
(
"
mpsglyph
"
)
)
145
end
146 147
function
mp
.
lmt_register_glyphs
(
)
148
registerglyphs
(
metapost
.
getparameterset
(
"
mpsglyphs
"
)
)
149
end
150 151
end
152 153
todecimal
=
xdecimal
and
xdecimal
.
new
or
tonumber
-- bonus
154