- Home
- Packages
- Forums
- Wiki
- Bugs
- AUR
- Download

# Font Configuration (简体中文)

From ArchWiki
翻译状态： 本文是英文页面 Font_Configuration 的翻译，最后翻译时间：2012-10-20，点击这里可以查看翻译后英文页面的改动。

摘要
An overview of font configuration options and various techniques for improving the readability of fonts
相关页面
Fonts: Information on adding fonts and font recommendations
Java Runtime Environment Fonts: Fonts specific to Sun's Java machine
MS Fonts: Adding Microsoft fonts and mimicking Windows' font settings
Font Configuration/fontconfig Examples

Fontconfig 是一个为应用程序提供可用 字体 的程序库，也可用来配置字体渲染效果，参见fontconfig 软件包和 Wikipedia:Fontconfig。Free type 库(freetype2 软件包)就是以此为配置基础来渲染字体。

尽管 Fontconfig 已经是当今 Linux 的字体界的标准，但是仍有一部分应用使用更加原始的字体显示方法法，X Logical Font Description。

Arch Linux 上的字体渲染包支持有字节码解释器(BCI)的freetype2。为了更好的字体渲染，特别是在 LCD 显示器上，一些补丁包也添加到库中，参见下面的 #补丁包。#Infinality 包同时支持自动微调和亚像素渲染，允许在无须重新编译的情况下微调LCD滤光器，而且在粗体字下自动微调仍然表现良好。

## 字体路径

由于应用程序需要知道它们所使用的字体的位置，所以必须对字体做良好的编目，以便于应用程序更好更快的访问它们。

Fontconfig包含的字体路径是`/usr/share/fonts/`和`~/.fonts/`(Fontconfig会递归访问刚刚提到的目录)。为了简化管理和安装过程，当需要添加字体时，推荐使用之前提到的路径。

查看Fontconfig所包含的字体：

    $ fc-list : file

参见`man fc-list`以获得更多输出格式方面的信息。

通过考察Xorg的log文件来检查Xorg包含的字体路径:

    $ grep /fonts /var/log/Xorg.0.log

小贴士: 你也可以通过命令`xset q`检查Xorg包含的字体路径。

需要明确的是，Xorg不会像Fontconfig一样递归搜索`/usr/share/fonts/`目录。如果要增加一个目录，必须使用一个完整目录：

    Section "Files"
        FontPath     "/usr/share/fonts/local/"
    EndSection

如果你想把字体路径设置到每一位用户，你可以通过添加下面的配置代码到文件`~/.xinitrc`，这样可以将字体路径添加进或者移除出默认的路径中：

    xset +fp /usr/share/fonts/local/           # Prepend a custom font path to Xorg's list of known font paths
    xset -fp /usr/share/fonts/sucky_fonts/     # Remove the specified font path from Xorg's list of known font paths

如果要查看Xorg包含的字体，可以使用这个命令`xlsfonts`, 这个命令来自包xorg-xlsfonts。

## Fontconfig配置

Fontconfig的文档在fonts.confman页面中。

通过对`$XDG_CONFIG_HOME/fontconfig/fonts.conf`的修改可以完成对某个用户的配置, 而对`/etc/fonts/local.conf`的修改则可以完成对每一位用户的配置． 如果用户的单独配置和全局配置不同，系统优先使用前者. 这些文件共用相同的语法.

Note: 配置文件和目录: `~/.fonts.conf`, `~/.fonts.conf.d` 和 `~/.fontconfig/*.cache-*` 从fontconfig 2.10.1开始已经被废弃(upstream commit) 未来的版本也不会默认读取这些文件当作配置依据. 分别用 `$XDG_CONFIG_HOME/fontconfig/fonts.conf`, `$XDG_CONFIG_HOME/fontconfig/conf.d` 和`$XDG_CACHE_HOME/fontconfig/*.cache-*` 来代替.

Fontconfig 把所有的配置都放在一个中心文件中 (`/etc/fonts/fonts.conf`). 这个文件会在fontconfig更新时被替换，注意你不应该编辑这个文件. 字体设置重要的程序会引用这个文件以获得当前字体配置和渲染配置．这个文件是一系列配置规则的集合包括全局配置文件(`/etc/fonts/local.conf`), 预设的配置 `/etc/fonts/conf.d/`, 和用户的配置文件 (`$XDG_CONFIG_HOME/fontconfig/fonts.conf`).

Note: 有一些桌面环境 (比如说 GNOME 和 KDE) 使用 Font Control Panel 会自动生成和重写用户字体配置文件. 对于这些桌面环境, 最好配合已定义的字体配置来得到需要的显示效果．

