Debian Codegraph @

texmf-dist/tex/lualatex/uninormalize/unicode-normalize.lua

raw
1 local M = {}
2 require("unicode-normalize-names")
3 require('unicode-normalization')
4 local NFC = unicode.conformance.toNFC
5 local char = unicode.utf8.char
6 local gmatch = unicode.utf8.gmatch
7 local name = unicode.conformance.name
8 local byte = unicode.utf8.byte
9 -- local unidata = unicharacters.data
10 local length = unicode.utf8.len
11
12 local glyph_id = node.id "glyph"
13
14 M.debug = false
15
16 -- for some reason variable number of arguments doesn't work
17 local function debug_msg(a,b,c,d,e,f,g,h,i)
18 if M.debug then
19 local t = {a,b,c,d,e,f,g,h,i}
20 print("[uninormalize]", table.unpack(t))
21 end
22 end
23
24 local function make_hash (t)
25 local y = {}
26 for _,v in ipairs(t) do
27 y[v] = true
28 end
29 return y
30 end
31
32 local letter_categories = make_hash {"lu","ll","lt","lo","lm"}
33
34 local mark_categories = make_hash {"mn","mc","me"}
35
36 local function printchars(s)
37 local t = {}
38 for x in gmatch(s,".") do
39 t[#t+1] = name(byte(x))
40 end
41 debug_msg("characters",table.concat(t,":"))
42 end
43
44 local categories = {}
45
46
47 local function get_category(charcode)
48 local charcode = charcode or ""
49 if categories[charcode] then
50 return categories[charcode]
51 else
52 local unidatacode = unidata[charcode] or {}
53 local category = unidatacode.category
54 categories[charcode] = category
55 return category
56 end
57 end
58
59 -- get glyph char and category
60 local function glyph_info(n)
61 local char = n.char
62 return char, get_category(char)
63 end
64
65 local function get_mark(n)
66 if n.id == glyph_id then
67 local character, cat = glyph_info(n)
68 if mark_categories[cat] then
69 return char(character)
70 end
71 end
72 return false
73 end
74
75 local function make_glyphs(head, nextn,s, lang, font, subtype)
76 local g = function(a)
77 local new_n = node.new(glyph_id, subtype)
78 new_n.lang = lang
79 new_n.font = font
80 new_n.char = byte(a)
81 return new_n
82 end
83 if length(s) == 1 then
84 return node.insert_before(head, nextn,g(s))
85 else
86 local t = {}
87 local first = true
88 for x in gmatch(s,".") do
89 debug_msg("multi letter",x)
90 head, newn = node.insert_before(head, nextn, g(x))
91 end
92 return head
93 end
94 end
95
96 local function normalize_marks(head, n)
97 local lang, font, subtype = n.lang, n.font, n.subtype
98 local text = {}
99 text[#text+1] = char(n.char)
100 local head, nextn = node.remove(head, n)
101 --local nextn = n.next
102 local info = get_mark(nextn)
103 while(info) do
104 text[#text+1] = info
105 head, nextn = node.remove(head,nextn)
106 info = get_mark(nextn)
107 end
108 local s = NFC(table.concat(text))
109 debug_msg("We've got mark: " .. s)
110 local new_n = node.new(glyph_id, subtype)
111 new_n.lang = lang
112 new_n.font = font
113 new_n.char = byte(s)
114 --head, new_n = node.insert_before(head, nextn, new_n)
115 -- head, new_n = node.insert_before(head, nextn, make_glyphs(s, lang, font, subtype))
116 head, new_n = make_glyphs(head, nextn, s, lang, font, subtype)
117 local t = {}
118 for x in node.traverse_id(glyph_id,head) do
119 t[#t+1] = char(x.char)
120 end
121 debug_msg("Variables ", table.concat(t,":"), table.concat(text,";"), char(byte(s)),length(s))
122 return head, nextn
123 end
124
125 local function normalize_glyphs(head, n)
126 --local charcode = n.char
127 --local category = get_category(charcode)
128 local charcode, category = glyph_info(n)
129 if letter_categories[category] then
130 local nextn = n.next
131 if nextn and nextn.id == glyph_id then
132 --local nextchar = nextn.char
133 --local nextcat = get_category(nextchar)
134 local nextchar, nextcat = glyph_info(nextn)
135 if mark_categories[nextcat] then
136 return normalize_marks(head,n)
137 end
138 end
139 end
140 return head, n.next
141 end
142
143
144 function M.nodes(head)
145 local t = {}
146 local text = false
147 local n = head
148 -- for n in node.traverse(head) do
149 while n do
150 if n.id == glyph_id then
151 local charcode = n.char
152 debug_msg("unicode name",name(charcode))
153 debug_msg("character category",get_category(charcode))
154 t[#t+1]= char(charcode)
155 text = true
156 head, n = normalize_glyphs(head, n)
157 else
158 if text then
159 local s = table.concat(t)
160 debug_msg("text chunk",s)
161 --printchars(NFC(s))
162 debug_msg("----------")
163 end
164 text = false
165 t = {}
166 n = n.next
167 end
168 end
169 return head
170 end
171
172 local unibytes = {}
173
174 local function get_charcategory(s)
175 local s = s or ""
176 local b = unibytes[s] or byte(s) or 0
177 unibytes[s] = b
178 return get_category(b)
179 end
180
181 local function normalize_charmarks(t,i)
182 local c = {t[i]}
183 local i = i + 1
184 local s = get_charcategory(t[i])
185 while mark_categories[s] do
186 c[#c+1] = t[i]
187 i = i + 1
188 s = get_charcategory(t[i])
189 end
190 return NFC(table.concat(c)), i
191 end
192
193 local function normalize_char(t,i)
194 local ch = t[i]
195 local c = get_charcategory(ch)
196 if letter_categories[c] then
197 local nextc = get_charcategory(t[i+1])
198 if mark_categories[nextc] then
199 return normalize_charmarks(t,i)
200 end
201 end
202 return ch, i+1
203 end
204
205 function M.buffer(line)
206 local t = {}
207 local new_t = {}
208 -- we need to make table witl all uni chars on the line
209 for x in gmatch(line,".") do
210 t[#t+1] = x
211 end
212 local i = 1
213 -- normalize next char
214 local c, i = normalize_char(t, i)
215 new_t[#new_t+1] = c
216 while t[i] do
217 c, i = normalize_char(t,i)
218 -- local c = t[i]
219 -- i = i + 1
220 new_t[#new_t+1] = c
221 end
222 return table.concat(new_t)
223 end
224
225
226 return M