[OS/2] define recalcXAvgCharWidth method

copied from current subsetter code (matches ufo2ft's outlineCompiler)
This commit is contained in:
Cosimo Lupo 2022-04-21 18:44:44 +01:00
parent 380f8d573d
commit 3731056401

View File

@ -1,4 +1,5 @@
from fontTools.misc import sstruct
from fontTools.misc.roundTools import otRound
from fontTools.misc.textTools import safeEval, num2binary, binary2num
from fontTools.ttLib.tables import DefaultTable
import bisect
@ -299,6 +300,19 @@ class table_O_S_2f_2(DefaultTable.DefaultTable):
self.setUnicodeRanges(bits)
return bits
def recalcXAvgCharWidth(self, ttFont):
"""Recalculate xAvgCharWidth using metrics from ttFont's 'hmtx' table.
Set it to 0 if the unlikely event 'hmtx' table is not found.
"""
avg_width = 0
hmtx = ttFont.get("hmtx")
if hmtx:
widths = [m[0] for m in hmtx.metrics.values() if m[0] > 0]
avg_width = otRound(sum(widths) / len(widths))
self.xAvgCharWidth = avg_width
return avg_width
# Unicode ranges data from the OpenType OS/2 table specification v1.7