How can I loop through all glyphs of a TTF or OTF font?



  • A while ago, I made a simple script for RoboFont + DrawBot which would step through all glyphs of an open font in order to export these to SVG files (useful for an icon font, in that case).

    However, it would be more convenient to run such a script outside of RoboFont, on a TTF/OTF font binary. Use cases for this general looping-through a font technique would probably include:

    • exporting an icon font to SVGs & PNGs
    • proofing every glyph in a font by printing it to a PDF

    Is this possible in DrawBot alone? Or should I import FontTools, e.g. to loop through the CMAP table? I’ll look into this a little further, and update if I find a way.

    Thanks for any tips!



  • Okay, so I’ve figured out the start of this. The core of it is this:

    from fontTools.ttLib import TTFont
    
    fontPath = "source/RecursiveMonoCslSt-Med.ttf"
    
    newPage(1000,1000)
    txt = FormattedString()
    
    with TTFont(fontPath) as f:
        for key, value in f["cmap"].getBestCmap().items():
            print(chr(key), end=" ")
            txt.append(chr(key) + " ")
    
    textBox(txt, (0, 0, 1000,1000))
    

    Run from a script outside of the DrawBot app, the fuller script looks like this so far:

    from fontTools.ttLib import TTFont
    from drawBot import *
    
    autoOpen = True
    
    fontPath = "source/RecursiveMonoCslSt-Med.ttf"
    
    W,H= 1000,1000
    fontSize = W/40
    
    newPage(W,H)
    
    txt = FormattedString()
    txt.font(fontPath)
    txt.fontSize(fontSize)
    txt.lineHeight(fontSize * 1.5)
    
    with TTFont(fontPath) as f:
        for key, value in f["cmap"].getBestCmap().items():
            print(chr(key), end=" ")
            txt.append(chr(key) + " ")
    
    textBox(txt, (0, 0, W, H))
    
    saveTo="test.pdf"
    saveImage(saveTo)
    
    if autoOpen:
        import os
        # os.system(f"open --background -a Preview {path}")
        os.system(f"open -a Preview {saveTo}")
    
    

    But this still prints out a very basic/ugly thing, for now.

    e3de853b-b7c9-4a7e-9579-2acad815dbbd-image.png



  • @arrowtype_2 said in How can I loop through all glyphs of a TTF or OTF font?:

    Okay, so I got a little further!

    This now includes a script that will output all glyphs of a font to SVG:

    https://github.com/arrowtype/drawbot--loop-through-font-glyphs

    The main caveat right now is that I just copied from the docs to center & size glyphs to the full W/H of the canvas. So, relative sizing is lost. If I had to use this for real, I would figure out how to scale things in one way that could work for all glyphs ... probably, I would find the min/max Y values and widths (values that are pre-recorded in fonts, actually), and use these instead of min/max values per glyph.


  • admin

    mmm, you need to use all the powers of drawBot!!

    pageSize = "A4"
    
    path = 'AdobeVFPrototype.ttf'
    
    border = 20
    
    txt = FormattedString(
        font=path,
        fontSize=20
    )
    txt.appendGlyph(*txt.listFontGlyphNames())
    
    while txt:
        newPage(pageSize)
        txt = textBox(txt, (border, border, width() - border*2, height() - border*2))
        
    


  • @frederik said in How can I loop through all glyphs of a TTF or OTF font?:

    pageSize = "A4"

    Amazing, thanks so much for the tips, @frederik!

    For anyone coming across this, there was a good discussion of this and of FontTools pens on Twitter:

    https://twitter.com/typemytype/status/1352985048639799297

    Also, I have cleaned up the several scripts in the GitHub repo. using Frederick’s & Just’s advice from both here & the Twitter thread. Have a look and remix as needed!

    https://github.com/arrowtype/drawbot--loop-through-font-glyphs


Log in to reply