diff --git a/Lib/fontTools/cffLib/__init__.py b/Lib/fontTools/cffLib/__init__.py index d7cbae28d..754af803e 100644 --- a/Lib/fontTools/cffLib/__init__.py +++ b/Lib/fontTools/cffLib/__init__.py @@ -2320,20 +2320,19 @@ class TopDict(BaseDict): i = i + 1 def recalcFontBBox(self): - bounds = None + fontBBox = None for charString in self.CharStrings.values(): - if not hasattr(charString, 'bounds'): - charString.recalcBounds() - if charString.bounds is not None: - if bounds is not None: - bounds = unionRect(bounds, charString.bounds) + bounds = charString.calcBounds() + if bounds is not None: + if fontBBox is not None: + fontBBox = unionRect(fontBBox, bounds) else: - bounds = charString.bounds + fontBBox = bounds - if bounds is None: + if fontBBox is None: self.FontBBox = self.defaults['FontBBox'][:] else: - self.FontBBox = list(intRect(bounds)) + self.FontBBox = list(intRect(fontBBox)) class FontDict(BaseDict): diff --git a/Lib/fontTools/misc/psCharStrings.py b/Lib/fontTools/misc/psCharStrings.py index 2efe15083..bc6b20a34 100644 --- a/Lib/fontTools/misc/psCharStrings.py +++ b/Lib/fontTools/misc/psCharStrings.py @@ -978,10 +978,10 @@ class T2CharString(object): extractor.execute(self) self.width = extractor.width - def recalcBounds(self): + def calcBounds(self): boundsPen = BoundsPen(None) self.draw(boundsPen) - self.bounds = boundsPen.bounds + return boundsPen.bounds def check_program(self, program, isCFF2=False): if isCFF2: diff --git a/Lib/fontTools/ttLib/tables/_h_h_e_a.py b/Lib/fontTools/ttLib/tables/_h_h_e_a.py index 100da8147..a77592c14 100644 --- a/Lib/fontTools/ttLib/tables/_h_h_e_a.py +++ b/Lib/fontTools/ttLib/tables/_h_h_e_a.py @@ -65,11 +65,9 @@ class table__h_h_e_a(DefaultTable.DefaultTable): topDict = ttFont['CFF '].cff.topDictIndex[0] for name in ttFont.getGlyphOrder(): cs = topDict.CharStrings[name] - if not hasattr(cs, 'bounds'): - cs.recalcBounds() - if cs.bounds is None: - continue - boundsWidthDict[name] = math.ceil(cs.bounds[2]) - math.floor(cs.bounds[0]) + bounds = cs.calcBounds() + if bounds is not None: + boundsWidthDict[name] = math.ceil(bounds[2]) - math.floor(bounds[0]) if boundsWidthDict: minLeftSideBearing = float('inf') diff --git a/Lib/fontTools/ttLib/tables/_v_h_e_a.py b/Lib/fontTools/ttLib/tables/_v_h_e_a.py index c3de47117..502ac7300 100644 --- a/Lib/fontTools/ttLib/tables/_v_h_e_a.py +++ b/Lib/fontTools/ttLib/tables/_v_h_e_a.py @@ -64,11 +64,9 @@ class table__v_h_e_a(DefaultTable.DefaultTable): topDict = ttFont['CFF '].cff.topDictIndex[0] for name in ttFont.getGlyphOrder(): cs = topDict.CharStrings[name] - if not hasattr(cs, 'bounds'): - cs.recalcBounds() - if cs.bounds is None: - continue - boundsHeightDict[name] = math.ceil(cs.bounds[3]) - math.floor(cs.bounds[1]) + bounds = cs.calcBounds() + if bounds is not None: + boundsHeightDict[name] = math.ceil(bounds[3]) - math.floor(bounds[1]) if boundsHeightDict: minTopSideBearing = float('inf') diff --git a/Tests/misc/psCharStrings_test.py b/Tests/misc/psCharStrings_test.py index 826e370c6..552565b05 100644 --- a/Tests/misc/psCharStrings_test.py +++ b/Tests/misc/psCharStrings_test.py @@ -11,20 +11,20 @@ class T2CharStringTest(unittest.TestCase): def stringToT2CharString(cls, string): return T2CharString(program=stringToProgram(string), private=PrivateDict()) - def test_recalcBounds_empty(self): + def test_calcBounds_empty(self): cs = self.stringToT2CharString("endchar") - cs.recalcBounds() - self.assertEqual(cs.bounds, None) + bounds = cs.calcBounds() + self.assertEqual(bounds, None) - def test_recalcBounds_line(self): + def test_calcBounds_line(self): cs = self.stringToT2CharString("100 100 rmoveto 40 10 rlineto -20 50 rlineto endchar") - cs.recalcBounds() - self.assertEqual(cs.bounds, (100, 100, 140, 160)) + bounds = cs.calcBounds() + self.assertEqual(bounds, (100, 100, 140, 160)) - def test_recalcBounds_curve(self): + def test_calcBounds_curve(self): cs = self.stringToT2CharString("100 100 rmoveto -50 -150 200 0 -50 150 rrcurveto endchar") - cs.recalcBounds() - self.assertEqual(cs.bounds, (91.90524980688875, -12.5, 208.09475019311125, 100)) + bounds = cs.calcBounds() + self.assertEqual(bounds, (91.90524980688875, -12.5, 208.09475019311125, 100)) if __name__ == "__main__":