MCPcopy Create free account
hub / github.com/PageBot/PageBot / getInstance2

Function getInstance2

Lib/pagebot/fonttoolbox/objects/font.py:294–348  ·  view source on GitHub ↗

Answers the VF-TTFont instance at location (created by fontTools.varLib.mutator.instantiateVariableFont) packed as Font instance. >>> vf = findFont('Amstelvar-Roman-VF') >>> instance = getInstance2(vf, opticalSize=8) >>> instance >>> instance.

(vf, location=None, dstPath=None, name=None,
        opticalSize=None, styleName=None, cached=True, lazy=True)

Source from the content-addressed store, hash-verified

292 return scaledLocation
293
294def getInstance2(vf, location=None, dstPath=None, name=None,
295 opticalSize=None, styleName=None, cached=True, lazy=True):
296 """Answers the VF-TTFont instance at location (created by
297 fontTools.varLib.mutator.instantiateVariableFont) packed as Font instance.
298
299 >>> vf = findFont('Amstelvar-Roman-VF')
300 >>> instance = getInstance2(vf, opticalSize=8)
301 >>> instance
302 <Font Amstelvar-Roman-VF-opsz8>
303 >>> instance.location
304 {'opsz': 8}
305 >>> instance['H'].width
306 1740
307 """
308 """
309 >>> instance = getInstance2(vf, location=dict(wght=300), cached=False, opticalSize=150)
310 >>> instance.location
311 {'wght': 300, 'opsz': 150}
312 >>> instance['H'].width
313 1740
314 >>> instance = getInstance2(vf, path='/tmp/TestVariableFontInstance.ttf', opticalSize=8)
315 >>> instance
316 <Font TestVariableFontInstance>
317 """
318
319 if location is None:
320 location = {}
321 if opticalSize is not None:
322 location['opsz'] = opticalSize
323
324 #if path is None and cached:
325
326 # Make a custom file name from the location e.g. VariableFont-wghtXXX-wdthXXX.ttf
327 # Only add axis values to the name that are not default.
328 instanceName = ""
329 for tag, value in sorted(location.items()):
330 if value != vf.axes[tag][1]:
331 instanceName += "-%s%s" % (tag, asFormatted(value))
332 instanceFileName = '.'.join(vf.path.split('/')[-1].split('.')[:-1]) + instanceName + '.ttf'
333
334 targetDirectory = getInstancePath()
335 if not os.path.exists(targetDirectory):
336 os.makedirs(targetDirectory)
337 path = targetDirectory + instanceFileName
338
339 if cached and os.path.exists(path):
340 instance = Font(path=path, name=name, location=location, opticalSize=opticalSize)
341 else:
342 ttFont = instantiateVariableFont(vf.ttFont, location) # Get instance from fontTools
343 instance = Font(path=path, ttFont=ttFont, name=name, location=location, opticalSize=opticalSize,
344 styleName=styleName, lazy=lazy)
345 if instance.path.endswith('.ttf'):
346 instance.save()
347
348 return instance
349
350def getInstance(pathOrFont, location, dstPath=None, styleName=None,
351 opticalSize=None, normalize=True, cached=True, lazy=True,

Callers

nothing calls this directly

Calls 4

saveMethod · 0.95
asFormattedFunction · 0.90
FontClass · 0.85
getInstancePathFunction · 0.70

Tested by

no test coverage detected