From 09174b91292f7ad56c75c4540c83cac4512182ce Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Fri, 31 Jan 2020 04:13:48 +0200 Subject: [PATCH] [CFF2] Make recalcBBoxes work with CFF2 table Add checks for CFF2 table next to CFF checks as applicable. --- Lib/fontTools/ttLib/tables/_h_e_a_d.py | 17 +++++++++++++++-- Lib/fontTools/ttLib/tables/_h_h_e_a.py | 11 +++++++---- Lib/fontTools/ttLib/tables/_v_h_e_a.py | 11 +++++++---- Tests/fontBuilder/data/test_var.otf.ttx | 14 +++++++------- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Lib/fontTools/ttLib/tables/_h_e_a_d.py b/Lib/fontTools/ttLib/tables/_h_e_a_d.py index 5afc925b1..cef0bf180 100644 --- a/Lib/fontTools/ttLib/tables/_h_e_a_d.py +++ b/Lib/fontTools/ttLib/tables/_h_e_a_d.py @@ -4,7 +4,7 @@ from fontTools.misc.fixedTools import floatToFixedToStr, strToFixedToFloat from fontTools.misc.textTools import safeEval, num2binary, binary2num from fontTools.misc.timeTools import timestampFromString, timestampToString, timestampNow from fontTools.misc.timeTools import epoch_diff as mac_epoch_diff # For backward compat -from fontTools.misc.arrayTools import intRect +from fontTools.misc.arrayTools import intRect, unionRect from . import DefaultTable import logging @@ -34,7 +34,7 @@ headFormat = """ class table__h_e_a_d(DefaultTable.DefaultTable): - dependencies = ['maxp', 'loca', 'CFF '] + dependencies = ['maxp', 'loca', 'CFF ', 'CFF2'] def decompile(self, data, ttFont): dummy, rest = sstruct.unpack2(headFormat, data, self) @@ -65,6 +65,19 @@ class table__h_e_a_d(DefaultTable.DefaultTable): if 'CFF ' in ttFont: topDict = ttFont['CFF '].cff.topDictIndex[0] self.xMin, self.yMin, self.xMax, self.yMax = intRect(topDict.FontBBox) + elif 'CFF2' in ttFont: + topDict = ttFont['CFF2'].cff.topDictIndex[0] + charStrings = topDict.CharStrings + fontBBox = None + for charString in charStrings.values(): + bounds = charString.calcBounds(charStrings) + if bounds is not None: + if fontBBox is not None: + fontBBox = unionRect(fontBBox, bounds) + else: + fontBBox = bounds + if fontBBox is not None: + self.xMin, self.yMin, self.xMax, self.yMax = intRect(fontBBox) if ttFont.recalcTimestamp: self.modified = timestampNow() data = sstruct.pack(headFormat, self) diff --git a/Lib/fontTools/ttLib/tables/_h_h_e_a.py b/Lib/fontTools/ttLib/tables/_h_h_e_a.py index 0f5ec51d9..4d93b28ab 100644 --- a/Lib/fontTools/ttLib/tables/_h_h_e_a.py +++ b/Lib/fontTools/ttLib/tables/_h_h_e_a.py @@ -33,7 +33,7 @@ class table__h_h_e_a(DefaultTable.DefaultTable): # Note: Keep in sync with table__v_h_e_a - dependencies = ['hmtx', 'glyf', 'CFF '] + dependencies = ['hmtx', 'glyf', 'CFF ', 'CFF2'] # OpenType spec renamed these, add aliases for compatibility @property @@ -52,7 +52,7 @@ class table__h_h_e_a(DefaultTable.DefaultTable): sstruct.unpack(hheaFormat, data, self) def compile(self, ttFont): - if ttFont.recalcBBoxes and (ttFont.isLoaded('glyf') or ttFont.isLoaded('CFF ')): + if ttFont.recalcBBoxes and (ttFont.isLoaded('glyf') or ttFont.isLoaded('CFF ') or ttFont.isLoaded('CFF2')): self.recalc(ttFont) self.tableVersion = fi2ve(self.tableVersion) return sstruct.pack(hheaFormat, self) @@ -74,8 +74,11 @@ class table__h_h_e_a(DefaultTable.DefaultTable): # Calculate those. g.recalcBounds(glyfTable) boundsWidthDict[name] = g.xMax - g.xMin - elif 'CFF ' in ttFont: - topDict = ttFont['CFF '].cff.topDictIndex[0] + elif 'CFF ' in ttFont or 'CFF2' in ttFont: + if 'CFF ' in ttFont: + topDict = ttFont['CFF '].cff.topDictIndex[0] + else: + topDict = ttFont['CFF2'].cff.topDictIndex[0] charStrings = topDict.CharStrings for name in ttFont.getGlyphOrder(): cs = charStrings[name] diff --git a/Lib/fontTools/ttLib/tables/_v_h_e_a.py b/Lib/fontTools/ttLib/tables/_v_h_e_a.py index 2fc4b013e..55ba45a1d 100644 --- a/Lib/fontTools/ttLib/tables/_v_h_e_a.py +++ b/Lib/fontTools/ttLib/tables/_v_h_e_a.py @@ -32,13 +32,13 @@ class table__v_h_e_a(DefaultTable.DefaultTable): # Note: Keep in sync with table__h_h_e_a - dependencies = ['vmtx', 'glyf', 'CFF '] + dependencies = ['vmtx', 'glyf', 'CFF ', 'CFF2'] def decompile(self, data, ttFont): sstruct.unpack(vheaFormat, data, self) def compile(self, ttFont): - if ttFont.recalcBBoxes and (ttFont.isLoaded('glyf') or ttFont.isLoaded('CFF ')): + if ttFont.recalcBBoxes and (ttFont.isLoaded('glyf') or ttFont.isLoaded('CFF ') or ttFont.isLoaded('CFF2')): self.recalc(ttFont) self.tableVersion = fi2ve(self.tableVersion) return sstruct.pack(vheaFormat, self) @@ -60,8 +60,11 @@ class table__v_h_e_a(DefaultTable.DefaultTable): # Calculate those. g.recalcBounds(glyfTable) boundsHeightDict[name] = g.yMax - g.yMin - elif 'CFF ' in ttFont: - topDict = ttFont['CFF '].cff.topDictIndex[0] + elif 'CFF ' in ttFont or 'CFF2' in ttFont: + if 'CFF ' in ttFont: + topDict = ttFont['CFF '].cff.topDictIndex[0] + else: + topDict = ttFont['CFF2'].cff.topDictIndex[0] charStrings = topDict.CharStrings for name in ttFont.getGlyphOrder(): cs = charStrings[name] diff --git a/Tests/fontBuilder/data/test_var.otf.ttx b/Tests/fontBuilder/data/test_var.otf.ttx index e00d33c59..ccf64dc62 100644 --- a/Tests/fontBuilder/data/test_var.otf.ttx +++ b/Tests/fontBuilder/data/test_var.otf.ttx @@ -19,10 +19,10 @@ - - - - + + + + @@ -35,10 +35,10 @@ - + - - + +