Important: You are required to change your passwords used for Gentoo services and set an email address for your Wiki account if you haven't done so. See the full announcement and Wiki email policy change for more information.

Fontconfig

From Gentoo Wiki
Jump to: navigation, search

The fontconfig library is intended to provide uniform font selection and configuration amongst all GUI applications. Even though it is common for various desktop environments to provide their own font overrides and configuration, still fontconfig is the underlying library.

Configuration

Note
Changes to fontconfig files will reflect only in applications started after the change!

Generic

fontconfig uses XML files in /etc/fonts/ directory to generate its internal configuration. By default it parses /etc/fonts/fonts.conf (users should not edit this file!) which sets some sane defaults and usually contains code to also parse /etc/fonts/conf.d/ content. In addition there is /etc/fonts/conf.avail/ directory that contains various possible configuration files that each cover some aspect of fontconfig. It's customary to symlink necessary files to /etc/fonts/conf.d/. These files are executed in order they are named, for this reason their names start with a two digit number with the first (tens) indicating class, that is, what the file affects.

Gentoo specific way

Gentoo ships an eselect module (eselect fontconfig) that does exactly what was described in generic way - it manages symlinks of files in /etc/fonts/conf.avail/ by adding or removing them from the /etc/fonts/conf.d/ directory. For obvious reasons changing system wide configuration requires appropriate permissions.

Listing available files

root # eselect fontconfig list
Available fontconfig .conf files (* is enabled):
  [1]   10-autohint.conf *
  [2]   10-no-sub-pixel.conf
  [3]   10-sub-pixel-bgr.conf
  [4]   10-sub-pixel-rgb.conf
  [5]   10-sub-pixel-vbgr.conf
  [6]   10-sub-pixel-vrgb.conf
  [7]   10-unhinted.conf
  [8]   11-lcdfilter-default.conf
  [9]   11-lcdfilter-legacy.conf
  ...
Warning
Different systems have different files in fonts.avail and therefore list number for one and that same file differ from system to system, never blindly use a list number from another source but your local list!

Enabling a file

Files can be enabled either by filename or by the number in brackets. These two do the same thing:

root # eselect fontconfig enable 10-sub-pixel-rgb.conf
root #
eselect fontconfig enable 4

Disabling a file

Files can be disabled likewise:

root # eselect fontconfig disable 10-sub-pixel-rgb.conf
root #
eselect fontconfig disable 4

Custom system wide configuration

Enable 51-local.conf and create /etc/fonts/local.conf (this is an XML file, you have been warned).

File/etc/fonts/local.confExample file that sets preferred font fallback order for sans-serif font

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <alias>
    <family>sans-serif</family>
    <prefer>
      <family>Ubuntu</family>
      <family>TakaoPGothic</family>
      <family>Droid Sans</family>
    </prefer>
    <default><family>DejaVu Sans</family></default>
  </alias>
</fontconfig>

Per-user configuration

Enable 50-user.conf (possibly enabled by default). And use ~/.fonts.conf (same format as local.conf)

Note
This is one way how a desktop environment might try to affect font rendering hence it might be prudent to disable this to be sure that what's being shown is actually system wide configuration when customizing it. Disabling it also makes font rendering more uniform across user accounts.

Checking configuration

You can check the default font replacement, for example for Arial, by typing:

user $ fc-match Arial

Anti-aliasing, hinting and sub-pixel rendering

Anti-aliasing is enabled by default and makes fonts less blocky. Hinting is an attempt to cope with the low pixel count per unit of area of current displays. Correct hinting makes characters more crisp but since font metrics aren't changed (and arguably should not change) affects how overall the rendered text looks like. Sub-pixel rendering uses the fact that LCD matrix has three primaries to effectively triple the resolution of text but can make characters appear not entirely black. To combat that lcdfilter is to be used with sub-pixel rendering (available for newer fontconfig) but it can blur the characters too much. In the end this entirely depends on person how they like their text.

Forcing hinting

The default fontconfig behaviour regarding hinting is unknown [personal note: what is this madness, how can one of the key components of Linux graphical stack be unknown?!] but it can be made deterministically sub-optimal by making a system wide default.

  1. First enable /etc/fonts/local.conf
  2. Edit the file to include full hinting by default
File/etc/fonts/local.confSetting hinting to full

<match target="font">
  <edit mode="assign" name="hintstyle">
    <const>hintfull</const>
  </edit>
</match>
Note
Most fonts look best with full hinting but others need slight or more rarely some other other hinting option. Some fonts for one reason or another will always be ugly.

Using sub-pixel rendering

It's important to determine the sub-pixel layout of the LCD matrix. It's usually RGB (10-sub-pixel-rgb.conf) but the only way to be sure is to either consult display specification or use this sub-pixel layout test to determine it. Once determined, enable the appropriate 10-sub-pixel-<matrix type>.conf file.

root # eselect fontconfig enable 10-sub-pixel-rgb.conf

