summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/mlib-lmt.lmt
blob: 651a98be5c3949a351afb5cb6da5b41e2709fcc9 (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
if not modules then modules = { } end modules ['mlib-lmt'] = {
    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",
}

-- todo: check for possible inject usage

local type = type
local round = math.round

local aux            = mp.aux
local mpdirect       = aux.direct
local mppath         = mp.path

local scan           = mp.scan
local scannumeric    = scan.numeric
local scanpath       = scan.path

local injectpath     = mp.inject.path
local injectcolor    = mp.inject.color

local getparameter   = metapost.getparameter
local registerscript = metapost.registerscript

function mp.lmt_function_x(xmin,xmax,xstep,code,shape) -- experimental
    local code      = "return function(x) return " .. code .. " end"
    local action    = load(code)
    local points    = { }
    local nofpoints = 0
    if action then
         action = action()
    end
    if shape == "steps" then
        local halfx     = xstep / 2
        local lastx     = xmin
        local lasty     = action(xmin)
        for xi = xmin, xmax, xstep do
            local yi  = action(xi)
            local xx  = lastx + halfx
            nofpoints = nofpoints + 1 ; points[nofpoints] = { xx, lasty }
            nofpoints = nofpoints + 1 ; points[nofpoints] = { xx, yi }
            lastx     = xi
            lasty     = yi
        end
        if points[nofpoints][1] ~= xmax then
            local yi  = action(xmax)
            local xx  = lastx + halfx
            nofpoints = nofpoints + 1 ; points[nofpoints] = { xx, lasty }
            nofpoints = nofpoints + 1 ; points[nofpoints] = { xx, yi }
            lastx     = xi
            lasty     = yi
        end
    else
        for xi = xmin, xmax, xstep do
            nofpoints = nofpoints + 1 ; points[nofpoints] = { xi, action(xi) }
        end
        if points[nofpoints][1] ~= xmax then
            nofpoints = nofpoints + 1 ; points[nofpoints] = { xmax, action(xmax) }
        end
    end
    mppath(points,shape == "curve" and ".." or "--",false)
end

function mp.lmt_mesh_set()
    local mesh = getparameter { "mesh", "paths" }
    structures.references.currentset.mesh = mesh
end

function mp.lmt_mesh_update()
    local mesh = getparameter { "paths" } or getparameter { "mesh", "paths" }
    mesh[scannumeric()] = scanpath(true)
end

-- moved here

function mp.lmt_svg_include()
    local labelfile = metapost.getparameter { "labelfile" }
    if labelfile and labelfile ~= "" then
        local labels = table.load(labelfile) -- todo: same path as svg file
        if type(labels) == "table" then
            for i=1,#labels do
                metapost.remaptext(labels[i])
            end
        end
    end
    local fontname = metapost.getparameter { "fontname" }
    if fontname and fontname ~= "" then
        local unicode = metapost.getparameter { "unicode" }
        if unicode then
            mpdirect (
                metapost.svgglyphtomp(fontname,math.round(unicode))
            )
        end
        return
    end
    local colorfile = metapost.getparameter { "colormap" }
    local colormap  = false
    if colorfile and colorfile ~= "" then
        colormap = metapost.svgcolorremapper(colorfile)
    end
    local filename = metapost.getparameter { "filename" }
    if filename and filename ~= "" then
        mpdirect ( metapost.svgtomp {
            data     = io.loaddata(filename),
            remap    = true,
            colormap = colormap,
            id       = filename,
        } )
    else
        local buffer = metapost.getparameter { "buffer" }
        if buffer then
            mpdirect ( metapost.svgtomp {
                data     = buffers.getcontent(buffer),
             -- remap    = true,
                colormap = colormap,
                id       = buffer or "buffer",
            } )
        else
            local code = metapost.getparameter { "code" }
            if code then
                mpdirect ( metapost.svgtomp {
                    data     = code,
                    colormap = colormap,
                    id       = "code",
                } )
            end
        end
    end
end

registerscript("remaptext", function()
    local parameters = metapost.scanparameters()
    if parameters and parameters.label then
        metapost.remaptext(parameters)
    end
end)

do

    local dropins        = fonts.dropins
    local registerglyph  = dropins.registerglyph
    local registerglyphs = dropins.registerglyphs

    registerscript("registerglyph", function() registerglyph (metapost.getparameterset("mpsglyph"))  end)
    registerscript("registerglyphs",function() registerglyphs(metapost.getparameterset("mpsglyphs")) end)

end

todecimal = xdecimal and xdecimal.new or tonumber -- bonus

-- mail on list by Mikael Sundqvist and Taco's analysis of near duplicate points (2021/02/11+)

registerscript("scrutenized", function()
    local p = scanpath()
    local d = 10^scannumeric()
    for i=1,#p do
        local pi = p[i]
        pi[1] = round(pi[1] * d) / d
        pi[2] = round(pi[2] * d) / d
    end
    local x1 = round(p[1][1])
    local y1 = round(p[1][2])
    local n = 1
    local m = #p
    local t = { p[1], cycle = p.cycle }
    for i=2,m-1 do
        local pi = p[i]
        local x2 = round(pi[1])
        local y2 = round(pi[2])
        if x1 ~= x2 or y1 ~= y2 then
            n = n + 1
            t[n] = p[i]
            x1 = x2
            y1 = y2
        end
    end
    local x1 = round(p[1][1])
    local y1 = round(p[1][2])
    local x2 = round(p[m][1])
    local y2 = round(p[m][2])
    if x1 ~= x2 or y1 ~= y2 then
        n = n + 1
        t[n] = p[m]
    end
  -- mp.path(t)
    injectpath(t)
end)

-- A goodie, mostly a side effect of updating the metafun manual.


local labtorgb = attributes.colors.labtorgb

registerscript("labtorgb", function()
    injectcolor(labtorgb(scannumeric(),scannumeric(),scannumeric()))
end)