| 1 |
from .IndicShaperData import IndicPosition, make_syllable_machine |
| 2 |
from .SyllabicShaper import SyllabicShaper |
| 3 |
from collections import OrderedDict |
| 4 |
|
| 5 |
|
| 6 |
myanmar_category_reassignments = { |
| 7 |
0x002D: "GB", |
| 8 |
0x00A0: "GB", |
| 9 |
0x00D7: "GB", |
| 10 |
0x1004: "Ra", |
| 11 |
0x101B: "Ra", |
| 12 |
0x1032: "A", |
| 13 |
0x1036: "A", |
| 14 |
0x1038: "SM", |
| 15 |
0x1039: "H", |
| 16 |
0x103A: "As", |
| 17 |
0x103B: "MY", |
| 18 |
0x103C: "MR", |
| 19 |
0x103D: "MW", |
| 20 |
0x103E: "MH", |
| 21 |
0x1040: "D", # XXX The spec says D0, but Uniscribe doesn't seem to do. |
| 22 |
0x104A: "P", |
| 23 |
0x104B: "P", |
| 24 |
0x104E: "C", |
| 25 |
0x105A: "Ra", |
| 26 |
0x105E: "MY", |
| 27 |
0x105F: "MY", |
| 28 |
0x1060: "MH", |
| 29 |
0x1082: "MW", |
| 30 |
0x109A: "SM", |
| 31 |
0x109B: "SM", |
| 32 |
0x109C: "SM", |
| 33 |
0x2012: "GB", |
| 34 |
0x2013: "GB", |
| 35 |
0x2014: "GB", |
| 36 |
0x2015: "GB", |
| 37 |
0x2022: "GB", |
| 38 |
0x25CC: "GB", |
| 39 |
0x25FB: "GB", |
| 40 |
0x25FC: "GB", |
| 41 |
0x25FD: "GB", |
| 42 |
0x25FE: "GB", |
| 43 |
0xAA74: "C", |
| 44 |
0xAA75: "C", |
| 45 |
0xAA76: "C", # https://github.com/harfbuzz/harfbuzz/issues/218 |
| 46 |
} |
| 47 |
|
| 48 |
for cp in range(0x1041, 0x1049 + 1): |
| 49 |
myanmar_category_reassignments[cp] = "D" |
| 50 |
for cp in range(0x1090, 0x1099 + 1): |
| 51 |
myanmar_category_reassignments[cp] = "D" |
| 52 |
for cp in range(0xF300, 0xFE0F + 1): |
| 53 |
myanmar_category_reassignments[cp] = "VS" |
| 54 |
for cp in range(0x1087, 0x108F + 1): |
| 55 |
myanmar_category_reassignments[cp] = "SM" |
| 56 |
for cp in [0x1063, 0x1064, 0x1069, 0x106A, 0x106B, 0x106C, 0x106D, 0xAA7B]: |
| 57 |
myanmar_category_reassignments[cp] = "PT" |
| 58 |
|
| 59 |
|
| 60 |
states = OrderedDict( |
| 61 |
j="ZWJ|ZWNJ", # Joiners |
| 62 |
k="(Ra As H)", # Kinzi |
| 63 |
c="C|Ra", # is_consonant |
| 64 |
medial_group="MY? As? MR? ((MW MH? | MH) As?)?", |
| 65 |
main_vowel_group="(VPre VS?)* VAbv* VBlw* A* (DB As?)?", |
| 66 |
post_vowel_group="VPst MH? As* VAbv* A* (DB As?)?", |
| 67 |
pwo_tone_group="PT A* DB? As?", |
| 68 |
complex_syllable_tail="As* medial_group main_vowel_group post_vowel_group* pwo_tone_group* V* j?", |
| 69 |
syllable_tail="(H (c|IV) VS?)* (H | complex_syllable_tail)", |
| 70 |
consonant_syllable="(k|CS)? (c|IV|D|GB) VS? syllable_tail", |
| 71 |
punctuation_cluster="P V", |
| 72 |
broken_cluster="k? VS? syllable_tail", |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
class MyanmarShaper(SyllabicShaper): |
| 77 |
basic_features = ["rphf", "pref", "blwf", "pstf"] |
| 78 |
other_features = ["pres", "abvs", "blws", "psts"] |
| 79 |
repha = None |
| 80 |
syllable_machine = make_syllable_machine( |
| 81 |
states, |
| 82 |
additional_categories=[ |
| 83 |
"VS", |
| 84 |
"MW", |
| 85 |
"P", |
| 86 |
"As", |
| 87 |
"PT", |
| 88 |
"MY", |
| 89 |
"MH", |
| 90 |
"D", |
| 91 |
"GB", |
| 92 |
"MR", |
| 93 |
"VPre", |
| 94 |
"VAbv", |
| 95 |
"VBlw", |
| 96 |
"VPst", |
| 97 |
], |
| 98 |
) |
| 99 |
syllable_types = [ |
| 100 |
"consonant_syllable", |
| 101 |
"punctuation_cluster", |
| 102 |
"broken_cluster", |
| 103 |
"other", |
| 104 |
] |
| 105 |
|
| 106 |
def reassign_category(self, item): |
| 107 |
cp = item.codepoint |
| 108 |
if cp in myanmar_category_reassignments: |
| 109 |
item.syllabic_category = myanmar_category_reassignments[cp] |
| 110 |
|
| 111 |
if item.syllabic_category == "M": |
| 112 |
if item.syllabic_position == IndicPosition.PRE_C: |
| 113 |
item.syllabic_category = "VPre" |
| 114 |
item.syllabic_position = IndicPosition.PRE_M |
| 115 |
elif item.syllabic_position == IndicPosition.ABOVE_C: |
| 116 |
item.syllabic_category = "VAbv" |
| 117 |
elif item.syllabic_position == IndicPosition.BELOW_C: |
| 118 |
item.syllabic_category = "VBlw" |
| 119 |
elif item.syllabic_position == IndicPosition.POST_C: |
| 120 |
item.syllabic_category = "VPst" |
| 121 |
|
| 122 |
def initial_reordering_consonant_syllable(self, start, end): |
| 123 |
def cat(i): |
| 124 |
return self.buffer.items[i].syllabic_category |
| 125 |
|
| 126 |
def get_pos(i): |
| 127 |
return self.buffer.items[i].syllabic_position |
| 128 |
|
| 129 |
def set_pos(i, pos): |
| 130 |
self.buffer.items[i].syllabic_position = pos |
| 131 |
|
| 132 |
def is_consonant(n): # XXX Something else is Placeholder too |
| 133 |
isc = cat(n) |
| 134 |
is_medial = isc == "CM" |
| 135 |
return ( |
| 136 |
isc in ["C", "CS", "Ra", "V", "PLACEHOLDER", "DOTTEDCIRCLE"] |
| 137 |
or is_medial |
| 138 |
) |
| 139 |
|
| 140 |
base = end |
| 141 |
has_reph = False |
| 142 |
|
| 143 |
limit = start |
| 144 |
if ( |
| 145 |
start + 3 <= end |
| 146 |
and cat(start) == "Ra" |
| 147 |
and cat(start + 1) == "As" |
| 148 |
and cat(start + 2) == "H" |
| 149 |
): |
| 150 |
limit += 3 |
| 151 |
base = start |
| 152 |
has_reph = True |
| 153 |
if not has_reph: |
| 154 |
base = limit |
| 155 |
for i in range(limit, end): |
| 156 |
if is_consonant(i): |
| 157 |
base = i |
| 158 |
break |
| 159 |
i = start |
| 160 |
while i < start + (3 if has_reph else 0): |
| 161 |
set_pos(i, IndicPosition.AFTER_MAIN) |
| 162 |
i += 1 |
| 163 |
while i < base: |
| 164 |
set_pos(i, IndicPosition.PRE_C) |
| 165 |
i += 1 |
| 166 |
if i < end: |
| 167 |
set_pos(i, IndicPosition.BASE_C) |
| 168 |
i += 1 |
| 169 |
pos = IndicPosition.AFTER_MAIN |
| 170 |
while i < end: |
| 171 |
if cat(i) == "MR": |
| 172 |
set_pos(i, IndicPosition.PRE_C) |
| 173 |
elif get_pos(i) < IndicPosition.BASE_C: # Left Matra |
| 174 |
pass |
| 175 |
elif cat(i) == "VS": |
| 176 |
set_pos(i, get_pos(i - 1)) |
| 177 |
elif pos == IndicPosition.AFTER_MAIN and cat(i) == "VBlw": |
| 178 |
pos = IndicPosition.BELOW_C |
| 179 |
set_pos(i, pos) |
| 180 |
elif pos == IndicPosition.BELOW_C and cat(i) == "A": |
| 181 |
set_pos(i, IndicPosition.BEFORE_SUB) |
| 182 |
elif pos == IndicPosition.BELOW_C and cat(i) == "VBlw": |
| 183 |
set_pos(i, pos) |
| 184 |
elif pos == IndicPosition.BELOW_C and cat(i) != "A": |
| 185 |
pos = IndicPosition.AFTER_SUB |
| 186 |
set_pos(i, pos) |
| 187 |
else: |
| 188 |
set_pos(i, pos) |
| 189 |
i = i + 1 |
| 190 |
continue |
| 191 |
|
| 192 |
self.buffer.items[start:end] = sorted( |
| 193 |
self.buffer.items[start:end], key=lambda x: x.syllabic_position |
| 194 |
) |
| 195 |
|
| 196 |
initial_reordering_syllable = { |
| 197 |
"broken_cluster": initial_reordering_consonant_syllable, |
| 198 |
"consonant_syllable": initial_reordering_consonant_syllable, |
| 199 |
} |