GlyphPreview performance



  • Hi,

    I'm trying to make a little extension that takes two open fonts and gives you a live preview of the interpolated result from each font of the current glyph. It's working fine except every time it updates it seems to slow RoboFont down significantly (to the point where the app is so slow it's unusable).

    Here's what I have so far:



  • from vanilla import *
    from mojo.glyphPreview import GlyphPreview
    from mojo.events import addObserver
    from mojo.roboFont import OpenWindow
    
    class InterpTool:
    
        def __init__(self):
            self.w = Window((440, 400), "Interpolator Tool", minSize=(100, 100))
            self.w.preview = GlyphPreview((0, 30, -0, -0))
            
            self.w.t = TextBox((10,50,100,20))
    
            self.factor = 2.0
    
            addObserver(self, "_currentGlyphChanged", "currentGlyphChanged")
            
            self.w.slider = Slider((10,10,-10,23), 
                                   tickMarkCount=4,
                                   callback=self.setFactor,
                                   minValue=-1,
                                   maxValue=2,
                                   value=self.factor,
                                   continuous=False)
    
            self.fs = AllFonts()
            
            self.setGlyph()
            self.w.open()
    
        def _currentGlyphChanged(self, glyph, info):
            self.setGlyph()
    
        def setGlyph(self):
            if not CurrentGlyph():
                return
                
            glyphname = CurrentGlyph().name
            
            if len(self.fs) > 0:
                if self.fs[0][glyphname].isCompatible(self.fs[1][glyphname])[0]:
                    newGlyph = RGlyph()
                    newGlyph.interpolate(self.factor, self.fs[0][glyphname], self.fs[1][glyphname])
                    self.w.t.set("Compatible")
                    self.w.preview.setGlyph(newGlyph)
                else:
                    self.w.t.set("Incompatible")
                    self.w.preview.setGlyph(CurrentGlyph())
    
        def setFactor(self, sender):
            self.factor = sender.get()
            print self.factor
            self.setGlyph()
            
    OpenWindow(InterpTool)
    

  • admin

    mmm, each time you open this window an observer is been added. If you don't remove the observer when the window closes it will keep getting notifications on current glyph changes and after a while this will idd slow down RoboFont

    from defconAppKit.windows.baseWindow import BaseWindowController
    from mojo.events import addObserver, removeObserver
    
    class InterpTool(BaseWindowController):
    
        def __init__(self):
    
            addObserver(self, "_currentGlyphChanged", "currentGlyphChanged")
    
            # do you stuff
            # ...
    
            self.setUpBaseWindowBehavior()
            self.w.open()
    
        def windowCloseCallback(self, sender):
            # remove the observer to stop getting notifications
            removeObserver(self, "currentGlyphChanged")
            super(InterpTool, self).windowCloseCallback(sender)
         
    


  • This seems to fix my problem, thanks so much! One more thing, what would be the name of the observer for if a glyph is modified?—so that I can update the preview as you make changes. Is there a list somewhere of all the built in observers?

    Cheers!