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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
if not modules then modules = { } end modules ['mlib-lmp'] = {
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",
}
-- path relates stuff ... todo: use a stack (or numeric index to list)
local type, tonumber, tostring = type, tonumber, tostring
local find, match = string.find, string.match
local insert, remove = table.insert, table.remove
local aux = mp.aux
local mpnumeric = aux.numeric
local mppair = aux.pair
local registerdirect = metapost.registerdirect
local registerscript = metapost.registerscript
local scan = mp.scan
local skip = mp.skip
local get = mp.get
local inject = mp.inject
local scannumber = scan.number
local scanstring = scan.string
local scaninteger = scan.integer
local scannumeric = scan.numeric
local scanwhatever = scan.whatever
local scanpath = scan.path
local scanproperty = scan.property
local gethashentry = get.hashentry
local bpfactor = number.dimenfactors.bp
local injectwhatever = inject.whatever
local injectboolean = inject.boolean
local injectnumeric = inject.numeric
local injectstring = inject.string
local injectpair = inject.pair
local injectwhd = inject.whd -- scaled
local injectxy = inject.xy
local injectpt = inject.pt
local report = logs.reporter("metapost", "log")
local report_message = logs.reporter("metapost")
local codes = metapost.codes
local types = metapost.types
local procodes = mplib.propertycodes
local implement = interfaces.implement
do
local p = nil
local n = 0
registerscript("pathreset", function()
p = nil
n = 0
end)
registerdirect("pathlengthof", function()
p = scanpath()
return p and #p or 1
end)
registerdirect("pathpointof", function()
local i = scannumber()
if i > 0 and i <= n then
local pi = p[i]
injectpair(pi[1],pi[2])
end
end)
registerdirect("pathleftof", function()
local i = scannumber()
if i > 0 and i <= n then
local pi = p[i]
injectpair(pi[5],pi[6])
end
end)
registerdirect("pathrightof", function()
local i = scannumber()
if i > 0 and i <= n then
local pn
if i == 1 then
pn = p[2] or p[1]
else
pn = p[i+1] or p[1]
end
injectpair(pn[3],pn[4])
end
end)
end
registerscript("showproperty", function()
local k, s, p, d = scanproperty()
if k then
report("name %a, property %a, command %a, detail %a",s,procodes[p] or "-",codes[k] or "-",types[d] or "-")
end
end)
registerscript("showhashentry", function()
local s = scanstring()
if s then
local k, p, d = gethashentry(s)
if k then
report("name %a, property %a, command %a, detail %a",s,procodes[p] or "-",codes[k] or "-",types[d] or "-")
end
end
end)
-- local getmacro = tokens.getters.macro
-- local mpgnamespace = getmacro("??graphicvariable")
-- registerscript("mpv_numeric", function() injectnumeric (getmacro(mpgnamespace .. getmacro("currentmpcategory") .. ":" .. scanmpstring())) end)
-- registerscript("mpv_dimension", function() return getmacro(mpgnamespace .. getmacro("currentmpcategory") .. ":" .. scanmpstring()) end)
-- registerscript("mpv_string", function() injectstring (getmacro(mpgnamespace .. getmacro("currentmpcategory") .. ":" .. scanmpstring())) end)
-- registerscript("mpvar", function() return getmacro(mpgnamespace .. getmacro("currentmpcategory") .. ":" .. scanmpstring(), true) end) -- Isn't it already edef'd?
-- registerscript("mpvar", function() return getmacro(metapost.namespace .. scanmpstring(), true) end) -- Isn't it already edef'd?
do
local expandtex = mp.expandtex
local tokenvalues = tokens.values
local dimension_value = tokenvalues.dimension
local integer_value = tokenvalues.integer
local boolean_value = tokenvalues.boolean
local string_value = tokenvalues.string
local unknown_value = tokenvalues.none
registerdirect("mpvard", function()
if not expandtex(dimension_value,"mpcategoryparameter",true,scanstring()) then
injectnumeric(0)
end
end)
registerdirect("mpvarn", function()
if not expandtex(integer_value,"mpcategoryparameter",true,scanstring()) then
injectnumeric(0)
end
end)
registerdirect("mpvars", function()
if not expandtex(string_value,"mpcategoryparameter",true,scanstring()) then
injectstring("")
end
end)
registerdirect("mpvarb", function()
if not expandtex(boolean_value,"mpcategoryparameter",true,scanstring()) then
injectboolean(false)
end
end)
registerdirect("mpvar", function()
if not expandtex(unknown_value,"mpcategoryparameter",true,scanstring()) then
injectnumeric(0)
end
end)
--
local mpprint = mp.print
local mpquoted = mp.quoted
local getmacro = tokens.getters.macro
registerscript("texvar", function() mpprint (getmacro(metapost.namespace .. scanstring())) end)
registerscript("texstr", function() mpquoted(getmacro(metapost.namespace .. scanstring())) end)
end
do
registerscript("textextanchor", function()
local x, y = match(scanstring(),"tx_anchor=(%S+) (%S+)") -- todo: make an lpeg
if x and y then
x = tonumber(x)
y = tonumber(y)
end
injectpair(x or 0,y or 0)
end)
end
do
local mpnamedcolor = attributes.colors.mpnamedcolor
local mpprint = mp.aux.print
mp.mf_named_color = function(str)
mpprint(mpnamedcolor(str))
end
-- todo: we can inject but currently we always get a string back so then
-- we need to deal with it upstream in the color module ... not now
registerscript("namedcolor",function() mpprint(mpnamedcolor(scanstring())) end)
end
do
local hashes = table.setmetatableindex("table")
registerdirect("lmt_hash_new", function()
-- local name = scanstring()
local name = scanwhatever()
hashes[name] = { }
end)
registerdirect("lmt_hash_dispose", function()
-- local name = scanstring()
local name = scanwhatever()
hashes[name] = nil
end)
registerdirect("lmt_hash_in", function()
-- local name = scanstring()
local name = scanwhatever()
-- local key = scanstring()
local key = scanwhatever()
local hash = hashes[name]
injectwhatever(hash and hash[key] and true or false)
end)
registerdirect("lmt_hash_to", function()
-- local name = scanstring()
local name = scanwhatever()
-- local key = scanstring()
local key = scanwhatever()
local value = scanwhatever()
local hash = hashes[name]
if hash then
hash[key] = value
end
end)
registerdirect("lmt_hash_from", function()
-- local name = scanstring()
local name = scanwhatever()
-- local key = scanstring()
local key = scanwhatever()
local hash = hashes[name]
injectwhatever(hash and hash[key] or false)
end)
interfaces.implement {
name = "MPfromhash",
arguments = "2 strings",
actions = function(name,key)
local hash = hashes[name] or hashes[tonumber(name)] or hashes[tostring(name)]
if hash then
local v = hash[key]
if v then
context(v)
end
end
end
}
end
do
local bpfactor = number.dimenfactors.bp
local nbdimensions = nodes.boxes.dimensions
registerdirect("boxdimensions", function()
local category = scanstring()
local index = scanwhatever()
injectwhd(nbdimensions(category,index))
end)
end
do
local skiptoken = skip.token
local comma_code = codes.comma
local getmacro = tokens.getters.macro
local setmacro = tokens.setters.macro
local getdimen = tex.getdimen
local getcount = tex.getcount
local gettoks = tex.gettoks
local setdimen = tex.setdimen
local setcount = tex.setcount
local settoks = tex.settoks
-- more helpers
registerdirect("getmacro", function() return getmacro(scanstring()) end)
registerdirect("getcount", function() return getcount(scanwhatever()) end)
registerdirect("gettoks", function() return gettoks (scanwhatever()) end)
registerdirect("getdimen", function() return getdimen(scanwhatever()) * bpfactor end)
registerscript("setmacro", function() setmacro(scanstring(),scanstring()) end)
registerscript("setdimen", function() setdimen(scanwhatever(),scannumeric()/bpfactor) end)
registerscript("setcount", function() setcount(scanwhatever(),scannumeric()) end)
registerscript("settoks", function() settoks (scanwhatever(),scanstring()) end)
registerscript("setglobalmacro", function() setmacro(scanstring(),scanstring(),"global") end)
registerscript("setglobaldimen", function() setdimen("global",scanwhatever(),scannumeric()/bpfactor) end)
registerscript("setglobalcount", function() setcount("global",scanwhatever(),scaninteger()) end)
registerscript("setglobaltoks", function() settoks ("global",scanwhatever(),scanstring()) end)
local utfnum = utf.byte
local utflen = utf.len
local utfsub = utf.sub
registerdirect("utfnum", function() return utfnum(scanstring()) end)
registerdirect("utflen", function() return utflen(scanstring()) end)
registerdirect("utfsub", function() -- we have an optional third argument so we explicitly scan a text argument
return utfsub(scanstring(),skiptoken(comma_code) and scannumeric(),skiptoken(comma_code) and scannumeric())
end)
local setlogging = metapost.setlogging
registerscript("message", function()
setlogging(false)
local str = scanstring()
setlogging(true)
report_message("message : %s",str)
end)
end
-- position fun
do
local mpprint = mp.print
local mpfprint = mp.fprint
local jobpositions = job.positions
local getwhd = jobpositions.whd
local getxy = jobpositions.xy
local getposition = jobpositions.position
local getpage = jobpositions.page
local getparagraph = jobpositions.paragraph
local getregion = jobpositions.region
local getcolumn = jobpositions.column
local getmacro = tokens.getters.macro
registerscript("positionpath", function()
local w, h, d = getwhd(scanstring())
if w then
mpfprint("((%p,%p)--(%p,%p)--(%p,%p)--(%p,%p)--cycle)",0,-d,w,-d,w,h,0,h)
else
mpprint("(origin--cycle)")
end
end)
registerscript("positioncurve", function()
local w, h, d = getwhd(scanstring())
if w then
mpfprint("((%p,%p)..(%p,%p)..(%p,%p)..(%p,%p)..cycle)",0,-d,w,-d,w,h,0,h)
else
mpprint("(origin--cycle)")
end
end)
registerscript("positionbox", function()
local p, x, y, w, h, d = getposition(scanstring())
if p then
mpfprint("((%p,%p)--(%p,%p)--(%p,%p)--(%p,%p)--cycle)",x,y-d,x+w,y-d,x+w,y+h,x,y+h)
else
mpprint("(%p,%p)--cycle",x or 0,y or 0)
end
end)
registerdirect("positionpage", function() return getpage (scanstring()) or 0 end)
registerdirect("positioncolumn", function() return getcolumn (scanstring()) or 0 end)
registerdirect("positionparagraph", function() return getparagraph(scanstring()) or 0 end)
registerdirect("positionregion", function() return getregion (scanstring()) or "unknown" end)
registerdirect("positionanchor", function() return getmacro ("MPanchorid") end)
registerdirect("positionwhd", function() injectwhd(getwhd(scanstring())) end)
registerdirect("positionxy", function() injectxy (getxy (scanstring())) end)
registerdirect("positionx", function() injectpt (getx (scanstring())) end)
registerdirect("positiony", function() injectpt (gety (scanstring())) end)
end
do
local modes = tex.modes
local systemmodes = tex.systemmodes
registerdirect("mode", function() injectboolean(modes [scanstring()] and true or false) end)
registerdirect("systemmode", function() injectboolean(systemmodes[scanstring()] and true or false) end)
-- for compatibility reasons we keep this (metafun manual):
local modes = tex.modes
local systemmodes = tex.systemmodes
function mp.mode(s)
injectboolean(modes[s] and true or false)
end
function mp.systemmode(s)
injectboolean(systemmodes[s] and true or false)
end
mp.processingmode = mp.mode
end
-- for alan's nodes:
do
local lpegmatch, lpegpatterns, P = lpeg.match, lpeg.patterns, lpeg.P
-- todo: scansuffix / why no return boolean (first one)
registerdirect("isarray", function()
injectboolean(find(scanstring(),"%d") and true or false)
end)
registerdirect("prefix", function()
local str = scanstring()
return match(str,"^(.-)[%d%[]") or str
end)
local dimension = lpeg.counter(P("[") * lpegpatterns.integer * P("]") + lpegpatterns.integer)
registerdirect("dimension", function() return dimension(scanstring()) end)
-- todo : share with mlib-pps.lua metapost,isobject
-- registerdirect("isobject", function()
-- injectboolean(find(scanstring(),"mf_object="))
-- end
local p1 = P("mf_object=")
local p2 = lpegpatterns.eol * p1
local pattern = (1-p2)^0 * p2 + p1
registerdirect("isobject", function()
local str = scanstring()
injectboolean(pattern and str ~= "" and lpegmatch(pattern,str))
end)
end
-- key/values (moved here, old mechanism)
do
local stack, top = { }, nil
local function setvariable(k,v)
if top then
top[k] = v
else
metapost.variables[k] = v
end
end
local function pushvariable(k)
local t = { }
if top then
insert(stack,top)
top[k] = t
else
metapost.variables[k] = t
end
top = t
end
local function popvariable()
top = remove(stack)
end
registerscript("passvariable", function() setvariable (scanstring(), scanwhatever()) end)
registerscript("pushvariable", function() pushvariable(scanstring()) end)
registerscript("popvariable", function() popvariable () end)
local stack = { }
local function pushvariables()
insert(stack,metapost.variables)
metapost.variables = { }
end
local function popvariables()
metapost.variables = remove(stack) or metapost.variables
end
metapost.setvariable = setvariable
metapost.pushvariable = pushvariable
metapost.popvariable = popvariable
metapost.pushvariables = pushvariables
metapost.popvariables = popvariables
implement {
name = "mppushvariables",
actions = pushvariables,
}
implement {
name = "mppopvariables",
actions = popvariables,
}
end
do
local repeatable = utilities.randomizer.repeatable
registerdirect("repeatablerandom", function()
return repeatable(scanstring())
end)
end
|