From c4899330c552fb9a7db0d05a50074004d6fa2805 Mon Sep 17 00:00:00 2001 From: Nikolaus Waxweiler Date: Sun, 10 Mar 2019 22:12:06 +0000 Subject: [PATCH] designspaceLib, findDefault: consider axis mapping --- Lib/fontTools/designspaceLib/__init__.py | 21 +++++++-- Tests/designspaceLib/designspace_test.py | 58 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/Lib/fontTools/designspaceLib/__init__.py b/Lib/fontTools/designspaceLib/__init__.py index 6490a47c3..02cc05811 100644 --- a/Lib/fontTools/designspaceLib/__init__.py +++ b/Lib/fontTools/designspaceLib/__init__.py @@ -1176,15 +1176,26 @@ class DesignSpaceDocument(LogMixin, AsDictMixin): return None def findDefault(self): - # new default finder - # take the sourcedescriptor with the location at all the defaults - # if we can't find it, return None, let someone else figure it out + """Set and return SourceDescriptor at the default location or None. + + The default location is the set of all `default` values in user space + of all axes. + """ self.default = None + + # Convert the default location from user space to design space before comparing + # it against the SourceDescriptor locations (always in design space). + # Note: given no map, piecewiseLinearMap will simply return the value. + default_location_design = { + axis.name: axis.map_forward(self.defaultLoc[axis.name]) + for axis in self.axes + } + for sourceDescriptor in self.sources: - if sourceDescriptor.location == self.defaultLoc: - # we choose you! + if sourceDescriptor.location == default_location_design: self.default = sourceDescriptor return sourceDescriptor + return None def normalizeLocation(self, location): diff --git a/Tests/designspaceLib/designspace_test.py b/Tests/designspaceLib/designspace_test.py index ffa5a69b1..30bbfa0c5 100644 --- a/Tests/designspaceLib/designspace_test.py +++ b/Tests/designspaceLib/designspace_test.py @@ -847,3 +847,61 @@ def test_with_with_path_object(tmpdir): doc = DesignSpaceDocument() doc.write(dest) assert dest.exists() + + +def test_findDefault_axis_mapping(): + designspace_string = """\ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + """ + designspace = DesignSpaceDocument.fromstring(designspace_string) + assert designspace.findDefault().filename == "Font-Italic.ufo"