Repository: fonttools/fonttools

Issue:
AttributeError: FeatureIndex
I'm trying to `addFeatureVariations` to a VF.ttf generated by fontmake ([here is the .glyphs file](https://raw.githubusercontent.com/weiweihuanghuang/Work-Sans/master/sources/WorkSans.glyphs)) using `fontmake -o variable -g WorkSans.glyphs`

Here is the script I run on the VF.ttf output
[swapBracketTrick.py.zip](https://github.com/fonttools/fonttools/files/2559662/swapBracketTrick.py.zip)

This is the error I get:
```
Traceback (most recent call last):
  File "swapBracketTrick.py", line 19, in <module>
    addFeatureVariations(f, condSubst)
  File "/usr/local/lib/python2.7/site-packages/fontTools/varLib/featureVars.py", line 65, in addFeatureVariations
    addFeatureVariationsRaw(font, explodedConditionalSubstitutions)
  File "/usr/local/lib/python2.7/site-packages/fontTools/varLib/featureVars.py", line 214, in addFeatureVariationsRaw
    langSys.FeatureIndex.append(rvrnFeatureIndex)
  File "/usr/local/lib/python2.7/site-packages/fontTools/ttLib/tables/otBase.py", line 546, in __getattr__
    raise AttributeError(attr)
AttributeError: FeatureIndex
```


Discussion:
While generating the font from fontmake I also did get these, not sure if relevant:
```
INFO:fontTools.ttLib.tables.otBase:Attempting to fix OTLOffsetOverflowError ('GPOS', 'LookupIndex:', 3, 'SubTableIndex:', None, 'ItemName:', None, 'ItemIndex:', None)
INFO:fontTools.ttLib.tables.otBase:Attempting to fix OTLOffsetOverflowError ('GPOS', 'LookupIndex:', 3, 'SubTableIndex:', None, 'ItemName:', None, 'ItemIndex:', None)
INFO:fontTools.ttLib.tables.otBase:Attempting to fix OTLOffsetOverflowError ('GPOS', 'LookupIndex:', 2, 'SubTableIndex:', None, 'ItemName:', None, 'ItemIndex:', None)
INFO:fontTools.ttLib.tables.otBase:Attempting to fix OTLOffsetOverflowError ('GPOS', 'LookupIndex:', 1, 'SubTableIndex:', 0, 'ItemName:', 'PairSet', 'ItemIndex:', 28)
INFO:fontTools.ttLib.tables.otBase:Attempting to fix OTLOffsetOverflowError ('GPOS', 'LookupIndex:', 1, 'SubTableIndex:', 0, 'ItemName:', 'PairSet', 'ItemIndex:', 28)

```
@weiweihuanghuang you don't need to write your own script to do that, fontTools.varLib supports building GSUB FeatureVariations tables based on the designspace document's `<rules>` element.
For example, see this test case:
https://github.com/fonttools/fonttools/blob/0331f668c0bd6adcb6a855ddf851054be9eb8a2a/Tests/varLib/data/FeatureVars.designspace#L9-L29

you can build the variable font using fontmake from a designspace file containing similar rule elements, and it will contain the corresponding GSUB with FeatureVariations tables.

We need to document this better, I know.
those INFO logging messages are benign, they're just informing your that fonttools is fixing an offset overflow occurring in some lookup subtables while compiling the GPOS table
I get the same error with a .designspace file:

```
INFO:fontTools.varLib:Generating GSUB FeatureVariations
Traceback (most recent call last):
  File "/usr/local/bin/fontmake", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/site-packages/fontmake/__main__.py", line 275, in main
    project.run_from_designspace(designspace_path, **args)
  File "/usr/local/lib/python2.7/site-packages/fontmake/font_project.py", line 663, in run_from_designspace
    **kwargs)
  File "/usr/local/lib/python2.7/site-packages/fontmake/font_project.py", line 732, in run_from_ufos
    master_bin_dir=master_bin_dir)
  File "/usr/local/lib/python2.7/site-packages/fontmake/font_project.py", line 269, in build_variable_font
    font, _, _ = varLib.build(designspace_path, finder)
  File "/usr/local/lib/python2.7/site-packages/fontTools/varLib/__init__.py", line 756, in build
    _add_GSUB_feature_variations(vf, ds.axes, ds.internal_axis_supports, ds.rules)
  File "/usr/local/lib/python2.7/site-packages/fontTools/varLib/__init__.py", line 606, in _add_GSUB_feature_variations
    addFeatureVariations(font, conditional_subs)
  File "/usr/local/lib/python2.7/site-packages/fontTools/varLib/featureVars.py", line 65, in addFeatureVariations
    addFeatureVariationsRaw(font, explodedConditionalSubstitutions)
  File "/usr/local/lib/python2.7/site-packages/fontTools/varLib/featureVars.py", line 214, in addFeatureVariationsRaw
    langSys.FeatureIndex.append(rvrnFeatureIndex)
  File "/usr/local/lib/python2.7/site-packages/fontTools/ttLib/tables/otBase.py", line 546, in __getattr__
    raise AttributeError(attr)
AttributeError: FeatureIndex
```
[Archive.zip](https://github.com/fonttools/fonttools/files/2559893/Archive.zip)

Confirmed, investigating.
Found the bug, working on a fix.
Thanks @justvanrossum!

Resolving patch:
diff --git a/Lib/fontTools/varLib/featureVars.py b/Lib/fontTools/varLib/featureVars.py
--- a/Lib/fontTools/varLib/featureVars.py
+++ b/Lib/fontTools/varLib/featureVars.py
@@ -210,7 +210,8 @@ def addFeatureVariationsRaw(font, conditionalSubstitutions):
     rvrnFeatureIndex = gsub.FeatureList.FeatureRecord.index(rvrnFeature)
 
     for scriptRecord in gsub.ScriptList.ScriptRecord:
-        for langSys in [scriptRecord.Script.DefaultLangSys] + scriptRecord.Script.LangSysRecord:
+        langSystems = [lsr.LangSys for lsr in scriptRecord.Script.LangSysRecord]
+        for langSys in [scriptRecord.Script.DefaultLangSys] + langSystems:
             langSys.FeatureIndex.append(rvrnFeatureIndex)
 
     # setup lookups


Test patch: