SOLVED Unique font ID or temp data



  • I'm writing a script with a persistent window that needs to keep track of which fonts are open. The document open/close/activated event callbacks are working well, but I'd like to have a reliable way to identify each open font. I can't always distinguish between fonts based on their family name, file path (new documents are the same), or their index in the list of AllFonts(). Does RoboFont assign the font/document a UUID that I could use? Or is there a good alternative?

    On a related note, I would like to be able to store temporary data in a font object without it being permanently stored in the UFO. Is this possible? The Glyphs API has tempData and userData, but in the RoboFont docs and forum I've only seen mention of font.lib—which is permanent like userData. If there isn't a unique ID for each font, I could create my own IDs and store them in each font's temporary data for as long as the document is open. Does RoboFont have something like font.tempData?


  • admin

    Tal and I spoke about tempLib some years ago. I dont remember why this idea is still on our the shelves.

    you could easily use a start up script to create a tempLib` attribute:

    def _get_temp_lib(self):
        lib = getattr(self.naked(), "myCustomTempLib", None)
        if lib is None:
            lib = self.naked().myCustomTempLib = dict()
        return lib
    
    
    RFont.tempLib = property(_get_temp_lib)
    


  • Maybe python’s built-in id function could be helpful to you there. I have been using it in the past to distinguish fonts with identical names/paths/etc by putting them in a “fonts dictionary” where the id is the key to get the font object. But I can imagine your usage being more complex than this.


  • admin

    do not use fontParts objects to get an id... those are wrapper classes around fe defcon object

    # this will get different results
    f = CurrentFont()
    print(id(f))
    
    f = CurrentFont()
    print(id(f))
    
    # this will get the correct id
    f = CurrentFont().asDefcon()
    print(id(f))
    
    f = CurrentFont().asDefcon()
    print(id(f))
    


  • Thank you for the code, @frederik! Your tempLib alternative works well for me. I really appreciate it.

    The code sample to get the id of a font's defcon object is giving me an error however. (I'm using RF v3.5b)

    AttributeError: 'RFont' object has no attribute 'asDefcon'
    

    Replacing .asDefcon() with .naked() appears to work however. I'm not sure if that's safe to use because there's no documentation on RFont.naked(). What do you think, Frederik?

    @benedikt Thanks for letting me know about the id() function—great to know about in general!


  • admin

    .asDefcon() is a RoboFont 4 attribute

    And see https://github.com/robotools/defcon/pull/367 for an official defcon support



  • Thanks @frederik. Is it ok to use id( CurrentFont().naked() ) to maintain RoboFont 3 support?


  • admin

    sure <fontPartsObject>.naked() still exists

    only the latest has .asDefcon() and .asFontParts() which is just a lot more readable


Log in to reply