How to add a new glyph to a font with FontTools?

Posts: 5
edited February 2 in Type Design Software

I'm trying to add a new glyph to a font. I managed to draw by glyph to a pen, then saved it to a glyph object with:

new_glyph = pen.glyph()

Now I would like to do something like:
font.getGlyphSet()["e_m"] = new_glyph
or (with a ttf font):

font["glyf"]["e_m"] = new_glyph
but I can't.

How to add a new glyph to a font with FontTools?

Thank you!

Tagged:

Welcome!

It looks like you're new here. Sign in or register to get started.

Comments

  • For anyone interested, the solution was to:

    1. create a new UFO;
    2. add the glyph to this UFO;
    3. compile this UFO to a font of the same type of the source font;
    4. merge the two fonts with fonttools.merge.
  • Posts: 1
    It turns out that you can do this directly in Python, the font object maintains a GlyphOrder and this must have the names of all glyphs; but you can set it with .setGlyphOrder. Here's a routine that i use:
    
    def AddExtras(font, extras):                                                        
        """Add extra glyphs to a font."""                                               
                                                                                        
        glyf = font["glyf"]                                                             
        hmtx = font["hmtx"]                                                             
                                                                                        
        go = list(font.getGlyphOrder())                                                 
        go += [glyph.name for glyph in extras]                                          
        font.setGlyphOrder(go)                                                          
        for glyph in extras:                                                            
            name = glyph.name                                                           
            glyf[name] = glyph                                                          
            hmtx[name] = glyph.width, glyph.lsb                                         
     
  • Glyph Copy Tool in DTL OTMaster

    Actually off-topic because the question was about FontTools, but maybe useful for some to know, is that OTM has a function to simply add glyphs, also from another font. For this the Glyph Copy Tool can be used. This works, of course, also for adding logos. In addition, there is a fully featured glyph editor. Moreover, if needed, the ‘Autohint all unhinted glyphs when saving font’ option for CFF fonts can be invoked via the preferences.

    Editing glyph details in DTL OTMaster

Welcome!

It looks like you're new here. Sign in or register to get started.

Welcome!

It looks like you're new here. Sign in or register to get started.