Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
anthrotype committed Jan 31, 2024
1 parent d71ef26 commit 1819e57
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions Lib/glyphsLib/builder/anchor_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _get_anchor_data(anchor_data, ufo, components, anchor_name):
anchor_data[anchor.name] = t.transformPoint((anchor.x, anchor.y))


def _componentAnchorFromLib(_glyph, _targetComponent):
def _componentAnchorFromLib(_glyph, _targetComponent) -> Optional[str]:
"""Pull component’s named anchor from Glyph.lib"""
if COMPONENT_INFO_KEY in _glyph.lib:
for _anchorLib in _glyph.lib[COMPONENT_INFO_KEY]:
Expand All @@ -101,32 +101,33 @@ def _componentAnchorFromLib(_glyph, _targetComponent):
and _anchorLib["name"] == _targetComponent.baseGlyph
and _anchorLib["index"] == _glyph.components.index(_targetComponent)
):
return _anchorLib["anchor"]
return _anchorLib["anchor"] or None
return None


def _adjust_anchors(anchor_data, ufo, parent, component):
"""Adjust anchors to which a mark component may have been attached."""
glyph = ufo[component.baseGlyph]
anchor_names = {a.name for a in glyph.anchors}
t = Transform(*component.transformation)
_componentAnchor = _componentAnchorFromLib(parent, component)
for anchor in glyph.anchors:
if anchor.name.startswith("_"):
# only base anchors should get propagated
continue
component_anchor = _componentAnchorFromLib(parent, component)
# ignore the component's named anchor if we don't have it
if component_anchor not in anchor_data:
component_anchor = None
# For each base anchor in the mark component glyph
for anchor in (a for a in glyph.anchors if not a.name.startswith("_")):
# adjust either if component is attached to a specific named anchor
# (e.g. top_2 for a ligature glyph)
# rather than to the standard anchors (top/bottom)
if (
_componentAnchor is not None
and _componentAnchor.startswith(anchor.name + "_")
and _componentAnchor in anchor_data
component_anchor
and component_anchor.startswith(anchor.name + "_")
and "_" + anchor.name in anchor_names
):
anchor_data[_componentAnchor] = t.transformPoint((anchor.x, anchor.y))
anchor_data[component_anchor] = t.transformPoint((anchor.x, anchor.y))
# ... or this anchor has data and the component also contains
# the associated mark anchor (e.g. "_top" for "top") ...
elif anchor.name in anchor_data and any(
a.name == "_" + anchor.name for a in glyph.anchors
):
elif anchor.name in anchor_data and "_" + anchor.name in anchor_names:
anchor_data[anchor.name] = t.transformPoint((anchor.x, anchor.y))


Expand Down

0 comments on commit 1819e57

Please sign in to comment.