Debian Codegraph @

Lib/fontFeatures/shaperLib/IndicShaperData.py

raw
1 from collections import OrderedDict
2 from enum import IntEnum
3 import re
4 from youseedee import ucd_data
5
6
7 script_config = {
8 "Invalid": dict(
9 old_spec=False,
10 virama=0,
11 base_pos="last",
12 reph_pos="before_post",
13 reph_mode="implicit",
14 blwf_mode="pre_and_post",
15 ),
16 "Devanagari": dict(
17 old_spec=True,
18 virama=0x094D,
19 base_pos="last",
20 reph_pos="before_post",
21 reph_mode="implicit",
22 blwf_mode="pre_and_post",
23 ),
24 "Bengali": dict(
25 old_spec=True,
26 virama=0x09CD,
27 base_pos="last",
28 reph_pos="after_sub",
29 reph_mode="implicit",
30 blwf_mode="pre_and_post",
31 ),
32 "Gurmukhi": dict(
33 old_spec=True,
34 virama=0x0A4D,
35 base_pos="last",
36 reph_pos="before_sub",
37 reph_mode="implicit",
38 blwf_mode="pre_and_post",
39 ),
40 "Gujarati": dict(
41 old_spec=True,
42 virama=0x0ACD,
43 base_pos="last",
44 reph_pos="before_post",
45 reph_mode="implicit",
46 blwf_mode="pre_and_post",
47 ),
48 "Oriya": dict(
49 old_spec=True,
50 virama=0x0B4D,
51 base_pos="last",
52 reph_pos="after_main",
53 reph_mode="implicit",
54 blwf_mode="pre_and_post",
55 ),
56 "Tamil": dict(
57 old_spec=True,
58 virama=0x0BCD,
59 base_pos="last",
60 reph_pos="after_post",
61 reph_mode="implicit",
62 blwf_mode="pre_and_post",
63 ),
64 "Telugu": dict(
65 old_spec=True,
66 virama=0x0C4D,
67 base_pos="last",
68 reph_pos="after_post",
69 reph_mode="explicit",
70 blwf_mode="post_only",
71 ),
72 "Kannada": dict(
73 old_spec=True,
74 virama=0x0CCD,
75 base_pos="last",
76 reph_pos="after_post",
77 reph_mode="implicit",
78 blwf_mode="post_only",
79 ),
80 "Malayalam": dict(
81 old_spec=True,
82 virama=0x0D4D,
83 base_pos="last",
84 reph_pos="after_main",
85 reph_mode="log_repha",
86 blwf_mode="pre_and_post",
87 ),
88 "Sinhala": dict(
89 old_spec=False,
90 virama=0x0DCA,
91 base_pos="last_sinhala",
92 reph_pos="after_post",
93 reph_mode="explicit",
94 blwf_mode="pre_and_post",
95 ),
96 }
97
98 # Go from UCD categories to the ones we care about
99
100 syllabic_category_map = {
101 "Other": "X",
102 "Avagraha": "Symbol",
103 "Bindu": "SM",
104 "Brahmi_Joining_Number": "PLACEHOLDER", # Don't care.
105 "Cantillation_Mark": "A",
106 "Consonant": "C",
107 "Consonant_Dead": "C",
108 "Consonant_Final": "CM",
109 "Consonant_Head_Letter": "C",
110 "Consonant_Killer": "M", # U+17CD only.
111 "Consonant_Medial": "CM",
112 "Consonant_Placeholder": "PLACEHOLDER",
113 "Consonant_Preceding_Repha": "Repha",
114 "Consonant_Prefixed": "X", # Don't care.
115 "Consonant_Subjoined": "CM",
116 "Consonant_Succeeding_Repha": "CM",
117 "Consonant_With_Stacker": "CS",
118 "Gemination_Mark": "SM", # https://github.com/harfbuzz/harfbuzz/issues/552
119 "Invisible_Stacker": "Coeng",
120 "Joiner": "ZWJ",
121 "Modifying_Letter": "X",
122 "Non_Joiner": "ZWNJ",
123 "Nukta": "N",
124 "Number": "PLACEHOLDER",
125 "Number_Joiner": "PLACEHOLDER", # Don't care.
126 "Pure_Killer": "M", # Is like a vowel matra.
127 "Register_Shifter": "RS",
128 "Syllable_Modifier": "SM",
129 "Tone_Letter": "X",
130 "Tone_Mark": "N",
131 "Virama": "H",
132 "Visarga": "SM",
133 "Vowel": "V",
134 "Vowel_Dependent": "M",
135 "Vowel_Independent": "V",
136 # The following come from reassignment
137 "Symbol": "Symbol",
138 "Ra": "Ra",
139 "Dotted_Circle": "DOTTEDCIRCLE",
140 }
141
142 ## Set up the state machine for syllable matching and syllabic
143 ## categories using composed regular expressions
144
145
146 states = OrderedDict(
147 c="C|Ra",
148 n="(ZWNJ?RS)?(N N?)?",
149 z="ZWJ|ZWNJ",
150 reph="Ra H|Repha",
151 cn="c ZWJ?n?",
152 forced_rakar="ZWJ H ZWJ Ra",
153 symbol="Symbol N?",
154 matra_group="z*M N?(H|forced_rakar)?",
155 syllable_tail="(z?SM SM?ZWNJ?)?A*",
156 halant_group="z?H(ZWJ N?)?",
157 final_halant_group="halant_group|H ZWNJ",
158 medial_group="CM?",
159 halant_or_matra_group="final_halant_group|matra_group*",
160 complex_syllable_tail="(halant_group cn)*medial_group halant_or_matra_group syllable_tail",
161 consonant_syllable="((Repha|CS)?cn complex_syllable_tail)",
162 vowel_syllable="(reph?V n?(complex_syllable_tail|ZWJ))",
163 standalone_cluster="((Repha|CS)?PLACEHOLDER|reph?DOTTEDCIRCLE)n? complex_syllable_tail",
164 symbol_cluster="symbol syllable_tail",
165 broken_cluster="reph? n? complex_syllable_tail",
166 )
167
168
169 def make_syllable_machine(states, additional_categories=[]):
170 categories = set(syllabic_category_map.values()) | set(additional_categories)
171 syllable_machine = {}
172 tail = r"\([^\)]+\)=\d+"
173 for value in categories:
174 syllable_machine[value] = f"<{value}>" + tail
175 syllable_machine["other"] = r"<[^\>]+>" + tail
176
177 for category, definition in states.items():
178 # Recursively replace any known states with their definitions:
179 states = (
180 "\\b("
181 + "|".join(sorted(syllable_machine.keys(), key=len, reverse=True))
182 + ")\\b"
183 )
184 definition = re.sub(
185 states, lambda match: "(" + syllable_machine[match[1]] + ")", definition
186 )
187 definition = definition.replace(" ", "")
188 syllable_machine[category] = definition
189 return syllable_machine
190
191
192 syllable_machine_indic = make_syllable_machine(states)
193
194
195 class IndicPosition(IntEnum):
196 START = 0
197
198 RA_TO_BECOME_REPH = 1
199 PRE_M = 2
200 PRE_C = 3
201
202 BASE_C = 4
203 AFTER_MAIN = 5
204
205 ABOVE_C = 6
206
207 BEFORE_SUB = 7
208 BELOW_C = 8
209 AFTER_SUB = 9
210
211 BEFORE_POST = 10
212 POST_C = 11
213 AFTER_POST = 12
214
215 FINAL_C = 13
216 SMVD = 14
217
218 END = 15
219
220
221 def is_ra(u):
222 return u in [
223 0x0930,
224 0x09B0,
225 0x09F0,
226 0x0A30,
227 0x0AB0,
228 0x0B30,
229 0x0BB0,
230 0x0C30,
231 0x0CB0,
232 0x0D30,
233 0x0DBB,
234 0x179A,
235 ]
236
237
238 def IndicPositionalCategory2IndicPosition(ipc):
239 if ipc == "Left":
240 return IndicPosition.PRE_C
241 if ipc == "Top":
242 return IndicPosition.ABOVE_C
243 if ipc == "Bottom":
244 return IndicPosition.BELOW_C
245
246 if ipc == "Right":
247 return IndicPosition.POST_C
248 if ipc == "Bottom_And_Right":
249 return IndicPosition.POST_C
250 if ipc == "Left_And_Right":
251 return IndicPosition.POST_C
252 if ipc == "Top_And_Bottom_And_Left":
253 return IndicPosition.POST_C
254
255 if ipc == "Top_And_Bottom":
256 return IndicPosition.BELOW_C
257 if ipc == "Top_And_Bottom_And_Left":
258 return IndicPosition.BELOW_C
259 if ipc == "Top_And_Bottom_And_Right":
260 return IndicPosition.POST_C
261
262 if ipc == "Top_And_Left":
263 return IndicPosition.ABOVE_C
264 if ipc == "Top_And_Left_And_Right":
265 return IndicPosition.POST_C
266 if ipc == "Top_And_Right":
267 return IndicPosition.POST_C
268
269 if ipc == "Overstruck":
270 return IndicPosition.AFTER_MAIN
271 if ipc == "Visual_Order_Left":
272 return IndicPosition.PRE_M
273
274 return IndicPosition.END
275
276
277 matra_pos_left = {"Default": IndicPosition.PRE_M}
278 matra_pos_right = {
279 # Telu and Knda need special cases
280 "Devanagari": IndicPosition.AFTER_SUB,
281 "Bengali": IndicPosition.AFTER_POST,
282 "Gurmukhi": IndicPosition.AFTER_POST,
283 "Gujarati": IndicPosition.AFTER_POST,
284 "Oriya": IndicPosition.AFTER_POST,
285 "Tamil": IndicPosition.AFTER_POST,
286 "Malayalam": IndicPosition.AFTER_POST,
287 "Default": IndicPosition.AFTER_SUB,
288 }
289 matra_pos_top = {
290 "Devanagari": IndicPosition.AFTER_SUB,
291 "Gurmukhi": IndicPosition.AFTER_POST,
292 "Gujarati": IndicPosition.AFTER_SUB,
293 "Oriya": IndicPosition.AFTER_MAIN,
294 "Tamil": IndicPosition.AFTER_SUB,
295 "Telugu": IndicPosition.BEFORE_SUB,
296 "Kannada": IndicPosition.BEFORE_SUB,
297 "Sinhala": IndicPosition.AFTER_SUB,
298 "Default": IndicPosition.AFTER_SUB,
299 }
300 matra_pos_bottom = {
301 "Devanagari": IndicPosition.AFTER_SUB,
302 "Bengali": IndicPosition.AFTER_SUB,
303 "Gurmukhi": IndicPosition.AFTER_POST,
304 "Gujarati": IndicPosition.AFTER_POST,
305 "Oriya": IndicPosition.AFTER_SUB,
306 "Tamil": IndicPosition.AFTER_POST,
307 "Telugu": IndicPosition.BEFORE_SUB,
308 "Malayalam": IndicPosition.AFTER_POST,
309 "Sinhala": IndicPosition.AFTER_SUB,
310 "Default": IndicPosition.AFTER_SUB,
311 }
312
313
314 def set_matra_position(item):
315 script = ucd_data(item.codepoint)["Script"]
316 u = item.codepoint
317 if item.syllabic_position == IndicPosition.PRE_C:
318 selector = matra_pos_left
319 elif item.syllabic_position == IndicPosition.POST_C:
320 selector = matra_pos_right
321 if script == "Telugu":
322 if u <= 0x0C42:
323 item.syllabic_position = IndicPosition.BEFORE_SUB
324 else:
325 item.syllabic_position = IndicPosition.AFTER_SUB
326 return
327 if script == "Kannada":
328 if u < 0x0CC3 or u > 0xCD6:
329 item.syllabic_position = IndicPosition.BEFORE_SUB
330 else:
331 item.syllabic_position = IndicPosition.AFTER_SUB
332 return
333 elif item.syllabic_position == IndicPosition.ABOVE_C:
334 selector = matra_pos_top
335 elif item.syllabic_position == IndicPosition.BELOW_C:
336 selector = matra_pos_bottom
337 else:
338 return
339 item.syllabic_position = selector.get(script, selector["Default"])
340
341
342 def reassign_category_and_position_indic(item):
343 cp = item.codepoint
344 if 0x0953 <= cp <= 0x0954:
345 item.syllabic_category = "SM"
346 elif 0x0A72 <= cp <= 0x0A73 or 0x1CF5 <= cp <= 0x1CF6:
347 item.syllabic_category = "C"
348 elif 0x1CE2 <= cp <= 0x1CE8 or cp == 0x1CED:
349 item.syllabic_category = "A"
350 elif 0xA8F2 <= cp <= 0xA8F7 or 0x1CE9 <= cp <= 0x1CEC or 0x1CEE <= cp <= 0x1CF1:
351 item.syllabic_category = "Symbol"
352 elif cp == 0x0A51: # https://github.com/harfbuzz/harfbuzz/issues/524
353 item.syllabic_category = "M"
354 item.syllabic_position = IndicPosition.BELOW_C
355 elif cp == 0x11301 or cp == 0x11303:
356 item.syllabic_category = "SM"
357 elif cp == 0x1133B or cp == 0x1133C:
358 item.syllabic_category = "N"
359 elif cp == 0x0AFB:
360 item.syllabic_category = "N"
361 elif cp == 0x0980 or cp == 0x09FC or cp == 0x0C80 or cp == 0x2010 or cp == 0x2011:
362 item.syllabic_category = "PLACEHOLDER"
363 elif cp == 0x25CC:
364 item.syllabic_category = "DOTTEDCIRCLE"
365
366 isc = item.syllabic_category
367
368 is_medial = isc == "CM"
369 is_consonant = (
370 isc in ["C", "CS", "Ra", "V", "PLACEHOLDER", "DOTTEDCIRCLE"] or is_medial
371 )
372
373 if is_consonant:
374 item.syllabic_position = IndicPosition.BASE_C
375 if is_ra(cp):
376 item.syllabic_category = "Ra"
377 elif isc == "M":
378 set_matra_position(item)
379 elif isc in ["SM", "A", "Symbol"]:
380 item.syllabic_position = IndicPosition.SMVD
381 if cp == 0x0B01:
382 item.syllabic_position = IndicPosition.BEFORE_SUB