字型配置實戰
下面,將以Fedora 20為例,自己動手將它配置為正確的顯示效果。目前,在Linux系統上配置字型的工具是Fontconfig。
為什麼是Fontconfig
感謝這個時代,曾經混亂不堪的字型配置方法終於被Fontconfig一統江湖。在Linux中,字型配置曾經各自為政、混亂不堪,XServer、Xft、GTK、GTK2、QT等等各自採用不同的配置手段,字型引擎也有Type1、FreeType等。目前,可以認為在Linux系統中只需要配置FontConfig即可。
XOrg的官方網站上的文檔說明:XOrg中有兩種字型系統,一種字型系統是XServer內建的字型系統,另一種就是Xft,而且XOrg官方建議所有的介面庫的開發人員首選Xft字型系統;對於字型引擎,現在只剩下FreeType了,Type1的功能已經合并到了FreeType中。FreeDesktop.org的官方網站上有Xft、Freetype以及Fontconfig的文檔,雖然很簡略,但是也提到:Xft1.0及以前的版本,需要通過XftConfig檔案來配置字型,從Xft1.1以後,都採用Fontconfig來配置字型;Fontconfig的文檔也說它只負責字型的配置,不負責字型的顯示。
So,配置Linux中的字型,我們只用理會Fontconfig,還有疑問嗎?
學習Fontconfig
學習Fontconfig的最佳方式是閱讀man fonts.conf手冊頁,其次,就是閱讀/etc/fonts/conf.d目錄下的設定檔,從執行個體中學習。
Fontconfig的功能就是輔助應用程式選擇字型並指導字型的顯示效果(只能說指導,因為具體的顯示由Xft、Freetype等說了算),用什麼策略選擇字型及用什麼選項顯示字型,通過設定檔來指定。Fontconfig對設定檔進行兩遍掃描,第一遍對應用程式傳遞給Fontconfig的字型列表(稱為pattern)進行操作,通過添加、刪除、替換pattern中的字型名,讓應用程式得到相應的字型;第二遍對已經選擇的字型進行操作,這時一般不改字型名了,而是對消除鋸齒(antialias)、微調(hinting)、自動微調(autohint)、微調層級(hintstyle)以及次像素平滑(rgba)等屬性進行控制。
Fontconfig設定檔的文法,這個不用我在這裡嘮叨,看man fonts.conf手冊頁即可,一點也不難,它的很多元素,如match、target、test、edit、string、bool、double、const等,本身就是自解釋的,看到這個單詞就知道它是什麼意思。它的原則就是對每一個<match...>...</match>,通過<test...>...</test>來選擇要編輯的元素,然後使用<edit...>...</edit>來對該元素進行編輯。
配置Fedora 20
Fontconfig首先讀取的設定檔是/etc/fonts/fonts.conf,然後,根據/etc/fonts/fonts.conf裡面的<include>...</include>資訊來載入其它的設定檔。Fedora 20預設是載入/etc/fonts/conf.d目錄下的所有檔案。當然,/etc/fonts/conf.d目錄下的檔案太多了,引入了很多其實沒有必要的複雜性。對於我這種追求簡潔的人,我直接就把它改了,讓/etc/fonts/conf.d下的檔案去見鬼,從我自己的主目錄的fonts.conf.d目錄下載入設定檔吧。如下圖,我注釋掉了第71行,增加了第72行:
下面正式開始寫設定檔。
第一步:將不標準的字型分類命名更改為標準的字型分類命名
英文的字型分類有serif、sans-serif和monospace,中文的分類有“宋體”和“黑體”,但我們不能保證別人在請求字型的時候拼字都是準確的,比如有可能拼字成“sans serif”或“sans”、“mono”等,中文也可能寫成繁體“宋體”、“黑體”或者拼音“SongTi”、“HeiTi”等,為了後面設定檔的簡潔,這些不標準的分類名要先替換成標準的分類名。
英文的分類名的標準化/etc/fonts/fonts.conf這個主設定檔(就是我上面截圖的那個檔案)已經做了,其完整代碼如下:
1<?xml version="1.0"?> 2<!DOCTYPE fontconfig SYSTEM "fonts.dtd"> 3 <!-- /etc/fonts/fonts.conf file to configure system font access --> 4<fontconfig> 5 6 <!-- 7 DO NOT EDIT THIS FILE. 8 IT WILL BE REPLACED WHEN FONTCONFIG IS UPDATED. 9 LOCAL CHANGES BELONG IN 'local.conf'. 10 11 The intent of this standard configuration file is to be adequate for 12 most environments. If you have a reasonably normal environment and 13 have found problems with this configuration, they are probably 14 things that others will also want fixed. Please submit any 15 problems to the fontconfig bugzilla system located at fontconfig.org 16 17 Note that the normal 'make install' procedure for fontconfig is to 18 replace any existing fonts.conf file with the new version. Place 19 any local customizations in local.conf which this file references. 20 21 Keith Packard 22 --> 23 24 <!-- Font directory list --> 25 26<dir>/usr/share/fonts</dir> 27<dir>/usr/share/X11/fonts/Type1</dir><dir>/usr/share/X11/fonts/TTF</dir><dir>/usr/local/share/fonts</dir> 28<dir prefix="xdg">fonts</dir> 29 <!-- the following element will be removed in the future --> 30<dir>~/.fonts</dir> 31 32 <!-- 33 Accept deprecated 'mono' alias, replacing it with 'monospace' 34 --> 35<match target="pattern"> 36<test qual="any" name="family"> 37<string>mono</string> 38</test> 39<edit name="family" mode="assign" binding="same"> 40<string>monospace</string> 41</edit> 42</match> 43 44 <!-- 45 Accept alternate 'sans serif' spelling, replacing it with 'sans-serif' 46 --> 47<match target="pattern"> 48<test qual="any" name="family"> 49<string>sans serif</string> 50</test> 51<edit name="family" mode="assign" binding="same"> 52<string>sans-serif</string> 53</edit> 54</match> 55 56 <!-- 57 Accept deprecated 'sans' alias, replacing it with 'sans-serif' 58 --> 59<match target="pattern"> 60<test qual="any" name="family"> 61<string>sans</string> 62</test> 63<edit name="family" mode="assign" binding="same"> 64<string>sans-serif</string> 65</edit> 66</match> 67 68 <!-- 69 Load local system customization file 70 --> 71 <!-- <include ignore_missing="yes">fonts.conf.d</incllude> --> 72<include ignore_missing="yes">~/fonts.conf.d</include> 73 74 <!-- Font cache directory list --> 75 76<cachedir>/var/cache/fontconfig</cachedir> 77<cachedir prefix="xdg">fontconfig</cachedir> 78 <!-- the following element will be removed in the future --> 79<cachedir>~/.fontconfig</cachedir> 80 81<config> 82 <!-- 83 These are the default Unicode chars that are expected to be blank 84 in fonts. All other blank chars are assumed to be broken and 85 won't appear in the resulting charsets 86 --> 87<blank> 88<int>0x0020</int> <!-- SPACE --> 89<int>0x00A0</int> <!-- NO-BREAK SPACE --> 90<int>0x00AD</int> <!-- SOFT HYPHEN --> 91<int>0x034F</int> <!-- COMBINING GRAPHEME JOINER --> 92<int>0x0600</int> <!-- ARABIC NUMBER SIGN --> 93<int>0x0601</int> <!-- ARABIC SIGN SANAH --> 94<int>0x0602</int> <!-- ARABIC FOOTNOTE MARKER --> 95<int>0x0603</int> <!-- ARABIC SIGN SAFHA --> 96<int>0x06DD</int> <!-- ARABIC END OF AYAH --> 97<int>0x070F</int> <!-- SYRIAC ABBREVIATION MARK --> 98<int>0x115F</int> <!-- HANGUL CHOSEONG FILLER --> 99<int>0x1160</int> <!-- HANGUL JUNGSEONG FILLER -->100<int>0x1680</int> <!-- OGHAM SPACE MARK -->101<int>0x17B4</int> <!-- KHMER VOWEL INHERENT AQ -->102<int>0x17B5</int> <!-- KHMER VOWEL INHERENT AA -->103<int>0x180E</int> <!-- MONGOLIAN VOWEL SEPARATOR -->104<int>0x2000</int> <!-- EN QUAD -->105<int>0x2001</int> <!-- EM QUAD -->106<int>0x2002</int> <!-- EN SPACE -->107<int>0x2003</int> <!-- EM SPACE -->108<int>0x2004</int> <!-- THREE-PER-EM SPACE -->109<int>0x2005</int> <!-- FOUR-PER-EM SPACE -->110<int>0x2006</int> <!-- SIX-PER-EM SPACE -->111<int>0x2007</int> <!-- FIGURE SPACE -->112<int>0x2008</int> <!-- PUNCTUATION SPACE -->113<int>0x2009</int> <!-- THIN SPACE -->114<int>0x200A</int> <!-- HAIR SPACE -->115<int>0x200B</int> <!-- ZERO WIDTH SPACE -->116<int>0x200C</int> <!-- ZERO WIDTH NON-JOINER -->117<int>0x200D</int> <!-- ZERO WIDTH JOINER -->118<int>0x200E</int> <!-- LEFT-TO-RIGHT MARK -->119<int>0x200F</int> <!-- RIGHT-TO-LEFT MARK -->120<int>0x2028</int> <!-- LINE SEPARATOR -->121<int>0x2029</int> <!-- PARAGRAPH SEPARATOR -->122<int>0x202A</int> <!-- LEFT-TO-RIGHT EMBEDDING -->123<int>0x202B</int> <!-- RIGHT-TO-LEFT EMBEDDING -->124<int>0x202C</int> <!-- POP DIRECTIONAL FORMATTING -->125<int>0x202D</int> <!-- LEFT-TO-RIGHT OVERRIDE -->126<int>0x202E</int> <!-- RIGHT-TO-LEFT OVERRIDE -->127<int>0x202F</int> <!-- NARROW NO-BREAK SPACE -->128<int>0x205F</int> <!-- MEDIUM MATHEMATICAL SPACE -->129<int>0x2060</int> <!-- WORD JOINER -->130<int>0x2061</int> <!-- FUNCTION APPLICATION -->131<int>0x2062</int> <!-- INVISIBLE TIMES -->132<int>0x2063</int> <!-- INVISIBLE SEPARATOR -->133<int>0x206A</int> <!-- INHIBIT SYMMETRIC SWAPPING -->134<int>0x206B</int> <!-- ACTIVATE SYMMETRIC SWAPPING -->135<int>0x206C</int> <!-- INHIBIT ARABIC FORM SHAPING -->136<int>0x206D</int> <!-- ACTIVATE ARABIC FORM SHAPING -->137<int>0x206E</int> <!-- NATIONAL DIGIT SHAPES -->138<int>0x206F</int> <!-- NOMINAL DIGIT SHAPES -->139<int>0x2800</int> <!-- BRAILLE PATTERN BLANK -->140<int>0x3000</int> <!-- IDEOGRAPHIC SPACE -->141<int>0x3164</int> <!-- HANGUL FILLER -->142<int>0xFEFF</int> <!-- ZERO WIDTH NO-BREAK SPACE -->143<int>0xFFA0</int> <!-- HALFWIDTH HANGUL FILLER -->144<int>0xFFF9</int> <!-- INTERLINEAR ANNOTATION ANCHOR -->145<int>0xFFFA</int> <!-- INTERLINEAR ANNOTATION SEPARATOR -->146<int>0xFFFB</int> <!-- INTERLINEAR ANNOTATION TERMINATOR -->147</blank>148 <!--149 Rescan configuration every 30 seconds when FcFontSetList is called150 -->151<rescan>152<int>30</int>153</rescan>154</config>155156</fontconfig>
/etc/fonts/fonts.conf