Skip to content

Commit

Permalink
Add translate anchors filter
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Jul 14, 2021
1 parent 86c3357 commit afb06e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/ufo2ft/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .decomposeTransformedComponents import DecomposeTransformedComponentsFilter
from .explodeColorLayerGlyphs import ExplodeColorLayerGlyphsFilter
from .flattenComponents import FlattenComponentsFilter
from .optimizeAnchors import OptimizeAnchorsFilter
from .propagateAnchors import PropagateAnchorsFilter
from .removeOverlaps import RemoveOverlapsFilter
from .sortContours import SortContoursFilter
Expand All @@ -23,6 +24,7 @@
"DecomposeTransformedComponentsFilter",
"ExplodeColorLayerGlyphsFilter",
"FlattenComponentsFilter",
"OptimizeAnchorsFilter",
"PropagateAnchorsFilter",
"RemoveOverlapsFilter",
"SortContoursFilter",
Expand Down
27 changes: 27 additions & 0 deletions Lib/ufo2ft/filters/optimizeAnchors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from ufo2ft.filters.transformations import TransformationsFilter
from fontTools.misc.transform import Identity, Transform
import logging

log = logging.getLogger(__name__)


class OptimizeAnchorsFilter(TransformationsFilter):
def set_context(self, font, glyphSet):
# Skip over transformations filter to base filter
return super(TransformationsFilter, self).set_context(font, glyphSet)

def filter(self, glyph):
if len(glyph.anchors) == 0 or any(
not (a.name.startswith("_")) for a in glyph.anchors
):
# We're a base!
return False

# We are a mark glyph with (at least) one attachment point.
theanchor = glyph.anchors[0]
self.context.matrix = Identity.translate(-theanchor.x, -theanchor.y)
log.warn(
"Transforming glyph %s to zero anchor %s: %s"
% (glyph.name, theanchor.name, self.context.matrix)
)
return super().filter(glyph)

0 comments on commit afb06e4

Please sign in to comment.