Add support for mathml_filter

This commit is contained in:
Marcel Fabian Krüger 2021-05-27 05:03:07 +02:00
parent ebc813d299
commit c9904b3bc5

View File

@ -121,41 +121,61 @@ local function sup_style(s) return s//4*2+4+s%2 end
-- We ignore large_... since they aren't used for modern fonts -- We ignore large_... since they aren't used for modern fonts
local function delim_to_table(delim) local function delim_to_table(delim)
if not delim then return end if not delim then return end
local props = properties[delim] props = props and props.mathml_table local props = properties[delim]
if props then return props end local mathml_table = props and props.mathml_table
if mathml_table then return mathml_table end
local mathml_filter = props and props.mathml_filter -- Kind of pointless since the arguments are literals, but present for consistency
local char = delim.small_char local char = delim.small_char
if char == 0 then if char == 0 then
return {[0] = 'mspace', width = string.format("%.3fpt", tex.nulldelimiterspace/65781.76)}, space_like local result = {[0] = 'mspace', width = string.format("%.3fpt", tex.nulldelimiterspace/65781.76)}
if mathml_filter then
return mathml_filter(result, space_like)
else
return result, space_like
end
else else
local fam = delim.small_fam local fam = delim.small_fam
char = remap_lookup[fam << 21 | char] char = remap_lookup[fam << 21 | char]
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 } 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 }
if mathml_filter then
return mathml_filter(result, result)
else
return result, result return result, result
end end
end end
end
-- Like kernel_to_table but always a math_char_t. Also creating a mo and potentially remapping to handle combining chars. -- 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. -- No lspace or space is set here since these never appear as core operators in an mrow.
local function acc_to_table(acc, cur_style, stretch) local function acc_to_table(acc, cur_style, stretch)
if not acc then return end if not acc then return end
local props = properties[acc] props = props and props.mathml_table local props = properties[acc]
if props then return props end local mathml_table = props and props.mathml_table
if mathml_table then return mathml_table end
if acc.id ~= math_char_t then if acc.id ~= math_char_t then
error'confusion' error'confusion'
end end
local mathml_filter = props and props.mathml_filter -- Kind of pointless since the arguments are literals, but present for consistency
local fam = acc.fam local fam = acc.fam
local char = remap_lookup[fam << 21 | acc.char] local char = remap_lookup[fam << 21 | acc.char]
char = remap_comb[char] or char char = remap_comb[char] or char
if stretch ~= not stretchy[char] then -- Handle nil gracefully in stretchy if stretch ~= not stretchy[char] then -- Handle nil gracefully in stretchy
stretch = nil stretch = nil
end end
return {[0] = 'mo', char, ['tex:family'] = fam ~= 0 and fam or nil, stretchy = stretch, [':node'] = acc} local result = {[0] = 'mo', char, ['tex:family'] = fam ~= 0 and fam or nil, stretchy = stretch, [':node'] = acc}
if mathml_filter then
return mathml_filter(result)
else
return result
end
end end
local function kernel_to_table(kernel, cur_style) local function kernel_to_table(kernel, cur_style)
if not kernel then return end if not kernel then return end
local props = properties[kernel] props = props and props.mathml_table local props = properties[kernel]
if props then return props, user_provided end local mathml_table = props and props.mathml_table
if mathml_table then return mathml_table, user_provided end
local mathml_filter = props and props.mathml_filter -- Kind of pointless since the arguments are literals, but present for consistency
local id = kernel.id local id = kernel.id
if id == math_char_t then if id == math_char_t then
local fam = kernel.fam local fam = kernel.fam
@ -167,17 +187,28 @@ local function kernel_to_table(kernel, cur_style)
mathvariant = utf8.len(char) == 1 and elem == 'mi' and utf8.codepoint(char) < 0x10000 and 'normal' or nil, mathvariant = utf8.len(char) == 1 and elem == 'mi' and utf8.codepoint(char) < 0x10000 and 'normal' or nil,
[':node'] = kernel, [':node'] = kernel,
} }
if mathml_filter then
return mathml_filter(result, result)
else
return result, result return result, result
end
elseif id == sub_box_t then elseif id == sub_box_t then
if kernel.list.id == hlist_t then -- We directly give up for vlists if kernel.list.id == hlist_t then -- We directly give up for vlists
local result = to_text(kernel.list.head) local result = to_text(kernel.list.head)
return result, result
else else
local result = {[0] = 'mi', {[0] = 'mglyph', ['tex:box'] = kernel.list, [':node'] = kernel}} local result = {[0] = 'mi', {[0] = 'mglyph', ['tex:box'] = kernel.list, [':node'] = kernel}}
end
if mathml_filter then
return mathml_filter(result, result)
else
return result, result return result, result
end end
elseif id == sub_mlist_t then elseif id == sub_mlist_t then
if mathml_filter then
return mathml_filter(nodes_to_table(kernel.list, cur_style))
else
return nodes_to_table(kernel.list, cur_style) return nodes_to_table(kernel.list, cur_style)
end
else else
error'confusion' error'confusion'
end end
@ -436,9 +467,9 @@ function nodes_to_table(head, cur_style)
for n, id, sub in node.traverse(head) do for n, id, sub in node.traverse(head) do
local new_core, new_mn, new_node, new_noad local new_core, new_mn, new_node, new_noad
local props = properties[n] local props = properties[n]
props = props and props.mathml_table local mathml_table = props and props.mathml_table
if props then if mathml_table then
new_node, new_core = props, user_provided new_node, new_core = mathml_table, user_provided
elseif id == noad_t then elseif id == noad_t then
local new_n local new_n
new_n, new_core, new_mn = noad_to_table(n, sub, cur_style, mn) new_n, new_core, new_mn = noad_to_table(n, sub, cur_style, mn)
@ -534,8 +565,13 @@ function nodes_to_table(head, cur_style)
assert(t == result) assert(t == result)
result = t[1] result = t[1]
end end
local mathml_filter = props and props.mathml_filter
if mathml_filter then
return mathml_filter(result, core)
else
return result, core return result, core
end end
end
local function register_remap(family, mapping) local function register_remap(family, mapping)
family = family << 21 family = family << 21