Thursday, July 5, 2012

Listing available monospace fonts in Emacs

After finding out that the Monospace font doesn't support italics, I set about trying to find a suitable replacement. This EmacsWiki page lists a way to print some example text in all of the fonts on the system, which is very helpful for comparing what's available. The problem with it is that it displays all fonts, not just the monospace fonts, which isn't useful. (For those of you not in the know, all programming is done with monospace or uniform-width fonts, meaning that l and m are the same size).

I modified the code so that it displays only monospace fonts. There's probably a property on the font family which one could check, but I used a much more primitive method: measure the pixel width of 5 consecutive lowercase L characters and 5 lowercase M characters. If they are the same, then the font is monospaced.

It isn't perfect (it has a couple of false positives, such as courier 10 pitch), but it's close enough to be a big improvement.

No comments:

Post a Comment