diff --git a/Lib/fontTools/subset/__init__.py b/Lib/fontTools/subset/__init__.py
index ebd99728f..108de4f31 100644
--- a/Lib/fontTools/subset/__init__.py
+++ b/Lib/fontTools/subset/__init__.py
@@ -1803,15 +1803,17 @@ def _remap_index_map(s, varidx_map, table_map):
def subset_glyphs(self, s):
table = self.table
- # TODO Update for retain_gids
-
used = set()
+ advIdxes_ = set()
+ retainAdvMap = False
if table.AdvWidthMap:
table.AdvWidthMap.mapping = _dict_subset(table.AdvWidthMap.mapping, s.glyphs)
used.update(table.AdvWidthMap.mapping.values())
else:
used.update(s.reverseOrigGlyphMap.values())
+ advIdxes_ = used.copy()
+ retainAdvMap = s.options.retain_gids
if table.LsbMap:
table.LsbMap.mapping = _dict_subset(table.LsbMap.mapping, s.glyphs)
@@ -1820,7 +1822,7 @@ def subset_glyphs(self, s):
table.RsbMap.mapping = _dict_subset(table.RsbMap.mapping, s.glyphs)
used.update(table.RsbMap.mapping.values())
- varidx_map = varStore.VarStore_subset_varidxes(table.VarStore, used, retainFirstMap=table.AdvWidthMap is None)
+ varidx_map = varStore.VarStore_subset_varidxes(table.VarStore, used, retainFirstMap=retainAdvMap, advIdxes=advIdxes_)
if table.AdvWidthMap:
table.AdvWidthMap.mapping = _remap_index_map(s, varidx_map, table.AdvWidthMap)
@@ -1837,12 +1839,16 @@ def subset_glyphs(self, s):
table = self.table
used = set()
+ advIdxes_ = set()
+ retainAdvMap = False
if table.AdvHeightMap:
table.AdvHeightMap.mapping = _dict_subset(table.AdvHeightMap.mapping, s.glyphs)
used.update(table.AdvHeightMap.mapping.values())
else:
used.update(s.reverseOrigGlyphMap.values())
+ advIdxes_ = used.copy()
+ retainAdvMap = s.options.retain_gids
if table.TsbMap:
table.TsbMap.mapping = _dict_subset(table.TsbMap.mapping, s.glyphs)
@@ -1854,7 +1860,7 @@ def subset_glyphs(self, s):
table.VOrgMap.mapping = _dict_subset(table.VOrgMap.mapping, s.glyphs)
used.update(table.VOrgMap.mapping.values())
- varidx_map = varStore.VarStore_subset_varidxes(table.VarStore, used, retainFirstMap=table.AdvHeightMap is None)
+ varidx_map = varStore.VarStore_subset_varidxes(table.VarStore, used, retainFirstMap=retainAdvMap, advIdxes=advIdxes_)
if table.AdvHeightMap:
table.AdvHeightMap.mapping = _remap_index_map(s, varidx_map, table.AdvHeightMap)
diff --git a/Lib/fontTools/varLib/varStore.py b/Lib/fontTools/varLib/varStore.py
index e6609a689..f8ce81996 100644
--- a/Lib/fontTools/varLib/varStore.py
+++ b/Lib/fontTools/varLib/varStore.py
@@ -187,8 +187,10 @@ class VarStoreInstancer(object):
#
# Optimizations
#
+# retainFirstMap - If true, major 0 mappings are retained. Deltas for unused indices are zeroed
+# advIdxes - Set of major 0 indices for advance deltas to be listed first. Other major 0 indices follow.
-def VarStore_subset_varidxes(self, varIdxes, optimize=True, retainFirstMap=False):
+def VarStore_subset_varidxes(self, varIdxes, optimize=True, retainFirstMap=False, advIdxes=set()):
# Sort out used varIdxes by major/minor.
used = {}
@@ -222,7 +224,11 @@ def VarStore_subset_varidxes(self, varIdxes, optimize=True, retainFirstMap=False
newItems.append(items[minor] if minor in usedMinors else [0] * len(items[minor]))
varDataMap[minor] = minor
else:
- for minor in sorted(usedMinors):
+ if major == 0:
+ minors = sorted(advIdxes) + sorted(usedMinors - advIdxes)
+ else:
+ minors = sorted(usedMinors)
+ for minor in minors:
newMinor = len(newItems)
newItems.append(items[minor])
varDataMap[(major<<16)+minor] = (newMajor<<16)+newMinor
diff --git a/Tests/subset/data/expect_HVVAR.ttx b/Tests/subset/data/expect_HVVAR.ttx
index 08d35a887..5fbc1770d 100644
--- a/Tests/subset/data/expect_HVVAR.ttx
+++ b/Tests/subset/data/expect_HVVAR.ttx
@@ -32,16 +32,15 @@
-
+
-
-
-
-
-
+
+
+
+
@@ -55,12 +54,12 @@
-
+
-
+
-
+