Skip to content

Commit

Permalink
Merge pull request #1832 from googlefonts/kern-gen-fix
Browse files Browse the repository at this point in the history
Fix kerning feature generation
  • Loading branch information
justvanrossum authored Dec 2, 2024
2 parents 73b85a5 + 5743f4e commit 1b74da5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/fontra/workflow/actions/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ def _kernKeySortFunc(self, item):
return key.startswith(self._kern1Prefix) or key.startswith(self._kern2Prefix)

async def processFeatures(self, features: OpenTypeFeatures) -> OpenTypeFeatures:
verticalKerning = (await self.inputKerning).get(self._kernFeatureTag)
if verticalKerning is None:
kerning = (await self.inputKerning).get(self._kernFeatureTag)
if kerning is None:
return features

glyphMap = await self.inputGlyphMap
Expand All @@ -213,24 +213,26 @@ async def processFeatures(self, features: OpenTypeFeatures) -> OpenTypeFeatures:
for k, v in mapLocation(sources[sid].location).items()
}
)
for sid in verticalKerning.sourceIdentifiers
for sid in kerning.sourceIdentifiers
]

w = FeatureWriter()

for groupName, group in sorted(verticalKerning.groups.items()):
for groupName, group in sorted(kerning.groups.items()):
w.addGroup(groupName, group)

fea = w.addFeature(self._kernFeatureTag)

for left, rightDict in sorted(
verticalKerning.values.items(), key=self._kernKeySortFunc
kerning.values.items(), key=self._kernKeySortFunc
):
if left.startswith(self._kern1Prefix):
leftIsClass = left.startswith(self._kern1Prefix)
if leftIsClass:
left = "@" + left

for right, values in sorted(rightDict.items(), key=self._kernKeySortFunc):
if right.startswith(self._kern2Prefix):
rightIsClass = right.startswith(self._kern2Prefix)
if rightIsClass:
right = "@" + right

values = [0 if v is None else round(v) for v in values]
Expand All @@ -245,7 +247,8 @@ async def processFeatures(self, features: OpenTypeFeatures) -> OpenTypeFeatures:
for loc, v in zip(locations, values, strict=True):
scalar.add_value(loc, v)

fea.addLine(f"pos {left} {right} {scalar}")
enumStr = "enum " if leftIsClass != rightIsClass else ""
fea.addLine(f"{enumStr}pos {left} {right} {scalar}")

featureText = w.asFea()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@kern.bottom.BC = [B C];
feature vkrn {
pos C C (wdth=100,wght=400:-5 wdth=100,wght=900:-15 wdth=200,wght=400:-25 wdth=200,wght=900:-35);
pos C @kern.bottom.BC (wdth=100,wght=400:-10 wdth=100,wght=900:-20 wdth=200,wght=400:-30 wdth=200,wght=900:-40);
pos @kern.top.AB C (wdth=100,wght=400:10 wdth=100,wght=900:20 wdth=200,wght=400:30 wdth=200,wght=900:40);
enum pos C @kern.bottom.BC (wdth=100,wght=400:-10 wdth=100,wght=900:-20 wdth=200,wght=400:-30 wdth=200,wght=900:-40);
enum pos @kern.top.AB C (wdth=100,wght=400:10 wdth=100,wght=900:20 wdth=200,wght=400:30 wdth=200,wght=900:40);
pos @kern.top.AB @kern.bottom.BC (wdth=100,wght=400:5 wdth=100,wght=900:15 wdth=200,wght=400:25 wdth=200,wght=900:35);
} vkrn;

0 comments on commit 1b74da5

Please sign in to comment.