為linux添加新字型

來源:互聯網
上載者:User

一、準備字型檔

我一直認為,如果我擁有了一套windows xp的使用權(通過oem方式),那麼我就同時擁有了對這份windows產品的只用於我個人的任意使用權,這裡麵包括了對其中的字型的使用權。所以,本文中將winxp的字型拷貝到linux下使用的做法,應該是沒有侵權的。

windows的字型一般存放在c:\windows\fonts目錄下,我拷貝到linux下的字型有:

simfang.ttf 仿宋體
simhei.ttf 黑體
simkai.ttf 楷體
simsun.ttf 宋體和新宋體,原檔案名稱simsun.ttc
tahoma.ttf tahoma字型
tahomabd.ttf tahoma字型的粗體形式
verdana.ttf verdana字型
verdanab.ttf verdana字型的粗體形式
verdanai.ttf verdana字型的斜體形式
verdanaz.ttf verdana字型的粗體+斜體形式

拷貝過來的字型檔放在了/home/fwolf/tools/fonts目錄下。

二、將字型加入到linux的可使用字型中

首先把字型檔連結到存放字型的目錄中

cd /usr/share/fonts
ln -s /home/fwolf/tools/fonts xpfonts
cd xpfonts
mkfontscale
mkfontdir

這樣作和把字型拷貝到/usr/share/fonts的一個目錄下的效果是一樣的。後面的兩個mkfont命令是產生xpfonts目錄下所包含的字型的索引資訊。然後運行fc-cache命令更新字型緩衝:

fc-cache

現在,應該說linux就可以使用這些新裝上的中文字型了,看一下openoffic裡的吧:

其中仿宋體的字型名是FangSong_GB2312,黑體是SimHei,楷體是KaiTi_GB2312,宋體是SimSun,字型名暫時都不是中文的。

三、修改/etc/fonts/fonts.conf的字型渲染順序

讓我們一邊修改,一邊分析fonts.conf這個檔案的內容吧,注意部分注釋(<–和–>之間的內容)和無關緊要或者重複的內容我將跳過不再解釋。

<?xml version=”1.0″?> 聲明這個檔案是一個xml文檔
<!DOCTYPE fontconfig SYSTEM “fonts.dtd”> xml文檔的格式聲明(dtd = document type define)
<!– /etc/fonts/fonts.conf file to configure system font access –>
<fontconfig> xml檔案需要的根節點 <!–
DO NOT EDIT THIS FILE.
IT WILL BE REPLACED WHEN FONTCONFIG IS UPDATED.
LOCAL CHANGES BELONG IN ‘local.conf’.

文檔中提示不要手工修改fonts.conf,個人的設定可以寫入local.conf檔案,並且會自動被fonts.conf讀入,不過我現在還不知道怎麼建立這個檔案,等知道了再公布出來吧,現在先拿fonts.conf開刀,反正預先已經作了備份。

<dir>/usr/share/fonts</dir>
<dir>/usr/X11R6/lib/X11/fonts/Type1</dir> <dir>/usr/local/share/fonts</dir>
<dir>~/.fonts</dir>

所有字型檔存放的位置,其中第一個就是我們剛才放字型檔的目錄/usr/share/fonts。

<!–
Accept deprecated ‘mono’ alias, replacing it with ‘monospace’
–>
<match target=”pattern”>
<test qual=”any” name=”family”>
<string>mono</string>
</test>
<edit name=”family” mode=”assign”>
<string>monospace</string>
</edit>
</match>

將本檔案中的別名mono替換成全稱monospace,這是xml的一個自動替換聲明,使用別名的目的應該是為了簡化書寫或者維持對舊版本的相容。後面還有幾個替換就不再一一介紹了。

<!–
Serif faces
–> <alias>
<family>Bitstream Vera Serif</family>
<family>Vera Sans YuanTi</family> <!– added by lifesinger–>
<family>Times</family>
<family>Times New Roman</family>

Serif字型將要使用的字型順序。當要顯示Serif字型的內容時,就從下面這個列表中選擇字型,匹配順序是自上而下。可以看到已經安裝的圓體美化再這裡作了修改。如果我們想把宋體作為首選,就在Bitstream Vera Serif字型前面比葫蘆畫瓢加上一行就可以了。

至於什麼是Serif字型,以及Sans-serif字型,可以看一下細說字型 Sans Serif 與 Serif和serif vs sans serif這兩篇文章,說明的很詳細。我覺得把他們叫做字型或者字族,相對於字型來說,更貼切一些。

下面的Sans-serif faces、Monospace faces段中處理方式相同。

<!–
If the font still has no generic name, add sans-serif
–>
<match target=”pattern”>

又是三個替換,大概意思是如果上面的Panose還不成功,就嘗試使用sans-serif、serif、monospace的預設字型來替換,最後如果還替換不成功,強制使用sans-serif字型。(不知道這麼理解是否正確)

