Skip to content

Commit

Permalink
Merge pull request #3132 from fonttools/fealib-simplify-variablescaler
Browse files Browse the repository at this point in the history
[feaLib] Simplify variable scalars that don’t vary
  • Loading branch information
khaledhosny committed May 27, 2023
2 parents bf77873 + fd854d7 commit 96bede3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
52 changes: 27 additions & 25 deletions Lib/fontTools/feaLib/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,26 @@ def add_conditionset(self, location, key, value):

self.conditionsets_[key] = value

def makeVariablePos(self, location, varscalar):
if not self.varstorebuilder:
raise FeatureLibError(
"Can't define a variable scalar in a non-variable font", location
)

varscalar.axes = self.axes
if not varscalar.does_vary:
return varscalar.default, None

default, index = varscalar.add_to_variation_store(
self.varstorebuilder, self.model_cache, self.font.get("avar")
)

device = None
if index is not None and index != 0xFFFFFFFF:
device = buildVarDevTable(index)

return default, device

def makeOpenTypeAnchor(self, location, anchor):
"""ast.Anchor --> otTables.Anchor"""
if anchor is None:
Expand All @@ -1616,7 +1636,6 @@ def makeOpenTypeAnchor(self, location, anchor):
deviceX = otl.buildDevice(dict(anchor.xDeviceTable))
if anchor.yDeviceTable is not None:
deviceY = otl.buildDevice(dict(anchor.yDeviceTable))
avar = self.font.get("avar")
for dim in ("x", "y"):
varscalar = getattr(anchor, dim)
if not isinstance(varscalar, VariableScalar):
Expand All @@ -1625,20 +1644,13 @@ def makeOpenTypeAnchor(self, location, anchor):
raise FeatureLibError(
"Can't define a device coordinate and variable scalar", location
)
if not self.varstorebuilder:
raise FeatureLibError(
"Can't define a variable scalar in a non-variable font", location
)
varscalar.axes = self.axes
default, index = varscalar.add_to_variation_store(
self.varstorebuilder, self.model_cache, avar
)
default, device = self.makeVariablePos(location, varscalar)
setattr(anchor, dim, default)
if index is not None and index != 0xFFFFFFFF:
if device is not None:
if dim == "x":
deviceX = buildVarDevTable(index)
deviceX = device
else:
deviceY = buildVarDevTable(index)
deviceY = device
variable = True

otlanchor = otl.buildAnchor(
Expand All @@ -1659,7 +1671,6 @@ def makeOpenTypeValueRecord(self, location, v, pairPosContext):
if not v:
return None

avar = self.font.get("avar")
vr = {}
for astName, (otName, isDevice) in self._VALUEREC_ATTRS.items():
val = getattr(v, astName, None)
Expand All @@ -1674,18 +1685,9 @@ def makeOpenTypeValueRecord(self, location, v, pairPosContext):
raise FeatureLibError(
"Can't define a device coordinate and variable scalar", location
)
if not self.varstorebuilder:
raise FeatureLibError(
"Can't define a variable scalar in a non-variable font",
location,
)
val.axes = self.axes
default, index = val.add_to_variation_store(
self.varstorebuilder, self.model_cache, avar
)
vr[otName] = default
if index is not None and index != 0xFFFFFFFF:
vr[otDeviceName] = buildVarDevTable(index)
vr[otName], device = self.makeVariablePos(location, val)
if device is not None:
vr[otDeviceName] = device
else:
vr[otName] = val

Expand Down
1 change: 1 addition & 0 deletions Tests/feaLib/data/variable_scalar_valuerecord.fea
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ languagesystem DFLT dflt;
feature kern {
pos one 1;
pos two <0 (wght=200:12 wght=900:22 wdth=150,wght=900:42) 0 0>;
pos three <0 (wght=200:12 wght=900:12 wdth=150,wght=900:12) 0 0>;
} kern;
9 changes: 8 additions & 1 deletion Tests/feaLib/data/variable_scalar_valuerecord.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<Lookup index="0">
<LookupType value="1"/>
<LookupFlag value="0"/>
<!-- SubTableCount=2 -->
<!-- SubTableCount=3 -->
<SinglePos index="0" Format="1">
<Coverage>
<Glyph value="one"/>
Expand All @@ -97,6 +97,13 @@
</YPlaDevice>
</Value>
</SinglePos>
<SinglePos index="2" Format="1">
<Coverage>
<Glyph value="three"/>
</Coverage>
<ValueFormat value="2"/>
<Value YPlacement="12"/>
</SinglePos>
</Lookup>
</LookupList>
</GPOS>
Expand Down

0 comments on commit 96bede3

Please sign in to comment.