Guide to X11/Fonts

From Wikibooks, open books for an open world
Jump to: navigation, search

Authors · History · Print · License

Introduction · Configuring · Building · Starting Sessions · Starting Programs · Fonts · Window Managers · Xt Clients · Political History · References and Links

Supplements: Commands

Edit this template

Fonts[edit]

When your X11 clients draw text, they use fonts, which are drawings of standard characters, such as letters, numerals, and punctuation. At minimum, a font is a typeface (also called family) such as Bitstream Vera Sans, Luxi Mono, Nimbus Roman No9 L, or fixed. More specifically, a font includes a size or style, such as Bitstream Vera Sans Bold Italic 10 point. Fonts also come in three categories called serif (like Nimbus Roman No9 L), sans-serif (like Bitstream Vera Sans), and monospace (like Luxi Mono or "fixed").

Fonts come in several font formats such as bitmap, TrueType, and PostScript. TrueType fonts are .ttf files. If the X11 server and client are at different computers, then you might have installed different fonts at each computer.

Core versus Xft fonts[edit]

X11 clients can draw texts in several different manners:

  1. Use the original core X11 protocol to draw text. With this approach, the X11 server loads and stores each character of a font. Thus, if the X11 server (with the screen and keyboard) and the X11 client (with your web browser or other window) are at different computers, then you must install fonts at the server. The server converts the characters into bitmaps and draws them upright, which means that you cannot rotate them. Further, the server never antialiases the fonts.
  2. Use the Xft library and RENDER extension to draw text. With this approach, the X11 client loads and stores each character of a font. You must install fonts at the client machine. The client converts the characters into geometric shapes. It decomposes the shapes into basic shapes such as triangles and trapezoids, then sends these to the server. The server draws the shapes and optionally provides antialiasing, which smooths the appearance of fonts. One can also rotate these shapes.
  3. Use a client-side library such as libart_lgpl or SDL_ttf to draw text. While libart_lgpl is a vector-graphics library, SDL_ttf is a library to draw text for programs that use the cross-platform Simple DirectMedia Layer. Typically, these libraries load the geometric shapes, then draw them into bitmaps, possibly antialiased and rotated, before sending them to the X11 server, as if the X11 server knew nothing about text. It is also possible for these libraries to do as Xft does and use RENDER.

The first X11 clients used the core X11 protocol to draw text, as that was the only choice. However, several clients now use Xft. Because GTK+ and Qt, the toolkits behind several applications including all GNOME and KDE applications, switched to Xft, many programs on most desktops, including Konqueror, now use Xft.

Configuring Xft with fontconfig[edit]

The older Xft version 1 had its own configuration scheme. Version 2, described here, uses fontconfig for its configuration. Because fontconfig is independent of X11, there are programs like fc-list which list fonts available to X11 programs like Konqueror, but do not themselves use X11. An easy terminal command to list available fonts is:

$ fc-list | sort | less

The default /etc/fonts/fonts.conf allows you to optionally create a custom file ~/.fonts.conf for your custom font settings.

Some customizations include:

  1. You can add additional directories that contain fonts. Fontconfig searches directories recursively. If you have a folder with .ttf files in it, then you can simply add it to ~/.fonts.conf; you do not need to write a fonts.scale or any other such file for fontconfig.
  2. You can specify additional fonts to search for obscure characters. If you do not see a right-arrow between these quotes "→" then the right-arrow character was not found during the search for characters to draw this sentence. In fact, the Bitstream and Luxi fonts included with Xorg and the Nimbus fonts from ghostscript do not have that character, but the "Standard Symbols L" font from ghostscript includes a right arrow.

However, ~/.fonts.conf uses a strict XML syntax. You must declare <?xml version="1.0"?> at the beginning of the file, and you must put everything else in the file between <fontconfig> and </fontconfig> tags, else it will not work.

Here is an example file, showing how to perform the two customizations:

