summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/mlib-lua.lmt
blob: 30fc2ff63d295ce7a6940e9b5cad285a33afb035 (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
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 scan   = mp.scan
local inject = mp.inject

local currentmpx = nil
local stack      = { }

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_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

scan.next       = function(k)   return scan_next      (currentmpx,k)   end
scan.expression = function(k)   return scan_expression(currentmpx,k)   end
scan.token      = function(k)   return scan_token     (currentmpx,k)   end
scan.symbol     = function(k,e) return scan_symbol    (currentmpx,k,e) end
scan.numeric    = function()    return scan_numeric   (currentmpx)     end
scan.integer    = function()    return scan_integer   (currentmpx)     end
scan.boolean    = function()    return scan_boolean   (currentmpx)     end
scan.string     = function()    return scan_string    (currentmpx)     end
scan.pair       = function(t)   return scan_pair      (currentmpx,t)   end
scan.color      = function(t)   return scan_color     (currentmpx,t)   end
scan.cmykcolor  = function(t)   return scan_cmykcolor (currentmpx,t)   end
scan.transform  = function(t)   return scan_transform (currentmpx,t)   end
scan.path       = function(t)   return scan_path      (currentmpx,t)   end
scan.pen        = function(t)   return scan_pen       (currentmpx,t)   end

local solve_path = mplib.solve_path

mp.solve = function(...)
    return solve_path(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)  return inject_path     (currentmpx,t,cycle,curled)  end
inject.numeric   = function(n)               return inject_numeric  (currentmpx,n)               end
inject.pair      = function(x,y)             return inject_pair     (currentmpx,x,y)             end
inject.boolean   = function(b)               return inject_boolean  (currentmpx,b)               end
inject.integer   = function(i)               return inject_integer  (currentmpx,i)               end
inject.string    = function(s)               return inject_string   (currentmpx,s)               end
inject.color     = function(r,g,b)           return inject_color    (currentmpx,r,g,b)           end
inject.cmykcolor = function(c,m,y,k)         return inject_cmykcolor(currentmpx,c,m,y,k)         end
inject.transform = function(x,y,xx,xy,yx,yy) return inject_transform(currentmpx,x,y,xx,xy,yx,yy) end
inject.whatever  = function(...)             return inject_whatever (currentmpx,...)             end

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 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
    return inject_path(currentmpx,p,curled,close)
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)
    insert(stack,mpx)
    currentmpx = mpx
end

function metapost.popscriptrunner()
    currentmpx = remove(stack,mpx)
end

function metapost.currentmpx()
    return currentmpx
end

local status = mplib.status

function metapost.currentmpxstatus()
    return status and status(currentmpx) or 0
end