Skip to content

Commit

Permalink
Use decomposeComposites to determine the needed axis ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Dec 13, 2024
1 parent 1bc5fd9 commit 54fa567
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions src/fontra/workflow/actions/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
VariableGlyph,
)
from ...core.glyphdependencies import GlyphDependencies
from ...core.instancer import GlyphInstancer
from ...core.path import PackedPath, PackedPathPointPen
from ...core.varutils import locationToTuple
from .axes import (
Expand Down Expand Up @@ -320,37 +321,25 @@ async def trimmedGlyphs(self):
fontInstancer = self.fontInstancer

dependencies = GlyphDependencies()
baseGlyphs = {}
glyphsToTrim = {}
glyphAxisRanges = {}

async def getInstancer(glyphName):
instancer = baseGlyphs.get(glyphName)
if instancer is None:
instancer = await fontInstancer.getGlyphInstancer(glyphName)
baseGlyphs[glyphName] = instancer
return instancer

for glyphName in await self.inputGlyphMap:
instancer = await getInstancer(glyphName)
instancer = await fontInstancer.getGlyphInstancer(glyphName, True)

glyph = instancer.glyph

axisRanges = await getComponentAxisRanges(glyph, getInstancer)

componentNames = set(axisRanges)
if componentNames:
dependencies.update(glyphName, componentNames)

glyphAxisRanges[glyphName] = axisRanges
glyphAxisRanges[glyphName] = await getComponentAxisRanges(
instancer, fontInstancer
)

if not glyph.axes:
del baseGlyphs[glyphName]
if instancer.componentNames:
dependencies.update(glyphName, instancer.componentNames)

glyphsToTrim = {
glyphName: instancer
for glyphName, instancer in baseGlyphs.items()
if instancer.glyph.axes
}
if glyph.axes:
glyphsToTrim[glyphName] = instancer
else:
fontInstancer.dropGlyphInstancerFromCache(glyphName)

axisRanges = mergeAxisRanges(glyphAxisRanges.values())

Expand All @@ -367,15 +356,18 @@ async def getInstancer(glyphName):
for parentGlyphName in dependencies.usedBy.get(glyphName, ())
)
}
assert nextBatch
assert nextBatch, list(glyphsToTrim)

for glyphName, instancer in nextBatch.items():
del glyphsToTrim[glyphName]
trimmedGlyphs[glyphName] = trimGlyphByAxisRanges(
fontInstancer, instancer, axisRanges.get(glyphName, {})
)
trimmedInstancer = GlyphInstancer(
trimmedGlyphs[glyphName], fontInstancer
)
glyphAxisRanges[glyphName] = await getComponentAxisRanges(
trimmedGlyphs[glyphName], getInstancer
trimmedInstancer, fontInstancer
)

axisRanges = mergeAxisRanges(glyphAxisRanges.values())
Expand All @@ -389,16 +381,10 @@ async def getGlyph(self, glyphName: str) -> VariableGlyph | None:
return glyph


async def getComponentAxisRanges(glyph, getInstancer):
axisRanges = defaultdict(lambda: defaultdict(AxisRange))
for source in getActiveSources(glyph.sources):
for component in glyph.layers[source.layerName].glyph.components:
baseGlyphName = component.name
baseInstancer = await getInstancer(baseGlyphName)
for axis in baseInstancer.glyph.axes:
axisRanges[baseGlyphName][axis.name].update(
component.location.get(axis.name, axis.defaultValue)
)
async def getComponentAxisRanges(instancer, fontInstancer):
with fontInstancer.collectVariableGlyphAxisRanges() as axisRanges:
_ = await decomposeComposites(fontInstancer, instancer)
return axisRanges
return axisRanges


Expand Down

0 comments on commit 54fa567

Please sign in to comment.