Debian Codegraph @

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

raw
1 -- char-def now contains all necessary fields, no need for a custom file
2 if not characters then
3 require "char-def"
4 end
5
6 if not unicode then require('unicode') end
7 unicode.conformance = unicode.conformance or { }
8
9 unicharacters = unicharacters or {}
10 uni = unicode.utf8
11 unidata = characters.data
12
13 function printf(s, ...) print(string.format(s, ...)) end
14 -- function debug(s, ...) io.write("DEBUG: ", string.format(s, ...), "\n") end
15 function warn(s, ...) io.write("Warning: ", string.format(s, ...), "\n") end
16
17 function md5sum(any) return md5.hex(md5.sum(any)) end
18
19 -- Rehash the character data
20 unicharacters.combinee = { }
21 unicharacters.context = unicharacters.context or { }
22 local charu = unicode.utf8.char
23
24 function unicharacters.context.rehash2()
25 for ucode, udata in pairs(unidata) -- *not* ipairs :-)
26 do
27 local sp = udata.specials
28 if sp then
29 if sp[1] == 'char' then
30 -- local ucode = udata.unicodeslot
31 local entry = { combinee = sp[2], combining = sp[3], combined = ucode }
32 if not unicharacters.combinee[sp[2]]
33 then unicharacters.combinee[sp[2]] = { } end
34 local n = #unicharacters.combinee[sp[2]]
35 unicharacters.combinee[sp[2]][n+1] = entry
36 end
37 end
38 -- copy context's combining field to combclass field
39 -- this field was in the custom copy of the char-def.lua that we no longer use
40 udata.combclass = udata.combining
41 end
42 end
43
44
45 unicharacters.context.rehash2()
46 combdata = unicharacters.combinee
47
48 --[[ function unicode.conformance.is_hangul(ucode)
49 return ucode >= 0xAC00 and ucode <= 0xD7A3
50 end ]] -- Make it local for the moment
51 local function is_hangul(char)
52 return char >= 0xAC00 and char <= 0xD7A3
53 end
54
55 local function is_jamo(char)
56 if char < 0x1100 then return false
57 elseif char < 0x1160 then return 'choseong'
58 elseif char < 0x11A8 then return 'jungseong'
59 elseif char < 0x11FA then return 'jongseong'
60 else return false
61 end
62 end
63
64 local function decompose(ucode, compat) -- if compat then compatibility
65 local invbuf = { }
66 local sp = unidata[ucode].specials
67 if not sp
68 then return { ucode } else
69 if compat then compat = (sp[1] == 'compat') else compat = false end
70 while sp[1] == 'char' or compat do
71 head, tail = sp[2], sp[3]
72 if not tail then invbuf[#invbuf + 1] = head break end -- singleton
73 invbuf[#invbuf + 1] = tail
74 sp = unidata[head].specials
75 if not sp then invbuf[#invbuf + 1] = head sp = { } end
76 -- end -- not unidata[head]
77 end -- while sp[1] == 'char' or compat
78 end -- not sp
79
80 local seq = { }
81 for i = #invbuf, 1, -1
82 do seq[#seq + 1] = invbuf[i]
83 end
84 return seq
85 end
86
87 local function canon(seq) -- Canonical reordering
88 if #seq < 3 then return seq end
89 local c1, c2, buf
90 -- I'd never thought I'd implement an actual bubble sort some day ;-)
91 for k = #seq - 1, 1, -1 do
92 for i = 2, k do -- was k - 1! Argh!
93 c1 = unidata[seq[i]].combclass
94 c2 = unidata[seq[i+1]].combclass
95 if c1 and c2 then
96 if c1 > c2 then
97 buf = seq[i]
98 seq[i] = seq[i+1]
99 seq[i+1] = buf
100 end
101 end
102 end
103 end
104 return seq
105 end
106
107 if not math.div then -- from l-math.lua
108 function math.div(n, m)
109 return math.floor(n/m)
110 end
111 end
112
113 local SBase, LBase, VBase, TBase = 0xAC00, 0x1100, 0x1161, 0x11A7
114 local LCount, VCount, TCount = 19, 21, 28
115 local NCount = VCount * TCount
116 local SCount = TCount * NCount
117
118 local function decompose_hangul(ucode) -- assumes input is really a Hangul
119 local SIndex = ucode - SBase
120 local L = LBase + math.div(SIndex, NCount)
121 local V = VBase + math.div((SIndex % NCount), TCount)
122 local T = TBase + SIndex % TCount
123 if T == TBase then T = nil end
124 return { L, V, T }
125 end
126
127 -- To NFK?D.
128 function toNF_D_or_KD(unistring, compat)
129 local nfd, seq = { }, { }
130 for uchar in uni.gmatch(unistring, '.') do
131 local ucode = uni.byte(uchar)
132 if is_hangul(ucode) then
133 seq = decompose_hangul(ucode)
134 for _, c in ipairs(seq)
135 do nfd[#nfd + 1] = c
136 end
137 seq = { }
138 elseif not unidata[ucode]
139 then nfd[#nfd + 1] = ucode else
140 local ccc = unidata[ucode].combclass
141 if not ccc or ccc == 0 then
142 seq = canon(seq)
143 for _, c in ipairs(seq) do nfd[#nfd + 1] = c end
144 seq = decompose(ucode, compat)
145 else seq[#seq + 1] = ucode
146 end -- not ccc or ccc == 0
147 end -- if is_hangul(ucode) / elseif not unidata[ucode]
148 end -- for uchar in uni.gmatch(unistring, ".")
149
150 if #seq > 0 then
151 seq = canon(seq)
152 for _, c in ipairs(seq) do nfd[#nfd + 1] = c end
153 end
154
155 local nfdstr = ""
156 for _, chr in ipairs(nfd)
157 do nfdstr = string.format("%s%s", nfdstr, uni.char(chr)) end
158 return nfdstr, nfd
159 end
160
161 function unicode.conformance.toNFD(unistring)
162 return toNF_D_or_KD(unistring, false)
163 end
164
165 function unicode.conformance.toNFKD(unistring)
166 return toNF_D_or_KD(unistring, true)
167 end
168
169 local function compose(seq)
170 local base = seq[1]
171 if not combdata[base] then return seq else
172 local i = 2
173 while i <= #seq do -- can I play with 'i' in a for loop?
174 local cbng = seq[i]
175 local cccprev
176 if unidata[seq[i-1]] then cccprev = unidata[seq[i-1]].combclass end
177 if not cccprev then cccprev = -1 end
178 if unidata[cbng].combclass > cccprev then
179 if not combdata[base] then return seq else
180 for _, cbdata in ipairs(combdata[base]) do
181 if cbdata.combining == cbng then
182 seq[1] = cbdata.combined
183 base = seq[1]
184 for k = i, #seq - 1
185 do seq[k] = seq[k+1]
186 end -- for k = i, #seq - 1
187 seq[#seq] = nil
188 i = i - 1
189 end -- if cbdata.combining == cbng
190 end -- for _, cbdata in ipairs(combdata[base])
191 end -- if unidata[cbng.combclass > cccprev
192 end -- if not combdata[base]
193 i = i + 1
194 end -- while i <= #seq
195 end -- if not combdata[base]
196 return seq
197 end
198
199 -- To NFC from NFD.
200 -- Does not yet take all the composition exclusions in account
201 -- (missing types 1 and 2 as defined by UAX #15 X6)
202 function unicode.conformance.toNFC_fromNFD(nfd)
203 local nfc = { }
204 local seq = { }
205 for uchar in uni.gmatch(nfd, '.') do
206 local ucode = uni.byte(uchar)
207 if not unidata[ucode]
208 then nfc[#nfc + 1] = ucode else
209 local cb = unidata[ucode].combclass
210 if not cb or (cb == 0) then
211 -- if seq ~= { } then -- Dubious ...
212 if #seq > 0 then
213 seq = compose(seq) -- There was a check for #seq == 1 here
214 for i = 1, #seq do nfc[#nfc + 1] = seq[i] end
215 end -- #seq > 0
216 seq = { ucode }
217 else seq[#seq + 1] = ucode --[[ Maybe check if seq is not empty ... ]]
218 end -- not cb or cb == 0
219 end
220 end
221
222 seq = compose(seq)
223 for i = 1, #seq do nfc[#nfc + 1] = seq[i] end
224
225 local nfcstr = ""
226 for _, chr in ipairs(nfc)
227 do nfcstr = string.format("%s%s", nfcstr, uni.char(chr)) end
228 return nfcstr, nfc
229 end
230
231 local function cancompose(seq, compat)
232 local dec = { } -- new table to hold the decomposed sequence
233
234 local shift
235 if #seq >= 2 then -- let's do it the brutal way :-)
236 if is_jamo(seq[1]) == 'choseong' and
237 is_jamo(seq[2]) == 'jungseong' then
238 LIndex = seq[1] - LBase
239 VIndex = seq[2] - VBase
240 if #seq == 2 or is_jamo(seq[3]) ~= 'jongseong' then
241 TIndex = 0
242 shift = 1
243 else
244 TIndex = seq[3] - TBase
245 shift = 2
246 end
247 seq[1] = (LIndex * VCount + VIndex) * TCount + TIndex + SBase
248 for i = 2, #seq -- this shifts and shrinks the table at the same time
249 do seq[i] = seq[i + shift]
250 end
251 end
252 end
253
254 dec[1] = seq[1]
255 for i = 2, #seq do
256 local u = seq[i]
257 local sp = unidata[u].specials
258 if sp then
259 if compat then compat = (sp[1] == 'compat') else compat = false end
260 if (sp[1] == 'char') or compat then
261 for i = 2, #sp
262 do dec[#dec + 1] = sp[i]
263 end
264 end
265 else dec[#dec + 1] = u
266 end
267 end -- we have the fully decomposed sequence; now sort it
268
269 for i = #dec - 1, 2, -1 do -- bubble sort!
270 for j = 2, #dec - 1 do
271 local u = dec[j]
272 local ccc1 = unidata[u].combclass
273 local v = dec[j+1]
274 local ccc2 = unidata[v].combclass
275 if ccc1 > ccc2 then -- swap
276 dec[j+1] = u
277 dec[j] = v
278 end
279 end
280 end -- dec sorted; now recursively compose
281
282 local base, i, n = dec[1], 2, #dec
283 local cbd = combdata[base]
284 local incr_i = true
285 while i <= n do
286 local cbg = dec[i]
287 if cbd then
288 for _, cb in ipairs(cbd) do
289 if cb.combining == cbg then
290 -- NO :-) -- if cbd[cbg] then -- base and cbg combine; compose
291 dec[1] = cb.combined
292 base = dec[1]
293 cbd = combdata[base]
294 for j = i, n-1 -- shift table elements right of i
295 do dec[j] = dec[j+1] end
296 dec[n] = nil
297 n = n-1 -- table has shrunk by 1, and i doesn't grow
298 incr_i = false
299 end
300 end
301 end
302 if incr_i then i = i + 1
303 else incr_i = true end
304 end -- we're finally through! return
305 return dec
306 end
307
308 function toNF_C_or_KC(unistring, compat)
309 if unistring == "" then return "" end
310 local nfc, seq = "", { }
311 local start, space = true, ""
312 for uchar in uni.gmatch(unistring, '.') do
313 local ucode = uni.byte(uchar)
314 if start then space = ", " start = true end
315 if not unidata[ucode] then -- unknown to the UCD, will not compose
316 nfc = string.format("%s%s", nfc, uchar)
317 else
318 local ccc = unidata[ucode].combclass
319 if not (ccc or is_jamo(ucode) == 'jongseong'
320 or is_jamo(ucode) == 'jungseong')
321 or ccc == 0 or (is_jamo(ucode) == 'choseong') then
322 -- and is actually good :-) -- Well, yes and no ;-)
323 if #seq == 0 then -- add ucode and go to next item of the loop
324 seq = { ucode }
325 else -- seq contains unicharacters, try and compose them
326 if #seq == 1 then nfc = string.format("%s%s", nfc, uni.char(seq[1]))
327 else dec = cancompose(seq, compat)
328 for _, c in ipairs(dec) -- add the whole sequence to nfc
329 do nfc = string.format("%s%s", nfc, uni.char(c)) end
330 end
331 seq = { ucode } -- don't forget to reinitialize seq with current char
332 end
333 else -- not ccc or ccc == 0 and is_choseong:
334 -- character is combining, add it to seq
335 seq[#seq + 1] = ucode
336 end
337 end
338 end
339 if #seq > 0 then dec = cancompose(seq, compat) end
340 for _, c in ipairs(dec)
341 do nfc = string.format("%s%s", nfc, uni.char(c)) end
342 return nfc
343 end
344
345 function unicode.conformance.toNFC(unistring)
346 return toNF_C_or_KC(unistring, false)
347 end
348
349 function unicode.conformance.toNFKC(unistring)
350 return toNF_C_or_KC(unistring, true)
351 end