配置文件使用XML格式并且需要一些格式头:

    <?xml version="1.0"?>
    <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
    <fontconfig>

      <!-- settings go here -->

    </fontconfig>

文中的配置示例省略了这些标签．

### 预设

预设配置放在目录{ic|/etc/fonts/conf.avail}}中. 当给它们创建了符号链接, 这些配置就被激活了．不管是对某一个用户还是全局配置, 这一规则是相同的．在`/etc/fonts/conf.d/README`中有描述. 这些预设会通过匹配它们原先它们的单独设置文件来覆盖配置．

举个例子，为了打开全局亚像素RGB渲染:

    # cd /etc/fonts/conf.d
    # ln -s ../conf.avail/10-sub-pixel-rgb.conf

为单个独立用户配置:

    $ mkdir $XDG_CONFIG_HOME/fontconfig/conf.d
    $ ln -s /etc/fonts/conf.avail/10-sub-pixel-rgb.conf $XDG_CONFIG_HOME/fontconfig/conf.d

### 反走样

字体光栅化 将矢量字体数据转化为位图数据以显示在屏幕上． 最后的效果可能由于走样会出现锯齿. 反走样默认被打开，这样可以增加字体边缘的分辨率.

      <match target="font">
        <edit name="antialias" mode="assign">
          <bool>true</bool>
        </edit>
      </match>

### 微调

字体微调 (这称作instructing) 是利于精确的指令来调整字体显示的轮廓，这样就可以用离散的网格来显示我需要的线条, 比如说像素网格．字体在DPI达到300之前不可能正确的显示．我们有两种hingting可以用.

#### 字节码解释器(BCI)

使用normal hinting, TrueType 的hinting指令被freetype的字节码解释器所翻译解释. 这在hinting指令很好的字体上工作的很好．

打开normal hinting:

      <match target="font">
        <edit name="hinting" mode="assign">
          <bool>true</bool>
        </edit>
      </match>

#### 自动微调器

从hinting中自动发现. 在hinting指令很好的字体上变现不如字节码解释器, 但是在hinting指令一般的字体上变现不错. autohinter和subpixel渲染当初设计时没有考虑它们协同工作的情形，所以不应该将它们放在一起使用．

打开自动微调(auto-hinting):

      <match target="font">
        <edit name="autohint" mode="assign">
          <bool>true</bool>
        </edit>
      </match>

#### 微调字体

Hint style is the amount of influence the hinting mode has. Hinting can be set to: `hintfull`, `hintmedium`, `hintslight` and `hintnone`. With BCI hinting, hintfull should work best for most fonts. With the autohinter, hintslight is recommended.

      <match target="font">
        <edit name="hintstyle" mode="assign">
          <const>hintfull</const>
        </edit>
      </match>

### 亚像素渲染

Subpixel rendering effectively triples the horizontal (or vertical) resolution for fonts by making use of subpixels. The autohinter and subpixel rendering are not designed to work together and should not be used in combination without the #Infinality patch set.

Most monitors manufactured today use the Red, Green, Blue (RGB) specification. Fontconfig will need to know your monitor type to be able to display your fonts correctly.

RGB (most common), BGR, V-RGB (vertical), or V-BGR

To enable subpixel rendering:

      <match target="font">
        <edit name="rgba" mode="assign">
          <const>rgb</const>
        </edit>
      </match>

If you notice unusual colors around font's borders, the wrong subpixel arrangement might be configured. The Lagom subpixel layout test web page can help identify it.

#### LCD滤光器

When using subpixel rendering, you should enable the LCD filter, which is designed to reduce colour fringing. This is described under LCD filtering in the FreeType 2 API reference. Different options are described under FT_LcdFilter, and are illustrated by this LCD filter test page.

The `lcddefault` filter will work for most users. Other filters are available that can be used in special situations: `lcdlight`; a lighter filter ideal for fonts that look too bold or fuzzy, `lcdlegacy`, the original Cairo filter; and `lcdnone` to disable it entirely.

      <match target="font">
        <edit mode="assign" name="lcdfilter">
          <const>lcddefault</const>
        </edit>
      </match>

#### 高级LCD滤光器说明

If the available, built-in LCD filters are not satisfactory, it is possible to tweak the font rendering very specifically by building a custom freetype2 package and modifying the hardcoded filters. The Arch Build System can be used to build and install packages from source.

First, refresh the freetype2 PKGBUILD as root:

    # abs extra/freetype2

