From 8be2760919141ea5b8f38bf046193fc81383ef53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Thu, 22 Apr 2021 23:38:28 +0200 Subject: [PATCH] Don't require unicode-math --- mlist_to_mml.lua | 41 ++++++++++++++++++++++++++++++++++------- test_tex.lua | 24 ++++++++++++++++++++---- test_tex.tex | 4 +++- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/mlist_to_mml.lua b/mlist_to_mml.lua index 67ee947..a1ba373 100644 --- a/mlist_to_mml.lua +++ b/mlist_to_mml.lua @@ -14,6 +14,16 @@ local noad_sub = node.subtypes'noad' local radical_sub = node.subtypes'radical' local fence_sub = node.subtypes'fence' +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,} + local nodes_to_table local function sub_style(s) return s//4*2+5 end @@ -25,7 +35,8 @@ local function delim_to_table(delim) local props = properties[delim] props = props and props.mathml_table if props then return props end local fam = delim.small_fam - return {[0] = 'mo', utf8.char(delim.small_char), ['tex:family'] = fam ~= 0 and fam or nil, stretchy = not stretchy[delim.small_char] or nil } + local char = remap_lookup[fam << 21 | delim.small_char] + return {[0] = 'mo', char, ['tex:family'] = fam ~= 0 and fam or nil, stretchy = not stretchy[char] or nil } end -- Like kernel_to_table but always a math_char_t. Also creating a mo and potentially remapping to handle combining chars @@ -36,9 +47,9 @@ local function acc_to_table(acc, cur_style, stretch) if acc.id ~= math_char_t then error'confusion' end - local char = utf8.char(acc.char) - char = remap_comb[char] or char local fam = acc.fam + local char = remap_lookup[fam << 21 | acc.char] + char = remap_comb[char] or char if stretch ~= not stretchy[char] then -- Handle nil gracefully in stretchy stretch = nil end @@ -51,10 +62,14 @@ local function kernel_to_table(kernel, cur_style) if props then return props end local id = kernel.id if id == math_char_t then - local char = kernel.char - local elem = char >= 0x30 and char < 0x39 and 'mn' or 'mi' local fam = kernel.fam - return {[0] = elem, utf8.char(char), ['tex:family'] = fam ~= 0 and fam or nil, mathvariant = char < 0x10000 and 'normal' or nil } + local char = remap_lookup[fam << 21 | kernel.char] + local elem = digit_map[char] and 'mn' or 'mi' + return {[0] = elem, + char, + ['tex:family'] = fam ~= 0 and fam or nil, + mathvariant = #char == 1 and utf8.codepoint(char) < 0x10000 and 'normal' or nil + } elseif id == sub_box_t then return {[0] = 'mi', {[0] = 'mglyph', ['tex:box'] = kernel.list}} elseif id == sub_mlist_t then @@ -269,7 +284,14 @@ function nodes_to_table(head, cur_style) return result end -return function(head, style) +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 + +local function to_mml(head, style) local result = nodes_to_table(head, style or 0) result[0] = 'math' result.xmlns = 'http://www.w3.org/1998/Math/MathML' @@ -279,3 +301,8 @@ return function(head, style) end return result end + +return { + register_family = register_remap, + process = to_mml, +} diff --git a/test_tex.lua b/test_tex.lua index 50b1b1a..697172b 100644 --- a/test_tex.lua +++ b/test_tex.lua @@ -1,14 +1,30 @@ local inspect = require'inspect' local function show(t) return print(inspect(t)) end -local mlist_to_table = require'mlist_to_mml' +local mlist_to_mml = require'mlist_to_mml' +local process_mlist = mlist_to_mml.process +local register_family = mlist_to_mml.register_family + +local mappings = require'remap' local write_xml = require'write_xml' +local funcid = luatexbase.new_luafunction'RegisterFamilyMapping' +token.set_lua('RegisterFamilyMapping', funcid, 'protected') +lua.get_functions_table()[funcid] = function() + local fam = token.scan_int() + local mapping = token.scan_string() + if mappings[mapping] then + register_family(fam, mappings[mapping]) + else + tex.error(string.format('Unknown font mapping %q', mapping)) + end +end + luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist, style) - print'\n\n' - local xml = mlist_to_table(mlist, style == 'display' and 2 or 0) + print'' + local xml = process_mlist(mlist, style == 'display' and 2 or 0) print(write_xml(xml)) -- print(write_xml(xml, '\n')) - print'\n' + print'' return true end, 'dump_list') diff --git a/test_tex.tex b/test_tex.tex index 73d8317..bd945a2 100644 --- a/test_tex.tex +++ b/test_tex.tex @@ -1,6 +1,8 @@ \documentclass{article} -\usepackage{unicode-math} +% \usepackage{unicode-math} \directlua{require'test_tex'} +\RegisterFamilyMapping\symsymbols{oms} +\RegisterFamilyMapping\symletters{oml} \begin{document} \[ ax^2+b+c=0