SOLVED Copying Glyph names from one UFO to other



  • I have 2 UFOs with the same glyph sets but with different glyph names. How do I copy the glyph names from one UFO and change the corresponding Glyph names on the other? Is there an extension for this?


  • admin

    How do you want to "identify" the same glyph with different naming? by unicode?

    I guess the easiest todo is to set up a map of glyph names to change...

    # map the old name to a new name
    glyphNameRenameMap = {
        "a": "a.new",
        "b": "b.new",
    }
    
    # get the current font
    font = CurrentFont()
    # start loop over all glyph names to rename
    for oldName, newName in glyphNameRenameMap.items():
        font.renameGlyph(oldName, newName, renameComponents=True, renameGroups=True, renameKerning=True)
    


  • If your glyph sets are identical, and the corresponding glyphs have the same index, you could work with this script.

    This script expects two UFOs. The CurrentFont is your source font (has the glyph name you want), the other font is your target font (where you want to change the glyph names).

    # Copy glyph names from current font to other font based on index only
    
    srcFont = CurrentFont()
    fonts = AllFonts()
    
    # Open 2 UFOs
    if len(fonts) == 2:
        # Source font (srcFont) is current font
        # Target font (trgtFont) is other font
        for f in fonts:
            if f != srcFont:
                trgtFont = f
    
        # collecting old and new names of glyphs from
        # srcFont and trgtFont using the index of the glyph order.
        oldNameToNewName = {}
        for gNameNew in srcFont.glyphOrder:
            gNameNewIndex = srcFont.glyphOrder.index(gNameNew)
            gNameOld = trgtFont.glyphOrder[gNameNewIndex]
            # If name of glyph in srcFont and trgtFont with the same index are 
            # not the same store their names in the dictionary oldNameToNewName
            if gNameOld != gNameNew:
                oldNameToNewName[gNameNew] = gNameOld
        for newName, oldName in oldNameToNewName.items():
            print("# changing: "+oldName+" → "+newName)
            trgtFont.renameGlyph(oldName, newName, renameComponents=True, renameGroups=True, renameKerning=True)
    else:
        print("# Open two UFOs")
    


  • Thank you @frederik @Martin


Log in to reply