Using the Monaco font in gvim on Linux

I wrote a previous tip about how to use Monaco while running an xterm on OSX’s X server. Here’s a similar tip, except running on Linux this time.
I like Monaco for it’s legibility at small screen sizes, so I wanted to use it in my gvim session.
The first step is to copy the font file over to your ~/.fonts directory. On a Mac system, you can find it under /System/Library/Fonts, called Monaco.dfont. The, run fc-cache to reload the fontconfig cache.
At this point, the font should already show up for any newly started GTK2/fontconfig-based program. You should also be able to say xterm -fa 'Monaco' to launch an xterm with the new font.
In gvim, you should also be able to select the font if you run the command :set guifont=* and select it from the menu.
Now here’s the more complicated part. If you’re fine with the antialiased version of the font, then stop here. If you want the pretty pixel-size based fonts, read on.
Modern linux systems, like Ubuntu, try to be really clever about your screen DPI. They know how big your monitor is, and what your resolution is set to, so they set calculate and store a DPI setting in the X server. The fontconfig and friends uses this information to map point sizes (what your apps ask for) to pixel sizes (what most hinted and bitmap fonts care about).
During this Process, if your DPI value is not one of the standard ones (i.e. 72), then trying to guess the point size that maps to a particular pixel size can be difficult. It could be 8.333333 or 8.3344 or some other very precise value (though fontconfig appears to be somewhat lenient)
Some programs let you control very precisely what parameters are asked of fontconfig. For example, for xterm, you can say xterm -fa 'Monaco:pixelsize=10' to get exactly the size that you want (change the value of pixelsize to see the different sizes). You can also say xterm -fa 'Monaco:pixelsize=10:antialias=off' to get exactly what I was looking for.
Ok, great, so how do we make gvim ask for this exact font. Well it turns out that gvim seems to hide the details of the font system from you, so only lets you specify things like :set guifont=Monaco\ 9 where the numeric value is the size of the font in point size. There’s no way (as far as I could tell) to tell it to ask for a particular pixel size.
So what now? It’s .fonts.conf to the rescue.
Here’s the code I added:

<match target="pattern">
<test name="family">
<string>Monaco10px</string>
</test>
<edit name="family">
<string>Monaco</string>
</edit>
<edit name="antialias">
<bool>false</bool>
</edit>
<edit name="pixelsize">
<int>10</int>
</edit>
</match>

What does it mean? Well it looks for an application to request a font pattern with family ‘Monaco10px’ (which actually doesn’t exist). When that pattern comes along, it secretly munges the pattern so that the “family” is now “Monaco”, and it explicitly sets the pixelsize value to 10. So now an application just needs to ask for the “Monaco10px” font, and it will always get this exact font.
Back in gvim, now I can finish up by adding the line set guifont=Monaco10px to my .vimrc
And one final perfectionist touch. In the mac terminal, each line is spaced by one pixel. This is not done by default in gvim, but you can add this by adding set linespace=1. The end result is something that looks very close to what you get in a Mac Terminal.App.
One final caveat: Unfortunately, this trick only works when you can tell an application exactly what font family to request (usually by typing it in somehow). On apps that only give you access to the font picker dialog, Monaco10px will not show up as a candidiate, because it’s not a real font.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: