| 1 |
import logging |
| 2 |
|
| 3 |
|
| 4 |
def shaper_inputs(self): |
| 5 |
"""Returns a list of potential glyphs to determine whether to test if this |
| 6 |
rule applies at a given point.""" |
| 7 |
return [self.bases.keys(), self.marks.keys()] |
| 8 |
|
| 9 |
|
| 10 |
def find_base_backwards(self, buf, ix): |
| 11 |
"""Looks backwards in a buffer from index ``ix`` to find the nearest base.""" |
| 12 |
start_ix = ix |
| 13 |
ix = ix - 1 |
| 14 |
while ix >= 0: |
| 15 |
if buf[ix].glyph in self.bases.keys(): |
| 16 |
# Check for unhelpful stuff in between |
| 17 |
my_category = buf[ix].category[0] |
| 18 |
for i in range(ix + 1, start_ix): |
| 19 |
if buf[i].category[0] == my_category or buf[i].category[0] == "unknown": |
| 20 |
# Oops, we skipped over another %s to get here |
| 21 |
return None |
| 22 |
return ix |
| 23 |
ix = ix - 1 |
| 24 |
return None |
| 25 |
|
| 26 |
|
| 27 |
def would_apply_at_position(self, buf, ix, namedclasses={}): |
| 28 |
"""Tests to see if this rule would apply at position ``ix`` of the buffer.""" |
| 29 |
from fontFeatures.shaperLib.Rule import _expand_slot |
| 30 |
|
| 31 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 32 |
"Testing if rule would apply at position %i" % (ix) |
| 33 |
) |
| 34 |
if namedclasses: |
| 35 |
marks = _expand_slot(self.marks.keys(), namedclasses) |
| 36 |
bases = _expand_slot(self.bases.keys(), namedclasses) |
| 37 |
else: |
| 38 |
marks = self.marks.keys() |
| 39 |
bases = self.bases.keys() |
| 40 |
|
| 41 |
if self.is_cursive: |
| 42 |
if ix == 0: |
| 43 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 44 |
" * No, it has no adjacent glyph" |
| 45 |
) |
| 46 |
return False |
| 47 |
# We will sort it out later |
| 48 |
# logging.getLogger("fontFeatures.shaperLib").debug(" * Yes, %s/%s is a pair" % (buf[ix].glyph, buf[ix-1].glyph)) |
| 49 |
return True |
| 50 |
|
| 51 |
# Mark to base is a bit different, as multiple marks can attach to a base |
| 52 |
# so we search backwards for the preceding base glyph |
| 53 |
# XXX mark to mark |
| 54 |
if buf[ix].glyph not in marks: |
| 55 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 56 |
" * No, %s is not in our mark list" % (buf[ix].glyph) |
| 57 |
) |
| 58 |
return False |
| 59 |
base_ix = find_base_backwards(self, buf, ix) |
| 60 |
if base_ix is None: |
| 61 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 62 |
" * No, I couldn't find a base glyph" |
| 63 |
) |
| 64 |
return False |
| 65 |
if buf[base_ix].glyph not in bases: |
| 66 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 67 |
" * No, %s is not in our base list" % buf[base_ix].glyph |
| 68 |
) |
| 69 |
return False |
| 70 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 71 |
" * Yes, attaching mark %s/%i to %s/%i" |
| 72 |
% (buf[ix].glyph, ix, buf[base_ix].glyph, base_ix) |
| 73 |
) |
| 74 |
return True |
| 75 |
|
| 76 |
|
| 77 |
def _do_apply_cursive(self, buf, ix): |
| 78 |
this_record = buf[ix] |
| 79 |
prev_record = buf[ix - 1] |
| 80 |
|
| 81 |
mark = prev_record.glyph |
| 82 |
base = this_record.glyph |
| 83 |
|
| 84 |
if mark not in self.marks or base not in self.bases: |
| 85 |
return |
| 86 |
exit_x, exit_y = self.marks.get(mark, (0, 0)) |
| 87 |
entry_x, entry_y = self.bases.get(base, (0, 0)) |
| 88 |
|
| 89 |
i = ix - 1 |
| 90 |
j = ix |
| 91 |
|
| 92 |
if buf.direction == "LTR": |
| 93 |
buf[i].position.xAdvance = exit_x + (buf[i].position.xPlacement or 0) |
| 94 |
d = entry_x + (buf[j].position.xPlacement or 0) |
| 95 |
buf[j].position.xAdvance = (buf[j].position.xAdvance or 0) - d |
| 96 |
buf[j].position.xPlacement = (buf[j].position.xPlacement or 0) - d |
| 97 |
elif buf.direction == "RTL": |
| 98 |
d = exit_x + (buf[i].position.xPlacement or 0) |
| 99 |
logging.getLogger("fontFeatures.shaperLib").debug( |
| 100 |
"Adjusting advance of %s by %i" % (buf[i].glyph, -d) |
| 101 |
) |
| 102 |
buf[i].position.xAdvance = (buf[i].position.xAdvance or 0) - d |
| 103 |
buf[i].position.xPlacement = (buf[i].position.xPlacement or 0) - d |
| 104 |
|
| 105 |
buf[j].position.xAdvance = entry_x + (buf[j].position.xPlacement or 0) |
| 106 |
|
| 107 |
child = i |
| 108 |
parent = j |
| 109 |
x_offset = entry_x - exit_x |
| 110 |
y_offset = entry_y - exit_y |
| 111 |
if not (self.flags & 1): |
| 112 |
parent, child = child, parent |
| 113 |
x_offset = -x_offset |
| 114 |
y_offset = -y_offset |
| 115 |
buf[child].position.yPlacement = y_offset |
| 116 |
this_record.attach_type = "cursive" |
| 117 |
this_record.attach_chain = parent - child |
| 118 |
|
| 119 |
|
| 120 |
def _do_apply(self, buf, ix, namedclasses={}): |
| 121 |
if self.is_cursive: |
| 122 |
return _do_apply_cursive(self, buf, ix) |
| 123 |
|
| 124 |
base_ix = find_base_backwards(self, buf, ix) |
| 125 |
mark = buf[ix].glyph |
| 126 |
base = buf[base_ix].glyph |
| 127 |
xpos = self.bases[base][0] - self.marks[mark][0] |
| 128 |
ypos = self.bases[base][1] - self.marks[mark][1] |
| 129 |
buf[ix].position.xPlacement = xpos |
| 130 |
buf[ix].position.yPlacement = ypos |
| 131 |
buf[ix].attach_type = "mark" |
| 132 |
buf[ix].attach_chain = base_ix - ix |