Skip to main content

A Condensed Monospaced Font

Posted by cayhorstmann on November 21, 2010 at 11:49 PM PST

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 fontforge and python-fontforge. Here is my script to condense a font to 75%:

import fontforge
import os
import psMat
import sys

inputFont = sys.argv[1]
outputFont = os.path.splitext(inputFont)[0] + ".otf"

font = fontforge.open(inputFont);
for x in font:
    font[x].transform(psMat.scale(0.75))
font.fontname=font.fontname + "Condensed"
font.familyname=font.familyname + " Condensed"
font.fullname=font.fullname + " Condensed"
font.generate(outputFont)

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.

Related Topics >>

Comments

A Condensed Monospaced Font

The free/open Bitstream fonts have 'vera sans mono' that you might want to experiment with.

A Condensed Monospaced Font

I like this post. Never thought of condensing a font.
By the way, I used that Su*~a O^ng Tho. when I was young and when it didn't have Chinese letters on it. So sad that Chinese is everywhere now :(

A Condensed Monospaced Font

Haha where did you get that condensed milk shot? It's funny because my parents put that stuff in their everyday's coffee (Vietnamese/French style milk coffee that is)

A Condensed Monospaced Font

I think that Cay is precisely in Vietnam at present time. And he's pulling out of his hat tons of fun and interesting stuff ;-) BTW, Courier is ugly too. One of the first thing that I do when I customize a brand new NetBeans installation is to change the font for the editor.