Preprocessing a font before compiling without leaving a trace



  • Hi,

    I have a script that adds mark positioning (plus kerning) to the font when it's being generated. But I have to revert the features to what it was before after the compile. This will make the font dirty. Is it possible to change the features during the compilation without leaving a trace?

    Here is the script:

    from vanilla import *
    from AppKit import NSApp
    from mojo.events import addObserver, removeObserver
    from ufo2ft.featureCompiler import FeatureCompiler
    from collections import OrderedDict
    
    """
    Postprocessor for font generating font in RoboFont. This one adds mark and
    kerning to the OTF before the font is generated
    """
    debug = True
    
    class addMarkKernOnGenerate():
    
        def __init__(self):
            windowname = 'Mark Kern UFO2FT'
            self._featuresBuffer = ""
            if debug:
                self.w = Window((500, 50), windowname)
                self.w.bind("close", self._windowClosed)
                for window in [w for w in NSApp().orderedWindows() if w.isVisible()]:
                    if window.title() == windowname:
                        window.close()
                self.w.open()
            addObserver(self, "_fontDidGenerate", "fontDidGenerate")
            addObserver(self, "_fontWillGenerate", "fontWillGenerate")
    
        def _windowClosed(self, sender):
            removeObserver(self, "fontDidGenerate")
            removeObserver(self, "fontWillGenerate")
            print('removing Observer')
    
        def _fontWillGenerate(self, info):
            f = info["font"]
            self._featuresBuffer = f.features.text # keep the features in the buffer and revert it back after the generate
            skipExport = f.lib.get("public.skipExportGlyphs", [])
            glyphOrder = (gn for gn in f.glyphOrder if gn not in skipExport)
            featureCompiler = FeatureCompiler(f)
            featureCompiler.glyphSet = OrderedDict((gn, f[gn]) for gn in glyphOrder)
            featureCompiler.compile()
            f.features.text = featureCompiler.features
            path = info["path"]
            print(f"{path}: GPOS added using ufo2ft")
    
        def _fontDidGenerate(self, info):
            f = info["font"]
            f.features.text = self._featuresBuffer
    
    addMarkKernOnGenerate()
    
    

    As you can see I keep the font original features in the _featuresBuffer attribute to revert it after the font has been compiled.

    Many Thanks!


Log in to reply