Repository: fonttools/fonttools

Issue:
Bad font data after instantiating variable font
I'm coming across bad font data when using fontTools pens to interact with instantiated variable fonts. The problem can be reproduced in every contour in every Quadratic variable font I've tried.

When reading the point data with a pen, the point order is offset by one (which in this case I don't mind) and the last two `lineTo` points are joined together into a `qCurve` segment (which distorts the shape). The problem happens in both the `instancer` and `mutator` variations of the `instantiateVariableFont` function.

However, the font data in the TTF looks fine to me when I save it to a file and open it in an editor or read through a TTX dump.

This demo just happens to use Monserrat (to make sure that the problem wasn't in my own font) and I set the location to the default weight to make the output comparison more clear, but the problem happens at any location (and with any Quadratic TTF)

Can anyone think of what I might be doing wrong, or where the problem might be coming from?


```python
from fontTools.varLib import mutator, instancer
from fontTools.ttLib import TTFont
from fontTools.pens.recordingPen import RecordingPen


def printReport(f):
    g = f.getGlyphSet()["I"]
    pen = RecordingPen()
    g.draw(pen)
    for i in pen.value:
        print(i)  

fontPath = "Montserrat-VF.ttf"
location = {"wght":100}

print("Default:")
f = TTFont(fontPath)
printReport(f)

print("\nmutator.instantiateVariableFont")
instM = mutator.instantiateVariableFont(f, location)
#instM.save("mutator.ttf")
printReport(instM)

print("\ninstancer.instantiateVariableFont")
instI = instancer.instantiateVariableFont(f, location)
#instI.save("instancer.ttf")
printReport(instI)
```

Output:

```
Default:
('moveTo', ((133, 700),))
('lineTo', ((153, 700),))
('lineTo', ((153, 0),))
('lineTo', ((133, 0),))
('lineTo', ((133, 700),))
('closePath', ())

mutator.instantiateVariableFont
('moveTo', ((153, 700),))
('lineTo', ((153, 0),))
('lineTo', ((133, 0),))
('qCurveTo', ((133, 700), (153, 700)))
('closePath', ())

instancer.instantiateVariableFont
('moveTo', ((153, 700),))
('lineTo', ((153, 0),))
('lineTo', ((133, 0),))
('qCurveTo', ((133, 700), (153, 700)))
('closePath', ())
```


Discussion:
It's a regression that was introduced in fonttools 3.39.0. I'll try to dig deeper.

Resolving patch:
diff --git a/Lib/fontTools/ttLib/tables/_g_l_y_f.py b/Lib/fontTools/ttLib/tables/_g_l_y_f.py
--- a/Lib/fontTools/ttLib/tables/_g_l_y_f.py
+++ b/Lib/fontTools/ttLib/tables/_g_l_y_f.py
@@ -1179,7 +1179,7 @@ def draw(self, pen, glyfTable, offset=0):
 		for end in endPts:
 			end = end + 1
 			contour = coordinates[start:end]
-			cFlags = flags[start:end]
+			cFlags = [flagOnCurve & f for f in flags[start:end]]
 			start = end
 			if 1 not in cFlags:
 				# There is not a single on-curve point on the curve,


Test patch:
diff --git a/Tests/ttLib/tables/_g_l_y_f_test.py b/Tests/ttLib/tables/_g_l_y_f_test.py
--- a/Tests/ttLib/tables/_g_l_y_f_test.py
+++ b/Tests/ttLib/tables/_g_l_y_f_test.py
@@ -2,6 +2,7 @@
 from fontTools.misc.fixedTools import otRound
 from fontTools.misc.testTools import getXML, parseXML
 from fontTools.pens.ttGlyphPen import TTGlyphPen
+from fontTools.pens.recordingPen import RecordingPen
 from fontTools.ttLib import TTFont, newTable, TTLibError
 from fontTools.ttLib.tables._g_l_y_f import (
     GlyphCoordinates,
@@ -284,6 +285,32 @@ def test_trim_remove_hinting_composite_glyph(self):
 
         composite.compact(glyfTable)
 
+    def test_bit6_draw_to_pen_issue1771(self):
+        # https://github.com/fonttools/fonttools/issues/1771
+        font = TTFont(sfntVersion="\x00\x01\x00\x00")
+        # glyph00003 contains a bit 6 flag on the first point,
+        # which triggered the issue
+        font.importXML(GLYF_TTX)
+        glyfTable = font['glyf']
+        pen = RecordingPen()
+        glyfTable["glyph00003"].draw(pen, glyfTable=glyfTable)
+        expected = [('moveTo', ((501, 1430),)),
+                    ('lineTo', ((683, 1430),)),
+                    ('lineTo', ((1172, 0),)),
+                    ('lineTo', ((983, 0),)),
+                    ('lineTo', ((591, 1193),)),
+                    ('lineTo', ((199, 0),)),
+                    ('lineTo', ((12, 0),)),
+                    ('lineTo', ((501, 1430),)),
+                    ('closePath', ()),
+                    ('moveTo', ((249, 514),)),
+                    ('lineTo', ((935, 514),)),
+                    ('lineTo', ((935, 352),)),
+                    ('lineTo', ((249, 352),)),
+                    ('lineTo', ((249, 514),)),
+                    ('closePath', ())]
+        self.assertEqual(pen.value, expected)
+
 
 class GlyphComponentTest: