summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/mlib-ctx.lua
blob: 96eb27cbd18fac5768460e97cc37b0176c7b4e3d (plain)
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
if not modules then modules = { } end modules ['mlib-ctx'] = {
    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",
}

-- for the moment we have the scanners here but they migh tbe moved to
-- the other modules

local type, tostring = type, tostring
local format, concat = string.format, table.concat
local settings_to_hash = utilities.parsers.settings_to_hash

local report_metapost = logs.reporter("metapost")

local starttiming        = statistics.starttiming
local stoptiming         = statistics.stoptiming

local mplib              = mplib

metapost                 = metapost or {}
local metapost           = metapost

local context            = context

local setters            = tokens.setters
local setmacro           = setters.macro
local implement          = interfaces.implement

local v_no               = interfaces.variables.no

metapost.defaultformat   = "metafun"
metapost.defaultinstance = "metafun"
metapost.defaultmethod   = "default"

local function setmpsformat(specification)
    local instance = specification.instance
    local format   = specification.format
    local method   = specification.method
    if not instance or instance == "" then
        instance = metapost.defaultinstance
        specification.instance = instance
    end
    if not format or format == "" then
        format = metapost.defaultformat
        specification.format = format
    end
    if not method or method == "" then
        method = metapost.defaultmethod
        specification.method = method
    end
    specification.mpx = metapost.format(instance,format,method)
    return specification
end

local extensiondata    = metapost.extensiondata or storage.allocate { }
metapost.extensiondata = extensiondata

storage.register("metapost/extensiondata",extensiondata,"metapost.extensiondata")

function metapost.setextensions(instances,data)
    if data and data ~= "" then
        extensiondata[#extensiondata+1] = {
            usedinall  = not instances or instances == "",
            instances  = settings_to_hash(instances or ""),
            extensions = data,
        }
    end
end

