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
199
200
201
202
203
204
205
206
207
208
209
210
|
if not modules then modules = { } end modules ['mlib-lua'] = {
version = 1.001,
comment = "companion to mlib-ctx.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files",
}
local type = type
local insert, remove = table.insert, table.remove
local trace = false trackers.register("metapost.instance",function(v) trace = v end)
local report = logs.reporter("metapost","instance")
local codes = mplib.getcodes()
local types = mplib.gettypes()
-- for k,v in next, mplib do if type(v) == "function" then local f = v mplib[k] = function(...) print(k) return v(...) end end end
table.hashed(codes)
table.hashed(types)
metapost.codes = codes
metapost.types = types
local scan = mp.scan
local skip = mp.skip
local get = mp.get
local inject = mp.inject
local currentmpx = nil
local stack = { }
local function reports(s) report("%a scan %s", tostring(currentmpx),s) end -- temporary, till we're okay
local function reporti(s) report("%a inject %s",tostring(currentmpx),s) end -- temporary, till we're okay
local scan_next = mplib.scan_next
local scan_expression = mplib.scan_expression
local scan_token = mplib.scan_token
local scan_symbol = mplib.scan_symbol
local scan_property = mplib.scan_property
local scan_numeric = mplib.scan_numeric
local scan_integer = mplib.scan_integer
local scan_boolean = mplib.scan_boolean
local scan_string = mplib.scan_string
local scan_pair = mplib.scan_pair
local scan_color = mplib.scan_color
local scan_cmykcolor = mplib.scan_cmykcolor
local scan_transform = mplib.scan_transform
local scan_path = mplib.scan_path
local scan_pen = mplib.scan_pen
local skip_token = mplib.skip_token
local get_hashentry = mplib.gethashentry
scan.next = function(k) if trace then reporti("next") end return scan_next (currentmpx,k) end
scan.expression = function(k) if trace then reporti("expression") end return scan_expression(currentmpx,k) end
scan.token = function(k) if trace then reporti("token") end return scan_token (currentmpx,k) end
scan.symbol = function(k,e) if trace then reporti("symbol") end return scan_symbol (currentmpx,k,e) end
scan.property = function(k) if trace then reporti("property") end return scan_property (currentmpx,k) end
scan.numeric = function() if trace then reporti("numeric") end return scan_numeric (currentmpx) end
scan.integer = function() if trace then reporti("integer") end return scan_integer (currentmpx) end
scan.boolean = function() if trace then reporti("boolean") end return scan_boolean (currentmpx) end
scan.string = function() if trace then reporti("string") end return scan_string (currentmpx) end
scan.pair = function(t) if trace then reporti("pair") end return scan_pair (currentmpx,t) end
scan.color = function(t) if trace then reporti("color") end return scan_color (currentmpx,t) end
scan.cmykcolor = function(t) if trace then reporti("cmykcolor") end return scan_cmykcolor (currentmpx,t) end
scan.transform = function(t) if trace then reporti("transform") end return scan_transform (currentmpx,t) end
scan.path = function(t) if trace then reporti("path") end return scan_path (currentmpx,t) end
scan.pen = function(t) if trace then reporti("pen") end return scan_pen (currentmpx,t) end
skip.token = function(t) return skip_token (currentmpx,t) end
get.hashentry = function(n) return get_hashentry (currentmpx,n) end
local solvepath = mplib.solvepath
local getstatus = mplib.getstatus
mp.solve = function(...)
return solvepath(currentmpx,...)
end
local inject_path = mplib.inject_path
local inject_numeric = mplib.inject_numeric
local inject_pair = mplib.inject_pair
local inject_boolean = mplib.inject_boolean
local inject_integer = mplib.inject_integer
local inject_string = mplib.inject_string
local inject_color = mplib.inject_color
local inject_cmykcolor = mplib.inject_cmykcolor
local inject_transform = mplib.inject_transform
local inject_whatever = mplib.inject_whatever
------.path = function(t,cycle,curled) if trace then reporti("path") end return inject_path (currentmpx,t,cycle,curled) end
inject.numeric = function(n) if trace then reporti("numeric") end return inject_numeric (currentmpx,n) end
inject.pair = function(x,y) if trace then reporti("pair") end return inject_pair (currentmpx,x,y) end
inject.boolean = function(b) if trace then reporti("boolean") end return inject_boolean (currentmpx,b) end
inject.integer = function(i) if trace then reporti("integer") end return inject_integer (currentmpx,i) end
inject.string = function(s) if trace then reporti("string") end return inject_string (currentmpx,s) end
inject.color = function(r,g,b) if trace then reporti("color") end return inject_color (currentmpx,r,g,b) end
inject.cmykcolor = function(c,m,y,k) if trace then reporti("cmykcolor") end return inject_cmykcolor(currentmpx,c,m,y,k) end
inject.transform = function(x,y,xx,xy,yx,yy) if trace then reporti("transform") end return inject_transform(currentmpx,x,y,xx,xy,yx,yy) end
inject.whatever = function(...) if trace then reporti("whatever") end return inject_whatever (currentmpx,...) end
inject.triplet = inject.color
inject.quadruplet = inject.cmykcolor
local function same(p,n)
local f = p[1]
local l = p[n]
local nf = #f
local nl = #l
if nf == nl then
for i=1,nf do
if f[i] ~= l[i] then
return false
end
end
return true
end
return false
end
-- local p = mp.scan.path()
-- mp.inject.path(p,true,true)
function inject.path(p,close,connector)
local curled = false
local n = #p
if p.close or p.cycle then
close = true
end
if n > 1 then
-- [ ../true | --/false | nil/auto ]
if connector == nil or connector == "auto" then
connector = #p[1] > 2
end
if connector == false or connector == "--" then
curled = true
elseif connector == true or connector == ".." then
if close and not same(p,n) then
p[n+1] = p[1]
end
end
end
if trace then reporti("path") end
return inject_path(currentmpx,p,close,curled)
end
-- bonus:
scan .number = scan .numeric
inject.number = inject.numeric
table.setmetatablecall(inject,function(t,...)
inject_whatever(currentmpx,...)
end)
-- experiment
function mp.autoinject(m)
local t = type(m)
if t == "table" then
local n = #t
if n == 2 then
inject_pair(currentmpx,m)
elseif n == 3 then
inject_color(currentmpx,m)
elseif n == 4 then
inject_cmykcolor(currentmpx,m)
elseif n == 6 then
inject_transform(currentmpx,m)
end
elseif t == "number" then
inject_numeric(currentmpx,m)
elseif t == "string" then
inject_string(currentmpx,m)
elseif t == "boolean" then
inject_boolean(currentmpx,m)
end
end
function metapost.pushscriptrunner(mpx)
if trace then
report("%a => %a",tostring(currentmpx),tostring(mpx))
end
insert(stack,currentmpx)
currentmpx = mpx
end
function metapost.popscriptrunner()
local mpx = remove(stack)
if trace then
report("%a <= %a",tostring(mpx),tostring(currentmpx))
end
currentmpx = mpx
end
function metapost.currentmpx()
return currentmpx
end
-- mplib.getstates(): zero is "normal"
function metapost.currentmpxstatus()
return getstatus(currentmpx) or 0
end
|