linuxbashfontstruetypefontconfig

Check whether a font is monospaced


I am creating a small (bash) script in Linux to convert monospaced fonts, and I want to return an error when a supplied font is not monospaced.

I have been looking at the fontconfig fc-query command, which has the spacing property, but a lot of times this property is not set (or I don't know how to retrieve it). Is there a better way to check whether a font is monospaced?

The fonts I am currently supporting are TrueType (.ttf) and X11 type (.pcf.gz, .pfb) fonts.


Solution

  • Off the top of my head:

    # script.py
    
    import sys
    import fontforge
    f = fontforge.open(sys.argv[1])
    i = f['i']
    m = f['m']
    
    if i.width == m.width:
        print('Monospace!')
    

    With the sys module you can pass command line arguments:

    $ python script.py path/to/font.ttf