Renaming the glyph problem



  • Hi,

    I'm renaming all glyphs in CurrentFont based on glyphNameFormatter.reader.u2n.

    To do that I'm using RFont.rename(oldname,newname) script. After a few months of using this code, I have the following throwback (so far I saw it only on one, healthy-looking ufo):

    File "unicodeStuff.py", line 57, in doit
      File "lib/fontObjects/fontPartsWrappers.pyc", line 1649, in renameGlyph
      File "lib/fontObjects/fontPartsWrappers.pyc", line 1318, in renameGlyph
      File "lib/fontObjects/doodleLayer.pyc", line 181, in renameGlyph
      File "/Applications/TypeDesign/RoboFont3.app/Contents/Resources/lib/python3.6/defcon/objects/layer.py", line 262, in __getitem__
      File "lib/fontObjects/doodleLayer.pyc", line 58, in loadGlyph
      File "/Applications/TypeDesign/RoboFont3.app/Contents/Resources/lib/python3.6/defcon/objects/layer.py", line 179, in loadGlyph
    KeyError: 'uni2611 not in layer'
    

    I was trying everything, I have rewritten the script with a different slightly approach over five times. It throws the same message, with around five different glyphs that throw an error (it depends on the version of my script).

    I cannot find the issue in the font.

    Before the running script, the glyph that causes the error exists in the font. Later it eighter completely disappear, or if I write its name in the search console, the RF crashes.

    Any suggestions?


  • admin

    do you have an example script that causes this issue?



  • ofcourse

    from glyphNameFormatter.reader import u2n, n2u, u2U
    def getNameFromUnicode(g):
        name = u2n(g.unicode)
        return name
        
    def doit(font):
        renamingDict = {}
        for glyph in font:
            old_name = glyph.name
            new_name = getNameFromUnicode(glyph)
    
            if new_name is None:
                continue
            else:
                if new_name not in renamingDict.keys():
                    if new_name != old_name:
                        renamingDict[old_name] = new_name
                    for glyph2 in font:
                        if glyph2.name.split(".")[0] == old_name:
                            old_name2 = glyph2.name
                            new_name2 = glyph2.name.replace(old_name,new_name)
                            if new_name2 not in renamingDict.keys():
                                if new_name2 != old_name2:
                                    renamingDict[old_name2] = new_name2
        reducedRenamingDictKeys = list(set(list(renamingDict.keys())))
        
        for old in reducedRenamingDictKeys:
            if old in font.keys():
                glyph = font[old]
                if len(glyph.contours) != 0:
                    font.renameGlyph(old,renamingDict[old])
                    for glyph2 in font:
                        for comp in glyph2.components:
                            if comp.baseGlyph == old:
                                comp.baseGlyph = renamingDict[old]
        for old in reducedRenamingDictKeys:
            
            glyph = font[old]
            if len(glyph.contours) == 0:
        
                font.renameGlyph(old,renamingDict[old])
                for glyph2 in font:
                    for comp in glyph2.components:
                        if comp.baseGlyph == old:
                            comp.baseGlyph = renamingDict[old]
        
    font = CurrentFont()
    doit(font)
    

  • admin

    When renaming a glyph (line 32) the old glyph name does not exist anymore (line 38).

    (side note: keys() from a dictionary are always unique, see line 25)


  • admin

    removing the renaming 'old' glyph name from your reducedRenamingDictKeys list, on the first rename, will help



  • shit, it was much easier than I thought,
    I should feel a bit ashamed. Thanks