- 论坛徽章:
- 0
|
试析FreeBSD桌面系统中利用Fontconfig进行字体配置的运作原理(一)
杜比环绕声
概要:本文以KDE桌面环境为例,试析Fontconfig的运作机制
一、Fontconfig在桌面系统中的作用
KDE是类Unix系统中建立在X基础上的一个主流桌面环境,对用户来说,它提供了良好的图形界面和语言切换机制,用以满足来自不同国家、使用不同语言的用户的使用需求,这种机制简称“国际化”。主要包括界面文字的翻译,多种语言字体的管理、显示的支持以及输入法等等。这其中系统层面字体的管理和显示的支持直接关系到文字的显示效果,因此显得尤为重要。在KDE系统中,字体管理、显示的支持采用的是如下框架。
Fontconfig(字体配置)-------->Freetype(字库操作)-------------->xft(字体渲染)
Fontconfig : 管理系统安装的字体,根据应用程序的字体配置,match(匹配)一种字体,填充match字体的各种属性。
Freetype : 以Fontconfig匹配到的字体为参数,根据应用程序需要读取相应的字库。
xft: 对需要显示的语言文字进行渲染
二、X系统中字体的管理
作为KDE底层支持的X,它使用字体服务器(xfs)对安装的字体进行管理,也称之为“核心字库管理系统”,基本配置原理是xfs使用可维护的字体安装目录和fonts.dir文件对字体进行操作。
X为之提供了一些工具来完成管理操作。诸如 :
mkfontdir,mkfontscale 用来生成安装字体的 fonts.dir和fonts.scale。
xlsfonts 用来显示安装的字体。
xfd 用来显示指定字库的信息。
xset 用来管理字体服务器。
对于FreeBSD,在安装了xorg 7.2并且X server运行的情况下,字体安装目录可用 xset q来查询
- shell>xset q
- Font Path:
- /usr/local/lib/X11/fonts/misc/,/usr/local/lib/X11/fonts/TTF/,
- /usr/local/lib/X11/fonts/OTF,/usr/local/lib/X11/fonts/Type1/,
- /usr/local/lib/X11/fonts/100dpi/,/usr/local/lib/X11/fonts/75dpi/,
- /home/dbhrscom/.fonts
复制代码
上面显示的是X系统安装时定义的字体目录。在这些目录中,都有利用mkfontdir生成的fonts.dir(对于true type 字体需要使用mkfontscale 生成 fonts.scale,然后拷贝到fonts.dir中)。fonts.dir是个文本文件,里面保存的是所在目录的字体信息。关于这些信息的具体含义,可以参见X的man手册,这里不再赘述。
如果准备把一个字体安装到X系统中,并且使用Xfs进行管理,大致需要以下过程:
1、建立一个目录,并且把字库文件拷贝到这个目录下
2、运行mkfontdir或者mkfontscale,生成安装字库的fonts.dir文件。
3、运行“mkfontdir -e 系统编码目录” 生成encodings.dir 文件(可选)
4、运行 “xset +fp 字体目录” 或者 “xset fp+ 字体目录”,把字体目录添加到xfs的字体目录列表中。需要注意的是当字体目录没有fonts.dir这个文件时,这种添加字体目录操作会失败。
5、运行“xset rehash”进行更新,确保新添加的字体可用。
注意:作为一种X系统的约定,安装字体最方便的做法是把字体文件拷贝到“$HOME/.fonts/“目录下,这样省去了添加字体目录的操作,而且xset +fp 是针对当前会话有效,如果准备系统启动时有效,还要修改相关的配置文件。
三、Fontconfig的字体管理
简单的说Fontconfig是使用字体缓存的机制来进行字体管理的。这个Xfs有所不同。fontconfig提供fc-cache来建立这个缓存。
- shell >fc-cache -v
- /usr/local/lib/X11/fonts: skipping, 0 fonts, 12 dirs
- /usr/local/lib/X11/fonts/100dpi: skipping, 398 fonts, 0 dirs
- /usr/local/lib/X11/fonts/75dpi: skipping, 398 fonts, 0 dirs
- /usr/local/lib/X11/fonts/Fonts: skipping, 4 fonts, 0 dirs
- /usr/local/lib/X11/fonts/OTF: skipping, 23 fonts, 0 dirs
- /usr/local/lib/X11/fonts/TTF: skipping, 13 fonts, 0 dirs
- /usr/local/lib/X11/fonts/Type1: skipping, 29 fonts, 0 dirs
- /usr/local/lib/X11/fonts/bitstream-vera: skipping, 10 fonts, 0 dirs
- /usr/local/lib/X11/fonts/cyrillic: skipping, 0 fonts, 0 dirs
- /usr/local/lib/X11/fonts/encodings: skipping, 0 fonts, 1 dirs
- /usr/local/lib/X11/fonts/encodings/large: skipping, 0 fonts, 0 dirs
- /usr/local/lib/X11/fonts/local: skipping, 0 fonts, 0 dirs
- /usr/local/lib/X11/fonts/misc: skipping, 55 fonts, 0 dirs
- /usr/local/lib/X11/fonts/util: skipping, 0 fonts, 0 dirs
- /home/dbhrscom/.fonts: skipping, 0 fonts, 0 dirs
- /var/db/fontconfig: not cleaning unwritable cache directory
- /home/dbhrscom/.fontconfig: cleaning cache directory
- fc-cache: succeeded
复制代码
fc-cache的运行原理是扫描fontconfig的配置文件中定义的字体目录,然后利用扫描的结果生成系统字体缓存。在这个fc-cache -v的运行结果中可以看到,它所扫描的路径已经涵盖了xfs所管理的系统字体路径。但这并不表示fontconfig和xfs的继承关系,fontconfig扫描这些目录,完全是因为在配置文件fonts.conf中有这样的定义!
- <!-- Font directory list -->
- <dir>/usr/local/lib/X11/fonts</dir>
- <dir>~/.fonts</dir>
- <!-- Font cache directory list -->
- <cachedir>/var/db/fontconfig</cachedir>
- <cachedir>~/.fontconfig</cachedir>
复制代码
fc-cache所作的扫描都是基于以上的<dir></dir>定义。
这个配置文件在fontconfig最新版本中,默认放在下面的目录中
/usr/local/etc/fonts/
而生成的字体缓存按照系统和当前用户,分别存放到
/var/db/fontconfig
~/.fontconfig
这两个目录中,诸如下面所看到的
- 0c3f6abf7a2705d2e8570f7f70e9302d-x86.cache-2
- 1e99991e6eeb0b3e05d4185c886f4b19-x86.cache-2
- 2550ddc31d0b62d94bc27b0fea358aba-x86.cache-2
- 55cbb48b13195d29d336fd465f155e7b-x86.cache-2
- 75ff5e8ddb5aa19e5f229f72d89c73db-x86.cache-2
复制代码
.cache-2 就是fc-cache的运行记号,fontconfig通过这个缓存来管理系统安装的字体,也因为这种机制在系统中安装字体变得非常简单,只要把字体文件放到fonts.conf定义的目录中,然后运行一下fc-cache。
甚至fc-cache一下也不需要,因为在fonts.conf中定义了刷新时间:
- <!--
- Rescan configuration every 30 seconds when FcFontSetList is called
- -->
- <rescan>
- <int>30</int>
- </rescan>
复制代码
由此看出,fontconfig和xfs相比,fontconfig提供了更加灵活的字体管理机制,目前系统中保持xfs和fontconfig并行工作的理由,只能是为必须使用xfs机制的程序提供一种支援。而作为KDE,它使用的就是fontconfig进行字体管理。举例来说,KDE的控制中心有个“字体安装”模块,通过分析可知它进行字体安装的原理就是自定义一个安装路径,放到fontconfig的配置文件中,然后把字体拷贝到自定义的目录。
四、Fontconfig配置文件的结构
在2.4.2以后的版本中,Fonfconfig的配置文件采用了模块化的结构。配置文件由以下文件组成
- /usr/local/etc/fonts/fonts.conf
- /usr/local/etc/fonts/conf.avail/*.conf
- /usr/local/etc/fonts/conf.d/*.conf
复制代码
/usr/local/etc/fonts/conf.d/ 目录下的文件大多数是 conf.avail/ 目录下的连接,大致是如下这些:
- 20-fix-globaladvance.conf
- 20-lohit-gujarati.conf
- 20-unhint-small-vera.conf
- 30-amt-aliases.conf
- 30-urw-aliases.conf
- 40-generic.conf
- 49-sansserif.conf
- 50-user.conf
- 51-local.conf
- 60-latin.conf
- 65-fonts-persian.conf
- 65-nonlatin.conf
- 69-unifont.conf
- 80-delicious.conf
- 90-synthetic.conf
复制代码
前面的数字用来控制执行的先后顺序,从名称上就可以看出,每个.conf文件都有针对性的字体特性进行处理。
而实现这种模块化,所借助的就是 /usr/local/etc/fonts/fonts.conf 文件。
- <!--
- Load local system customization file
- -->
- <include ignore_missing="yes">conf.d</include>
复制代码
分析/usr/local/etc/fonts/conf.d 目录下的文件,并通过打开Fontconfig的调试功能,可知这些配置文件的执行顺序:
- Loading config file /usr/local/etc/fonts/fonts.conf
- Scanning config dir /usr/local/etc/fonts/conf.d
- Loading config file /usr/local/etc/fonts/conf.d/20-fix-globaladvance.conf
- Loading config file /usr/local/etc/fonts/conf.d/20-lohit-gujarati.conf
- Loading config file /usr/local/etc/fonts/conf.d/20-unhint-small-vera.conf
- Loading config file /usr/local/etc/fonts/conf.d/30-amt-aliases.conf
- Loading config file /usr/local/etc/fonts/conf.d/30-urw-aliases.conf
- Loading config file /usr/local/etc/fonts/conf.d/40-generic.conf
- Loading config file /usr/local/etc/fonts/conf.d/49-sansserif.conf
- Loading config file /usr/local/etc/fonts/conf.d/50-user.conf
- Loading config file /home/dbhrscom/.fonts.conf
- Loading config file /usr/local/etc/fonts/conf.d/51-local.conf
- Loading config file /usr/local/etc/fonts/conf.d/60-latin.conf
- Loading config file /usr/local/etc/fonts/conf.d/65-fonts-persian.conf
- Loading config file /usr/local/etc/fonts/conf.d/65-nonlatin.conf
- Loading config file /usr/local/etc/fonts/conf.d/69-unifont.conf
- Loading config file /usr/local/etc/fonts/conf.d/80-delicious.conf
- Loading config file /usr/local/etc/fonts/conf.d/90-synthetic.conf
复制代码
五、Fontconfig所提供的调试功能
Fontconfig提供了调制功能,用来诊断字体配置运行时的信息,使用调制功能非常简单,在KDE环境下用模拟终端定义 FC_DEBUG 这个环境变量的值即可:
例如:csh环境下
setenv FC_DEBUG 1
在模拟终端中启动相应的程序,就可以查看设定的调试信息。可以按照下面Fontconfig用户手册介绍的说明设定具体显示的调试信息。
- MATCH 1 Brief information about font matching 字体匹配信息
- MATCHV 2 Extensive font matching information
- EDIT 4 Monitor match/test/edit execution 显示match/test/edit信息
- FONTSET 8 Track loading of font information at startup
- CACHE 16 Watch cache files being written
- CACHEV 32 Extensive cache file writing information
- PARSE 64 (no longer in use)
- SCAN 128 Watch font files being scanned to build caches
- SCANV 256 Verbose font file scanning information
- MEMORY 512 Monitor fontconfig memory usage
- CONFIG 1024 Monitor which config files are loaded 显示配置文件装载信息
- LANGSET 2048 Dump char sets used to construct lang values
- OBJTYPES 4096 Display message when value typechecks fail
复制代码
待续。。。。。。
[ 本帖最后由 杜比环绕声 于 2007-5-30 16:17 编辑 ] |
|