From 169731c7f5d96e96d531d35db477af5e727fe945 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Sat, 9 Apr 2022 14:50:59 +0200 Subject: [PATCH] [CFF/T2] Ensure that pen.closePath() gets called for CFF2 charstrings (#2577) * [CFF/T2] Make sure to call pen.closePath() at the end of a CFF2/T2 charstring. Fixes #2455 * Add test case to verify pen.closePath() behavior --- Lib/fontTools/misc/psCharStrings.py | 1 + Tests/misc/psCharStrings_test.py | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/Lib/fontTools/misc/psCharStrings.py b/Lib/fontTools/misc/psCharStrings.py index 29c2d3658..6014abc29 100644 --- a/Lib/fontTools/misc/psCharStrings.py +++ b/Lib/fontTools/misc/psCharStrings.py @@ -980,6 +980,7 @@ class T2CharString(object): self.private.nominalWidthX, self.private.defaultWidthX, self.private) extractor.execute(self) + extractor.endPath() self.width = extractor.width def calcBounds(self, glyphSet): diff --git a/Tests/misc/psCharStrings_test.py b/Tests/misc/psCharStrings_test.py index 47ff4fda7..5e36fe73c 100644 --- a/Tests/misc/psCharStrings_test.py +++ b/Tests/misc/psCharStrings_test.py @@ -8,6 +8,7 @@ from fontTools.misc.psCharStrings import ( read_fixed1616, read_realNumber, ) +from fontTools.pens.recordingPen import RecordingPen import unittest @@ -158,6 +159,14 @@ class T2CharStringTest(unittest.TestCase): self.assertNotIsInstance(expected_arg, str) self.assertAlmostEqual(arg, expected_arg) + def test_pen_closePath(self): + # Test CFF2/T2 charstring: it does NOT end in "endchar" + # https://github.com/fonttools/fonttools/issues/2455 + cs = self.stringToT2CharString("100 100 rmoveto -50 -150 200 0 -50 150 rrcurveto") + pen = RecordingPen() + cs.draw(pen) + self.assertEqual(pen.value[-1], ('closePath', ())) + if __name__ == "__main__": import sys