Better LCD Font Rendering

November 25, 2008 at 3:41 pm (Linux)

HeaderBetter font rendering in Linux has been a long time a ‘comin. The slow adoption of better font rendering in Linux has been because Apple and Microsoft have restrictive patents governing how they are rendered. Gradually though a few distro’s are implementing better font rendering that’s attempting to be non-patent restrictive. Ubuntu by default adds subpixel font rendering to Xft and cairo and a few other distro’s too. However for most other distributions, the lcd-specific FreeType API’s will need to be patched in.

First be sure that your xorg server DPI settings are correct for your monitor. Then you’ll need the updated and patched versions of freetype, fontconfig, libXft, and cairo.

Installing on Gentoo

For Gentoo users these updated packages are in the devnull overlay. Devnull uses mercurial:

emerge mercurial

To add an overlay, use layman to add it:

layman -a devnull

Read nico’s comment on preferred USE flags (Ubuntu’s) for best rendering. Then emerge these four programs.:

emerge -1 freetype fontconfig libXft cairo

Arch Install

For Arch I use the Ubuntu font rendering too. To install the Ubuntu font rendering do:

yaourt -S freetype2-ubuntu fontconfig-ubuntu cairo-ubuntu libxft-ubuntu

For other types, take a look at the Arch wiki page on Fonts.

Setting Up Fontconfig

Setting up fontconfig should be done as simple as possible. If you add too many options, fontconfig will use all of them and your fonts will become a mangled mess. Some font options are already set up in ‘/etc/fonts/conf.d’ and there more available in ‘/etc/fonts/conf.avail’. However, though you can choose to set up your font preferences here so that they are applied globally, I’ve discovered it’s better to create a local configuration (talked about below). If you want to set any of these options up, you’ll need to link them. For example, to replace Microsoft fonts with the Liberation fonts:

ln -s /etc/fonts/conf.avail/60-liberation.conf /etc/fonts/conf.d/

A fontconfig configuration file needs to be built to define how to render the fonts. This ideally shouldn’t be necessary as you should be able to set up most or all your settings by creating links to the fontconfig configuration directory. However, I’ve found some applications create a local configuration that can conflict with the global setting and create ugly fonts. Ok, there’s only one application I know that does this and that application is KDE’s System Settings. System Settings incorrectly sets the rgba value when creating the local font configuration and mangles how fonts should look. As of KDE 4.3.2 this bug is still here so try to avoid the Appearance > Fonts panel if you can ;). The local font configuration is a hidden file in the home directory called ‘.fonts.conf’ (~/.fonts.conf) and the one I provide here should work for most people:

<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>

<fontconfig>

 <!-- General Settings -->

 <match target="font" >
  <edit mode="assign" name="rgba" >
   <const>rgb</const>
  </edit>
 </match>

 <match target="font" >
  <edit mode="assign" name="hinting" >
   <bool>true</bool>
  </edit>
 </match>

 <match target="font" >
  <edit mode="assign" name="hintstyle" >
   <const>hintslight</const>
  </edit>
 </match>

 <match target="font" >
  <edit mode="assign" name="antialias" >
   <bool>true</bool>
  </edit>
 </match>

 <match target="pattern" >
  <edit mode="assign" name="autohint" >
   <bool>true</bool>
  </edit>
 </match>

 <match target="font" >
  <edit mode="assign" name="lcdfilter">
   <const>lcddefault</const>
  </edit>
 </match>

 <!-- set dpi -->
 <match target="pattern" >
  <edit mode="assign" name="dpi" >
   <double>88</double>
  </edit>
 </match>

 <!-- replace monoco font with monospace -->
 <!--
 <match target="pattern">
  <test qual="any" name="family" compare="eq">
   <string>Monaco</string>
  </test>
  <edit name="family" mode="prepend" binding="same">
   <string>Bitstream Vera Sans Mono</string>
  </edit>
 </match>
-->

 <!-- consolas and inconsolata appear fuzzy -->
 <match target="font">
  <test compare="eq" name="family">
   <string>Consolas</string>
  </test>
  <edit mode="assign" name="hintstyle">
   <const>hintmedium</const>
  </edit>
 </match>

 <match target="font">
  <test compare="eq" name="family">
   <string>Inconsolata</string>
  </test>
  <edit mode="assign" name="hintstyle">
   <const>hintslight</const>
  </edit>
 </match>

 <!-- hintmedium for small fonts, reduces fuzziness -->
 <!-- pretty sure this doesn't work -->
 <match target="font">
  <test compare="less_eq" name="pixelsize">
   <double>11</double>
  </test>
  <edit mode="assign" name="hintstyle">
   <const>hintslight</const>
  </edit>
 </match>

 <!-- calibri jaggedness -->
  <!-- http://forums.fedoraforum.org/showthread.php?p=1045807#post1045807 -->
 <match target="font" >
  <edit mode="assign" name="embeddedbitmap" >
   <bool>false</bool></edit>
 </match>

 <match target="font">
  <test compare="eq" name="family">
   <string>Arial</string>
  </test>
  <edit mode="assign" name="hinting">
   <bool>true</bool>
  </edit>
  <edit mode="assign" name="hintstyle">
   <const>hintfull</const>
  </edit>
  <edit mode="assign" name="autohint">
   <bool>true</bool>
  </edit>
  <test compare="less" name="weight">
   <const>medium</const>
  </test>
 </match>

 <match target="font">
  <test compare="eq" name="family">
   <string>Impact</string>
  </test>
  <edit mode="assign" name="hinting">
   <bool>true</bool>
  </edit>
  <edit mode="assign" name="hintstyle">
   <const>hintfull</const>
  </edit>
  <edit mode="assign" name="autohint">
   <bool>false</bool>
  </edit>
 </match>