<?xml version="1.0"?><fontconfig>
 <dir>/usr/local/share/ghostscript/fonts</dir>
 <alias>
  <family>sans-serif</family>
  <prefer><family>Standard Symbols L</family></prefer>
 </alias>
 <alias>
  <family>serif</family>
  <prefer>
   <family>Standard Symbols L</family>
  </prefer>
 </alias>
 <alias>
  <family>monospace</family>
  <prefer>
   <family>Standard Symbols L</family>
  </prefer>
 </alias>
</fontconfig>

Adding a directory of fonts is simple: use <dir>/full/path</dir>. The above example adds /usr/local/share/ghostscript/fonts, which at some computers is the location of Ghostscript fonts including Standard Symbols L.

To add Standard Symbols L to the list of preferred fonts, so that we can always draw the right-arrow "→", is more difficult. Suppose that you had set the Konqueror web browser to use Luxi Serif to display web pages. You then had loaded some text including some right-arrows. When fontconfig failed to find a right-arrow in the Luxi Serif font, it resorted to the generic font name "serif", which is actually an alias for a list of preferred serif fonts.

For reference, here are the font aliases from the /etc/fonts/fonts.conf included with Xorg 6.9rc1.

<!--
  Provide required aliases for standard names
-->
        <alias>
                <family>serif</family>
                <prefer>
                        <family>Bitstream Vera Serif</family>
                        <family>Times New Roman</family>
                        <family>Thorndale AMT</family>
                        <family>Luxi Serif</family>
                        <family>Nimbus Roman No9 L</family>
                        <family>Times</family>
                        <family>Frank Ruehl</family>
                        <family>Kochi Mincho</family>
                        <family>AR PL SungtiL GB</family>
                        <family>AR PL Mingti2L Big5</family>
                        <family>MS 明朝</family>
                        <family>Baekmuk Batang</family>
                        <family>FreeSerif</family>
                </prefer>
        </alias>
        <alias>
                <family>sans-serif</family>
                <prefer>
                        <family>Bitstream Vera Sans</family>
                        <family>Verdana</family>
                        <family>Arial</family>
                        <family>Albany AMT</family>
                        <family>Luxi Sans</family>
                        <family>Nimbus Sans L</family>
                        <family>Helvetica</family>
                        <family>Nachlieli</family>
                        <family>Kochi Gothic</family>
                        <family>AR PL KaitiM GB</family>
                        <family>AR PL KaitiM Big5</family>
                        <family>MS ゴシック</family>
                        <family>Baekmuk Dotum</family>
                        <family>SimSun</family>
                        <family>FreeSans</family>
                </prefer>
        </alias>
        <alias>
                <family>monospace</family>
                <prefer>
                        <family>Bitstream Vera Sans Mono</family>
                        <family>Andale Mono</family>
                        <family>Courier New</family>
                        <family>Cumberland AMT</family>
                        <family>Luxi Mono</family>
                        <family>Nimbus Mono L</family>
                        <family>Courier</family>
                        <family>Miriam Mono</family>
                        <family>Kochi Gothic</family>
                        <family>AR PL KaitiM GB</family>
                        <family>Baekmuk Dotum</family>
                        <family>FreeMono</family>
                </prefer>
        </alias>

Installing new fonts[edit]

Depending on your distribution there may be a foolproof font installation program - or not. In any case, using simple commands always works.

To install new fonts, just download the font files (for example ".ttf" files) and put them in the correct directory. For Xft with fontconfig, where and which types of fonts you can install is determined by the default configuration file /etc/fonts/fonts.conf or the extra configuration in /etc/fonts/local.conf or ~/.fonts.conf. Most distributions have in /etc/fonts/fonts.conf the following line:

        <dir>~/.fonts</dir>

This means you can install new fonts in your home directory with simple user privileges. To update fontconfig you use the command fc-cache.

To give an example: suppose you have downloaded the font anicefont.ttf to your Desktop. Open your favorite terminal or a simple xterm, and type:

cd 
mkdir .fonts
cd .fonts
cp ~/Desktop/anicefont.ttf .
fc-cache

Done. You can check whether the font was successfully installed by using the command fc-list (for example paging it with sort and more) and check for the name of your font: fc-list | sort | more.