From 373105640163da86bfc602bea409e430fab9890f Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 21 Apr 2022 18:44:44 +0100 Subject: [PATCH] [OS/2] define recalcXAvgCharWidth method copied from current subsetter code (matches ufo2ft's outlineCompiler) --- Lib/fontTools/ttLib/tables/O_S_2f_2.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/fontTools/ttLib/tables/O_S_2f_2.py b/Lib/fontTools/ttLib/tables/O_S_2f_2.py index a57652246..8de984214 100644 --- a/Lib/fontTools/ttLib/tables/O_S_2f_2.py +++ b/Lib/fontTools/ttLib/tables/O_S_2f_2.py @@ -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