Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-case.lua

raw
1 local unicode_data = require'luaotfload-unicode'
2 local bcp47 = require'luaotfload-bcp47'
3
4 local mapping_tables = unicode_data.casemapping
5 local soft_dotted = unicode_data.soft_dotted
6 local ccc = unicode_data.ccc
7
8 local uppercase = mapping_tables.uppercase
9 local lowercase = mapping_tables.lowercase
10 local cased = mapping_tables.cased
11 local case_ignorable = mapping_tables.case_ignorable
12
13 local otfregister = fonts.constructors.features.otf.register
14
15 local direct = node.direct
16 local is_char = direct.is_char
17 local has_glyph = direct.has_glyph
18 local uses_font = direct.uses_font
19 local getnext = direct.getnext
20 local setchar = direct.setchar
21 local setdisc = direct.setdisc
22 local getdisc = direct.getdisc
23 local getfield = direct.getfield
24 local remove = direct.remove
25 local free = direct.free
26 local copy = direct.copy
27 local insert_after = direct.insert_after
28 local traverse = direct.traverse
29
30 local report = luaotfload.log.report
31
32 local disc = node.id'disc'
33
34 --[[ We make some implicit assumptions about contexts in SpecialCasing.txt here which happened to be true when I wrote the code:
35 --
36 -- * Before_Dot only appears as Not_Before_Dot
37 -- * No other context appears with Not_
38 -- * Final_Sigma is never language dependent
39 -- * Other contexts are always language dependent
40 -- * The only languages with special mappings are Lithuanian (lt/"LTH "/lit), Turkish (tr/"TRK "/tur), and Azeri/Azerbaijani (az/"AZE "/aze)
41 -- (Additionally we add special mappings for de-x-eszett, el, el-x-iota, hy-x-yiwn which are not present in SpecialCasing.txt)
42 ]]
43
44 local UPPER_MASK = 0x3FF
45 local HAS_VOWEL = 0x200000
46 local HAS_YPOGEGRAMMENI = 0x400000
47 local HAS_ACCENT = 0x800000
48 local HAS_DIALYTIKA = 0x1000000
49 local HAS_OTHER_GREEK_DIACRITIC = 0x2000000
50
51 local greek_data
52 local greek_diacritic = {
53 [0x0300] = HAS_ACCENT,
54 [0x0301] = HAS_ACCENT,
55 [0x0342] = HAS_ACCENT,
56 [0x0302] = HAS_ACCENT,
57 [0x0303] = HAS_ACCENT,
58 [0x0311] = HAS_ACCENT,
59 [0x0308] = HAS_DIALYTIKA,
60 [0x0344] = HAS_DIALYTIKA | HAS_ACCENT,
61 [0x0345] = HAS_YPOGEGRAMMENI,
62 [0x0304] = HAS_OTHER_GREEK_DIACRITIC,
63 [0x0306] = HAS_OTHER_GREEK_DIACRITIC,
64 [0x0313] = HAS_OTHER_GREEK_DIACRITIC,
65 [0x0314] = HAS_OTHER_GREEK_DIACRITIC,
66 [0x0343] = HAS_OTHER_GREEK_DIACRITIC,
67 }
68
69 local greek_precombined_iota = {
70 [0x0391] = 0x1FBC,
71 [0x0397] = 0x1FCC,
72 [0x03A9] = 0x1FFC,
73 }
74
75 -- Greek handling based on https://icu.unicode.org/design/case/greek-upper
76 -- with smaller variations since we ant to preserve nodes whenever possible.
77 local function init_greek_data()
78 local NFD = require'lua-uni-normalize'.NFD
79 local data = {}
80 greek_data = data
81
82 local vowels = {
83 [utf8.codepoint'Α'] = true, [utf8.codepoint'Ε'] = true,
84 [utf8.codepoint'Η'] = true, [utf8.codepoint'Ι'] = true,
85 [utf8.codepoint'Ο'] = true, [utf8.codepoint'Ω'] = true,
86 [utf8.codepoint'Υ'] = true,
87 }
88 local function handle_char(c)
89 local decomp = NFD(utf8.char(c))
90 local first = utf8.codepoint(decomp)
91 local upper = uppercase[first]
92 if upper then
93 if not tonumber(upper) then
94 upper = upper._
95 assert(#upper == 1)
96 upper = upper[1]
97 end
98 else
99 upper = first
100 end
101 if upper > UPPER_MASK then return end -- Only happens for unassigned codepoints
102 local datum = upper
103 if vowels[upper] then
104 datum = datum | HAS_VOWEL
105 end
106 if utf8.len(decomp) > 1 then
107 for _, c in utf8.codes(decomp) do
108 local dia = greek_diacritic[c]
109 if dia and dia ~= HAS_OTHER_GREEK_DIACRITIC then datum = datum | dia end
110 end
111 end
112 data[c] = datum
113 end
114 for c = 0x0370, 0x03ff do handle_char(c) end
115 for c = 0x1f00, 0x1fff do handle_char(c) end
116 for c = 0x2126, 0x2126 do handle_char(c) end
117 end
118
119 local relevant_languages = {
120 lt = true,
121 tr = true,
122 az = true,
123 hy = {_ = true, yiwn = true},
124 el = {_ = true, iota = true},
125 de = {_ = false, eszett = true},
126 }
127
128 local function font_lang(feature)
129 return setmetatable({}, {__index = function(t, fid)
130 local f = font.getfont(fid)
131 local features = f.specification.features.normal
132 local lang = features[feature]
133 if type(lang) ~= 'string' or lang == 'auto' then
134 lang = features.language
135 lang = lang == 'lth' and 'lt'
136 or lang == 'trk' and 'tr'
137 or lang == 'aze' and 'az'
138 or lang == 'hye' and 'hy'
139 or (lang == 'ell' or lang == 'pgr') and 'el'
140 or false
141 end
142 if lang == 'de-alt' then
143 lang = 'de-x-eszett'
144 end
145 local parsed = lang and bcp47.parse(lang)
146 if lang and not parsed then
147 report('luaotfload-case', 0, 'Unable to parse passed language tag')
148 end
149 lang = parsed and parsed.language
150 local subtags = lang and relevant_languages[lang]
151 if subtags then
152 local private = parsed.private
153 if subtags ~= true and private then
154 local first = true
155 for _, ext in ipairs(private) do
156 if subtags[ext] then
157 if first then
158 lang = lang .. '-x'
159 first = nil
160 end
161 lang = lang .. '-' .. ext
162 end
163 end
164 if first then
165 lang = subtags._ and lang
166 end
167 end
168 else
169 lang = false
170 end
171 t[fid] = lang
172 return lang
173 end})
174 end
175
176 local function is_followed_by_cased(font, n, after)
177 n = getnext(n)
178 repeat
179 while n do
180 local char, id = is_char(n, font)
181 if not char and id == disc then
182 after = getnext(n)
183 n = getfield(n, 'replace')
184 char, id = is_char(n, font)
185 end
186 if char then
187 if not case_ignorable[char] then
188 return cased[char]
189 end
190 n = getnext(n)
191 else
192 return false
193 end
194 end
195 n, after = after
196 until not n
197 return false
198 end
199
200 local function is_Final_Sigma(font, mapping, n, after)
201 mapping = mapping.Final_Sigma
202 if not mapping then return false end
203 mapping = mapping._
204 if not mapping then return false end
205 return not is_followed_by_cased(font, n, after) and mapping
206 end
207
208 local function is_More_Above(font, mapping, n, after)
209 mapping = mapping.More_Above
210 if not mapping then return false end
211 mapping = mapping._
212 if not mapping then return false end
213 n = getnext(n)
214 repeat
215 while n do
216 local char, id = is_char(n, font)
217 if id == disc then
218 after = getnext(n)
219 n = getfield(n, 'replace')
220 char, id = is_char(n, font)
221 elseif char then
222 local char_ccc = ccc[char]
223 if not char_ccc then
224 return false
225 elseif char_ccc == 230 then
226 return mapping
227 end
228 n = getnext(n)
229 else
230 return false
231 end
232 end
233 n, after = after
234 until not n
235 return false
236 end
237
238 local function is_Not_Before_Dot(font, mapping, n, after)
239 mapping = mapping.Not_Before_Dot
240 if not mapping then return false end
241 mapping = mapping._
242 if not mapping then return false end
243 n = getnext(n)
244 repeat
245 while n do
246 local char, id = is_char(n, font)
247 if id == disc then
248 after = getnext(n)
249 n = getfield(n, 'replace')
250 char, id = is_char(n, font)
251 elseif char then
252 local char_ccc = ccc[char]
253 if not char_ccc then
254 return mapping
255 elseif char_ccc == 230 then
256 return char ~= 0x0307 and mapping
257 end
258 n = getnext(n)
259 else
260 return mapping
261 end
262 end
263 n, after = after
264 until not n
265 return mapping
266 end
267
268 local function is_Language_Mapping(font, mapping, n, after, seen_soft_dotted, seen_I)
269 if not mapping then return false end
270 if seen_soft_dotted then
271 local mapping = mapping.After_Soft_Dotted
272 mapping = mapping and mapping._
273 if mapping then
274 return mapping
275 end
276 end
277 if seen_I then
278 local mapping = mapping.After_I
279 mapping = mapping and mapping._
280 if mapping then
281 return mapping
282 end
283 end
284 return is_More_Above(font, mapping, n, after) or is_Not_Before_Dot(font, mapping, n, after) or mapping._ -- Might be nil
285 end
286
287 local function process(table, feature)
288 local font_lang = font_lang(feature)
289 -- The other seen_... are booleans, while seen_greek has more states:
290 -- - nil: Not greek
291 -- - true: Greek. Last was not a vowel with accent and without dialytika
292 -- - node: Greek. Last vowel with accent and without dialytika
293 local function processor(head, font, after, seen_cased, seen_soft_dotted, seen_I, seen_greek)
294 local lang = font_lang[font]
295 local greek, greek_iota
296 if lang == 'el' or lang == 'el-x-iota' then
297 if table == uppercase then
298 if not greek_data then
299 init_greek_data()
300 end
301 greek, greek_iota = greek_data, lang == 'el-x-iota'
302 end
303 lang = false
304 end
305 local n = head
306 while n do
307 do
308 local new = has_glyph(n)
309 if n ~= new then
310 seen_cased, seen_soft_dotted, seen_I, seen_greek = nil
311 end
312 n = new
313 end
314 if not n then break end
315 local char, id = is_char(n, font)
316 if char then
317 if greek and (char >= 0x0370 and char <= 0x03ff or char >= 0x1f00 and char <= 0x1fff or char == 0x2126) then
318 -- In the greek uppercase situation we want to remove diacritics except under some exceptions.
319 local first_datum = greek[char] or 0
320 local datum = first_datum
321 local upper = datum & UPPER_MASK
322 -- When a vowel ges an accent removed and does not have a dialytika and is followed by a Ι or Υ,
323 -- then this iota or ypsilon gets a dialytika.
324 if datum & HAS_VOWEL ~= 0 and seen_greek and seen_greek ~= true and (upper == 0x0399 or upper == 0x03a5) then
325 datum = datum | HAS_DIALYTIKA;
326 end
327 local has_ypogegrammeni = datum & HAS_YPOGEGRAMMENI ~= 0
328 local add_ypogegrammeni = has_ypogegrammeni
329 local post = getnext(n)
330 local last
331 local saved_tonos, saved_dialytika
332 while post do
333 local char = is_char(post, font)
334 if not char then break end
335 local diacritic_data = greek_diacritic[char]
336 if not diacritic_data then break end
337 -- Preserve flags to be aware if a dialytika has to be reinserted
338 -- TODO: Keep dialytika node around
339 datum = datum | diacritic_data
340 -- Preserve ypogegrammeni (iota subscript) but convert them into capital iotas.
341 -- If el-x-iota is active keep the combining character instead.
342 if diacritic_data & HAS_YPOGEGRAMMENI ~= 0 then
343 has_ypogegrammeni = true
344 if not greek_iota then
345 setchar(post, 0x0399)
346 end
347 last = post
348 post = getnext(post)
349 else
350 -- Otherwise they get removed
351 local old = post
352 head, post = remove(head, post)
353 if char == 0x0301 and not saved_tonos then
354 -- But if we have a tonos we might want to reinsert it later
355 saved_tonos = old
356 elseif diacritic_data & HAS_DIALYTIKA ~= 0 and not saved_dialytika then
357 -- Similar for dilytika
358 saved_dialytika = old
359 else
360 free(old)
361 end
362 end
363 end
364 -- Special case: An isolated Ή preserves the tonos.
365 if upper == 0x0397
366 and not has_ypogegrammeni
367 and not seen_cased
368 and not is_followed_by_cased(font, n, after)
369 then
370 if first_datum & HAS_ACCENT ~= 0 then
371 upper = 0x0389
372 -- If it's precomposed we don't have to keep any combining accents
373 if saved_tonos then
374 free(saved_tonos)
375 saved_tonos = nil
376 end
377 end
378 else
379 -- Not the special case so we don't have to keep the tonos node
380 if saved_tonos then
381 free(saved_tonos)
382 saved_tonos = nil
383 end
384 -- Handle precomposed dialytika. If both a combining ans a precomposed
385 -- dialyika are present (typically because the precomposed one is
386 -- automatically added at the beginning) prefer the combining one to
387 -- preserve attributes.
388 if datum & HAS_DIALYTIKA ~= 0 and not saved_dialytika then
389 if upper == 0x0399 then -- upper == 'Ι'
390 upper = 0x03AA
391 elseif upper == 0x03A5 then -- upper == 'Υ'
392 upper = 0x03AB
393 else
394 assert(false) -- Should not be possible
395 end
396 end
397 end
398 if greek_iota and add_ypogegrammeni then
399 local mapped = greek_precombined_iota[upper]
400 if mapped then -- AFAICT always true
401 upper = mapped
402 add_ypogegrammeni = false
403 end
404 end
405 setchar(n, upper)
406 -- Potentially reinsert accents
407 if saved_dialytika then
408 head, n = insert_after(head, n, saved_dialytika)
409 setchar(n, 0x0308) -- Needed since we might have a U+0344 (COMBINING GREEK DIALYTIKA TONOS)
410 end
411 if saved_tonos then
412 head, n = insert_after(head, n, saved_tonos)
413 end
414 if add_ypogegrammeni then
415 head, n = insert_after(head, n, copy(n))
416 setchar(n, 0x0399)
417 end
418 -- If we preserved any combining ypogegrammeni nodes, skip them now
419 n = last or n
420 seen_greek = datum & (HAS_VOWEL | HAS_ACCENT | HAS_DIALYTIKA) == HAS_VOWEL | HAS_ACCENT and n or true
421 seen_I, seen_soft_dotted = nil
422 else
423 local mapping = table[char]
424 if mapping then
425 if tonumber(mapping) then
426 setchar(n, mapping)
427 else
428 mapping = seen_cased and is_Final_Sigma(font, mapping, n, after)
429 or lang and is_Language_Mapping(font, mapping[lang], n, after, seen_soft_dotted, seen_I)
430 or mapping._
431 if #mapping == 0 then
432 local old = n
433 head, n = remove(head, n)
434 free(old)
435 goto continue
436 else
437 setchar(n, mapping[1])
438 for i=2, #mapping do
439 head, n = insert_after(head, n, copy(n))
440 setchar(n, mapping[i])
441 end
442 end
443 end
444 end
445 local char_ccc = ccc[char]
446 if not char_ccc or char_ccc == 230 then
447 seen_I = char == 0x49 or nil
448 seen_soft_dotted = soft_dotted[char]
449 end
450 seen_greek = nil
451 end
452 if not case_ignorable[char] then
453 seen_cased = cased[char] or nil
454 end
455 elseif id == disc and uses_font(n, font) then
456 local pre, post, rep = getdisc(n)
457 local after = getnext(n)
458 pre, post, rep, seen_cased, seen_soft_dotted, seen_I, seen_greek =
459 processor(pre, font, nil, seen_cased, seen_soft_dotted, seen_I, seen_greek),
460 processor(post, font, after, seen_greek),
461 processor(rep, font, after, seen_cased, seen_soft_dotted, seen_I, seen_greek)
462 setdisc(n, pre, post, rep)
463 else
464 seen_cased, seen_soft_dotted, seen_I = nil
465 end
466 n = getnext(n)
467 ::continue::
468 end
469 return head, seen_cased, seen_soft_dotted, seen_I, seen_greek
470 end
471 return function(head, font, ...) return (processor(head, font)) end
472 end
473
474 local upper_process = process(uppercase, 'upper')
475 otfregister {
476 name = 'upper',
477 description = 'Map to uppercase',
478 default = false,
479 processors = {
480 position = 1,
481 plug = upper_process,
482 node = upper_process,
483 base = upper_process,
484 },
485 }
486
487 local lower_process = process(lowercase, 'lower')
488 otfregister {
489 name = 'lower',
490 description = 'Map to lowercase',
491 default = false,
492 processors = {
493 position = 1,
494 plug = lower_process,
495 node = lower_process,
496 base = lower_process,
497 },
498 }