| 1 |
import logging |
| 2 |
|
| 3 |
__all__ = [ |
| 4 |
"would_apply_at_position", |
| 5 |
"pre_post_context_matches", |
| 6 |
"_expand_slot", |
| 7 |
] |
| 8 |
|
| 9 |
|
| 10 |
def i2s(buffer_items): |
| 11 |
return "|".join([x.glyph for x in buffer_items]) |
| 12 |
|
| 13 |
|
| 14 |
def _expand_slot(slot, namedclasses={}): |
| 15 |
expanded = [] |
| 16 |
for g in slot: |
| 17 |
if g.startswith("@"): |
| 18 |
expanded.extend(namedclasses.get(g[1:], "")) |
| 19 |
else: |
| 20 |
expanded.append(g) |
| 21 |
return expanded |
| 22 |
|
| 23 |
|
| 24 |
def glyphs_match(buffer_glyphs, routine_glyphs, namedclasses={}): |
| 25 |
if len(buffer_glyphs) != len(routine_glyphs): |
| 26 |
return False |
| 27 |
for a, b in zip(buffer_glyphs, routine_glyphs): |
| 28 |
if a.glyph not in _expand_slot(b, namedclasses): |
| 29 |
return False |
| 30 |
return True |
| 31 |
|
| 32 |
|
| 33 |
def pre_post_context_matches(self, buf, ix, namedclasses={}): |
| 34 |
if hasattr(self, "precontext") and self.precontext: |
| 35 |
if ix < len(self.precontext): |
| 36 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 37 |
" - No, not enough precontext" |
| 38 |
) |
| 39 |
return False |
| 40 |
precontext = buf[ix - len(self.precontext) : ix] |
| 41 |
if not glyphs_match(precontext, self.precontext, namedclasses): |
| 42 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 43 |
" - No, precontext doesn't match %s != %s" |
| 44 |
% (i2s(precontext), self.precontext) |
| 45 |
) |
| 46 |
return False |
| 47 |
if hasattr(self, "postcontext") and self.postcontext: |
| 48 |
coverage = self.shaper_inputs() |
| 49 |
coverage_l = len(coverage) |
| 50 |
end_of_coverage = ix + coverage_l |
| 51 |
if end_of_coverage + len(self.postcontext) > len(buf): |
| 52 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 53 |
" - No, not enough postcontext" |
| 54 |
) |
| 55 |
return False |
| 56 |
postcontext = buf[end_of_coverage : end_of_coverage + len(self.postcontext)] |
| 57 |
if not glyphs_match(postcontext, self.postcontext, namedclasses): |
| 58 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 59 |
" - No, postcontext doesn't match %s != %s" |
| 60 |
% (i2s(postcontext), self.postcontext) |
| 61 |
) |
| 62 |
return False |
| 63 |
return True |
| 64 |
|
| 65 |
|
| 66 |
def would_apply_at_position(self, buf, ix, namedclasses={}): |
| 67 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 68 |
"Testing if rule would apply at position %i" % (ix) |
| 69 |
) |
| 70 |
coverage = self.shaper_inputs() |
| 71 |
coverage_l = len(coverage) |
| 72 |
if coverage_l < 1: |
| 73 |
return False |
| 74 |
buffer_glyphs = buf[ix : ix + coverage_l] |
| 75 |
|
| 76 |
if not glyphs_match(buffer_glyphs, coverage, namedclasses): |
| 77 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 78 |
" - No! %s != %s" % (i2s(buffer_glyphs), coverage) |
| 79 |
) |
| 80 |
return False |
| 81 |
|
| 82 |
if not pre_post_context_matches(self, buf, ix, namedclasses): |
| 83 |
return False |
| 84 |
|
| 85 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 86 |
" - Yes! %s == %s" % (i2s(buffer_glyphs), coverage) |
| 87 |
) |
| 88 |
return True |