2021-04-23 02:58:07 +02:00
|
|
|
local remap_comb = require'luamml-data-combining'
|
|
|
|
local stretchy = require'luamml-data-stretchy'
|
2021-05-01 08:00:29 +02:00
|
|
|
local to_text = require'luamml-lr'
|
2021-04-19 19:56:03 +02:00
|
|
|
|
|
|
|
local properties = node.get_properties_table()
|
2021-04-19 17:07:29 +02:00
|
|
|
|
2021-05-01 07:57:03 +02:00
|
|
|
local hlist_t, kern_t, glue_t, rule_t = node.id'hlist', node.id'kern', node.id'glue', node.id'rule'
|
2021-04-19 21:04:48 +02:00
|
|
|
|
2021-04-18 15:19:52 +02:00
|
|
|
local noad_t, accent_t, style_t, choice_t = node.id'noad', node.id'accent', node.id'style', node.id'choice'
|
|
|
|
local radical_t, fraction_t, fence_t = node.id'radical', node.id'fraction', node.id'fence'
|
|
|
|
|
|
|
|
local math_char_t, sub_box_t, sub_mlist_t = node.id'math_char', node.id'sub_box', node.id'sub_mlist'
|
|
|
|
|
2021-05-11 06:16:36 +02:00
|
|
|
local function invert_table(t)
|
|
|
|
local t_inv = {}
|
|
|
|
for k, v in next, t do
|
|
|
|
t_inv[v] = k
|
|
|
|
end
|
|
|
|
return t_inv
|
|
|
|
end
|
|
|
|
|
|
|
|
local noad_names = node.subtypes'noad'
|
2021-05-13 02:56:37 +02:00
|
|
|
|
|
|
|
--[[ We could determine the noad subtypes dynamically:
|
2021-05-11 06:16:36 +02:00
|
|
|
local noad_sub = invert_table(noad_names)
|
|
|
|
local noad_ord = noad_sub.ord
|
|
|
|
local noad_op = noad_sub.opdisplaylimits
|
|
|
|
local noad_oplimits = noad_sub.oplimits
|
|
|
|
local noad_opnolimits = noad_sub.opnolimits
|
|
|
|
local noad_bin = noad_sub.bin
|
|
|
|
local noad_rel = noad_sub.rel
|
|
|
|
local noad_open = noad_sub.open
|
|
|
|
local noad_close = noad_sub.close
|
|
|
|
local noad_punct = noad_sub.punct
|
|
|
|
local noad_inner = noad_sub.inner
|
|
|
|
local noad_under = noad_sub.under
|
|
|
|
local noad_over = noad_sub.over
|
|
|
|
local noad_vcenter = noad_sub.vcenter
|
2021-05-13 02:56:37 +02:00
|
|
|
-- But the spacing table depends on their specific values anyway, so we just verify the values
|
|
|
|
]]
|
|
|
|
local noad_ord, noad_op, noad_oplimits, noad_opnolimits = 0, 1, 2, 3
|
|
|
|
local noad_bin, noad_rel, noad_open, noad_close, noad_punct = 4, 5, 6, 7, 8
|
|
|
|
local noad_inner, noad_under, noad_over, noad_vcenter = 9, 10, 11, 12
|
|
|
|
|
|
|
|
for i, n in ipairs{'ord', 'opdisplaylimits', 'oplimits', 'opnolimits', 'bin',
|
|
|
|
'rel', 'open', 'close', 'punct', 'inner', 'under', 'over', 'vcenter'} do
|
|
|
|
assert(noad_names[i-1] == n)
|
|
|
|
end
|
|
|
|
-- Attention, the spacing_table is indexed by subtype+1 since 1-based tables are faster in Lua
|
|
|
|
local spacing_table = {
|
|
|
|
{0 , '0.167em', '0.167em', '0.167em', '0.222em', '0.278em', 0 , 0 , 0 , '0.167em', 0 , 0 , 0 , },
|
|
|
|
{'0.167em', '0.167em', '0.167em', '0.167em', nil , '0.278em', 0 , 0 , 0 , '0.167em', '0.167em', '0.167em', '0.167em', },
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
{'0.222em', '0.222em', '0.222em', '0.222em', nil , nil , '0.222em', nil , nil , '0.222em', '0.222em', '0.222em', '0.222em', },
|
|
|
|
{'0.278em', '0.278em', '0.278em', '0.278em', nil , 0 , '0.278em', 0 , 0 , '0.278em', '0.278em', '0.278em', '0.278em', },
|
|
|
|
{0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
|
|
|
|
{0 , '0.167em', '0.167em', '0.167em', '0.222em', '0.278em', 0 , 0 , 0 , '0.167em', 0 , 0 , 0 , },
|
|
|
|
{'0.167em', '0.167em', '0.167em', '0.167em', nil , '0.167em', '0.167em', '0.167em', '0.167em', '0.167em', '0.167em', '0.167em', '0.167em', },
|
|
|
|
{'0.167em', '0.167em', '0.167em', '0.167em', '0.222em', '0.278em', '0.167em', 0 , '0.167em', '0.167em', '0.167em', '0.167em', '0.167em', },
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
local spacing_table_script = {
|
|
|
|
{0 , '0.167em', '0.167em', '0.167em', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
|
|
|
|
{'0.167em', '0.167em', '0.167em', '0.167em', nil , 0 , 0 , 0 , 0 , 0 , '0.167em', '0.167em', '0.167em', },
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
{0 , 0 , 0 , 0 , nil , nil , 0 , nil , nil , 0 , 0 , 0 , 0 , },
|
|
|
|
{0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
|
|
|
|
{0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
|
|
|
|
{0 , '0.167em', '0.167em', '0.167em', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
|
|
|
|
{0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
|
|
|
|
{0 , '0.167em', '0.167em', '0.167em', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
do -- Fill the blanks
|
|
|
|
local st, sts = spacing_table, spacing_table_script
|
|
|
|
|
|
|
|
local st_op, sts_op = st[noad_op+1], sts[noad_op+1]
|
|
|
|
st[noad_oplimits+1], sts[noad_oplimits+1] = st_op, sts_op
|
|
|
|
st[noad_opnolimits+1], sts[noad_opnolimits+1] = st_op, sts_op
|
|
|
|
|
|
|
|
local st_ord, sts_ord = st[noad_ord+1], sts[noad_ord+1]
|
|
|
|
st[noad_under+1], sts[noad_under+1] = st_ord, sts_ord
|
|
|
|
st[noad_over+1], sts[noad_over+1] = st_ord, sts_ord
|
|
|
|
st[noad_vcenter+1], sts[noad_vcenter+1] = st_ord, sts_ord
|
|
|
|
end
|
|
|
|
|
2021-05-11 06:16:36 +02:00
|
|
|
|
2021-04-18 15:19:52 +02:00
|
|
|
local radical_sub = node.subtypes'radical'
|
|
|
|
local fence_sub = node.subtypes'fence'
|
|
|
|
|
2021-04-22 23:38:28 +02:00
|
|
|
local remap_lookup = setmetatable({}, {__index = function(t, k)
|
|
|
|
local ch = utf8.char(k & 0x1FFFFF)
|
|
|
|
t[k] = ch
|
|
|
|
return ch
|
|
|
|
end})
|
|
|
|
local digit_map = {["0"] = true, ["1"] = true,
|
|
|
|
["2"] = true, ["3"] = true, ["4"] = true,
|
|
|
|
["5"] = true, ["6"] = true, ["7"] = true,
|
|
|
|
["8"] = true, ["9"] = true,}
|
|
|
|
|
2021-04-23 07:50:21 +02:00
|
|
|
-- Two marker tables. They are used instead of an embellished operator to mark space-like or user provided constructs
|
|
|
|
local user_provided, space_like = {}, {}
|
|
|
|
|
2021-04-18 15:19:52 +02:00
|
|
|
local nodes_to_table
|
|
|
|
|
2021-04-19 13:30:54 +02:00
|
|
|
local function sub_style(s) return s//4*2+5 end
|
|
|
|
local function sup_style(s) return s//4*2+4+s%2 end
|
|
|
|
|
2021-04-23 07:50:21 +02:00
|
|
|
-- The _to_table functions generally return a second argument which is
|
|
|
|
-- could be (if it were a <mo>) a core operator of the embellishe operator
|
|
|
|
-- or space_like/user_provided
|
2021-04-28 03:49:31 +02:00
|
|
|
-- acc_to_table is special since it's return value should
|
2021-04-23 07:50:21 +02:00
|
|
|
-- always be considered a core operator
|
|
|
|
|
2021-04-18 15:19:52 +02:00
|
|
|
-- We ignore large_... since they aren't used for modern fonts
|
|
|
|
local function delim_to_table(delim)
|
|
|
|
if not delim then return end
|
2021-04-19 19:56:03 +02:00
|
|
|
local props = properties[delim] props = props and props.mathml_table
|
|
|
|
if props then return props end
|
2021-04-28 03:49:31 +02:00
|
|
|
local char = delim.small_char
|
|
|
|
if char == 0 then
|
2021-04-29 02:07:53 +02:00
|
|
|
return {[0] = 'mspace', width = string.format("%.3fpt", tex.nulldelimiterspace/65781.76)}, space_like
|
2021-04-28 03:49:31 +02:00
|
|
|
else
|
|
|
|
local fam = delim.small_fam
|
|
|
|
char = remap_lookup[fam << 21 | char]
|
2021-05-12 07:43:57 +02:00
|
|
|
local result = {[0] = 'mo', char, ['tex:family'] = fam ~= 0 and fam or nil, stretchy = not stretchy[char] or nil, lspace = 0, rspace = 0, [':node'] = delim }
|
2021-04-28 03:49:31 +02:00
|
|
|
return result, result
|
|
|
|
end
|
2021-04-19 19:56:03 +02:00
|
|
|
end
|
|
|
|
|
2021-05-11 06:18:51 +02:00
|
|
|
-- Like kernel_to_table but always a math_char_t. Also creating a mo and potentially remapping to handle combining chars.
|
|
|
|
-- No lspace or space is set here since these never appear as core operators in an mrow.
|
2021-04-19 19:56:03 +02:00
|
|
|
local function acc_to_table(acc, cur_style, stretch)
|
|
|
|
if not acc then return end
|
|
|
|
local props = properties[acc] props = props and props.mathml_table
|
|
|
|
if props then return props end
|
|
|
|
if acc.id ~= math_char_t then
|
|
|
|
error'confusion'
|
|
|
|
end
|
|
|
|
local fam = acc.fam
|
2021-04-22 23:38:28 +02:00
|
|
|
local char = remap_lookup[fam << 21 | acc.char]
|
|
|
|
char = remap_comb[char] or char
|
2021-04-19 19:56:03 +02:00
|
|
|
if stretch ~= not stretchy[char] then -- Handle nil gracefully in stretchy
|
|
|
|
stretch = nil
|
|
|
|
end
|
2021-05-12 07:43:57 +02:00
|
|
|
return {[0] = 'mo', char, ['tex:family'] = fam ~= 0 and fam or nil, stretchy = stretch, [':node'] = acc}
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-19 13:30:54 +02:00
|
|
|
local function kernel_to_table(kernel, cur_style)
|
2021-04-18 15:19:52 +02:00
|
|
|
if not kernel then return end
|
2021-04-19 19:56:03 +02:00
|
|
|
local props = properties[kernel] props = props and props.mathml_table
|
2021-04-23 07:50:21 +02:00
|
|
|
if props then return props, user_provided end
|
2021-04-18 15:19:52 +02:00
|
|
|
local id = kernel.id
|
|
|
|
if id == math_char_t then
|
|
|
|
local fam = kernel.fam
|
2021-04-22 23:38:28 +02:00
|
|
|
local char = remap_lookup[fam << 21 | kernel.char]
|
|
|
|
local elem = digit_map[char] and 'mn' or 'mi'
|
2021-04-23 07:50:21 +02:00
|
|
|
local result = {[0] = elem,
|
2021-04-22 23:38:28 +02:00
|
|
|
char,
|
|
|
|
['tex:family'] = fam ~= 0 and fam or nil,
|
2021-05-12 07:43:57 +02:00
|
|
|
mathvariant = utf8.len(char) == 1 and elem == 'mi' and utf8.codepoint(char) < 0x10000 and 'normal' or nil,
|
|
|
|
[':node'] = kernel,
|
2021-04-22 23:38:28 +02:00
|
|
|
}
|
2021-04-23 07:50:21 +02:00
|
|
|
return result, result
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == sub_box_t then
|
2021-05-01 07:57:03 +02:00
|
|
|
if kernel.list.id == hlist_t then -- We directly give up for vlists
|
2021-05-01 08:03:21 +02:00
|
|
|
local result = to_text(kernel.list.head)
|
2021-05-01 08:00:29 +02:00
|
|
|
return result, result
|
2021-05-01 07:57:03 +02:00
|
|
|
else
|
2021-05-12 07:43:57 +02:00
|
|
|
local result = {[0] = 'mi', {[0] = 'mglyph', ['tex:box'] = kernel.list, [':node'] = kernel}}
|
2021-05-01 07:57:03 +02:00
|
|
|
return result, result
|
|
|
|
end
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == sub_mlist_t then
|
2021-04-19 13:30:54 +02:00
|
|
|
return nodes_to_table(kernel.list, cur_style)
|
2021-04-18 15:19:52 +02:00
|
|
|
else
|
|
|
|
error'confusion'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-23 07:50:21 +02:00
|
|
|
local function do_sub_sup(t, core, n, cur_style)
|
2021-04-19 13:30:54 +02:00
|
|
|
local sub = kernel_to_table(n.sub, sub_style(cur_style))
|
|
|
|
local sup = kernel_to_table(n.sup, sup_style(cur_style))
|
2021-04-18 15:19:52 +02:00
|
|
|
if sub then
|
|
|
|
if sup then
|
2021-04-23 07:50:21 +02:00
|
|
|
return {[0] = 'msubsup', t, sub, sup}, core
|
2021-04-18 15:19:52 +02:00
|
|
|
else
|
2021-04-23 07:50:21 +02:00
|
|
|
return {[0] = 'msub', t, sub}, core
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
elseif sup then
|
2021-04-23 07:50:21 +02:00
|
|
|
return {[0] = 'msup', t, sup}, core
|
2021-04-18 15:19:52 +02:00
|
|
|
else
|
2021-04-23 07:50:21 +02:00
|
|
|
return t, core
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-25 15:20:20 +02:00
|
|
|
|
|
|
|
-- If we encounter a . or , after a number, test if it's followed by another number and in that case convert it into a mn
|
|
|
|
local function maybe_to_mn(noad, core)
|
|
|
|
if noad.sub or noad.sup then return end
|
|
|
|
local after = noad.next
|
|
|
|
if not after then return end
|
|
|
|
if after.id ~= noad_t then return end
|
2021-05-11 06:16:36 +02:00
|
|
|
if after.subtype ~= noad_ord then return end
|
2021-04-25 15:20:20 +02:00
|
|
|
after = after.nucleus
|
|
|
|
if not after then return end
|
|
|
|
if after.id ~= math_char_t then return end
|
|
|
|
if not digit_map[remap_lookup[after.fam << 21 | after.char]] then return end
|
|
|
|
core[0] = 'mn'
|
|
|
|
end
|
|
|
|
|
|
|
|
local function noad_to_table(noad, sub, cur_style, mn)
|
2021-05-11 06:16:36 +02:00
|
|
|
local nucleus, core = kernel_to_table(noad.nucleus, sub == noad_over and cur_style//2*2+1 or cur_style)
|
|
|
|
if sub == noad_ord then
|
2021-04-24 20:41:10 +02:00
|
|
|
if core and core[0] == 'mo' then
|
|
|
|
core[0] = 'mi'
|
2021-05-11 06:18:51 +02:00
|
|
|
core.mathvariant = #core == 1 and type(core[0]) == 'string' and utf8.len(core[0]) == 1 and utf8.codepoint(core[0]) < -0x10000 and 'normal' or nil
|
|
|
|
core['tex:class'], core.stretchy, core.lspace, core.rspace = nil
|
2021-04-25 15:20:20 +02:00
|
|
|
end
|
|
|
|
if nucleus == core and #core == 1 then
|
|
|
|
if mn and core[0] == 'mi' and (core[1] == '.' or core[1] == ',') and maybe_to_mn(noad, core) or core[0] == 'mn' then
|
|
|
|
if mn then
|
|
|
|
mn[#mn+1] = core[1]
|
|
|
|
nucleus = do_sub_sup(mn, mn, noad, cur_style)
|
|
|
|
if nucleus == mn then
|
|
|
|
return nil, mn, mn
|
|
|
|
else
|
|
|
|
return nucleus, mn, false
|
|
|
|
end
|
|
|
|
elseif not noad.sub and not noad.sup then
|
|
|
|
return core, core, core
|
|
|
|
end
|
|
|
|
end
|
2021-04-24 20:41:10 +02:00
|
|
|
end
|
2021-05-11 06:16:36 +02:00
|
|
|
elseif sub == noad_op or sub == noad_oplimits or sub == noad_opnolimits or sub == noad_bin or sub == noad_rel or sub == noad_open
|
|
|
|
or sub == noad_close or sub == noad_punct or sub == noad_inner then
|
2021-04-23 07:50:21 +02:00
|
|
|
if not core or not core[0] then
|
2021-04-18 15:19:52 +02:00
|
|
|
-- TODO
|
|
|
|
else
|
2021-04-23 07:50:21 +02:00
|
|
|
core[0] = 'mo'
|
|
|
|
if stretchy[core[1]] then core.stretchy = false end
|
|
|
|
if core.mathvariant == 'normal' then core.mathvariant = nil end
|
2021-05-11 06:18:51 +02:00
|
|
|
core.lspace, core.rspace = 0, 0
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
2021-05-11 06:16:36 +02:00
|
|
|
nucleus['tex:class'] = noad_names[sub]
|
2021-04-19 13:30:54 +02:00
|
|
|
|
2021-05-11 06:16:36 +02:00
|
|
|
if (noad.sup or noad.sub) and (sub == noad_op or sub == noad_oplimits) then
|
|
|
|
nucleus.movablelimits = sub == noad_op
|
2021-04-19 13:30:54 +02:00
|
|
|
local sub = kernel_to_table(noad.sub, sub_style(cur_style))
|
|
|
|
local sup = kernel_to_table(noad.sup, sup_style(cur_style))
|
|
|
|
return {[0] = sup and (sub and 'munderover' or 'mover') or 'munder',
|
|
|
|
nucleus,
|
|
|
|
sub or sup,
|
|
|
|
sub and sup,
|
2021-04-23 07:50:21 +02:00
|
|
|
}, core
|
2021-04-19 13:30:54 +02:00
|
|
|
end
|
2021-05-11 06:16:36 +02:00
|
|
|
elseif sub == noad_under then
|
2021-04-19 13:30:54 +02:00
|
|
|
return {[0] = 'munder',
|
|
|
|
nucleus,
|
|
|
|
{[0] = 'mo', '_',},
|
2021-04-23 07:50:21 +02:00
|
|
|
}, core
|
2021-05-11 06:16:36 +02:00
|
|
|
elseif sub == noad_over then
|
2021-04-19 13:30:54 +02:00
|
|
|
return {[0] = 'mover',
|
|
|
|
nucleus,
|
|
|
|
{[0] = 'mo', '\u{203E}',},
|
2021-04-23 07:50:21 +02:00
|
|
|
}, core
|
2021-05-11 06:16:36 +02:00
|
|
|
elseif sub == noad_vcenter then -- Ignored. Nucleus will need special handling anyway
|
2021-04-19 13:30:54 +02:00
|
|
|
else
|
|
|
|
error[[confusion]]
|
|
|
|
end
|
2021-04-23 07:50:21 +02:00
|
|
|
return do_sub_sup(nucleus, core, noad, cur_style)
|
2021-04-19 13:30:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function accent_to_table(accent, sub, cur_style)
|
2021-04-23 07:50:21 +02:00
|
|
|
local nucleus, core = kernel_to_table(accent.nucleus, cur_style//2*2+1)
|
2021-04-19 19:56:03 +02:00
|
|
|
local top_acc = acc_to_table(accent.accent, cur_style, sub & 1 == 1)
|
|
|
|
local bot_acc = acc_to_table(accent.bot_accent, cur_style, sub & 2 == 2)
|
2021-04-19 13:30:54 +02:00
|
|
|
return {[0] = top_acc and (bot_acc and 'munderover' or 'mover') or 'munder',
|
|
|
|
nucleus,
|
|
|
|
bot_acc or top_acc,
|
|
|
|
bot_acc and top_acc,
|
2021-04-23 07:50:21 +02:00
|
|
|
}, core
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-19 13:30:54 +02:00
|
|
|
local style_table = {
|
|
|
|
display = {displaystyle = "true", scriptlevel = "0"},
|
|
|
|
text = {displaystyle = "false", scriptlevel = "0"},
|
|
|
|
script = {displaystyle = "false", scriptlevel = "1"},
|
|
|
|
scriptscript = {displaystyle = "false", scriptlevel = "2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
style_table.crampeddisplay, style_table.crampedtext,
|
|
|
|
style_table.crampedscript, style_table.crampedscriptscript =
|
|
|
|
style_table.display, style_table.text,
|
|
|
|
style_table.script, style_table.scriptscript
|
|
|
|
|
|
|
|
local function radical_to_table(radical, sub, cur_style)
|
2021-04-18 15:19:52 +02:00
|
|
|
local kind = radical_sub[sub]
|
2021-04-23 07:50:21 +02:00
|
|
|
local nucleus, core = kernel_to_table(radical.nucleus, cur_style//2*2+1)
|
2021-04-18 15:19:52 +02:00
|
|
|
local left = delim_to_table(radical.left)
|
|
|
|
local elem
|
|
|
|
if kind == 'radical' or kind == 'uradical' then
|
|
|
|
-- FIXME: Check that this is really a square root
|
2021-05-24 18:41:12 +02:00
|
|
|
elem, core = {[0] = 'msqrt', nucleus, }, nil
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif kind == 'uroot' then
|
|
|
|
-- FIXME: Check that this is really a root
|
2021-04-23 07:50:21 +02:00
|
|
|
elem, core = {[0] = 'msqrt', nucleus, kernel_to_table(radical.degree)}, nil
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif kind == 'uunderdelimiter' then
|
2021-04-23 07:50:21 +02:00
|
|
|
elem, core = {[0] = 'munder', left, nucleus}, left
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif kind == 'uoverdelimiter' then
|
2021-04-23 07:50:21 +02:00
|
|
|
elem, core = {[0] = 'mover', left, nucleus}, left
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif kind == 'udelimiterunder' then
|
|
|
|
elem = {[0] = 'munder', nucleus, left}
|
|
|
|
elseif kind == 'udelimiterover' then
|
|
|
|
elem = {[0] = 'mover', nucleus, left}
|
|
|
|
else
|
|
|
|
error[[confusion]]
|
|
|
|
end
|
2021-04-23 07:50:21 +02:00
|
|
|
return do_sub_sup(elem, core, radical, cur_style)
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-19 13:30:54 +02:00
|
|
|
local function fraction_to_table(fraction, sub, cur_style)
|
2021-05-13 02:56:37 +02:00
|
|
|
local num, core = kernel_to_table(fraction.num, cur_style + 2 - cur_style//6*2)
|
|
|
|
local denom = kernel_to_table(fraction.denom, cur_style//2*2 + 3 - cur_style//6*2)
|
2021-04-18 15:19:52 +02:00
|
|
|
local left = delim_to_table(fraction.left)
|
|
|
|
local right = delim_to_table(fraction.right)
|
|
|
|
local mfrac = {[0] = 'mfrac',
|
|
|
|
linethickness = fraction.width and fraction.width == 0 and 0 or nil,
|
|
|
|
bevelled = fraction.middle and "true" or nil,
|
|
|
|
num,
|
|
|
|
denom,
|
|
|
|
}
|
|
|
|
if left then
|
|
|
|
return {[0] = 'mrow',
|
|
|
|
left,
|
|
|
|
mfrac,
|
|
|
|
right, -- might be nil
|
|
|
|
}
|
|
|
|
elseif right then
|
|
|
|
return {[0] = 'mrow',
|
|
|
|
mfrac,
|
|
|
|
right,
|
|
|
|
}
|
|
|
|
else
|
2021-04-23 07:50:21 +02:00
|
|
|
return mfrac, core
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-19 13:30:54 +02:00
|
|
|
local function fence_to_table(fence, sub, cur_style)
|
2021-04-28 03:49:31 +02:00
|
|
|
local delim, core = delim_to_table(fence.delim)
|
|
|
|
if delim[0] == 'mo' then
|
|
|
|
delim.fence = 'true'
|
|
|
|
end
|
|
|
|
return delim, core
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-19 21:04:48 +02:00
|
|
|
local function space_to_table(amount, sub, cur_style)
|
|
|
|
if amount == 0 then return end
|
|
|
|
if sub == 99 then -- TODO magic number
|
2021-04-23 07:50:21 +02:00
|
|
|
-- 18*2^16=1179648
|
2021-04-29 02:07:53 +02:00
|
|
|
return {[0] = 'mspace', width = string.format("%.3fem", amount/1179648)}, space_like
|
2021-04-19 21:04:48 +02:00
|
|
|
else
|
2021-04-23 07:50:21 +02:00
|
|
|
-- 65781.76=tex.sp'100bp'/100
|
2021-04-29 02:07:53 +02:00
|
|
|
return {[0] = 'mspace', width = string.format("%.3fpt", amount/65781.76)}, space_like
|
2021-04-19 21:04:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-01 02:44:50 +02:00
|
|
|
local running_length = -1073741824
|
|
|
|
local function rule_to_table(rule, sub, cur_style)
|
|
|
|
local width = string.format("%.3fpt", rule.width/65781.76)
|
|
|
|
local height = rule.height
|
|
|
|
if height == running_length then
|
|
|
|
height = '0.8em'
|
|
|
|
else
|
|
|
|
height = height
|
|
|
|
end
|
|
|
|
local depth = rule.depth
|
|
|
|
if depth == running_length then
|
|
|
|
depth = '0.2em'
|
|
|
|
else
|
|
|
|
depth = depth
|
|
|
|
end
|
|
|
|
return {[0] = 'mspace', mathbackground = 'currentColor', width = width, height = height, depth = depth}, space_like
|
|
|
|
end
|
|
|
|
|
2021-05-07 23:14:20 +02:00
|
|
|
-- The only part which changes the nodelist, we are converting bin into ord
|
|
|
|
-- nodes in the same way TeX would do it later anyway.
|
|
|
|
local function cleanup_mathbin(head)
|
|
|
|
local last = 'open' -- last sub if id was noad_t, left fence acts fakes being a open noad, bin are themselves. Every other noad is ord
|
|
|
|
for n, id, sub in node.traverse(head) do
|
|
|
|
if id == noad_t then
|
2021-05-11 06:16:36 +02:00
|
|
|
if sub == noad_bin then
|
|
|
|
if node.is_node(last) or last == noad_opdisplaylimits
|
|
|
|
or last == noad_oplimits or last == noad_opnolimits
|
|
|
|
or last == noad_rel or last == noad_open or last == noad_punct then
|
|
|
|
n.subtype, last = noad_ord, noad_ord
|
2021-05-07 23:14:20 +02:00
|
|
|
else
|
|
|
|
last = n
|
|
|
|
end
|
|
|
|
else
|
2021-05-11 06:16:36 +02:00
|
|
|
if (sub == noad_rel or sub == noad_close or sub == noad_punct)
|
2021-05-07 23:14:20 +02:00
|
|
|
and node.is_node(last) then
|
2021-05-11 06:16:36 +02:00
|
|
|
last.subtype = noad_ord
|
2021-05-07 23:14:20 +02:00
|
|
|
end
|
|
|
|
last = sub
|
|
|
|
end
|
|
|
|
elseif id == fence_t then
|
|
|
|
if sub == fence_sub.left then
|
2021-05-11 06:16:36 +02:00
|
|
|
last = noad_open
|
2021-05-07 23:14:20 +02:00
|
|
|
else
|
|
|
|
if node.is_node(last) then
|
2021-05-11 06:16:36 +02:00
|
|
|
last.subtype = noad_ord, noad_ord
|
2021-05-07 23:14:20 +02:00
|
|
|
end
|
2021-05-11 06:16:36 +02:00
|
|
|
last = noad_ord
|
2021-05-07 23:14:20 +02:00
|
|
|
end
|
|
|
|
elseif id == fraction_t or id == radical_t or id == accent_t then
|
2021-05-11 06:16:36 +02:00
|
|
|
last = noad_ord
|
2021-05-07 23:14:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if node.is_node(last) then
|
2021-05-11 06:16:36 +02:00
|
|
|
last.subtype = noad_ord
|
2021-05-07 23:14:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-19 13:30:54 +02:00
|
|
|
function nodes_to_table(head, cur_style)
|
2021-05-07 23:14:20 +02:00
|
|
|
cleanup_mathbin(head)
|
2021-04-25 15:20:20 +02:00
|
|
|
local t = {[0] = 'mrow'}
|
2021-04-19 13:30:54 +02:00
|
|
|
local result = t
|
2021-04-19 21:04:48 +02:00
|
|
|
local nonscript
|
2021-05-13 02:56:37 +02:00
|
|
|
local core, last_noad, last_core, mn = space_like, nil, nil, nil
|
2021-04-18 15:19:52 +02:00
|
|
|
for n, id, sub in node.traverse(head) do
|
2021-05-13 02:56:37 +02:00
|
|
|
local new_core, new_mn, new_node, new_noad
|
2021-05-01 02:44:50 +02:00
|
|
|
local props = properties[n]
|
|
|
|
props = props and props.mathml_table
|
2021-04-19 19:56:03 +02:00
|
|
|
if props then
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = props, user_provided
|
2021-04-19 19:56:03 +02:00
|
|
|
elseif id == noad_t then
|
2021-04-25 15:20:20 +02:00
|
|
|
local new_n
|
|
|
|
new_n, new_core, new_mn = noad_to_table(n, sub, cur_style, mn)
|
|
|
|
if new_mn == false then
|
|
|
|
t[#t], new_mn = new_n, nil
|
|
|
|
else
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node = new_n -- might be nil
|
2021-04-25 15:20:20 +02:00
|
|
|
end
|
2021-05-13 02:56:37 +02:00
|
|
|
new_noad = sub
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == accent_t then
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = accent_to_table(n, sub, cur_style)
|
|
|
|
new_noad = noad_ord
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == style_t then
|
2021-04-24 16:53:50 +02:00
|
|
|
if sub ~= cur_style then
|
|
|
|
if #t == 0 then
|
|
|
|
t[0] = 'mstyle'
|
|
|
|
else
|
|
|
|
local new_t = {[0] = 'mstyle'}
|
|
|
|
t[#t+1] = new_t
|
|
|
|
t = new_t
|
|
|
|
end
|
|
|
|
if sub < 2 then
|
|
|
|
t.displaystyle, t.scriptlevel = true, 0
|
|
|
|
else
|
|
|
|
t.displaystyle, t.scriptlevel = false, sub//2 - 1
|
|
|
|
end
|
|
|
|
cur_style = sub
|
2021-04-19 13:30:54 +02:00
|
|
|
end
|
2021-04-23 07:50:21 +02:00
|
|
|
new_core = space_like
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == choice_t then
|
2021-04-19 13:30:54 +02:00
|
|
|
local size = cur_style//2
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = nodes_to_table(n[size == 0 and 'display'
|
2021-04-23 07:50:21 +02:00
|
|
|
or size == 1 and 'text'
|
|
|
|
or size == 2 and 'script'
|
|
|
|
or size == 3 and 'scriptscript'
|
|
|
|
or assert(false)], 2*size), space_like
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == radical_t then
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = radical_to_table(n, sub, cur_style)
|
|
|
|
new_noad = noad_ord
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == fraction_t then
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = fraction_to_table(n, sub, cur_style)
|
|
|
|
new_noad = noad_inner
|
2021-04-18 15:19:52 +02:00
|
|
|
elseif id == fence_t then
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = fence_to_table(n, sub, cur_style)
|
|
|
|
local class = n.class
|
|
|
|
new_noad = class >= 0 and class or sub == fence_sub.left and noad_open or noad_close
|
2021-04-19 21:04:48 +02:00
|
|
|
elseif id == kern_t then
|
|
|
|
if not nonscript then
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = space_to_table(n.kern, sub, cur_style)
|
2021-04-19 21:04:48 +02:00
|
|
|
end
|
|
|
|
elseif id == glue_t then
|
|
|
|
if cur_style >= 4 or not nonscript then
|
|
|
|
if sub == 98 then -- TODO magic number
|
|
|
|
nonscript = true
|
|
|
|
else
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = space_to_table(n.width, sub, cur_style)
|
2021-04-19 21:04:48 +02:00
|
|
|
end
|
|
|
|
end
|
2021-05-01 02:44:50 +02:00
|
|
|
elseif id == rule_t then
|
2021-05-13 02:56:37 +02:00
|
|
|
new_node, new_core = rule_to_table(n, sub, cur_style)
|
2021-05-01 02:44:50 +02:00
|
|
|
-- elseif id == disc_t then -- Uncommon, does not play nicely with math mode and no sensible mapping anyway
|
|
|
|
end -- The other possible ids are whatsit, penalty, adjust, ins, mark. Ignore them.
|
2021-04-19 21:04:48 +02:00
|
|
|
nonscript = nil
|
2021-04-23 07:50:21 +02:00
|
|
|
if core and new_core ~= space_like then
|
2021-04-27 01:47:42 +02:00
|
|
|
core = core == space_like and new_core or nil
|
2021-04-23 07:50:21 +02:00
|
|
|
end
|
2021-05-13 02:56:37 +02:00
|
|
|
if new_node then
|
|
|
|
if new_noad then
|
|
|
|
local space = last_noad and (cur_style >= 4 and spacing_table_script or spacing_table)[last_noad + 1][new_noad + 1] or 0
|
|
|
|
if assert(space) ~= 0 then
|
|
|
|
if new_core and new_core[0] == 'mo' then
|
|
|
|
new_core.lspace = space
|
|
|
|
elseif last_core and last_core[0] == 'mo' then
|
|
|
|
last_core.rspace = space
|
|
|
|
else
|
|
|
|
t[#t+1] = {[0] = 'mspace', width = space} -- TODO Move into operators whenever possible
|
|
|
|
end
|
|
|
|
end
|
|
|
|
last_noad, last_core = new_noad, new_core
|
|
|
|
elseif new_node[0] ~= 'mspace' or new_node.mathbackground then
|
|
|
|
last_core = nil
|
|
|
|
end
|
|
|
|
t[#t+1] = new_node
|
|
|
|
end
|
2021-04-25 15:20:20 +02:00
|
|
|
mn = new_mn
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
2021-04-27 01:47:42 +02:00
|
|
|
-- In TeX, groups are never space like
|
|
|
|
if core == space_like then
|
|
|
|
core = {[0] = 'mi', intent = '@ignore'}
|
|
|
|
result[#result+1] = core
|
|
|
|
end
|
2021-04-24 20:41:10 +02:00
|
|
|
if t[0] == 'mrow' and #t == 1 then
|
|
|
|
assert(t == result)
|
|
|
|
result = t[1]
|
|
|
|
end
|
2021-04-23 07:50:21 +02:00
|
|
|
return result, core
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-22 23:38:28 +02:00
|
|
|
local function register_remap(family, mapping)
|
|
|
|
family = family << 21
|
|
|
|
for from, to in next, mapping do
|
|
|
|
remap_lookup[family | from] = utf8.char(to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-24 16:53:50 +02:00
|
|
|
local function to_math(root, style)
|
|
|
|
if root[0] == 'mrow' then
|
|
|
|
root[0] = 'math'
|
|
|
|
else
|
|
|
|
root = {[0] = 'math', root}
|
|
|
|
end
|
|
|
|
root.xmlns = 'http://www.w3.org/1998/Math/MathML'
|
|
|
|
root['xmlns:tex'] = 'http://typesetting.eu/2021/LuaMathML'
|
|
|
|
if style < 2 then
|
|
|
|
root.display = 'block'
|
2021-04-19 13:30:54 +02:00
|
|
|
end
|
2021-04-24 16:53:50 +02:00
|
|
|
return root
|
2021-04-18 15:19:52 +02:00
|
|
|
end
|
2021-04-22 23:38:28 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
register_family = register_remap,
|
2021-04-24 16:53:50 +02:00
|
|
|
process = function(head, style) return nodes_to_table(head, style or 2) end,
|
|
|
|
make_root = to_math,
|
2021-04-22 23:38:28 +02:00
|
|
|
}
|