A Comprehensive Guide to Getting Microsoft Fonts Right in Ubuntu

March 22nd, 2009 § 1

While the default fonts in Ubuntu may look nice, they differ from the default fonts in Windows in an obvious way: their big size. This applies to other aspects of the GUI in Ubuntu aswell, but I will discuss that in another post. Since having Microsoft fonts in your Ubuntu installation is an advantage anyway, I thought I might aswell use them as my system fonts.

There is a problem, however, and that is the hassle to get the looks of those fonts right. After you figured it all out, which can take a while if you are on your own, it is actually quite simple. But like every “fix” for Ubuntu, it takes a lot of time learning what is causing your problems and finding a solution for it.

How it’s done

There are two steps involved to make it happen:

  1. set the system to use Microsoft fonts as the default system fonts
  2. make them look good in every application you use, from native Gnome applications to native KDE applications to QT applications and GTK applications.

The first step is done in a jiffy and a how-to is found quickly on the internet. The second step, however, while swiftly performed, was a pain in the ass to figure out completely, because of the variety of libraries a GUI can be built for.

First Step

This is done in the menu System which is located on the upper panel, then Preferences and finally Appearance.

Set every font and its size to your liking.

System - Preferences - Appearance - Fonts

Second Step

This step is about setting the rendering options for fonts to the correct values. While these options can be set in the window used in step 1, some applications refuse to use those set options and decide to use some default preset values which may differ from the values set in the options window. The clue is to make those applications use the font rendering settings that you chose. “Where do those applications get their settings from, if not from the gnome font options window?”, I hear you ask yourself. Well, there are multiple alternative sources to read font rendering settings from, which are all file based. The files in question are ~/.fonts.conf, ~/.Xresources and /etc/fonts/local.conf and are each the default settings sources for different sets of applications. Mozilla Firefox and OpenOffice.org for example both first rely on the file ~/.Xresources, while qt3 and qt4 applications rely on the ~/.fonts.conf and /etc/fonts/local.conf files.

Let’s start off with the most intuitive source of font rendering settings: the gnome appearance preferences window.

Appearance Preferences

In the same window as in the First Step, you have to set the font rendering options to the correct values, which is done selecting Best Contrast in the lower area of the window. More specific changes can be made by clicking the Details button. This opens the window shown below.

System - Preferences - Appearance - Fonts - Font Rendering Details

Since it doesn’t really matter you can set font Smoothing to Subpixel or Greyscale, whichever looks best for you. If you selected Best Contrast in the previous window, then Smoothing will be set to Greyscale.

The difference between the two settings is that Greyscale smoothing applies anti-aliasing to the fonts by adding grey coloured pixels to the edges of the fonts. Subpixel smoothing, on the other hand, adds coloured pixels to the edges of the fonts. Which colours depends on the Subpixel Order, which can be set in the same window. Depending on the screen you use, this will give an optimal or a sub-optimal result. Most screens have their subpixels arranged in the order RGB, which means Red Green Blue. Thus when Subpixel Order is set to RGB, then Subpixel smoothing will result in blueish coloured pixels around the left edge and reddish coloured pixels around the right edge of the fonts. While those colours might seem odd when used to smoothen the edges of black coloured fonts, the colours don’t become visible since a subpixel is too small to distinguish with the naked eye from a reasonable distance.

The last setting named Hinting, however, proves to be much more important, since it can make or break the looks of the classic Microsoft fonts like Arial, Times New Roman, Verdana, etc.

~/.fonts.conf & /etc/fonts/local.conf

These files offer an XML based structure for your font rendering settings. Since it is our intention to copy the settings from the Gnome Appearance Preferences window as much as possible, I will just give you the contents of the file I put together:

<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
 <match target="font" >
  <edit mode="assign" name="autohint" >
   <bool>false</bool>
  </edit>
  <edit mode="assign" name="rgba" >
   <const>rgb</const>
  </edit>
  <edit mode="assign" name="hinting" >
   <bool>true</bool>
  </edit>
  <edit mode="assign" name="hintstyle" >
   <const>hintfull</const>
  </edit>
  <edit mode="assign" name="antialias" >
   <bool>true</bool>
  </edit>
  <edit mode="assign" name="lcdfilter" >
   <const>lcddefault</const>
  </edit>
 </match>
 <match target="font" >
  <test compare="eq" name="family" qual="any" >
   <string>Calibri</string>
   <string>Cambria</string>
   <string>Candara</string>
   <string>Corbel</string>
   <string>Constantia</string>
   <string>Consolas</string>
  </test>
  <edit mode="assign" name="embeddedbitmap" >
   <bool>false</bool>
  </edit>
 </match>
 <match target="font" >
  <test compare="eq" name="family" qual="any" >
   <string>FreeSans</string>
   <string>FreeSerif</string>
  </test>
  <edit mode="assign" name="hintstyle" >
   <const>hintslight</const>
  </edit>
 </match>
</fontconfig>

The first “match” element is used for default settings, while the next two “match” elements are used to further specify or alter settings depending on the outcome of the test, of which the conditions are declared by the “test” element.

As you can see, the second “match” element tests if the font currently treated is one of the new Microsoft Office 2007 fonts. These fonts have broken bitmap rendering which results in non-smoothed fonts, so it is necessary to use an alternate rendering technique in order to avoid this. Too see for yourself: install the needed MS Office 2007 fonts and use them in an OpenOffice.org document with the second “match” element excluded from your ~/.fonts.conf file.

~/.Xresources

The same is true for the ~/.Xresources file. Just a reiteration of the font rendering settings mentioned previously.

Xft.antialias:  true
Xft.hinting:    true
Xft.hintstyle:  hintfull
Xft.lcdfilter:  lcddefault
Xft.rgba:       rgb

History and background of my personal conquest

I have been doing a lot of research on how Ubuntu fonts are actually used by programs. It turns out there are four major sources of settings and each one is used by a different group of applications. For example: Mozilla Firefox and OpenOffice.org use the ~/.Xresources file, while other applications (most GTK and QTx applications) look at the ~/.fonts.conf file or the /etc/fonts/local.conf file if the gnome appearance settings are ignored.

Another thing I noticed is that while some fonts (like the Free font series) look horrible when font-hinting is set to medium or full, Microsoft fonts (e.g. from the msttcorefonts package) tend to look aweful when font-hinting is set to slight. I used FreeSans as my main system font since Ubuntu 8.04, but I always loathed the looks of the Microsoft fonts at slight hinting. Unaware of the cause of the uglyness of the Microsoft fonts, I blamed Ubuntu and its maintainers for letting a regression of this magnitude happen.

Lukily I have it all figured out and I’m happy to share this knowledge with everyone who is interested.

Further investigation

In the (hopefully near) future I will cover the behavior of certain widely used applications and fonts in relation to all possible font rendering settings and sources, which will uncover the great mysteries of font rendering in Ubuntu and GNU/Linux in general. But for now this is all you get from me.