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

Function makeInstance

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

Instantiate an instance of a variable font at the specified location. Keyword arguments: - varfilename -- a variable font file path - location -- a dictionary of axis tag and value {"wght": 0.75, "wdth": -0.5} >>> vf = findFont('RobotoDelta-VF') >>> print(vf) <Font RobotoDe

(pathOrVarFont, location, dstPath=None, normalize=True, cached=True,
        lazy=True, kerning=None)

Source from the content-addressed store, hash-verified

398 return instance
399
400def makeInstance(pathOrVarFont, location, dstPath=None, normalize=True, cached=True,
401 lazy=True, kerning=None):
402 """Instantiate an instance of a variable font at the specified location.
403
404 Keyword arguments:
405 - varfilename -- a variable font file path
406 - location -- a dictionary of axis tag and value {"wght": 0.75, "wdth": -0.5}
407
408 >>> vf = findFont('RobotoDelta-VF')
409 >>> print(vf)
410 <Font RobotoDelta-VF>
411 >>> print(len(vf))
412 188
413 >>> instance = makeInstance(vf.path, dict(opsz=8), cached=False)
414 >>> instance
415 <Font RobotoDelta-VF-opsz8>
416 >>> len(instance) > 100
417 True
418 >>> len(instance['H'].points)
419 12
420 """
421 # make a custom file name from the location e.g.
422 # VariableFont-wghtXXX-wdthXXX.ttf
423 instanceName = ""
424
425 if isinstance(pathOrVarFont, Font):
426 pathOrVarFont = pathOrVarFont.path
427
428 varFont = Font(pathOrVarFont, lazy=lazy)
429 ttFont = varFont.ttFont
430
431 for k, v in sorted(location.items()):
432 # TODO better way to normalize the location name to (0, 1000)
433 v = min(v, 1000)
434 v = max(v, 0)
435 instanceName += "-%s%s" % (k, v)
436
437 if dstPath is None:
438 targetFileName = '.'.join(varFont.path.split('/')[-1].split('.')[:-1]) + instanceName + '.ttf'
439 targetDirectory = getInstancePath()
440 if not targetDirectory.endswith('/'):
441 targetDirectory += '/'
442 if not os.path.exists(targetDirectory):
443 os.makedirs(targetDirectory)
444 dstPath = targetDirectory + targetFileName
445
446 # Instance does not exist as file. Create it.
447 if not cached or not os.path.exists(dstPath):
448 # Set the instance name IDs in the name table
449 platforms=((1, 0, 0), (3, 1, 0x409)) # Macintosh and Windows
450
451 for platformID, platEncID, langID in platforms:
452 # 1 Font Family name
453 familyName = ttFont['name'].getName(1, platformID, platEncID, langID)
454 if not familyName:
455 continue
456 # NameRecord to unicode string
457 familyName = familyName.toUnicode()

Callers 1

getInstanceFunction · 0.85

Calls 5

saveMethod · 0.95
FontClass · 0.85
getInstancePathFunction · 0.70
keysMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected