From e9e8f9748966add7a33ee7a61ce0599f8a5d381e Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 26 Jan 2024 15:40:44 +0000 Subject: [PATCH] make contextual mark writer continue to work with old ufo2ft Follow up from #957 to avoid introducing a too strong dependency on ufo2ft v3 --- .../featureWriters/markFeatureWriter.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/glyphsLib/featureWriters/markFeatureWriter.py b/Lib/glyphsLib/featureWriters/markFeatureWriter.py index 67c9ffb6a..e40ba3500 100644 --- a/Lib/glyphsLib/featureWriters/markFeatureWriter.py +++ b/Lib/glyphsLib/featureWriters/markFeatureWriter.py @@ -11,6 +11,7 @@ MarkToBasePos, NamedAnchor, ) +from ufo2ft.util import quantize class ContextuallyAwareNamedAnchor(NamedAnchor): @@ -115,6 +116,21 @@ def __init__(self, name, x, y, markClass=None, libData=None): class ContextualMarkFeatureWriter(MarkFeatureWriter): NamedAnchor = ContextuallyAwareNamedAnchor + def _getAnchor(self, glyphName, anchorName, anchor=None): + # the variable FEA aware method is defined with ufo2ft v3; make sure we don't + # fail but continue to work unchanged with older ufo2ft MarkFeatureWriter API. + try: + getter = super()._getAnchor + except AttributeError: + x = anchor.x + y = anchor.y + if hasattr(self.options, "quantization"): + x = quantize(x, self.options.quantization) + y = quantize(y, self.options.quantization) + return x, y + else: + return getter(glyphName, anchorName, anchor=anchor) + def _getAnchorLists(self): gdefClasses = self.context.gdefClasses if gdefClasses.base is not None: @@ -139,7 +155,7 @@ def _getAnchorLists(self): self.log.warning( "duplicate anchor '%s' in glyph '%s'", anchorName, glyphName ) - x, y = self._getAnchor(glyphName, anchorName) + x, y = self._getAnchor(glyphName, anchorName, anchor=anchor) libData = None if anchor.identifier: libData = glyph.lib[OBJECT_LIBS_KEY].get(anchor.identifier)