Fontconfig

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Fontconfig and the translation is 54% complete.
Other languages:
Resources
fontconfig 库 (media-libs/fontconfig) 旨为所有图形界面应用程序提供统一的字体选择和配置。尽管常常是不同的桌面环境 提供它们自己的字体重写和配置使用程序,但fontconfig 仍然属于底层库。

安装

当需要 fontconfig 时,它应该是已经安装好了的。 如果没有,请确保你正确安装配置好了图形系统 (即 XWayland) 和desktop environment

USE 标记

fontconfig 并没有多少USE标记。

USE flags for media-libs/fontconfig A library for configuring and customizing font access

doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
nls Add Native Language Support (using gettext - GNU locale utilities)
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)

配置

附注
所有对 fontconfig 文件的更改只有在重新启动程序后才会生效!

通用配置

fontconfig 使用来自 /etc/fonts/ 目录的 XML 文件生成内部配置。 默认情况下,它会解析/etc/fonts/fonts.conf (普通用户不应该编辑此文件!) ,它会使用一些默认的配置并解析 /etc/fonts/conf.d/ 内的内容。 另外还有个 /etc/fonts/conf.avail/ 目录包含了 fontconfig 的各种配置文件。 一般我们把需要的文件链接到 /etc/fonts/conf.d/ 目录。 这些文件按照它们的命名排序使用,它们以俩位数字开头,第一个数字(10)表示文件的影响力(类别)

Gentoo特色

Gentoo有一个 eselect 模块 (eselect fontconfig) ,完全符合通用配置方式-它管理 /etc/fonts/conf.avail/ 中的链接,通过删除或添加到 /etc/fonts/conf.d/ 目录的链接。 当然,更改这些配置也需要适当的权限(Root)。

以下几个小节将介绍如何使用 fontconfig 模块

可用文件列表

list 命令可以显示 fontconfig 可用的文件列表, 并标记启用的配置为星号(*)。

root #eselect fontconfig list
Available fontconfig .conf files (* is enabled):
  [1]   10-autohint.conf *
  [2]   10-no-sub-pixel.conf
  [3]   10-sub-pixel-bgr.conf
  [4]   10-sub-pixel-rgb.conf
  [5]   10-sub-pixel-vbgr.conf
  [6]   10-sub-pixel-vrgb.conf
  [7]   10-unhinted.conf
  [8]   11-lcdfilter-default.conf
  [9]   11-lcdfilter-legacy.conf
  ...
警告
不同的系统在 fonts.avail 有不同的配置文件,所以输出可能会不一样。 切勿盲目使用其它来源的列表号码 (比如博客或维基文章)。

启用配置文件

配置文件可以通过文件名或序号启用。这俩者没有区别:

root #eselect fontconfig enable 10-sub-pixel-rgb.conf
root #eselect fontconfig enable 4

禁用配置文件

同样也可以这样禁用:

root #eselect fontconfig disable 10-sub-pixel-rgb.conf
root #eselect fontconfig disable 4

定制全局配置

如果你想创建自定义的 fontconfig 全局配置,请启用 51-local.conf 并创建 /etc/fonts/local.conf (这是一个XML文件)。

文件 /etc/fonts/local.conf设置sans-serif为首选字体的后备配置
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <alias>
    <family>sans-serif</family>
    <prefer>
      <family>Ubuntu</family>
      <family>TakaoPGothic</family>
      <family>Droid Sans</family>
    </prefer>
    <default><family>DejaVu Sans</family></default>
  </alias>
</fontconfig>

以上配置简单来说就是当使用sans-serif字体时,倾向使用Ubuntu, TakaoPGothic 和 Droid Sans 字体(按顺序)而不是 DejaVu Sans字体(只有在必要时才使用)。当然,你也可以设置不同的字体。

单用户配置

如果要创建使用单用户 fontconfig 配置,启用 50-user.conf (可能已经被默认启用),用户最后将使用 ~/.config/fontconfig/fonts.conf 配置文件。该文件与 local.conf 同为XML格式。

附注
这是桌面环境会影响字体渲染的一种方式。谨慎起见,在定制字体配置时,最好禁用这个功能,以确保所生效的配置是系统级的。禁用它可以使字体的渲染在不同的用户账户之间更加统一。
附注
~/.fonts.conf已经废弃, 改为基于 $XDG_CONFIG_HOME。 这个变量的指向默认是 ~/.config

检查配置

检查默认字体的替代,例如,针对 Arial,输入下列命令:

user $fc-match Arial

抗锯齿, 微调, 和亚像素渲染

渲染效果也可以调整到更合适。在以下章节中,对“抗锯齿”,“微调”和“亚像素渲染”功能进行了调整。

Feature Description
Anti-aliasing is enabled by default and makes fonts less blocky.
Hinting is an attempt to cope with the low pixel count per unit of area of current displays. Correct hinting makes characters more crisp but since font metrics aren't changed (and arguably should not change) affects how overall the rendered text looks like.
Sub-pixel rendering uses the fact that LCD matrix has three primaries to effectively triple the resolution of text but can make characters appear not entirely black. To combat that lcdfilter is to be used with sub-pixel rendering (available for newer fontconfig) but it can blur the characters too much. In the end this entirely depends on person how they like their text.

