| 1 |
local function sub_style(s) return (s >> 1) | 5 end |
| 2 |
local function sup_style(s) return ((s & 4) >> 1) | (s & 1) | 4 end |
| 3 |
local function num_style(s) return s + 2 - s//6 * 2 end |
| 4 |
local function denom_style(s) return (s | 1) + 2 - s//6 * 2 end |
| 5 |
|
| 6 |
local unset_attribute = -0x7FFFFFFF |
| 7 |
|
| 8 |
local style_names = { |
| 9 |
[0] = 'display', |
| 10 |
[1] = 'crampeddisplay', |
| 11 |
[2] = 'text', |
| 12 |
[3] = 'crampedtext', |
| 13 |
[4] = 'script', |
| 14 |
[5] = 'crampedscript', |
| 15 |
[6] = 'scriptscript', |
| 16 |
[7] = 'crampedscriptscript', |
| 17 |
} |
| 18 |
local reverse_styles = {} |
| 19 |
for i=0, 7 do |
| 20 |
reverse_styles[style_names[i]] = i |
| 21 |
end |
| 22 |
|
| 23 |
local mathfamattr = token.create'mathfamattr' |
| 24 |
assert(mathfamattr.cmdname == 'assign_attr') |
| 25 |
local attr = mathfamattr.index |
| 26 |
|
| 27 |
local leftroot_attr = luatexbase.new_attribute'leftroot' |
| 28 |
local uproot_attr = luatexbase.new_attribute'uproot' |
| 29 |
|
| 30 |
local symlummain = token.create'symlummain' |
| 31 |
assert(symlummain.cmdname == 'char_given') |
| 32 |
local main_fam = symlummain.index |
| 33 |
|
| 34 |
local processed_families = {[main_fam] = true} |
| 35 |
|
| 36 |
local parser = require'lua-uni-parse' |
| 37 |
local mathclasses = parser.parse_file('MathClass-15', parser.eol + lpeg.Cg(parser.fields(parser.codepoint_range, lpeg.C(lpeg.S'NABCDFGLOPRSUVX'))), parser.multiset) |
| 38 |
|
| 39 |
-- Store all Lua tables that should be cleaned whenever the mathsetup get changed. |
| 40 |
-- The tables are stored as keys, the values are ignored. |
| 41 |
local mathsize_reset_maps = setmetatable({}, {__mode = 'k'}) |
| 42 |
|
| 43 |
-- Generate a table that is queried by (fam << 2) | (style >> 1) and provides |
| 44 |
-- get_data(font.getfont(fid)) for fid matching the font at fam/style while |
| 45 |
-- aggressively caching everything involved. |
| 46 |
local function family_style_fontdir_map(get_data, default) |
| 47 |
-- The base is a metatable based cache on fontids. This is stable since fontids never change. |
| 48 |
local font_cache = setmetatable({}, {__index = function(t, fid) |
| 49 |
local fontdir = font.getfont(fid) |
| 50 |
if not fontdir then return default end |
| 51 |
local data = get_data(fontdir) |
| 52 |
if data == nil then |
| 53 |
data = default |
| 54 |
end |
| 55 |
t[fid] = data |
| 56 |
return data |
| 57 |
end}) |
| 58 |
local fam_cache = setmetatable({}, {__index = function(t, mathfont_locator) |
| 59 |
local fam = mathfont_locator >> 2 |
| 60 |
local size = mathfont_locator & 3 |
| 61 |
if size ~= 0 then |
| 62 |
size = size - 1 |
| 63 |
end |
| 64 |
local fid = node.family_font(fam, size) |
| 65 |
if fid == 0 then return default end |
| 66 |
local data = font_cache[fid] |
| 67 |
t[mathfont_locator] = data |
| 68 |
return data |
| 69 |
end}) |
| 70 |
mathsize_reset_maps[fam_cache] = true |
| 71 |
return fam_cache |
| 72 |
end |
| 73 |
|
| 74 |
local vs_maps = setmetatable({}, {__index = function(t, vs) |
| 75 |
local map = family_style_fontdir_map(function(fontdir) |
| 76 |
local resources = fontdir.resources |
| 77 |
local variants = resources and resources.variants |
| 78 |
return variants and variants[vs] |
| 79 |
end, {}) |
| 80 |
t[vs] = map |
| 81 |
return map |
| 82 |
end}) |
| 83 |
|
| 84 |
-- We derive mathclasses from the Unicode data file MathClass-15.txt, but some characters can be used |
| 85 |
-- in different ways in math and LaTeX traditionally defines a different class for them by default than |
| 86 |
-- Unicode does. For these we adjust the class here. |
| 87 |
mathclasses[0x2F] = 'N' -- / |
| 88 |
mathclasses[0x5C] = 'N' -- \ |
| 89 |
mathclasses[0x22EF] = 'N' -- ⋯ |
| 90 |
mathclasses[0x2E] = 'N' -- . |
| 91 |
|
| 92 |
-- Integrals and other big operators have the same classes in data files, but we need to tell them apart. |
| 93 |
-- Therefore we have a fixed list of all integral like codepoints here. |
| 94 |
local integral_codepoints = { |
| 95 |
[0x222B] = true, -- ∫ |
| 96 |
[0x222C] = true, -- ∬ |
| 97 |
[0x222D] = true, -- ∭ |
| 98 |
[0x222E] = true, -- ∮ |
| 99 |
[0x222F] = true, -- ∯ |
| 100 |
[0x2230] = true, -- ∰ |
| 101 |
[0x2231] = true, -- ∱ |
| 102 |
[0x2232] = true, -- ∲ |
| 103 |
[0x2233] = true, -- ∳ |
| 104 |
[0x2A0B] = true, -- ⨋ |
| 105 |
[0x2A0C] = true, -- ⨌ |
| 106 |
[0x2A0D] = true, -- ⨍ |
| 107 |
[0x2A0E] = true, -- ⨎ |
| 108 |
[0x2A0F] = true, -- ⨏ |
| 109 |
[0x2A10] = true, -- ⨐ |
| 110 |
[0x2A11] = true, -- ⨑ |
| 111 |
[0x2A12] = true, -- ⨒ |
| 112 |
[0x2A13] = true, -- ⨓ |
| 113 |
[0x2A14] = true, -- ⨔ |
| 114 |
[0x2A15] = true, -- ⨕ |
| 115 |
[0x2A16] = true, -- ⨖ |
| 116 |
[0x2A17] = true, -- ⨗ |
| 117 |
[0x2A18] = true, -- ⨘ |
| 118 |
[0x2A19] = true, -- ⨙ |
| 119 |
[0x2A1A] = true, -- ⨚ |
| 120 |
[0x2A1B] = true, -- ⨛ |
| 121 |
[0x2A1C] = true, -- ⨜ |
| 122 |
} |
| 123 |
|
| 124 |
-- Generally mathematical alphabets in Unicode are sequential blocks which reflect the order of the corresponding non-mathematical characters. |
| 125 |
-- Therefore we can just store the position of the base characters for each style and then access other characters as offsets. |
| 126 |
-- |
| 127 |
-- There are a few exceptions though: |
| 128 |
-- For some greek character, the non-mathematical alphabet has some characters in positions not matching the mathematical alphabets. |
| 129 |
-- This typically happens for variant forms where the non-mathematical slot is occupied by some pre-composed accented glyph. |
| 130 |
-- These we map before processing to be in the position we expect them to be in. |
| 131 |
-- |
| 132 |
-- Special mention: |
| 133 |
-- U+0131 (dotless i), U+0237 (dotless j), U+03DC (capital digamma), and U+03DD (small digamma) |
| 134 |
-- These four need to remapped in some styles but don't fit the general alphabets at all. |
| 135 |
-- There are no expected locations to map them into, so we remap them completely outside the normal |
| 136 |
-- Unicode range and then map the adjusted versions back. |
| 137 |
local pre_replacement = { |
| 138 |
[0x03F4] = 0x03A2, |
| 139 |
[0x2207] = 0x03AA, |
| 140 |
|
| 141 |
[0x2202] = 0x03CA, |
| 142 |
[0x03F5] = 0x03CB, |
| 143 |
[0x03D1] = 0x03CC, |
| 144 |
[0x03F0] = 0x03CD, |
| 145 |
[0x03D5] = 0x03CE, |
| 146 |
[0x03F1] = 0x03CF, |
| 147 |
[0x03D6] = 0x03D0, |
| 148 |
|
| 149 |
[0x0131] = 0x100000, |
| 150 |
[0x0237] = 0x100001, |
| 151 |
[0x03DC] = 0x100002, |
| 152 |
[0x03DD] = 0x100003, |
| 153 |
} |
| 154 |
|
| 155 |
-- More common is the opposite case: Some mathematical characters are not in the spaces normally associated with their alphabets but much lower. |
| 156 |
-- This happens when the corresponding character was already encoded by the time the remaining alphabet got added. |
| 157 |
-- We remap after processing from their expected to their actual places. |
| 158 |
local post_replacement = { |
| 159 |
[0x1D49D] = 0x212C, |
| 160 |
[0x1D4A0] = 0x2130, |
| 161 |
[0x1D4A1] = 0x2131, |
| 162 |
[0x1D4A3] = 0x210B, |
| 163 |
[0x1D4A4] = 0x2110, |
| 164 |
[0x1D4A7] = 0x2112, |
| 165 |
[0x1D4A8] = 0x2133, |
| 166 |
[0x1D4AD] = 0x211B, |
| 167 |
|
| 168 |
[0x1D506] = 0x212D, |
| 169 |
[0x1D50B] = 0x210C, |
| 170 |
[0x1D50C] = 0x2111, |
| 171 |
[0x1D515] = 0x211C, |
| 172 |
[0x1D51D] = 0x2128, |
| 173 |
|
| 174 |
[0x1D53A] = 0x2102, |
| 175 |
[0x1D53F] = 0x210D, |
| 176 |
[0x1D545] = 0x2115, |
| 177 |
[0x1D547] = 0x2119, |
| 178 |
[0x1D548] = 0x211A, |
| 179 |
[0x1D549] = 0x211D, |
| 180 |
[0x1D551] = 0x2124, |
| 181 |
|
| 182 |
[0x1D455] = 0x210E, |
| 183 |
|
| 184 |
[0x1D4BA] = 0x212F, |
| 185 |
[0x1D4BC] = 0x210A, |
| 186 |
[0x1D4C4] = 0x2134, |
| 187 |
|
| 188 |
-- dotless i -- only italic variant exists |
| 189 |
[0x11D3B9] = 0x0131, -- bfup |
| 190 |
[0x11D3ED] = 0x1D6A4, -- it |
| 191 |
[0x11D421] = 0x1D6A4, -- bfit |
| 192 |
[0x11D559] = 0x0131, -- sfup |
| 193 |
[0x11D58D] = 0x0131, -- sfit |
| 194 |
[0x11D5C1] = 0x1D6A4, -- bfsfup |
| 195 |
[0x11D5F5] = 0x1D6A4, -- bfsfit |
| 196 |
[0x11D455] = 0x1D6A4, -- cal |
| 197 |
[0x11D489] = 0x1D6A4, -- bfcal |
| 198 |
[0x11D4BD] = 0x0131, -- frak |
| 199 |
[0x11D525] = 0x0131, -- bffrak |
| 200 |
[0x11D629] = 0x0131, -- tt |
| 201 |
[0x11D4F1] = 0x0131, -- bb |
| 202 |
|
| 203 |
-- dotless j -- only italic variant exists |
| 204 |
[0x11D3BA] = 0x0237, -- bfup |
| 205 |
[0x11D3EE] = 0x1D6A5, -- it |
| 206 |
[0x11D422] = 0x1D6A5, -- bfit |
| 207 |
[0x11D55A] = 0x0237, -- sfup |
| 208 |
[0x11D58E] = 0x0237, -- sfit |
| 209 |
[0x11D5C2] = 0x1D6A5, -- bfsfup |
| 210 |
[0x11D5F6] = 0x1D6A5, -- bfsfit |
| 211 |
[0x11D456] = 0x1D6A5, -- cal |
| 212 |
[0x11D48A] = 0x1D6A5, -- bfcal |
| 213 |
[0x11D4BE] = 0x0237, -- frak |
| 214 |
[0x11D526] = 0x0237, -- bffrak |
| 215 |
[0x11D62A] = 0x0237, -- tt |
| 216 |
[0x11D4F2] = 0x0237, -- bb |
| 217 |
|
| 218 |
-- capital digamma -- only bold variant exists |
| 219 |
[0x11D319] = 0x1D7CA, -- bfup |
| 220 |
[0x11D353] = 0x03DC, -- it |
| 221 |
[0x11D38D] = 0x1D7CA, -- bfit |
| 222 |
[0x11D3C7] = 0x1D7CA, -- bfsfup |
| 223 |
[0x11D401] = 0x1D7CA, -- bfsfit |
| 224 |
|
| 225 |
-- small digamma -- only bold variant exists |
| 226 |
[0x11D314] = 0x1D7CB, -- bfup |
| 227 |
[0x11D34E] = 0x03DD, -- it |
| 228 |
[0x11D388] = 0x1D7CB, -- bfit |
| 229 |
[0x11D3C2] = 0x1D7CB, -- bfsfup |
| 230 |
[0x11D3FC] = 0x1D7CB, -- bfsfit |
| 231 |
} |
| 232 |
|
| 233 |
-- The characters come in 5 blocks |
| 234 |
-- 1: Latin uppercase |
| 235 |
-- 2: Latin lowercase |
| 236 |
-- 3: Greek uppercase |
| 237 |
-- 4: Greek lowercase |
| 238 |
-- 5: Digits |
| 239 |
local char_Latin = 1 -- Latin uppercase |
| 240 |
local char_latin = 2 -- Latin lowercase |
| 241 |
local char_Greek = 3 -- Greek uppercase |
| 242 |
local char_greek = 4 -- Greek lowercase |
| 243 |
local char_digit = 5 -- Digits |
| 244 |
local char_types = {} |
| 245 |
|
| 246 |
for i=0x41, 0x5A do |
| 247 |
char_types[i], char_types[i + 0x20] = char_Latin, char_latin |
| 248 |
end |
| 249 |
|
| 250 |
for i=0x0391, 0x03A9 do |
| 251 |
char_types[i] = char_Greek |
| 252 |
end |
| 253 |
|
| 254 |
for i=0x03B1, 0x03D0 do |
| 255 |
char_types[i] = char_greek |
| 256 |
end |
| 257 |
|
| 258 |
for i=0x0030, 0x0039 do |
| 259 |
char_types[i] = char_digit |
| 260 |
end |
| 261 |
|
| 262 |
for base, remapped in next, pre_replacement do |
| 263 |
-- We want to apply char_types before pre_replacement |
| 264 |
char_types[base], char_types[remapped] = char_types[remapped], nil |
| 265 |
end |
| 266 |
char_types[0x0131] = char_latin -- ı -- dotless i |
| 267 |
char_types[0x0237] = char_latin -- ȷ -- dotless j |
| 268 |
char_types[0x03DC] = char_Greek -- Ϝ -- capital digamma |
| 269 |
char_types[0x03DD] = char_greek -- ϝ -- small digamma |
| 270 |
|
| 271 |
local serif, sans, script, calligraphic, fraktur, mono, bb = 0, 4, 8, 12, 16, 20, 24 |
| 272 |
local bold, italic = 1, 2 |
| 273 |
local bold_default = 1024 |
| 274 |
|
| 275 |
local special_offsets = {} |
| 276 |
|
| 277 |
local function special_offset(t) |
| 278 |
special_offsets[t] = true |
| 279 |
return t |
| 280 |
end |
| 281 |
|
| 282 |
local remap_bases = { |
| 283 |
[serif] = { -- Serif Upright |
| 284 |
0x0041, -- A |
| 285 |
0x0061, -- a |
| 286 |
0x0391, -- Α |
| 287 |
0x03B1, -- α |
| 288 |
0x0030, -- 0 |
| 289 |
}, |
| 290 |
[serif | bold] = { -- Serif Bold |
| 291 |
0x1D400, -- 𝐀 |
| 292 |
0x1D41A, -- 𝐚 |
| 293 |
0x1D6A8, -- 𝚨 |
| 294 |
0x1D6C2, -- 𝛂 |
| 295 |
0x1D7CE, -- 𝟎 |
| 296 |
}, |
| 297 |
[serif | italic] = { -- Serif Italic |
| 298 |
0x1D434, -- 𝐴 |
| 299 |
0x1D44E, -- 𝑎 |
| 300 |
0x1D6E2, -- 𝛢 |
| 301 |
0x1D6FC, -- 𝛼 |
| 302 |
0x0030, -- 0 |
| 303 |
}, |
| 304 |
[serif | bold | italic] = { -- Serif Bold Italic |
| 305 |
0x1D468, -- 𝑨 |
| 306 |
0x1D482, -- 𝒂 |
| 307 |
0x1D71C, -- 𝜜 |
| 308 |
0x1D736, -- 𝜶 |
| 309 |
0x1D7CE, -- 𝟎 |
| 310 |
}, |
| 311 |
[sans] = { -- Sans Upright |
| 312 |
0x1D5A0, -- 𝖠 |
| 313 |
0x1D5BA, -- 𝖺 |
| 314 |
0x0391, -- Α |
| 315 |
0x03B1, -- α |
| 316 |
0x1D7E2, -- 𝟢 |
| 317 |
}, |
| 318 |
[sans | bold] = { -- Sans Bold Upright |
| 319 |
0x1D5D4, -- 𝗔 |
| 320 |
0x1D5EE, -- 𝗮 |
| 321 |
0x1D756, -- 𝝖 |
| 322 |
0x1D770, -- 𝝰 |
| 323 |
0x1D7EC, -- 𝟬 |
| 324 |
}, |
| 325 |
[sans | italic] = { -- Sans Italic |
| 326 |
0x1D608, -- 𝘈 |
| 327 |
0x1D622, -- 𝘢 |
| 328 |
0x1D6E2, -- 𝛢 |
| 329 |
0x1D6FC, -- 𝛼 |
| 330 |
0x1D7E2, -- 𝟢 |
| 331 |
}, |
| 332 |
[sans | bold | italic] = { -- Sans Bold Italic |
| 333 |
0x1D63C, -- 𝘼 |
| 334 |
0x1D656, -- 𝙖 |
| 335 |
0x1D790, -- 𝞐 |
| 336 |
0x1D7AA, -- 𝞪 |
| 337 |
0x1D7EC, -- 𝟬 |
| 338 |
}, |
| 339 |
[script] = { -- Script Normal -- Chancery |
| 340 |
special_offset{offset = 0x1D49C, vs = 0xFE00}, -- 𝒜 |
| 341 |
special_offset{offset = 0x1D4B6, vs = 0xFE00}, -- 𝒶 |
| 342 |
0x1D6E2, -- 𝛢 |
| 343 |
0x1D6FC, -- 𝛼 |
| 344 |
0x0030, -- 0 |
| 345 |
}, |
| 346 |
[script | bold] = { -- Script Bold -- Chancery |
| 347 |
special_offset{offset = 0x1D4D0, vs = 0xFE00}, -- 𝓐 |
| 348 |
special_offset{offset = 0x1D4EA, vs = 0xFE00}, -- 𝓪 |
| 349 |
0x1D71C, -- 𝜜 |
| 350 |
0x1D736, -- 𝜶 |
| 351 |
0x1D7CE, -- 𝟎 |
| 352 |
}, |
| 353 |
[calligraphic] = { -- Script Normal -- Roundhand |
| 354 |
special_offset{offset = 0x1D49C, vs = 0xFE01}, -- 𝒜 |
| 355 |
special_offset{offset = 0x1D4B6, vs = 0xFE01}, -- 𝒶 |
| 356 |
0x1D6E2, -- 𝛢 |
| 357 |
0x1D6FC, -- 𝛼 |
| 358 |
0x0030, -- 0 |
| 359 |
}, |
| 360 |
[calligraphic | bold] = { -- Script Bold -- Roundhand |
| 361 |
special_offset{offset = 0x1D4D0, vs = 0xFE01}, -- 𝓐 |
| 362 |
special_offset{offset = 0x1D4EA, vs = 0xFE01}, -- 𝓪 |
| 363 |
0x1D71C, -- 𝜜 |
| 364 |
0x1D736, -- 𝜶 |
| 365 |
0x1D7CE, -- 𝟎 |
| 366 |
}, |
| 367 |
[fraktur] = { -- Fraktur Normal |
| 368 |
0x1D504, -- 𝔄 |
| 369 |
0x1D51E, -- 𝔞 |
| 370 |
0x0391, -- Α |
| 371 |
0x03B1, -- α |
| 372 |
0x0030, -- 0 |
| 373 |
}, |
| 374 |
[fraktur | bold] = { -- Fraktur Bold |
| 375 |
0x1D56C, -- 𝕬 |
| 376 |
0x1D586, -- 𝖆 |
| 377 |
0x1D6A8, -- 𝚨 |
| 378 |
0x1D6C2, -- 𝛂 |
| 379 |
0x1D7CE, -- 𝟎 |
| 380 |
}, |
| 381 |
[mono] = { -- Mono Normal |
| 382 |
0x1D670, -- 𝙰 |
| 383 |
0x1D68A, -- 𝚊 |
| 384 |
0x0391, -- Α |
| 385 |
0x03B1, -- α |
| 386 |
0x1D7F6, -- 𝟶 |
| 387 |
}, |
| 388 |
[bb] = { -- Double-struck |
| 389 |
0x1D538, -- 𝔸 |
| 390 |
0x1D552, -- 𝕒 |
| 391 |
0x1D6A8, -- 𝚨 |
| 392 |
0x1D6C2, -- 𝛂 |
| 393 |
0x1D7D8, -- 𝟘 |
| 394 |
}, |
| 395 |
} |
| 396 |
remap_bases[unset_attribute] = { -- Default |
| 397 |
remap_bases[serif | italic][char_Latin], -- Latin uppercase |
| 398 |
remap_bases[serif | italic][char_latin], -- Latin lowercase |
| 399 |
remap_bases[serif][char_Greek], -- Greek uppercase |
| 400 |
remap_bases[serif | italic][char_greek], -- Greek lowercase |
| 401 |
remap_bases[serif][char_digit], -- Serif digits |
| 402 |
} |
| 403 |
remap_bases[bold_default] = { -- Bold Default |
| 404 |
remap_bases[bold][1], -- Latin uppercase |
| 405 |
remap_bases[bold][2], -- Latin lowercase |
| 406 |
remap_bases[bold][3], -- Greek uppercase |
| 407 |
remap_bases[bold | italic][4], -- Greek lowercase |
| 408 |
remap_bases[bold][5], -- Serif digits |
| 409 |
} |
| 410 |
do |
| 411 |
local base = remap_bases[0] |
| 412 |
remap_bases[0] = {} -- We don't want to overwrite it in the next step |
| 413 |
local function adjust_offset(v, i) |
| 414 |
local bi, vi = base[i], v[i] |
| 415 |
if not vi or bi == vi then |
| 416 |
v[i] = nil |
| 417 |
elseif special_offsets[vi] then |
| 418 |
vi.offset = vi.offset - bi |
| 419 |
vi.special_replacement = vs_maps[vi.vs] |
| 420 |
else |
| 421 |
v[i] = vi - bi |
| 422 |
end |
| 423 |
end |
| 424 |
for _, v in next, remap_bases do |
| 425 |
adjust_offset(v, 1) |
| 426 |
adjust_offset(v, 2) |
| 427 |
adjust_offset(v, 3) |
| 428 |
adjust_offset(v, 4) |
| 429 |
adjust_offset(v, 5) |
| 430 |
end |
| 431 |
end |
| 432 |
|
| 433 |
local classcodes = { |
| 434 |
N = 0, -- Normal --> ord |
| 435 |
A = 7, -- Alphabetic --> variable |
| 436 |
B = 2, -- Binary --> bin |
| 437 |
C = 5, -- Closing --> close |
| 438 |
D = nil, -- Diacritic --> ??? |
| 439 |
F = 0, -- Fence --> ??? |
| 440 |
G = nil, -- Glyph part --> ??? |
| 441 |
L = 1, -- Large --> op |
| 442 |
O = 4, -- Opening --> open |
| 443 |
P = 6, -- Punctuation --> punct |
| 444 |
R = 3, -- Relation --> rel |
| 445 |
S = nil, -- Space --> ??? |
| 446 |
U = 0, -- Unary --> ord |
| 447 |
V = 2, -- Vary --> bin |
| 448 |
X = nil, -- Special --> ??? |
| 449 |
} |
| 450 |
|
| 451 |
local delimiterclasses = { |
| 452 |
O = true, |
| 453 |
F = true, |
| 454 |
C = true, |
| 455 |
} |
| 456 |
|
| 457 |
local setmathcode = tex.setmathcode |
| 458 |
for cp, class in pairs(mathclasses) do |
| 459 |
local classcode = classcodes[class] |
| 460 |
if classcode then |
| 461 |
tex.setmathcode(cp, classcode, main_fam, cp) |
| 462 |
if delimiterclasses[class] then |
| 463 |
tex.setdelcode(cp, main_fam, cp, 0, 0) |
| 464 |
end |
| 465 |
elseif class == 'G' or class == 'S' then |
| 466 |
-- Ignored |
| 467 |
else |
| 468 |
-- print(string.format("U+%04X (%s): %s", cp, utf8.char(cp), class)) |
| 469 |
end |
| 470 |
end |
| 471 |
|
| 472 |
local math_char_t = node.id'math_char' |
| 473 |
local sub_mlist_t = node.id'sub_mlist' |
| 474 |
local sub_box_t = node.id'sub_box' |
| 475 |
local whatsit_t = node.id'whatsit' |
| 476 |
local user_defined_s = node.subtype'user_defined' |
| 477 |
|
| 478 |
-- Map from whatsit user_ids to handlers |
| 479 |
-- Handler return values: head, node, state |
| 480 |
local math_whatsit_processors = {} |
| 481 |
|
| 482 |
local traverse_list |
| 483 |
|
| 484 |
-- container is set if we are a nucleus |
| 485 |
local function traverse_kernel(style, n, outer_head, outer) |
| 486 |
if not n then return 0, container end |
| 487 |
local id = n.id |
| 488 |
if id == math_char_t then |
| 489 |
local fam, char = n.fam, n.char |
| 490 |
if processed_families[fam] then |
| 491 |
local char_type = char_types[char] |
| 492 |
if char_type then |
| 493 |
char = pre_replacement[char] or char |
| 494 |
local offset = remap_bases[node.get_attribute(n, attr) or unset_attribute][char_type] |
| 495 |
if offset then |
| 496 |
if special_offsets[offset] then |
| 497 |
local special_replacement = offset.special_replacement |
| 498 |
char = char + offset.offset |
| 499 |
char = post_replacement[char] or char |
| 500 |
n.char = special_replacement[(style >> 2) | (n.fam << 2)][char] or char |
| 501 |
-- TODO: Set properties for replacing luamml mapping |
| 502 |
else |
| 503 |
char = char + offset |
| 504 |
n.char = post_replacement[char] or char |
| 505 |
end |
| 506 |
end |
| 507 |
end |
| 508 |
end |
| 509 |
elseif id == sub_mlist_t then |
| 510 |
n.head = traverse_list(style, n.head) |
| 511 |
elseif id == whatsit_t and n.subtype == user_defined_s then |
| 512 |
local user_id = n.user_id |
| 513 |
local processor = math_whatsit_processors[user_id] |
| 514 |
if processor then |
| 515 |
return processor(style, n, outer_head, outer) |
| 516 |
end |
| 517 |
end |
| 518 |
return outer_head, outer |
| 519 |
end |
| 520 |
|
| 521 |
local use_i_nn = token.create'use_i:nn' |
| 522 |
local use_ii_nn = token.create'use_ii:nn' |
| 523 |
local bool_token = {[false] = use_ii_nn, [true] = use_i_nn} |
| 524 |
|
| 525 |
local noad_t = node.id'noad' |
| 526 |
local accent_t = node.id'accent' |
| 527 |
local choice_t = node.id'choice' |
| 528 |
local style_t = node.id'style' |
| 529 |
local radical_t = node.id'radical' |
| 530 |
local fraction_t = node.id'fraction' |
| 531 |
-- local fence_t = node.id'fence' |
| 532 |
|
| 533 |
-- parent is only set if we are a nucleus sub_mlist |
| 534 |
function traverse_list(style, head) |
| 535 |
local next_node, state, n = node.traverse(head) |
| 536 |
while true do |
| 537 |
local id, sub |
| 538 |
n, id, sub = next_node(state, n) |
| 539 |
if n == nil then break end |
| 540 |
-- end |
| 541 |
-- for n, id, sub in node.traverse(head) do |
| 542 |
if id == noad_t then |
| 543 |
traverse_kernel(sub_style(style), n.sub) |
| 544 |
traverse_kernel(sup_style(style), n.sup) |
| 545 |
head, n, state = traverse_kernel(style, n.nucleus, head, n) |
| 546 |
elseif id == accent_t then |
| 547 |
traverse_kernel(sub_style(style), n.sub) |
| 548 |
traverse_kernel(sup_style(style), n.sup) |
| 549 |
traverse_kernel(style, n.accent) |
| 550 |
traverse_kernel(style, n.bot_accent) |
| 551 |
head, n, state = traverse_kernel(style | 1, n.nucleus, head, n) |
| 552 |
elseif id == choice_t then |
| 553 |
-- Currently we process all of these. We know the current style though, so we could simplify if needed. |
| 554 |
n.display = traverse_list(0, n.display) |
| 555 |
n.text = traverse_list(2, n.text) |
| 556 |
n.script = traverse_list(4, n.script) |
| 557 |
n.scriptscript = traverse_list(6, n.scriptscript) |
| 558 |
elseif id == style_t then |
| 559 |
style = reverse_styles[n.style] |
| 560 |
elseif id == radical_t then |
| 561 |
traverse_kernel(sub_style(style), n.sub) |
| 562 |
traverse_kernel(sup_style(style), n.sup) |
| 563 |
traverse_kernel(6, n.degree) |
| 564 |
if sub < 3 or sub >= 6 then -- radicals and roots, \Udelimiterover, \Uhextension |
| 565 |
if sub == 2 then -- Handle uproot and leftroot attributes |
| 566 |
local leftroot = node.get_attribute(n, leftroot_attr) |
| 567 |
if leftroot then |
| 568 |
local degree = n.degree |
| 569 |
if degree.id == math_char_t then |
| 570 |
local noad = node.new(noad_t) |
| 571 |
noad.nucleus = degree |
| 572 |
degree = node.new(sub_mlist_t) |
| 573 |
degree.head = noad |
| 574 |
n.degree = degree |
| 575 |
end |
| 576 |
assert(degree.id == sub_mlist_t) |
| 577 |
local quad = tex.getmath('quad', style_names[style]) |
| 578 |
leftroot = leftroot * quad // 18 |
| 579 |
local pre_kern, post_kern = node.new'kern', node.new'kern' |
| 580 |
pre_kern.kern, post_kern.kern = -leftroot, leftroot |
| 581 |
pre_kern.kern = -leftroot |
| 582 |
degree.head = node.insert_after(node.insert_before(degree.head, degree.head, pre_kern), nil, post_kern) |
| 583 |
end |
| 584 |
local uproot = node.get_attribute(n, uproot_attr) |
| 585 |
if uproot then |
| 586 |
local degree = n.degree |
| 587 |
if degree.id == math_char_t then |
| 588 |
local noad = node.new(noad_t) |
| 589 |
noad.nucleus = degree |
| 590 |
degree = node.new(sub_mlist_t) |
| 591 |
degree.head = noad |
| 592 |
n.degree = degree |
| 593 |
end |
| 594 |
assert(degree.id == sub_mlist_t) |
| 595 |
local quad = tex.getmath('quad', style_names[style]) |
| 596 |
uproot = uproot * quad // 18 |
| 597 |
local new_degree = node.new(sub_box_t) |
| 598 |
new_degree.head = node.hpack(node.mlist_to_hlist(degree.head, 'scriptscript', false)) |
| 599 |
degree.head = nil |
| 600 |
node.free(degree) |
| 601 |
new_degree.head.shift = -uproot |
| 602 |
n.degree = new_degree |
| 603 |
end |
| 604 |
end |
| 605 |
head, n, state = traverse_kernel(style | 1, n.nucleus, head, n) |
| 606 |
elseif sub == 3 then -- \Uunderdelimiter |
| 607 |
head, n, state = traverse_kernel(sub_style(style), n.nucleus, head, n) |
| 608 |
elseif sub == 4 then -- \Uoverdelimiter |
| 609 |
head, n, state = traverse_kernel(sup_style(style), n.nucleus, head, n) |
| 610 |
elseif sub == 5 then -- \Udelimiterunder |
| 611 |
head, n, state = traverse_kernel(style, n.nucleus, head, n) |
| 612 |
end |
| 613 |
-- traverse_delim(n.left) |
| 614 |
elseif id == fraction_t then |
| 615 |
traverse_kernel(num_style(style), n.num) |
| 616 |
traverse_kernel(denom_style(style), n.denom) |
| 617 |
-- traverse_delim(n.left) |
| 618 |
-- traverse_delim(n.middle) |
| 619 |
-- traverse_delim(n.right) |
| 620 |
-- elseif id == fence_t then |
| 621 |
-- traverse_delim(n.delim) |
| 622 |
elseif id == whatsit_t and sub == user_defined_s then |
| 623 |
local user_id = n.user_id |
| 624 |
local processor = math_whatsit_processors[user_id] |
| 625 |
if processor then |
| 626 |
head, n, state = processor(style, n, head) |
| 627 |
end |
| 628 |
end |
| 629 |
end |
| 630 |
return head |
| 631 |
end |
| 632 |
|
| 633 |
luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(n, style, penalties) |
| 634 |
return traverse_list(reverse_styles[style], n) |
| 635 |
end, 'lua-unicode-math') |
| 636 |
|
| 637 |
tex.setmathcode(0x2A, tex.getmathcodes(0x2217)) -- '*' gets the mathcode of '∗' |
| 638 |
tex.setmathcode(0x2D, tex.getmathcodes(0x2212)) -- '-' gets the mathcode of '−' |
| 639 |
tex.setmathcode(0x3A, tex.getmathcodes(0x2236)) -- ':' gets the mathcode of '∶' |
| 640 |
|
| 641 |
local slash_fence = 0x2F |
| 642 |
tex.setdelcode(0x2F, main_fam, slash_fence, 0, 0) -- / |
| 643 |
tex.setdelcode(0x2044, main_fam, slash_fence, 0, 0) -- ⁄ |
| 644 |
tex.setdelcode(0x2215, main_fam, slash_fence, 0, 0) -- ∕ |
| 645 |
|
| 646 |
local backslash_fence = 0x5C |
| 647 |
tex.setdelcode(0x5C, main_fam, backslash_fence, 0, 0) -- \ |
| 648 |
tex.setdelcode(0x2216, main_fam, backslash_fence, 0, 0) -- ∖ |
| 649 |
tex.setdelcode(0x29F5, main_fam, backslash_fence, 0, 0) -- ⧵ |
| 650 |
|
| 651 |
tex.setdelcode(0x3C, tex.getdelcodes(0x27E8)) -- < => ⟨ |
| 652 |
tex.setdelcode(0x3E, tex.getdelcodes(0x27E9)) -- > => ⟩ |
| 653 |
|
| 654 |
for _, cp in ipairs{0x2191, 0x2193, 0x2195, 0x21D1, 0x21D3, 0x21D5} do -- ↑↓↕⇑⇓⇕ |
| 655 |
tex.setdelcode(cp, main_fam, cp, 0, 0) |
| 656 |
end |
| 657 |
|
| 658 |
local nest = tex.nest |
| 659 |
|
| 660 |
local mmode do |
| 661 |
for k, v in next, tex.getmodevalues() do |
| 662 |
if v == 'math' then |
| 663 |
mmode = k |
| 664 |
break |
| 665 |
end |
| 666 |
end |
| 667 |
assert(mmode) |
| 668 |
end |
| 669 |
|
| 670 |
local Ustartmath = token.new(text_style, token.command_id'math_shift_cs') |
| 671 |
local lua_call = token.command_id'lua_call' |
| 672 |
local environment = setmetatable({ |
| 673 |
check_math = function(id) |
| 674 |
local mode = nest.top.mode |
| 675 |
if mode ~= mmode and mode ~= -mmode then |
| 676 |
token.push_back(Ustartmath, token.new(id, lua_call)) |
| 677 |
return tex.error('Missing $ inserted', { |
| 678 |
"I've inserted a begin-math/end-math symbol since I think", |
| 679 |
"you left one out. Proceed, with fingers crossed." |
| 680 |
}) |
| 681 |
end |
| 682 |
end, |
| 683 |
math_whatsit_processors = math_whatsit_processors, |
| 684 |
main_fam = main_fam, |
| 685 |
write_whatsit = function(user_id, kind, value) |
| 686 |
local n = node.new(whatsit_t, user_defined_s) |
| 687 |
n.user_id, n.type, n.value = user_id, kind, value |
| 688 |
node.write(n) |
| 689 |
end, |
| 690 |
write_whatsit_wrapped = function(user_id, kind, value, noad_sub) |
| 691 |
local n = node.new(noad_t, noad_sub) |
| 692 |
local nuc = node.new(whatsit_t, user_defined_s) |
| 693 |
n.nucleus = nuc |
| 694 |
nuc.user_id, nuc.type, nuc.value = user_id, kind, value |
| 695 |
node.write(n) |
| 696 |
end, |
| 697 |
}, { __index = _ENV }) |
| 698 |
|
| 699 |
for _, name in ipairs{'prime', 'not', 'dots'} do |
| 700 |
loadfile(kpse.find_file(string.format('lua-unicode-math--%s', name), 'lua'), 'bt', environment)() |
| 701 |
end |
| 702 |
|
| 703 |
local func = luatexbase.new_luafunction'__l_uni_math_set_mathstyle_mappings:NNNNNN' |
| 704 |
token.set_lua('__l_uni_math_set_mathstyle_mappings:NNNNNN', func, 'protected') |
| 705 |
lua.get_functions_table()[func] = function() |
| 706 |
remap_bases[token.scan_int()] = { -- Default |
| 707 |
remap_bases[token.scan_int()][char_Latin], -- Latin uppercase |
| 708 |
remap_bases[token.scan_int()][char_latin], -- Latin lowercase |
| 709 |
remap_bases[token.scan_int()][char_Greek], -- Greek uppercase |
| 710 |
remap_bases[token.scan_int()][char_greek], -- Greek lowercase |
| 711 |
remap_bases[token.scan_int()][char_digit], -- Serif digits |
| 712 |
} |
| 713 |
end |
| 714 |
|
| 715 |
local func = luatexbase.new_luafunction'__l_uni_math_uproot:w' |
| 716 |
token.set_lua('__l_uni_math_uproot:w', func, 'protected') |
| 717 |
lua.get_functions_table()[func] = function() |
| 718 |
local value = token.scan_int() |
| 719 |
|
| 720 |
local root_nest = tex.nest[tex.nest.ptr - 1] |
| 721 |
local root = root_nest and root_nest.tail |
| 722 |
if not root or root.id ~= radical_t or root.subtype ~= 2 or root.nucleus or not root.degree then |
| 723 |
tex.error'Misuse' |
| 724 |
return |
| 725 |
end |
| 726 |
|
| 727 |
node.set_attribute(root, uproot_attr, value) |
| 728 |
end |
| 729 |
|
| 730 |
local func = luatexbase.new_luafunction'__l_uni_math_leftroot:w' |
| 731 |
token.set_lua('__l_uni_math_leftroot:w', func, 'protected') |
| 732 |
lua.get_functions_table()[func] = function() |
| 733 |
local value = token.scan_int() |
| 734 |
|
| 735 |
local root_nest = tex.nest[tex.nest.ptr - 1] |
| 736 |
local root = root_nest and root_nest.tail |
| 737 |
if not root or root.id ~= radical_t or root.subtype ~= 2 or root.nucleus or not root.degree then |
| 738 |
tex.error'Misuse' |
| 739 |
return |
| 740 |
end |
| 741 |
|
| 742 |
node.set_attribute(root, leftroot_attr, value) |
| 743 |
end |
| 744 |
|
| 745 |
local func = luatexbase.new_luafunction'__l_uni_math_is_integral_cp:wTF' |
| 746 |
token.set_lua('__l_uni_math_is_integral_cp:wTF', func) |
| 747 |
lua.get_functions_table()[func] = function() |
| 748 |
local value = token.scan_int() |
| 749 |
token.put_next(bool_token[integral_codepoints[value] or false]) |
| 750 |
end |
| 751 |
|
| 752 |
local func = luatexbase.new_luafunction'__l_uni_math_every_math_size:' |
| 753 |
token.set_lua('__l_uni_math_every_math_size:', func) |
| 754 |
lua.get_functions_table()[func] = function() |
| 755 |
for t in pairs(mathsize_reset_maps) do |
| 756 |
for k in pairs(t) do |
| 757 |
t[k] = nil |
| 758 |
end |
| 759 |
end |
| 760 |
end |