X.Org/Fonts

From Gentoo Linux Wiki

Jump to: navigation, search


X.Org TOC


This article will walk you through the steps necessary to make fonts considerably more readable.

Contents

[edit] Background

Fonts on computers, in general and on Linux's X11 in particular, are a messy affair. There are many different font formats, ranging from old bitmap fonts to TrueType fonts. In addition, many fonts have problematic licensing and embedding issues. Finally, the main font rendering programs suffers from patent issues -- which Apple, fortunately, has not enforced yet -- if you want to turn on hinting, which only adds to the confusion.

Many programs, notably Ghostscript, Mozilla, OpenOffice.org and TeX, have their own ideas about fonts and font configuration. This means that the information in this article may not be sufficient to get your fonts to work.

To give you an idea: X11 does not have one font subsystem, but two! The old "core X font subsystem" have names like -misc-fixed-medium-r-normal--10-100-75-75-c-60-iso8859-1. The newer font system is known as fontconfig, works with the Xft library and, hopefully, will eventually replace the old system entirely. In addition, there is a font server named xfs.

[edit] On DPI

Before any software based tweaks, the best thing you can do to make sure that you get good looking fonts under Linux is to use an LCD/CRT monitor with at least 96 dpi (dots per inch). To calculate this, take the horizontal resolution and divide it by the width of the LCD/CRT screen (in inches). Anything less than 96 dpi will produce noticeable problems that no amount of tweaking will fix.

CRT's will almost always render fonts blurrier than LCD's because of the inherent blurring from the electron beam scan and the phosphor coated screen.

[edit] On Comparing Windows and Mac OS Fonts to Linux Fonts

Windows ClearType and Mac OS fonts will always look better than Linux fonts when a LCD has less than 96 dpi. When the dpi is 96 or greater, Linux fonts can look as good as or better than Windows or Mac OS. Of course, none of this is true if you install a patched libXft, which is available in the xeffects overlay.

[edit] Emerging "Essential" Packages

You will need the global USE flag truetype for various packages.

If you plan to run the KDrive Tiny X Server, you should set type1.

Only if you will be running an X-Server for multiple clients should you need the font-server USE flag.

Essential high quality fonts:

Ebuild Description
media-fonts/corefonts Microsoft's free-ish fonts: Arial, Andale Mono, Verdana, ...
media-fonts/ttf-bitstream-vera High-quality fonts by GNOME project: Vera Sans, Serif, Mono
media-fonts/font-bh-type1
media-fonts/font-bh-ttf
Notable for Luxi Mono, a fixed width serif
media-fonts/terminus-font A clean, fixed font for the console

Other popular fonts:

Ebuild Description
media-fonts/arkpandora Alternative to corefonts with nicer license. Includes font similar to Arial Mono
media-fonts/liberation-fonts GPL-2 Helvetica/Times/Courier replacement TrueType font set, courtesy of Red Hat
media-fonts/artwiz-aleczapka-en
media-fonts/dejavu
media-fonts/freefonts
media-fonts/unifont

If you want to see your national characters by default, then you should use dejavu font instead of ttf-bitstream-vera. To get it perform following steps:

emerge -C ttf-bitstream-vera
emerge dejavu
echo "media-fonts/ttf-bitstream-vera-1.10-r3" >> /etc/portage/profile/package.provided

Enable the USE flags either by editing /etc/make.conf or running:

euse --enable truetype

And, if you plan to use KDrive Tiny X Server :

echo "x11-base/kdrive type1" >> /etc/portage/package.use

If you updated your USE flags, you should reemerge world:

emerge -avN world

Then, emerge the packages (you can skip some of the unnecessary ones):

emerge -av --noreplace freetype corefonts freefonts artwiz-aleczapka-en terminus-font \ ttf-bitstream-vera unifont dejavu xorg-x11
Fix me: Is the following still true?

For X.Org 7.0: If you use UTF-8, you will need fonts for Eastern/CJK character sets. Some examples:

Language Ebuild
Japanese media-fonts/font-misc-misc
media-fonts/font-sony-misc
media-fonts/font-jis-misc
Chinese media-fonts/font-isas-misc
Korean media-fonts/font-daewoo-misc
International X11 Fixed Fonts media-fonts/intlfonts

[edit] Fonts for Programmers

Programmers often need monospaced fonts that are readable at small sizes.

