[OpenBSD]

Anti-aliasing and TrueType Fonts on OpenBSD

Table of Contents


xterm screenshot

xterm(1) with aliased and anti-aliased fonts side-by-side:
[Standard and anti-aliased xterm]

Konqueror screenshot

Still not convinced? Let's have a look at this.

The Konqueror Web browser with aliased and anti-aliased fonts side-by-side:

[Standard and anti-aliased Konqueror]

How does it work?

The anti-aliasing calculations are done by FreeType, a free font engine that is included as a part of XFree86. XFree86 additionally contains an extension called Xft(3), the X FreeType extension, that interacts with FreeType for font rendering operations. Xft also interacts with an extension named Xrender, the X Render extension, which turns the calculations into digital images.

What needs to be done to get it?

OpenBSD ships XFree86 4.x with the X Render extension enabled. To be able to use it, your X server (video card driver) has to support it. Some drivers have not been converted to the 'fb' code yet and thus cannot use the Render extension, which is needed.

You can check your card by running:

$ /usr/X11R6/bin/xdpyinfo | grep RENDER
RENDER
If it doesn't say "RENDER" in its extension list, you'll have to wait until someone implements the extension for that card.

The next step is to grab and install some TrueType fonts. A collection of popular fonts has been compiled and is available in the OpenBSD ports tree, in /usr/ports/x11/msttcorefonts. They can be installed by doing the following:

# cd /usr/ports/x11/msttcorefonts
# make
# make install
At this point, the instructions for registering new fonts with X are listed in another section.

Which applications can use it?

Any application that interfaces with the Xft library can display anti-aliased fonts. This includes QT 3.x (KDE 3.x) and GTK+2 (Gnome 2.x) applications. Standard X programs that use this interface include xterm(1), xditview(1), xfd(1), and a few others.

xterm

xterm can be invoked with arguments that enable Xft rendering of fonts:
$ xterm -fa 'Andale Mono' -fs 14
These options are:
Option   Resource         Meaning
----------------------------------------
 -fa     XTerm*faceName   Font face name
 -fs     XTerm*faceSize   Font size
Alternatively, X resources can be used. They are by default placed in $HOME/.Xdefaults and are controlled through the xrdb(1) program.

Some TrueType fonts (such as Arial, Georgia, and Verdana, for example) don't work well in xterm. This is because xterm uses the maximum width of all characters in the specified font to display each character. With a monospace font, the widths of all the characters are the same, and everything works. With a proportional font, such as the ones just mentioned, xterm fits each character into a cell the size of the maximum width, creating unreadable large gaps between characters.

QT 3.x/KDE 3.x applications

Applications that use QT 3.x, such as any KDE 3.x applications, including Konqueror, can use Xft rendering after the following value has been set in $HOME/.qt/qtrc under the [General] section.
useXft=true
The GUI Qt configuration program, qtconfig (or qtconfig-mt if the multi-threaded flavor of Qt is installed), can set this value from the Enable Anti-aliased Font Support checkbox under the Fonts tab.

GTK+2 applications

GTK+2 applications require the use of the GDK_USE_XFT environmental variable being set. GDK is the backend GTK uses to isolate drawing operation details, and will offload its font rendering operations onto Xft when it detects this environmental variable.

The following will launch the GTK+2-based instant messenging program Gaim available in /usr/ports/net/gaim:

$ env GDK_USE_XFT=1 gaim

How can I add fonts manually?

Adding a TrueType font "to X" requires registering the font with both the X server and fontconfig(3), since both keep track of fonts separately. In fact, some fonts types are only recognized by the regular X server, and others only by fontconfig. TrueType fonts are one type that is recognized by both.

The X server

Font paths (i.e., directories containing fonts) are managed either directly by X in /etc/X11/XF86Config or by a separate program such as xfs(1). To add a font, add the directory where it resides to /etc/X11/XF86Config:
Section "Files"
	FontPath	"/usr/X11R6/lib/X11/fonts/TTF/"
	FontPath	"/usr/local/lib/X11/fonts/myfonts/"
	...
EndSection
Or, if using xfs, add the directory to /etc/X11/fs/config:
catalogue = /usr/X11R6/lib/X11/fonts/TTF/,
	    /usr/local/lib/X11/fonts/myfonts/
Next, the X server must be made aware of the font in that directory's font index, i.e., the file fonts.dir. Traditionally, all available sizes of a font were listed in the font index, generated by the mkfontdir(1) command. But since TrueType fonts can be scaled to any size, listing every possible size is not required. Instead, scalable fonts are listed in the file fonts.scale which is recognized by mkfontdir when generating fonts.dir indexes.

To build a fonts.scale file, the program mkfontscale(1) can be used:

# cd /usr/local/lib/X11/fonts/myfonts
# /usr/X11R6/bin/mkfontscale
# /usr/X11R6/bin/mkfontdir
Finally, to apply the changes to the current X session, instead of restarting X, the following command can be used to re-examine the currently registered font paths for changes:
$ xset fp rehash
Or, if a new font path was added:
$ xset +fp /usr/local/lib/X11/fonts/myfonts
If xfs is being used, following should be used instead:
# pkill -HUP xfs

fontconfig

The other component of X that must be made aware of a new font is fontconfig, which Xft uses to find fonts. fontconfig uses the file /etc/fonts/fonts.conf for its main system-wide configuration, but since this file will be replaced on subsequent X upgrades, /etc/fonts/local.conf should be used instead to manage system-wide changes.

Add the directory containing the font to this configuration file:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "/etc/fonts/fonts.dtd">
<fontconfig>
  <dir>/usr/local/lib/X11/fonts/myfonts</dir>
</fontconfig>
Or, if only one user wants fonts added to their sessions, the above can be placed in their $HOME/.fonts.conf file.

Note that fontconfig will recursively examine all sub-directories for available fonts whereas the regular X server will not.

After the directory is added, the font cache for the directory will need to be regenerated. The following command instructs fontconfig to rebuild the font caches for all directories specified in its configuration:

# /usr/X11R6/bin/fc-cache -v
These changes will take effect in the current X session, so X itself will not need to be restarted, but running applications will.
[back] www@openbsd.org
$OpenBSD: truetype.html,v 1.15 2004/09/11 23:18:23 jaredy Exp $