[varLib/featureVars] Insert rvrn lookups at beginning

Fixes https://github.com/fonttools/fonttools/issues/2140
Fixes https://github.com/googlefonts/fontmake/issues/950
This commit is contained in:
Behdad Esfahbod 2022-10-28 13:20:32 -06:00
parent 492645239b
commit 0f48f0a5ea
2 changed files with 49 additions and 23 deletions

View File

@ -7,6 +7,7 @@ from fontTools.misc.dictTools import hashdict
from fontTools.misc.intTools import popCount
from fontTools.ttLib import newTable
from fontTools.ttLib.tables import otTables as ot
from fontTools.ttLib.ttVisitor import TTVisitor
from fontTools.otlLib.builder import buildLookup, buildSingleSubstSubtable
from collections import OrderedDict
@ -356,7 +357,7 @@ def addFeatureVariationsRaw(font, table, conditionalSubstitutions, featureTag='r
records = []
for varFeatureIndex in varFeatureIndices:
existingLookupIndices = table.FeatureList.FeatureRecord[varFeatureIndex].Feature.LookupListIndex
records.append(buildFeatureTableSubstitutionRecord(varFeatureIndex, existingLookupIndices + lookupIndices))
records.append(buildFeatureTableSubstitutionRecord(varFeatureIndex, lookupIndices + existingLookupIndices))
featureVariationRecords.append(buildFeatureVariationRecord(conditionTable, records))
table.FeatureVariations = buildFeatureVariations(featureVariationRecords)
@ -414,19 +415,44 @@ def makeSubstitutionsHashable(conditionalSubstitutions):
condSubst.append((conditionSet, substitutions))
return condSubst, sorted(allSubstitutions)
class ShifterVisitor(TTVisitor):
def __init__(self, shift):
self.shift = shift
@ShifterVisitor.register_attr(ot.Feature, "LookupListIndex") # GSUB/GPOS
def visit(visitor, obj, attr, value):
shift = visitor.shift
value = [l + shift for l in value]
setattr(obj, attr, value)
@ShifterVisitor.register_attr(
(ot.SubstLookupRecord, ot.PosLookupRecord),
"LookupListIndex"
)
def visit(visitor, obj, attr, value):
setattr(obj, attr, visitor.shift + value)
def buildSubstitutionLookups(gsub, allSubstitutions):
"""Build the lookups for the glyph substitutions, return a dict mapping
the substitution to lookup indices."""
firstIndex = len(gsub.LookupList.Lookup)
# Insert lookups at the beginning of the lookup vector
# https://github.com/googlefonts/fontmake/issues/950
lookupMap = {}
for i, substitutionMap in enumerate(allSubstitutions):
lookupMap[substitutionMap] = i + firstIndex
lookupMap[substitutionMap] = i
for subst in allSubstitutions:
# Shift all lookup indices in gsub by len(allSubstitutions)
shift = len(allSubstitutions)
visitor = ShifterVisitor(shift)
visitor.visit(gsub.FeatureList.FeatureRecord)
visitor.visit(gsub.LookupList.Lookup)
for i, subst in enumerate(allSubstitutions):
substMap = dict(subst)
lookup = buildLookup([buildSingleSubstSubtable(substMap)])
gsub.LookupList.Lookup.append(lookup)
gsub.LookupList.Lookup.insert(i, lookup)
assert gsub.LookupList.Lookup[lookupMap[subst]] is lookup
gsub.LookupList.LookupCount = len(gsub.LookupList.Lookup)
return lookupMap

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="3.29">
<ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="4.38">
<fvar>
@ -54,14 +54,14 @@
<FeatureTag value="rclt"/>
<Feature>
<!-- LookupCount=1 -->
<LookupListIndex index="0" value="0"/>
<LookupListIndex index="0" value="3"/>
</Feature>
</FeatureRecord>
<FeatureRecord index="1">
<FeatureTag value="rclt"/>
<Feature>
<!-- LookupCount=1 -->
<LookupListIndex index="0" value="1"/>
<LookupListIndex index="0" value="4"/>
</Feature>
</FeatureRecord>
</FeatureList>
@ -72,7 +72,7 @@
<LookupFlag value="0"/>
<!-- SubTableCount=1 -->
<SingleSubst index="0">
<Substitution in="uni0041" out="uni0061"/>
<Substitution in="uni0024" out="uni0024.nostroke"/>
</SingleSubst>
</Lookup>
<Lookup index="1">
@ -88,7 +88,7 @@
<LookupFlag value="0"/>
<!-- SubTableCount=1 -->
<SingleSubst index="0">
<Substitution in="uni0024" out="uni0024.nostroke"/>
<Substitution in="uni0061" out="uni0041"/>
</SingleSubst>
</Lookup>
<Lookup index="3">
@ -104,7 +104,7 @@
<LookupFlag value="0"/>
<!-- SubTableCount=1 -->
<SingleSubst index="0">
<Substitution in="uni0061" out="uni0041"/>
<Substitution in="uni0041" out="uni0061"/>
</SingleSubst>
</Lookup>
</LookupList>
@ -133,7 +133,7 @@
<Feature>
<!-- LookupCount=3 -->
<LookupListIndex index="0" value="0"/>
<LookupListIndex index="1" value="2"/>
<LookupListIndex index="1" value="1"/>
<LookupListIndex index="2" value="3"/>
</Feature>
</SubstitutionRecord>
@ -141,9 +141,9 @@
<FeatureIndex value="1"/>
<Feature>
<!-- LookupCount=3 -->
<LookupListIndex index="0" value="1"/>
<LookupListIndex index="1" value="2"/>
<LookupListIndex index="2" value="3"/>
<LookupListIndex index="0" value="0"/>
<LookupListIndex index="1" value="1"/>
<LookupListIndex index="2" value="4"/>
</Feature>
</SubstitutionRecord>
</FeatureTableSubstitution>
@ -169,15 +169,15 @@
<FeatureIndex value="0"/>
<Feature>
<!-- LookupCount=2 -->
<LookupListIndex index="0" value="0"/>
<LookupListIndex index="1" value="4"/>
<LookupListIndex index="0" value="2"/>
<LookupListIndex index="1" value="3"/>
</Feature>
</SubstitutionRecord>
<SubstitutionRecord index="1">
<FeatureIndex value="1"/>
<Feature>
<!-- LookupCount=2 -->
<LookupListIndex index="0" value="1"/>
<LookupListIndex index="0" value="2"/>
<LookupListIndex index="1" value="4"/>
</Feature>
</SubstitutionRecord>
@ -199,7 +199,7 @@
<FeatureIndex value="0"/>
<Feature>
<!-- LookupCount=2 -->
<LookupListIndex index="0" value="0"/>
<LookupListIndex index="0" value="1"/>
<LookupListIndex index="1" value="3"/>
</Feature>
</SubstitutionRecord>
@ -208,7 +208,7 @@
<Feature>
<!-- LookupCount=2 -->
<LookupListIndex index="0" value="1"/>
<LookupListIndex index="1" value="3"/>
<LookupListIndex index="1" value="4"/>
</Feature>
</SubstitutionRecord>
</FeatureTableSubstitution>
@ -230,15 +230,15 @@
<Feature>
<!-- LookupCount=2 -->
<LookupListIndex index="0" value="0"/>
<LookupListIndex index="1" value="2"/>
<LookupListIndex index="1" value="3"/>
</Feature>
</SubstitutionRecord>
<SubstitutionRecord index="1">
<FeatureIndex value="1"/>
<Feature>
<!-- LookupCount=2 -->
<LookupListIndex index="0" value="1"/>
<LookupListIndex index="1" value="2"/>
<LookupListIndex index="0" value="0"/>
<LookupListIndex index="1" value="4"/>
</Feature>
</SubstitutionRecord>
</FeatureTableSubstitution>