Debian Codegraph @

texmf-dist/tex/luatex/lua-uni-algos/lua-uni-normalize.lua

raw
1 -- lua-uni-normalize.lua
2 -- Copyright 2020--2025 Marcel Krüger
3 --
4 -- This work may be distributed and/or modified under the
5 -- conditions of the LaTeX Project Public License, either version 1.3
6 -- of this license or (at your option) any later version.
7 -- The latest version of this license is in
8 -- http://www.latex-project.org/lppl.txt
9 -- and version 1.3 or later is part of all distributions of LaTeX
10 -- version 2005/12/01 or later.
11 --
12 -- This work has the LPPL maintenance status `maintained'.
13 --
14 -- The Current Maintainer of this work is Marcel Krüger
15
16 -- Provide all four kinds of Unicode normalization
17
18 local newtable = lua.newtable
19 local move = table.move
20 local char = utf8.char
21 local codes = utf8.codes
22 local unpack = table.unpack
23
24 if tex.initialize then
25 kpse.set_program_name'kpsewhich'
26 end
27 local ccc, composition_mapping, decomposition_mapping, compatibility_mapping, nfc_qc do
28 local function doubleset(ts, key, v1, kind, v2)
29 ts[1][key] = v1
30 ts[3][key] = v2
31 if not kind then
32 ts[2][key] = v2
33 end
34 return ts
35 end
36 local p = require'lua-uni-parse'
37 local l = lpeg
38 local Cnil = l.Cc(nil)
39 local letter = lpeg.R('AZ', 'az')
40 ccc, decomposition_mapping, compatibility_mapping
41 = unpack(p.parse_file('UnicodeData', l.Cf(
42 l.Ct(l.Ct'' * l.Ct'' * l.Ct'') * (
43 l.Cg(p.fields(p.codepoint,
44 p.ignore_field,
45 p.ignore_field,
46 '0' * Cnil + p.number,
47 p.ignore_field,
48 ('<' * l.C(letter^1) * '> ' + Cnil)
49 * l.Ct(p.codepoint * (' ' * p.codepoint)^0)^-1,
50 p.ignore_line)) + p.eol
51 )^0 * -1, doubleset)))
52
53 composition_mapping = {}
54 local composition_exclusions = { [0x00958] = true, [0x00959] = true,
55 [0x0095A] = true, [0x0095B] = true, [0x0095C] = true, [0x0095D] = true,
56 [0x0095E] = true, [0x0095F] = true, [0x009DC] = true, [0x009DD] = true,
57 [0x009DF] = true, [0x00A33] = true, [0x00A36] = true, [0x00A59] = true,
58 [0x00A5A] = true, [0x00A5B] = true, [0x00A5E] = true, [0x00B5C] = true,
59 [0x00B5D] = true, [0x00F43] = true, [0x00F4D] = true, [0x00F52] = true,
60 [0x00F57] = true, [0x00F5C] = true, [0x00F69] = true, [0x00F76] = true,
61 [0x00F78] = true, [0x00F93] = true, [0x00F9D] = true, [0x00FA2] = true,
62 [0x00FA7] = true, [0x00FAC] = true, [0x00FB9] = true, [0x0FB1D] = true,
63 [0x0FB1F] = true, [0x0FB2A] = true, [0x0FB2B] = true, [0x0FB2C] = true,
64 [0x0FB2D] = true, [0x0FB2E] = true, [0x0FB2F] = true, [0x0FB30] = true,
65 [0x0FB31] = true, [0x0FB32] = true, [0x0FB33] = true, [0x0FB34] = true,
66 [0x0FB35] = true, [0x0FB36] = true, [0x0FB38] = true, [0x0FB39] = true,
67 [0x0FB3A] = true, [0x0FB3B] = true, [0x0FB3C] = true, [0x0FB3E] = true,
68 [0x0FB40] = true, [0x0FB41] = true, [0x0FB43] = true, [0x0FB44] = true,
69 [0x0FB46] = true, [0x0FB47] = true, [0x0FB48] = true, [0x0FB49] = true,
70 [0x0FB4A] = true, [0x0FB4B] = true, [0x0FB4C] = true, [0x0FB4D] = true,
71 [0x0FB4E] = true,
72 [0x02ADC] = true, [0x1D15E] = true, [0x1D15F] = true, [0x1D160] = true,
73 [0x1D161] = true, [0x1D162] = true, [0x1D163] = true, [0x1D164] = true,
74 [0x1D1BB] = true, [0x1D1BC] = true, [0x1D1BD] = true, [0x1D1BE] = true,
75 [0x1D1BF] = true, [0x1D1C0] = true,
76 }
77
78 -- We map for NFC_QC:
79 -- No -> false
80 -- Maybe -> true
81 -- Yes -> nil
82 -- since Yes should be the default.
83 nfc_qc = {}
84 for cp, decomp in next, decomposition_mapping do
85 if ccc[cp] or ccc[decomp[1]] then
86 nfc_qc[cp] = false
87 elseif #decomp == 1 then
88 nfc_qc[cp] = false
89 elseif composition_exclusions[cp] then
90 nfc_qc[cp] = false
91 else
92 nfc_qc[decomp[2]] = true
93 end
94 end
95 for i=0x1161, 0x1175 do
96 nfc_qc[i] = true
97 end
98 for i=0x11A8, 0x11C2 do
99 nfc_qc[i] = true
100 end
101
102 for cp, decomp in next, decomposition_mapping do
103 if #decomp > 1 and not (composition_exclusions[cp] or ccc[decomp[1]]) then
104 local mapping = composition_mapping[decomp[1]]
105 if not mapping then
106 mapping = {}
107 composition_mapping[decomp[1]] = mapping
108 end
109 mapping[decomp[2]] = cp
110 end
111 end
112
113 local function fixup_decomp(decomp)
114 local first = decomp[1]
115 local first_decomp = decomposition_mapping[first]
116 if not first_decomp then return false end
117 if fixup_decomp(first_decomp) then
118 -- print('nested', first)
119 end
120 move(decomp, 2, #decomp, #first_decomp + 1)
121 move(first_decomp, 1, #first_decomp, 1, decomp)
122 return true
123 end
124 -- Fixup stage
125 for cp, decomp in next, decomposition_mapping do
126 if fixup_decomp(decomp) then
127 -- print(':(', cp)
128 end
129 end
130
131 -- NFKD edition
132 local DEBUG = false
133 local function fixup_decomp(orig, decomp)
134 local work
135 local shared = decomposition_mapping[orig] == decomp
136 local j = 0
137 for i = 1, #decomp do
138 local cp = decomp[i]
139 local cp_decomp = compatibility_mapping[cp]
140 if cp_decomp then
141 if shared then
142 local old = decomp
143 decomp = {}
144 compatibility_mapping[orig] = decomp
145 move(old, 1, #old, 1, decomp)
146 end
147 decomp[i] = cp_decomp
148 j = j + #cp_decomp
149 work = true
150 else
151 j = j + 1
152 end
153 end
154 if not work then return decomp end
155 for i = #decomp, 1, -1 do
156 local v = decomp[i]
157 if type(v) == 'number' then
158 decomp[j] = v
159 j = j - 1
160 else
161 local count = #v
162 move(v, 1, count, j - count + 1, decomp)
163 j = j - count
164 end
165 end
166 assert(j == 0)
167 return decomp
168 end
169 -- Fixup stage
170 for cp, decomp in next, compatibility_mapping do
171 fixup_decomp(cp, decomp)
172 end
173
174 --[[ To verify that nfc_qc is correctly generated
175 local ref_nfc_qc = p.parse_file('DerivedNormalizationProps', l.Cf(
176 l.Ct'' * (
177 l.Cg(p.fields(p.codepoint_range,
178 'NFC_QC',
179 'N' * l.Cc(false) + 'M' * l.Cc(true))) + p.ignore_line
180 )^0 * -1, p.multiset))
181 for k,v in next, ref_nfc_qc do
182 if nfc_qc[k] ~= v then
183 print('MISMATCH1', k, v, nfc_qc[k])
184 end
185 end
186 for k,v in next, nfc_qc do
187 if ref_nfc_qc[k] ~= v then
188 print('MISMATCH2', k, v)
189 end
190 end
191 ]]
192 end
193
194 local function ccc_reorder(codepoints, i, j, k)
195 if k >= j then return end
196 local first = codepoints[k]
197 local first_ccc = ccc[first]
198 if not first_ccc then
199 return ccc_reorder(codepoints, k+1, j, k+1)
200 end
201 local new_pos = k
202 local cur_ccc
203 repeat
204 new_pos = new_pos + 1
205 if new_pos > j then break end
206 local cur = codepoints[new_pos]
207 cur_ccc = ccc[cur]
208 until (not cur_ccc) or (cur_ccc >= first_ccc)
209 new_pos = new_pos - 1
210 if new_pos == k then
211 return ccc_reorder(codepoints, i, j, k+1)
212 end
213 move(codepoints, k+1, new_pos, k)
214 codepoints[new_pos] = first
215 return ccc_reorder(codepoints, i, j, k == i and i or k-1)
216 end
217
218 local result_table = {}
219 local function get_string()
220 local result_table = result_table
221 local s = char(unpack(result_table))
222 for i=1,#result_table do
223 result_table[i] = nil
224 end
225 return s
226 end
227 local function to_nfd_table(s, decomposition_mapping)
228 local new_codepoints = result_table
229 local j = 1
230 for _, c in codes(s) do
231 local decomposed = decomposition_mapping[c]
232 if decomposed then
233 move(decomposed, 1, #decomposed, j, new_codepoints)
234 j = j + #decomposed
235 elseif c >= 0xAC00 and c <= 0xD7A3 then
236 c = c - 0xAC00
237 local tIndex = c % 28
238 c = c // 28
239 local vIndex = c % 21
240 local lIndex = c // 21
241 new_codepoints[j] = 0x1100 + lIndex
242 new_codepoints[j+1] = 0x1161 + vIndex
243 if tIndex == 0 then
244 j = j + 2
245 else
246 new_codepoints[j+2] = 0x11A7 + tIndex
247 j = j + 3
248 end
249 else
250 new_codepoints[j] = c
251 j = j + 1
252 end
253 end
254 ccc_reorder(new_codepoints, 1, #new_codepoints, 1)
255 end
256 local function to_nfd(s)
257 to_nfd_table(s, decomposition_mapping)
258 return get_string()
259 end
260 local function to_nfkd(s)
261 to_nfd_table(s, compatibility_mapping)
262 return get_string()
263 end
264 local function to_nfc_generic(s, decomposition_mapping)
265 to_nfd_table(s, decomposition_mapping)
266 local codepoints = result_table
267 local starter, lookup, last_ccc, lvt
268 local j = 1
269 for i, c in ipairs(codepoints) do
270 local cur_ccc = ccc[c]
271 if lookup then
272 if (cur_ccc == nil) == (cur_ccc == last_ccc) then -- unblocked
273 local composed = lookup[c]
274 if composed then
275 codepoints[starter] = composed
276 lookup = composition_mapping[composed]
277 goto CONTINUE
278 end
279 end
280 elseif lvt then
281 if lvt == 1 then
282 if c >= 0x1161 and c <= 0x1175 then
283 lvt = 2
284 codepoints[starter] = ((codepoints[starter] - 0x1100) * 21 + c - 0x1161) * 28 + 0xAC00
285 goto CONTINUE
286 end
287 else -- if lvt == 2 then
288 if c >= 0x11A8 and c <= 0x11C2 then
289 lvt = nil
290 codepoints[starter] = codepoints[starter] + c - 0x11A7
291 goto CONTINUE
292 end
293 end
294 end
295 codepoints[j] = c
296 lvt = nil
297 if not cur_ccc then
298 starter = j
299 lookup = composition_mapping[c]
300 if not lookup and c >= 0x1100 and c <= 0x1112 then
301 lvt = 1
302 end
303 end
304 j = j + 1
305 last_ccc = cur_ccc
306 ::CONTINUE::
307 end
308 for i = j,#codepoints do codepoints[i] = nil end
309 return get_string()
310 end
311 local function to_nfc(s)
312 return to_nfc_generic(s, decomposition_mapping)
313 end
314 local function to_nfkc(s)
315 return to_nfc_generic(s, compatibility_mapping)
316 end
317
318 if tex.initialize then
319 return {
320 NFD = to_nfd,
321 NFC = to_nfc,
322 NFKD = to_nfkd,
323 NFKC = to_nfkc,
324 }
325 end
326
327 local direct = node.direct
328 local node_new = direct.new
329 local node_copy = direct.copy
330 local is_char = direct.is_char
331 local setchar = direct.setchar
332 local insert_after = direct.insert_after
333 local insert_before = direct.insert_before
334 local getnext = direct.getnext
335 local remove = direct.remove
336 local free = direct.free
337 local getattrlist = direct.getattributelist
338 local getprev = direct.getprev
339 local setprev = direct.setprev
340 local getboth = direct.getboth
341 local setlink = direct.setlink
342
343 -- allowed_characters only works reliably if it's closed under canonical decomposition mappings
344 -- but it should fail in reasonable ways as long as it's at least closed under full canonical decompositions
345 --
346 -- This could be adapted to NFKC as above except that we would either need to handle Hangul syllables
347 -- while iterating over starter_decomposition or adapt ~5 entries in compatibility_mapping to not decompose the syllables.
348 -- We don't do this currently since I don't see a usecase for NFKC normalized nodes.
349 local function nodes_to_nfc(head, f, allowed_characters, preserve_attr)
350 if not head then return head end
351 local tmp_node = node_new'temp'
352 -- This is more complicated since we want to ensure that nodes (including their attributes and properties) are preserved whenever possible
353 --
354 -- We use three passes:
355 -- 1&2. Decompose everything with NFC_Quick_Check == No and reorder marks
356 local last_ccc
357 local n = head
358 local prev = getprev(head)
359 setlink(tmp_node, head)
360 local require_work
361 while n do
362 local char = is_char(n, f)
363 if char then
364 local qc = nfc_qc[char]
365 if qc == false then
366 local decomposed = decomposition_mapping[char]
367 local available = true
368 if allowed_characters then
369 -- This is probably buggy for weird fonts
370 for i=1, #decomposed do
371 if not allowed_characters[decomposed[i]] then
372 available = false
373 break
374 end
375 end
376 end
377 if available then
378 -- Here we never want to compose again, so we can decompose directly
379 local n = n
380 char = decomposed[1]
381 qc = nfc_qc[char]
382 setchar(n, char)
383 for i=2, #decomposed do
384 local nn = node_copy(n)
385 setchar(nn, decomposed[i])
386 insert_after(head, n, nn)
387 n = nn
388 end
389 end
390 end
391 -- Now reorder marks. The goal here is to reduce the overhead
392 -- in the common case that no reordering is needed
393 local this_ccc = ccc[char]
394 if last_ccc and this_ccc and last_ccc > this_ccc then
395 local nn = n
396 while nn ~= tmp_node do
397 nn = getprev(nn)
398 local nn_char = is_char(nn, f)
399 if not nn_char then break end
400 local nn_ccc = ccc[nn_char]
401 if not nn_ccc or nn_ccc <= this_ccc then break end
402 end
403 local before, after = getboth(n)
404 insert_after(head, nn, n)
405 setlink(before, after)
406 n = after
407 else
408 n = getnext(n)
409 last_ccc = this_ccc
410 end
411 require_work = require_work or qc
412 else
413 n = getnext(n)
414 last_ccc = nil
415 end
416 end
417 head = getnext(tmp_node)
418 setprev(head, prev)
419 if not require_work then
420 free(tmp_node)
421 return head
422 end
423 -- 3. The rest: Maybe decompose and then compose again
424 local starter_n, starter, lookup
425 local starter_decomposition
426 local last_ccc
427 local i -- index into starter_decomposition
428 local i_ccc
429 n = head
430 insert_after(head, nil, tmp_node)
431 repeat
432 local char = is_char(n, f)
433 local this_ccc = ccc[char] or 300
434 local is_composed -- Did we generate char through composition?
435 while i and i_ccc <= this_ccc do
436 local new_starter = lookup and lookup[starter_decomposition[i]]
437 if new_starter and (not allowed_characters or allowed_characters[new_starter]) then
438 starter = new_starter
439 setchar(starter_n, starter)
440 lookup = composition_mapping[starter]
441 else
442 local nn = node_copy(starter_n)
443 setchar(nn, starter_decomposition[i])
444 insert_before(head, n, nn)
445 last_ccc = i_ccc
446 end
447 i = i + 1
448 local i_char = starter_decomposition[i]
449 if i_char then
450 i_ccc = ccc[starter_decomposition[i]] or 300
451 else
452 i = nil
453 end
454 end
455 if char then
456 if lookup and (this_ccc == 300) == (this_ccc == last_ccc) then
457 local new_starter = lookup[char]
458 if new_starter and (not allowed_characters or allowed_characters[new_starter]) and (not preserve_attr or getattrlist(starter_n) == getattrlist(n)) then
459 local last = getprev(n)
460 remove(head, n)
461 free(n)
462 n = last
463 starter = new_starter
464 setchar(starter_n, starter)
465 char, is_composed = starter, true
466 lookup = composition_mapping[starter]
467 else
468 last_ccc = this_ccc
469 end
470 -- Now handle Hangul syllables. We never decompose them since we would just recompose them anyway and they are starters
471 elseif not lookup and this_ccc == 300 and last_ccc == 300 then
472 if starter >= 0x1100 and starter <= 0x1112 and char >= 0x1161 and char <= 0x1175 then -- L + V -> LV
473 local new_starter = ((starter - 0x1100) * 21 + char - 0x1161) * 28 + 0xAC00
474 if (not allowed_characters or allowed_characters[new_starter]) and (not preserve_attr or getattrlist(starter_n) == getattrlist(n)) then
475 remove(head, n)
476 free(n)
477 starter = new_starter
478 setchar(starter_n, starter)
479 char, is_composed = starter, true
480 lookup = composition_mapping[starter]
481 n = starter_n
482 end
483 elseif char >= 0x11A8 and char <= 0x11C2 and starter >= 0xAC00 and starter <= 0xD7A3 and (starter-0xAC00) % 28 == 0 then -- LV + T -> LVT
484 local new_starter = starter + char - 0x11A7
485 if (not allowed_characters or allowed_characters[new_starter]) and (not preserve_attr or getattrlist(starter_n) == getattrlist(n)) then
486 remove(head, n)
487 free(n)
488 starter = new_starter
489 setchar(starter_n, starter)
490 char, is_composed = starter, true
491 lookup = composition_mapping[starter]
492 n = starter_n
493 end
494 end
495 else
496 last_ccc = this_ccc
497 end
498 if this_ccc == 300 then
499 starter_n = n
500 if is_composed then -- If we just composed starter, we don't want to decompose it again
501 starter = char
502 else
503 starter_decomposition = decomposition_mapping[char]
504 if allowed_characters and starter_decomposition then
505 for i=1, #starter_decomposition do
506 if not allowed_characters[starter_decomposition[i]] then
507 starter_decomposition = nil
508 break
509 end
510 end
511 end
512 starter = starter_decomposition and starter_decomposition[1] or char
513 setchar(starter_n, starter)
514 if starter_decomposition then
515 i, i_ccc = 2, ccc[starter_decomposition[2]] or 300
516 else
517 i, i_ccc = nil
518 end
519 end
520 lookup = composition_mapping[starter]
521 end
522 else
523 starter, lookup, last_ccc, last_decomposition, i, i_ccc = nil
524 end
525 if n == tmp_node then
526 remove(head, tmp_node)
527 break
528 end
529 n = getnext(n)
530 until false
531 free(tmp_node)
532 return head
533 end
534
535 -- This is almost the same as the first loop from nodes_to_nfc, just without checking for NFC_QC and decomposing everything instead.
536 -- Also we have to decompose Hangul syllables.
537 -- No preserve_attr parameter since we never compose.
538 local function nodes_to_nfd_generic(decomposition_mapping, head, f, allowed_characters)
539 if not head then return head end
540 local tmp_node = node_new'temp'
541 -- This is more complicated since we want to ensure that nodes (including their attributes and properties) are preserved whenever possible
542 --
543 -- We use three passes:
544 -- 1&2. Decompose everything with NFC_Quick_Check == No and reorder marks
545 local last_ccc
546 local n = head
547 local prev = getprev(head)
548 setlink(tmp_node, head)
549 while n do
550 local char = is_char(n, f)
551 if char then
552 local decomposed = decomposition_mapping[char]
553 if decomposed then
554 local available = true
555 if allowed_characters then
556 -- This is probably buggy for weird fonts
557 for i=1, #decomposed do
558 if not allowed_characters[decomposed[i]] then
559 available = false
560 break
561 end
562 end
563 end
564 if available then
565 local n = n
566 char = decomposed[1]
567 setchar(n, char)
568 for i=2, #decomposed do
569 local nn = node_copy(n)
570 setchar(nn, decomposed[i])
571 insert_after(head, n, nn)
572 n = nn
573 end
574 end
575 elseif char >= 0xAC00 and char <= 0xD7A3 then -- Hangul clusters. In this case we update n since we never need to reorder them anyway
576 local c = char - 0xAC00
577 local t = 0x11A7 + c % 28
578 c = c // 28
579 local l = 0x1100 + c // 21
580 local v = 0x1161 + c % 21
581 if not allowed_characters or (allowed_characters[l] and allowed_characters[v] and (t == 0x11A7 or allowed_characters[t])) then
582 setchar(n, l)
583 local nn = node_copy(n)
584 setchar(nn, v)
585 insert_after(head, n, nn)
586 n = nn
587 char = v
588 if t ~= 0x11A7 then
589 nn = node_copy(n)
590 setchar(nn, t)
591 insert_after(head, n, nn)
592 n = nn
593 char = t
594 end
595 end
596 end
597 -- Now reorder marks. The goal here is to reduce the overhead
598 -- in the common case that no reordering is needed
599 local this_ccc = ccc[char]
600 if last_ccc and this_ccc and last_ccc > this_ccc then
601 local nn = n
602 while nn ~= tmp_node do
603 nn = getprev(nn)
604 local nn_char = is_char(nn, f)
605 if not nn_char then break end
606 local nn_ccc = ccc[nn_char]
607 if not nn_ccc or nn_ccc <= this_ccc then break end
608 end
609 local before, after = getboth(n)
610 insert_after(head, nn, n)
611 setlink(before, after)
612 n = after
613 else
614 n = getnext(n)
615 last_ccc = this_ccc
616 end
617 else
618 n = getnext(n)
619 last_ccc = nil
620 end
621 end
622 head = getnext(tmp_node)
623 setprev(head, prev)
624 free(tmp_node)
625 return head
626 end
627 local function nodes_to_nfd(head, f, allowed_characters)
628 return nodes_to_nfd_generic(decomposition_mapping, head, f, allowed_characters)
629 end
630 local function nodes_to_nfkd(head, f, allowed_characters)
631 return nodes_to_nfd_generic(compatibility_mapping, head, f, allowed_characters)
632 end
633
634 local todirect, tonode = direct.todirect, direct.tonode
635
636 return {
637 NFD = to_nfd,
638 NFC = to_nfc,
639 NFKD = to_nfkd,
640 NFKC = to_nfkc,
641 node = {
642 NFC = function(head, ...) return tonode(nodes_to_nfc(todirect(head), ...)) end,
643 NFD = function(head, ...) return tonode(nodes_to_nfd(todirect(head), ...)) end,
644 NFKD = function(head, ...) return tonode(nodes_to_nfkd(todirect(head), ...)) end,
645 },
646 direct = {
647 NFC = nodes_to_nfc,
648 NFD = nodes_to_nfd,
649 NFKD = nodes_to_nfkd,
650 },
651 }
652 -- print(require'inspect'{to_nfd{0x1E0A}, to_nfc{0x1E0A}})
653
654 -- print(require'inspect'{to_nfd{0x1100, 0x1100, 0x1161, 0x11A8}, to_nfc{0x1100, 0x1100, 0x1161, 0x11A8}})