Repository: googlefonts/glyphsLib
Pull request #649 (2020-12-07 14:33:36)

ufo2glyphs: preserve DS axis order
Previously, wght and wdth would be forcibly be made first.


Patch:
diff --git a/Lib/glyphsLib/builder/axes.py b/Lib/glyphsLib/builder/axes.py
index 4e82077b..849df8de 100644
--- a/Lib/glyphsLib/builder/axes.py
+++ b/Lib/glyphsLib/builder/axes.py
@@ -269,29 +269,14 @@ def font_uses_axis_locations(font):
 
 
 def to_glyphs_axes(self):
-    weight = None
-    width = None
-    customs = []
+    axes_parameter = []
     for axis in self.designspace.axes:
         if axis.tag == "wght":
-            weight = axis
+            axes_parameter.append({"Name": axis.name or "Weight", "Tag": "wght"})
         elif axis.tag == "wdth":
-            width = axis
+            axes_parameter.append({"Name": axis.name or "Width", "Tag": "wdth"})
         else:
-            customs.append(axis)
-
-    axes_parameter = []
-    if weight is not None:
-        axes_parameter.append({"Name": weight.name or "Weight", "Tag": "wght"})
-        # TODO: (jany) store other data about this axis?
-
-    if width is not None:
-        axes_parameter.append({"Name": width.name or "Width", "Tag": "wdth"})
-        # TODO: (jany) store other data about this axis?
-
-    for custom in customs:
-        axes_parameter.append({"Name": custom.name, "Tag": custom.tag})
-        # TODO: (jany) store other data about this axis?
+            axes_parameter.append({"Name": axis.name, "Tag": axis.tag})
 
     if axes_parameter and not _is_subset_of_default_axes(axes_parameter):
         self.font.customParameters["Axes"] = axes_parameter


Test patch:
diff --git a/tests/builder/axes_test.py b/tests/builder/axes_test.py
index a3f49ccf..749ef089 100644
--- a/tests/builder/axes_test.py
+++ b/tests/builder/axes_test.py
@@ -21,7 +21,7 @@ import pytest
 from fontTools import designspaceLib
 from glyphsLib import to_glyphs, to_designspace, to_ufos
 from glyphsLib.classes import GSFont, GSFontMaster
-from glyphsLib.builder.axes import get_regular_master
+from glyphsLib.builder.axes import _is_subset_of_default_axes, get_regular_master
 
 """
 Goal: check how files with custom axes are roundtripped.
@@ -47,6 +47,10 @@ Goal: check how files with custom axes are roundtripped.
             ("IJKL", "Third custom"),
             ("MNOP", "Fourth custom"),
         ],
+        [("opsz", "First custom"), ("wght", "Second custom")],
+        # Test that standard axis definitions don't generate an Axes custom parameter.
+        [("wght", "Weight"), ("wdth", "Width")],
+        [("wdth", "Width"), ("wght", "Weight")],
     ],
 )
 def test_weight_width_custom(axes, ufo_module):
@@ -57,9 +61,12 @@ def test_weight_width_custom(axes, ufo_module):
 
     font = to_glyphs(doc)
 
-    assert font.customParameters["Axes"] == [
-        {"Tag": tag, "Name": name} for tag, name in axes
-    ]
+    if _is_subset_of_default_axes([{"Name": n, "Tag": t} for t, n in axes]):
+        assert font.customParameters["Axes"] is None
+    else:
+        assert font.customParameters["Axes"] == [
+            {"Tag": tag, "Name": name} for tag, name in axes
+        ]
 
     doc = to_designspace(font, ufo_module=ufo_module)