This example uses `/var/abs/build` as the build directory, substitute it according to your personal ABS setup. Download and extract the freetype2 package as a regular user:

    $ cd /var/abs/build
    $ cp -r ../extra/freetype2 .
    $ cd freetype2
    $ makepkg -o

Edit the file `src/freetype-VERSION/src/base/ftlcdfil.c` and look up the definition of the constant `default_filter[5]`:

    static const FT_Byte  default_filter[5] =
        { 0x10, 0x40, 0x70, 0x40, 0x10 };

This constant defines a low-pass filter applied to the rendered glyph. Modify it as needed. Save the file, build and install the custom package:

    $ makepkg -e
    $ sudo pacman -Rd freetype2
    $ sudo pacman -U freetype2-VERSION-ARCH.pkg.tar.xz

Reboot or restart X. The lcddefault filter should now render fonts differently.

### 取消粗体字的自动微调功能

The auto-hinter uses sophisticated methods for font rendering, but often makes bold fonts too wide. Fortunately, a solution can be turning off the autohinter for bold fonts while leaving it on for the rest:

    ...
    <match target="font">
        <test name="weight" compare="more">
            <const>medium</const>
        </test>
        <edit name="autohint" mode="assign">
            <bool>false</bool>
        </edit>
    </match>
    ...

### 只对较大的字体打开反锯齿功能

See also sharpfonts.co.cc for related information.

Some users prefer the sharper rendering that anti-aliasing does not offer:

    ...
    <match target="font">
        <edit name="antialias" mode="assign">
            <bool>false</bool>
        </edit>
    </match>

    <match target="font" >
        <test name="size" qual="any" compare="more">
            <double>12</double>
        </test>
        <edit name="antialias" mode="assign">
            <bool>true</bool>
        </edit>
    </match>

    <match target="font" >
        <test name="pixelsize" qual="any" compare="more">
            <double>16</double>
        </test>
        <edit name="antialias" mode="assign">
            <bool>true</bool>
        </edit>
    </match>
    ...

### 替换字体

The most reliable way to do this is to add an XML fragment similar to the one below. This will cause Bitstream Vera Sans to be used in place of Helvetica:

    ...
    <match target="pattern" name="family" >
        <test name="family" qual="any" >
            <string>Helvetica</string>
        </test>
        <edit name="family" mode="assign">
            <string>Bitstream Vera Sans</string>
        </edit>
    </match>
    ...

An alternate approach is to set the "preferred" font, but this only works if the original font is not on the system, in which case the one specified will be substituted:

    ...
    < !-- Replace Helvetica with Bitstream Vera Sans Mono -->
    < !-- Note, an alias for Helvetica should already exist in default conf files -->
    <alias>
        <family>Helvetica</family>
        <prefer><family>Bitstream Vera Sans Mono</family></prefer>
        <default><family>fixed</family></default>
    </alias>
    ...

### 禁用点阵字型

To disable bitmap fonts in fontconfig, use `70-no-bitmaps.conf` (which is not placed by fontconfig by default):

    # cd /etc/fonts/conf.d
    # rm 70-yes-bitmaps.conf
    # ln -s ../conf.avail/70-no-bitmaps.conf

You may not need to remove `70-yes-bitmaps.conf` if it does not exist. You can choose which fonts to replace bitmaps fonts with (Helvetica, Courier and Times bitmap mapts to TTF fonts) by:

    # cd /etc/fonts/conf.d
    # ln -s ../conf.avail/29-replace-bitmap-fonts.conf

To disable embedded bitmap fonts for a specific font:

    <match target="font">
      <test qual="any" name="family">
        <string>Monaco</string>
      </test>
      <edit name="embeddedbitmap"><bool>false</bool></edit>
    </match>

### 为不完善的字体构建粗体风格和斜体风格

Freetype字体可以自动为没有斜体和粗体的字体创建斜体和粗体，但这只会在应用程序明确地提出这样请求下生效。但现有的程序很少会发出这样的请求，这版讲述如何手动强制生成缺少的字体风格。

按照如下描述编辑`/usr/share/fonts/fonts.cache-1`。因为执行`fc-cache`时会覆盖`/usr/share/fonts/fonts.cache-1`，所以编辑前请先备份`/usr/share/fonts/fonts.cache-1`到其他地方。

假设已经安装了Dupree字体：

    "dupree.ttf" 0 "Dupree:style=Regular:slant=0:weight=80:width=100:foundry=unknown:index=0:outline=True:etc...

