New API: Generate layout features/classes/prefixes with a plug-in

New in the recently released cutting-edge version Glyphs 3.5 (3526) is an API for generating custom feature code.

In a plug-in, you can now register a feature code generator like so:

GSCallbackHandler.addFeatureCodeGenerator_(SomeFeatureCodeGenerator)

The generator needs to be a subclass of NSObject and implement the GSFeatureCodeGeneratorProtocol protocol:

class SomeFeatureCodeGenerator(NSObject):
    __pyobjc_protocols__ = [GSFeatureCodeGeneratorProtocol]

Inside this class, you can add method pairs like the following:

@classmethod
def canGenerateFeatureCodeForFeatureWithTag_(cls, featureTag: str):
    return featureTag == "calt"

@classmethod
def featureCodeForFeature_font_error_(cls, feature: GSFeature, font: GSFont, error):
    if feature.name == "calt":
        feature.code = "..."
    return True, None

The same applies to classes and prefixes:

@classmethod
def canGenerateFeatureCodeForClassWithName_(cls, className: str): ...

@classmethod
def featureCodeForClass_font_error_(cls, aClass: GSClass, font: GSFont, error): ...
@classmethod
def canGenerateFeatureCodeForPrefixWithName_(cls, prefixName: str): ...

@classmethod
def featureCodeForPrefix_font_error_(cls, prefix: GSFeaturePrefix, font: GSFont, error): ...

The code generator is used when the “automatic” checkbox above the code editor in the Features tab is checked. You can overwrite the existing feature code, append your own code, or perform any other customizations. Your generator is invoked every time the feature code is updated.

You can find a sample project on the Glyphs SDK site:

6 Likes