local scriptType = {
upright = 1,
boldScript = 2,
boldItalic = 3,
bold = 5,
fraktur = 6,
script = 7,
initial = 8,
monospace = 9,
sansSerif = 10,
doubleStruck = 11,
italic = 14,
boldFraktur = 15,
sansSerifBoldItalic = 16,
sansSerifItalic = 17,
boldSansSerif = 18,
}
local mathVariantAttribute = {
normal = scriptType.upright,
["bold-script"] = scriptType.boldScript, ["bold-italic"] = scriptType.boldItalic, bold = scriptType.bold, fraktur = scriptType.fraktur, script = scriptType.script, monospace = scriptType.monospace, ["sans-serif"] = scriptType.sansSerif, ["double-struck"] = scriptType.doubleStruck, italic = scriptType.italic, ["bold-fraktur"] = scriptType.boldFraktur, ["sans-serif-bold-italic"] = scriptType.sansSerifBoldItalic, ["sans-serif-italic"] = scriptType.sansSerifItalic, ["bold-sans-serif"] = scriptType.boldSansSerif, }
local function mathVariantToScriptType (attr)
if mathVariantAttribute[attr] then
return mathVariantAttribute[attr]
end
end
local mathScriptConversionTable = {
latinUpper = {
[scriptType.upright] = function (codepoint)
return codepoint
end,
[scriptType.boldScript] = function (codepoint)
return codepoint + 0x1D4D0 - 0x41
end,
[scriptType.boldItalic] = function (codepoint)
return codepoint + 0x1D468 - 0x41
end,
[scriptType.bold] = function (codepoint)
return codepoint + 0x1D400 - 0x41
end,
[scriptType.doubleStruck] = function (codepoint)
return codepoint == 0x43 and 0x2102 or codepoint == 0x48 and 0x210D or codepoint == 0x4E and 0x2115 or codepoint == 0x50 and 0x2119 or codepoint == 0x51 and 0x211A or codepoint == 0x52 and 0x211D or codepoint == 0x5A and 0x2124 or codepoint + 0x1D538 - 0x41
end,
[scriptType.fraktur] = function (codepoint)
return codepoint == 0x43 and 0x212D or codepoint == 0x48 and 0x210B or codepoint == 0x49 and 0x2110 or codepoint == 0x52 and 0x211C or codepoint == 0x5A and 0x2128 or codepoint + 0x1D504 - 0x41
end,
[scriptType.script] = function (codepoint)
return codepoint == 0x42 and 0x212C or codepoint == 0x45 and 0x2130 or codepoint == 0x46 and 0x2131 or codepoint == 0x48 and 0x210B or codepoint == 0x49 and 0x2110 or codepoint == 0x4C and 0x2112 or codepoint == 0x4D and 0x2133 or codepoint == 0x52 and 0x211D or codepoint + 0x1D49C - 0x41
end,
[scriptType.monospace] = function (codepoint)
return codepoint + 0x1D670 - 0x41
end,
[scriptType.sansSerif] = function (codepoint)
return codepoint + 0x1D5A0 - 0x41
end,
[scriptType.italic] = function (codepoint)
return codepoint + 0x1D434 - 0x41
end,
[scriptType.boldFraktur] = function (codepoint)
return codepoint + 0x1D56C - 0x41
end,
[scriptType.sansSerifBoldItalic] = function (codepoint)
return codepoint + 0x1D63C - 0x41
end,
[scriptType.sansSerifItalic] = function (codepoint)
return codepoint + 0x1D608 - 0x41
end,
[scriptType.boldSansSerif] = function (codepoint)
return codepoint + 0x1D5D4 - 0x41
end,
},
latinLower = {
[scriptType.upright] = function (codepoint)
return codepoint
end,
[scriptType.boldScript] = function (codepoint)
return codepoint + 0x1D4EA - 0x61
end,
[scriptType.boldItalic] = function (codepoint)
return codepoint + 0x1D482 - 0x61
end,
[scriptType.bold] = function (codepoint)
return codepoint + 0x1D41A - 0x61
end,
[scriptType.doubleStruck] = function (codepoint)
return codepoint + 0x1D552 - 0x61
end,
[scriptType.fraktur] = function (codepoint)
return codepoint + 0x1D51E - 0x61
end,
[scriptType.script] = function (codepoint)
return codepoint == 0x65 and 0x212F or codepoint == 0x67 and 0x210A or codepoint == 0x6F and 0x2134 or codepoint + 0x01D4B6 - 0x61
end,
[scriptType.monospace] = function (codepoint)
return codepoint + 0x1D68A - 0x61
end,
[scriptType.sansSerif] = function (codepoint)
return codepoint + 0x1D5BA - 0x61
end,
[scriptType.italic] = function (codepoint)
return codepoint == 0x68 and 0x210E or codepoint + 0x1D44E - 0x61
end,
[scriptType.boldFraktur] = function (codepoint)
return codepoint + 0x1D586 - 0x61
end,
[scriptType.sansSerifBoldItalic] = function (codepoint)
return codepoint + 0x1D656 - 0x61
end,
[scriptType.sansSerifItalic] = function (codepoint)
return codepoint + 0x1D622 - 0x61
end,
[scriptType.boldSansSerif] = function (codepoint)
return codepoint + 0x1D5EE - 0x61
end,
},
number = {
[scriptType.upright] = function (codepoint)
return codepoint
end,
[scriptType.bold] = function (codepoint)
return codepoint + 0x1D7CE - 0x30
end,
[scriptType.monospace] = function (codepoint)
return codepoint + 0x1D7F6 - 0x30
end,
[scriptType.sansSerif] = function (codepoint)
return codepoint + 0x1D7E2 - 0x30
end,
[scriptType.doubleStruck] = function (codepoint)
return codepoint + 0x1D7D8 - 0x30
end,
[scriptType.boldSansSerif] = function (codepoint)
return codepoint + 0x1D7EC - 0x30
end,
},
greekUpper = {
[scriptType.upright] = function (codepoint)
return codepoint
end,
[scriptType.boldItalic] = function (codepoint)
return codepoint == 0x3F4 and 0x1D72D or codepoint == 0x2207 and 0x1D735 or codepoint + 0x1D71C - 0x391 end,
[scriptType.bold] = function (codepoint)
return codepoint == 0x3F4 and 0x1D6B9 or codepoint == 0x2207 and 0x1D6C1 or codepoint + 0x1D6A8 - 0x391 end,
[scriptType.italic] = function (codepoint)
return codepoint == 0x3F4 and 0x1D6F3 or codepoint == 0x2207 and 0x1D6FB or codepoint + 0x1D6E2 - 0x391 end,
[scriptType.sansSerifBoldItalic] = function (codepoint)
return codepoint == 0x3F4 and 0x1D7A1 or codepoint == 0x2207 and 0x1D7A9 or codepoint + 0x1D790 - 0x391 end,
[scriptType.boldSansSerif] = function (codepoint)
return codepoint == 0x3F4 and 0x1D767 or codepoint == 0x2207 and 0x1D76F or codepoint + 0x1D756 - 0x391 end,
},
greekLower = {
[scriptType.upright] = function (codepoint)
return codepoint
end,
[scriptType.boldItalic] = function (codepoint)
return codepoint == 0x3D1 and 0x1D751 or codepoint == 0x3D5 and 0x1D753 or codepoint == 0x3D6 and 0x1D755 or codepoint == 0x3F0 and 0x1D752 or codepoint == 0x3F1 and 0x1D754 or codepoint == 0x3F5 and 0x1D750 or codepoint + 0x1D736 - 0x3B1 end,
[scriptType.bold] = function (codepoint)
return codepoint == 0x3D1 and 0x1D6DD or codepoint == 0x3D5 and 0x1D6DF or codepoint == 0x3D6 and 0x1D6E1 or codepoint == 0x3F0 and 0x1D6DE or codepoint == 0x3F1 and 0x1D6E0 or codepoint == 0x3F5 and 0x1D6DC or codepoint + 0x1D6C2 - 0x3B1
end,
[scriptType.italic] = function (codepoint)
return codepoint == 0x3D1 and 0x1D717 or codepoint == 0x3D5 and 0x1D719 or codepoint == 0x3D6 and 0x1D71B or codepoint == 0x3F0 and 0x1D718 or codepoint == 0x3F1 and 0x1D71A or codepoint == 0x3F5 and 0x1D716 or codepoint + 0x1D6FC - 0x3B1
end,
[scriptType.sansSerifBoldItalic] = function (codepoint)
return codepoint == 0x3D1 and 0x1D7C5 or codepoint == 0x3D5 and 0x1D7C7 or codepoint == 0x3D6 and 0x1D7C9 or codepoint == 0x3F0 and 0x1D7C6 or codepoint == 0x3F1 and 0x1D7C8 or codepoint == 0x3F5 and 0x1D7C4 or codepoint + 0x1D7AA - 0x3B1 end,
[scriptType.boldSansSerif] = function (codepoint)
return codepoint == 0x3D1 and 0x1D78B or codepoint == 0x3D5 and 0x1D78D or codepoint == 0x3D6 and 0x1D78F or codepoint == 0x3F0 and 0x1D78C or codepoint == 0x3F1 and 0x1D78E or codepoint == 0x3F5 and 0x1D78A or codepoint + 0x1D770 - 0x3B1 end,
},
}
local function convertMathVariantScript (text, script)
local converted = ""
for _, uchr in luautf8.codes(text) do
local dst_char = luautf8.char(uchr)
local converter
if uchr >= 0x41 and uchr <= 0x5A then
converter = mathScriptConversionTable.latinUpper[script]
elseif uchr >= 0x61 and uchr <= 0x7A then
converter = mathScriptConversionTable.latinLower[script]
elseif uchr >= 0x30 and uchr <= 0x39 then
converter = mathScriptConversionTable.number[script]
elseif
uchr >= 0x391
and uchr <= 0x3A9 and uchr ~= 0x3A2 or uchr == 0x3F4
or uchr == 0x2207 then
converter = mathScriptConversionTable.greekUpper[script]
elseif
uchr >= 0x3B1 and uchr <= 0x3C9 or uchr == 0x03D1 or uchr == 0x03D5 or uchr == 0x03D6 or uchr == 0x03F0 or uchr == 0x03F1 or uchr == 0x03F5 then
converter = mathScriptConversionTable.greekLower[script]
end
dst_char = converter and luautf8.char(converter(uchr)) or dst_char
converted = converted .. dst_char
end
return converted
end
return {
mathVariantToScriptType = mathVariantToScriptType,
scriptType = scriptType,
convertMathVariantScript = convertMathVariantScript,
}