Created
May 10, 2022 13:42
-
-
Save mathieureguer/5829220cea708cfda5016c4f17eb1346 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IGNORE_ANCHORS = [] | |
# ---------------------------------------- | |
def match_anchor(anchor_source, anchor_targets): | |
if anchor_source.name.startswith("_"): | |
target_name = anchor_source.name[1:] | |
else: | |
target_name = "_" + anchor_source.name | |
for a in anchor_targets: | |
if a.name == target_name: | |
return a | |
def offset_anchor(anchor, offset): | |
"""return a copy of the anchor, with the offset applied""" | |
out = anchor.copy() | |
out.moveBy(offset) | |
return out | |
# ---------------------------------------- | |
f = CurrentFont() | |
for g in f.selectedGlyphs: | |
comp = g.components | |
collected_anchors = [] | |
for c in comp: | |
base = f[c.baseGlyph] | |
base_anchors = [offset_anchor(a, c.offset) for a in base.anchors] | |
collected_anchors.append(base_anchors) | |
# filter used anchors | |
filtered_anchors = [] | |
for i, anchors in enumerate(collected_anchors): | |
filtered_anchors.append([]) | |
for a in anchors: | |
# locate previous and next component parts | |
if i < len(collected_anchors) - 1: | |
next_part = collected_anchors[i + 1] | |
else: | |
next_part = [] | |
if i > 0: | |
prev_part = collected_anchors[i - 1] | |
else: | |
prev_part = [] | |
# look behind for matching anchor | |
if a.name.startswith("_"): | |
match = match_anchor(a, prev_part) | |
# look ahead for matching anchor | |
else: | |
match = match_anchor(a, next_part) | |
# collect only non matching anchors | |
if not match: | |
filtered_anchors[-1].append(a) | |
collected_anchors = filtered_anchors | |
for anchors in reversed(collected_anchors): | |
for anchor in anchors: | |
if anchor.name not in [a.name for a in g.anchors] and anchor.name not in IGNORE_ANCHORS: | |
g.appendAnchor(anchor.name, anchor.position) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment