| 1 |
from youseedee import ucd_data |
| 2 |
from fontFeatures.shaperLib.Buffer import BufferItem |
| 3 |
from .IndicShaperData import ( |
| 4 |
script_config, |
| 5 |
syllable_machine_indic, |
| 6 |
IndicPosition, |
| 7 |
reassign_category_and_position_indic, |
| 8 |
) |
| 9 |
from .SyllabicShaper import SyllabicShaper |
| 10 |
import unicodedata |
| 11 |
|
| 12 |
DOTTED_CIRCLE = 0x25CC |
| 13 |
|
| 14 |
|
| 15 |
class IndicShaper(SyllabicShaper): |
| 16 |
syllable_machine = syllable_machine_indic |
| 17 |
syllable_types = [ |
| 18 |
"consonant_syllable", |
| 19 |
"vowel_syllable", |
| 20 |
"standalone_cluster", |
| 21 |
"symbol_cluster", |
| 22 |
"broken_cluster", |
| 23 |
"other", |
| 24 |
] |
| 25 |
|
| 26 |
@property |
| 27 |
def config(self): |
| 28 |
return script_config.get(self.buffer.script, script_config["Invalid"]) |
| 29 |
|
| 30 |
def override_features(self, shaper): |
| 31 |
shaper.disable_feature("liga") |
| 32 |
|
| 33 |
def consonant_position_from_face(self, consonant): |
| 34 |
virama = self.config["virama"] |
| 35 |
consonant_item = BufferItem.new_unicode(consonant) |
| 36 |
virama_item = BufferItem.new_unicode(virama) |
| 37 |
consonant_item.map_to_glyph(self.buffer.font) |
| 38 |
virama_item.map_to_glyph(self.buffer.font) |
| 39 |
|
| 40 |
if self.would_substitute("blwf", [virama_item, consonant_item]): |
| 41 |
return IndicPosition.BELOW_C |
| 42 |
if self.would_substitute("blwf", [consonant_item, virama_item]): |
| 43 |
return IndicPosition.BELOW_C |
| 44 |
if self.would_substitute("vatu", [virama_item, consonant_item]): |
| 45 |
return IndicPosition.BELOW_C |
| 46 |
if self.would_substitute("vatu", [consonant_item, virama_item]): |
| 47 |
return IndicPosition.BELOW_C |
| 48 |
|
| 49 |
if self.would_substitute("pstf", [virama_item, consonant_item]): |
| 50 |
return IndicPosition.POST_C |
| 51 |
if self.would_substitute("pstf", [consonant_item, virama_item]): |
| 52 |
return IndicPosition.POST_C |
| 53 |
|
| 54 |
if self.would_substitute("pref", [virama_item, consonant_item]): |
| 55 |
return IndicPosition.POST_C |
| 56 |
if self.would_substitute("pref", [consonant_item, virama_item]): |
| 57 |
return IndicPosition.POST_C |
| 58 |
return IndicPosition.BASE_C |
| 59 |
|
| 60 |
def initial_reordering_pre(self): |
| 61 |
if self.config["base_pos"] == "last": # Not Sinhala |
| 62 |
for item in self.buffer.items: |
| 63 |
if item.syllabic_position == IndicPosition.BASE_C: |
| 64 |
item.syllabic_position = self.consonant_position_from_face( |
| 65 |
item.codepoint |
| 66 |
) |
| 67 |
pass |
| 68 |
|
| 69 |
def reassign_category(self, item): |
| 70 |
reassign_category_and_position_indic(item) |
| 71 |
|
| 72 |
def initial_reordering_consonant_syllable(self, start, end): |
| 73 |
def cat(i): |
| 74 |
return self.buffer.items[i].syllabic_category |
| 75 |
|
| 76 |
def pos(i): |
| 77 |
return self.buffer.items[i].syllabic_position |
| 78 |
|
| 79 |
def swap(a, b): |
| 80 |
self.buffer.items[b], self.buffer.items[a] = ( |
| 81 |
self.buffer.items[a], |
| 82 |
self.buffer.items[b], |
| 83 |
) |
| 84 |
|
| 85 |
def is_joiner(n): |
| 86 |
return cat(n) == "ZWJ" or cat(n) == "ZWNJ" |
| 87 |
|
| 88 |
def is_consonant(n): |
| 89 |
isc = cat(n) |
| 90 |
is_medial = isc == "CM" |
| 91 |
return ( |
| 92 |
isc in ["C", "CS", "Ra", "V", "PLACEHOLDER", "DOTTEDCIRCLE"] |
| 93 |
or is_medial |
| 94 |
) |
| 95 |
|
| 96 |
if ( |
| 97 |
self.buffer.script == "Kannada" |
| 98 |
and start + 3 <= end |
| 99 |
and cat(start) == "Ra" |
| 100 |
and cat(start + 1) == "H" |
| 101 |
and cat(start + 2) == "ZWJ" |
| 102 |
): |
| 103 |
swap(start + 1, start + 2) |
| 104 |
|
| 105 |
syllable_index = self.buffer.items[start].syllable_index |
| 106 |
|
| 107 |
base = end |
| 108 |
has_reph = False |
| 109 |
|
| 110 |
limit = start |
| 111 |
if ( |
| 112 |
"rphf" in self.plan.fontfeatures.features |
| 113 |
and start + 3 <= end |
| 114 |
and ( |
| 115 |
(self.config["reph_mode"] == "implicit" and not is_joiner(start + 2)) |
| 116 |
or (self.config["reph_mode"] == "explicit" and cat(start + 2) == "ZWJ") |
| 117 |
) |
| 118 |
): |
| 119 |
if self.would_substitute( |
| 120 |
"rphf", self.buffer.items[start : start + 2] |
| 121 |
) or self.would_substitute("rphf", self.buffer.items[start : start + 3]): |
| 122 |
limit = limit + 2 |
| 123 |
while limit < end and is_joiner(limit): |
| 124 |
limit = limit + 1 |
| 125 |
base = start |
| 126 |
has_reph = True |
| 127 |
elif self.config["reph_mode"] == "log_repha" and cat(start) == "Repha": |
| 128 |
limit = limit + 1 |
| 129 |
while limit < end and is_joiner(limit): |
| 130 |
limit = limit + 1 |
| 131 |
base = start |
| 132 |
has_reph = True |
| 133 |
|
| 134 |
if self.config["base_pos"] == "last": |
| 135 |
i = end |
| 136 |
seen_below = False |
| 137 |
while True: |
| 138 |
i = i - 1 |
| 139 |
if is_consonant(i): |
| 140 |
if pos(i) != IndicPosition.BELOW_C and ( |
| 141 |
pos(i) != IndicPosition.POST_C or seen_below |
| 142 |
): |
| 143 |
base = i |
| 144 |
break |
| 145 |
if pos(i) == IndicPosition.BELOW_C: |
| 146 |
seen_below = True |
| 147 |
base = i |
| 148 |
else: |
| 149 |
if start < i and cat(i) == "ZWJ" and cat(i - 1) == "H": |
| 150 |
break |
| 151 |
if i <= limit: |
| 152 |
break |
| 153 |
elif self.config["base_pos"] == "last_sinhala": |
| 154 |
if not has_reph: |
| 155 |
base = limit |
| 156 |
for i in range(limit, end): |
| 157 |
if is_consonant(i): |
| 158 |
if limit < i and cat(i - 1) == "ZWJ": |
| 159 |
break |
| 160 |
else: |
| 161 |
base = i |
| 162 |
for i in range(base + 1, end): |
| 163 |
if is_consonant(i): |
| 164 |
self.buffer.items[i].syllabic_position = IndicPosition.BELOW_C |
| 165 |
|
| 166 |
if has_reph and base == start and limit - base <= 2: |
| 167 |
has_reph = False |
| 168 |
|
| 169 |
self.plan.msg( |
| 170 |
"Base consonant for syllable %i is %s" |
| 171 |
% (syllable_index, self.buffer.items[base].glyph) |
| 172 |
) |
| 173 |
for i in range(start, base): |
| 174 |
self.buffer.items[i].syllabic_position = min(IndicPosition.PRE_C, pos(i)) |
| 175 |
if base < end: |
| 176 |
self.buffer.items[i].syllabic_position = IndicPosition.BASE_C |
| 177 |
|
| 178 |
# Mark final consonants |
| 179 |
for i in range(base + 1, end): |
| 180 |
if cat(i) == "M": |
| 181 |
for j in range(i, end): |
| 182 |
if is_consonant(j): |
| 183 |
self.buffer.items[j].syllabic_position = IndicPosition.FINAL_C |
| 184 |
break |
| 185 |
break |
| 186 |
|
| 187 |
if has_reph: |
| 188 |
self.buffer.items[start].syllabic_category = IndicPosition.RA_TO_BECOME_REPH |
| 189 |
|
| 190 |
if self.config["old_spec"]: |
| 191 |
disallow_double_halants = self.buffer.script == "Kannada" |
| 192 |
for i in range(base + 1, end): |
| 193 |
if cat(i) == "H": |
| 194 |
j = end - 1 |
| 195 |
while j > i: |
| 196 |
if is_consonant(j) or ( |
| 197 |
disallow_double_halants and cat(j) == "H" |
| 198 |
): |
| 199 |
break |
| 200 |
j = j - 1 |
| 201 |
if cat(j) != "H" and j > i: |
| 202 |
self.buffer.items.insert(j, self.buffer.items.pop(i)) |
| 203 |
self.plan.msg("Moved double halant", self.buffer) |
| 204 |
break |
| 205 |
|
| 206 |
last_pos = IndicPosition.START |
| 207 |
for i in range(start, end): |
| 208 |
if cat(i) in ["ZWJ", "ZWNJ", "N", "RS", "CM", "H"]: |
| 209 |
self.buffer.items[i].syllabic_position = last_pos |
| 210 |
if cat(i) == "H" and pos(i) == IndicPosition.PRE_M: |
| 211 |
for j in range(i, start, -1): |
| 212 |
if pos(j - 1) != IndicPosition.PRE_M: |
| 213 |
self.buffer.items[i].syllabic_position = pos(j - 1) |
| 214 |
break |
| 215 |
elif pos(i) != IndicPosition.SMVD: |
| 216 |
last_pos = pos(i) |
| 217 |
|
| 218 |
last = base |
| 219 |
for i in range(base + 1, end): |
| 220 |
if is_consonant(i): |
| 221 |
for j in range(last + 1, i): |
| 222 |
if pos(j) < IndicPosition.SMVD: |
| 223 |
self.buffer.items[j].syllabic_position = pos(i) |
| 224 |
last = i |
| 225 |
elif cat(i) == "M": |
| 226 |
last = i |
| 227 |
|
| 228 |
# As with Harfbuzz, temporarily abuse syllable index |
| 229 |
for i in range(start, end): |
| 230 |
self.buffer.items[i].syllable_index = start - i |
| 231 |
|
| 232 |
# REORDER |
| 233 |
self.buffer.items[start:end] = sorted( |
| 234 |
self.buffer.items[start:end], key=lambda x: x.syllabic_position |
| 235 |
) |
| 236 |
|
| 237 |
base = end |
| 238 |
for i in range(start, end): |
| 239 |
if pos(i) == IndicPosition.BASE_C: |
| 240 |
base = i |
| 241 |
break |
| 242 |
|
| 243 |
if self.config["old_spec"] or end - start > 127: |
| 244 |
# Merge clusters |
| 245 |
pass |
| 246 |
else: |
| 247 |
for i in range(base, end): |
| 248 |
if self.buffer.items[i].syllable_index != 255: |
| 249 |
max_i = i |
| 250 |
j = start + self.buffer.items[i].syllable_index |
| 251 |
while j != i: |
| 252 |
max_i = max(max_i, j) |
| 253 |
next_i = start + self.buffer.items[j].syllable_index |
| 254 |
self.buffer.items[j].syllable_index = 255 |
| 255 |
j = next_i |
| 256 |
if i != max_i: |
| 257 |
# Merge clusters |
| 258 |
pass |
| 259 |
|
| 260 |
for i in range(start, end): |
| 261 |
self.buffer.items[i].syllable_index = syllable_index |
| 262 |
self.plan.msg("After initial reordering", self.buffer) |
| 263 |
|
| 264 |
# Set up masks now. Note that these masks have the opposite |
| 265 |
# value to Harfbuzz - i.e. False means "not masked" |
| 266 |
rphf_mask = False |
| 267 |
for i in range(start, end): |
| 268 |
if pos(i) != IndicPosition.RA_TO_BECOME_REPH: |
| 269 |
rphf_mask = True |
| 270 |
self.buffer.items[i].feature_masks["rphf"] = rphf_mask |
| 271 |
self.buffer.items[i].feature_masks["half"] = i > base |
| 272 |
if ( |
| 273 |
not self.config["old_spec"] |
| 274 |
and self.config["blwf_mode"] == "pre_and_post" |
| 275 |
): |
| 276 |
self.buffer.items[i].feature_masks["blwf"] = i > base |
| 277 |
self.buffer.items[i].feature_masks["blwf"] = i < base |
| 278 |
self.buffer.items[i].feature_masks["abvf"] = i < base |
| 279 |
self.buffer.items[i].feature_masks["pstf"] = i < base |
| 280 |
|
| 281 |
# We are not supporting old spec eyelash ra |
| 282 |
|
| 283 |
# pref substitutes pairwise |
| 284 |
pref_len = 2 |
| 285 |
i = base + 1 |
| 286 |
for j in range(0, i): |
| 287 |
self.buffer.items[j].feature_masks["pref"] = True |
| 288 |
while i < end - pref_len: |
| 289 |
if self.would_substitute( |
| 290 |
"pref", [self.buffer.items[i], self.buffer.items[i + 1]] |
| 291 |
): |
| 292 |
self.buffer.items[i].feature_masks["pref"] = False |
| 293 |
self.buffer.items[i + 1].feature_masks["pref"] = False |
| 294 |
i = i + 2 |
| 295 |
else: |
| 296 |
self.buffer.items[i].feature_masks["pref"] = True |
| 297 |
i = i + 1 |
| 298 |
|
| 299 |
# ZWJ/ZWNJ |
| 300 |
for i in range(start + 1, end): |
| 301 |
if cat(i) in ["ZWJ", "ZWNJ"]: |
| 302 |
non_joiner = cat(i) == "ZWNJ" |
| 303 |
j = i |
| 304 |
while True: |
| 305 |
j = j - 1 |
| 306 |
if non_joiner: |
| 307 |
self.buffer.items[j].feature_masks["half"] = True |
| 308 |
if not (j > start and not is_consonant(j)): |
| 309 |
break |
| 310 |
|
| 311 |
initial_reordering_syllable = { |
| 312 |
"standalone_cluster": initial_reordering_consonant_syllable, |
| 313 |
"consonant_syllable": initial_reordering_consonant_syllable, |
| 314 |
} |
| 315 |
|
| 316 |
def final_reordering_syllable(self, start, end): |
| 317 |
def cat(i): |
| 318 |
return self.buffer.items[i].syllabic_category |
| 319 |
|
| 320 |
def pos(i): |
| 321 |
return self.buffer.items[i].syllabic_position |
| 322 |
|
| 323 |
def swap(a, b): |
| 324 |
self.buffer.items[b], self.buffer.items[a] = ( |
| 325 |
self.buffer.items[a], |
| 326 |
self.buffer.items[b], |
| 327 |
) |
| 328 |
|
| 329 |
def is_joiner(n): |
| 330 |
return cat(n) == "ZWJ" or cat(n) == "ZWNJ" |
| 331 |
|
| 332 |
def is_halant(n): |
| 333 |
return cat(n) == "H" |
| 334 |
|
| 335 |
def is_consonant(n): |
| 336 |
isc = cat(n) |
| 337 |
is_medial = isc == "CM" |
| 338 |
return ( |
| 339 |
isc in ["C", "CS", "Ra", "V", "PLACEHOLDER", "DOTTEDCIRCLE"] |
| 340 |
or is_medial |
| 341 |
) |
| 342 |
|
| 343 |
virama = self.config["virama"] |
| 344 |
virama_item = BufferItem.new_unicode(virama) |
| 345 |
virama_item.map_to_glyph(self.buffer.font) |
| 346 |
if virama_item.glyph != ".notdef": |
| 347 |
for i in range(start, end): |
| 348 |
if ( |
| 349 |
self.buffer.items[i].glyph == virama_item.glyph |
| 350 |
and self.buffer.items[i].ligated |
| 351 |
and self.buffer.items[i].multiplied |
| 352 |
): |
| 353 |
self.buffer.items[i].syllabic_category = "H" |
| 354 |
self.buffer.items[i].ligated = False |
| 355 |
self.buffer.items[i].multiplied = False |
| 356 |
try_pref = any( |
| 357 |
[ |
| 358 |
"pref" in item.feature_masks and item.feature_masks["pref"] is False |
| 359 |
for item in self.buffer.items |
| 360 |
] |
| 361 |
) |
| 362 |
base = start |
| 363 |
while base < end: |
| 364 |
if pos(base) >= IndicPosition.BASE_C: |
| 365 |
if try_pref and base + 1 < end: |
| 366 |
for i in range(base + 1, end): |
| 367 |
item = self.buffer.items[i] |
| 368 |
if not item.feature_masks.get("pref", True): |
| 369 |
if not ( |
| 370 |
item.substituted |
| 371 |
and (item.ligated and not item.multiplied) |
| 372 |
): |
| 373 |
base = i |
| 374 |
while base < end and is_halant(base): |
| 375 |
base = base + 1 |
| 376 |
self.buffer.items[ |
| 377 |
base |
| 378 |
].syllabic_positional_category = IndicPosition.BASE_C |
| 379 |
try_pref = False |
| 380 |
break |
| 381 |
if self.buffer.script == "Malayalam": |
| 382 |
i = base + 1 |
| 383 |
while i < end: |
| 384 |
while i < end and is_joiner(i): |
| 385 |
i = i + 1 |
| 386 |
if i == end or not is_halant(i): |
| 387 |
break |
| 388 |
i = i + 1 |
| 389 |
while i < end and is_joiner(i): |
| 390 |
i = i + 1 |
| 391 |
if ( |
| 392 |
i < end |
| 393 |
and is_consonant(i) |
| 394 |
and pos(i) == IndicPosition.BELOW_C |
| 395 |
): |
| 396 |
base = i |
| 397 |
self.buffer.items[ |
| 398 |
base |
| 399 |
].syllabic_positional_category = IndicPosition.BASE_C |
| 400 |
i = i + 1 |
| 401 |
if start < base and pos(base) > IndicPosition.BASE_C: |
| 402 |
base = base - 1 |
| 403 |
break |
| 404 |
base = base + 1 |
| 405 |
if base == end and start < base and cat(base - i) == "ZWJ": |
| 406 |
base = base - 1 |
| 407 |
if base < end: |
| 408 |
while start < base and cat(base) in ["N", "H"]: |
| 409 |
base = base - 1 |
| 410 |
|
| 411 |
# Reorder matras |
| 412 |
if start + 1 < end and start < base: |
| 413 |
new_pos = base - 1 |
| 414 |
if base == end: |
| 415 |
new_pos = base - 2 |
| 416 |
# XXX |
| 417 |
|
| 418 |
for i in range(start, end): |
| 419 |
self.buffer.items[i].feature_masks["init"] = True |
| 420 |
if pos(start) == IndicPosition.PRE_M: |
| 421 |
reverse_map = {k: v for k, v in self.buffer.font.unicode_map.items()} |
| 422 |
if start == 0 or ucd_data( |
| 423 |
reverse_map.get(self.buffer.items[start - 1].glyph, 0) |
| 424 |
)["General_Category"] not in [ |
| 425 |
"Cf", |
| 426 |
"Cn", |
| 427 |
"Co", |
| 428 |
"Cs", |
| 429 |
"Ll", |
| 430 |
"Lm", |
| 431 |
"Lo", |
| 432 |
"Lt", |
| 433 |
"Lu", |
| 434 |
"Mc", |
| 435 |
"Me", |
| 436 |
"Mn", |
| 437 |
]: |
| 438 |
self.buffer.items[start].feature_masks["init"] = False |
| 439 |
|
| 440 |
def normalize_unicode_buffer(self): |
| 441 |
unicodes = [item.codepoint for item in self.buffer.items] |
| 442 |
newunicodes = [] |
| 443 |
for cp in unicodes: |
| 444 |
if cp in [0x0931, 0x09DC, 0x09DD, 0x0B94]: |
| 445 |
newunicodes.append(cp) |
| 446 |
elif cp in [0x0DDA, 0x0DDC, 0x0DDD, 0x0DDE]: # Sinhala split matras |
| 447 |
glyph = BufferItem.new_unicode(cp) |
| 448 |
glyph.map_to_glyph(self.buffer.font) |
| 449 |
if self.would_substitute("pstf", [glyph]): |
| 450 |
newunicodes.extend([0x0DD9, cp]) |
| 451 |
else: |
| 452 |
newunicodes.append(cp) |
| 453 |
else: |
| 454 |
newunicodes.extend( |
| 455 |
[ord(x) for x in unicodedata.normalize("NFD", chr(cp))] |
| 456 |
) |
| 457 |
# Now recompose |
| 458 |
newstring = "" |
| 459 |
ix = 0 |
| 460 |
while ix < len(newunicodes): |
| 461 |
a = newunicodes[ix] |
| 462 |
if ix + 1 == len(newunicodes): |
| 463 |
newstring = newstring + chr(a) |
| 464 |
break |
| 465 |
|
| 466 |
b = newunicodes[ix + 1] |
| 467 |
s = chr(a) + chr(b) |
| 468 |
# composed = unicodedata.normalize("NFC", s) |
| 469 |
if ucd_data(a)["General_Category"][0] == "M": |
| 470 |
newstring = newstring + chr(a) |
| 471 |
ix = ix + 1 |
| 472 |
continue |
| 473 |
elif a == 0x9AF and b == 0x9BC: |
| 474 |
newstring = newstring + chr(0x9DF) |
| 475 |
ix = ix + 2 |
| 476 |
continue |
| 477 |
# elif composed != unicodedata.normalize("NFD", s): |
| 478 |
# assert(len(s) == 1) |
| 479 |
# newunicodes[ix] = ord(x) |
| 480 |
# del newunicodes[ix+1] |
| 481 |
# continue |
| 482 |
else: |
| 483 |
newstring = newstring + chr(a) |
| 484 |
ix = ix + 1 |
| 485 |
|
| 486 |
self.buffer.store_unicode(newstring) |