Debian Codegraph @

Lib/fontFeatures/shaperLib/KhmerShaper.py

raw
1 from .IndicShaperData import IndicPosition, make_syllable_machine
2 from .SyllabicShaper import SyllabicShaper
3 import unicodedata
4 from youseedee import ucd_data
5 from collections import OrderedDict
6
7
8 khmer_category_reassignments = {
9 0x179A: "Ra",
10 0x17CC: "Robatic",
11 0x17C9: "Robatic",
12 0x17CA: "Robatic",
13 0x17C6: "Xgroup",
14 0x17CB: "Xgroup",
15 0x17CD: "Xgroup",
16 0x17CE: "Xgroup",
17 0x17CF: "Xgroup",
18 0x17D0: "Xgroup",
19 0x17D1: "Xgroup",
20 0x17C7: "Ygroup",
21 0x17C8: "Ygroup",
22 0x17DD: "Ygroup",
23 0x17D3: "Ygroup",
24 }
25
26 states = OrderedDict(
27 c="(C | Ra | V)",
28 cn="c ((ZWJ|ZWNJ)? Robatic)?",
29 joiner="(ZWJ | ZWNJ)",
30 xgroup="(joiner* Xgroup)*",
31 ygroup="Ygroup*",
32 matra_group="VPre? xgroup VBlw? xgroup (joiner? VAbv)? xgroup VPst?",
33 syllable_tail="xgroup matra_group xgroup (Coeng c)? ygroup",
34 broken_cluster="(Coeng cn)* (Coeng | syllable_tail)",
35 consonant_syllable="(cn|PLACEHOLDER|DOTTEDCIRCLE) broken_cluster",
36 )
37
38
39 class KhmerShaper(SyllabicShaper):
40 repha = "Repha"
41 syllable_machine = make_syllable_machine(
42 states,
43 additional_categories=[
44 "Ra",
45 "Robatic",
46 "Xgroup",
47 "Ygroup",
48 "VPre",
49 "VBlw",
50 "VAbv",
51 "VPst",
52 ],
53 )
54 syllable_types = ["consonant_syllable", "broken_cluster", "other"]
55
56 def collect_features(self, shaper):
57 shaper.add_pause(self.setup_syllables)
58 shaper.add_pause(self.initial_reordering)
59 shaper.add_features("locl", "ccmp")
60 shaper.add_features("pref", "blwf", "abvf", "pstf", "cfar")
61 shaper.add_pause() # Clear syllables
62 shaper.add_features("pres", "blws", "abvs", "psts")
63
64 def override_features(self, shaper):
65 shaper.add_features("clig")
66 shaper.disable_feature("liga")
67
68 def reassign_category(self, item):
69 cp = item.codepoint
70 if cp in khmer_category_reassignments:
71 item.syllabic_category = khmer_category_reassignments[cp]
72
73 if item.syllabic_category == "M":
74 if item.syllabic_position == IndicPosition.PRE_C:
75 item.syllabic_category = "VPre"
76 elif item.syllabic_position == IndicPosition.ABOVE_C:
77 item.syllabic_category = "VAbv"
78 elif item.syllabic_position == IndicPosition.BELOW_C:
79 item.syllabic_category = "VBlw"
80 elif item.syllabic_position == IndicPosition.POST_C:
81 item.syllabic_category = "VPst"
82
83 def initial_reordering_consonant_syllable(self, start, end):
84 def cat(i):
85 return self.buffer.items[i].syllabic_category
86
87 def mask_disallow(places, *feats):
88 for p in places:
89 for f in feats:
90 self.buffer.items[p].feature_masks[f] = True
91
92 def mask_allow(places, *feats):
93 for p in places:
94 for f in feats:
95 self.buffer.items[p].feature_masks[f] = False
96
97 mask_allow(range(start + 1, end), "blwf", "advf", "pstf")
98 mask_disallow(range(start, end), "cfar")
99
100 num_coengs = 0
101 for i in range(start + 1, end):
102 if cat(i) == "Coeng" and num_coengs <= 2 and i + 1 < end:
103 num_coengs += 1
104 if cat(i + 1) == "Ra":
105 mask_allow([i, i + 1], "pref")
106 # Move the Coeng,Ro sequence to the start.
107 # XXX merge_clusters
108 ro = self.buffer.items.pop(i + 1)
109 coeng = self.buffer.items.pop(i)
110 self.buffer.items = (
111 self.buffer.items[:start]
112 + [coeng, ro]
113 + self.buffer.items[start:]
114 )
115 mask_allow(range(i + 2, end), "cfar")
116 num_coengs = 2
117 elif cat(i) == "VPre":
118 # XXX merge_clusters
119 vpre = self.buffer.items.pop(i)
120 self.buffer.items = (
121 self.buffer.items[:start] + [vpre] + self.buffer.items[start:]
122 )
123
124 initial_reordering_syllable = {
125 "broken_cluster": initial_reordering_consonant_syllable,
126 "consonant_syllable": initial_reordering_consonant_syllable,
127 }
128
129 def normalize_unicode_buffer(self):
130 unicodes = [item.codepoint for item in self.buffer.items]
131 newunicodes = []
132 for cp in unicodes:
133 if cp in [0x17BE, 0x17BF, 0x17C0, 0x17C4, 0x17C5]:
134 newunicodes.extend([0x17C1, cp])
135 else:
136 newunicodes.extend(
137 [ord(x) for x in unicodedata.normalize("NFD", chr(cp))]
138 )
139 # Now recompose
140 newstring = ""
141 ix = 0
142 while ix < len(newunicodes):
143 a = newunicodes[ix]
144 if ix + 1 == len(newunicodes):
145 newstring = newstring + chr(a)
146 break
147
148 b = newunicodes[ix + 1]
149 s = chr(a) + chr(b)
150 composed = unicodedata.normalize("NFC", s)
151 if ucd_data(a)["General_Category"][0] == "M":
152 newstring = newstring + chr(a)
153 ix = ix + 1
154 continue
155 elif composed != unicodedata.normalize("NFD", s):
156 assert len(s) == 1
157 newunicodes[ix] = ord(s)
158 del newunicodes[ix + 1]
159 continue
160 else:
161 newstring = newstring + chr(a)
162 ix = ix + 1
163
164 self.buffer.store_unicode(newstring)