A Condensed Monospaced Font

There are plenty of monospaced fonts that look pretty on the screen, but they don't work so well for a printed page. Typically, code wraps at 80 columns on the screen, but 12 point Courier on a printed page with reasonable margins wraps at about 60 columns. In this blog, I show how to use the scripting feature of FontForge to make a narrow code font.

When the time comes for my graduate students to write their project reports, I give them a long checklist of do's and don'ts. One of the more vexing issues is the code font. I am astonished how many people who have been programming for years are unaware that computer code is usually presented in a monospaced font, like this.

Of course, they soon discover that their word processor has a Courier (or Courier New) font for this purpose.

Next, they discover that Courier is awfully wide. If you write on letter size paper with an inch of margin on each side, and you use 12 point Courier , you can fit 65 characters on a line. That's not a lot. With 10 point Courier, that goes up to 78. But if you want to indent your displayed code by half an inch, you are back to 72 characters. And anyway, 10 point Courier looks awful when combined with 12 point Times.

At that point, I tell them that they should just use a condensed font. When they ask “which font”, I mumble something about Googling for it, but I don't recall anyone ever following through. Instead, they stick with Courier and tediously reformat their code to fit the printed page.

Why don't I just point them to a font? For my Sun Press Java books, I used a Lucida Typewriter Narrow font that Sun commissioned years ago. But I can't give my copy to others, and I can't find any source on the web. Googling for it[2] yields two hits (probably three by the time you read this).

Hans Dietrich put together a great survey of monospaced programming fonts[3], but his emphasis is on fonts that look good on the screen. A few of them are narrower than Courier, for example Inconsolata[4], Share-TechMono[5], and Crystal[6]. They are reasonable choices, but they still aren't as narrow as I would like.

Once or twice, I fussed with FontForge[7] to condense a font, but I found it tedious and never got to anything that I wanted to distribute to the world. But now I learned that FontForge has scripting support[8], with two interpreters, one for a home-grown programming language and one for Python. When I hear “home-grown programming language”, I run for the hills, so I chose the Python support instead.

It's easy enough to use. On Ubuntu, install fontforge and python-fontforge. Here is my script to condense a font to 75%:

I applied that to the DejaVu Sans Mono font, which you can get here[9]. The result was quite good. Below is a sample with Courier, Inconsolata, and DejaVu Sans Mono Condensed. In each case, I chose a point size that matches 12 point Times—see “The binding attribute” for the match in x-height. As you can see, the third code line wraps with the wider fonts.

If you want to experiment, you can find the font here[10], or you can just run the FontForge script yourself. I hope that this will motivate someone with more expertise in font design than myself to create a beautiful narrow code font.