| 1 |
from .BaseShaper import BaseShaper |
| 2 |
from youseedee import ucd_data |
| 3 |
|
| 4 |
|
| 5 |
jts = {"U": 0, "L": 1, "R": 2, "D": 3, "ALAPH": 4, "DALATH_RISH": 5} |
| 6 |
state_table = [ |
| 7 |
# jt_U, jt_L, jt_R, jt_D, jg_ALAPH, jg_DALATH_RISH */ |
| 8 |
# State 0: prev was U, not willing to join. |
| 9 |
[ |
| 10 |
("none", "none", 0), |
| 11 |
("none", "isol", 2), |
| 12 |
("none", "isol", 1), |
| 13 |
("none", "isol", 2), |
| 14 |
("none", "isol", 1), |
| 15 |
("none", "isol", 6), |
| 16 |
], |
| 17 |
# State 1: prev was R or ISOL/ALAPH, not willing to join. |
| 18 |
[ |
| 19 |
("none", "none", 0), |
| 20 |
("none", "isol", 2), |
| 21 |
("none", "isol", 1), |
| 22 |
("none", "isol", 2), |
| 23 |
("none", "fin2", 5), |
| 24 |
("none", "isol", 6), |
| 25 |
], |
| 26 |
# State 2: prev was D/L in ISOL form, willing to join. |
| 27 |
[ |
| 28 |
("none", "none", 0), |
| 29 |
("none", "isol", 2), |
| 30 |
("init", "fina", 1), |
| 31 |
("init", "fina", 3), |
| 32 |
("init", "fina", 4), |
| 33 |
("init", "fina", 6), |
| 34 |
], |
| 35 |
# State 3: prev was D in FINA form, willing to join. |
| 36 |
[ |
| 37 |
("none", "none", 0), |
| 38 |
("none", "isol", 2), |
| 39 |
("medi", "fina", 1), |
| 40 |
("medi", "fina", 3), |
| 41 |
("medi", "fina", 4), |
| 42 |
("medi", "fina", 6), |
| 43 |
], |
| 44 |
# State 4: prev was FINA ALAPH, not willing to join. |
| 45 |
[ |
| 46 |
("none", "none", 0), |
| 47 |
("none", "isol", 2), |
| 48 |
("med2", "isol", 1), |
| 49 |
("med2", "isol", 2), |
| 50 |
("med2", "fin2", 5), |
| 51 |
("med2", "isol", 6), |
| 52 |
], |
| 53 |
# State 5: prev was FIN2/FIN3 ALAPH, not willing to join. |
| 54 |
[ |
| 55 |
("none", "none", 0), |
| 56 |
("none", "isol", 2), |
| 57 |
("isol", "isol", 1), |
| 58 |
("isol", "isol", 2), |
| 59 |
("isol", "fin2", 5), |
| 60 |
("isol", "isol", 6), |
| 61 |
], |
| 62 |
# State 6: prev was DALATH/RISH, not willing to join. |
| 63 |
[ |
| 64 |
("none", "none", 0), |
| 65 |
("none", "isol", 2), |
| 66 |
("none", "isol", 1), |
| 67 |
("none", "isol", 2), |
| 68 |
("none", "fin3", 5), |
| 69 |
("none", "isol", 6), |
| 70 |
], |
| 71 |
] |
| 72 |
|
| 73 |
arabic_features = ["isol", "fina", "fin2", "fin3", "medi", "med2", "init"] |
| 74 |
|
| 75 |
|
| 76 |
class ArabicShaper(BaseShaper): |
| 77 |
def collect_features(self, shaper): |
| 78 |
# shaper.add_features("stch") |
| 79 |
shaper.add_features("ccmp", "locl") |
| 80 |
shaper.add_pause() |
| 81 |
shaper.add_features(*arabic_features) |
| 82 |
shaper.add_pause() |
| 83 |
shaper.add_features("rlig", "rclt", "calt") |
| 84 |
shaper.add_pause() |
| 85 |
shaper.add_features("mset") |
| 86 |
|
| 87 |
def substitute_default(self): |
| 88 |
super().substitute_default() |
| 89 |
state = 0 |
| 90 |
prev_item = None |
| 91 |
for item in self.buffer.items: |
| 92 |
item.arabic_joining = "NONE" |
| 93 |
ucd = ucd_data(item.codepoint) |
| 94 |
joining = ucd.get("Joining_Type") |
| 95 |
if not joining: |
| 96 |
if ucd.get("General_Category") in ["Mn", "Cf", "Em"]: |
| 97 |
joining = "T" |
| 98 |
else: |
| 99 |
joining = "U" |
| 100 |
if joining == "T": |
| 101 |
continue |
| 102 |
if joining == "C": |
| 103 |
joining = "D" # Mongolian |
| 104 |
if ucd.get("Joining_Group") == "ALAPH": |
| 105 |
joining = "ALAPH" |
| 106 |
if ucd.get("Joining_Group") == "DALATH RISH": |
| 107 |
joining = "DALATH_RISH" |
| 108 |
prev, this, state = state_table[state][jts[joining]] |
| 109 |
if prev_item: |
| 110 |
prev_item.arabic_joining = prev |
| 111 |
item.arabic_joining = this |
| 112 |
prev_item = item |
| 113 |
if self.buffer.script == "Mongolian": |
| 114 |
self.mongolian_variation_selectors() |
| 115 |
self.plan.msg( |
| 116 |
"Assigned Arabic joining", self.buffer, serialize_options=["arabic_joining"] |
| 117 |
) |
| 118 |
for f in arabic_features: |
| 119 |
if f not in self.plan.fontfeatures.features: |
| 120 |
continue |
| 121 |
for item in self.buffer.items: |
| 122 |
item.feature_masks[f] = item.arabic_joining != f |
| 123 |
|
| 124 |
def mongolian_variation_selectors(self): |
| 125 |
for ix, item in enumerate(self.buffer.items): |
| 126 |
if 0x180B <= item.codepoint <= 0x180D and ix > 0: |
| 127 |
item.arabic_joining = self.buffer.items[ix - 1].arabic_joining |