| 1 |
from youseedee import ucd_data |
| 2 |
from .BaseShaper import BaseShaper |
| 3 |
import re |
| 4 |
from fontFeatures.shaperLib.Buffer import BufferItem |
| 5 |
from fontFeatures.shaperLib.VowelConstraints import preprocess_text_vowel_constraints |
| 6 |
from .IndicShaperData import ( |
| 7 |
syllabic_category_map, |
| 8 |
IndicPositionalCategory2IndicPosition, |
| 9 |
) |
| 10 |
|
| 11 |
DOTTED_CIRCLE = 0x25CC |
| 12 |
|
| 13 |
|
| 14 |
class SyllabicShaper(BaseShaper): |
| 15 |
basic_features = [ |
| 16 |
"nukt", |
| 17 |
"akhn", |
| 18 |
"rphf", |
| 19 |
"rkrf", |
| 20 |
"pref", |
| 21 |
"blwf", |
| 22 |
"abvf", |
| 23 |
"half", |
| 24 |
"pstf", |
| 25 |
"vatu", |
| 26 |
"cjct", |
| 27 |
] |
| 28 |
after_syllable_features = ["ccmp", "locl"] |
| 29 |
other_features = ["init", "pres", "abvs", "blws", "psts", "haln", "calt", "clig"] |
| 30 |
repha = "Repha" |
| 31 |
|
| 32 |
def collect_features(self, shaper): |
| 33 |
shaper.add_pause(self.setup_syllables) |
| 34 |
shaper.add_features(*self.after_syllable_features) |
| 35 |
shaper.add_pause(self.initial_reordering) |
| 36 |
for i in self.basic_features: |
| 37 |
shaper.add_features(i) |
| 38 |
shaper.add_pause() |
| 39 |
shaper.add_pause(self.final_reordering) |
| 40 |
shaper.add_features(*self.other_features) |
| 41 |
|
| 42 |
def preprocess_text(self): |
| 43 |
preprocess_text_vowel_constraints(self.buffer) |
| 44 |
|
| 45 |
def reassign_category(self, item): |
| 46 |
pass |
| 47 |
|
| 48 |
def assign_category(self, item): |
| 49 |
# Base behavior is Indic |
| 50 |
ucd = ucd_data(item.codepoint) |
| 51 |
item.syllabic_category = syllabic_category_map.get( |
| 52 |
ucd.get("Indic_Syllabic_Category", "Other"), "X" |
| 53 |
) |
| 54 |
item.positional_category = ucd.get("Indic_Positional_Category", "x") |
| 55 |
item.syllabic_position = IndicPositionalCategory2IndicPosition( |
| 56 |
item.positional_category |
| 57 |
) |
| 58 |
self.reassign_category(item) |
| 59 |
|
| 60 |
def assign_categories(self): |
| 61 |
serialized = [] |
| 62 |
for ix, item in enumerate(self.buffer.items): |
| 63 |
self.assign_category(item) |
| 64 |
serialized.append( |
| 65 |
"<" |
| 66 |
+ item.syllabic_category |
| 67 |
+ ">(" |
| 68 |
+ item.positional_category |
| 69 |
+ ")=" |
| 70 |
+ str(ix) |
| 71 |
) |
| 72 |
return "".join(serialized) |
| 73 |
|
| 74 |
def setup_syllables(self, shaper): |
| 75 |
syllable_index = 0 |
| 76 |
category_string = self.assign_categories() |
| 77 |
self.plan.msg("Set up syllables: " + category_string) |
| 78 |
while len(category_string) > 0: |
| 79 |
state, end, matched_type = None, None, None |
| 80 |
for syllable_type in self.syllable_types: |
| 81 |
m = re.match(self.syllable_machine[syllable_type], category_string) |
| 82 |
if m and len(m[0]): |
| 83 |
matched_type = syllable_type |
| 84 |
category_string = category_string[len(m[0]) :] |
| 85 |
indexes = re.findall("=(\\d+)", m[0]) |
| 86 |
start, end = int(indexes[0]), int(indexes[-1]) |
| 87 |
break |
| 88 |
assert matched_type |
| 89 |
for i in range(start, end + 1): |
| 90 |
self.buffer.items[i].syllable_index = syllable_index |
| 91 |
self.buffer.items[i].syllable = syllable_type |
| 92 |
syllable_index = syllable_index + 1 |
| 93 |
self.plan.msg("Syllables", self.buffer, ["syllable_index", "syllable"]) |
| 94 |
|
| 95 |
def iterate_syllables(self): |
| 96 |
ix = 0 |
| 97 |
while ix < len(self.buffer.items): |
| 98 |
syll_type = self.buffer.items[ix].syllable |
| 99 |
index = self.buffer.items[ix].syllable_index |
| 100 |
start = ix |
| 101 |
while ( |
| 102 |
ix < len(self.buffer.items) |
| 103 |
and self.buffer.items[ix].syllable_index == index |
| 104 |
): |
| 105 |
ix = ix + 1 |
| 106 |
end = ix |
| 107 |
yield index, syll_type, start, end |
| 108 |
|
| 109 |
def insert_dotted_circles(self, repha): |
| 110 |
for ix, i in enumerate(self.buffer.items): |
| 111 |
if i.syllable == "broken_cluster" and ( |
| 112 |
ix == 0 or i.syllable_index != self.buffer.items[ix - 1].syllable_index |
| 113 |
): |
| 114 |
# Need to insert dotted circle. |
| 115 |
dotted_circle = BufferItem.new_unicode(DOTTED_CIRCLE) |
| 116 |
dotted_circle.syllable_index = i.syllable_index |
| 117 |
dotted_circle.syllable = i.syllable |
| 118 |
self.assign_category(dotted_circle) |
| 119 |
dotted_circle.map_to_glyph(self.buffer.font) |
| 120 |
if self.repha is not None and i.syllabic_category == repha: |
| 121 |
self.buffer.items.insert(ix + 1, dotted_circle) |
| 122 |
else: |
| 123 |
self.buffer.items.insert(ix, dotted_circle) |
| 124 |
|
| 125 |
def initial_reordering_pre(self): |
| 126 |
pass |
| 127 |
|
| 128 |
def initial_reordering(self, shaper): |
| 129 |
self.initial_reordering_pre() |
| 130 |
self.insert_dotted_circles(self.repha) |
| 131 |
for index, syll_type, start, end in self.iterate_syllables(): |
| 132 |
reorder = self.initial_reordering_syllable.get(syll_type, None) |
| 133 |
if reorder: |
| 134 |
reorder(self, start, end) |
| 135 |
self.plan.msg( |
| 136 |
"After initial reordering", self.buffer, ["syllable_index", "syllable"] |
| 137 |
) |
| 138 |
|
| 139 |
def final_reordering(self, shaper): |
| 140 |
for index, syll_type, start, end in self.iterate_syllables(): |
| 141 |
self.final_reordering_syllable(start, end) |
| 142 |
|
| 143 |
def final_reordering_syllable(self, start, end): |
| 144 |
pass |