| 1 |
from youseedee import ucd_data, database |
| 2 |
from .SyllabicShaper import SyllabicShaper |
| 3 |
from .IndicShaperData import make_syllable_machine |
| 4 |
import unicodedata |
| 5 |
from collections import OrderedDict |
| 6 |
|
| 7 |
states = OrderedDict( |
| 8 |
h="H | HVM | Sk", |
| 9 |
consonant_modifiers="CMAbv* CMBlw* ((h B | SUB) CMAbv? CMBlw*)*", |
| 10 |
medial_consonants="MPre? MAbv? MBlw? MPst?", |
| 11 |
dependent_vowels="VPre* VAbv* VBlw* VPst*", |
| 12 |
vowel_modifiers="HVM? VMPre* VMAbv* VMBlw* VMPst*", |
| 13 |
final_consonants="FAbv* FBlw* FPst*", |
| 14 |
final_modifiers="FMAbv* FMBlw* | FMPst?", |
| 15 |
complex_syllable_start="(R | CS)? (B | GB)", |
| 16 |
complex_syllable_middle="consonant_modifiers medial_consonants dependent_vowels vowel_modifiers (Sk B)*", |
| 17 |
complex_syllable_tail="complex_syllable_middle final_consonants final_modifiers", |
| 18 |
number_joiner_terminated_cluster_tail="(HN N)* HN", |
| 19 |
numeral_cluster_tail="(HN N)+", |
| 20 |
symbol_cluster_tail="SMAbv+ SMBlw* | SMBlw+", |
| 21 |
virama_terminated_cluster="complex_syllable_start consonant_modifiers h", |
| 22 |
sakot_terminated_cluster="complex_syllable_start complex_syllable_middle Sk", |
| 23 |
standard_cluster="complex_syllable_start complex_syllable_tail", |
| 24 |
broken_cluster="R? (complex_syllable_tail | number_joiner_terminated_cluster_tail | numeral_cluster_tail | symbol_cluster_tail)", |
| 25 |
number_joiner_terminated_cluster="N number_joiner_terminated_cluster_tail", |
| 26 |
numeral_cluster="N numeral_cluster_tail?", |
| 27 |
symbol_cluster="(S | GB) symbol_cluster_tail?", |
| 28 |
hieroglyph_cluster="SB+ | SB* G SE* (J SE* (G SE*)?)*", |
| 29 |
independent_cluster="O", |
| 30 |
) |
| 31 |
use_categories = set([x[2] for x in database["USECategory.txt"]["data"]]) |
| 32 |
|
| 33 |
|
| 34 |
class USEShaper(SyllabicShaper): |
| 35 |
basic_features = ["rkrf", "abvf", "blwf", "half", "pstf", "vatu", "cjct"] |
| 36 |
topographical_features = ["isol", "init", "medi", "fina"] |
| 37 |
other_features = ["abvs", "blws", "haln", "pres", "psts"] |
| 38 |
repha = "R" |
| 39 |
syllable_machine = make_syllable_machine( |
| 40 |
states, additional_categories=use_categories |
| 41 |
) |
| 42 |
syllable_types = [ |
| 43 |
"independent_cluster", |
| 44 |
"virama_terminated_cluster", |
| 45 |
"sakot_terminated_cluster", |
| 46 |
"standard_cluster", |
| 47 |
"number_joiner_terminated_cluster", |
| 48 |
"numeral_cluster", |
| 49 |
"symbol_cluster", |
| 50 |
"hieroglyph_cluster", |
| 51 |
"broken_cluster", |
| 52 |
"other", |
| 53 |
] |
| 54 |
|
| 55 |
def collect_features(self, shaper): |
| 56 |
shaper.add_pause(self.setup_syllables) |
| 57 |
# Default glyph pre-processing |
| 58 |
shaper.add_features("locl", "ccmp", "nukt", "akhn") |
| 59 |
shaper.add_pause(self.clear_substitution) |
| 60 |
shaper.add_features("rphf") |
| 61 |
shaper.add_pause(self.record_rphf_use) |
| 62 |
shaper.add_pause(self.clear_substitution) |
| 63 |
shaper.add_features("pref") |
| 64 |
shaper.add_pause(self.record_pref_use) |
| 65 |
|
| 66 |
shaper.add_features(*self.basic_features) |
| 67 |
shaper.add_pause(self.initial_reordering) |
| 68 |
# shaper.add_pause(self.clear_syllables) |
| 69 |
shaper.add_features(*self.topographical_features) |
| 70 |
shaper.add_pause() |
| 71 |
shaper.add_features(*self.other_features) |
| 72 |
|
| 73 |
def assign_category(self, item): |
| 74 |
item.syllabic_category = ucd_data(item.codepoint).get("USE_Category", "X") |
| 75 |
# Separate positional categories are not used, it's all in the syllabic_category |
| 76 |
item.positional_category = "x" |
| 77 |
|
| 78 |
def cat(self, i): |
| 79 |
return self.buffer.items[i].syllabic_category |
| 80 |
|
| 81 |
def setup_syllables(self, shaper): |
| 82 |
super().setup_syllables(shaper) |
| 83 |
self.setup_rphf_mask() |
| 84 |
self.setup_topographical_masks() |
| 85 |
|
| 86 |
def setup_rphf_mask(self): |
| 87 |
for index, syll_type, start, end in self.iterate_syllables(): |
| 88 |
if self.cat(start) == "R": |
| 89 |
limit = 1 |
| 90 |
else: |
| 91 |
limit = min(3, end - start) |
| 92 |
for i in range(start, end): |
| 93 |
self.buffer.items[i].feature_masks["rphf"] = i > start + limit |
| 94 |
|
| 95 |
def clear_substitution(self, shaper): |
| 96 |
for i in self.buffer.items: |
| 97 |
i.substituted = False |
| 98 |
|
| 99 |
def record_pref_use(self, shaper): |
| 100 |
for i in self.buffer.items: |
| 101 |
if i.substituted: |
| 102 |
i.syllabic_category = "VPre" |
| 103 |
|
| 104 |
def record_rphf_use(self, shaper): |
| 105 |
for i in self.buffer.items: |
| 106 |
if i.substituted: |
| 107 |
i.syllabic_category = "R" |
| 108 |
|
| 109 |
def is_post_base(self, i): |
| 110 |
return self.cat(i) in [ |
| 111 |
"FAbv", |
| 112 |
"FBlw", |
| 113 |
"FPst", |
| 114 |
"MAbv", |
| 115 |
"MBlw", |
| 116 |
"MPst", |
| 117 |
"MPre", |
| 118 |
"VAbv", |
| 119 |
"VBlw", |
| 120 |
"VPst", |
| 121 |
"VPre", |
| 122 |
"VMAbv", |
| 123 |
"VMBlw", |
| 124 |
"VMPst", |
| 125 |
"VMPre", |
| 126 |
] |
| 127 |
|
| 128 |
def is_halant(self, i): |
| 129 |
return self.cat(i) in ["H", "HVM"] and not self.buffer.items[i].ligated |
| 130 |
|
| 131 |
def reorder_syllable(self, start, end): |
| 132 |
if self.cat(start) == "R": |
| 133 |
for i in range(start + 1, end): |
| 134 |
post_base = self.is_post_base(i) or self.is_halant(i) |
| 135 |
if post_base or i == end - 1: |
| 136 |
if post_base: |
| 137 |
i -= 1 |
| 138 |
self.buffer.merge_clusters(start, i + 1) |
| 139 |
self.buffer.move_item(src=start, dest=i) |
| 140 |
break |
| 141 |
j = start |
| 142 |
for i in range(start, end): |
| 143 |
if self.is_halant(i): |
| 144 |
j = i + 1 |
| 145 |
if self.cat(i) in ["VPre", "VMPre"] and j < i: # XXX Only first ligcomp |
| 146 |
self.buffer.merge_clusters(j, i + 1) |
| 147 |
self.buffer.move_item(src=i, dest=j) |
| 148 |
|
| 149 |
initial_reordering_syllable = { |
| 150 |
"virama_terminated_cluster": reorder_syllable, |
| 151 |
"sakot_terminated_cluster": reorder_syllable, |
| 152 |
"standard_cluster": reorder_syllable, |
| 153 |
"broken_cluster": reorder_syllable, |
| 154 |
} |
| 155 |
|
| 156 |
def setup_topographical_masks(self): |
| 157 |
# Right now we don't actually send joining scripts through the USE |
| 158 |
pass |
| 159 |
|
| 160 |
def normalize_unicode_buffer(self): |
| 161 |
unicodes = [item.codepoint for item in self.buffer.items] |
| 162 |
newunicodes = [] |
| 163 |
for cp in unicodes: |
| 164 |
newunicodes.extend([ord(x) for x in unicodedata.normalize("NFD", chr(cp))]) |
| 165 |
# Now recompose |
| 166 |
newstring = "" |
| 167 |
ix = 0 |
| 168 |
while ix < len(newunicodes): |
| 169 |
a = newunicodes[ix] |
| 170 |
if ix + 1 == len(newunicodes): |
| 171 |
newstring = newstring + chr(a) |
| 172 |
break |
| 173 |
|
| 174 |
b = newunicodes[ix + 1] |
| 175 |
s = chr(a) + chr(b) |
| 176 |
composed = unicodedata.normalize("NFC", s) |
| 177 |
if ucd_data(a)["General_Category"][0] == "M": |
| 178 |
newstring = newstring + chr(a) |
| 179 |
ix = ix + 1 |
| 180 |
continue |
| 181 |
elif composed != unicodedata.normalize("NFD", s): |
| 182 |
assert len(s) == 1 |
| 183 |
newunicodes[ix] = ord(s) |
| 184 |
del newunicodes[ix + 1] |
| 185 |
continue |
| 186 |
else: |
| 187 |
newstring = newstring + chr(a) |
| 188 |
ix = ix + 1 |
| 189 |
|
| 190 |
self.buffer.store_unicode(newstring) |