Changing the default fallback subsitution fonts in Linux

The default/fallback/substitution fonts are those used when an application or webpage doesn't specify a specific font or when a character glyph is not available in the selected font. When the CSS contains "font-family: sans", which font is that? Note: I'm using an Ubuntu-based distribution, but the details should apply anywhere fontconfig is used.

Tools:

  • Character Map(mucharmap) - Right click on any character to see the name of the source font.
  • fc-list - Lists available fonts
  • fc-match - Display a list of what fonts will be used for a specific named font. Example: fc-match -s "Liberation Sans"

Changing the defaults

The Linxu font configuration is stored in /etc/fonts/fonts.conf with additional enabled files in /etc/fonts/conf.d/. The files in /etc/fonts/conf.d/ are explained by the README file:

Each file in this directory is a fontconfig configuration file.  Fontconfig
scans this directory, loading all files of the form [0-9][0-9]*.conf.
These files are normally installed in /usr/share/fontconfig/conf.avail
and then symlinked here, allowing them to be easily installed and then
enabled/disabled by adjusting the symlinks.

The files are loaded in numeric order, the structure of the configuration
has led to the following conventions in usage:

 Files begining with:   Contain:

 00 through 09      Font directories
 10 through 19      system rendering defaults (AA, etc)
 20 through 29      font rendering options
 30 through 39      family substitution
 40 through 49      generic identification, map family->generic
 50 through 59      alternate config file loading
 60 through 69      generic aliases, map generic->family
 70 through 79      select font (adjust which fonts are available)
 80 through 89      match target="scan" (modify scanned patterns)
 90 through 99      font synthesis

Part of the directory listing:

user@server /etc/fonts/conf.d $ ls
10-antialias.conf                          64-23-tlwg-mono.conf
10-hinting.conf                            65-droid-sans-fallback.conf
10-hinting-slight.conf                     65-fonts-persian.conf
10-scale-bitmap-fonts.conf                 65-fonts-takao-pgothic.conf
11-lcdfilter-default.conf                  65-khmer.conf
20-unhint-small-dejavu-lgc-sans.conf       65-nonlatin.conf
20-unhint-small-dejavu-lgc-sans-mono.conf  65-wqy-microhei.conf
20-unhint-small-dejavu-lgc-serif.conf      69-language-selector-zh-cn.conf
20-unhint-small-dejavu-sans.conf           69-language-selector-zh-hk.conf
<snip>

Get to the point already!

Yes, Alright. The default, in this case, for sans-serif fonts is specified in the file 57-dejavu-sans.conf:

  <!-- Generic name aliasing -->
  <alias>
    <family>sans-serif</family>
    <prefer>
      <family>DejaVu Sans</family>
    </prefer>
  </alias>

Yes it is XML! Similar files exist for serif and monospaced fonts. The solution is to specify updated information in your user configuration or add a new file to change the option system-wide. Your user configuration goes in ~/.config/fontconfig/fonts.conf. An example changing the system defaults to use the Liberation font set:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- -->
<fontconfig>
  <!-- Generic name aliasing -->
  <alias>
    <family>sans-serif</family>
    <prefer>
      <family>Liberation Sans</family>
    </prefer>
  </alias>
  <alias>
    <family>serif</family>
    <prefer>
      <family>Liberation Serif</family>
    </prefer>
  </alias>
  <alias>
    <family>monospace</family>
    <prefer>
      <family>Liberation Mono</family>
    </prefer>
  </alias>
</fontconfig>

Save that file and update your configuration cache using fc-cache -fv and you're done.

External resources: