1if 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
10
11local type = type
12
13local aux = mp . aux
14local mpdirect = aux . direct
15local mppath = mp . path
16
17local scan = mp . scan
18local scannumeric = scan . numeric
19local scanpath = scan . path
20
21local getparameter = metapost . getparameter
22
23function mp . lmt_function_x ( xmin , xmax , xstep , code , shape )
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 )
60end
61
62function mp . lmt_mesh_set ( )
63 local mesh = getparameter { " mesh " , " paths " }
64 structures . references . currentset . mesh = mesh
65end
66
67function mp . lmt_mesh_update ( )
68 local mesh = getparameter { " paths " } or getparameter { " mesh " , " paths " }
69 mesh [ scannumeric ( ) ] = scanpath ( true )
70end
71
72
73
74function mp . lmt_svg_include ( )
75 local labelfile = metapost . getparameter { " labelfile " }
76 if labelfile and labelfile ~ = " " then
77 local labels = table . load ( labelfile )
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
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
127end
128
129
130function mp . lmt_do_remaptext ( )
131 local parameters = metapost . scanparameters ( )
132 if parameters and parameters . label then
133 metapost . remaptext ( parameters )
134 end
135end
136
137do
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
151end
152
153todecimal = xdecimal and xdecimal . new or tonumber
154 |