Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feaLib] Simplify variable scalars that don’t vary #3132

Merged
merged 2 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@

self.conditionsets_[key] = value

def makeVariablePos(self, location, varscalar):
if not self.varstorebuilder:
raise FeatureLibError(

Check warning on line 1611 in Lib/fontTools/feaLib/builder.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/feaLib/builder.py#L1611

Added line #L1611 was not covered by tests
"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 @@
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 @@
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

Check warning on line 1651 in Lib/fontTools/feaLib/builder.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/feaLib/builder.py#L1651

Added line #L1651 was not covered by tests
else:
deviceY = buildVarDevTable(index)
deviceY = device
variable = True

otlanchor = otl.buildAnchor(
Expand All @@ -1659,7 +1671,6 @@
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 @@
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