Repository: googlefonts/ufo2ft
Pull request #374 (2020-03-30 15:57:44)

Reverse generated .notdef for TrueType fonts


Patch:
diff --git a/Lib/ufo2ft/outlineCompiler.py b/Lib/ufo2ft/outlineCompiler.py
index 4d0e90e..56a759f 100644
--- a/Lib/ufo2ft/outlineCompiler.py
+++ b/Lib/ufo2ft/outlineCompiler.py
@@ -17,6 +17,7 @@ from fontTools.cffLib import (
     PrivateDict,
     IndexedStrings,
 )
+from fontTools.pens.reverseContourPen import ReverseContourPen
 from fontTools.pens.boundsPen import ControlBoundsPen
 from fontTools.pens.t2CharStringPen import T2CharStringPen
 from fontTools.pens.ttGlyphPen import TTGlyphPen
@@ -80,7 +81,7 @@ class BaseOutlineCompiler(object):
         # use the previously filtered glyphSet, if any
         if glyphSet is None:
             glyphSet = {g.name: g for g in font}
-        self.makeMissingRequiredGlyphs(font, glyphSet)
+        self.makeMissingRequiredGlyphs(font, glyphSet, self.sfntVersion)
         self.allGlyphs = glyphSet
         # store the glyph order
         if glyphOrder is None:
@@ -207,7 +208,7 @@ class BaseOutlineCompiler(object):
         return makeUnicodeToGlyphNameMapping(self.allGlyphs, self.glyphOrder)
 
     @staticmethod
-    def makeMissingRequiredGlyphs(font, glyphSet):
+    def makeMissingRequiredGlyphs(font, glyphSet, sfntVersion):
         """
         Add .notdef to the glyph set if it is not present.
 
@@ -228,6 +229,7 @@ class BaseOutlineCompiler(object):
             unitsPerEm=unitsPerEm,
             ascender=ascender,
             descender=descender,
+            reverseContour=(sfntVersion == "\000\001\000\000")
         )
 
     def makeOfficialGlyphOrder(self, glyphOrder):
@@ -1394,7 +1396,16 @@ class StubGlyph(object):
     (specifically .notdef) in the provided UFO.
     """
 
-    def __init__(self, name, width, unitsPerEm, ascender, descender, unicodes=[]):
+    def __init__(
+        self,
+        name,
+        width,
+        unitsPerEm,
+        ascender,
+        descender,
+        unicodes=[],
+        reverseContour=False,
+    ):
         self.name = name
         self.width = width
         self.unitsPerEm = unitsPerEm
@@ -1409,6 +1420,7 @@ class StubGlyph(object):
             self.unicode = None
         if name == ".notdef":
             self.draw = self._drawDefaultNotdef
+        self.reverseContour = reverseContour
 
     def __len__(self):
         if self.name == ".notdef":
@@ -1423,6 +1435,10 @@ class StubGlyph(object):
         pass
 
     def _drawDefaultNotdef(self, pen):
+        # Draw contour in PostScript direction (counter-clockwise) by default. Reverse
+        # for TrueType.
+        if self.reverseContour:
+            pen = ReverseContourPen(pen)
         width = otRound(self.unitsPerEm * 0.5)
         stroke = otRound(self.unitsPerEm * 0.05)
         ascender = self.ascender


Test patch:
diff --git a/tests/data/TestVariableFont-TTF-useProductionNames.ttx b/tests/data/TestVariableFont-TTF-useProductionNames.ttx
index 622eefa..2c6a8c3 100644
--- a/tests/data/TestVariableFont-TTF-useProductionNames.ttx
+++ b/tests/data/TestVariableFont-TTF-useProductionNames.ttx
@@ -163,15 +163,15 @@
     <TTGlyph name=".notdef" xMin="50" yMin="-250" xMax="450" yMax="750">
       <contour>
         <pt x="50" y="-250" on="1"/>
-        <pt x="450" y="-250" on="1"/>
-        <pt x="450" y="750" on="1"/>
         <pt x="50" y="750" on="1"/>
+        <pt x="450" y="750" on="1"/>
+        <pt x="450" y="-250" on="1"/>
       </contour>
       <contour>
         <pt x="100" y="-200" on="1"/>
-        <pt x="100" y="700" on="1"/>
-        <pt x="400" y="700" on="1"/>
         <pt x="400" y="-200" on="1"/>
+        <pt x="400" y="700" on="1"/>
+        <pt x="100" y="700" on="1"/>
       </contour>
       <instructions/>
     </TTGlyph>
diff --git a/tests/data/TestVariableFont-TTF.ttx b/tests/data/TestVariableFont-TTF.ttx
index 997c0e0..25ae161 100644
--- a/tests/data/TestVariableFont-TTF.ttx
+++ b/tests/data/TestVariableFont-TTF.ttx
@@ -163,15 +163,15 @@
     <TTGlyph name=".notdef" xMin="50" yMin="-250" xMax="450" yMax="750">
       <contour>
         <pt x="50" y="-250" on="1"/>
-        <pt x="450" y="-250" on="1"/>
-        <pt x="450" y="750" on="1"/>
         <pt x="50" y="750" on="1"/>
+        <pt x="450" y="750" on="1"/>
+        <pt x="450" y="-250" on="1"/>
       </contour>
       <contour>
         <pt x="100" y="-200" on="1"/>
-        <pt x="100" y="700" on="1"/>
-        <pt x="400" y="700" on="1"/>
         <pt x="400" y="-200" on="1"/>
+        <pt x="400" y="700" on="1"/>
+        <pt x="100" y="700" on="1"/>
       </contour>
       <instructions/>
     </TTGlyph>