summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/math-pre.lmt
blob: 974623f27f5f0e7d368df7ce8ee76554564a171c (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
if not modules then modules = { } end modules ['math-pre'] = {
    version   = 1.001,
    optimize  = true,
    comment   = "companion to math-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local type, tonumber = type, tonumber
local gmatch, find, topattern = string.gmatch, string.find, string.topattern

local parameterlist = tex.getmathparametervalues()
local mathstylelist = tex.getmathstylenamevalues()
local parameterhash = table.swapped(parameterlist)
local mathstylehash = table.swapped(mathstylelist)

local axis = parameterhash.axis

local getmath = tex.getmath
local setmath = tex.setmath

local function expandparameters(t)
    local result = { }
    local kind   = type(t)
    local function expand(s)
        s = topattern(s)
        for i=1,#parameterlist do
            local p = parameterlist[i]
            if find(p,s) then
                result[#result+1] = p
            end
        end
    end
    if kind == "string" then
        for s in gmatch(t,"[^%s,]+") do
            expand(s)
        end
    elseif kind == "table" then
        for i=1,#t do
            expand(t[i])
        end
    end
    return result
end

function setmathparameters(t)
    if t then
        for i=1,#t do
            local ti     = t[i]
            local list   = ti.list
            local factor = ti.factor or 1
            local style  = ti.style
            local value  = ti.value
            local unit   = ti.unit

            local function set(li,si,value)
                if value then
                    setmath(li,si,value)
                elseif factor == 0 then
                    setmath(li,si,0)
                elseif unit == "axis" then
                    setmath(li,si,factor * getmath(axis,i))
                else
                    setmath(li,si,factor * getmath(li,i))
                end
            end

            for i=1,#list do
                local li = parameterhash[list[i]]
                if li then
                    if style == "all" then
                        for si=0,7 do
                            set(li,si,value)
                        end
                    elseif type(style) == "string" then
                        local si = mathstylehash[style]
                        if si then
                            set(li,si,value)
                        end
                    else
                        for s=1,#style do
                            local si = mathstylehash[style[s]]
                            if si then
                                set(li,si,value)
                            end
                        end
                    end
                end
            end
        end
    end
end

-- example

local stacklist = {
    "fractionnumvgap",
    "fractiondenomvgap",
    "fractionnumup",
    "fractiondenomdown",
    "stackdenomdown",
    "stacknumup",
    "stackvgap",
}

local presets = {
    less = {
        {
            factor = .5,
         -- factor = 0,
         -- value  = 655360,
         -- unit   = "axis",
            list   = stacklist,
         -- style  = { "display" },
         -- style  = "display",
            style = "all"
        },
    },
    more = {
        {
            factor = 2,
            list   = stacklist,
            style  = "all"
        },
    },
    zero = {
        {
            factor = 0,
            list   = stacklist,
            style  = "all"
        },
    },
}

mathematics.presets = presets -- we might need to store these in the format file

function mathematics.preset(list)
    for s in gmatch(list,"[^%s,]+") do
        setmathparameters(presets[s])
    end
end

-- todo: append, prepend, inherit

interfaces.implement {
    name      = "definemathpreset",
    public    = true,
    protected = true,
    arguments = { "optional", "hash" },
    actions   = function(name,t)
        if next(t) then
            local factor = t.factor
            local style  = t.style
            local list   = t.list
            local unit   = t.unit
            if factor then
                t.factor = tonumber(factor)
            end
            if style and style ~= "all" then
                t.style = utilities.parsers.settings_to_array(style)
            end
            if list then
             -- t.list = utilities.parsers.settings_to_array(list)
                t.list = expandparameters(list)
            end
            if unit and unit ~= "axis" then
                t.unit = nil
            end
            -- todo: value
            local p = presets[name]
            if p then
                p[#p+1] = t
            else
                presets[name] = t
            end
        else
            presets[name] = nil
        end
    end,
}

interfaces.implement {
    name      = "presetmathematics",
    public    = true,
    protected = true,
    arguments = "optional",
    actions   = mathematics.preset,
}