<!–
Some Asian fonts misadvertise themselves as monospaced when
in fact they are dual-spaced (half and full). This makes
FreeType very confused as it forces all widths to match.
Undo this magic by disabling the width forcing code –>
<match target=”font”>
<test name=”family”><string>GulimChe</string></test>
<edit name=”globaladvance”><bool>false</bool></edit>
</match>

一些亞洲字型實際使用了等寬字型,但是卻沒有明確的聲明,這段就是為了相容這個進行的處理,保持不變即可。

<!–
The Bitstream Vera fonts have GASP entries suggesting that hinting be
disabled below 8 ppem, but FreeType ignores those, preferring to use
the data found in the instructed hints. The initial Vera release
didn’t include the right instructions in the ‘prep’ table. Fix this
by disabling hinting manually at smaller sizes (< 8ppem)
–>

修正Bitstream Vera字型在小於8點的時候的顯示問題,保持不變即可。

<!–
Load local system customization file

<include ignore_missing=”yes”>local.conf</include>
–>

載入設定檔local.conf,下面的~/.fonts.conf也是一樣。

<!–
Provide required aliases for standard names
–>
<alias>
<family>serif</family>
<prefer>
<family>Bitstream Vera Serif</family>

又是三個系列字型的字型別名,不知道這個和前面的類似設定是怎麼個關係,大概前面設定的是備選字型,這裡是推薦字型?不過照例把我們的SimSun加進去。

<!–
Artificial oblique for fonts without an italic or oblique version
–>

對沒有斜體字的字型的處理方法,保持不變即可。

<!–
Synthetic emboldening for fonts that do not have bold face available
–>

沒有黑體字的字型的處理方法,保持不變即可。

<!–
These are the default Unicode chars that are expected to be blank
in fonts. All other blank chars are assumed to be broken and
won’t appear in the resulting charsets
–>

Unicode編碼中在所有字型中都應該顯示為空白的編碼錶,保持不變即可。

<!– Rescan configuration every 30 seconds when FcFontSetList is called
–>

每隔30秒鐘就重新檢查一下本設定檔案(看設定改變了沒有),保持不變即可。

至此,標準的設定檔就結束了,我們需要作的主要修改也都完成了。重啟X應該就能看到宋體中文的介面了。不過在fonts.conf還可以作一些其他的字型處理設定,這個在圓體美化所帶的那個fonts.conf中有例子,比如:


<!– Win XP 效果 –>
<match target=”font”>
<test name=”family”><string>Vera Sans YuanTi</string></test>
<edit name=”globaladvance”><bool>false</bool></edit>
</match>

這就是指明在對Vera Sans YuanTi字型的處理中,globaladvance參數設定為false,其他的設定也多是這種文法,具體的含義和效果我就不知道了:)。

四、中文字型顯示最佳化

按照上面的方法配置好以後,宋體能夠顯示了,但是字型有些發虛,那麼一種簡單的方法就是在系統->喜好設定->字型(system->preference->font) 中選擇單色(Monochrome),但是這樣所有的字型包括英文字型都會有一點點的小鋸齒,那麼替代的方法就是通過修改fonts.conf,去掉宋體的反鋸齒,並且用黑體替代宋體的粗體。在fonts.conf中添加:

<!– 用黑體替代宋體的粗體 –>
<match target=”pattern”>
<test name=”family”><string>SimSun</string></test>
<test name=”weight” compare=”more_eq”><const>bold</const></test>
<edit name=”family” mode=”assign”><string>SimHei</string></edit>
</match>
<!– 關閉宋體的AA –>
<match target=”font”>
<test qual=”any” name=”family”><string>SimSun</string></test>
<edit name=”antialias” mode=”assign”><bool>false</bool></edit>
</match>
<!– 解決不能識別中文字型名稱的問題 –>
<match target=”pattern”>
<test name=”family”><string>宋體</string></test>
<edit name=”family” mode=”assign”><string>SimSun</string></edit>
</match>
<match target=”pattern”>
<test name=”family”><string>新宋體</string></test>
<edit name=”family” mode=”assign”><string>SimSun</string></edit>
</match>
<match target=”pattern”>
<test name=”family”><string>仿宋_GB2312</string></test>
<edit name=”family” mode=”assign”><string>FangSong_GB2312</string></edit>
</match>
<match target=”pattern”>
<test name=”family”><string>楷體_GB2312</string></test>
<edit name=”family” mode=”assign”><string>KaiTi_GB2312</string></edit>
</match>
<match target=”pattern”>
<test name=”family”><string>黑體</string></test>
<edit name=”family” mode=”assign”><string>SimHei</string></edit>
</match>

為什麼要用黑體替代宋體的粗體呢,因為宋體字型其實是不含粗體的,windows下也是,只是windows採用了更進階一些的方法來間接實現了宋體的粗體而已。

小結

以上就是簡單的中文字型設定方法,和我對fonts.conf的簡單理解,希望能對大家有用,也希望過路大俠指正錯誤和不足之處。另外還可以參考一下一個詳細的漢化美化方案(freetype/fontconfig/xft),分析得比我深入多了。

 

轉自:http://blog.chinaunix.net/space.php?uid=8613773&do=blog&id=2018326

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.