Repository: fonttools/fonttools

Issue:
[instancer] OS/2 xAvgCharWidth not recalculated
```
$ fontbakery check-googlefonts -c com.google.fonts/check/xavgcharwidth fonts/variable/QLDSchoolHandCursive\[wght].ttf
[P] 100%

$ fonttools varLib.instancer fonts/variable/QLDSchoolHandCursive\[wght].ttf wght=700 --update-name-table -o QLDSchoolHandCursive-Bold.ttf
Restricting axes: {'wght': (700, 700, 700)}
Loading variable font
Restricted limits: {'wght': (700, 700, 700)}
Normalized limits: {'wght': (1, 1, 1, 0, 300)}
Updating name table
Instantiating glyf/gvar tables
Dropping HVAR table
Dropping VVAR table
Instantiating GDEF and GPOS tables
Dropping avar table
Instantiating STAT table
Dropping fvar table
Pruning name table
Setting OS/2.usWeightClass = 700
Saving instance font QLDSchoolHandCursive-Bold.ttf

$ fontbakery check-googlefonts -c com.google.fonts/check/xavgcharwidth QLDSchoolHandCursive-Bold.ttf
Start ... running 1 individual check executions.
 >> Check:   com.google.fonts/check/xavgcharwidth
    Desc:    Check if OS/2 xAvgCharWidth is correct.
    Files:   QLDSchoolHandCursive-Bold.ttf

     WARN OS/2 xAvgCharWidth is 1200 but it should be 1283
          which corresponds to the average of the widths of all glyphs in the
          font. [code: xAvgCharWidth-wrong]
    Result: WARN
```


Discussion:

Resolving patch:
diff --git a/Lib/fontTools/varLib/instancer/__init__.py b/Lib/fontTools/varLib/instancer/__init__.py
--- a/Lib/fontTools/varLib/instancer/__init__.py
+++ b/Lib/fontTools/varLib/instancer/__init__.py
@@ -1273,6 +1273,9 @@ def instantiateVariableFont(
                     ignoreErrors=(overlap == OverlapMode.REMOVE_AND_IGNORE_ERRORS),
                 )
 
+    if "OS/2" in varfont:
+        varfont["OS/2"].recalcAvgCharWidth(varfont)
+
     varLib.set_default_weight_width_slant(
         varfont, location=axisLimits.defaultLocation()
     )


Test patch:
diff --git a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,100.ttx b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,100.ttx
--- a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,100.ttx
+++ b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,100.ttx
@@ -74,7 +74,7 @@
     <!-- The fields 'usFirstCharIndex' and 'usLastCharIndex'
          will be recalculated by the compiler -->
     <version value="4"/>
-    <xAvgCharWidth value="577"/>
+    <xAvgCharWidth value="502"/>
     <usWeightClass value="100"/>
     <usWidthClass value="5"/>
     <fsType value="00000000 00000000"/>
diff --git a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,62.5.ttx b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,62.5.ttx
--- a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,62.5.ttx
+++ b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-100,62.5.ttx
@@ -74,7 +74,7 @@
     <!-- The fields 'usFirstCharIndex' and 'usLastCharIndex'
          will be recalculated by the compiler -->
     <version value="4"/>
-    <xAvgCharWidth value="577"/>
+    <xAvgCharWidth value="383"/>
     <usWeightClass value="100"/>
     <usWidthClass value="2"/>
     <fsType value="00000000 00000000"/>
diff --git a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,100.ttx b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,100.ttx
--- a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,100.ttx
+++ b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,100.ttx
@@ -74,7 +74,7 @@
     <!-- The fields 'usFirstCharIndex' and 'usLastCharIndex'
          will be recalculated by the compiler -->
     <version value="4"/>
-    <xAvgCharWidth value="577"/>
+    <xAvgCharWidth value="543"/>
     <usWeightClass value="400"/>
     <usWidthClass value="5"/>
     <fsType value="00000000 00000000"/>
diff --git a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,62.5.ttx b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,62.5.ttx
--- a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,62.5.ttx
+++ b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-400,62.5.ttx
@@ -74,7 +74,7 @@
     <!-- The fields 'usFirstCharIndex' and 'usLastCharIndex'
          will be recalculated by the compiler -->
     <version value="4"/>
-    <xAvgCharWidth value="577"/>
+    <xAvgCharWidth value="428"/>
     <usWeightClass value="400"/>
     <usWidthClass value="2"/>
     <fsType value="00000000 00000000"/>
diff --git a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,100.ttx b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,100.ttx
--- a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,100.ttx
+++ b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,100.ttx
@@ -74,7 +74,7 @@
     <!-- The fields 'usFirstCharIndex' and 'usLastCharIndex'
          will be recalculated by the compiler -->
     <version value="4"/>
-    <xAvgCharWidth value="577"/>
+    <xAvgCharWidth value="609"/>
     <usWeightClass value="900"/>
     <usWidthClass value="5"/>
     <fsType value="00000000 00000000"/>
diff --git a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,62.5.ttx b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,62.5.ttx
--- a/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,62.5.ttx
+++ b/Tests/varLib/instancer/data/test_results/PartialInstancerTest2-VF-instance-900,62.5.ttx
@@ -74,7 +74,7 @@
     <!-- The fields 'usFirstCharIndex' and 'usLastCharIndex'
          will be recalculated by the compiler -->
     <version value="4"/>
-    <xAvgCharWidth value="577"/>
+    <xAvgCharWidth value="506"/>
     <usWeightClass value="900"/>
     <usWidthClass value="2"/>
     <fsType value="00000000 00000000"/>