Font Name Ebuild
Aerial Mono media-fonts/arkpandora
Andale Mono media-fonts/corefonts
Luxi Mono media-fonts/font-bh-type1
media-fonts/font-bh-ttf
Lucida media-fonts/font-bh-lucidatypewriter-75dpi
media-fonts/font-bh-lucidatypewriter-100dpi
Terminus media-fonts/terminus-font
Vera Sans Mono media-fonts/ttf-bitstream-vera
media-fonts/dejavu

The following fonts are not in Portage, but are freely available and worth searching Google for:

  • Anonymous
  • Aurulent Sans Mono
  • Bitstream Cyberbit
  • Monaco
  • Dina
  • Proggy Programming Fonts
  • TeX Gyre Collection
  • Serif Monospaced Fonts
    • Luxi Mono
    • Verily Serif Mono

[edit] Windows Fonts

If you feel constrained by the free fonts, Microsoft Windows, Office, et cetera, come with some excellent fonts that are not included in media-fonts/corefonts. True Type Fonts can be installed in the /usr/share/fonts directory and subdirectories below it, and are detected automatically when the X system is started. If you haven't wiped your Windows partition yet, the *.ttf files you'll find there could be used with X.Org. Note that in order to use those fonts legally, you must have an appropriate license.

Some of Windows Vista's fonts deserve special mention: Candara, Consolas (monospaced) and Corbel.

[edit] Emerging the Unnecessary Fonts

If you are the kind of person who absolutely must have every available font installed, here is the command that will do so:

emerge `emerge -s '@font' | sed -rne 's:\W+media-fonts/(\S+)$:\1:p'` -avN

[edit] Hinting Information

Hinting is part of the rendering process of a font. In general, hinting guides the rasterizer so that it produces a better looking onscreen font, especially with smaller font sizes. Due to possible patent issues, FreeType has the option of being compiled with TrueType's BCI, Byte Code Interpreter.

Without BCI, the default, FreeType uses its own auto-hinter to improve the quality of fonts at small sizes. The auto-hinter is still a work in progress, and improves with each release of FreeType. The auto-hinter usually produces excellent results, however, many feel that TrueType's native BCI produces results superior to the auto-hinter, especially on combined dual-head layouts, as it uses the hinting information contained in the font.

If you live in a country where software patents are not an issue or you interpret the possible patent issues as unlikely since Apple knows about FreeType and hasn't filed a lawsuit, you can enable the BCI by disabling the bindist USE Flag. See The FreeType Project's article on FreeType and Patents for more information.

echo "media-libs/freetype -bindist" >> /etc/portage/package.use
emerge -av freetype

If you find out that fonts are too fuzzy on your LCD panel, please try to enable bindist and see the difference. It might work better for you.


[edit] Fixing Firefox

Firefox uses the Cairo backend for rendering graphics. Cairo is a vector graphics backend. However, fonts are rendered as bitmaps. This is why some web page text look discolored only in Firefox. There is a patch from Arch to render bitmaps correctly for LCDs. The cleartype flag is required for cairo

echo "x11-libs/cairo cleartype" >> /etc/portage/package.use
emerge -av cairo

That's it. Restart Firefox to see the effect. Also see original article.

[edit] Configuration Files

With the fonts installed, you now have to modify the configuration files in order to allow the system to recognize them. These are the files you have to modify:

  • /etc/fonts/local.conf
  • ~/.fonts.conf
  • /etc/X11/xorg.conf

You can use fc-list, a part of the media-libs/fontconfig package, to check which fonts are installed. For visuals on available fonts, try media-gfx/gnome-specimen and gnome-extra/gucharmap, a Unicode character map viewer.

[edit] The Xft Font System

The system wide configuration file resides at /etc/fonts/local.conf, while ~/.fonts.conf is an optional user font configuration file which takes precedence over system wide changes, except in regards to anti-aliasing settings which cannot be overridden by users.

You do not need to set all the font directories unless you install fonts in non-standard places. /etc/fonts/fonts.conf includes /usr/share/fonts and everything beneath that, as well as a few other directories such as ~/.fonts.

Note that /etc/fonts/local.conf and ~/.fonts.conf are XML files that and are easier to edit with an editor that supports XML syntax highlighting. Included in the following example are many options you may want to use:

File: /etc/fonts/local.conf or ~/.fonts.conf
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/local.conf file to configure system font access -->
<fontconfig>

<!-- Use the Autohinter -->
	<match target="font">
		<edit name="autohint" mode="assign"><bool>true</bool></edit>
	</match>

