| 1 |
"""The base class for complex shapers. |
| 2 |
|
| 3 |
Shaper.py is the top-level interface to shaperlib. This is a lower-level |
| 4 |
module implementing common routines used to the individual complex shapers.""" |
| 5 |
|
| 6 |
from fontFeatures.shaperLib.Buffer import Buffer, BufferItem |
| 7 |
from copy import copy |
| 8 |
import unicodedata |
| 9 |
from youseedee import ucd_data |
| 10 |
from fontFeatures import RoutineReference |
| 11 |
|
| 12 |
|
| 13 |
def _is_default_ignorable(c): |
| 14 |
return ( |
| 15 |
c in [0x00AD, 0x034F, 0x061C, 0x17B4, 0x17B5, 0xFEFF] |
| 16 |
or 0x180B <= c <= 0x180E |
| 17 |
or 0x200B <= c <= 0x200F |
| 18 |
or 0x202A <= c <= 0x202E |
| 19 |
or 0x2060 <= c <= 0x206F |
| 20 |
or 0xFE00 <= c <= 0xFE0F |
| 21 |
or 0xFFF0 <= c <= 0xFFF8 |
| 22 |
or 0x1D173 <= c <= 0x1D17A |
| 23 |
or 0xE0000 <= c <= 0xE0FFF |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class BaseShaper: |
| 28 |
"""A complex shaper object.""" |
| 29 |
|
| 30 |
def __init__(self, plan, font, buf, features=[]): |
| 31 |
self.plan = plan |
| 32 |
self.font = font |
| 33 |
self.buffer = buf |
| 34 |
self.features = features |
| 35 |
|
| 36 |
def shape(self): |
| 37 |
"""Shape a buffer.""" |
| 38 |
self.plan.fontfeatures.resolveAllRoutines() |
| 39 |
# self.buffer.set_unicode_props() |
| 40 |
# self.insert_dotted_circles() |
| 41 |
# self.buffer.form_clusters() |
| 42 |
# self.buffer.ensure_native_direction() |
| 43 |
if not self.buffer.is_all_glyphs: |
| 44 |
self.preprocess_text() |
| 45 |
# Substitute pre |
| 46 |
self.substitute_default() |
| 47 |
self.substitute_complex() |
| 48 |
# Substitute post |
| 49 |
self.delete_default_ignorables() |
| 50 |
|
| 51 |
self.position() |
| 52 |
self.postprocess_glyphs() |
| 53 |
# self.buffer.propagate_flags() |
| 54 |
|
| 55 |
def preprocess_text(self): |
| 56 |
"""Do any processing which needs to happen to character items before shaping.""" |
| 57 |
pass |
| 58 |
|
| 59 |
def postprocess_glyphs(self): |
| 60 |
"""Do any processing which needs to happen to glyph items after shaping.""" |
| 61 |
pass |
| 62 |
|
| 63 |
def substitute_default(self): |
| 64 |
"""Do initial subsitution processing: buffer normalization and mapping.""" |
| 65 |
self.normalize_unicode_buffer() |
| 66 |
self.buffer.map_to_glyphs() |
| 67 |
self.plan.msg("Initial glyph mapping", self.buffer) |
| 68 |
# Setup masks |
| 69 |
# if self.buf.fallback_mark_positioning: |
| 70 |
# self.fallback_mark_position_recategorize_marks() |
| 71 |
pass |
| 72 |
|
| 73 |
def normalize_unicode_buffer(self): |
| 74 |
"""Normalize an OpenType buffer.""" |
| 75 |
unistring = "".join([chr(item.codepoint) for item in self.buffer.items]) |
| 76 |
self.buffer.store_unicode(unicodedata.normalize("NFC", unistring)) |
| 77 |
|
| 78 |
# Some fix-ups from hb-ot-shape-normalize |
| 79 |
for item in self.buffer.items: |
| 80 |
if ucd_data(item.codepoint)[ |
| 81 |
"General_Category" |
| 82 |
] == "Zs" and self.font.unicode_map.get(0x20, None): |
| 83 |
item.codepoint = 0x20 |
| 84 |
# Harfbuzz adjusts the width here, in _hb_ot_shape_fallback_spaces |
| 85 |
if item.codepoint == 0x2011 and self.font.unicode_map.get(0x2010, None): |
| 86 |
item.codepoint = 0x2010 |
| 87 |
|
| 88 |
def collect_features(self, shaper): |
| 89 |
"""Collect all complex features to be run by this shaper. (Abstract method.)""" |
| 90 |
return [] |
| 91 |
|
| 92 |
def substitute_complex(self): |
| 93 |
"""Run the substitution stage.""" |
| 94 |
self._run_stage("sub") |
| 95 |
|
| 96 |
def position(self): |
| 97 |
"""Run the positioning stage.""" |
| 98 |
self._run_stage("pos") |
| 99 |
self.buffer.clear_mask() |
| 100 |
# zero width marks |
| 101 |
for i in self.buffer.items: |
| 102 |
if i.category[0] == "mark": |
| 103 |
i.position.xAdvance = 0 |
| 104 |
# zero width default ignorables |
| 105 |
self.zero_width_default_ignorables() |
| 106 |
for i in range(0, len(self.buffer.items)): |
| 107 |
self.propagate_attachment_offsets(i) |
| 108 |
self.plan.msg("Positioning done", self.buffer) |
| 109 |
|
| 110 |
def propagate_attachment_offsets(self, i): |
| 111 |
"""Resolve attachment offsets for item ``i``.""" |
| 112 |
if not hasattr(self.buffer.items[i], "attach_type"): |
| 113 |
return |
| 114 |
attach_type = self.buffer.items[i].attach_type |
| 115 |
attach_chain = self.buffer.items[i].attach_chain |
| 116 |
if not attach_chain: |
| 117 |
return |
| 118 |
self.buffer.items[i].attach_chain = None |
| 119 |
j = i + attach_chain |
| 120 |
if j >= len(self.buffer.items): |
| 121 |
return |
| 122 |
self.propagate_attachment_offsets(j) |
| 123 |
if attach_type == "cursive": |
| 124 |
pass |
| 125 |
# self.buffer.items[i].position.yPlacement = (self.buffer.items[i].position.yPlacement or 0) + (self.buffer.items[j].position.yPlacement or 0) # XXX Horizontal only |
| 126 |
else: |
| 127 |
self.buffer.items[i].position.xPlacement += ( |
| 128 |
self.buffer.items[j].position.xPlacement or 0 |
| 129 |
) |
| 130 |
self.buffer.items[i].position.yPlacement += ( |
| 131 |
self.buffer.items[j].position.yPlacement or 0 |
| 132 |
) |
| 133 |
assert j < i |
| 134 |
if self.buffer.direction == "LTR": |
| 135 |
for k in range(j, i): |
| 136 |
self.buffer.items[i].position.xPlacement -= self.buffer.items[ |
| 137 |
k |
| 138 |
].position.xAdvance |
| 139 |
self.buffer.items[i].position.yPlacement -= ( |
| 140 |
self.buffer.items[k].position.yAdvance or 0 |
| 141 |
) |
| 142 |
else: |
| 143 |
for k in range(j + 1, i + 1): |
| 144 |
self.buffer.items[i].position.xPlacement += self.buffer.items[ |
| 145 |
k |
| 146 |
].position.xAdvance |
| 147 |
self.buffer.items[i].position.yPlacement += ( |
| 148 |
self.buffer.items[k].position.yAdvance or 0 |
| 149 |
) |
| 150 |
|
| 151 |
def _run_stage(self, current_stage): |
| 152 |
self.plan.msg("Running %s stage" % current_stage) |
| 153 |
self.plan.fontfeatures.hoist_languages() |
| 154 |
for stage in self.plan.stages: |
| 155 |
lookups = [] |
| 156 |
if isinstance(stage, list): # Features |
| 157 |
for f in stage: |
| 158 |
if f not in self.plan.fontfeatures.features: |
| 159 |
continue |
| 160 |
|
| 161 |
routines = self.plan.fontfeatures.features[f] |
| 162 |
routines = [ |
| 163 |
x.routine if isinstance(x, RoutineReference) else x |
| 164 |
for x in routines |
| 165 |
] |
| 166 |
lookups.extend( |
| 167 |
[(routine, f) for routine in self._filter_by_lang(routines)] |
| 168 |
) |
| 169 |
self.plan.msg("Processing features: %s" % ",".join(stage)) |
| 170 |
for r, feature in lookups: |
| 171 |
self.plan.msg( |
| 172 |
"Before %s (%s)" % (r.name, feature), buffer=self.buffer |
| 173 |
) |
| 174 |
r.apply_to_buffer( |
| 175 |
self.buffer, |
| 176 |
stage=current_stage, |
| 177 |
feature=feature, |
| 178 |
namedclasses=self.plan.fontfeatures.namedClasses, |
| 179 |
) |
| 180 |
self.plan.msg( |
| 181 |
"After %s (%s)" % (r.name, feature), buffer=self.buffer |
| 182 |
) |
| 183 |
else: |
| 184 |
# It's a pause. We only support GSUB pauses. |
| 185 |
if current_stage == "sub": |
| 186 |
stage(current_stage) |
| 187 |
|
| 188 |
def _filter_by_lang(self, routines): |
| 189 |
script = self.script_to_opentype.get(self.buffer.script, "DFLT") |
| 190 |
s_l = self.plan.fontfeatures.scripts_and_languages |
| 191 |
if ( |
| 192 |
not self.buffer.script |
| 193 |
or len(s_l.values()) < 2 |
| 194 |
and (len(s_l.values()) == 0 or len(list(s_l.values())[0]) < 2) |
| 195 |
): |
| 196 |
return routines # ! |
| 197 |
language = self.buffer.language or "dflt" |
| 198 |
if script in s_l and (script, language) != ("DFLT", "dflt"): |
| 199 |
return [ |
| 200 |
x for x in routines if x.languages and (script, language) in x.languages |
| 201 |
] |
| 202 |
else: |
| 203 |
return [ |
| 204 |
x |
| 205 |
for x in routines |
| 206 |
if not x.languages or ("DFLT", language) in x.languages |
| 207 |
] |
| 208 |
|
| 209 |
def delete_default_ignorables(self): |
| 210 |
"""Remove all items from the buffer which are ignorable.""" |
| 211 |
self.buffer.items = [ |
| 212 |
x |
| 213 |
for x in self.buffer.items |
| 214 |
if not (x.codepoint) or not _is_default_ignorable(x.codepoint) |
| 215 |
] |
| 216 |
|
| 217 |
def zero_width_default_ignorables(self): |
| 218 |
"""Set all items from the buffer which are ignorable to zero advance width.""" |
| 219 |
space = BufferItem.new_unicode(0x20) |
| 220 |
space.map_to_glyph(self.buffer.font) |
| 221 |
if space.glyph == -1: |
| 222 |
return |
| 223 |
for ix, item in enumerate(self.buffer.items): |
| 224 |
if item.codepoint and _is_default_ignorable(item.codepoint): |
| 225 |
item.glyph = space.glyph |
| 226 |
item.position.xAdvance = 0 |
| 227 |
for i in self.buffer.items[ix:]: |
| 228 |
if hasattr(i, "syllable_index"): |
| 229 |
i.syllable_index += 1 |
| 230 |
|
| 231 |
def would_substitute(self, feature, subbuffer_items): |
| 232 |
"""Test whether the feature would apply to a list of BufferItem objects.""" |
| 233 |
if feature not in self.plan.fontfeatures.features: |
| 234 |
return False |
| 235 |
subbuffer = Buffer( |
| 236 |
self.buffer.font, |
| 237 |
direction=self.buffer.direction, |
| 238 |
script=self.buffer.script, |
| 239 |
language=self.buffer.language, |
| 240 |
) |
| 241 |
subbuffer.clear_mask() |
| 242 |
subbuffer.items = [copy(x) for x in subbuffer_items] |
| 243 |
subbuffer.clear_mask() |
| 244 |
routines = self.plan.fontfeatures.features[feature] |
| 245 |
for r in routines: |
| 246 |
if isinstance(r, RoutineReference): |
| 247 |
r = r.routine |
| 248 |
for rule in r.rules: |
| 249 |
if rule.stage == "pos": |
| 250 |
continue |
| 251 |
if any( |
| 252 |
[ |
| 253 |
rule.would_apply_at_position( |
| 254 |
subbuffer, |
| 255 |
i, |
| 256 |
namedclasses=self.plan.fontfeatures.namedClasses, |
| 257 |
) |
| 258 |
for i in range(len(subbuffer)) |
| 259 |
] |
| 260 |
): |
| 261 |
return True |
| 262 |
return False |
| 263 |
|
| 264 |
script_to_opentype = { |
| 265 |
"Common": "zyyy", |
| 266 |
"Inherited": "zinh", |
| 267 |
"Unknown": "zzzz", |
| 268 |
"Arabic": "arab", |
| 269 |
"Armenian": "armn", |
| 270 |
"Bengali": "bng2", # Not beng |
| 271 |
"Cyrillic": "cyrl", |
| 272 |
"Devanagari": "dev2", # Not deva |
| 273 |
"Georgian": "geor", |
| 274 |
"Greek": "grek", |
| 275 |
"Gujarati": "gjr2", # Not gujr |
| 276 |
"Gurmukhi": "gur2", # Not guru |
| 277 |
"Hangul": "hang", |
| 278 |
"Han": "hani", |
| 279 |
"Hebrew": "hebr", |
| 280 |
"Hiragana": "hira", |
| 281 |
"Kannada": "knd2", # Not knda |
| 282 |
"Katakana": "kana", |
| 283 |
"Lao": "laoo", |
| 284 |
"Latin": "latn", |
| 285 |
"Malayalam": "mlm2", # Not mlym |
| 286 |
"Oriya": "ory2", # Not orya |
| 287 |
"Tamil": "tml2", # Not taml |
| 288 |
"Telugu": "tel2", # Not telu |
| 289 |
"Thai": "thai", |
| 290 |
"Tibetan": "tibt", |
| 291 |
"Bopomofo": "bopo", |
| 292 |
"Braille": "brai", |
| 293 |
"Canadian_Syllabics": "cans", |
| 294 |
"Cherokee": "cher", |
| 295 |
"Ethiopic": "ethi", |
| 296 |
"Khmer": "khmr", |
| 297 |
"Mongolian": "mong", |
| 298 |
"Myanmar": "mym2", # Not mymr |
| 299 |
"Ogham": "ogam", |
| 300 |
"Runic": "runr", |
| 301 |
"Sinhala": "sinh", |
| 302 |
"Syriac": "syrc", |
| 303 |
"Thaana": "thaa", |
| 304 |
"Yi": "yiii", |
| 305 |
"Deseret": "dsrt", |
| 306 |
"Gothic": "goth", |
| 307 |
"Old_Italic": "ital", |
| 308 |
"Buhid": "buhd", |
| 309 |
"Hanunoo": "hano", |
| 310 |
"Tagalog": "tglg", |
| 311 |
"Tagbanwa": "tagb", |
| 312 |
"Cypriot": "cprt", |
| 313 |
"Limbu": "limb", |
| 314 |
"Linear_B": "linb", |
| 315 |
"Osmanya": "osma", |
| 316 |
"Shavian": "shaw", |
| 317 |
"Tai_Le": "tale", |
| 318 |
"Ugaritic": "ugar", |
| 319 |
"Buginese": "bugi", |
| 320 |
"Coptic": "copt", |
| 321 |
"Glagolitic": "glag", |
| 322 |
"Kharoshthi": "khar", |
| 323 |
"New_Tai_Lue": "talu", |
| 324 |
"Old_Persian": "xpeo", |
| 325 |
"Syloti_Nagri": "sylo", |
| 326 |
"Tifinagh": "tfng", |
| 327 |
"Balinese": "bali", |
| 328 |
"Cuneiform": "xsux", |
| 329 |
"Nko": "nkoo", |
| 330 |
"Phags_Pa": "phag", |
| 331 |
"Phoenician": "phnx", |
| 332 |
"Carian": "cari", |
| 333 |
"Cham": "cham", |
| 334 |
"Kayah_Li": "kali", |
| 335 |
"Lepcha": "lepc", |
| 336 |
"Lycian": "lyci", |
| 337 |
"Lydian": "lydi", |
| 338 |
"Ol_Chiki": "olck", |
| 339 |
"Rejang": "rjng", |
| 340 |
"Saurashtra": "saur", |
| 341 |
"Sundanese": "sund", |
| 342 |
"Vai": "vaii", |
| 343 |
"Avestan": "avst", |
| 344 |
"Bamum": "bamu", |
| 345 |
"Egyptian_Hieroglyphs": "egyp", |
| 346 |
"Imperial_Aramaic": "armi", |
| 347 |
"Inscriptional_Pahlavi": "phli", |
| 348 |
"Inscriptional_Parthian": "prti", |
| 349 |
"Javanese": "java", |
| 350 |
"Kaithi": "kthi", |
| 351 |
"Lisu": "lisu", |
| 352 |
"Meetei_Mayek": "mtei", |
| 353 |
"Old_South_Arabian": "sarb", |
| 354 |
"Old_Turkic": "orkh", |
| 355 |
"Samaritan": "samr", |
| 356 |
"Tai_Tham": "lana", |
| 357 |
"Tai_Viet": "tavt", |
| 358 |
"Batak": "batk", |
| 359 |
"Brahmi": "brah", |
| 360 |
"Mandaic": "mand", |
| 361 |
"Chakma": "cakm", |
| 362 |
"Meroitic_Cursive": "merc", |
| 363 |
"Meroitic_Hieroglyphs": "mero", |
| 364 |
"Miao": "plrd", |
| 365 |
"Sharada": "shrd", |
| 366 |
"Sora_Sompeng": "sora", |
| 367 |
"Takri": "takr", |
| 368 |
"Bassa_Vah": "bass", |
| 369 |
"Caucasian_Albanian": "aghb", |
| 370 |
"Duployan": "dupl", |
| 371 |
"Elbasan": "elba", |
| 372 |
"Grantha": "gran", |
| 373 |
"Khojki": "khoj", |
| 374 |
"Khudawadi": "sind", |
| 375 |
"Linear_A": "lina", |
| 376 |
"Mahajani": "mahj", |
| 377 |
"Manichaean": "mani", |
| 378 |
"Mende_Kikakui": "mend", |
| 379 |
"Modi": "modi", |
| 380 |
"Mro": "mroo", |
| 381 |
"Nabataean": "nbat", |
| 382 |
"Old_North_Arabian": "narb", |
| 383 |
"Old_Permic": "perm", |
| 384 |
"Pahawh_Hmong": "hmng", |
| 385 |
"Palmyrene": "palm", |
| 386 |
"Pau_Cin_Hau": "pauc", |
| 387 |
"Psalter_Pahlavi": "phlp", |
| 388 |
"Siddham": "sidd", |
| 389 |
"Tirhuta": "tirh", |
| 390 |
"Warang_Citi": "wara", |
| 391 |
"Ahom": "ahom", |
| 392 |
"Anatolian_Hieroglyphs": "hluw", |
| 393 |
"Hatran": "hatr", |
| 394 |
"Multani": "mult", |
| 395 |
"Old_Hungarian": "hung", |
| 396 |
"Signwriting": "sgnw", |
| 397 |
"Adlam": "adlm", |
| 398 |
"Bhaiksuki": "bhks", |
| 399 |
"Marchen": "marc", |
| 400 |
"Osage": "osge", |
| 401 |
"Tangut": "tang", |
| 402 |
"Newa": "newa", |
| 403 |
"Masaram_Gondi": "gonm", |
| 404 |
"Nushu": "nshu", |
| 405 |
"Soyombo": "soyo", |
| 406 |
"Zanabazar_Square": "zanb", |
| 407 |
"Dogra": "dogr", |
| 408 |
"Gunjala_Gondi": "gong", |
| 409 |
"Hanifi_Rohingya": "rohg", |
| 410 |
"Makasar": "maka", |
| 411 |
"Medefaidrin": "medf", |
| 412 |
"Old_Sogdian": "sogo", |
| 413 |
"Sogdian": "sogd", |
| 414 |
"Elymaic": "elym", |
| 415 |
"Nandinagari": "nand", |
| 416 |
"Nyiakeng_Puachue_Hmong": "hmnp", |
| 417 |
"Wancho": "wcho", |
| 418 |
"Chorasmian": "chrs", |
| 419 |
"Dives_Akuru": "diak", |
| 420 |
"Khitan_Small_Script": "kits", |
| 421 |
"Yezidi": "yezi", |
| 422 |
} |