</fontconfig>

Be sure you check your monitor’s subpixel type by looking at this page. You might also want to experiment with the hintslight, hintmedium, and hintfull options (though I’ve found hintslight to do odd things). In the configuration are also tweaks that make Microsoft fonts look good. After you have done this you should restart your xorg server. Technically font effects should take place immediately but applications that are already loaded (panel, window manager…) will need to be restarted to get the effect.

Again, keep it simple! You get too complicated and your fonts are going to blur.

A Note About Desktops

Desktops have previously handled font rendering and can over-ride fontconfig configuration. KDE 4 has the ability to let fontconfig handle font configuration in System Settings > Appearance > Fonts – choose ‘System Settings’ for anti-aliasing and disable DPI. In Gnome there is only one DPI option that should be picked up (in Preferences > Appearance > Fonts). If your DPI doesn’t match horizontally and vertically pick a median in between.

15 Comments

  1. Sebastian said,

    >> Let’s get going. I’d like to thank bi3l who has been building the ebuilds, and also to whomever runs the devnull overlay.

    heh, you’re welcome

  2. nico said,

    Nice article.
    @Sebastian: Thanks for the feedback (I’m from devnull overlay :)

  3. cheap lcd said,

    Nice article. Am sure will help many people get the crsytal clear viewing that lcd’s are meant to give

  4. Dirk Gently said,

    Bah! Post updated.

  5. nico said,

    We from devnull overlay stopped using IUSE defaults in the font ebuilds. There are 2 useflags which affect font rendering now – cleartype and ubuntu. I suggest you to update your post that it says to use the cleartype useflag (1. this is what portage uses 2. ubuntu one does not work for me for some reason, but others report it works well). I would say that the ubuntu useflag is for more experienced users.

  6. Dirk Gently said,

    Danke nico. Good tip, thanks for adding it.

  7. Josh Enders said,

    I don’t quite follow what Nico is pointing out. I’ve added the devnull overlay but when I attempted to `emerge freetype fontconfig libXft cairo` I was greeted with this lovely error message that I don’t quite grok:

    emerge: there are no ebuilds built with USE flags to satisfy “>=media-libs/freetype-2.2.1[cleartype,-ubuntu]“.
    !!! One of the following packages is required to complete your request:
    - media-libs/freetype-2.3.8 (Missing IUSE: cleartype ubuntu)
    - media-libs/freetype-2.3.7-r1 (Missing IUSE: cleartype ubuntu)
    - media-libs/freetype-2.3.7 (Missing IUSE: cleartype ubuntu)
    (dependency required by “media-libs/fontconfig-2.6.0-r2″ [ebuild])
    (dependency required by “fontconfig” [argument])

    I don’t mean to be asking for support here but if it’s a problem with a recent update or the guide, just thought I’d let you know…

  8. Josh Enders said,

    Solved it.

    I reread it a few times and did some further reading about ebuilds. I’m new to overlays and didn’t realize that make.conf and package.use flags effected overlay’d ebuilds.

    The obvious solution was to unmask and add the “cleartype” flag as seen below.

    # echo media\-libs\/freetype ~x86
    # echo media\-libs\/freetype cleartype >> /etc/portage/package.use

  9. Gen2ly said,

    Yep, that’s exactly how you do it.

  10. Josh Enders said,

    Ack, I must have been exhausted when I wrote that, the correct solution is:

    # echo media-libs/freetype ~x86 >> /etc/portage/package.keywords
    # echo media-libs/freetype cleartype >> /etc/portage/package.use

  11. Gen2ly said,

    Gen2ly facepalms.

    heh, i missed that first part.

  12. Prateek Dayal said,

    It should be emerge mercurial and not emerge mecurial (there is a typo above)

    Thanks!
    Prateek

  13. Gen2ly said,

    Ahhhhh, yes. Good catch Prateek. Fixed.

  14. Todd Partridge said,

  15. trueentrepreneur said,

    Todd Patridge……. great note buddy that helps out

Post a Comment