It's strongly advised that lcdfilter, if available, is used with sub-pixel rendering. It comes in different varieties but the default (11-lcdfilter-default.conf) should be appropriate for all common fonts.

root # eselect fontconfig enable 11-lcdfilter-default.conf
Warning
The 'bindist' USE flag in media-libs/freetype disables sub-pixel rendering altogether due to potential patenting issues. Make sure to disable it for this package.

Regarding autohinter

Autohinter attempts to do automatic hinting disregarding any existing hinting information. Until recently it was the default because TrueType2 was covered by patents but now that they have expired there's very little reason to use it. From technical point of view it does better than broken or no hinting information but it will be strongly sub-optimal for fonts with good hinting information. Generally system fonts are of the second kind so autohinter should not be used.

Warning
Autohinter is not compatible with sub-pixel rendering, do not use the two together!

Infinality

Infinality is a somewhat controversial set of patches for FreeType and accompanying fontconfig files. The goal of Infinality is to provide higher quality font rendering including emulation of other OS font rendering styles. It's at best of beta quality but newer FreeType ebuilds have a USE flag for enabling Infinality. It should be kept in mind that some fonts can be rendered worse than with vanilla FreeType and furthermore Infinality is known for making applications crash under certain conditions.

Note
As usual with software still in development it is prudent to always use only the latest version available unless there are known problems with it.

Enabling Infinality

Enable the 'infinality' USE flag by editing make.conf either manually or by using euse tool.

root # euse -E infinality
Note
euse is part of app-portage/gentoolkit.

And then rebuilding affected programs.

root # emerge --ask -vuND @world

Unless messages shown after rebuild tell otherwise, enable 52-infinality.conf.

root # eselect fontconfig enable 52-infinality.conf

Configuring Infinality

Infinality can be configured in various ways including custom configurations but easiest is to use pre-made profiles shipped in media-libs/fontconfig-infinality that along with eselect modules should have been installed as dependencies of infinality USE flag.

Quick and reasonable config

root # eselect infinality set infinality
root # eselect lcdfilter set infinality
Note
As always, fontconfig changes affect only applications started after changes have been made.

It's also recommended to disable all fontconfig settings except for 52-infinality.conf as they interfere with the rendering of infinality styles:

root # eselect fontconfig disable <number>

Certain fontconfig settings do not interfere with how the fonts are rendered, but rather determine the default fonts for font-types (such as Monospace, sans-serif, etc.). Such settings (for example the 62-croscore-*.conf configurations) can be left enabled.

Explanation

Infinality provides FreeType environment variables that can be used to configure features introduced with its patches as well as fontconfig files to be used with these changes. Fontconfig file sets are administrated on Gentoo via eselect infinality while the FreeType behaviour is set using eselect lcdfilter.

These eselect modules function as usual. Listing available fontconfig file sets.

root # eselect infinality list

Listing available FreeType variable sets.

root # eselect lcdfilter list

Both sets should match of course except when there are multiple matches possible such as shown below.

root # eselect infinality set win7
root # eselect lcdfilter set windows-7-light

Picking fonts

Choosing the right font can be trickier than deciding on the right hinting type.For one reason or another fonts will not be perfect but it's certainly doable to make that same fonts look better than, say, Windows 7 default font configuration. The following information is biased but it can't be helped.

Font family Good Bad
DejaVu (media-fonts/dejavu) Many styles, covers a lot of code points Exceptionally wide - even condensed is wider than same height monospace (not to mention average sans-serif font), overall second to Verdana (an MS font) in width.
Droid (media-fonts/droid) Covers a lot of code points and scripts Very dry, wide yet thin glyphs is probably the most memorable aspect of Droid font family. Clearly designed with handheld devices and their small screens in mind.
Gentium Plus (media-fonts/sil-gentium) Fairly distinctive, might appeal to people who like narrow fonts Only serif, as with other SIL fonts hinting is questionable.
Liberation (media-fonts/liberation-fonts) MS TrueType core font metric compatible, overall look pretty decent Few fonts seem to have hinting trouble.
Ubuntu (media-fonts/ubuntu-font-family) A distinctive font with style which might not appeal to everyone, overall looks good as well as covering a fair bit of code points. Only default sans-serif is truly polished, narrow and monospaced versions are unfinished. No known serif font that would accompany it well.
URW (media-fonts/urw-fonts) Metric compatible with popular Adobe fonts (among others?) Seem to require slight hinting.
MS TrueType core fonts (media-fonts/corefonts) Includes most fonts used in documents or on web (as of 01.07.2012 Gentoo only has an old version without Tahoma) MS does not distribute them nowadays so the available fonts are from many years ago and might not reflect the current state of them. Obviously lacks fonts introduced later. Seem to require full hinting.
Unifont (media-fonts/unifont) Covers a lot of code points. In addition to being ugly as sin it also fails basic requirements to be considered a typeface - is it sans-serif, is it serif? Please never use this.

External resources