function metapost.getextensions(instance,state)
    if state and state == v_no then
        return ""
    else
        local t = { }
        for i=1,#extensiondata do
            local e = extensiondata[i]
            local status = e.instances[instance]
            if (status ~= true) and (e.usedinall or status) then
                t[#t+1] = e.extensions
                e.instances[instance] = true
            end
        end
        return concat(t," ")
    end
end

-- function commands.getmpextensions(instance,state)
--     context(metapost.getextensions(instance,state))
-- end

implement {
    name      = "setmpextensions",
    actions   = metapost.setextensions,
    arguments = { "string", "string" }
}

implement {
    name      = "getmpextensions",
    actions   = { metapost.getextensions, context } ,
    arguments = "string"
}

local report_metapost = logs.reporter ("metapost")
local status_metapost = logs.messenger("metapost")

local patterns = {
    "meta-imp-%s.mkiv",
    "meta-imp-%s.tex",
    -- obsolete:
    "meta-%s.mkiv",
    "meta-%s.tex"
}

local function action(name,foundname)
    status_metapost("library %a is loaded",name)
    context.startreadingfile()
    context.input(foundname)
    context.stopreadingfile()
end

local function failure(name)
    report_metapost("library %a is unknown or invalid",name)
end

implement {
    name      = "useMPlibrary",
    arguments = "string",
    actions   = function(name)
        resolvers.uselibrary {
            name     = name,
            patterns = patterns,
            action   = action,
            failure  = failure,
            onlyonce = true,
        }
    end
}

-- metapost.variables  = { } -- to be stacked

implement {
    name      = "mprunvar",
    arguments = "string",
    actions   = function(name)
        local value = metapost.variables[name]
        if value ~= nil then
            local tvalue = type(value)
            if tvalue == "table" then
                context(concat(value," "))
            elseif tvalue == "number" or tvalue == "boolean" then
                context(tostring(value))
            elseif tvalue == "string" then
                context(value)
            end
        end
    end
}

implement {
    name      = "mpruntab",
    arguments = { "string", "integer" },
    actions   = function(name,n)
        local value = metapost.variables[name]
        if value ~= nil then
            local tvalue = type(value)
            if tvalue == "table" then
                context(value[n])
            elseif tvalue == "number" or tvalue == "boolean" then
                context(tostring(value))
            elseif tvalue == "string" then
                context(value)
            end
        end
    end
}

implement {
    name      = "mprunset",
    arguments = { "string", "string" },
    actions   = function(name,connector)
        local value = metapost.variables[name]
        if value ~= nil then
            local tvalue = type(value)
            if tvalue == "table" then
                context(concat(value,connector))
            elseif tvalue == "number" or tvalue == "boolean" then
                context(tostring(value))
            elseif tvalue == "string" then
                context(value)
            end
        end
    end
}

-- we need to move more from pps to here as pps is the plugin .. the order is a mess
-- or just move the scanners to pps

function metapost.graphic(specification)
    metapost.graphic_base_pass(setmpsformat(specification))
end

implement {
    name      = "mpgraphic",
    actions   = metapost.graphic,
    arguments = {
        {
            { "instance" },
            { "format" },
            { "data" },
            { "initializations" },
            { "extensions" },
            { "inclusions" },
            { "definitions" },
            { "figure" },
            { "method" },
            { "namespace" },
        }
    }
}

implement {
    name      = "mpsetoutercolor",
    actions   = function(...) metapost.setoutercolor(...) end, -- not yet implemented
    arguments = { "integer", "integer", "integer", "integer" }
}

implement {
    name      = "mpflushreset",
    actions   = function() metapost.flushreset() end -- not yet implemented
}

implement {
    name      = "mpflushliteral",
    actions   = function(str) metapost.flushliteral(str) end, -- not yet implemented
    arguments = "string",
}

function metapost.getclippath(specification) -- why not a special instance for this
    setmpsformat(specification)
    local mpx = specification.mpx
    local data = specification.data or ""
    if mpx and data ~= "" then
        starttiming(metapost)
        starttiming(metapost.exectime)
        local result = mpx:execute ( format ( "%s;%s;beginfig(1);%s;%s;endfig;",
            specification.extensions or "",
            specification.inclusions or "",
            specification.initializations or "",
            data
        ) )
        stoptiming(metapost.exectime)
        if result.status > 0 then
            report_metapost("%s: %s", result.status, result.error or result.term or result.log)
            result = nil
        else
            result = metapost.filterclippath(result)
        end
        stoptiming(metapost)
        return result
    end
end

function metapost.filterclippath(result)
    if result then
        local figures = result.fig
        if figures and #figures > 0 then
            local figure = figures[1]
            local objects = figure:objects()
            if objects then
                local lastclippath
                for o=1,#objects do
                    local object = objects[o]
                    if object.type == "start_clip" then
                        lastclippath = object.path
                    end
                end
                return lastclippath
            end
        end
    end
end

function metapost.theclippath(...)
    local result = metapost.getclippath(...)
    if result then -- we could just print the table
        return concat(metapost.flushnormalpath(result)," ")
    else
        return ""
    end
end

implement {
    name      = "mpsetclippath",
    actions   = function(specification)
        setmacro("MPclippath",metapost.theclippath(specification),"global")
    end,
    arguments = {
        {
            { "instance" },
            { "format" },
            { "data" },
            { "initializations" },
            { "useextensions" },
            { "inclusions" },
            { "method" },
            { "namespace" },
        },
    }
}

statistics.register("metapost processing time", function()
    local n =  metapost.n
    if n and n > 0 then
        local nofconverted = metapost.makempy.nofconverted
        local elapsedtime = statistics.elapsedtime
        local elapsed = statistics.elapsed
        local instances, memory = metapost.getstatistics(true)
        local str = format("%s seconds, loading: %s, execution: %s, n: %s, average: %s, instances: %i, memory: %0.3f M",
            elapsedtime(metapost), elapsedtime(mplib), elapsedtime(metapost.exectime), n,
            elapsedtime((elapsed(metapost) + elapsed(mplib) + elapsed(metapost.exectime)) / n),
            instances, memory/(1024*1024))
        if nofconverted > 0 then
            return format("%s, external: %s (%s calls)",
                str, elapsedtime(metapost.makempy), nofconverted)
        else
            return str
        end
    else
        return nil
    end
end)

-- only used in graphictexts

metapost.tex = metapost.tex or { }
local mptex  = metapost.tex

local environments = { }

function mptex.set(str)
    environments[#environments+1] = str
end

function mptex.setfrombuffer(name)
    environments[#environments+1] = buffers.getcontent(name)
end

function mptex.get()
    return concat(environments,"\n")
end

function mptex.reset()
    environments = { }
end

implement {
    name      = "mppushvariables",
    actions   = metapost.pushvariables,
}

implement {
    name      = "mppopvariables",
    actions   = metapost.popvariables,
}

implement {
    name      = "mptexset",
    arguments = "string",
    actions   = mptex.set
}

implement {
    name      = "mptexsetfrombuffer",
    arguments = "string",
    actions   = mptex.setfrombuffer
}

implement {
    name      = "mptexget",
    actions   = { mptex.get, context }
}

implement {
    name      = "mptexreset",
    actions   = mptex.reset
}