Best way to remove glyph from RFont?


  • admin

    @StephenNixon ✔︎ docs updated. thanks for your comments!



  • @frederik Awesome, thanks!



  • @frederik

    also remove them from font.glyphOrder

    Strangely, my script is able to remove most of the glyphs I ask it to, but it misses a few in glyphOrder. Can you see what I'm doing wrong here?

    (The script below removes all lowercase glyphs in a font, but misses b d h n p r u v w lslash)

    f = CurrentFont()
    
    # copy space-separated glyph names here
    glyphsToRemove = "a agrave aacute acircumflex atilde adieresis aring amacron abreve aogonek b c ccedilla cacute ccircumflex cdotaccent ccaron d dcaron e egrave eacute ecircumflex edieresis emacron ebreve edotaccent eogonek ecaron f g gcircumflex gbreve gdotaccent gcommaaccent h hcircumflex i igrave iacute icircumflex idieresis itilde imacron ibreve iogonek j jcircumflex k kcommaaccent l lacute lcommaaccent lcaron m n ntilde nacute ncommaaccent ncaron o ograve oacute ocircumflex otilde odieresis omacron obreve ohungarumlaut p q r racute rcommaaccent rcaron s sacute scircumflex scedilla scaron scommaaccent t tcommaaccent tcaron u ugrave uacute ucircumflex udieresis utilde umacron ubreve uring uhungarumlaut uogonek v w wcircumflex wgrave wacute wdieresis x y yacute ydieresis ycircumflex ygrave z zacute zdotaccent zcaron ae eth oslash thorn dcroat hbar ij ldot lslash eng oe tbar"
    
    # clean up the rest of the data
    for glyphToRemove in glyphsToRemove.split(" "):
    
        # remove from keys
        if glyphToRemove in f.keys():
            del f[glyphToRemove]
    
        # remove from glyphOrder 
        for glyphName in f.glyphOrder:
            if glyphName == glyphToRemove:
                del f.glyphOrder[glyphName]
    
        # GROUPS ------------------------------------------------------------
    
        # iterate over all groups in the font
        for groupName in f.groups.keys():
    
            # get the group
            group = f.groups[groupName]
            groupList = list(f.groups[groupName])
    
            # if glyph is in the group, remove it
            if glyphToRemove in group:
                print('removing %s from group %s...' % (glyphToRemove, groupName))
                groupList.remove(glyphToRemove)
                f.groups[groupName] = tuple(groupList)
    
        # KERNING -----------------------------------------------------------
    
        # iterate over all kerning pairs in the font
        for kerningPair in f.kerning.keys():
    
            # if glyph is in the kerning pair, remove it
            if glyphToRemove in kerningPair:
                print('removing kerning pair (%s, %s)...' % kerningPair)
                del f.kerning[kerningPair]
    
        # COMPONENTS --------------------------------------------------------
    
        # iterate over all glyphs in the font
        for glyph in f:
    
            # skip glyphs which don’t have components
            if not glyph.components:
                continue
    
            # iterate over all components in glyph
            for component in glyph.components:
    
                # if the base glyph is the glyph to be removed
                if component.baseGlyph == glyphToRemove:
                    # delete the component
                    glyph.removeComponent(component)
    
    


  • Actually, hmm. I'm finding that the script in the docs for removing-glyphs-from-kerning doesn't seem to be removing kern pairs.

    For example, this is failing to remove data around the a kerning:

    font = CurrentFont()
    
    # the glyph to be removed
    glyphName = 'a'
    
    # iterate over all kerning pairs in the font
    for kerningPair in font.kerning.keys():
    
        # if the glyph is in the kerning pair:
        if glyphName in kerningPair:
    
            # remove the kerning pair
            print('removing kerning pair (%s, %s)...' % kerningPair)
            del font.kerning[kerningPair]
    

    Even after running that, I still have all of the kern1 and kern2 data from a in the UFO.


  • admin

    don’t change the font.glyphOrder inside a loop, mainly as the glyphOrder is not the actual list you are editing but a copy of it.

    glyphOrder = font.glyphOrder
    
    for glyphName in glyphsToRemove.split:
    
        if glyphName in glyphOrder:
            glyphOrder.remove(glyphName)
    
    font.glyphOrder = glyphOrder
    

  • admin

    and I cannot reproduce the kerning issue...
    are you sure the 'a' is not in some group kerning?



  • @StephenNixon said in Best way to remove glyph from RFont?:

    if glyphToRemove in f.keys():
            del f[glyphToRemove]
    

    Thanks for the earlier feedback, @frederik! I think I must have figured out my kerning issue, but it may have been with someone's internal tool, back in March ... I should have documented that, here.

    I do still have a remaining issue, however. Even with the suggested edits, there are still some holdout glyphs in my font that I can't seem to remove. They appear to go away when I run the script, but when I restart RoboFont and open my font, they're back.

    Here's my current script, and here's a permalink to its exact current state.

    From what I can tell, these holdout glyphs are hiding in the layer "overlap". Is my script missing something necessary to delete a selected glyph in all layers?

    f8747140-d563-44d6-bf31-5989868fcc3b-image.png

    Part of the problem might be that selectedGlyphNames isn't returning all of the selected template glyphs, as shown here:

    cceba5af-1139-4bab-b007-cbb2e31e040a-image.png

    However, even the glyph names that selectedGlyphNames does return are ghosts that keep coming back when I close and reopen the UFO.

    What might I do to really get rid of them?


  • admin

    hi @StephenNixon,

    Is my script missing something necessary to delete a selected glyph in all layers?

    yes – as it turns out, to remove a glyph from all layers we need to remove the glyph from all layers :)

    for layerName in f.layerOrder:
        layer = f.getLayer(layerName)
        for glyphToRemove in glyphsToRemove:
            if glyphToRemove in layer:
                del layer[glyphToRemove]
            else:
                print("%s does not contain a glyph named '%s'" % (layerName, glyphToRemove))
    

    this should work, please give it a try.


    Part of the problem might be that selectedGlyphNames isn't returning all of the selected template glyphs, as shown here:

    cceba5af-1139-4bab-b007-cbb2e31e040a-image.png

    this is a tricky one, I understand why you are confused:

    the image is showing some glyphs which don’t exist in the default layer (because they were deleted by your script), but which do exist in other layers (because the script did not delete them).

    so, the glyph cells are showing a template glyph in the default layer overlaid over existing glyphs in other layers. and because the glyph cells are at an intermediate size, the glyph shapes from other layers are not being displayed, only their widths are. (edit: the small L icon at the bottom right also indicate that these glyphs have layers)

    if you make the glyph cells bigger, you will be able to see the other layers in the background:

    Screen Shot 2019-07-29 at 21.33.04.png

    I hope this makes sense… I’ll do my best to improve the docs about removing glyphs and template glyphs.

    thanks for your comments!


  • admin

    ps.

    as it turns out, to remove a glyph from all layers we need to remove the glyph from all layers

    this behaviour is new in RF3, because of its new layers model.

    in RF1, layers were bound to glyphs, so removing a glyph would remove all layers in that glyph.

    in RF3, however, layers are independent glyph sets, so removing a glyph from the default layer does not automatically remove it from the other layers.



  • @gferreira said in Best way to remove glyph from RFont?:

    for layerName in f.layerOrder:
        layer = f.getLayer(layerName)
        for glyphToRemove in glyphsToRemove:
            if glyphToRemove in layer:
                del layer[glyphToRemove]
            else:
                print("%s does not contain a glyph named '%s'" % (layerName, glyphToRemove))
    

    Ahhh this makes a lot of sense now, but it's helpful to see the specific way to do it.

    Maybe it would be worth adding this to the docs, here?

    https://robofont.com/documentation/how-tos/adding-and-removing-glyphs/?highlight=remove glyphs#removing-glyphs-1

    Thanks so much for the help!