| 1 |
from .BaseShaper import BaseShaper |
| 2 |
from fontFeatures.shaperLib.Buffer import BufferItem |
| 3 |
|
| 4 |
LBase = 0x1100 |
| 5 |
VBase = 0x1161 |
| 6 |
TBase = 0x11A7 |
| 7 |
LCount = 19 |
| 8 |
VCount = 21 |
| 9 |
TCount = 28 |
| 10 |
SBase = 0xAC00 |
| 11 |
NCount = VCount * TCount |
| 12 |
SCount = LCount * NCount |
| 13 |
|
| 14 |
|
| 15 |
def isCombiningL(u): |
| 16 |
return LBase <= u <= LBase + LCount - 1 |
| 17 |
|
| 18 |
|
| 19 |
def isCombiningV(u): |
| 20 |
return VBase <= u <= VBase + VCount - 1 |
| 21 |
|
| 22 |
|
| 23 |
def isCombiningT(u): |
| 24 |
return TBase + 1 <= u <= TBase + TCount - 1 |
| 25 |
|
| 26 |
|
| 27 |
def isCombinedS(u): |
| 28 |
return SBase <= u <= SBase + SCount - 1 |
| 29 |
|
| 30 |
|
| 31 |
def isL(u): |
| 32 |
return 0x1100 <= u <= 0x115F or 0xA960 <= u <= 0xA97C |
| 33 |
|
| 34 |
|
| 35 |
def isV(u): |
| 36 |
return 0x1160 <= u <= 0x11A7 or 0xD7B0 <= u <= 0xD7C6 |
| 37 |
|
| 38 |
|
| 39 |
def isT(u): |
| 40 |
return 0x11A8 <= u <= 0x11FF or 0xD7CB <= u <= 0xD7FB |
| 41 |
|
| 42 |
|
| 43 |
def isHangulTone(u): |
| 44 |
return 0x302E <= u <= 0x302F |
| 45 |
|
| 46 |
|
| 47 |
class HangulShaper(BaseShaper): |
| 48 |
def collect_features(self, shaper): |
| 49 |
shaper.add_features("ljmo", "vjmo", "tjmo") |
| 50 |
|
| 51 |
def override_features(self, shaper): |
| 52 |
shaper.disable_feature("calt") |
| 53 |
|
| 54 |
def preprocess_text(self): |
| 55 |
def cp(i): |
| 56 |
return self.buffer.items[i].codepoint |
| 57 |
|
| 58 |
def has_glyph(u): |
| 59 |
return self.font.unicode_map.get(u, None) |
| 60 |
|
| 61 |
# Setup masks here too |
| 62 |
for i in self.buffer.items: |
| 63 |
i.feature_masks["ljmo"] = False |
| 64 |
i.feature_masks["vjmo"] = False |
| 65 |
i.feature_masks["tjmo"] = False |
| 66 |
start, end, processed = 0, 0, 0 |
| 67 |
count = len(self.buffer.items) |
| 68 |
for i in range(count): |
| 69 |
u = cp(i) |
| 70 |
if isHangulTone(u): |
| 71 |
if start < end and end == processed: |
| 72 |
if not self.is_zero_width_char(u): |
| 73 |
# merge out clusters? |
| 74 |
|
| 75 |
# Move end to start |
| 76 |
l = self.buffer.items |
| 77 |
l.insert(start, l.pop(end)) |
| 78 |
else: |
| 79 |
if has_glyph(0x25CC): |
| 80 |
if self.is_zero_width_char(u): |
| 81 |
l.insert(i + 1, BufferItem.new_unicode(0x25CC)) |
| 82 |
else: |
| 83 |
l.insert(i, BufferItem.new_unicode(0x25CC)) |
| 84 |
start = end = i |
| 85 |
continue |
| 86 |
start = i |
| 87 |
if isL(u) and i + 1 < count: |
| 88 |
l = u |
| 89 |
v = cp(i + 1) |
| 90 |
if isV(v): |
| 91 |
# <L,V> or <L,V,T> |
| 92 |
t, tindex = 0, 0 |
| 93 |
if i + 2 < count: |
| 94 |
t = cp(i + 2) |
| 95 |
if isT(t): |
| 96 |
tindex = t - TBase |
| 97 |
else: |
| 98 |
t = 0 |
| 99 |
# Unsafe to break |
| 100 |
if ( |
| 101 |
isCombiningL(l) |
| 102 |
and isCombiningV(v) |
| 103 |
and (not t or isCombiningT(t)) |
| 104 |
): |
| 105 |
s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex |
| 106 |
if has_glyph(s): |
| 107 |
# Combine |
| 108 |
if t: |
| 109 |
self.buffer.items[i : i + 3] = [ |
| 110 |
BufferItem.new_unicode(s) |
| 111 |
] |
| 112 |
else: |
| 113 |
self.buffer.items[i : i + 2] = [ |
| 114 |
BufferItem.new_unicode(s) |
| 115 |
] |
| 116 |
end = start + 1 |
| 117 |
continue |
| 118 |
# Didn't compose |
| 119 |
self.buffer.items[i].feature_masks["ljmo"] = True |
| 120 |
self.buffer.items[i + 1].feature_masks["vjmo"] = True |
| 121 |
if t: |
| 122 |
self.buffer.items[i + 2].feature_masks["tjmo"] = True |
| 123 |
end = start + 3 |
| 124 |
else: |
| 125 |
end = start + 2 |
| 126 |
continue |
| 127 |
elif isCombinedS(u): |
| 128 |
s = u |
| 129 |
has_glyph_s = has_glyph(s) |
| 130 |
lindex = (s - SBase) // NCount |
| 131 |
nindex = (s - SBase) % NCount |
| 132 |
vindex = nindex // TCount |
| 133 |
tindex = nindex % TCount |
| 134 |
if ( |
| 135 |
not tindex |
| 136 |
and i + 1 < count |
| 137 |
and isCombiningT(self.buffer.items[i + 1].codepoint) |
| 138 |
): |
| 139 |
# <LV,T> |
| 140 |
new_tindex = cp(i + 1) - TBase |
| 141 |
new_s = s + new_tindex |
| 142 |
if has_glyph(new_s): |
| 143 |
self.buffer.items[i : i + 2] = [BufferItem.new_unicode(new_s)] |
| 144 |
end = start + 1 |
| 145 |
continue |
| 146 |
else: |
| 147 |
# unsafe to break |
| 148 |
pass |
| 149 |
if not has_glyph or (not tindex and i + 1 < count and isT(cp(i + 1))): |
| 150 |
decomposed = [LBase + lindex, VBase + vindex, TBase + tindex] |
| 151 |
if ( |
| 152 |
has_glyph(decomposed[0]) |
| 153 |
and has_glyph(decomposed[1]) |
| 154 |
and (not tindex or has_glyph(decomposed[2])) |
| 155 |
): |
| 156 |
if not tindex: |
| 157 |
decomposed.pop() |
| 158 |
s_len = len(decomposed) |
| 159 |
self.buffer.items[i : i + s_len] = [ |
| 160 |
BufferItem.new_unicode(u) for u in decomposed |
| 161 |
] |
| 162 |
if has_glyph_s and not tindex: |
| 163 |
i = i + 1 |
| 164 |
s_len = s_len + 1 |
| 165 |
end = start + s_len |
| 166 |
self.buffer.items[start + 1].feature_masks["ljmo"] = True |
| 167 |
self.buffer.items[start + 2].feature_masks["vjmo"] = True |
| 168 |
if start + 3 < end: |
| 169 |
self.buffer.items[start + 3].feature_masks["tjmo"] = True |
| 170 |
# merge out clusters |
| 171 |
continue |
| 172 |
elif not tindex and i + 1 < count and isT(cp(i + 1)): |
| 173 |
# Mark unsafe to break |
| 174 |
pass |