Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Last active September 3, 2023 19:29
Show Gist options
  • Save connordavenport/805b81898203cf00810f1c0d7a64b177 to your computer and use it in GitHub Desktop.
Save connordavenport/805b81898203cf00810f1c0d7a64b177 to your computer and use it in GitHub Desktop.
Subsriber to automatically set font name entries when info has changed
from mojo.subscriber import Subscriber, registerCurrentFontSubscriber
import unicodedata
class AutoNameSetter(Subscriber):
debug = True
def build(self):
pass
def remove_accents(self, input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
only_ascii = nfkd_form.encode('ASCII', 'ignore')
return (only_ascii).decode('utf-8')
def currentFontInfoDidChangeValue(self, info):
p = False
iterations = info["iterations"]
keys = [key for it in iterations for key, values in it["fontInfo"].asDict().items() if (values == it["newValue"]) and (key in ["styleName","familyName"])]
# only change if the new value is associated with the familyName or styleName
if keys:
font = info["font"]
font.info.styleMapFamilyName = None
font.info.openTypeNamePreferredFamilyName = None
font.info.openTypeNamePreferredSubfamilyName = None
font.info.openTypeNameCompatibleFullName = "%s %s" % (font.info.familyName, font.info.styleName)
fixby = (len(font.info.openTypeNameCompatibleFullName)) - 31
postscriptFontName = "%s-%s" % (font.info.familyName, font.info.styleName)
postscriptFontName = postscriptFontName.replace(" ", "")
font.info.postscriptFontName = self.remove_accents(postscriptFontName)
postscriptFullName = "%s %s" % (font.info.familyName, font.info.styleName)
font.info.postscriptFullName = self.remove_accents(postscriptFullName)
if not font.info.versionMinor:
font.info.versionMinor = 0
if not font.info.versionMajor:
font.info.versionMajor = 0
font.info.openTypeNameUniqueID = f'{font.info.versionMajor}.{str(font.info.versionMinor).zfill(3)};{font.info.openTypeOS2VendorID};{font.info.postscriptFontName}'
if len(font.info.openTypeNameCompatibleFullName) > 31:
print("Fix this naming for compatibility, %s character(s) too long" % fixby)
font.info.note = "Fix this naming for compatibility, %s character(s) too long" % fixby
registerCurrentFontSubscriber(AutoNameSetter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment