Preserve empty glyphs when applying defcon sort order through script



  • Re: Can I apply a "smart sort" for Glyph Order via script?

    I have a script to make suffixed (alternate) glyphs into non-suffixed ("default") glyphs.

    However, without a sorting step, this places the new "default" glyph at the end of the glyph order.

    When I apply a new glyph order, as suggested in the earlier thread posted above, it removes A) all template glyphs, and B) the /space glyph.

    This is quite disruptive to a UFO, and means that I keep deleting spaces.

    One workaround I can think of is:

    • save the current glyphOrder as a list
    • create a dictionary for empty glyphs
    • checking through glyphs of a font
      • if a glyph has no contours
        • save its data to the dictionary. unicodes, width, inclusion in groups, inclusion in kerning pairs, maybe more (?)
    • perform the glyph swap
    • apply the sort order
    • re-add the glyphs from the empty-glyphs dictionary
    • re-assert the saved glyphOrder, and add the new glyph at the end

    However, this feels a bit easy to mess up and possibly inefficient.

    So, this leaves me with two questions:

    1. Is there a way to cue the built-in "smart sort" feature from a script, as per the old, original question?

    2. If not, is there a smarter way to save and restore the empty glyphs?

    Thanks so much for any pointers!


  • admin

    hi @StephenNixon,

    try using f.templateGlyphOrder instead of f.glyphOrder for sorting:

    f = CurrentFont()
    newGlyphOrder = f.naked().unicodeData.sortGlyphNames(f.templateGlyphOrder, sortDescriptors=[dict(type="cannedDesign", ascending=True, allowPseudoUnicode=True)])
    f.templateGlyphOrder = newGlyphOrder
    

    see FontParts > RFont.templateGlyphOrder

    ps. there are other RFont attributes related to template glyphs:

    • f.templateGlyphs
    • f.templateSelectedGlyphNames
    • f.templateKeys


  • @gferreira said in Preserve empty glyphs when applying defcon sort order through script:

    newGlyphOrder = f.naked().unicodeData.sortGlyphNames(f.templateGlyphOrder, sortDescriptors=[dict(type="cannedDesign", ascending=True, allowPseudoUnicode=True)])
    f.templateGlyphOrder = newGlyphOrder
    

    Oh, interesting!

    Sure enough, changing glyphOrder to templateGlyphOrder fixed my script. It now works just as hoped. :)

    Thanks so much for point out that difference to me!