- 论坛徽章:
- 0
|
试析FreeBSD桌面系统中利用Fontconfig进行字体配置的运作原理(二)
杜比环绕声
概要:本文以KDE桌面环境为例,试析Fontconfig的运作机制
六、Fontconfig的字体匹配
Fontconfig的字体匹配模块是它的功能核心,设计的目标是确保根据“使用Fontconfig的应用程序所题出来的字体要求”能在系统安装的字体中找到一个匹配的字体,并为后续的字体渲染、字体显示填充一些用户可以控制的属性。字体匹配的原理简单的说就是“字体之间属性距离的计算”,通过比较应用程序所要求的字体与Fontconfig所管理的字体一些属性之间的距离,来确保可以找到一个字体,理想的字体匹配是“完全满足应用程序的要求”,但一般情况下,因为受系统中安装字体种类的限制、用户审美取向的不同以及Fontconfig配置文件的具体设定等因素的影响,只能依据“综合距离最小”的原则匹配到一种字体。然后根据Fontconfig配置文件中对具体字体的设定,以及后续字体渲染的一些要求,最终“加工”出一个“具有完整属性”的字体。
在整个字体匹配过程中,Fontconfig提供了一种非常灵活的控制字体匹配的机制,它通过可定制的的配置文件(fonts.conf)生成可对字体匹配造成影响的匹配规则,利用这些规则,用户可以替换字体,可以对可用字体进行排序,可以针对不同语言环境进行优化调整,甚至可以针对一种字体的不同大小,进行字体渲染的微调,这一切都可以利用Fontconfig的配置文件来调整,这一切也都是利用配置文件来控制,而这正符合Unix应用哲学中的“高可定制性”,因此想要掌握桌面系统的字体匹配,首先必须要了解掌握的就是Fontconfig的配置文件—fonts.conf和conf.d下的所有配置文件。
在第一部分已经提到了目前fonts.conf的一种发展趋势,就是模快化设计,但是fontconfig系统运行时,是加载全部的配置文件。
为了更深层次的对fontconfig的字体匹配机制进行分析,先明确几个概念性的东西:
(一)字体的属性
在fontconfig中定义了35种字体属性,在fontconfig的用户手册中有所介绍,现摘录如下:
- 属性 类型 说明
- family String Font family names
- familylang String Languages corresponding to each family
- style String Font style. Overrides weight and slant
- stylelang String Languages corresponding to each style
- fullname String Font full names (often includes style)
- fullnamelang String Languages corresponding to each fullname
- slant Int Italic, oblique or roman
- weight Int Light, medium, demibold, bold or black
- size Double Point size
- width Int Condensed, normal or expanded
- aspect Double Stretches glyphs horizontally before hinting
- pixelsize Double Pixel size
- spacing Int Proportional, dual-width, monospace or charcell
- foundry String Font foundry name
- antialias Bool Whether glyphs can be antialiased
- hinting Bool Whether the rasterizer should use hinting
- hintstyle Int Automatic hinting style
- verticallayout Bool Use vertical layout
- autohint Bool Use autohinter instead of normal hinter
- globaladvance Bool Use font global advance data
- file String The filename holding the font
- index Int The index of the font within the file
- ftface FT_Face Use the specified FreeType face object
- rasterizer String Which rasterizer is in use
- outline Bool Whether the glyphs are outlines
- scalable Bool Whether glyphs can be scaled
- scale Double Scale factor for point->pixel conversions
- dpi Double Target dots per inch
- rgba Int unknown, rgb, bgr, vrgb, vbgr,
- none - subpixel geometry
- minspace Bool Eliminate leading from line spacing
- charset CharSet Unicode chars encoded by the font
- lang String List of RFC-3066-style languages this
- font supports
- fontversion Int Version number of the font
- capability String List of layout capabilities in the font
- embolden Bool Rasterizer should synthetically embolden the font
复制代码
每个属性都有一个或多个值,用来描述字体的某方面特征。如 lang,它保存的是字体所支持的语言字符串,里面可以有多个值, 如 hinting ,它保存的是该字体在显示时是否进行hint渲染,值只能是 true 或 fasle。
从属性的功用上分,大致可以分为三类:
1、为后续的字体渲染所定义,fontconfig可以对这些属性进行设定,影响后续的字体显示动作。如:antialias、hinting、hintstyle、rgba 等等
2、为字体匹配所定义,这些属性用来完成字体匹配的运算。如:family、style 、lang
3、为标识字体所定义,这部分属性一般不参与字体匹配与字体渲染的操作,但用来标识字体的信息。如 fontversion。
一个具体的例子:simsun的字体属性,部分属性是根据fontconfig的配置文件生成。
- family: "SimSun"(s) :字体家族名
- familylang: "en"(s) :家族语言
- style: "Regular"(s) :正常风格
- stylelang: "en"(s) :语言风格
- slant: 0(i)(s) :倾斜
- weight: 80(i)(s) :一些字体参数
- width: 100(i)(s)
- size: 12(f)(s)
- pixelsize: 12.5(f)(s)
- spacing: 90(i)(s)
- foundry: "unknown"(s) :出品公司
- antialias: FcTrue(w) :可antialias
- hintstyle: 2(i)(w) :hint风格
- hinting: FcTrue(w) :可hinting
- verticallayout: FcFalse(s) :纵向输出
- autohint: FcFalse(s) :不antohint
- globaladvance: FcTrue(s) :高级控制可用
- file: "/usr/local/lib/X11/fonts/Fonts/simsun.ttc"(s) :字体位置
- index: 0(i)(s) :索引
- outline: FcTrue(s) :可outline
- scalable: FcTrue(s) :可scalable
- dpi: 75(f)(s) :DPI值
- rgba: 5(i)(w) :rgba方式
- scale: 1(f)(s) :缩放方式
- charset: 0000: 00000000 ffffffff ffffffff 7fffffff 00000000 ffffffff ffffffff ffffffff (这里删去很多)
- lang: aa|ast|ay|bg|bi|br|ch|co|da|de|en|es|eu|fj|fo|fr|fur|fy|gd|gl|gv|ho|ia|id|ie|io|is|it|kum|lb|mg|nb|nds|nl|nn|no|oc|om|os|pt|rm|ru|sel|sma|smj|so|sq|sv|sw|tn|ts|vo|wa|xh|yap|zh-cn|zh-sg|zu(s) :支持zh-cn语言
- fontversion: 131072(i)(s) :字体版本
- capability: "otlayout:hani"(s) :兼容
- fontformat: "TrueType"(s) :字体格式
- embeddedbitmap: FcTrue(s) :位图字体增强
- decorative: FcFalse(s) :可修饰
复制代码
(二)、pattern
pattern是fontconfig字体匹配的核心数据结构,所有的匹配操作都是围绕着pattern来完成的,pattern可以理解成为一个在流水线上制造的汽车,汽车的用户需求相当于应用程序提出的字体要求,可以非常简单,比如sans serif字体,大小12号,这也是pattern的最初形态 ,Fontconfig相当于制造流水线,根据配置文件中所定义的匹配规则,对这个最初的pattern进行加工,随着工序的逐步完成,pattern变得有模有样,无论是功能还是外形,都按照匹配规则的定义进行了设定,最终当fontconfig的所有流水作业完成时,pattern已经被武装成了一种具体的字体 ,并添加了控制后续字体显示,渲染的属性参数。
这就是pattern,它最终表示的是一种设置周全的字体,满足应用程序,或者说用户的要求。
(三)、匹配规则
就是Fontconfig的配置文件中定义的那些规则,在进行字体匹配时fonts.conf和conf.d目录下的配置文件都要被fontconfig读取、分析,然后形成匹配规则列表,匹配规则可以分为三类
1、与pattern相关,这些规则用于进行字体匹配运算,最终可以得到一种字体
这种类型的规则在配置文件中大都以下面的形式进行定义:
- <match target="pattern">
- <test qual="any" name="family">
- <string>sans</string>
- </test>
- <edit name="family" mode="assign">
- <string>sans-serif</string>
- </edit>
- </match>
复制代码
简单说明一下:这个语句块的作用是,对pattern进行匹配操作,当pattern中的family属性中包含有sans时,匹配成功,执行下面的操作 —— 把pattern 中 family 属性中的 sans字体 替换成 sans-serif 字体。这里不对语句快中的具体参数进行解释,可以参看fontconfig的用户手册。
2、与font相关,这些规则主要是对匹配得到的字体进行进一步调整,满足用户的需求
- <match target="font">
- <test name="family">
- <string>SimSun</string>
- </test>
- <edit name="hinting">
- <bool>false</bool>
- </edit>
- </match>
复制代码
简单说明一下:这个语句块的作用是,对匹配到的字体pattern进行字体匹配,如果这个字体pattern中的family属性是SimSun,则对这个匹配到的pattern做如下修改:把hinting的属性设定成false ,整个语句块的作用就是把simsun的hinting设定成假,如果匹配到的字体是simsun,那么这个设定就生效。如果没有匹配到simsun这个字体,这个匹配操作无效。
3、与字体渲染属性相关,这些规则主要是按照用户的需求,对字体的显示作一些定义。
- <match target="font" >
- <edit mode="assign" name="antialias" >
- <bool>true</bool>
- </edit>
- </match>
复制代码
简单说明一下:这个语句块是对fontconfig匹配到的字体,无论是什么字体,都修改antialias属性为true,这个语句块摘自用户目录下的.fonts.conf文件,由KDE控制中心的字体配置中进行设定。
试析FreeBSD桌面系统中利用Fontconfig进行字体配置的运作原理(一)
[ 本帖最后由 杜比环绕声 于 2007-6-1 02:37 编辑 ] |
|