Human Readable   

 

     
   
     

Installing TrueType Fonts on GNU/Linux

© Copyright Darrell Anderson.

Before I settled on Slackware as my preferred GNU/Linux distro, I used the Mandrake 9.x distro for a while. After trying to use Mandrake’s font installer program, I decided to dispense with that utility and learn to manually install my TrueType font collection. I found the Mandrake utility clumsy and I did not like the idea that I could not figure out how to install the fonts to a directory of my choice.

I wanted my TrueType Font collection installed to my /usr/local partition so I would not have to reinstall fonts whenever I performed a clean reinstall of Mandrake or I wanted to install additional distros. Through a collection of some outdated on-line How-Tos, some trial-and-error on my part, and some additional research, I devised a punch list for installing TrueType fonts.

First select a directory location for installing the fonts. Preferably, select a location that does not need to be reinstalled if you later want to reinstall your GNU/Linux distro or want to install other distros. Typically, the best place is your /usr/local or /opt partitions. These locations should be separate physical partitions that will not be affected by any future reinstallations.

  1. If multi-booting, then before booting into GNU/Linux, copy all of your TrueType fonts to a separate temporary directory, a directory that can be seen and accessed from within GNU/Linux.
  2. Convert all font file names to lower case and to include no spaces in the file name.
  3. If you are not multi-booting, then copy all of the fonts to a CD.
  4. Boot into GNU/Linux.
  5. Perform the following as root.
  6. If necessary create a new font directory (for example, /usr/local/share/fonts/ttf).
  7. Verify or edit (chmod) the font directory permissions to 755 (drwxr-xr-x).
  8. Copy your TrueType fonts to the new font directory.
  9. Verify or edit (chmod) the individual file permissions to 644 (rw-r—r—).
  10. In /etc/X11/xorg.conf, add this new directory path to your list of font directories.
  11. In /etc/fonts/local.conf, add this new directory path to your list of font directories.
  12. Open a command line shell and change the directory (cd) to the new font directory.
  13. If necessary, delete the following files:

    font.scale
    fonts.dir
    fonts.cache-1
  14. Run mkfontscale. This will create a new file name fonts.scale.
  15. Run mkfontdir. This will create a new file name fonts.dir. Ensure you run this program after running mkfontscale.
  16. Run fc-cache -fv. This will update the fonts.cache-1 files throughout your system, including your TrueType font directory.
  17. Run xset fp rehash.
  18. If you are using the KDE desktop environment, open the KDE Control Center and run the system Font Installer to add the new fonts. You should not have to do anything except allow the tool to create the list and create local fonts.cache-1 files.

Caveats

I have noticed something on my system that I cannot explain fully. Apparently the KDE Font Installer stalls if the number of font files exceeds a certain threshold. If this happens to you then when you run the Font Installer you might receive the error message “The process for the fonts protocol died unexpectedly.

The root problem seems to be with the mkfontscale program and not the KDE Font Installer. The mkfontscale utility fails to generate a complete list when the number of fonts exceeds approximately 110. This would seem to be a bug.

My work-around solution:

  1. Count the number of TTF font files in your new TrueType font directory.
  2. Divide the number by 100 and round the result up to the nearest whole number. For example if you have 365 TTF files (as I do), round the calculation from 3.65 to 4.
  3. Create several temporary directories, such as TTF-1, TTF-2, TTF-3, TTF-n, where n is the number you just calculated.
  4. Copy the TTF files from your new directory to the new temporary directories, without exceeding approximately 100 files in each new temporary directory. Do not duplicate any files in any of the temporary directories.
  5. In each temporary directory run mkfontscale.
  6. Do not yet run mkfontdir.
  7. Do yet not run fc-cache.
  8. Using a text editor, open each of the new temporary fonts.scale files. At the top of each file will be a number. Record the number from each file.
  9. Add all of the numbers. Record this number.
  10. Merge all of the temporary fonts.scale files into one file. Except for the first file, do not include the top header. Merge only the font lists.
  11. In the new merged file, edit the number at the top of the file to be the same as the number you recorded when you added the total of all the individual numbers.
  12. Save this new merged file to your original TTF fonts directory.
  13. Delete all of the temporary TTF directories.
  14. In your original TTF fonts directory, now run mkfontdir.
  15. Run fc-cache -fv.
  16. Run xset fp rehash.
  17. Open the KDE Control Center to run the system Font Installer and add the new fonts. This time the Font Installer should successfully cache all the TTF files.

X Server

One area of frustration for many people when installing and rendering fonts is the X server itself. Improper screen resolution will play a role. Without specific auto-configuration instructions from the monitor, the X server will guess the screen resolution dots-per-inch. This sometimes will create a lot of hair-pulling. The best way to avoid this potential problem is forcing X to accept your desired screen resolution.

