summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/x-mathml.lua
blob: fa5e617e60733203f6cf9a0d42f81cb921e9c532 (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
if not modules then modules = { } end modules ['x-mathml'] = {
    version   = 1.001,
    comment   = "companion to x-mathml.tex",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

lxml.mml = lxml.mml or { }

local texsprint = tex.sprint
local format    = string.format
local xmlsprint = xml.sprint
local xmlcprint = xml.cprint

-- an alternative is to remap to private codes, where we can have
-- different properties .. to be done; this will move and become
-- generic

local n_replacements = {
--  [" "] = utf.char(0x2002),  -- "&textspace;" -> tricky, no &; in mkiv
    ["."] = "{.}",
    [","] = "{,}",
    [" "] = "",
}

local o_replacements = {
    ["@l"] = "\\mmlleftdelimiter.",
    ["@r"] = "\\mmlrightdelimiter.",
    ["{"]  = "\\mmlleftdelimiter\\lbrace",
    ["}"]  = "\\mmlrightdelimiter\\rbrace",
    ["("]  = "\\mmlleftdelimiter(",
    [")"]  = "\\mmlrightdelimiter)",
    ["["]  = "\\mmlleftdelimiter[",
    ["]"]  = "\\mmlrightdelimiter]",
    ["<"]  = "\\mmlleftdelimiter<",
    [">"]  = "\\mmlrightdelimiter>",
    ["#"]  = "\\mmlchar{35}",
    ["$"]  = "\\mmlchar{36}", -- $
    ["%"]  = "\\mmlchar{37}",
    ["&"]  = "\\mmlchar{38}",
    ["^"]  = "\\mmlchar{94}{}", -- strange, sometimes luatex math sees the char instead of \char
    ["_"]  = "\\mmlchar{95}{}", -- so we need the {}
    ["~"]  = "\\mmlchar{126}",
    [" "]  = "",
}

local i_replacements = {
    ["sin"]         = "\\mathopnolimits{sin}",
    ["cos"]         = "\\mathopnolimits{cos}",
    ["abs"]         = "\\mathopnolimits{abs}",
    ["arg"]         = "\\mathopnolimits{arg}",
    ["codomain"]    = "\\mathopnolimits{codomain}",
    ["curl"]        = "\\mathopnolimits{curl}",
    ["determinant"] = "\\mathopnolimits{det}",
    ["divergence"]  = "\\mathopnolimits{div}",
    ["domain"]      = "\\mathopnolimits{domain}",
    ["gcd"]         = "\\mathopnolimits{gcd}",
    ["grad"]        = "\\mathopnolimits{grad}",
    ["identity"]    = "\\mathopnolimits{id}",
    ["image"]       = "\\mathopnolimits{image}",
    ["lcm"]         = "\\mathopnolimits{lcm}",
    ["max"]         = "\\mathopnolimits{max}",
    ["median"]      = "\\mathopnolimits{median}",
    ["min"]         = "\\mathopnolimits{min}",
    ["mode"]        = "\\mathopnolimits{mode}",
    ["mod"]         = "\\mathopnolimits{mod}",
    ["polar"]       = "\\mathopnolimits{Polar}",
    ["exp"]         = "\\mathopnolimits{exp}",
    ["ln"]          = "\\mathopnolimits{ln}",
    ["log"]         = "\\mathopnolimits{log}",
    ["sin"]         = "\\mathopnolimits{sin}",
    ["arcsin"]      = "\\mathopnolimits{arcsin}",
    ["sinh"]        = "\\mathopnolimits{sinh}",
    ["arcsinh"]     = "\\mathopnolimits{arcsinh}",
    ["cos"]         = "\\mathopnolimits{cos}",
    ["arccos"]      = "\\mathopnolimits{arccos}",
    ["cosh"]        = "\\mathopnolimits{cosh}",
    ["arccosh"]     = "\\mathopnolimits{arccosh}",
    ["tan"]         = "\\mathopnolimits{tan}",
    ["arctan"]      = "\\mathopnolimits{arctan}",
    ["tanh"]        = "\\mathopnolimits{tanh}",
    ["arctanh"]     = "\\mathopnolimits{arctanh}",
    ["cot"]         = "\\mathopnolimits{cot}",
    ["arccot"]      = "\\mathopnolimits{arccot}",
    ["coth"]        = "\\mathopnolimits{coth}",
    ["arccoth"]     = "\\mathopnolimits{arccoth}",
    ["csc"]         = "\\mathopnolimits{csc}",
    ["arccsc"]      = "\\mathopnolimits{arccsc}",
    ["csch"]        = "\\mathopnolimits{csch}",
    ["arccsch"]     = "\\mathopnolimits{arccsch}",
    ["sec"]         = "\\mathopnolimits{sec}",
    ["arcsec"]      = "\\mathopnolimits{arcsec}",
    ["sech"]        = "\\mathopnolimits{sech}",
    ["arcsech"]     = "\\mathopnolimits{arcsech}",
    [" "]           = "",

    ["false"]       = "{\\mr false}",
    ["notanumber"]  = "{\\mr NaN}",
    ["otherwise"]   = "{\\mr otherwise}",
    ["true"]        = "{\\mr true}",
    ["declare"]     = "{\\mr declare}",
    ["as"]          = "{\\mr as}",
}

function lxml.mml.checked_operator(str)
    texsprint(tex.ctxcatcodes,(str:gsub(".",o_replacements)))
end

function lxml.mml.mn(id,pattern)
    local str = xml.content(lxml.id(id),pattern) or ""
    texsprint(tex.ctxcatcodes,(str:gsub(".",n_replacements)))
end
function lxml.mml.mo(id,pattern)
    local str = xml.content(lxml.id(id),pattern) or ""
    tex.sprint(tex.ctxcatcodes,(str:gsub(".",o_replacements)))
end
function lxml.mml.mi(id,pattern)
    local str = xml.content(lxml.id(id),pattern) or ""
    str = str:gsub("^%s*(.*)%s*$","%1")
    local rep = i_replacements[str]
    if rep then
        tex.sprint(tex.ctxcatcodes,rep)
    else
        tex.sprint(tex.ctxcatcodes,(str:gsub(".",i_replacements)))
    end
end

function lxml.mml.mfenced(id,pattern) -- multiple separators
    id = lxml.id(id)
    local left, right, separators = id.at.open or "(", id.at.close or ")", id.at.separators or ","
    local l, r = left:find("[%(%{%<%[]"), right:find("[%)%}%>%]]")
    texsprint(tex.ctxcatcodes,"\\enabledelimiter")
    if l then
        texsprint(tex.ctxcatcodes,o_replacements[left])
    else
        texsprint(tex.ctxcatcodes,o_replacements["@l"])
        texsprint(tex.ctxcatcodes,left)
    end
    texsprint(tex.ctxcatcodes,"\\disabledelimiter")
    local n = xml.count(id,pattern)
    if n == 0 then
        -- skip
    elseif n == 1 then
        lxml.all(id,pattern)
    else
        local t = { }
        for s in utf.gmatch(separators,"([^%s])") do
            t[#t+1] = s
        end
        for i=1,n do
            lxml.idx(id,pattern,i) -- kind of slow, some day ...
            if i < n then
                local m = t[i] or t[#t] or ""
                if m == "|" then
                    m = "\\enabledelimiter\\middle|\\relax\\disabledelimiter"
                elseif m == "{" then
                    m = "\\{"
                elseif m == "}" then
                    m = "\\}"
                end
                texsprint(tex.ctxcatcodes,m)
            end
        end
    end
    texsprint(tex.ctxcatcodes,"\\enabledelimiter")
    if r then
        texsprint(tex.ctxcatcodes,o_replacements[right])
    else
        texsprint(tex.ctxcatcodes,right)
        texsprint(tex.ctxcatcodes,o_replacements["@r"])
    end
    texsprint(tex.ctxcatcodes,"\\disabledelimiter")
end

local function flush(e,tag,toggle)
 -- texsprint(tex.ctxcatcodes,(toggle and "^{") or "_{")
    if toggle then
        texsprint(tex.ctxcatcodes,"^{")
    else
        texsprint(tex.ctxcatcodes,"_{")
    end
    if tag == "none" then
        texsprint(tex.ctxcatcodes,"{}")
    else
        xmlsprint(e.dt)
    end
    if not toggle then
        texsprint(tex.ctxcatcodes,"}")
    else
        texsprint(tex.ctxcatcodes,"}{}")
    end
    return not toggle
end

function lxml.mml.mmultiscripts(id)
    local done, toggle = false, false
    id = lxml.id(id)
    -- for i=1,#id.dt do local e = id.dt[i] if type(e) == table then ...
    for r, d, k in xml.elements(id,"/*") do
        local e = d[k]
        local tag = e.tg
        if tag == "mprescripts" then
            texsprint(tex.ctxcatcodes,"{}")
            done = true
        elseif done then
            toggle = flush(e,tag,toggle)
        end
    end
    local done, toggle = false, false
    for r, d, k in xml.elements(id,"/*") do
        local e = d[k]
        local tag = e.tg
        if tag == "mprescripts" then
            break
        elseif done then
            toggle = flush(e,tag,toggle)
        else
            xmlsprint(e.dt)
            done = true
        end
    end
end

local columnalignments = {
    left   = "flushleft",
    right  = "flushright",
    center = "middle",
}

local rowalignments = {
    top      = "high",
    bottom   = "low",
    center   = "lohi",
    baseline = "top",
    axis     = "lohi",
}

local frametypes = {
    none   = "off",
    solid  = "on",
    dashed = "on",
}

function lxml.mml.mtable(root)

    root = lxml.id(root)

    -- todo: align, rowspacing, columnspacing, rowlines, columnlines

    local at           = root.at
    local rowalign     = at.rowalign
    local columnalign  = at.columnalign
    local frame        = at.frame
    local rowaligns    = rowalign    and rowalign   :split(" ") -- we have a faster one
    local columnaligns = columnalign and columnalign:split(" ") -- we have a faster one
    local frames       = frame       and frame      :split(" ") -- we have a faster one
    local framespacing = at.framespacing or ".5ex"

    texsprint(tex.ctxcatcodes, format("\\bTABLE[frame=%s,offset=%s]",frametypes[frame or "none"] or "off",framespacing))
    for r, d, k in xml.elements(root,"/(mml:mtr|mml:mlabeledtr)") do
        texsprint(tex.ctxcatcodes,"\\bTR")
        local dk = d[k]
        local at = dk.at
        local col = 0
        local rfr = at.frame       or (frames       and frames      [#frames])
        local rra = at.rowalign    or (rowaligns    and rowaligns   [#rowaligns])
        local rca = at.columnalign or (columnaligns and columnaligns[#columnaligns])
        for rr, dd, kk in xml.elements(dk,"/mml:mtd") do
            col = col + 1
            local dk = dd[kk]
            local at = dk.at
            local rowspan, columnspan = at.rowspan or 1, at.columnspan or 1
            local cra = rowalignments   [at.rowalign    or (rowaligns    and rowaligns   [col]) or rra or "center"] or "lohi"
            local cca = columnalignments[at.columnalign or (columnaligns and columnaligns[col]) or rca or "center"] or "middle"
            local cfr = frametypes      [at.frame       or (frames       and frames      [col]) or rfr or "none"  ] or "off"
            texsprint(tex.ctxcatcodes,format("\\bTD[align={%s,%s},frame=%s,nx=%s,ny=%s]$\\ignorespaces",cra,cca,cfr,columnspan,rowspan))
            xmlcprint(dk)
            texsprint(tex.ctxcatcodes,"\\removeunwantedspaces$\\eTD") -- $
        end
        if dk.tg == "mlabeledtr" then
            texsprint(tex.ctxcatcodes,"\\bTD")
            xmlcprint(xml.first(dk,"/!mml:mtd"))
            texsprint(tex.ctxcatcodes,"\\eTD")
        end
        texsprint(tex.ctxcatcodes,"\\eTR")
    end
    texsprint(tex.ctxcatcodes, "\\eTABLE")
end

function lxml.mml.csymbol(root)
    root = lxml.id(root)
    local encoding = root.at.encoding or ""
    local hash = url.hashed((root.at.definitionUrl or ""):lower())
    local full = hash.original or ""
    local base = hash.path or ""
    local text = string.strip(xml.content(root) or "")
    texsprint(tex.ctxcatcodes,format("\\mmlapplycsymbol{%s}{%s}{%s}{%s}",full,base,encoding,text))
end