Skip to content

Commit

Permalink
Add cache for component base glyphs, to reduce the load on the reader…
Browse files Browse the repository at this point in the history
… (which may not do any caching)
  • Loading branch information
justvanrossum committed Aug 17, 2023
1 parent 73e119e commit af4bdae
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/fontra_compile/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ async def setup(self):
self.globalAxisTags = {axis.name: axis.tag for axis in self.globalAxes}
self.defaultLocation = {k: v[1] for k, v in self.globalAxisDict.items()}

self.cachedSourceGlyphs = {}

self.glyphs = {}
self.cmap = {}
self.xAdvances = {}
Expand All @@ -55,6 +57,14 @@ async def build(self):
await self.buildGlyphs()
return await self.buildFont()

async def getSourceGlyph(self, glyphName, storeInCache=False):
sourceGlyph = self.cachedSourceGlyphs.get(glyphName)
if sourceGlyph is None:
sourceGlyph = await self.reader.getGlyph(glyphName)
if storeInCache:
self.cachedSourceGlyphs[glyphName] = sourceGlyph
return sourceGlyph

def ensureGlyphDependency(self, glyphName):
if glyphName not in self.glyphs and glyphName not in self.glyphOrder:
self.glyphOrder.append(glyphName)
Expand Down Expand Up @@ -88,7 +98,7 @@ async def buildGlyphs(self):
self.glyphs[glyphName] = glyph

async def buildOneGlyph(self, glyphName):
glyph = await self.reader.getGlyph(glyphName)
glyph = await self.getSourceGlyph(glyphName, False)
localAxisDict = {axis.name: axisTuple(axis) for axis in glyph.axes}
localDefaultLocation = {k: v[1] for k, v in localAxisDict.items()}
defaultLocation = {**self.defaultLocation, **localDefaultLocation}
Expand Down Expand Up @@ -291,7 +301,7 @@ async def setupComponentBaseAxes(self, compo):
# Ideally we need the full "made of" graph, so we can normalize
# nested var composites, but then again, our local axis name -> fvar tag name
# mechanism doesn't account for that, either.
baseGlyph = await self.reader.getGlyph(compo.name)
baseGlyph = await self.getSourceGlyph(compo.name, True)
localAxisNames = {axis.name for axis in baseGlyph.axes}
responsiveAxesNames = {
axisName for source in baseGlyph.sources for axisName in source.location
Expand Down

0 comments on commit af4bdae

Please sign in to comment.