Thursday, May 05, 2011

[Linux]中文字体美化笔记

升级到Ubuntu 11.04之后, 突然中文设置全都混乱了.于是只好重新设置.笔记一下以免下次再出现.我主要是设置firefox和fontconfig的字体替换.

背景知识
有衬线(Serif) 无衬线 (Sans Serif)和等宽(Monospace)字型
  • 1 有衬线(Serif)字型是比较正式的字体,比划粗细不一,在笔划的边缘有装饰部分(我的理解是有笔锋)。英文当中比较有名的包括 Times New Roman, Georgia, 而中文当中则是包括大名鼎鼎的宋体,明体(繁体宋体在港台的叫法)。Serif font 的好处是看起来比较容易辨认,我自己觉得读起来速度会快。
  • 2 无衬线(Sans Serif)是比较活泼一些的字体,比划粗细一致。。英文当中包括Arial, Helvetica, Tahoma, Verdana等,而中文则是包括黑体,幼圆,仿宋好像也算是sans serif。要注意的是,这里的"黑体"有时会造成歧义,如要表达Bold的字体,可能粗体是更恰当。Sans Serif 的好处,据说字体小的时候看起来比较舒服。
  • 3 等宽(Monospace)字型顾名思义就是每个字母的宽度相同。通常显示代码的时候用等宽的字体会比较整齐. 另外命令行(Console)里面也用等宽字体比较好看。比较有名的是Courier。 我是Dejavu Sans Mono的忠实粉丝。

DPI
DPI=Dots per inch. 在显示器里可以理解为一英寸长要多少像素。Windows 以及Firefox (all platform)里的默认值为96。以前的话这个值是和大多数显示器差的不会太远。但是很多现在的显示器可以支持到更高(比如我的Macbook Pro的LCD就是110)。那么如果还设定DPI=96,相应的字体就会显小。所以可以首先确定自己系统里设定的DPI是否正确。

字体的单位
Font的大小分为pointsize(单位是pt磅)和pixelsize(单位是px像素).两者换算的关系可参考 http://www.emdpi.com/fontsize.html . 简单说来关于磅和像素这两个单位的换算关系是:1磅=1/72英寸,而1英寸= DPI 像素。 所以一个 X pt 的字,折合 X*DPI/72 px.
Linux系统里面容易混淆的地方是不同地方的设置用的不同的单位. 比如GNOME桌面设置字体的时候,单位是磅(pt)而而Firefox当中字体的设置用的是像素(px).

GNOME设置字体
首先在桌面右键->"Change Desktop Background"->"Font",会出现如下的面板

注意这里的单位是磅(pt)

Firefox设置字体
firefox设置起来很简单. 以Firefox 4为例, 在Edit->Preference->Content->Advanced 面板里, 选择是否允许使用网页指定的字体:
  • 如果你允许网页自己选择字体,则直接跳至fontconfig的设置. 我自己是这么选的---不然网页字体都那么千篇一律,多无聊阿).
  • 如果不允许网页自己指定的字体,则在对于中文(Simplified Chinese)字符集和西文(Western)字符集分别设置Serif, Sans Serif, Monospace三种字型的默认字体.这里字体大小的单位是px(像素),而不是pt(磅).

    但是有时候浏览器自己侦测的网页语言字符集是不对的, 或者有时候就是中文西文混合的(比如上facebook),这样就造成一个两难的局面: 网页语言detect错误时,以英文字体设置显示中文会很丑或者很虚. 如果干脆把浏览器的英文字体也都默认成中文--可是这样上英文网页就会崩溃因为几乎所有的中文字体的英文部分都太丑了.所以需要进行下一步,在fontconfig里做字体替换

fontconfig设置字体
设置fontconfig的作用在于你可以手动设置系统什么时候用什么中文字体.比如系统知道自己要显示一段Sans Serif的文字,英文部分好说,咣咣弄好了, 但是中文部分它不知道得用什么字体.于是你可以在fontconfig里设置,使得它可以用微软雅黑(Microsoft YaHei)来显示那部分中文,这就是字体替换.对自己账户的fontconfig设置在~/.fonts.conf中.
关于fontconfig设置的用法,参考http://linux.die.net/man/5/fonts-conf

这里附上我的~/.fonts.conf文件.
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <match target="pattern">
    <test qual="any" name="family">
      <string>serif</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>Times New Roman</string>
      <string>Times</string>
      <string>Georgia</string>
      <string>Bitstream Vera Serif</string>
      <string>DejaVu Serif</string>
      <string>SimSun</string>
      <string>NSimSun</string>
      <string>Microsoft YaHei</string>
    </edit>
  </match> 
  <match target="pattern">
    <test qual="any" name="family">
      <string>sans-serif</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>DejaVu Sans</string>
      <string>Ubuntu</string>
      <string>Arial</string>
      <string>Helvetica</string>
      <string>Microsoft YaHei</string>
    </edit>
  </match> 
  <match target="pattern">
    <test qual="any" name="family">
      <string>monospace</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>DejaVu Sans Mono</string>
      <string>Courier</string>
      <string>Courier New</string>
      <string>Microsoft YaHei</string>
    </edit>
  </match> 
</fontconfig>
有几点说明:
1 因为宋体(比如SimSun, NSimSun)被认为是Serif, 所以我把他们放在了Serif里
2 黑体(比如Microsoft YaHei)是Sans Serif,所以放在了Sans Serif那类
3 因为所有中文字体大小都一样,所以Monospace那里随便用神马字体都可以, 我个人喜欢Microsoft YaHei

4 comments:

Anonymous said...

Wow, awesome blog format! How long have you been blogging
for? you made running a blog glance easy. The full look of your web site is fantastic, as smartly as the content!


my homepage :: castle clash hack android no survey

Anonymous said...

Its not my first time to pay a quick visit this web site, i am visiting
this web site dailly and obtain fastidious facts from here
every day.

Here is my web blog - test

Anonymous said...

I'll immediately grab your rss feed as I can not find your e-mail subscription link or newsletter service.
Do you have any? Kindly allow me recognise in order that I could subscribe.

Thanks.

Stop by my blog :: call of duty hacks ps3 modern warfare 2 - http://hipsterpunk.com/u/nohallstro/,

yanmaneee said...

air max
nike air max 2018
kobe byrant shoes
yeezy boost 350
kd 12 shoes
hermes bags
kyrie 6 shoes
off white
louboutin shoes
canada goose jacket