<!-- Disable Autohinting for bold fonts -->
	<match target="font">
   		<test name="weight" compare="more"><const>medium</const></test>
   		<edit name="autohint" mode="assign"><bool>false</bool></edit>
	</match>

<!-- Enable sub-pixel rendering -->
<!-- Uncomment this if you have an LCD screen
        <match target="font">
                <edit name="rgba" mode="assign"><const>rgb</const></edit>
        </match>
-->
 
<!-- Exclude/Include a range of fonts for Anti Aliasing -->
<!--
	<match target="font">
		<test qual="any" name="size" compare="more"><double>9</double></test>
		<test qual="any" name="size" compare="less"><double>14</double></test>
		<edit name="antialias" mode="assign"><bool>true</bool></edit>
	</match>
-->

<!-- And/Or disable Anti Aliasing for a range on pixel-based size.
     Disabling this using both methods seems to fix Firefox. -->
<!--
	<match target="font">
		<test compare="less" name="pixelsize" qual="any"><double>20</double></test>
		<edit mode="assign" name="antialias"><bool>false</bool></edit>
	</match>
-->

<!-- Ignore any embedded bitmaps in TTF, etc (Microsoft's Calibri and others from Office 07/Vista have these) -->
        <match target="font" >
          <edit name="embeddedbitmap" mode="assign" >
            <bool>false</bool>
          </edit>
        </match>


<!-- Other config options that were added to previous versions of config files in the Gentoo Wiki HOWTO.
     Need clarification. -->
	<match target="font">
		<edit name="hinting" mode="assign"><bool>true</bool></edit>
		<edit name="hintstyle" mode="assign"><const>hintmedium</const></edit>
	</match>

</fontconfig>

Some settings should not reside in a system wide configuration file, unless it is certain that all users will approve of such tweaks. Such settings may be better set in ~/.fonts.conf.

File: ~/.fonts.conf
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- ~/.fonts.conf file to configure system font access -->
<fontconfig>

	<!-- Replace Courier with a better-looking font -->
	<match target="pattern" name="family">
		<test name="family" qual="any"><string>Courier</string></test>
		<edit name="family" mode="assign">
			<!-- Other choices - Courier New, Luxi Mono -->
			<string>Bitstream Vera Sans Mono</string>
		</edit>
	</match>

	<!-- Reject bitmap fonts in favour of Truetype, Postscript, etc. -->
	<selectfont>
		<rejectfont>
			<pattern>
				<patelt name="scalable"><bool>false</bool></patelt>
			</pattern>
		</rejectfont>
	</selectfont>

</fontconfig>

Remember that all changes to /etc/fonts/local.conf are system wide and will be overridden by ~/.fonts.conf, except for the anti-aliasing setting which will be forced on if set in the system wide file. The syntax of /etc/fonts/local.conf is exactly the same as the local user font configuration file ~/.fonts.conf, therefore you may copy either file and rename it as the other.

Note: The <rejectfont> in this example (~/.fonts.conf) will make the X server reject fonts like the terminus-fonts. This took me two hours to realize it. So you should think well, which options you copy.

[edit] Hinting

You will almost certainly want to use some form of font hinting. There are two forms of hinting supported by Gentoo Linux:

[edit] Auto-Hinting

By default, FreeType's auto-hinting is used, and is the default setting in the example configuration file. When using auto-hinting, however, you will most likely want to disable auto-hinting on bold fonts to prevent them from appearing too wide. The example file defaults auto-hinting on bold fonts to be off.

[edit] TrueType Bytecode Interpreter (BCI)

If you have compiled FreeType to use the TrueType bytecode interpreter (see above), and wish to use the BCI, then turn the auto-hinting off entirely. To do this, have <edit name="autohint" mode="assign"><bool>true</bool></edit> set to false instead of true. There are possible patent issues with using the TrueType BCI; if you wish to avoid the issue entirely, don't use the BCI.

Both KDE and GNOME have nice GUI's in which the user can select between 'light,' 'medium,' and 'strong' hinting. This appears to refer to the FreeType auto-hinter, and not the BCI. If you use the BCI, the only settings that seem to make any difference is to have hinting off entirely, which looks ugly. In other words light, medium, and strong hinting appear to all look identical when the BCI is used instead of the auto-hinter.

[edit] Sub-Pixel Rendering

In the above file, the sub-pixel rendering option is commented out because it is only useful for LCD panels. When enabled on a CRT display it only causes X.Org to do more rendering work. Possible values are for the sub-pixel rendering type none, rgb, bgr, vrgb and vbgr. The most common type of LCD panel is of the RGB variety, which is what the example above uses.

[edit] Anti-Aliasing

The anti-aliasing option is commented out because some people prefer to exclude or include a set of font sizes from getting anti-aliasing applied to them. It is a common tweak that makes your fonts look much more like the Windows® default fonts. When disabling anti-aliasing make sure to also disable sub-pixel rendering to achieve the best non anti-aliased fonts.

[edit] User Fonts

If you have your own fonts in a different directory than the default /usr/share/fonts/ or /usr/local/share/fonts/, here's how to add them:

File: /etc/fonts/local.conf or ~/.fonts.conf
...
<fontconfig>
...
  <dir>/home/david/extrafonts</dir>
</fontconfig>

[edit] X.Org

When configuring X.Org, it is important that it has the correct understanding of the size of your screen. For the best results, have the resolution in X.Org set to the native resolution of your LCD and/or set for a comfortable resolution for your CRT that maintains an appropriate aspect ratio for your display; this is actually more important than it seems as fonts in GTK+ applications seem to be disfigured by improper screen information and may also reveal itself in other applications.

[edit] Setting Screen Resolution

The /etc/X11/xorg.conf configuration file is split into sections. The following is an example of the screen section, which deals with, oddly enough, display screens.

File: /etc/X11/xorg.conf
...
Section "Screen"
	Identifier "Screen0"
	SubSection "Display"
		Modes "1280x960"
	EndSubSection
EndSection
....

[edit] Listing Fonts

Next you have to list your font directories in /etc/X11/xorg.conf. The font paths are searched in the order they are listed, so before you're done you'll have to tweak that order a bit.

Reasonably modern applications and DE's such as Gnome and KDE use fontconfig, so the only fonts that are actually required via the X server (or font server) is fixed and cursor.

File: /etc/X11/xorg.conf
# **********************************************************************
# Files section.  This allows default font and rgb paths to be set
# **********************************************************************

Section "Files"

# The location of the RGB database.  Note, this is the name of the
# file minus the extension (like ".txt" or ".db").  There is normally
# no need to change the default.

#    RgbPath	"/usr/share/X11/rgb"

# Multiple FontPath entries are allowed (which are concatenated together),
# as well as specifying multiple comma-separated entries in one FontPath
# command (or a combination of both methods)
# 
# 

    FontPath   "/usr/share/fonts/misc/"
#    FontPath   "/usr/share/fonts/TTF/"
#    FontPath   "/usr/share/fonts/OTF"
    FontPath   "/usr/share/fonts/Type1/"
    FontPath   "/usr/share/fonts/100dpi/"
    FontPath   "/usr/share/fonts/75dpi/"
#    FontPath   "/usr/lib/X11/fonts/local/"
#    FontPath   "/usr/lib/X11/fonts/misc/"
#    FontPath   "/usr/lib/X11/fonts/75dpi/:unscaled"
#    FontPath   "/usr/lib/X11/fonts/100dpi/:unscaled"
#    FontPath   "/usr/lib/X11/fonts/Speedo/"
#    FontPath   "/usr/lib/X11/fonts/Type1/"
#    FontPath   "/usr/lib/X11/fonts/TrueType/"
#    FontPath   "/usr/lib/X11/fonts/freefont/"
#    FontPath   "/usr/lib/X11/fonts/75dpi/"
#    FontPath   "/usr/lib/X11/fonts/100dpi/"

# The module search path.  The default path is shown here.

#    ModulePath "/usr/lib/modules"

EndSection
  • /usr/share/fonts/misc:unscaled

The first path we want on our list is /usr/share/fonts/misc. This is where the all important fixed and cursor fonts are kept. These are crucial for the proper operation of X.Org. This is where the default cursor is located, and putting it first in the path keeps X from using the custom cursors that some font packages install.

The :unscaled option tells X to use the unscaled versions of these fonts whenever possible to prevent pixelization. This can apply to any bitmap font.

  • /usr/share/fonts/75dpi:unscaled
  • /usr/share/fonts/100dpi:unscaled

These are the base bitmap fonts. We usually want 75dpi to be loaded before 100dpi since a lot of themes expect it. Most modern monitors will have a high DPI however, so if you find yourself squinting to read menus, you might want to experiment switching the order.

  • /usr/share/fonts/Type1
  • /usr/share/fonts/TTF

Type1 and TrueType fonts. The order shouldn't matter much, but might be notable that both of these packages contain the Luxi Sans/Serif/Mono fonts.

  • /usr/share/fonts/corefonts
  • /usr/share/fonts/freefonts

These are Microsoft's TrueType core fonts and a collection of TrueType fonts, respectively.

  • /usr/share/fonts/artwiz
  • /usr/share/fonts/terminus
  • /usr/share/fonts/ttf-bitstream-vera
  • /usr/share/fonts/unifont

If you emerged the packages suggested above you should also have this list of fonts.

  • /usr/share/fonts/local
  • /usr/share/fonts/cyrillic

These are usually not needed, and are mostly empty or non-existent. To be sure check the file fonts.dir inside each directory. If this file contains a '0' the directory does not need to be added to the font path.

  • /usr/share/fonts/*everythingelse*

All the directories of the new fonts you just emerged. The order shouldn't matter as most of the fonts these packages provide are unique. If you're worried about a particular font, place it higher up. You can check a list of what fonts are provided in each directory's fonts.dir file.

Now to make a few final changes to the order. Take the base bitmap fonts and put them last. This is because we now have much better TTF or Type1 versions of some of these fonts we'd rather use. Also, media-fonts/corefonts contains a lot of, um, core fonts and should be pretty high on the list.

The end result should look something like this:

File: /etc/X11/xorg.conf
...
FontPath    "/usr/share/fonts/misc:unscaled"
FontPath    "/usr/share/fonts/Type1"
FontPath    "/usr/share/fonts/TTF"
FontPath    "/usr/share/fonts/corefonts"
FontPath    "/usr/share/fonts/freefonts"
FontPath    "/usr/share/fonts/terminus"
FontPath    "/usr/share/fonts/ttf-bitstream-vera"
FontPath    "/usr/share/fonts/unifont"
FontPath    "/usr/share/fonts/75dpi:unscaled"
FontPath    "/usr/share/fonts/100dpi:unscaled"
FontPath    "/usr/share/fonts/artwiz"
...
[edit] Listing the Unnecessary Fonts

The following Python script will find all of the fonts that you've installed and run mkfontdir on those that haven't had it done yet, and generate output that you can copy and paste into /etc/X11/xorg.conf:

Code: fontlist_fix.py
#!/usr/bin/python 
#Original author: John Klehm 
#Changelog:  
# 20090409 - Check fonts.dir for 0 value and exclude it 
#            Be verbose when running mkfontdir so people can check if its right 
#            General code cleanup 
# 20090408 - Initial Version 

import os 

fontDir = '/usr/share/fonts' 
mkfontdirCmd = 'mkfontdir' 
#use this file to check if mkfontdir succeeded 
mkfontdirFile = 'fonts.dir' 
#contents of the fonts.dir file when no fonts are registered for this dir 
noFonts = '0\n' 
#start and end of fontpath lines 
sfp = '\tFontPath "' 
efp = '"\n' 
#start and end of files section 
sfs = 'Section "Files"\n' 
efs = 'EndSection\n' 
 
 
#find mkfontdirFile and make sure it doesnt have noFonts content 
def hasFonts(root, files): 
    found = False 
 
    for fileName in files: 
        if fileName == mkfontdirFile: 
            fh = open(root + '/' + mkfontdirFile, 'r') 
            fileContent = fh.read() 
            fh.close() 
             
            if (fileContent != '') and (fileContent != noFonts): 
                found = True 
     
    return found 


output = sfs 

#go through fontDir and all subdirs 
for root, dirs, files in os.walk(fontDir): 
    #if this is a font dir then add it 
    if hasFonts(root, files): 
        output += sfp + root + efp 
    #otherwise try running mkfontdir and if it works add it 
    elif root != fontDir: 
        print mkfontdirCmd + ' ' + root 
        os.system(mkfontdirCmd + ' ' + root) 

        if hasFonts(root, os.listdir(root)): 
            output += sfp + root + efp 

output += efs 

print output 

[edit] Restarting Services

After modifying config files, you have to restart X.Org. If you use an X display manager such as GDM, KDM, XDM, etc., you may be able to simply log out of your session and restart X by pressing Ctrl+Alt+Backspace at the login screen. If that doesn't work, restarting X.Org is possible by restarting the xdm service.

Someone could notice a system freeze by launching the following commands in X.Org, so you should launch them in another terminal. That is press CTRL + ALT + F1 and log in. (Pressing ALT + F7 will return you to X from the console.)

/etc/init.d/xdm restart

After executing this command, your X display manager should restart. You should have anti-aliased fonts after logging back in.


[edit] See Also

Personal tools