First determine the current screen size and the monitor’s physical screen size:

xdpyinfo | grep dimension

Next determine the current screen resolution in dots-per-inch:

xdpyinfo | grep resolution

Notice that physical screen size is listed in millimeters while resolution is listed in inches. To convert, remember that there are 25.4 millimeters in one inch.

After running the xdpyinfo command, you might notice that the dots-per-inch number is not square or unbalanced by a pixel or two. That is, the result might be something like 96x95 or 72x71. If this is the case, adding the DisplaySize instruction to xorg.conf will remedy this.

In the xorg.conf file, in the Monitor section, add the following X variable:

DisplaySize x-width y-height

x-width and y-height is the monitor’s physical screen width and height in millimeters. If you have a spec sheet handy from the vendor, then that information might already be included. If not, then grab a ruler or tape measure and actually measure the width (be careful not to scratch your LCD screen). Be patient, don’t worry about perfection, and measure the width and height as close as possible.

When you are finished the xorg.conf file will look something this:

DisplaySize 338 271

Using the numbers from the actual screen size that X is using, such as 1280x1024, 1024x768, 800x600, etc., mathematically divide the screen size width and height (pixels or dots) by the actual size (millimeters) you measured or found in the spec sheet. The results will be in dots-per-millimeter. For example, divide 1280 (pixels or dots) by 338 (millimeters) and the result is 3.7869. Divide 1024 by 271 and the result is 3.7786. Multiply those results by 25.4 to convert to dots-per-inch. In this example the result is 96.19 dpi x 95.98 dpi. Ignoring the mathematical precision of the calculations, both numbers should be the same. If they are, then add the DisplaySize instruction to xorg.conf.

Of course, remember to restart the X server.

After modifying xorg.conf and restarting the X server, repeat the xdpyinfo | grep resolution command to verify your efforts. You might need to adjust the DisplaySize numbers by a millimeter or two to finally derive a square dpi resolution.

Notice that if you modify the screen size that the dots-per-inch number changes too. This is not magical but mere mathematics.

Yes, if you have to resort to this level of fine-tuning X and you conclude that this nonsense is not yet ready for prime time, then you are correct!

Other Potential Font Problems

You might want to review your /var/log/Xorg.0.log file and note any font related error messages. If some messages exist, then notice the affected font path. Use the console to change directories and run mkfontscale and mkfontdir in each of those directories. After updating all the affected directories in this manner, then again run fc-cache -fv.

You must do this even if there are no fonts installed in those directories, but if that is the case, then consider deleting that directory and the reference in xorg.conf too.

Another clue that you might not have run mkfontscale and mkfontdir in certain font directories is that KDE will generate a ~/.fonts-cache-1 file in your home directory. Open that file, note the affected directories, and repeat the mkfontscale and mkfontdir process in those directories.

Only after ensuring that every font directory list in xorg.conf contains a fonts.scale and fonts.dir file should you then run fc-cache -fv. And only then should you run the KDE Font Installer. A clean installation will reveal no error messages in Xorg.0.log and KDE not generating any .fonts-cache-1 file.

Lastly, if using KDE and some GTK-based programs, create a text file named ~/.gtkrc-2.0. Add one sole entry:

gtk-font-name = "Arial 12"

Modify the font name to match your font choices in KDE.

The Final Trick to Nice TrueType Fonts

If you want your TrueType fonts looking as nice as they do in Windows, then be sure to install a version of FreeType that enables the Bytecode Interpreter. Some distro providers do this automatically, but some do not. Slackware is packaged without that option enabled. The visual difference is noticeable. For Slackware users, you can download an updated package from linuxpackages.net. Or, if you possess the source CDs and are comfortable compiling source code, you can recompile that package.

After updating that package you then modify /etc/fonts/local.conf:

  1. Enable/disable anti-aliasing. (Some people like anti-aliasing, some do not. Decide for yourself.)
  2. Enable/disable hinting. (Once again, decide for yourself.)
  3. Enable/disable auto-hinting. (Once again, decide for yourself.)

The local.conf file is global to all users, but individual users can override these settings by creating a ~./fonts.conf file in their home directory.

For all of these modifications you might need to log out and log in to notice the changes. You might also need to restart the X server and the font server if you are using one.

Also ensure that the FreeType module is loaded in xorg.conf:

Load "freetype"

Conclusion

Are you curious about all the fonts installed on your box? Try this:

fc-list | sort | less

You know what Porky Pig would say—That’s all folks! (Please add your own version of stuttering.)

Sample files:

/etc/fonts/fonts.conf

/etc/fonts/local.conf

/etc/X11/xorg.conf

~/.gtkrc-2.0

Finis.

Table of Contents