[CFF2] Make recalcBBoxes work with CFF2 table

Add checks for CFF2 table next to CFF checks as applicable.
This commit is contained in:
Khaled Hosny 2020-01-31 04:13:48 +02:00
parent 366c08a9d6
commit 09174b9129
4 changed files with 36 additions and 17 deletions

View File

@ -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)

View File

@ -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]

View File

@ -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]

View File

@ -19,10 +19,10 @@
<unitsPerEm value="1000"/>
<created value="Wed Mar 27 00:23:21 2019"/>
<modified value="Wed Mar 27 00:23:21 2019"/>
<xMin value="0"/>
<yMin value="0"/>
<xMax value="0"/>
<yMax value="0"/>
<xMin value="100"/>
<yMin value="100"/>
<xMax value="600"/>
<yMax value="1000"/>
<macStyle value="00000000 00000000"/>
<lowestRecPPEM value="3"/>
<fontDirectionHint value="2"/>
@ -35,10 +35,10 @@
<ascent value="824"/>
<descent value="200"/>
<lineGap value="0"/>
<advanceWidthMax value="0"/>
<advanceWidthMax value="600"/>
<minLeftSideBearing value="0"/>
<minRightSideBearing value="0"/>
<xMaxExtent value="0"/>
<minRightSideBearing value="200"/>
<xMaxExtent value="400"/>
<caretSlopeRise value="1"/>
<caretSlopeRun value="0"/>
<caretOffset value="0"/>