Debian Codegraph @

texmf-dist/tex/lualatex/luamml/luamml-convert.lua

raw
1 --[[
2 This file returns
3 * nodes_to_table (as process)
4 * register_remap (as register_family)
5 * to_math (as make_root)
6
7 nodes_to_table takes the argument (head, cur_style, text_families) and
8 converts nodes/noads to the mathml representation. It calls various subfunctions
9 like kernel_to_table, delim_to_table etc.
10
11 to_math surrounds the result with the <math> element.
12
13 --]]
14
15 local remap_comb = require'luamml-data-combining'
16 local stretchy = require'luamml-data-stretchy'
17 local to_text = require'luamml-lr'
18
19 local properties = node.get_properties_table()
20
21 local hlist_t, kern_t, glue_t, rule_t = node.id'hlist', node.id'kern', node.id'glue', node.id'rule'
22
23 local noad_t, accent_t, style_t, choice_t = node.id'noad', node.id'accent', node.id'style', node.id'choice'
24 local radical_t, fraction_t, fence_t = node.id'radical', node.id'fraction', node.id'fence'
25
26 local math_char_t, sub_box_t, sub_mlist_t = node.id'math_char', node.id'sub_box', node.id'sub_mlist'
27
28 local function invert_table(t)
29 local t_inv = {}
30 for k, v in next, t do
31 t_inv[v] = k
32 end
33 return t_inv
34 end
35
36 local noad_names = node.subtypes'noad'
37
38 --[[ We could determine the noad subtypes dynamically:
39 local noad_sub = invert_table(noad_names)
40 local noad_ord = noad_sub.ord
41 local noad_op = noad_sub.opdisplaylimits
42 local noad_oplimits = noad_sub.oplimits
43 local noad_opnolimits = noad_sub.opnolimits
44 local noad_bin = noad_sub.bin
45 local noad_rel = noad_sub.rel
46 local noad_open = noad_sub.open
47 local noad_close = noad_sub.close
48 local noad_punct = noad_sub.punct
49 local noad_inner = noad_sub.inner
50 local noad_under = noad_sub.under
51 local noad_over = noad_sub.over
52 local noad_vcenter = noad_sub.vcenter
53 -- But the spacing table depends on their specific values anyway, so we just verify the values
54 ]]
55 local noad_ord, noad_op, noad_oplimits, noad_opnolimits = 0, 1, 2, 3
56 local noad_bin, noad_rel, noad_open, noad_close, noad_punct = 4, 5, 6, 7, 8
57 local noad_inner, noad_under, noad_over, noad_vcenter = 9, 10, 11, 12
58
59 for i, n in ipairs{'ord', 'opdisplaylimits', 'oplimits', 'opnolimits', 'bin',
60 'rel', 'open', 'close', 'punct', 'inner', 'under', 'over', 'vcenter'} do
61 assert(noad_names[i-1] == n)
62 end
63 -- Attention, the spacing_table is indexed by subtype+1 since 1-based tables are faster in Lua
64 local spacing_table = {
65 {0 , '0.167em', '0.167em', '0.167em', '0.222em', '0.278em', 0 , 0 , 0 , '0.167em', 0 , 0 , 0 , },
66 {'0.167em', '0.167em', '0.167em', '0.167em', nil , '0.278em', 0 , 0 , 0 , '0.167em', '0.167em', '0.167em', '0.167em', },
67 nil,
68 nil,
69 {'0.222em', '0.222em', '0.222em', '0.222em', nil , nil , '0.222em', nil , nil , '0.222em', '0.222em', '0.222em', '0.222em', },
70 {'0.278em', '0.278em', '0.278em', '0.278em', nil , 0 , '0.278em', 0 , 0 , '0.278em', '0.278em', '0.278em', '0.278em', },
71 {0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
72 {0 , '0.167em', '0.167em', '0.167em', '0.222em', '0.278em', 0 , 0 , 0 , '0.167em', 0 , 0 , 0 , },
73 {'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', },
74 {'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', },
75 nil,
76 nil,
77 nil,
78 }
79 local spacing_table_script = {
80 {0 , '0.167em', '0.167em', '0.167em', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
81 {'0.167em', '0.167em', '0.167em', '0.167em', nil , 0 , 0 , 0 , 0 , 0 , '0.167em', '0.167em', '0.167em', },
82 nil,
83 nil,
84 {0 , 0 , 0 , 0 , nil , nil , 0 , nil , nil , 0 , 0 , 0 , 0 , },
85 {0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
86 {0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
87 {0 , '0.167em', '0.167em', '0.167em', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
88 {0 , 0 , 0 , 0 , nil , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
89 {0 , '0.167em', '0.167em', '0.167em', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
90 nil,
91 nil,
92 nil,
93 }
94 do -- Fill the blanks
95 local st, sts = spacing_table, spacing_table_script
96
97 local st_op, sts_op = st[noad_op+1], sts[noad_op+1]
98 st[noad_oplimits+1], sts[noad_oplimits+1] = st_op, sts_op
99 st[noad_opnolimits+1], sts[noad_opnolimits+1] = st_op, sts_op
100
101 local st_ord, sts_ord = st[noad_ord+1], sts[noad_ord+1]
102 st[noad_under+1], sts[noad_under+1] = st_ord, sts_ord
103 st[noad_over+1], sts[noad_over+1] = st_ord, sts_ord
104 st[noad_vcenter+1], sts[noad_vcenter+1] = st_ord, sts_ord
105 end
106
107
108 local radical_sub = node.subtypes'radical'
109 local fence_sub = node.subtypes'fence'
110
111 local remap_lookup = setmetatable({}, {__index = function(t, k)
112 local ch = utf8.char(k & 0x1FFFFF)
113 t[k] = ch
114 return ch
115 end})
116 local digit_map = {["0"] = true, ["1"] = true,
117 ["2"] = true, ["3"] = true, ["4"] = true,
118 ["5"] = true, ["6"] = true, ["7"] = true,
119 ["8"] = true, ["9"] = true,}
120
121 local always_mo = {["%"] = true, ["&"] = true, ["."] = true, ["/"] = true,
122 ["\\"] = true, ["¬"] = true, ["′"] = true, ["″"] = true, ["‴"] = true,
123 ["⁗"] = true, ["‵"] = true, ["‶"] = true, ["‷"] = true, ["|"] = true,
124 ["∀"] = true, ["∁"] = true, ["∃"] = true, ["∂"] = true, ["∄"] = true,}
125
126 -- Marker tables replacing the core operator for space like elements
127 local space_like = {}
128
129 local nodes_to_table
130
131 local function sub_style(s) return s//4*2+5 end
132 local function sup_style(s) return s//4*2+4+s%2 end
133
134 -- The _to_table functions generally return a second argument which is
135 -- could be (if it were a <mo>) a core operator of the embellishe operator
136 -- or space_like
137 -- acc_to_table is special since it's return value should
138 -- always be considered a core operator
139
140 -- We ignore large_... since they aren't used for modern fonts
141 local function delim_to_table(delim)
142 if not delim then return end
143 local props = properties[delim]
144 local mathml_core = props and props.mathml_core
145 local mathml_table = props and (props.mathml_table or mathml_core)
146 if mathml_table ~= nil then return mathml_table, mathml_core end
147 local mathml_filter = props and props.mathml_filter -- Kind of pointless since the arguments are literals, but present for consistency
148 local char = delim.small_char
149 if char == 0 then
150 local result = {[0] = 'mspace', width = string.format("%.3fpt", tex.nulldelimiterspace/65781.76)}
151 if mathml_filter then
152 return mathml_filter(result, space_like)
153 else
154 return result, space_like
155 end
156 else
157 local fam = delim.small_fam
158 char = remap_lookup[fam << 21 | char]
159 local result = {[0] = 'mo', char, ['tex:family'] = fam ~= 0 and fam or nil, stretchy = not stretchy[char] or nil, lspace = 0, rspace = 0, [':nodes'] = {delim}, [':actual'] = char}
160 if mathml_filter then
161 return mathml_filter(result, result)
162 else
163 return result, result
164 end
165 end
166 end
167
168 -- Like kernel_to_table but always a math_char_t. Also creating a mo and potentially remapping
169 -- to handle combining chars.
170 -- No lspace or space is set here since these never appear as core operators in an mrow.
171 local function acc_to_table(acc, cur_style, stretch)
172 if not acc then return end
173 local props = properties[acc]
174 local mathml_core = props and props.mathml_core
175 local mathml_table = props and (props.mathml_table or mathml_core)
176 if mathml_table ~= nil then return mathml_table, mathml_core end
177 if acc.id ~= math_char_t then
178 error'confusion'
179 end
180 local mathml_filter = props and props.mathml_filter -- Kind of pointless since the arguments are literals, but present for consistency
181 local fam = acc.fam
182 local char = remap_lookup[fam << 21 | acc.char]
183 char = remap_comb[char] or char
184 if stretch ~= not stretchy[char] then -- Handle nil gracefully in stretchy
185 stretch = nil
186 end
187 local result = {[0] = 'mo', char, ['tex:family'] = fam ~= 0 and fam or nil, stretchy = stretch, [':nodes'] = {acc}, [':actual'] = stretch and char or nil}
188 if mathml_filter then
189 return mathml_filter(result)
190 else
191 return result
192 end
193 end
194
195 local function kernel_to_table(kernel, cur_style, text_families)
196 if not kernel then return end
197 local props = properties[kernel]
198 local mathml_core = props and props.mathml_core
199 local mathml_table = props and (props.mathml_table or mathml_core)
200 if mathml_table ~= nil then return mathml_table, mathml_core end
201 local mathml_filter = props and props.mathml_filter -- Kind of pointless since the arguments are literals, but present for consistency
202 local id = kernel.id
203 if id == math_char_t then
204 local fam = kernel.fam
205 local char = remap_lookup[fam << 21 | kernel.char]
206 local elem = digit_map[char] and 'mn' or 'mi'
207 local result = {[0] = elem,
208 char,
209 ['tex:family'] = fam ~= 0 and fam or nil,
210 mathvariant = utf8.len(char) == 1 and elem == 'mi' and utf8.codepoint(char) < 0x10000 and 'normal' or nil,
211 [':nodes'] = {kernel},
212 }
213 if mathml_filter then
214 -- this applies changes from annotations, for example adding structure node references
215 -- from the options :struct and :structnum
216 return mathml_filter(result, result)
217 else
218 return result, result
219 end
220 elseif id == sub_box_t then
221 local result
222 if kernel.list.id == hlist_t then -- We directly give up for vlists
223 result = to_text(kernel.list.head)
224 else
225 result = {[0] = 'mi', {[0] = 'mglyph', ['tex:box'] = kernel.list, [':nodes'] = {kernel}}}
226 end
227 if mathml_filter then
228 return mathml_filter(result, result)
229 else
230 return result, result
231 end
232 elseif id == sub_mlist_t then
233 if mathml_filter then
234 return mathml_filter(nodes_to_table(kernel.list, cur_style, text_families))
235 else
236 return nodes_to_table(kernel.list, cur_style, text_families)
237 end
238 else
239 error'confusion'
240 end
241 end
242
243 local function do_sub_sup(t, core, n, cur_style, text_families)
244 local sub = kernel_to_table(n.sub, sub_style(cur_style), text_families)
245 local sup = kernel_to_table(n.sup, sup_style(cur_style), text_families)
246 if sub then
247 if sup then
248 return {[0] = 'msubsup', t, sub, sup}, core
249 else
250 return {[0] = 'msub', t, sub}, core
251 end
252 elseif sup then
253 return {[0] = 'msup', t, sup}, core
254 else
255 return t, core
256 end
257 end
258
259
260 -- 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
261 local function maybe_to_mn(noad, core)
262 if noad.sub or noad.sup then return end
263 local after = noad.next
264 if not after then return end
265 if after.id ~= noad_t then return end
266 if after.subtype ~= noad_ord then return end
267 after = after.nucleus
268 if not after then return end
269 if after.id ~= math_char_t then return end
270 if not digit_map[remap_lookup[after.fam << 21 | after.char]] then return end
271 core[0] = 'mn'
272 end
273
274 local function noad_to_table(noad, sub, cur_style, joining, bin_replacements, text_families)
275 local nucleus, core = kernel_to_table(noad.nucleus, sub == noad_over and cur_style//2*2+1 or cur_style, text_families)
276 if not nucleus then return end
277 if core and core[0] == 'mo' and core.minsize and not core.maxsize then
278 core.maxsize = core.minsize -- This happens when a half-specified delimiter appears alone in a list.
279 -- If it has a minimal size, it should be fixed to that size (since there is nothing bigger in it's list)
280 end
281 if sub == noad_ord and not (bin_replacements[node.direct.todirect(noad)] or (nucleus == core and #core == 1 and always_mo[core[1]])) then
282 if core and core[0] == 'mo' then
283 core['tex:class'] = nil
284 if not core.minsize and not core.movablelimits then
285 core[0] = 'mi'
286 core.movablelimits = nil
287 core.mathvariant = #core == 1 and type(core[1]) == 'string' and utf8.len(core[1]) == 1 and utf8.codepoint(core[1]) < 0x10000 and 'normal' or nil
288 core.stretchy, core.lspace, core.rspace = nil
289 end
290 end
291 if nucleus == core and #core == 1 then
292 if joining and joining[0] == 'mn' and core[0] == 'mi' and (core[1] == '.' or core[1] == ',') and maybe_to_mn(noad, core)
293 or core[0] == 'mn' or text_families[core['tex:family'] or 0] then
294 if joining and core[0] == joining[0] and core['tex:family'] == joining['tex:family'] then
295 joining[#joining+1] = core[1]
296 local cnodes = core[':nodes']
297 if cnodes then -- very likely
298 local jnodes = joining[':nodes']
299 if jnodes then -- very likely
300 table.move(cnodes, 1, #cnodes, #jnodes+1, jnodes)
301 else
302 joining[':nodes'] = cnodes
303 end
304 end
305 nucleus = do_sub_sup(joining, joining, noad, cur_style, text_families)
306 if nucleus == joining then
307 return nil, joining, joining
308 else
309 return nucleus, joining, false
310 end
311 elseif not noad.sub and not noad.sup then
312 return core, core, core
313 end
314 end
315 end
316 elseif sub == noad_op or sub == noad_oplimits or sub == noad_opnolimits or sub == noad_bin or sub == noad_rel or sub == noad_open
317 or sub == noad_close or sub == noad_punct or sub == noad_inner or sub == noad_ord then
318 if not core or not core[0] then
319 -- TODO
320 else
321 core[0] = 'mo'
322 if not core.minsize then
323 if stretchy[core[1]] then core.stretchy = false end
324 end
325 if core.mathvariant == 'normal' then core.mathvariant = nil end
326 core.lspace, core.rspace = 0, 0
327 end
328 nucleus['tex:class'] = noad_names[sub]
329
330 if (noad.sup or noad.sub) and (sub == noad_op or sub == noad_oplimits) then
331 if core and core[0] == 'mo' then core.movablelimits = sub == noad_op end
332 local sub = kernel_to_table(noad.sub, sub_style(cur_style), text_families)
333 local sup = kernel_to_table(noad.sup, sup_style(cur_style), text_families)
334 return {[0] = sup and (sub and 'munderover' or 'mover') or 'munder',
335 nucleus,
336 sub or sup,
337 sub and sup,
338 }, core
339 end
340 elseif sub == noad_under then
341 return {[0] = 'munder',
342 nucleus,
343 {[0] = 'mo', '_',},
344 }, core
345 elseif sub == noad_over then
346 return {[0] = 'mover',
347 nucleus,
348 {[0] = 'mo', '\u{203E}',},
349 }, core
350 elseif sub == noad_vcenter then -- Ignored. Nucleus will need special handling anyway
351 else
352 error[[confusion]]
353 end
354 return do_sub_sup(nucleus, core, noad, cur_style, text_families)
355 end
356
357 local function accent_to_table(accent, sub, cur_style, text_families)
358 local nucleus, core = kernel_to_table(accent.nucleus, cur_style//2*2+1, text_families)
359 local top_acc = acc_to_table(accent.accent, cur_style, sub & 1 == 0)
360 local bot_acc = acc_to_table(accent.bot_accent, cur_style, sub & 2 == 0)
361 local with_accents = {[0] = top_acc and (bot_acc and 'munderover' or 'mover') or 'munder',
362 nucleus,
363 bot_acc or top_acc,
364 bot_acc and top_acc,
365 }
366 return do_sub_sup(with_accents, core, accent, cur_style, text_families)
367 end
368
369 local style_table = {
370 display = {displaystyle = "true", scriptlevel = "0"},
371 text = {displaystyle = "false", scriptlevel = "0"},
372 script = {displaystyle = "false", scriptlevel = "1"},
373 scriptscript = {displaystyle = "false", scriptlevel = "2"},
374 }
375
376 style_table.crampeddisplay, style_table.crampedtext,
377 style_table.crampedscript, style_table.crampedscriptscript =
378 style_table.display, style_table.text,
379 style_table.script, style_table.scriptscript
380
381 local function radical_to_table(radical, sub, cur_style, text_families)
382 local kind = radical_sub[sub]
383 local nucleus, core = kernel_to_table(radical.nucleus, cur_style//2*2+1, text_families)
384 local left = delim_to_table(radical.left)
385 local elem
386 if kind == 'radical' or kind == 'uradical' then
387 -- FIXME: Check that this is really a square root
388 elem, core = {[0] = 'msqrt', nucleus, [':nodes'] = left[':nodes'], [':artifact'] = true}, nil
389 elseif kind == 'uroot' then
390 -- FIXME: Check that this is really a root
391 -- UF 2024-12-04: force use of only one return value
392 elem, core = {[0] = 'mroot', nucleus, (kernel_to_table(radical.degree, 7, text_families)), [':nodes'] = left[':nodes'], [':artifact'] = true}, nil
393 elseif kind == 'uunderdelimiter' then
394 elem, core = {[0] = 'munder', left, nucleus}, left
395 elseif kind == 'uoverdelimiter' then
396 elem, core = {[0] = 'mover', left, nucleus}, left
397 elseif kind == 'udelimiterunder' then
398 elem = {[0] = 'munder', nucleus, left}
399 elseif kind == 'udelimiterover' then
400 elem = {[0] = 'mover', nucleus, left}
401 else
402 error[[confusion]]
403 end
404 return do_sub_sup(elem, core, radical, cur_style, text_families)
405 end
406
407 local function fraction_to_table(fraction, sub, cur_style, text_families)
408 local num, core = kernel_to_table(fraction.num, cur_style + 2 - cur_style//6*2, text_families)
409 local denom = kernel_to_table(fraction.denom, cur_style//2*2 + 3 - cur_style//6*2, text_families)
410 local left = delim_to_table(fraction.left)
411 local right = delim_to_table(fraction.right)
412 local mfrac = {[0] = 'mfrac',
413 [':nodes'] = {fraction},
414 [':artifact'] = true,
415 linethickness = fraction.width and fraction.width == 0 and 0 or nil,
416 bevelled = fraction.middle and "true" or nil,
417 num,
418 denom,
419 }
420 if left then
421 return {[0] = 'mrow',
422 left,
423 mfrac,
424 right, -- might be nil
425 }
426 elseif right then
427 return {[0] = 'mrow',
428 mfrac,
429 right,
430 }
431 else
432 return mfrac, core
433 end
434 end
435
436 local function fence_to_table(fence, sub, cur_style)
437 local delim, core = delim_to_table(fence.delim)
438 if core[0] ~= 'mo' then
439 return delim, core
440 end
441 core.fence, core.symmetric = 'true', 'true'
442 local options = fence.options
443 local axis
444 if fence.height ~= 0 or fence.depth ~= 0 then
445 axis = 0xA == options & 0xA
446 local exact = 0x18 == options & 0x18
447 -- We treat them always as exact. mpadded would allow us to support
448 -- non-exact ones too and I will implement that if I ever encounter
449 -- someone who does that intentionally. Until then, we warn people
450 -- since such fences are absurd.
451 if not exact then
452 texio.write_nl'luamml: The document uses a fence with \z
453 explicit dimensions but without the "exact" option. \z
454 This is probably a mistake.'
455 end
456 core.minsize = string.format("%.3fpt", (fence.height + fence.depth)/65781.76)
457 core.maxsize = core.minsize
458 else
459 axis = 0xC ~= options & 0xC
460 end
461 if not axis then
462 texio.write_nl'luamml: Baseline centered fence will be centered around math axis instead'
463 end
464 return delim, core
465 end
466
467 local function space_to_table(amount, sub, cur_style)
468 if amount == 0 then return end
469 if sub == 99 then -- TODO magic number
470 -- 18*2^16=1179648
471 return {[0] = 'mspace', width = string.format("%.3fem", amount/1179648)}, space_like
472 else
473 -- 65781.76=tex.sp'100bp'/100
474 return {[0] = 'mspace', width = string.format("%.3fpt", amount/65781.76)}, space_like
475 end
476 end
477
478 local running_length = -1073741824
479 local function rule_to_table(rule, sub, cur_style)
480 local width = string.format("%.3fpt", rule.width/65781.76)
481 local height = rule.height
482 if height == running_length then
483 height = '0.8em'
484 else
485 height = string.format("%.3fpt", height/65781.76)
486 end
487 local depth = rule.depth
488 if depth == running_length then
489 depth = '0.2em'
490 else
491 depth = string.format("%.3fpt", depth/65781.76)
492 end
493 return {[0] = 'mspace', mathbackground = 'currentColor', width = width, height = height, depth = depth}, space_like
494 end
495
496 -- The only part which changes the nodelist, we are converting bin into ord
497 -- nodes in the same way TeX would do it later anyway.
498 local function cleanup_mathbin(head)
499 local replacements = {}
500 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
501 for n, id, sub in node.traverse(head) do
502 if id == noad_t then
503 if sub == noad_bin then
504 if node.is_node(last) or last == noad_opdisplaylimits
505 or last == noad_oplimits or last == noad_opnolimits
506 or last == noad_rel or last == noad_open or last == noad_punct then
507 replacements[node.direct.todirect(n)] = true
508 n.subtype, last = noad_ord, noad_ord
509 else
510 last = n
511 end
512 else
513 if (sub == noad_rel or sub == noad_close or sub == noad_punct)
514 and node.is_node(last) then
515 replacements[node.direct.todirect(last)] = true
516 last.subtype = noad_ord
517 end
518 last = sub
519 end
520 elseif id == fence_t then
521 if sub == fence_sub.left then
522 last = noad_open
523 else
524 if node.is_node(last) then
525 replacements[node.direct.todirect(last)] = true
526 last.subtype = noad_ord, noad_ord
527 end
528 last = noad_ord
529 end
530 elseif id == fraction_t or id == radical_t or id == accent_t then
531 last = noad_ord
532 end
533 end
534 if node.is_node(last) then
535 replacements[node.direct.todirect(last)] = true
536 last.subtype = noad_ord
537 end
538 return replacements
539 end
540
541 local function has_relevant_attributes(t)
542 for k in next, t do
543 if type(k) == 'string' and not string.find(k, ':') and k ~= 'xmlns' then
544 return true
545 end
546 end
547 return false
548 end
549
550 function nodes_to_table(head, cur_style, text_families)
551 local bin_replacements = cleanup_mathbin(head)
552 local t = {[0] = 'mrow'}
553 local result = t
554 local nonscript
555 local core, last_noad, last_core, joining = space_like, nil, nil, nil
556 for n, id, sub in node.traverse(head) do
557 local new_core, new_joining, new_node, new_noad
558 local props = properties[n]
559 local mathml_core = props and props.mathml_core
560 local mathml_table = props and (props.mathml_table or mathml_core)
561 if mathml_table ~= nil then
562 new_node, new_core = mathml_table, mathml_core
563 elseif id == noad_t then
564 local new_n
565 new_n, new_core, new_joining = noad_to_table(n, sub, cur_style, joining, bin_replacements, text_families)
566 if new_joining == false then
567 t[#t], new_joining = new_n, nil
568 else
569 new_node = new_n -- might be nil
570 end
571 new_noad = sub
572 elseif id == accent_t then
573 new_node, new_core = accent_to_table(n, sub, cur_style, text_families)
574 new_noad = noad_ord
575 elseif id == style_t then
576 if sub ~= cur_style then
577 if #t == 0 then
578 t[0] = 'mstyle'
579 else
580 local new_t = {[0] = 'mstyle'}
581 t[#t+1] = new_t
582 t = new_t
583 end
584 if sub < 2 then
585 t.displaystyle, t.scriptlevel = true, 0
586 else
587 t.displaystyle, t.scriptlevel = false, sub//2 - 1
588 end
589 cur_style = sub
590 end
591 new_core = space_like
592 elseif id == choice_t then
593 local size = cur_style//2
594 new_node, new_core = nodes_to_table(n[size == 0 and 'display'
595 or size == 1 and 'text'
596 or size == 2 and 'script'
597 or size == 3 and 'scriptscript'
598 or assert(false)], 2*size, text_families), space_like
599 elseif id == radical_t then
600 new_node, new_core = radical_to_table(n, sub, cur_style, text_families)
601 new_noad = noad_ord
602 elseif id == fraction_t then
603 new_node, new_core = fraction_to_table(n, sub, cur_style, text_families)
604 new_noad = noad_inner
605 elseif id == fence_t then
606 new_node, new_core = fence_to_table(n, sub, cur_style)
607 local class = n.class
608 new_noad = class >= 0 and class or sub == fence_sub.left and noad_open or noad_close
609 elseif id == kern_t then
610 if not nonscript then
611 new_node, new_core = space_to_table(n.kern, sub, cur_style)
612 end
613 elseif id == glue_t then
614 if cur_style >= 4 or not nonscript then
615 if sub == 98 then -- TODO magic number
616 nonscript = true
617 else
618 new_node, new_core = space_to_table(n.width, sub, cur_style)
619 end
620 end
621 elseif id == rule_t then
622 new_node, new_core = rule_to_table(n, sub, cur_style)
623 -- elseif id == disc_t then -- Uncommon, does not play nicely with math mode and no sensible mapping anyway
624 end -- The other possible ids are whatsit, penalty, adjust, ins, mark. Ignore them.
625 nonscript = nil
626 if core and new_core ~= space_like then
627 core = core == space_like and new_core or nil
628 end
629 if new_node then
630 if new_noad then
631 local space = last_noad and (cur_style >= 4 and spacing_table_script or spacing_table)[last_noad + 1][new_noad + 1] or 0
632 if assert(space) ~= 0 then
633 if new_core and new_core[0] == 'mo' then
634 new_core.lspace = space
635 elseif last_core and last_core[0] == 'mo' then
636 last_core.rspace = space
637 else
638 t[#t+1] = {[0] = 'mspace', width = space} -- TODO Move into operators whenever possible
639 end
640 end
641 last_noad, last_core = new_noad, new_core
642 elseif new_node[0] ~= 'mspace' or new_node.mathbackground then
643 last_core = nil
644 end
645 -- Omit completely empty mrows.
646 if new_node[0] ~= 'mrow' or #new_node > 0 or next(new_node) ~= 0 or next(new_node, 0) ~= nil then
647 t[#t+1] = new_node
648 end
649 end
650 joining = new_joining
651 end
652 if t[0] == 'mrow' and #t == 1 and not has_relevant_attributes(t) then
653 assert(t == result)
654 result = t[1]
655 end
656 local mathml_filter = props and props.mathml_filter
657 if mathml_filter then
658 return mathml_filter(result, core)
659 else
660 return result, core
661 end
662 end
663
664 local function register_remap(family, mapping)
665 family = family << 21
666 for from, to in next, mapping do
667 remap_lookup[family | from] = utf8.char(to)
668 end
669 end
670
671 -- 2025-05-30: Check if the outer outer mrow should be replaced by math
672 -- as firefox doesn't properly support tagging-project#856.
673
674 local function to_math(root, style)
675 if root[0] == 'mrow' then
676 root[0] = 'math'
677 else
678 root = {[0] = 'math', root}
679 end
680 root.xmlns = 'http://www.w3.org/1998/Math/MathML'
681 root['xmlns:tex'] = 'http://typesetting.eu/2021/LuaMathML'
682 if style < 2 then
683 root.display = 'block'
684 end
685 return root
686 end
687
688 return {
689 register_family = register_remap,
690 process = function(head, style, families) return nodes_to_table(head, style or 2, families) end,
691 make_root = to_math,
692 has_relevant_attributes = has_relevant_attributes,
693 }