[cff] Fix calcBound when seac-like components happen in endchar

I'm also unimpressed by the copy-pasted bounds logic in hhea and vhea,
and the fact that that's coded in there instead of calling a function
on CFF / glyf tables respectively.
This commit is contained in:
Behdad Esfahbod 2018-01-26 16:53:04 -08:00
parent c99a731adb
commit 4fec016862
5 changed files with 12 additions and 10 deletions

View File

@ -2327,7 +2327,7 @@ class TopDict(BaseDict):
def recalcFontBBox(self):
fontBBox = None
for charString in self.CharStrings.values():
bounds = charString.calcBounds()
bounds = charString.calcBounds(self.CharStrings)
if bounds is not None:
if fontBBox is not None:
fontBBox = unionRect(fontBBox, bounds)

View File

@ -979,8 +979,8 @@ class T2CharString(object):
extractor.execute(self)
self.width = extractor.width
def calcBounds(self):
boundsPen = BoundsPen(None)
def calcBounds(self, glyphSet):
boundsPen = BoundsPen(glyphSet)
self.draw(boundsPen)
return boundsPen.bounds

View File

@ -64,9 +64,10 @@ class table__h_h_e_a(DefaultTable.DefaultTable):
boundsWidthDict[name] = g.xMax - g.xMin
elif 'CFF ' in ttFont:
topDict = ttFont['CFF '].cff.topDictIndex[0]
charStrings = topDict.CharStrings
for name in ttFont.getGlyphOrder():
cs = topDict.CharStrings[name]
bounds = cs.calcBounds()
cs = charStrings[name]
bounds = cs.calcBounds(charStrings)
if bounds is not None:
boundsWidthDict[name] = int(
math.ceil(bounds[2]) - math.floor(bounds[0]))

View File

@ -63,9 +63,10 @@ class table__v_h_e_a(DefaultTable.DefaultTable):
boundsHeightDict[name] = g.yMax - g.yMin
elif 'CFF ' in ttFont:
topDict = ttFont['CFF '].cff.topDictIndex[0]
charStrings = topDict.CharStrings
for name in ttFont.getGlyphOrder():
cs = topDict.CharStrings[name]
bounds = cs.calcBounds()
cs = charStrings[name]
bounds = cs.calcBounds(charStrings)
if bounds is not None:
boundsHeightDict[name] = int(
math.ceil(bounds[3]) - math.floor(bounds[1]))

View File

@ -13,17 +13,17 @@ class T2CharStringTest(unittest.TestCase):
def test_calcBounds_empty(self):
cs = self.stringToT2CharString("endchar")
bounds = cs.calcBounds()
bounds = cs.calcBounds(None)
self.assertEqual(bounds, None)
def test_calcBounds_line(self):
cs = self.stringToT2CharString("100 100 rmoveto 40 10 rlineto -20 50 rlineto endchar")
bounds = cs.calcBounds()
bounds = cs.calcBounds(None)
self.assertEqual(bounds, (100, 100, 140, 160))
def test_calcBounds_curve(self):
cs = self.stringToT2CharString("100 100 rmoveto -50 -150 200 0 -50 150 rrcurveto endchar")
bounds = cs.calcBounds()
bounds = cs.calcBounds(None)
self.assertEqual(bounds, (91.90524980688875, -12.5, 208.09475019311125, 100))