复制上面一行，并将`style=Regular`改为`style=Bold`或者其他你想要的字体风格（粗体用Bold，斜体用Italic）。同时，若需要斜体的将`slant=0`改为`slant=100`，需要粗体的将`weight=80` 改为`weight=200`，或者你可以将两句组合起来来生成粗体和斜体的风格，其中一个示例如下:

    "dupree.ttf" 0 "Dupree:style=Bold Italic:slant=100:weight=200:width=100:foundry=unknown:index=0:outline=True:etc...

现在将以下内容添加到`$XDG_CONFIG_HOME/fontconfig/fonts.conf`文件中:

    ...
    <match target="font">
        <test name="family" qual="any">
            <string>Dupree</string>
             <!-- other fonts here .... -->
         </test>
         <test name="weight" compare="more_eq"><int>140</int></test>
         <edit name="embolden" mode="assign"><bool>true</bool></edit>
    </match>

    <match target="font">
        <test name="family" qual="any">
            <string>Dupree</string>
            <!-- other fonts here .... -->
        </test>
        <test name="slant" compare="more_eq"><int>80</int></test>
        <edit name="matrix" mode="assign">
            <times>
                <name>matrix</name>
                    <matrix>
                        <double>1</double><double>0.2</double>
                        <double>0</double><double>1</double>
                    </matrix>
            </times>
        </edit>
    </match>
    ...

小贴士: 使用了参数 'embolden' 可以令原来就存在粗体的字体变得更粗

### 改变规则重定义机制

Fontconfig processes files in `/etc/fonts/conf.d` in reverse numerical order. This enables rules or files to override one another, but often confuses users about what file gets parsed last.

To guarantee that personal settings take precedence over any other rules, change their ordering:

    # cd /etc/fonts/conf.d
    # mv 50-user.conf 00-user.conf

This change seems however to be unnecessary for the most of the cases, because a user is given enough control by default to set up own font preferences, hinting and antialiasing properties, alias new fonts to generic font families, etc.

### fontconfig配置的例子

Example fontconfig configurations can be found on this page.

A simple starting point:

    /etc/fonts/local.conf

    <?xml version='1.0'?>
    <!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
    <fontconfig>
     <match target="font">

      <edit mode="assign" name="rgba">
       <const>rgb</const>
      </edit>

      <edit mode="assign" name="hinting">
       <bool>true</bool>
      </edit>

      <edit mode="assign" name="hintstyle">
       <const>hintfull</const>
      </edit>

      <edit mode="assign" name="antialias">
       <bool>true</bool>
      </edit>

      <edit mode="assign" name="lcdfilter">
        <const>lcddefault</const>
      </edit>

     </match>
    </fontconfig>

## 补丁包

These patched packages are available in the AUR. A few considerations:

- Configuration is usually necessary.
- The new font rendering will not kick in until applications restart.
- Applications which statically link to a library will not be affected by patches applied to the system library.

### Infinality

Warning: Since the Fontconfig 2.10.1 update, Infinality users will receive multiple warnings similar to:

`Fontconfig warning: "/etc/fonts/infinality/conf.d/60-group-non-tt-fonts.conf", line 483: Having multiple values in <test> isn't supported and may not works as expected`

Rule adjustments are currently being worked on. See: this article for more information.

The infinality patchset aims to greatly improve freetype2 font rendering. It adds multiple new capabilities.

- Home page.
- Forum.

Infinality's settings are all configurable at runtime via environment variables in `/etc/profile.d/infinality-settings.sh`, and include the following:

- Emboldening Enhancement: Disables Y emboldening, producing a much nicer result on fonts without bold versions. Works on native TT hinter and autohinter.
- Auto-Autohint: Automatically forces autohint on fonts that contain no TT instructions.
- Autohint Enhancement: Makes autohint snap horizontal stems to pixels. Gives a result that appears like a well-hinted truetype font, but is 100% patent-free (as far as I know).
- Customized FIR Filter: Select your own filter values at run-time. Works on native TT hinter and autohinter.
- Stem Alignment: Aligns bitmap glyphs to optimized pixel boundaries. Works on native TT hinter and autohinter.
- Pseudo Gamma Correction: Lighten and darken glyphs at a given value, below a given size. Works on native TT hinter and autohinter.
- Embolden Thin Fonts: Embolden thin or light fonts so that they are more visible. Works on autohinter.
- Force Slight Hinting: Force slight hinting even when programs want full hinting. If you use the local.conf I provide (included in infinality-settings fedora package) you will notice nice improvements on @font-face fonts.
- ChromeOS Style Sharpening: ChromeOS uses a patch to sharpen the look of fonts. This is now included in the infinality patchset.

