Debian Codegraph @

texmf-dist/tex/lualatex/lua-unicode-math/lua-unicode-math--prime.lua

raw
1 local prime_node_id = luatexbase.new_whatsit'prime'
2
3 local func = luatexbase.new_luafunction'prime_helper:w'
4 token.set_lua("prime_helper:w", func, 'protected')
5
6 lua.get_functions_table()[func] = function(id)
7 check_math(id)
8 write_whatsit_wrapped(prime_node_id, 100, token.scan_int(), 0)
9 end
10
11 local prime_lookup = {
12 [0x2032] = {
13 [0x2032] = 0x2033,
14 [0x2033] = 0x2034,
15 [0x2034] = 0x2057,
16 },
17 [0x2035] = {
18 [0x2035] = 0x2036,
19 [0x2036] = 0x2037,
20 },
21 }
22
23 local accent_t = node.id'accent'
24 local math_char_t = node.id'math_char'
25 local sub_mlist_t = node.id'sub_mlist'
26 local noad_t = node.id'noad'
27 local radical_t = node.id'radical'
28
29 math_whatsit_processors[prime_node_id] = function(_style, n, parent_head, parent)
30 assert(parent)
31 local char = n.value
32 local pre_sup, pre_sub
33 local prev = parent.prev
34 local prev_id = prev and prev.id
35 if prev_id == noad_t or prev_id == accent_t or prev_id == radical_t then
36 pre_sup, pre_sub = prev.sup, prev.sub
37 else
38 node.free(n)
39 parent.nucleus = node.new(sub_mlist_t)
40 prev = parent
41 end
42 if pre_sub == nil then
43 prev.sub, parent.sub = parent.sub, nil
44 elseif parent.sub then
45 tex.error'Double subscript ignored'
46 end
47 local post_sup = parent.sup
48
49 local merge_lookup = prime_lookup[char]
50 -- First check for the cases where no sub_mlist is needed:
51 local done
52 if post_sup == nil then
53 if pre_sup == nil then
54 local new_sup = node.new(math_char_t)
55 new_sup.fam, new_sup.char = main_fam, char
56 prev.sup = new_sup
57
58 done = true
59 elseif pre_sup.id == math_char_t and pre_sup.fam == main_fam then
60 local merged = merge_lookup[pre_sup.char]
61 if merged then
62 pre_sup.char = merged
63
64 done = true
65 end
66 end
67 end
68
69 if not done then
70 local mlist -- Some sub_mlist node which will become the new sup
71 local new_head, current_tail
72
73 if pre_sup ~= nil then
74 if pre_sup.id == sub_mlist_t then
75 mlist, new_head = pre_sup, pre_sup.head
76 current_tail = node.tail(new_head)
77 else
78 new_head = node.new(noad_t, 0)
79 new_head.nucleus = pre_sup
80 current_tail = new_head
81 end
82 end
83
84 if current_tail and current_tail.id == noad_t then
85 local nucleus = current_tail.nucleus
86 if nucleus.id == math_char_t and nucleus.fam == main_fam then
87 local oldchar = nucleus.char
88 local nextchar = merge_lookup[oldchar]
89 if nextchar then
90 nucleus.char, done = nextchar, true
91 end
92 end
93 end
94
95 if not done then
96 local noad = node.new(noad_t, 0)
97 local nucleus = node.new(math_char_t)
98 nucleus.fam, nucleus.char = main_fam, char
99 noad.nucleus = nucleus
100 new_head, current_tail = node.insert_after(new_head, current_tail, noad)
101 end
102
103 if post_sup ~= nil then
104 if post_sup.id == sub_mlist_t then
105 local post_head = post_sup.head
106 current_tail.next, post_head.prev = post_head, current_tail
107
108 if mlist then
109 post_sup.head = nil
110 else
111 mlist = post_sup
112 parent.sup = nil
113 end
114 else
115 local noad = node.new(noad_t, 0)
116 noad.nucleus = post_sup
117 parent.sup = nil
118 node.insert_after(new_head, current_tail, noad)
119 end
120 end
121
122 if not mlist then
123 mlist = node.new(sub_mlist_t)
124 end
125
126 mlist.head = new_head
127 prev.sup = mlist
128 end
129
130 if parent ~= prev then
131 node.remove(prev, parent)
132 node.free(parent)
133 end
134 return parent_head, prev
135 end