Forcing hinting

The default fontconfig behavior regarding hinting is rather undocumented, but it can be made deterministically sub-optimal by making a system wide default.

  1. First enable /etc/fonts/local.conf
  2. Edit the local.conf file to include full hinting by default

This local.conf snippet enables full hinting:

文件 /etc/fonts/local.confSetting hinting to full
<match target="font">
  <edit mode="assign" name="hintstyle">
    <const>hintfull</const>
  </edit>
</match>
附注
Most fonts look best with full hinting but others need slight or more rarely some other hinting option. Some fonts for one reason or another will always be ugly.

使用亚像素渲染

It's important to determine the sub-pixel layout of the LCD matrix. It's usually RGB (10-sub-pixel-rgb.conf) but the only way to be sure is to either consult display specification or use this sub-pixel layout test to determine it.

Once determined, enable the appropriate 10-sub-pixel-<matrix type>.conf file.

root #eselect fontconfig enable 10-sub-pixel-rgb.conf

It's strongly advised that lcdfilter, if available, is used with sub-pixel rendering. It comes in different varieties but the default (11-lcdfilter-default.conf) should be appropriate for all common fonts.

root #eselect fontconfig enable 11-lcdfilter-default.conf

自动微调器

Autohinter attempts to do automatic hinting disregarding any existing hinting information. Until recently it was the default because TrueType2 was covered by patents but now that they have expired there's very little reason to use it. From technical point of view it does better than broken or no hinting information but it will be strongly sub-optimal for fonts with good hinting information. Generally system fonts are of the second kind so autohinter should not be used.

警告
Autohinter is not compatible with sub-pixel rendering, do not use the two together!

挑选 fonts

Choosing the right font can be trickier than deciding on the right hinting type. For one reason or another, some fonts will never be perfect — but it's certainly possible to make them look better than, say, the Windows 7 default font configuration.

附注
Many fonts don't provide glyphs for every assigned Unicode codepoint, due to the size of Unicode. If a font doesn't provide a glyph for a given codepoint, the character might be represented by a substitute character (such as a rectangle containing the hexadecimal value of the code point), or by a glyph from a different font (the "fallback font"), depending on the application.

Here are some recommendations regarding well known fonts in Gentoo:

Font family 优点 缺点
Liberation
media-fonts/liberation-fonts

Red Hat's fonts, which are metric-compatible with MS TrueType corefonts, have a decent, modern look. This is the Gentoo Fonts team recommendation for default Latin fonts. Covers about 2,600 code points.

Latin, Greek, Cyrillic, and Hebrew only. A few glyphs may have hinting trouble.

Linux Libertine
media-fonts/libertine

Very similar to Liberation, covering about 2,700 code points. Linux Libertine itself is proportional serif only, but the package contains less extensive sans and mono fonts, as well. Can be used as a fallback for some glyphs not in Liberation.

Latin, Greek, Cyrillic, and Hebrew only. Sans and mono fonts are limited.

Noto
media-fonts/noto

Google's font family that aims to support all the world's languages (so, well over 60,000 code points). It goes well with Liberation or Droid. Adobe's Source Han Sans fonts are included for CJK. Recommended as a fallback for many glyphs not covered by Liberation.

下载文件大
DejaVu
media-fonts/dejavu

Many styles and covers a lot of code points (about 6,100 for sans).

Exceptionally wide — even condensed is wider than same-height monospace. Overall second to Verdana (an MS font) in width. Sans-serif font is only average.

Droid
media-fonts/droid

Covers a lot of code points and scripts.

Very dry, wide yet thin glyphs. Clearly designed with handheld devices and their small screens in mind.

Gentium Plus
media-fonts/sil-gentium

Fairly distinctive; might appeal to people who like narrow fonts.

Serif only. As with other SIL fonts, the hinting is questionable.

Ubuntu
media-fonts/ubuntu-font-family

Used in Ubuntu (obviously). A distinctive font family with a style which might not appeal to everyone. Overall looks good and covers a fair number of code points.

Only the sans-serif font is truly polished; narrow and monospaced versions are unfinished. No known serif font that would accompany it well.

URW
media-fonts/urw-fonts

Metric compatible with popular Adobe fonts (among others?).

Seem to require slight hinting.

MS TrueType corefonts
media-fonts/corefonts

Includes most fonts used in documents and on the web.

MS does not distribute them nowadays, so the available fonts are from many years ago and do not reflect their current state (not to mention the state of the art). Obviously, lacks fonts introduced more recently. Require full hinting.

Unifont
media-fonts/unifont

Covers a lot of code points.

In addition to being ugly as sin, it also fails some basic requirements to be considered a typeface. Is it sans-serif? Is it serif? Please never use this.


External resources