A number of presets are included and can be used by setting the USE_STYLE variable in `/etc/profile.d/infinality-settings.sh`.

freetype2-infinality can be installed from the AUR. Additionally, if you are using lib32-freetype2 from [multilib], replace it with lib32-freetype2-infinality from the AUR. The AUR also contains a Git version of freetype2 that builds the latest development snapshot of freetype2 with the Infinality patchset: freetype2-git-infinality, lib32-freetype2-git-infinality.

It is recommended to also install fontconfig-infinality to enable selection of predefined font substitution styles and antialiasing settings, apart from the rendering settings of the engine itself. After doing so, you can select the font style (win7, winxp, osx, linux, ...) with:

    # infctl setstyle

If you set e.g. win7 or osx you need the corresponding fonts installed.

Note: Default infinality settings can cause some programs to display fonts at 72 DPI instead of 96. If you notice a problem open /etc/fonts/infinality/infinality.conf search for the section on DPI and change 72 to 96. This problem can specifically affect conky causing the fonts to appear smaller than they should. Thus not aligning properly with images.
Note: The README for fontconfig-infinality says that /etc/fonts/local.conf should either not exist, or have no infinality-related configurations in it. The local.conf is now obsolete and completely replaced by this configuration.

for more information see this article: http://www.infinality.net/forum/viewtopic.php?f=2&t=77

### Ubuntu

Ubuntu adds extra configurations, and occasionally patches to the font rendering libraries.

Install the patched packages from the AUR, the package names are: freetype2-ubuntu fontconfig-ubuntu libxft-ubuntu cairo-ubuntu.

The global configuration will need to be added. See #Example fontconfig configurations for a starting point.

### 恢复到未打补丁的包

To restore the unpatched packages, reinstall the originals:

    # pacman -S --asdeps freetype2 libxft cairo fontconfig

## 没有fontconfig支持的应用程序

Some applications like URxvt will ignore fontconfig settings. This is very apparent when using the infinality patches which are heavily reliant on proper configuration. You can work around this by using `~/.Xresources`, but it is not nearly as flexible as fontconfig. Example (see #Fontconfig configuration for explanations of the options):

    ~/.Xresources

    Xft.autohint: 0
    Xft.lcdfilter:  lcddefault
    Xft.hintstyle:  hintfull
    Xft.hinting: 1
    Xft.antialias: 1
    Xft.rgba: rgb

Make sure the settings are loaded properly when X starts with `xrdb -q` (see Xresources for more information).

## 字体问题解决方法

### 字体扭曲

Note: 96 DPI is not a standard. You should use your monitor's actual DPI to get proper font rendering, especially when using subpixel rendering.

If fonts are still unexpectedly large or small, poorly proportioned or simply rendering poorly, fontconfig may be using the incorrect DPI.

Fontconfig should be able to detect DPI parameters as discovered by the Xorg server. You can check the automatically discovered DPI with `xdpyinfo`:

    $ xdpyinfo | grep dots

      resolution:    102x102 dots per inch

Note: To use the xdpyinfo command, you must install the package xorg-xdpyinfo.

If the DPI is detected incorrectly (usually due to an incorrect monitor EDID), you can specify it manually in the Xorg configuration, see Xorg#Display Size and DPI. This is the recommended solution, but it may not work with buggy drivers.

Fontconfig will default to the Xft.dpi variable if it is set. Xft.dpi is usually set by desktop environments (usually to Xorg's DPI setting) or manually in `~/.Xdefaults` or `~/.Xresources`. Use xrdb to query for the value:

    $ xrdb -query | grep dpi

    Xft.dpi:	102

Those still having problems can fall back to manually setting the DPI used by fontconfig:

    ...
    <match target="pattern">
       <edit name="dpi" mode="assign"><double>102</double></edit>
    </match>
    ...

### 较旧的GTK和QT应用程序

Modern GTK apps enable Xft by default but this was not the case before version 2.2. If it is not possible to update these applications, force Xft for old GNOME applications by adding to `~/.bashrc`:

    export GDK_USE_XFT=1

For older QT applications:

    export QT_XFT=true

## 资源

- Fonts in X11R6.8.2 - Official Xorg font information
- FreeType 2 Overview
- Gentoo font-rendering thread

Retrieved from "https://wiki.archlinux.org/index.php?title=Font_Configuration_(简体中文)&oldid=259437"
Categories:

- X Server (简体中文)
- Fonts (简体中文)

## Navigation menu