A Condensed Monospaced 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 yields two hits (probably three by the time you read this).
Hans Dietrich put together a great survey of monospaced programming fonts, but his emphasis is on fonts that look good on the screen. A few of them are narrower than Courier, for example Inconsolata, Share-TechMono, and Crystal. They are reasonable choices, but they still aren't as narrow as I would like.
Once or twice, I fussed with FontForge 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, 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
python-fontforge. Here is my script to condense a font to 75%:
inputFont = sys.argv
outputFont = os.path.splitext(inputFont) + ".otf"
font = fontforge.open(inputFont);
for x in font:
font.fontname=font.fontname + "Condensed"
font.familyname=font.familyname + " Condensed"
font.fullname=font.fullname + " Condensed"
I applied that to the DejaVu Sans Mono font, which you can get here. 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, 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.