Tuesday, August 26, 2008

Web|Kupu:开源的Web富文本编辑器Rich Text

Kupu:开源的Web富文本编辑器
chenkw 翻译   更新:2005-05-05 22:32:12  版本: 1.0   

原文:Rich Web Text Editing with Kupu
作者:Robert Jones
原文发表时间:04/28/2005
译者:chenkw

   我编写web应用程序也有些年头了,但一直苦于找不到一个称心的方法让用户输入并提交任意文本块。HTML表单的TEXTAREA标记可以处理普通文 本,但我的应用程序大多涉及格式更丰富的内容。一个获得富文本(rich text)的办法是,让用户在字处理器中编辑内容然后将文档提交到服务器,但文 档提交后便无法随意处理。原来我最需要的编辑器,就是一个可以嵌入到任何网页中的WYSIWG (What You See Is What You Get,所见即所得)编辑器。天哪,我终于找到了我的理想,它就是----Kupu。
   Kupu是一个开源的JavaScript应用程序,它实现了一个灵活的、全功能(full-featured)的HTML编辑器,不需要任何插件就可 以在网页中运行。它的主要应用是作为内容管理系统(如Zope、Plone)的嵌入式编辑器,提供创建网页的功能。它的设计非常灵活,你几乎可以毫不费力 地将其嵌入到任何web应用程序中。
  当然,你首先需要明白了它的工作原理。和其他一些优秀的软件一样,Kupu只提供了有限的文档,而且源代 码中的注释也少得可怜。我可以理解为什么会这样。开发人员总是将希望有限的时间用于编写新代码,而非撰写文档。然而,这大大降低了其工作对社区的影响。开 源软件除了具有可获得性(availability),还应该具有易用性(accessibility)。
  我撰写本文,希望提高Kupu的易用性,并演示将它嵌入你的应用程序是一个多么简单的过程。它是一个非常优秀的程序,应该被更多的人接受。
  

JavaScript


  Kupu由Paul Everitt、Guido Wesdorp、Philipp von Weitershausen等人编写,它是JavaScript程序设计的一个精彩示例。用这种语言编一个最简单的应用程序对我来说都是一种考验,Kupu让我惊叹不已。
   由于人们意识到XMLHttpRequest的价值,JavaScript目前似乎正处于一个复兴阶段。Google Suggest以及最近给人深刻 印象的Google Maps都采用这种技术,Kupu也不例外。XMLHttpRequest是浏览器和服务器之间的一种后台通信方式,这样就不需要在 每次数据交换时刷新整个页面。更多内容请参考Drew McLellan的文章Very Dynamic Web Interfaces。此外Joel Webber在其blog里撰文剖析了Google Maps的实现技术。
   不幸的是,使用JavaScript,特别是和XMLHttpRequest一起,会让你卷入浏览器兼容性的漩涡。Kupu目前仅在 Mozilla 1.3.1、Internet Explorer 5.5和Netscape Navigator 7.1或更高版本上测试通过。它尚不 能在Konqueror、Operah和Safari上正确运行,但新版本有望支持这些浏览器。
  

安装Kupu


  基本安装是非常简单的。你只需要到 http://kupu.oscom.org/download/ 下载压缩包(450K),然后解压到网站的一个可访问的目录下就行了。它将创建kupu/目录,其下包含一些文件和子目录。你不需要进行make或者其他类似的操作。但我建议你先不要看doc/子目录下的内容。
  在浏览器中输入<your path>/kupu/common/kupu.html(<your path>指kupu所在的路径,你需要补充完整这个URL)就可以看到编辑器界面了。你也可以访问我的Kupu编辑器测试页面。结果如图1所示。

 
  图1 Kupu示例页面

  该页面由三部分组成:工具栏和左右面板。顶部的工具栏包含了编辑器标准功能按钮。右侧面板由一些块(block)组成,每个块为相应功能提供额外信息。左侧面板就是你正在编辑的文档。在本示例中,左侧面板预装入了简要介绍Kupu设计理念的文档。
   在左侧面板内某处单击然后就可以开始输入。如果你使用Firefox浏览器,而上述操作没有反应,那么请按F7键。按F7可以启动"插入浏览" (caret browsing),修从而复这个问题。这个界面有些怪异,但还是挺直观的。选择一些文本然后改变其颜色和格式。创建一些链接。插入源为某 URL的图片并改变其大小。最好再插入一个表格并增删其行列。这个演示程序并不支持保存操作,它将触发一个错误。我将在下文介绍如何修复这个错误。
   Kupu目前的版本也许不能满足你对它作为WYSIWYG编辑器的全部期望。例如,你无法改变字体,只能插入Web上的图片。还有一些功能是不正常的, 至少在我的机器上如此。但其兼容性对多数应用程序来说已经绰绰有余了。若你不想涉及插件或者Java applet,Kupu绝对是一个让人振奋的成就。 它仅仅是一个包含了JavaScript的网页。
  Kupu的实际价值在于它可以嵌入到其他应用程序中。嵌入方式有两种。最简单的用法是使用 POST方法将数据作为一个CGI脚本参数值发送到服务器。另一个用法稍有点复杂,但它是Kupu小组的选择,那就是使用PUT方法和一个简单的CGI脚 本将数据发送到web站点的一个文件中。
  

使用Kupu


  嵌入Kupu利用其功能是以在页面中包含许多 JavaScript文件为代价的。即使像common/kupu.html这样的基本页面中也包含了让人望而生畏的代码。更要命的是,包含文件的链接都 是相对的,这会让你很头疼,若你想在Kupu源程序结构上花些时间的话。你其实不想碰这些代码,但是你需要建立自定义内容和Kupu之间的关联并让一切正 常运行。幸运的是,你可以使用<base>标记,这是一个简单办法。
  将Kupu发布版放到你的站点树(web tree)的某子 目录下。然后在站点树的其他地方为你的网页建立一个目录。将模板页 (kupu/common/kupu.html or kupu/common/kupuform.html)复制过去,并在页面的< head>段中添加<base>标记,指向发布版的common目录。例如:

<head>
<base href="http://www.craic.com/oreilly/kupu/kupu/common/">
</head>

  浏览器将在所有相对链接前附加该URL。注意,它必须是一个包括主机名的完整URL路径;这个规则适用于该页面中的所有相对URL,而不仅是Kupu用到的。在插入图片、样式表、或链向本站其他页面时,务必注意<base>标记的影响。
  

以PUT方法使用Kupu


   整合Kupu的第一种方法是使用PUT方法上传文件到web服务器。PUT虽然也是http协议的一种方法,但在知名度上无法与GET和POST相提并 论。PUT允许上传完整的文件,然后将其置入站点树中。当然,你也可以使用HTML表单和CGI脚本来完成同样任务,但PUT的目的就是为了简化这种过 程。《Publishing Pages with PUT》一文提供了一些背景知识,该文发表于1997年。
   也许大家很少使用PUT方法的主要原因就是其固有的不安全性了。PUT方法有允许任何人提交任何文件到服务器的安全隐患。你需要仔细配置服务器,避开任 何潜在的安全漏洞。任何一个小小的失误都可能给攻击者创造莫大机会。相反,你可以考虑使用POST方法打造一个封闭、安全的系统,通过CGI脚本与之交 互。这将要求更多编程工作,但其风险更易于管理。
  一个PUT请求将发送两行头信息,然后才是请求内容。CONTENT_LENGTH告诉服务 器发送的数据长度。PATH_TRANSLATED指定数据写入哪个文件。你可以将其视为浏览器从服务器获取一个页面的逆过程。Apache httpd 配置默认接受PUT请求,但你还需要配置一个CGI脚本来处理接收的文件。下面是CGI脚本配置示例,你只需要根据你的情况替换掉脚本名就可以了:

Script PUT /cgi-bin/kupu/handle_put.cgi

  以下是一个简单的CGI脚本实现。注意,该脚本中没有考虑安全检查。任何人都可以向运行了该脚本的站点上传文件。在公网站点上安装该脚本时务必添加安全限制。切记!(本示例并非我的站点上运行的应用程序)

#!/usr/bin/perl -w
# handle_put.cgi
# Basic PUT Handling routine with NO SECURITY!

if($ENV{'REQUEST_METHOD'} ne 'PUT') {
  errorMsg("Request method is not PUT");
}

my $filename = $ENV{'PATH_TRANSLATED'};

if(not $filename) {
  errorMsg("PATH_TRANSLATED was empty");
}

my $length = $ENV{'CONTENT_LENGTH'};


if(not $length) {
  errorMsg("CONTENT_LENGTH was empty");
}

# Add Security Checks Here! Limit access to certain
# directories and limit size and/or type of file.  E.g.

if($length > 100000) {
  errorMsg("CONTENT_LENGTH is too large");
}

# Read in the uploaded data
my $content = '';

my $nread = read(STDIN, $content, $length);
# Make the output more readable by adding newlines
$content =~ s/\>\</\>\n\</g> $filename" || errorMsg("Unable to open $filename");
print OUT $content;
close OUT;

# The 204 code signals the transfer was OK but does not 
# update the current page - so you stay in the editor
print qq[Status: 204\n];
print qq[Content-type: text/html\n\n];
print $content;
exit;

sub errorMsg {
  my $msg = shift;
  print qq[Content-type: text/html\n\n];
  print qq[<html><head><title>Error</title></head>\n];
  print qq[<body>\nError: $msg<br>\n</body></html>\n];
  exit;
}

   OK,服务器已经就绪。现在可以设置Kupu使用PUT了。你不需要在网页中指定CGI脚本,因为Apache配置文件中已经预先做好了配置。你所需要 做的只是在页面中指定服务器上用于写入数据的文件。将kupu/common/kupu.html复制到你的目录,按照上文的说明添加< base>标记,这样你就可以从发布版目录中装入JavaScript库了。
  看一眼这个页面的源代码。300多行看了就晕,要命的是其 中不乏非HTML内容。幸运的是,现在只需要其中两行,一行在在头部,另一行在尾部。现在并不需要触动其他内容。在kupu.html的源代码尾部指定页 面装入编辑器。这里,我使用了一个空白页面(kupublank.html)。

<iframe id="kupu-editor" frameborder="0" src="kupublank.html" scrolling="auto">

  接着指定服务器上写入数据的文件。查找<kupuconfig>,从上往下数大约30行左右就是:

<kupuconfig>
<dst>http://www.craic.com/oreilly/kupu/no_such_file.html</dst>

  将<dst>标记中的内容替换为目标文件的URL。web服务器必须拥有该目录的写权限。注意,你指定的是单个文件。在这些示例中,你无法在运行时定义文件名。
  浏览器装入刚配置的编辑器后,输入一些文本,编辑其格式。若想保存文件,只需要单击Kupu工具栏上的磁盘图标。结果如图2所示,好像没什么变化:

 
  图2 编辑文本

  现在转到刚才被定义为目标的URL。你将看到和刚才的编辑时完全一样的结果。当然,目标页面是没有工具栏的,如图3所示:

 
  图3 新创建的页面

  这是一个简单示例,你马上就可以发现一些缺点。例如,你每次保存都会覆盖目标文件。虽然Kupu开发人员偏爱PUT方法,但若将编辑器嵌入到自己的应用程序,特别是CMS(内容管理系统)中,你可能发现在表单中使用编辑器将提供更多的控制。
  

在HTML表单中使用Kupu


  第二种方法是使用表单包装Kupu代码。这需要更多的努力,因为你需要一个CGI脚本来处理数据。不过,相对PUT技术来说,它提供更多的数据控制。PUT只是简单地对编辑器中的东西作了一下镜像。
   下面介绍一个HTML表单示例(kupu/common/kupuform.html)。该页在浏览器中的显示结果和上一个示例(kupu.html) 的一样。页面源代码非常相似,但关键之处略有不同。从上到下数,在第20行左右,你可以找到一个<form>标记:

<form action="http://debris.demon.nl/printpost" method="POST">

  这个URL定义了处理上传文本的脚本。将它替换为你的脚本。从下往上数大约8行,你可以找到一个<iframe>标记:

<iframe id="kupu-editor" frameborder="0"
       src="fulldoc.html" scrolling="auto">

  src属性指定了启动编辑器时装载的文档。本例的装载文档是fulldoc.html。
   若想使用这个表单,只需要改变这些行。首先,将kupuform.html复制到你的目录,并像上例一样添加一个<base>标记来访问 JavaScript函数库。在<iframe>标记中指定一个空白文件kupublank.html,这样编辑器启动时就显示空白文档。
   你需要在服务器上运行一个CGI脚本来处理提交的数据。下面是一个简单的脚本(kupu_echo.cgi),它读入表单传来的数据(该数据随kupu 参数传递),然后直接将该数据返回给用户。Kupu生成的HTML是没有换行的,这给查看源代码带来不便,所以示例脚本中在每个标记后面插入一个换行符。

#!/usr/bin/perl -w
# kupu_echo.cgi

use CGI;
my $cgi = CGI->new();

my $text = $cgi->param('kupu');
$text =~ s/\>/\>\n/g;

print $cgi->header();
print $text;

  将该脚本放到站点的cgi-bin/目录中,将赋予可执行权限:chmod a+x kupu_echo.cgi,然后将其URL替换到<form>标记。这两个标记改变后应该和下面的类似:

<form action="http://www.craic.com/cgi-bin/kupu/kupu_echo.cgi" method="POST">

...

<iframe id="kupu-editor" frameborder="0"
       src="kupublank.html" scrolling="auto">

   在浏览器里打开kupuform.html,你会看到编辑器的左面板为空白。随便敲入一些文本,修改其格式。单击工具栏中的磁盘图标将编辑结果上传到 CGI脚本。如果你想即刻获得满足,那么请访问我的站点上的演示页面。http: //www.craic.com/oreilly/kupu/kupuform.html
  这个过程并不是很麻烦。你只需要改变模板页面的两行代码,然后编写一个简单的CGI脚本。最重要的是,你连一行JavaScript都不需要触动。当然,这只是一个简单示例,但它帮助你度过了学习曲线的第一个阶段。
  

一个规模较大的示例


  我编写了一个基本的网志程序(blogging application),在表单中嵌入Kupu来创建新纪录,这是一个稍微有点复杂的例子。
  它由一个包含了所有Kupu代码的HTML模板文件,以及一个处理记录提交并显示所有记录链接的CGI脚本。这是一个很简单的网志程序,但它演示了如何将编辑器整合到一个包含其他控件的表单。它还介绍了如何修改Kupu模板。
  图4是该程序的截图,图上显示已经加入了几条记录。

 
  图4 Kupu作为weblog编辑器

   除了编辑器之外,示例中还包含一个普通的表单控件<input>,用于输入该纪录的标题。使用表单来整合Kupu,你在充分利用编辑器功能 的同时,还可以加入自己的表单控件。我对模板页作了一些修改,对Kupu的工具栏作了一下调整。你可以到Kupu的主页上去看看不同的CMS系统的截图, 它们都针对自己的应用作了一些修改。如果你真想那么做,那么你需要理解这个JavaScript函数库,而且明白它是如何使用XML进行配置的。不过,这 已经大大超出本文的范围了。
  本程序的源代码太长,就不包含在这里了,你可以到我的Kupu站点下载。
  查看我的Kupu模板页时,你会发现一些形如<!-- ### -->的注释,这表明在该处我修改了原始的kupuform.html模板页。CGI脚本中替换了模板中的Perl风格的变量,如$srcFile。(的确,用PHP将更简单!)我也包含了Perl CGI脚本的源代码。
  你也可以在我的站点上体验一下基于Kupu的网志系统。(我已经删掉了所有记录。)

  

总结


  本文的示例仅仅涉及Kupu的皮毛,但它为你自行体验Kupu或理解Kupu中的XML和JavaScript铺设了道路。这些程序使用了XMLHttpRequest。XMLHttpRequest逐渐在web开发中受到重视,它是一项值得关注的技术。


  Robet Jones经营着Craic Computing,这是一家坐落于西雅图的小型生物仪器公司,为生物技术行业提供高级软件和数据分析服务。多年的程序设计经验使他成了名副其实的分子生物学家。

Search|Extending Django’s database API to include full-text search

Extending Django's database API to include full-text search

A Mercurytide white paper: 1st August 2006.

Introducing Django

Django is an open-source web application framework that—to quote its creators—encourages rapid development and clean, pragmatic design. It is written in Python, and provides components to build high-quality web applications: an object-relational mapper, a full-featured templating system, URL dispatching, session management, authentication, and much more besides.

One valuable measure of a framework is the ease with which it can be extended to meet one's needs. Here we will introduce Django's database API, and demonstrate its flexibility by extending it to support MySQL's full-text search capabilities.

The source code included in this article is released into the public domain.

Django's Database API

Django's object-relational mapper provides a rich API for building database queries.

Django's object-relational mapper provides a rich API for building database queries. This API insulates the application developer from the details of the SQL statements, while still allowing queries with complex selection criteria to be made in an efficient manner. Another notable benefit of this approach is that it removes entirely the possibility of SQL injection attacks, as the application developer is no longer inserting values directly into SQL statements.

Objects are retrieved from the database with a QuerySet, which is an abstraction of an SQL SELECT statement. QuerySets provide methods that narrow down the results to objects matching specific criteria, much like the WHERE clause in an SQL statement—in fact, behind the scenes a QuerySet builds an SQL statement as its methods are called. QuerySet instances are obtained from a model class's Manager instance, which is normally called objects. Here are a few examples of the use of QuerySets:

# Retrieve the QuerySet containing all articles
articles = Article.objects.all()

# Include only articles written before this year
articles = articles.filter(posted_date__lt='2006-01-01')

# But leave out those written by me
articles = articles.exclude(author__exact='Andrew')

# And finally, sort them by rating and date
articles = articles.order_by('rating', 'posted_date')

QuerySets can be filtered and sorted very cheaply, as they are lazily evaluated: these actions manipulate the QuerySet's internal SQL statement, and the statement is not executed until you try to access the results of the QuerySet, by iteration or slicing, for example.

# Get the top five articles; only now is the database accessed.
a = articles[:5]

To extend this interface, we'll develop a Manager subclass and a QuerySet subclass, but first we'll briefly describe MySQL's full text search.

Using the Search Component

We'll now demonstrate the use of these subclasses. Here is a simple model that represents articles posted on a web site; so that it can be searched, we create a SearchManager instance and assign it to objects:

from django.db import models
from fulltext.search import SearchManager

class Article(models.Model):
posted_date = models.DateField(db_index=True)
title = models.CharField(maxlength=100)
text = models.TextField()

# Use a SearchManager for retrieving objects,
# and tell it which fields to search.
objects = SearchManager(('title', 'text'))

class Admin:
pass

def __str__(self):
return "%s (%s)" % (self.title, self.posted_date)

Articles have a title, body text, and the date they were posted. We will define a FULLTEXT INDEX on the title and text columns in the database, and we pass a tuple of the corresponding field names to the SearchManager instance to declare that here. Here's the SQL statement that creates the index:

CREATE FULLTEXT INDEX fulltext_article_title_text
ON fulltext_article (title, text);

Given a Django project, an application that defines the Article model, and a database populated with suitable articles, the full text search can be easily demonstrated with Python's interactive interpreter:

>>> # How many articles are there?
>>> len(Article.objects.all())

12

>>> # Find articles about frameworks:
>>> Article.objects.search('framework')

[<Article: Introducing Django (2006-07-17)>,
<Article: Django on Windows HOWTO (2006-04-03)>,
<Article: Django for non-programmers (2006-05-02)>]


>>> # Show the relevance score for these articles:
>>> [(a, a.relevance)
... for a in Article.objects.search('framework')]

[(<Article: Introducing Django (2006-07-17)>, 1.0980),
(<Article: Django on Windows HOWTO (2006-04-03)>, 0.6015),
(<Article: Django for non-programmers (2006-05-02)>, 0.5842)]


>>> # Limit the results to those posted before June:
>>> Article.objects.search('framework').filter(
... posted_date__lt='2006-06-01')

[<Article: Django on Windows HOWTO (2006-04-03)>,
<Article: Django for non-programmers (2006-05-02)>]


>>> # Note that filter() also returns a SearchQuerySet:
>>> Article.objects.filter(
... posted_date__lt='2006-06-01').search('framework')

[<Article: Django on Windows HOWTO (2006-04-03)>,
<Article: Django for non-programmers (2006-05-02)>]

Final Remarks

I'll now let you in on a secret: since the start of June, Django has supported a search operator for queries that uses MySQL's full text search:

# This uses the Boolean search mode, not
# natural language queries
Article.objects.filter(title__search='+Django -Rails')

However, the techniques demonstrated in this article can be built upon extend the database API to support any SQL feature, whether that's supporting full text search, grouping and aggregate queries, or any other SQL feature or database-specific extension.

Technorati tags
;
;
;
;
; and
.

References

Mercurytide is a forward thinking, dynamic, and innovative Internet applications development company. We are well-established with a proven track-record within the industry of attracting blue chip clients from around the world. We produce monthly white papers on a variety of technology-orientated topics. For more details see the Mercurytide web site.

Django|Tinymce rich text editor in Django


http://tinymce.moxiecode.com/dump.php?example=true

前几天搭好的0.0.1版是纯文本化的,除非自己手动<b></b>才能显示粗体。因此得安装一个富文框是必须的。

我打算用TinyMCE,它是一个非常流行的富文本编辑器,100% JavaScript,WordPress也是用它的,所以理所当然就选择TinyMCE了。

参考了Django网站上的AddWYSIWYGEditor - Django Code - Trac,搞了很久都不成功,也失败。

只好在论坛请教,得到了详细的帮助。终于搞成功了。记录一下!

―-

帖子在这里: How to enable the rich editor in Django?

Michael很热心,给我回复了这么多。真是非常感谢他!

首先,根据Michael的意见,尽量使所有自己加入的media都不与系统默认的admin media相关联,即自己设定一个新目录来放media,不要放在系统目录里,方便布署。

所以,我把tiny_mce安装在工程目录下的mymedia/js这个文件夹下,并新建一个文件textareas.js,作为tiny_mce的配置文件:

tinyMCE.init({
mode : "textareas",
theme : "advanced"
});

然后,在工程根目录的urlpatterns中,加一个url即可:

(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': './mymedia'}),

最后,在所有你想将TextField用Tiny_MCE来编辑的Models里,加入:

class Admin:
js = (
'/media/js/tiny_mce/tiny_mce.js',
'/media/js/textareas.js',
)

这里需要注意,虽然上面urlpattern中我指定了"文档根目录"是./mymedia,但是在Models中设定路径,依旧是/media/,而非mymedia。似乎这个/media/只是一个变量,就是因为这个我搞了很久。

弄好以后,管理后台不会因为新设定的mymedia而受影响,新的mymedia只是一个附加品吧。看截图:

关于Django的其他补充:

  • Django的API还真太会break了,网上很多人说建议使用最新的svn版也就是这个原因了。0.96版定义CharFiled的长度是用maxlength,到了0.97(还未发布)就是用max_length了。
  • Google Code上有很多用Django搞的开源项目,当然也包括很多Blog系统,这样我就有很多可以参考和学习的例子了。其中有个国人写的fallever非常不错,他本人的主页http://www.fallever.com也是用fallever搭的,学习的对象。
  • 国内也有Python的主机可以买,似乎不是很贵。其中jeff(fallever的作者)就是买这个"浪点主机",以后我也可以考虑一下,还能支持SSH,国内的应该很快。

Django|Static Files

Django and Static Files

One of the most common questions that comes up in #django is about static files. Why are my images not loading? How do I make Django serve my CSS files? The answer depends on the environment. You might want to hook up django.views.static.serve in development and Alias in production. I want to help clarify all static file questions.

How do I make Django serve static files?

It is possible to setup Django to serve your static files. This functionality is off by default. Django is a web application framework. It is not intended to serve any static files. It might serve static content which is cached in memcached, the database or on the filesystem. The method to setup static file serving in Django is disclaimed in the documentation:

Using this method is inefficient and insecure. Do not use this in a production setting. Use this only for development.

Check out the static documentation for Django to get more information about how to set it up with your urls.py. Lets cover what settings that Django has that I will use through out the rest of this article:

MEDIA_ROOT
The default value is "" (an empty string). Django uses MEDIA_ROOT as the root path for the upload_to parameter for FileField and ImageField. It is typically the place where you will want to make publically available through your web server.
MEDIA_URL
The default value is "" (an empty string). There is nothing magically about MEDIA_URL. It is more of an aide to you to properly map static files to the correct location in your templates. You can get MEDIA_URL defined in all of your templates by using django.core.context_processors.media in your TEMPLATE_CONTEXT_PROCESSORS. This is turned on by default if you are using SVN revision 5379 or newer. However, you need to ensure you are using the RequestContext as your context in reach view that renders a template.

A quick tip serving static files in development

I can't say that I invented this idea, because I originally found it in Satchmo. Create a new settings variable called LOCAL_DEVELOPMENT. Set the value to either True or False depending on whether the project is local or on a production server. Then make your urls.py look similar to this:

from django.conf import settings
from django.conf.urls.defaults import *

urlpatterns = patterns("",
# normal url patterns here.
)

if settings.LOCAL_DEVELOPMENT:
urlpatterns += patterns("django.views",
url(r"%s(?P<path>.*)/$" % settings.MEDIA_URL[1:], "static.serve", {
"document_root": settings.MEDIA_ROOT,
})
)

Keep in mind that if you have a url pattern that consumes your MEDIA_URL before getting to the new one I show you above then you will need to restructure things. Also keep in mind that if your MEDIA_URL is an absolute URL then this will clearly not work. If you have separated your settings.py file to allow for different settings in different locations then just override your main MEDIA_URL to be something that can be used in the urlpatterns.

UPDATE: It has come to my attension that the above code was wrong and has now been corrected. It used to allow settings.MEDIA_URL pass through to the regex, but that was incorrect when settings.MEDIA_URL has a leading slash as it should to be properly referenced in the templates. I am now using settings.MEDIA_URL[1:] to eat the leading slash.

Serving static files in production

I will describe how to accomplish static file serving using Apache. This is the easiest method to configure considering you are probably deploying to Apache. Here is a quick example of how you might setup Django within Apache and mod_python:

<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On
</Location>

This works well, but as you can see if a request is made to /site_media/css/layout.css then it is passed to Django. Then Django would need to resolve it and return something back to client. If you accidently left LOCAL_DEVELOPMENT turned on then this will magically work and you may ignore that and assume everything is okay. However, this is very bad because now Django is being forced to serve static files when the web server should be responsible for that. Apache can serve the static files much more efficiently than Django can. So, let's add on to our Apache configuration:

<Location "/site_media">
SetHandler None
</Location>

Okay, now we have told Apache that when a request is made with /site_media as the beginning portion of the path to do nothing. We are not done yet, because we need to tell Apache to map requests from /site_media to the files located on the file system. This can be done using Apache's Alias directive:

Alias /site_media/ /path/to/media
<Location "/site_media">
SetHandler None
</Location>

Replace /path/to/media to your MEDIA_ROOT path. Setting up the admin media files is done the same way. Just map ADMIN_MEDIA_PREFIX to the location of the admin media files. The location will vary from system to system, but the path inside the Django source tree is django/contrib/admin/media. Be sure to find out where your Django source tree resides and map it accordingly. Also, keep in mind you may need to look at the permissions to ensure the webserver user that Apache runs as has the permission to read those files. This applies to all static files being served by Apache.

This wraps up how to serve static files in a Django environment. I am looking forward to expand on this article and cover Cherokee and the environment that is required for that setup.

Monday, August 25, 2008

Django|Mod_python settings

Django的Apache/mod_python配置



主要看看httpd.conf:

  1. MaxRequestsPerChild  1  
  2.   
  3. LoadModule python_module modules/mod_python.so   
  4.   
  5. <Location "/">   
  6.     SetHandler python-program   
  7.     PythonPath "['D:/py'] + sys.path"  
  8.     PythonHandler django.core.handlers.modpython   
  9.     SetEnv DJANGO_SETTINGS_MODULE mysite.settings   
  10.     PythonAutoReload Off   
  11.     PythonDebug On   
  12. </Location>   
  13.   
  14. Alias /sitemedia D:/py/mysite/media   
  15.   
  16. <Directory "D:/py/mysite/media">   
  17.     Order allow,deny   
  18.     Allow from all   
  19. </Directory>   
  20.   
  21. <Location "/sitemedia">   
  22.     SetHandler None   
  23. </Location>   
  24.   
  25. <LocationMatch "\.(jpg|gif|png|css|js)$">   
  26.     SetHandler None   
  27. </LocationMatch>  

添加和修改上面这些就可以了,别的不要动
MaxRequestsPerChild设为1特别有用,不用重启Apache就可以看到程序修改的结果
PythonPath "['D:/py'] + sys.path"注意加的是D:/py,也就是我们运行django-admin.py startproject mysite时所在的目录
SetEnv DJANGO_SETTINGS_MODULE mysite.settings中mysite是project名
Alias /sitemedia D:/py/mysite/media的意思是把media目录设置成为/sitemedia这样的URL访问方式,紧接着的Directory给任何人访问该目录下文件的权限,默认无权限
然后Location告诉Apache /sitemedia下是静态内容不用python解释
然后LocationMatch告诉Apache列表中结尾的文件是静态内容不用python解释
当我们需要引入其它站点的jpg或css时这个LocationMatch就有用了
我们设置模板的绝对路径来访问:
Java代码 复制代码
  1. TEMPLATE_DIRS = (   
  2.     # Put strings here, like "/home/html/django_templates".   
  3.     # Always use forward slashes, even on Windows.   
  4.     'D:/py/mysite/templates',   
  5. )  

然后模板目录下的html就可以使用下面的方式访问静态内容了:
Java代码 复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  2. <html xmlns="http://www.w3.org/1999/xhtml">   
  3. <head>   
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />   
  5. <title>测试页面   
  6. </title>   
  7. <meta name="description" content="python,Django" />    
  8. <link href="/sitemedia/stylesheet/blue.css" media="screen" rel="Stylesheet" type="text/css" />   
  9. </head>   
  10. <body>   
  11. {{ content }}   
  12. <a href="http://hideto.javaeye.com">hideto@蛙眼</a>   
  13. <img src="http://hideto.javaeye.com/upload/user/logo/34344/f5e9dd3a-a090-4697-828b-edb8836c221a.jpg"></img>   
  14. </body>   
  15. </html>  

How to use Django with mod_python

This document is for Django's SVN release, which can be significantly different from previous releases. Get old docs here: 0.96, 0.95.

Apache with mod_python currently is the preferred setup for using Django on a production server.

mod_python is similar to mod_perl : It embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements.

Django requires Apache 2.x and mod_python 3.x, and you should use Apache's prefork MPM, as opposed to the worker MPM.

You may also be interested in How to use Django with FastCGI, SCGI or AJP (which also covers SCGI and AJP).

Basic configuration

To configure Django with mod_python, first make sure you have Apache installed, with the mod_python module activated.

Then edit your httpd.conf file and add the following:

<Location "/mysite/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonOption django.root /mysite
PythonDebug On
</Location>

…and replace mysite.settings with the Python import path to your Django project's settings file.

This tells Apache: "Use mod_python for any URL at or under '/mysite/', using the Django mod_python handler." It passes the value of DJANGO_SETTINGS_MODULE so mod_python knows which settings to use.

New in Django development version: Because mod_python does not know we are serving this site from underneath the /mysite/ prefix, this value needs to be passed through to the mod_python handler in Django, via the PythonOption django.root ... line. The value set on that line (the last item) should match the string given in the <Location ...> directive. The effect of this is that Django will automatically strip the /mysite string from the front of any URLs before matching them against your URLConf patterns. If you later move your site to live under /mysite2, you will not have to change anything except the django.root option in the config file.

When using django.root you should make sure that what's left, after the prefix has been removed, begins with a slash. Your URLConf patterns that are expecting an initial slash will then work correctly. In the above example, since we want to send things like /mysite/admin/ to /admin/, we need to remove the string /mysite from the beginning, so that is the django.root value. It would be an error to use /mysite/ (with a trailing slash) in this case.

Note that we're using the <Location> directive, not the <Directory> directive. The latter is used for pointing at places on your filesystem, whereas <Location> points at places in the URL structure of a Web site. <Directory> would be meaningless here.

Also, if your Django project is not on the default PYTHONPATH for your computer, you'll have to tell mod_python where your project can be found:

<Location "/mysite/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonOption django.root /mysite
PythonDebug On
PythonPath "['/path/to/project'] + sys.path"
</Location>

The value you use for PythonPath should include the parent directories of all the modules you are going to import in your application. It should also include the parent directory of the DJANGO_SETTINGS_MODULE location. This is exactly the same situation as setting the Python path for interactive usage. Whenever you try to import something, Python will run through all the directories in sys.path in turn, from first to last, and try to import from each directory until one succeeds.

An example might make this clearer. Suppose you have some applications under /usr/local/django-apps/ (for example, /usr/local/django-apps/weblog/ and so forth), your settings file is at /var/www/mysite/settings.py and you have specified DJANGO_SETTINGS_MODULE as in the above example. In this case, you would need to write your PythonPath directive as:

PythonPath "['/usr/local/django-apps/', '/var/www'] + sys.path"

With this path, import weblog and import mysite.settings will both work. If you had import blogroll in your code somewhere and blogroll lived under the weblog/ directory, you would also need to add /usr/local/django-apps/weblog/ to your PythonPath. Remember: the parent directories of anything you import directly must be on the Python path.

Note

If you're using Windows, we still recommended that you use forward slashes in the pathnames, even though Windows normally uses the backslash character as its native separator. Apache knows how to convert from the forward slash format to the native format, so this approach is portable and easier to read. (It avoids tricky problems with having to double-escape backslashes.)

This is valid even on a Windows system:

PythonPath "['c:/path/to/project'] + sys.path"

You can also add directives such as PythonAutoReload Off for performance. See the mod_python documentation for a full list of options.

Note that you should set PythonDebug Off on a production server. If you leave PythonDebug On, your users would see ugly (and revealing) Python tracebacks if something goes wrong within mod_python.

Restart Apache, and any request to /mysite/ or below will be served by Django. Note that Django's URLconfs won't trim the "/mysite/" ― they get passed the full URL.

When deploying Django sites on mod_python, you'll need to restart Apache each time you make changes to your Python code.

Multiple Django installations on the same Apache

It's entirely possible to run multiple Django installations on the same Apache instance. Just use VirtualHost for that, like so:

NameVirtualHost *

<VirtualHost *>
ServerName www.example.com
# ...
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</VirtualHost>

<VirtualHost *>
ServerName www2.example.com
# ...
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings
</VirtualHost>

If you need to put two Django installations within the same VirtualHost, you'll need to take a special precaution to ensure mod_python's cache doesn't mess things up. Use the PythonInterpreter directive to give different <Location> directives separate interpreters:

<VirtualHost *>
ServerName www.example.com
# ...
<Location "/something">
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonInterpreter mysite
</Location>

<Location "/otherthing">
SetEnv DJANGO_SETTINGS_MODULE mysite.other_settings
PythonInterpreter othersite
</Location>
</VirtualHost>

The values of PythonInterpreter don't really matter, as long as they're different between the two Location blocks.

Running a development server with mod_python

If you use mod_python for your development server, you can avoid the hassle of having to restart the server each time you make code changes. Just set MaxRequestsPerChild 1 in your httpd.conf file to force Apache to reload everything for each request. But don't do that on a production server, or we'll revoke your Django privileges.

If you're the type of programmer who debugs using scattered print statements, note that print statements have no effect in mod_python; they don't appear in the Apache log, as one might expect. If you have the need to print debugging information in a mod_python setup, either do this:

assert False, the_value_i_want_to_see

Or add the debugging information to the template of your page.

Serving media files

Django doesn't serve media files itself; it leaves that job to whichever Web server you choose.

We recommend using a separate Web server ― i.e., one that's not also running Django ― for serving media. Here are some good choices:

If, however, you have no option but to serve media files on the same Apache VirtualHost as Django, here's how you can turn off mod_python for a particular part of the site:

<Location "/media">
SetHandler None
</Location>

Just change Location to the root URL of your media files. You can also use <LocationMatch> to match a regular expression.

This example sets up Django at the site root but explicitly disables Django for the media subdirectory and any URL that ends with .jpg, .gif or .png:

<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</Location>

<Location "/media">
SetHandler None
</Location>

<LocationMatch "\.(jpg|gif|png)$">
SetHandler None
</LocationMatch>

Serving the admin files

Note that the Django development server automagically serves admin media files, but this is not the case when you use any other server arrangement. You're responsible for setting up Apache, or whichever media server you're using, to serve the admin files.

The admin files live in (django/contrib/admin/media) of the Django distribution.

Here are two recommended approaches:

  1. Create a symbolic link to the admin media files from within your document root. This way, all of your Django-related files ― code and templates ― stay in one place, and you'll still be able to svn update your code to get the latest admin templates, if they change.
  2. Or, copy the admin media files so that they live within your Apache document root.

Using eggs with mod_python

If you installed Django from a Python egg or are using eggs in your Django project, some extra configuration is required. Create an extra file in your project (or somewhere else) that contains something like the following:

import os
os.environ['PYTHON_EGG_CACHE'] = '/some/directory'

Here, /some/directory is a directory that the Apache webserver process can write to. It will be used as the location for any unpacking of code the eggs need to do.

Then you have to tell mod_python to import this file before doing anything else. This is done using the PythonImport directive to mod_python. You need to ensure that you have specified the PythonInterpreter directive to mod_python as described above (you need to do this even if you aren't serving multiple installations in this case). Then add the PythonImport line in the main server configuration (i.e., outside the Location or VirtualHost sections). For example:

PythonInterpreter my_django
PythonImport /path/to/my/project/file.py my_django

Note that you can use an absolute path here (or a normal dotted import path), as described in the mod_python manual. We use an absolute path in the above example because if any Python path modifications are required to access your project, they will not have been done at the time the PythonImport line is processed.

Error handling

When you use Apache/mod_python, errors will be caught by Django ― in other words, they won't propagate to the Apache level and won't appear in the Apache error_log.

The exception for this is if something is really wonky in your Django setup. In that case, you'll see an "Internal Server Error" page in your browser and the full Python traceback in your Apache error_log file. The error_log traceback is spread over multiple lines. (Yes, this is ugly and rather hard to read, but it's how mod_python does things.)

If you get a segmentation fault

If Apache causes a segmentation fault, there are two probable causes, neither of which has to do with Django itself.

  1. It may be because your Python code is importing the "pyexpat" module, which may conflict with the version embedded in Apache. For full information, see Expat Causing Apache Crash.
  2. It may be because you're running mod_python and mod_php in the same Apache instance, with MySQL as your database backend. In some cases, this causes a known mod_python issue due to version conflicts in PHP and the Python MySQL backend. There's full information in the mod_python FAQ entry.

If you continue to have problems setting up mod_python, a good thing to do is get a barebones mod_python site working, without the Django framework. This is an easy way to isolate mod_python-specific problems. Getting mod_python Working details this procedure.

The next step should be to edit your test code and add an import of any Django-specific code you're using ― your views, your models, your URLconf, your RSS configuration, etc. Put these imports in your test handler function and access your test URL in a browser. If this causes a crash, you've confirmed it's the importing of Django code that causes the problem. Gradually reduce the set of imports until it stops crashing, so as to find the specific module that causes the problem. Drop down further into modules and look into their imports, as necessary.


Wednesday, August 20, 2008

Putty| putty输入中文 - Linux/Unix编程

putty输入中文 - Linux/Unix编程 - 工作中用到的东西

putty输入中文
默认状态下向putty中输入中文会变成...可以用以下方法解决:
Category中选择Window=>Appearance=>change...将字体设置为Fixedsys字符集设
置为CHINESE_GB2312
然后别忘了save一下要不就得每次更改了。
主要是要把字符集设置为CHINESE_GB2312。

Saturday, August 16, 2008

Linux| 字体微调 - windows 效果版 [转]

Linux 字体微调 - windows 效果版 [转] - Linux 字体 - 风间流云

Linux 字体微调 - windows 效果版 [转]
原文地址:http://www.linuxsir.org/bbs/showthread.php?t=266659


Linux 字体微调 - windows 效果版

作者:quanliking

转载请注明:来自 http://www.linuxsir.org

适用范围:针对 LCD 用户,CRT 用户做少量修改。

实验系统:Debian\Ubuntu,其它发行版应该也适用。

目的:使 linux 下的字体显示达到和 windows 下一样的效果。

一、必须掌握的概念 DPI (以下讨论先避免 sub-pixle 子像素渲染技术,以免混淆)
看过无数流行的汉化或字体美化的文章,往往存在一个致命伤,即忽略了 DPI 这个重要概念,因此是不完整的。

DPI 原来是印刷上的记量单位,意思是每个英寸上,所能印刷的网点数(Dot Per Inch)。但随着数字输入,输出设备快速发展,大多数的人也将数字影像的解析度用DPI表示,但较为严谨的人可能注意到,印刷时计算的网点(Dot)和 电脑显示器的显示像素(Pixel)并非相同,所以较专业的人士,会用PPI(Pixel Per Inch)表示数字影像的解析度,以区分二者。而在本文中我们约定两个概念可以互换,即 DPI 就是 PPI。
参考: Coming to Terms with DPI, PPI and Size

问题一: dpi 和我们的 LCD 屏有什么关系呢?
把眼睛凑近屏幕,容易发现我们的屏幕是由一个个小方格组成,我们称这些方格为像素,LCD 中有个术语叫屏幕的 native resolution(物理解析度),指的的就是屏幕所包含的像素数量,比如我的 TFT LCD 14" native resolution 为 1024x768,即水平由 1024 个像素组成,垂直由 768 个像素组成。通常让我们的 LCD 工作在 native resolution,成像效果最好。

通过控制像素的开关和色彩的组合来显示我们需要的图像,但不要忽略了屏幕像素的数量是有限的,所以往往在低解析度的显示设备上看到的图像并不十分清晰,比 如在观察白底黑字(或黑底白字)的文字时,文字的边缘有尖锐的锯齿状(阶梯状),长时间观看,眼睛很容易疲乏,这也就是很多人喜欢看纸制书籍的一个重要原 因,制作精良的纸张,它包含的"像素"可以达到很高,边缘看起来非常的流畅。

问题二: 有什么办法可以解决或缓解上述缺陷呢?
除了通过各种辅助技术(hinting,anti-alias,sub-pixel,etc.)改善成像效果外,最快捷的途径是换一台高解析度(高分屏或高 dpi) 的屏幕,简单说像素越精细越好。

讲到这里你可能很关心自己的屏幕的 dpi 具体为多少?(拿去和别人炫耀 :)
到这里找找看有没有你的 LCD dpi 信息: http://www.prismo.ch/comparisons/notebook.php

问题三: 如果找不到,如何来手工计算?
找把尺子,把 LCD 屏可视区域的宽和高量一下,比如我的屏幕 width = 287 mm = 28.7 cm,height = 215 mm = 21.5 cm
换算比例: 1 cm = 0.39 inch, 1 inch = 2.54 cm
水平 dpi = 水平 resolution * 2.54 / width = 1024 * 2.54 / 28.7 = 90.6
垂直 dpi = 垂直 resolution * 2.54 / height = 768 * 2.54 / 21.5 = 90.7

故我的 dpi 大概为 90x90,在某些机子上水平 dpi 和 垂直 dpi 会不相当,这是很正常的。

问题四: 下面我们看看自己的单个像素具体有多大?
术语: pixel pitch - 像素间距,即单个像素的水平宽度,类似于 CRT 的 dot pitch(点距)
我的 TFT 14" 1024x768
pixel pitch = 28.7 / 1024 = 0.028 cm = 0.28 mm

而一台 T43 高分屏 SXGA+ 14" 1400x1050, pixel pitch = 0.2047 mm
可以看出 SXGA+ 14" 要精细的多。羡慕啊!

问题五: 我要显示一个 px = 12 的文字,实际看起来会有多大?
术语: [color=red]px(pixel size) - 显示单个文字需要的水平像素数量
实际大小 = px * pixel pitch
TFT 14" 1024x768: 12 * 0.28 = 3.36 mm
SXGA+ 14" 1400x1050: 12 * 0.20 = 2.4 mm

看到这里高分屏的用户应该明白为什么同样是 12 px 的 SimSun 点阵字体(bitmap font),在自己的屏幕上看起来要小很多,因为你的 pixel pitch 小。pixel pitch 小不是更好么?
不是不好,而是字太小了,看起来太累,为了保护你的视力,你应该选择大一些的 px。
但要注意中文点阵字体提供商并没有为每个 px 制作点阵汉字,因为中文点阵都是手工绘制的,需要很高的专业技能,非常耗时,故一般只绘制最常用的 px,而且点阵的一个最大的缺点就是不能够缩放,每个点阵汉字的每一个像素都是事先安排好的,缩放后打乱了原有的布局,比如出现破碎,高高低低之类的现 象,使字体变得非常丑陋。
好在 winxp 带的 simsun.ttc 除了 outline 字体外,还包含了丰富的点阵,ppem(pixels per em)分别为 12px, 13px, 14px, 15px, 16px, 18px,你可以根据你的需要选择。例如选择 16px:
实际大小 = 16 * 0.20 = 3.2 mm,这样看起来和 14" TFT 上的 12 px 字就差不多大小了。

当然因为 ppem 不同(或 px 不同),绘制点阵字体时的空间自由度也不同,出来的点阵效果也不同,而且竖线横线比划一般都只占用一个像素宽度,在高分屏上 16px 看起来会有细胳膊细腿的感觉,这也就是为什么不少人会觉得 12px 在 14" 1024x768 上会比较好看,比较符合东方人对中文的审美观。

要注意,由于点阵字体以上的缺陷,通常用在低解析度的屏幕上,现在随着技术的发展,屏幕解析度逐步提高,越来越流行使用 outline 字体,这种字体可以随意的缩放,而不影响形状,现在高分屏用户可以尝试用黑体,如 mac os 带的华文细黑或微软新出的雅黑,黑体比划很粗,适合一部分人的审美观,在大屏幕上效果比较好。


问题六:既然 dpi 都不一样,我要去和别人讨论字体大小,如何讲清楚该字体究竟有多大呢?
真是一个难题,显然用 pixel 这个相对度量去讨论是不合适的,谁也说不清。所以这里我们引入另外一个重要单位 point(磅)。
1 point = 1/72 inch

这里的 inch 就是实际生活中使用的 inch。没错 point 才是绝对度量,比如一个 9 point(9磅)的字的实际大小:
9 * 1/72 = 1/8 inch = 1/8 * 2.54 cm = 3.175 mm
这样的话,大家才能从同一个标准出发讨论问题。所以你看到 MS office word 里的 size,都是以 point 为单位的。
注意,不同家族的字体,如 Arial 和 Verdana 字体,由于设计思路不同,同样磅值的字,看起来 Verdana 感觉要大一些。
Arial VS Verdana

不要让 windows 蒙蔽了你的眼睛!
由于显示器市场的混乱,不同产品 dpi 都不同,为了统一,windows 默认设置 dpi 为 96。注意这里的 dpi 不是你机器的实际 dpi,而是 windows 给你强加上去的。
统一的 96 dpi 有它的优点,因为市面上大多数的显示器 dpi 和这个值接近,而且大多数人都是用 wndows 来设计网页,因此无形中 96dpi 成了一个 de facto 上的标准。但缺点也是很明显的,设计师在 96 dpi 显示器上设计的网页到了 120 dpi 的机器上看起来可能效果会差强人意。所以现在有人建议设置 em 值,这个已经超出我的知识范围。

问题七:你在自己机器上看到的 9 point 还是 9 point 大么?
初看这句话好像自相矛盾,难不成 1 != 1 了!小学老师欺骗了俺?!

别着急!因为你打开 MS word,字体设置栏里看到的 9 point 字,很可能是错误的信息。
假如你运气很好,你的机器实际 dpi 刚好为 96 dpi,那么恭喜你,你没受骗,你看到的是正确信息,如果你的实际 dpi 和 96 相差比较大,那么你看到的是虚假信息。为什么这么讲呢?
我们来做几道小学数学题:

文字在 LCD 上成像,不管是 bitmap font 还是 outline 字体,最终都是以像素绘制。

比方我有一台实际 96 dpi 的 LCD,我要显示一个 9 point 大小的字,我们来看看具体需要多少像素 px?

换算公式: px = pt * dpi / 72

96 dpi, 9 pt: px = 9 * 96 / 72 = 12
操作系统水平要用 12 个像素来描绘该字。
屏幕上看起来有多大?
12 * pixel pitch

假如我通过各种手段人为的把 dpi 改成了 72,来算一下需要的像素值 px?
72 dpi, 9 pt: px = 9 * 72 / 72 = 9
屏幕大小:9 * pixel pitch
注意 pixel pitch 是不变的,计算方法见上文。

现在操作系统就以 9 个像素给你描绘该字,显然比 12 像素描绘出来的小。
这也就是为什么很多人抱怨 Debian/Ubuntu 下默认的 75 dpi ,设置为 9 ponit 的字会看起来比 windows 96 dpi 下小的多的缘故。
此时最可能发生的做法就是,把 9 point 改成更高的 point,而不知其实是 dpi 在跟你开玩笑呢!正确做法,应该先调整好系统 dpi,使之接近你的实际 dpi。

再结合高分屏来讨论一下。
问题八: 同样显示一个 9 point 字(即实际大小一样),显然在 TFT 上需要描绘该字的像素比较少,因为像素个头比较大,而在 SXGA+ 上用来描绘的像素多,个头细,哪个会看起来更漂亮些呢?
我猜你会毫不犹豫选择后者,好比用一个高 dpi 的数码相机拍摄同一个场景,拍出来的效果比低 dpi 要好的多。
但我只能部分同意,还要看你选择的是哪种字体。
如果你正在使用 outline 字体,它是可以任意缩放的,明显同样大小在 SXGA+ 上的表现要好很多,即字体边缘更加光滑流畅,好比在一张好的和画布上作画。
点阵字体要分情况讨论,不要忘了点阵字体是不能缩放的,而且 px 并不完整,只绘制了一些最常用的 px。
例如:
拿两台实际 dpi 分别为 96 和 72 的机器来讨论,同样显示 9 point 的点阵汉字。
上面已经计算过,dpi 96 要用 12 px, dpi 72 要用 9 px 绘制该字。
而使用的字体只带了 9px, 10px, 11px, 13px,14px,15px .....,恰好没提供 12px。
此时 96 dpi 机器上的 9 point 汉字,将会非常丑陋,因为此时的 12px 点阵是机器为你合成的,现在的 AI 还没那么智能,远没达到和人类一样的审美能力。就好比一台高性能手机,可惜不在服务器,任你本事多强,也无用武之地,多郁闷啊!
而 72 dpi 的机器,9 point 刚好落在 9px,此时的字体比划端端正正,自然效果会好的多。
以上的讨论也正说明了点阵字体的缺点,很不灵活。

所以在后面的讨论中,对于 outline 字体,我们用 point size 讨论, 直观方便。而对于 simsun 内嵌的点阵(bitmap font)会用 pixel size 来控制比较好,因为用 point size 的话,还要进行换算,看看是否刚好落在特定的 px,而换算要用到 dpi,个人的实际 dpi 又不尽相同,事先没法得知,故用 point size 是不合适的。

总结一下,dpi 在这里就像是一个桥梁,将 point size 转化为需要的 pixel size。

听了这么多,你也累了,休息一下吧!然后我们一起来实践一下,注意 Linux 将会有多个需要调整 dpi 的地方,希望你能区分清楚。

1、告诉 X server 你的实际 dpi
Debian/Ubuntu 下 X server 默认会使用 75 dpi,可以通过以下命令查看你的 X server 的 dpi 设置:
代码:
$ xdpyinfo |grep resolution
resolution: 75x75 dots per inch
因为我的实际 dpi 为 90, 故 75 对我来说低了。如果让 X server 工作在正确的 dpi,有多种途径,而且不同发行版略有差异,由于篇幅所限,只能部分介绍,具体请参考:
Mozilla DPI-related Font Size Issues on Unix
Solving the Linux DPI Puzzle
引用其中的几句话,简单看一下有哪几种和它们的优先级:
引用:
How the XFree86 and Xorg servers calculate DPI

The DPI of the X server is determined in the following manner:

1. The -dpi command line option has highest priority.
2. If this is not used, the DisplaySize setting in the X config file is used to derive the DPI, given the screen resolution.
3. If no DisplaySize is given, the monitor size values from DDC are used to derive the DPI, given the screen resolution.
4. If DDC does not specify a size, 75 DPI is used by default.

You can check what DPI your X server is set to by running xdpyinfo | grep resolution in a terminal window.
对于 Debian/Ubuntu 我采用方法 2,下面的设置改成你自己的,注意 dpi 的数值最好为 6 或 12 的倍数,原因:
引用:
As above, if this works, but at less than 100% satisfaction, tweak the number lower or higher to suit your preference. If possible, use a number that is a multiple of 6, or even 12, as numbers that aren't sometimes result in annoying rounding errors that cause adjacent bitmap font sizes to not increment and decrement linearly.
同时你可以更加自己的满意程度,进行微调。
代码:
$ vi /etc/X11/xorg.conf 加入
Section "Monitor"
......
DisplaySize 287 215 # 1024x768 90dpi
......
EndSection
这里的 287, 215 即我的实际显示区域的宽和高,单位为 mm。


重启 X 后,生效。以 gdm 为例:
代码:
$ /etc/init.d/gdm restart
查看是否生效:
代码:
$ xdpyinfo |grep dimensions
dimensions: 1024x768 pixels (289x217 millimeters)
$ xdpyinfo |grep resolution
resolution: 90x90 dots per inch
告诉 X server 实际 dpi 后,最直观的感觉就是用 gdm 登录管理器,在使用默认 75dpi 下,gdm Option 菜单和输入框中的字体会显得较小,其它的变化待大家慢慢发觉吧!

2、fontconfig/xft
对于传统的程序或系统,使用 legacy x font server,简称 xfs,那么上面的做法应该已经足够,但对于现在的程序来说,通常使用 fontconfig/xft,还得做进一步的工作。
引用:
Unfortunately, variations in the implementations of X mean display size reconciliation as above won't be a solution on every system. Failure here seems to be common on newer systems using fontconfig instead of legacy xfs
代码:
$ vi /etc/fonts/local.conf 或 vi ~/.fonts.conf 加入
<!--
Target dots per inch
-->
<match target="pattern">
<edit name="dpi" mode="assign" >
<double>90</double>
</edit>
</match>
fontconfig 将根据该 dpi 值来进行字体的 point size 到 pixel size 的转换,具体过程参考:
fontconfig 中 pixel size 的计算过程

据我实践,kde Qt3 程序会使用该值,包括 kdm,用该值来控制 Qt3 程序的字体 dpi 比较方便。

3、dpi in gonome
引用:
GNOME also offers an interface to set the Xft DPI. If "gnome-settings-daemon" is running, this will advertise the DPI value set in the gnome-font-properties dialog to GNOME applications via XSETTINGS, and set the Xft.dpi X resource. Any Qt application started after gnome-settings-daemon is running will use the Xft.dpi value configured by GNOME.
简单说 gnome 额外提供了一个接口来更加灵活的控制设置 fontconfig/xft 程序的 dpi,当你运行 gnome 时,将会运行 gnome-settings-daemon 这个守护进程,来通知 Gtk2 程序 dpi 值,同时设置 Xft.dpi 来影响 Qt 程序。
操作方法,在 gnome 的字体功能面板 dpi 设置栏里输入 90,就可以了,你可以根据喜好,进行微调,马上能见到效果。
查看 Xft.dpi 值,方法如下:
代码:
$ xrdb -query
......
Xft.antialias: 1
Xft.dpi: 90.00000
Xft.hinting: 1
Xft.hintstyle: hintfull
Xft.rgba: rgb
据我实践,Gtk2 程序标题栏和菜单栏字体发生明显变化,包括 gnome 的开始菜单、工具栏、桌面字体等等,。而对于 Qt3 程序,除了标题栏发生变化,其它稳丝不动,因此还是得通过 fontconfig 修改。
细节:
如果 gnome 中的 dpi 和 X server 中的不同,而且相差很大,比如一个 120,一个 90。那么你在登录到 gnome 那一瞬间,会发现开始菜单的字体一下子变大,原因是最开始时 gnome-settings-daemon 还没运行。

4、字体大小不等,dpi 惹得祸
如果你在使用 kde 或 fvwm 之类,而又没有运行 gnome-font-properties,那么你会发现同样是 9 point 字,gtk2 程序和 Qt3 程序的显示大小不同。例如你打开 gnome terminal 和 kde terminal 比较一下。
解决方法:
$ vi ~/.Xresources
代码:
Xft.dpi: 90
我的理解是 kde 会自动读取 fontconfig 的 dpi 的设置,而 gtk2 程序依赖 Xft.dpi。

如果你 dpi 设置遇到问题或有关术语不清楚,请看参考链接:
Mozilla DPI-related Font Size Issues on Unix
Solving the Linux DPI Puzzle
FAQVideoModes
Monitor Tech Guide
Toward a standard font size interval system

二、安装 MS core fonts
即然讲的是实现和 Windows 一样的效果,而我却叽哩咕噜讲了一大堆 dpi,你也听烦了,听会音乐,暂时把 dpi 丢在一边。我们开始一些机械劳动。
1、字体下载
其它发行版,请到这里下载:
Microsoft's TrueType core fonts

Debian/Ubuntu:
代码:
$ apt-get install msttcorefonts
字体将会被安装到 /usr/share/fonts/truetype/msttcorefonts
分别是:
Andale Mono, Arial, Comic Sans MS, Courier New, Georgia
Impact, Times New Roman, Trebuchet MS, Verdana, Webdings

先来分分类:
无衬线字体(San Serif):
Arial 和 Verdana 作为无衬线字体的经典代表,是我们平时浏览 Web 是最常见的字体,在小字体下都有很好的表现,但是 Verdana 字符间隔(spacing)比较大,在浏览某些网页时,会出现布局混乱的现象,故我们把 Arial 作为我们的主字体。Trebuchet MS 也是比较常见的页面字体。
见: The Scourge of Arial

衬线字体 (Serif):
Georgia 和 Times New Roman 都是比较美观的衬线字体,Georgia 和 Verdana 都出自字体巨匠 Matthew Carter 之手,并由 Monotype's hinting 专家 Tom Rickner 润色,具有很优雅的外观,故同样选择作为我们的主字体。
见: Georgia & Verdana Typefaces designed for the screen

等宽字体 (monotype):
Courier New 和 Adobe 的 Courier 有很像的外观,在终端下有非常好的表现,适合编程故选为我们的主字体。

卡通字体
Comic_Sans_MS 很休闲,也非常出名的卡通字体,可以用来装饰桌面等,有时候作为页面字体,也有很好的可读性。
见: Comic Sans Cafe

界面字体 (UI):
Tahoma 一直以来都是 MS 很多主线产品的 UI 字体,在小字下也有很优秀的表现,故选为我们的主字体,不过由于版权问题,MS 并不提供 Tahoma 免费下载,故不包含在核心字体包里。

简体中文字体:
winxp 带的 simsun.ttc 是一个 truetype 字体集,包括 SimSun(宋体)和 NSimSun(新宋体),它们的中文部分完全一样,唯一不同在于英文部分,NSimSun 的英文是等宽的。不过它们所带的英文衬线字体并不美观,我们在下面将用到字体替换技术,替换成我们喜欢的英文字体。

选择 simsun,除了它是 winxp 的默认字体外,另一个重要原因是带有丰富的点阵字体,在特定的 px 下有非常锐利的外观,所带点阵有效像素(ppem) 分别为 12,13,14,15,16,18,不仅制作规整专业,而且方方正正,很适合一般人的习惯。当然点阵字体的缺点也很明显,不像 Outline 字体可以随意缩放,点阵字体的每个像素都是人为安排的,缩放后打乱了整体结构,会变得非常丑陋。不过不要担心,我会很详细的讲解如何显示特定像素大小的点 阵。
注意如果没有必要,请不要使用 simsun18030.ttc 或 vista 中的 simsun.ttc,因为 simsun18030.ttc 所带的点阵只有 12,14,16 三种 ppem,而且没有 xp 中 simsun.ttc 的点阵设计美观,而 vista 中的 simsun.ttc 就好比 simsun18030.ttc 的缩小版,除了字符集小一点外,我没看出区别来,故不要被这些名字所迷惑。
见: simsun - 微�宋� TrueType 字型

繁体中文字体:
mingliu.ttc 同样是 truetype 字符集,包含 MingLiU(细明体)和 PMingLiU(新细明体),这里刚好和 simsun 倒一下,MingLiU 的英文部分是才是等宽的,所带的 ppem 分别为 11,12,13,15,16,20。一个需要注意的地方是 mingliu 使用了 hinting 技术,必须打开 freetype2 中的 BYTECODE INTERPRETER,否则在某些字号下,字体会整个破碎掉。而使用 bytecode 会涉及到一些专利问题,引用 firefly 大虾的话:
引用:
宋体和 Freetype2 有什么关系呀?
(新)宋体、楷体_GB2312使用 bytecode 组字法,与一般的 TrueType 格式不一样,一般的 TrueType 是存放"笔划",所以字型引擎只要依照笔划指引,就可以绘出字形,而 bytecode 则是一些虚拟指令(类似 java 的虚拟指令),这时候,字型引擎就必须化身为一个解译器,来解释并执行这些虚拟指令,就像 JVM 一样,bytecode 的好处是减少字型档案的大小。

bytecode 有三个部份为 Apple 专利,正是因为这个缘故,freetype2 预设并未打开这个 bytecode 解译功能,所以当遇到了 bytecode 组字的字型时,就会不正常(破碎),幸好此类字型不常见,有个好消息是:freetype unstable 版已经避开了 Apple 专利部份,而且预设也是开启 bytecode 解译功能,可见专利问题已解决了(猜测),所以才会预设开启。
庆幸的是通过观察源代码 Debian/Ubuntu 通过补丁的形式默认都开启了 BYTECODE INTERPRETER,而省去了我们自己编译的麻烦。可能某些发行版没有这么做。
参考链接:
mingliu - 微��明� TrueType 字型
Freetype 主页关于 Apple 专利的说明

如果你看不到上述网页效果,很正常,因为你还安装完成。
代码:
$ mkdir /usr/share/fonts/truetype/winxp
找台 Winxp,拷贝 c:\windows\fonts 下的 tahoma.ttf tahomab.ttf simsun.ttf mingliu.ttf 到该目录下。

同时在 /etc/X11/xorg.conf 中加入该路径,好让 legacy X font server 找到是上述四个新添加字体。
代码:
$ vi /etc/X11/xorg.conf
FontPath "/usr/share/fonts/truetype/winxp"
这时候有人会问了,刚装的 MS core font 的路径需要加么?
答案是一般不用了,因为在 Debian/Ubuntu 中有 defoma 帮我们管理字体,而 defoma 会自动在 /etc/X11/xorg.conf 中添加自己的字体路径:
代码:
# path to defoma fonts
FontPath "/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType"
在该路径中已经包含了指向 MS core fonts 的字体链接。
代码:
$ /var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType# ls -al |grep Arial
lrwxrwxrwx 1 root root 55 2006-08-02 20:06 Arial_Black.ttf -> /usr/share/fonts/truetype/msttcorefonts/Arial_Black.ttf
lrwxrwxrwx 1 root root 61 2006-08-02 20:05 Arial_Bold_Italic.ttf -> /usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf
lrwxrwxrwx 1 root root 54 2006-08-02 20:06 Arial_Bold.ttf -> /usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf
lrwxrwxrwx 1 root root 56 2006-08-02 20:05 Arial_Italic.ttf -> /usr/share/fonts/truetype/msttcorefonts/Arial_Italic.ttf
lrwxrwxrwx 1 root root 49 2006-08-02 20:05 Arial.ttf -> /usr/share/fonts/truetype/msttcorefonts/Arial.ttf
而且 defoma 会自动调用 mkfontdir,mkfontscale 来创建 fonts.dir,fonts.scale,这些是 legacy X font server 需要的,注意不是 fontconfig 的需要。但是你使用老式程序的话,你会用到 xfs。
这里要注意区分 fontconfig 和 xfs,它们在你的系统中是共存的,可以简单用新旧两代字体系统来区分。

重启 X, 使之生效。

2、建立字体缓存
$ sudo fc-cache -fv
它会搜索在 /etc/fonts/fonts.conf 指定的字体路径或你自定义的字体路径中的任何 fontconfig 可识别的格式:
代码:
<!-- Font directory list -->

<dir>/usr/share/fonts</dir>
<dir>/usr/share/X11/fonts</dir> <dir>/usr/local/share/fonts</dir>
<dir>~/.fonts</dir>
并在路径下建立字体缓存文件 fonts.cache-1,这才是 fontconfig 的痕迹。

3、两个很有用的命令
1) fc-list
让我们看看刚装的 MS core fonts
代码:

$ fc-list : family foundry spacing |grep monotype
Andale Mono:spacing=100:foundry=monotype
Arial:foundry=monotype
Arial Black:foundry=monotype
Impact:foundry=monotype
Courier New:spacing=100:foundry=monotype
Times New Roman:foundry=monotype

$ fc-list : family foundry spacing |grep microsoft
Verdana:foundry=microsoft
Georgia:foundry=microsoft
Tahoma:foundry=microsoft
Comic Sans MS:foundry=microsoft
Webdings:foundry=microsoft
Trebuchet MS:foundry=microsoft

$ fc-list : family foundry spacing |grep SimSun
SimSun,宋体:spacing=90:foundry=unknown
NSimSun,新宋体:spacing=90:foundry=unknown

quanli@tpr50-ubuntu:~/fonts$ fc-list : family foundry spacing |grep MingLiU
PMingLiU,新�明�:foundry=dynalab
MingLiU,�明�:spacing=90:foundry=dynalab
family 表示字体家族,foundry 表示出品人,spacing 表示字符间隔
很容易看出 MS 的很多字体都不是它自己制作的,而是委托其它专业字体公司,如 monotype 完成制作,然后获取授权。简单说就是付过钱的。
这里的数字 90 代表 dual-width(双宽度),100 表示 mono-width(等宽),没有默认为 0 表示 proportional (不等宽)。
终端字体最好用等宽字体,可以看出我们选择的 Courier New 就是英文等宽字体,spacing=100。
而中文字体如 SimSun,NSimSun,MingLiU 都是双宽度字体,spacing=90,字体渲染引擎 Freetype2 在处理这些双宽度亚洲字体时会出现错误,故我们在后面的配置中要涉及这个问题。

2) fc-match
如果我们要看到单个字体的具体信息
代码:
$ fc-match -v Tahoma
Pattern 31 of 32
file: "/usr/share/fonts/myfonts/winxp/tahoma.ttf"(s)
scalable: FcTrue(s)
slant: 0(i)(s)
prefer_bitmap: FcTrue(w)
rgba: 1(i)(w)
rh_prefer_bitmaps: FcTrue(w)
size: 12(f)(s)
fontversion: 209060(i)(s)
weight: 80(i)(s)
family: "Tahoma"(s)
familylang: "en"(s)
antialias: FcFalse(w)
outline: FcTrue(s)
foundry: "microsoft"(s)
dpi: 90(f)(s)
stylelang: "ca"(s)
style: "Normal"(s)
lang: aa|ab|af|ar|ast|ava|ay|az|az-ir|ba|bam|be|bg|bi|bin|br|bs|bua|ca|ce|ch|chm|co|cs|cu|cv|cy|da|de|el|en|eo|es|et|eu|fa|fi|fj|fo|fr|ful|fur|fy|ga|gd|gl|gn|gv|ha|haw|he|ho|hr|hu|ia|ibo|id|ie|ik|io|is|it|kaa|ki|kk|kl|ku|ku-ir|kum|kv|kw|ky|la|lb|lez|lt|lv|mg|mh|mi|mk|mo|mt|nb|nds|nl|nn|no|ny|oc|om|os|pl|ps-af|ps-pk|pt|rm|ro|ru|sah|sco|se|sel|sh|sk|sl|sm|sma|smj|smn|sms|so|sq|sr|sv|sw|tg|th|tk|tn|to|tr|ts|tt|tw|tyv|ug|uk|ur|uz|ven|vi|vo|vot|wa|wen|wo|xh|yap|yi|yo|zu(s)
fontformat: "TrueType"(s)
width: 100(i)(s)
charset: set(s)
index: 0(i)(s)
pixelsize: 15(f)(s)
capability: "otlayout:arab otlayout:cyrl otlayout:hebr otlayout:latn otlayout:thai"(s)
verticallayout: FcFalse(s)
hinting: FcTrue(w)
autohint: FcFalse(w)
embeddedbitmap: FcTrue(w)
scale: 1(f)(s)
hintstyle: 3(i)(w)
globaladvance: FcTrue(s)
注意如果你在后面使用了一些字体替换技术,可能看到的信息是替换后的字体信息,比如:
代码:
$ fc-match -v SimSun
Pattern 30 of 32
file: "/usr/share/fonts/truetype/msttcorefonts/arial.ttf"(s)
......
这里我因为在后面把 SimSun 中的英文替换为 Arial,故出现信息混乱的情况。

看到这里聪明的你一定学会了如何给 fc-list 添加你想看到的参数,同时不要忘了看一下 man。

如果你想学到更多关于 fontconfig 的知识,请看下面链接:
fonts-conf - English Version
fonts-conf - traditional Chinese translation

三、字体配置
该是我们开始对字体进行微调的时候了!Go go ...
1) $ sudo dpkg-reconfigure fontconfig
代码:
Select Native if you mostly use Bitstream Vera (the default in Debian) or any of the Microsoft fonts.
Select Autohinter if you mostly use other TrueType fonts.
Select None if you want blurry text.
How should fonts be tuned for the screen?

Native
Autohinter
None
选 Native。
解释一下 Native 表示使用字体自带的 hinting 信息,Autohinter 顾名思义采用 freetype 的 autohint 来机器生产 hint 信息。none 则不使用以上任何一种 hint,通常的结果就是字体更模糊。

选择 Native 的原因是 MS 的 truetype 都带有非常漂亮的 hinting,那什么是 hinting 呢?
引用:
The "mathematically correct" pixels for character outlines scaled to a given size and resolution are often unaesthetic or illegible. Hinting is the name for the set of techniques for restoring, as far as possible, their aesthetics and legibility.
......
Each glyph in the font contains its own little hint program, with which the control points of the outlines can be manipulated just prior to rasterization in any way the hint programmer desires. In practice, this means that the outline is carefully distorted by each glyph's hinting instructions to surround only those pixels that produce the desired bitmap image.
hinting 是 truetype 的心脏,与其说它是程序,不如说它是数学。每个字符最终在屏幕上都是 bitmap 形式成像,在这之前要先生成 outline 字符,然后缩放到需要比例,在送到 rasterization 产生二维图形前,要先被 hinting 调整,从而生产出屏幕最美观的 bitmap 字体。
常用的英文符号或其它欧州字符并不是很多,因此可以花很多的时间来给它们手工或其它方式添加 hinting,其中不同的字号大小,不同的字体风格,如 roman,bold,italic,需要进行单独调整,非常费时,从而提供最佳的效果,当然还是那句老话,只有更好,没有最好。
一般来说,hinting 比 autohint 要效果好的多,但对于一些免费字体,用 autohint 反倒是效果更好,原因可能是 Freetype 的设计者对这些字体做了优化。
中文字体由于为数众多,而且字型复杂,为每个字型添加 hinting,显然不实际,也做不好,故很少带 hinting 的。

本文的下文将围绕 hinting 这个主角展开。
见: TrueType Hinting

代码:
Rendering text at a subpixel level generally makes it look a bit better on flat (LCD) screens, but can show color artifacts on CRT screens. The "Automatic" choice will enable it only if a LCD screen is detected.
screens. The "Automatic" choice will enable it only if a LCD screen is detected.

Automatic
Always
Never
如果你是 LCD 请选上 Always, CRT 选择 Never。
sub-pixel 技术(暂略)
见: [url="http://www.grc.com/cleartype.htm"]Sub-Pixel Font Rendering Technology[/url]
sub-pixel 开关可以单独对每个字体进行的,个人建议是全局开 sub-pixel,而对于中文字体关闭 sub-pixel,对于英文字体根据喜好,有选择性的关闭。

代码:
By default, only outline fonts are used by applications which support fontconfig. Outline fonts are fonts which scale well to various sizes. In contrast, bitmapped fonts are often lower quality. Enabling this option will affect the systemwide default; this and many other fontconfig options may be enabled or disabled on a per-user basis.

Enable bitmapped fonts by default?
在这里我们选 No。这里不用担心用不了 simsun 内置的点阵字体(bitmap font),等会我们会为它单独打开。
确定以后,它还会为我们作一次 fc-cache,故前面 fc-cache 可以不做。

现在看看 dpkg-reconfigure fontconfig 到底为我们做了什么,背地里的行为我看不到,我只发现多了这么两个软链接:
代码:
ubuntu:/etc/fonts/conf.d# ls -al
total 32
drwxr-xr-x 2 root root 4096 2006-08-03 22:41 .
drwxr-xr-x 3 root root 4096 2006-08-03 19:11 ..
lrwxrwxrwx 1 root root 32 2006-08-03 22:41 20-debconf-sub-pixel.conf -> /etc/fonts/conf.d/sub-pixel.conf
lrwxrwxrwx 1 root root 33 2006-08-03 22:41 30-debconf-no-bitmaps.conf -> /etc/fonts/conf.d/no-bitmaps.conf
-rw-r--r-- 1 root root 250 2006-05-19 14:44 autohint.conf
-rw-r--r-- 1 root root 306 2006-05-19 14:44 no-bitmaps.conf
-rw-r--r-- 1 root root 257 2006-05-19 14:44 no-sub-pixel.conf
-rw-r--r-- 1 root root 256 2006-05-19 14:44 sub-pixel.conf
-rw-r--r-- 1 root root 247 2006-05-19 14:44 unhinted.conf
-rw-r--r-- 1 root root 296 2006-05-19 14:44 yes-bitmaps.conf
前面带数字的两个软链接是 dpkg-reconfigure fontconfig 的杰作,很简单吧!自己可以学着改改。Freetype 默认开的是 hinting,故虽然我回答了三个问题,只多了两个软链接。
在 /etc/fonts/fonts.conf 里包含了 /etc/fonts/conf.d 这个路径,故它会 Load 里面的配置。
如果 dpkg-reconfigure fontconfig 只为我们做了这么一点工作,那么该命令是完全没有必要运行的。我们后面的工作可比这个复杂的多。我在这里主要是为了起推波助澜的作用,同时也作为一部分全 局设置。如果你喜欢清清爽爽一个配置文件的话,你可以删掉软链接,直接都写在一个文件中去。

关于 Ubuntu 中的 fontconfig-voodoo 命令,其实就是帮你写了一个不痛不痒的 fontconfig 配置文件。比如:
代码:
$ fontconfig-voodoo -l
ja_JP
ko_KR
none
zh_CN
zh_HK
zh_SG
zh_TW

ubuntu:/etc/fonts# fontconfig-voodoo -s zh_CN -f
多了
language-selector.conf -> /usr/share/language-selector/fontconfig/zh_CN
也就是说除了读 fonts.conf 多读一个事先配置好的 zh_CN 文件,里面的内容你可以参考学习,但为了不影响本文,把它去掉:
代码:
ubuntu:/etc/fonts# fontconfig-voodoo -s none -f
language-selector.conf -> /usr/share/language-selector/fontconfig/none
2. 写自己的配置文件
不要去修改 /etc/fonts/fonts.conf,因为它很可能在以后升级过程中被替换掉,为了不让你辛苦配置好的文件付诸东流,我们把配置写到 /etc/fonts/local.conf,可以被每位用户读到,当然你也可以写到 ~/.fonts.conf,但要注意如果使用 kde, 每次在 kde 控制中心设置字体后,kde 都会自动创建 ~/.fonts.conf,如果已经存在则在最底下加上它的配置,从而可能覆盖或影响到你的配置,这种做法在我看来是很讨厌的。好在 /etc/fonts/local.conf 是在 ~/.fonts.conf 后被 fontconfig 读取,具有更高的优先级,所以放在这里最合适。

因为配置会很长,故我分三个文件:
local.conf 负责全局配置,本身被 fonts.conf 读取,
msttcorefonts.conf 负责 MS core fonts,被 local.conf 读取,
cjk.conf 负责中日韩字体,被 local.conf 读取。

任何 fontconfig 配置文件,都被包含在 XML 结构之中:
代码:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
...
</fontconfig>
1) local.conf
给使用 fontconfig 的程序设定目标 dpi,程序包括 kdm, kde 等:
代码:
<!--
Target dots per inch
-->
<match target="pattern">
<edit name="dpi" mode="assign" >
<double>90</double>
</edit>
</match>
字体分类:
代码:
<!--
Mark common families with their generics so we'll get
something reasonable
-->
<!--
Serif faces
-->
<alias>
<family>Georgia</family>
<family>Times New Roman</family>
<family>SimSun</family>
<family>PMingLiU</family>
<default><family>serif</family></default>
</alias>
<!--
Sans-serif faces
-->
<alias>
<family>Arial</family>
<family>Tahoma</family>
<family>Verdana</family>
<family>Trebuchet MS</family>
<family>Comic Sans MS</family>
<family>Arial Unicode MS</family>
<default><family>sans-serif</family></default>
</alias>
<!--
Monospace faces
-->
<alias>
<family>Courier New</family>
<family>Andale Mono</family>
<family>NSimSun</family>
<family>MingLiU</family>
<default><family>monospace</family></default>
</alias>
给标准字体名提供别名,越靠前的字体家族优先级越高。
代码:
<!--
Provide required aliases for standard names
-->
<alias>
<family>serif</family>
<prefer>
<family>Georgia</family>
<family>Times New Roman</family>
<family>SimSun</family>
<family>PMingLiU</family>
</prefer>
</alias>
<alias>
<family>sans-serif</family>
<prefer>
<family>Arial</family>
<family>Verdana</family>
<family>Trebuchet MS</family>
<family>Comic Sans MS</family>
<family>SimSun</family>
<family>PMingLiU</family>
<family>Arial Unicode MS</family>
</prefer>
</alias>
<alias>
<family>monospace</family>
<prefer>
<family>Courier New</family>
<family>Andale Mono</family>
<family>NSimSun</family>
<family>MingLiU</family>
</prefer>
</alias>
这里在 serif 和 sans-serif 标准字体名里我都加入了 SimSun 和 PMingLiU,而照理说 SimSun 和 PMingLiU 都属于 Serif?
道理很简单,比如 Georgia 没有中文字型,会向下匹配查找,直到找到第一个中文字型,即 SimSun。这里要注意的地方是,如果你还放了其它中文字体或日文韩文等,一定要加到 pMingLiU 的后面,否则会先使用这些字体里的相同字型,到时界面很丑陋,可别哭啊!
同样 Arial 虽然为 sans-serif 字型,但同样没带中文字型,故使用 SimSun 和 PMingLiU 来补充。
而 NSimSun 和 MingLiU 都是等宽字型,故作为终端字体配合 Courier New 使用。

其实还有个字体叫 Arial Unicode MS,在 Office 2k/xp 里可以找到,带有很全的 Unicode 编码,包括 CJK,是一个很好的补充字体,为了不增加劳动量,我在这里就一撇带过了。


设置 3 个常见 Adobe 字体家族的可接受字体
代码:

<!--
Use MS core fonts provides metric and shape compatible fonts
for these 3 Adobe families.
-->
<alias>
<family>Times</family>
<accept><family>Times New Roman</family></accept>
</alias>
<alias>
<family>Helvetica</family>
<accept><family>Arial</family></accept>
</alias>
<alias>
<family>Courier</family>
<accept><family>Courier New</family></accept>
</alias>
一般来说,正常安装 Debian/Ubuntu 过程,都会将以下两个字体包装上:
xfonts-100dpi xfonts-75dpi
分别包含 100 和 75 dpi 的字体 for X,里面包括了 Times, Helverica, Courier 这三个字体家族,但它们都是 pcf bitmap 字体格式,而且只包含部分字体大小,最重要的是 pcf 并非很有效的字体格式,在读取时要整个读入内存,故一般体积比较小,而 truetype 是非常先进的字体格式,支持字体部分读取,效率很高。而且 MS 提供的以上三种 turetype 字体都是 outline 字体,可以随意缩放。故我一般直接将上面两个包删除,因为我根本用不到它们。如果你觉得它们有用,比如你觉得 Courier 作为终端字体更好看,那么你保留它们。关于如何删除系统字体,而不影响依赖关系,有兴趣的朋友,可以在文章的最后看到。

见: pcf 的效率

包含 cjk.conf 和 msttcorefonts.conf 文件
代码:
<!--
Load Chinese customization file
-->
<include ignore_missing="yes">./cjk.conf</include>


<!--
Load MS core font customization file
-->
<include ignore_missing="yes">./msttcorefonts.conf</include>
2)MS core fonts - msttcorefonts.conf
对于核心字体的配置是本文的重点,是实现和 windows 一样显示效果的关键。
很多人在处理英文字体的时候,都会很草率的进行,比如将低于某个数值的字体关闭 Anti-alias(反锯齿),其它开 AA。这种做法在许多场合效果并不理想。因为 MS 的 truetype 在做 hinting 时,是针对不同大小,不同风格做不同的处理,而非一锤子买卖。
在这方面我不得不说 MS 做得很仁义,我们来看看 MS 提供的
Core fonts Quick reference table
代码:
font name smoothing and hinting*
smoothed hinted both
Arial 0 - 6 7 - 13 14+
Arial Bold 0 - 6 7 - 8 9+
Arial Italic 0 - 6 7 - 13 14+
Arial Bold Italic 0 - 6 7 - 8 9+
Arial Black 0 - 6 7 - 12 13+
注意这里的单位是 point,对于 outline 字体用 pixel 讨论,在我看来是没什么意义的。
smoothed 即 Anti-alias
hinted 即 hinting
both 即 Anti-alias + hinting
(注意字体版本不同,信息和上面可以有所出入。)

可以很清楚的看到 Arial 和 Arial Bold 的不同,对于不同分格做了不同优化。写到这里明眼人已经看出我取这个文章题目的原因,我所有的诡计已被识破,黔驴技穷了。

如果上面没有的字体,如何找呢?一般 MS 提供的字体都会自带此类信息,而且 MS 提供了一个工具来帮助你更加简单的查看字体信息。
Font properties extension, version 2.1

如果你没有 windows,或你懒得一个个去看,你可以查看我收集的数据。如下:
代码:
Font Name version smoothed hinted both
Andale Mono 2.00 0-6 7-20 21+
Arial 2.82 0-6 7-13 14+
Arial Black 2.35 0-6 7-12 13+
Arial Bold 2.82 0-6 7-8 9+
Arial Bold Italic 2.82 0-6 7-8 9+
Arial Italic 2.82 0-6 7-13 14+
Comic Sans MS 2.10 0-6 7-11 12+
Comic Sans MS Bold 2.10 0-6 7-8 9+
Courier New 2.82 0-6 7-27 28+
Courier New Bold 2.82 0-6 7-11 12+
Courier New Bold Italic 2.82 0-6 7-12 13+
Courier New Italic 2.82 0-6 7-27 28+
Georgia 2.05 0-6 7-12 13+
Georgia Bold 2.05 0-6 7-12 13+
Georgia Bold Italic 2.05 0-6 7-12 13+
Georgia Italic 2.05 0-6 7-12 13+
Impact 2.35 0-6 7-16 17+
Times New Roman 2.82 0-6 7-13 14+
Times New Roman Bold 2.82 0-6 7-10 11+
Times New Roman Bold Italic 2.82 0-6 7-13 14+
Times New Roman Italic 2.82 0-6 7-15 16+
Trebuchet MS 1.22 0-6 7-12 13+
Trebuchet MS Bold 1.22 0-6 7-8 9+
Trebuchet MS Bold Italic 1.22 0-6 7-8 9+
Trebuchet MS Italic 1.22 0-6 7-12 13+
Verdana 2.35 0-6 7-12 13+
Verdana Bold 2.35 0-6 7-12 13+
Verdana Bold Italic 2.35 0-6 7-12 13+
Verdana Italic 2.35 0-6 7-12 13+
Webdings 1.03 0-6 7-21 22-1536 1537+
Tahoma 3.09 0-6 7-12 13+
Tahoma Bold 3.09 0-6 7-12 13+
SimSun 3.03 0-6 7-18 19+
MingLiU 5.03 0-6 7-36 37+

Others:
Arial Unicode MS 1.01 0-6 7-18 19+
MS 雅黑 0.72 0-6 7-17 18+
MS 雅黑 Bold 0.72 0-6 7-13 14+
Segoe UI 0.98 0-6 7-14 15+
Segoe UI Bold 0.98 0-6 7-14 15+
Segoe UI Italic 0.96 0-6 7-14 15+
Segoe UI Bold Italic 0.95 0-6 7-14 15+
接下来的工作是非常繁琐的,要针对每个字体微调,但你不必担心,我已经帮你做好。
代码:
$ vi /etc/fonts/msttcorefonts.conf
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">

<!-- /etc/fonts/msttcorefonts.conf file to configure system font access -->
<fontconfig>
......
</fontconfig>
对应上述表格 both 栏,即最右边一栏,开 AA + hinting,注意这里的 size 即 point size
代码:
<!--
default : smoothed and hinted
-->
<match target="font" >
<test name="foundry" qual="any" >
<string>monotype</string>
<string>microsoft</string>
</test>
<edit name="antialias" mode="assign" >
<bool>true</bool>
</edit>
<edit name="autohint" mode="assign" >
<bool>false</bool>
</edit>
<edit name="hinting" mode="assign" >
<bool>true</bool>
</edit>
<edit name="hintstyle" mode="assign" >
<const>hintfull</const>
</edit>
</match>
对应表格 smoothed 栏,即小于等于 6 point 的字,只开 AA
代码:
<!--
For point size less equal than 6 : only smoothed
-->
<match target="font" >
<test name="foundry" qual="any" >
<string>monotype</string>
<string>microsoft</string>
</test>
<test name="size" compare="less_eq" >
<double>6</double>
</test>
<edit name="antialias" mode="assign" >
<bool>true</bool>
</edit>
<edit name="autohint" mode="assign" >
<bool>false</bool>
</edit>
<edit name="hinting" mode="assign" >
<bool>false</bool>
</edit>
</match>
对应表格 hinted 栏,只开 hinting。这里是最繁重的一关,我在配置文件中将每一种字体,每一种风格详细列出,方便大家以后自己微调。
篇幅关系,只举几个特例:
代码:
<!--
Arial Regular
-->
<match target="font" >
<test name="family" >
<string>Arial</string>
</test>
<test name="weight" compare="eq">
<const>regular</const>
</test>
<test name="slant" compare="eq" >
<const>roman</const>
</test>
<test name="size" compare="more_eq" >
<double>7</double>
</test>
<test name="size" compare="less_eq" >
<double>13</double>
</test>
<edit name="antialias" mode="assign" >
<bool>false</bool>
</edit>
<edit name="autohint" mode="assign" >
<bool>false</bool>
</edit>
<edit name="hinting" mode="assign" >
<bool>true</bool>
</edit>
<edit name="hintstyle" mode="assign" >
<const>hintfull</const>
</edit>
</match>
还记得上面介绍的如何查看字体信息的方法么!这里 roman 表示非斜体,其它信息都是自解释的。

再来个
代码:
<!--
Times New Roman Bold Italic
-->
<match target="font" >
<test name="family" >
<string>Times New Roman</string>
</test>
<test name="weight" compare="eq">
<const>bold</const>
</test>
<test name="slant" compare="eq" >
<const>italic</const>
</test>
<test name="size" compare="more_eq" >
<double>7</double>
</test>
<test name="size" compare="less_eq" >
<double>13</double>
</test>
<edit name="antialias" mode="assign" >
<bool>false</bool>
</edit>
<edit name="autohint" mode="assign" >
<bool>false</bool>
</edit>
<edit name="hinting" mode="assign" >
<bool>true</bool>
</edit>
<edit name="hintstyle" mode="assign" >
<const>hintfull</const>
</edit>
</match>
bold 表示出体,italic 表示斜体,这些都是常量,你也可以用对应的数字表示,具体参考:
fontconfig-user
如果某种风格的字体由于开了 sub-pixel,造成边缘花花绿绿,让你很不舒服,你可以单独的关闭它,插入以下语句:
代码:
...
<edit name="rgba" mode="assign">
<const>none</const>
</edit>
...
因为这个因人而异,我没法帮你调。

3) CJK 字型
老规矩:
[code]
$ vi /etc/fonts/cjk.conf
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">

<!-- /etc/fonts/mscorefont.conf file to configure CJK font access -->
<fontconfig>
......
</fontconfig>

CJK 双宽度纠正
代码:
<!--
The dual-width Asian fonts (spacing=dual) are not rendered correctly,
apparently FreeType forces all widths to match. Trying to disable the
width forcing code by setting globaladvance=false alone doesn't help.
As a brute force workaround, also set spacing=proportional, i.e. handle
them as proportional fonts:
-->
<match target="font" >
<test target="pattern" name="lang" compare="contains" >
<string>zh</string>
<string>ja</string>
<string>ko</string>
</test>
<test name="spacing" compare="eq">
<const>dual</const>
</test>
<edit name="spacing" mode="assign" >
<const>proportional</const>
</edit>
<edit name="globaladvance" mode="assign" >
<bool>false</bool>
</edit>
</match>
替换 SimSun/NSimSun 和 PMingLiU/MingLiU 的英文部分为你喜欢的英文
代码:
<!--
English portion substitution
-->
<match target="pattern" >
<test name="family" >
<string>SimSun</string>
<string>PMingLiU</string>
</test>
<edit name="family" mode="prepend" binding="strong" >
<string>Arial</string>
</edit>
</match>
<match target="pattern" >
<test name="family" >
<string>NSimSun</string>
<string>MingLiU</string>
</test>
<edit name="family" mode="prepend" binding="strong" >
<string>Courier New</string>
</edit>
</match>
如果你喜欢 Verdana 把 Arial 改为 Verdana,如果你喜欢衬线字体,把 Arial 改为 Georgia 之类的。

以下是 SimSun/NSimSun,PMingLiU/MingLiU 微调,原则是存在点阵就优先使用点阵,否则选择 Outline 字体
代码:
<!--
SimSun/PMingLiU, NSimSun/MingLiU global setting
-->
<match target="font" >
<test qual="any" name="family" compare="eq" >
<string>SimSun</string>
<string>NSimSun</string>
<string>PMingLiU</string>
<string>MingLiU</string>
</test>
<edit name="rgba" mode="assign">
<const>none</const>
</edit>
<edit name="antialias" mode="assign" >
<bool>true</bool>
</edit>
<edit name="autohint" mode="assign" >
<bool>false</bool>
</edit>
<edit name="hinting" mode="assign" >
<bool>true</bool>
</edit>
<edit name="hintstyle" mode="assign" >
<const>hintfull</const>
</edit>
<edit name="embeddedbitmap" mode="assign" >
<bool>true</bool>
</edit>
</match>
开启 SimSun/NSimSun 点阵字体,分别为 px = 12,13,14,15,16,18:
代码:
<!--
Enable embedded bitmap fonts
-->
<match target="font" >
<test qual="any" name="family" compare="eq" >
<string>SimSun</string>
<string>NSimSun</string>
</test>
<test name="pixelsize" compare="more_eq" >
<double>12</double>
</test>
<test name="pixelsize" compare="less_eq" >
<double>18</double>
</test>
<edit name="antialias" mode="assign" >
<bool>false</bool>
</edit>
</match>
<match target="font" >
<test qual="any" name="family" compare="eq" >
<string>SimSun</string>
<string>NSimSun</string>
</test>
<test name="pixelsize" compare="eq" >
<double>17</double>
</test>
<edit name="antialias" mode="assign" >
<bool>true</bool>
</edit>
</match>
注意对于点阵我们要用 pixel size,道理上面说过了,这里看你个人的喜好,你不喜欢这么多,就少开点。

下面对 PMingLiU/MingLiU 做同样的处理,包含的点阵分别为 px = 11、12、13、15、16、20
代码:
<match target="font" >
<test qual="any" name="family" compare="eq" >
<string>MingLiU</string>
<string>PMingLiU</string>
</test>
<test name="pixelsize" compare="more_eq" >
<double>11</double>
</test>
<test name="pixelsize" compare="less_eq" >
<double>16</double>
</test>
<edit name="antialias" mode="assign" >
<bool>false</bool>
</edit>
</match>
<match target="font" >
<test qual="any" name="family" compare="eq" >
<string>MingLiU</string>
<string>PMingLiU</string>
</test>
<test name="pixelsize" compare="eq" >
<double>14</double>
</test>
<edit name="antialias" mode="assign" >
<bool>true</bool>
</edit>
</match>
20 px 的我觉得还是开 AA 吧!

为不带粗体的中文字体机器合成粗体
代码:
<!--
Synthetic emboldening for Chinese fonts that do not have bold face available
-->
<match target="font" >
<test name="family" compare="contains" >
<string>Song</string>
<string>Sun</string>
<string>Kai</string>
<string>Ming</string>
</test>
<test target="pattern" name="weight" compare="more_eq">
<int>180</int>
</test>
<edit name="embolden" mode="assign" >
<bool>true</bool>
</edit>
</match>
其实 Win 下的 simsun 粗体也是合成的。还有的人觉得 Ubuntu 好像比其它发行版优秀,比如有斜体/粗体支持,其实不过是配置文件略有不同。

如果你觉得合成的粗体不过好看,可以用其它黑体/圆体之类来替换,可以增加下面的语句
代码:
<!--
Substitude Bold part of SimSun with other font
-->
<!--
<match target="pattern">
<test qual="any" name="family" >
<string>SimSun</string>
<string>NSimSun</string>
</test>
<test name="weight" compare="more" >
<const>medium</const>
</test>
<edit name="family" mode="assign" >
<string>其它字体</string>
</edit>
</match>
-->
再补充一个微软雅黑字体,作为输入法输入框字体很不错,黑体要求块头越大,越好看,很壮观的。
只要简单拷贝该字体到我们的字体路径下,然后 $ sudo fc-cache -fv,就 OK 了。
代码:
<match target="font" >
<test qual="any" name="family" compare="eq" >
<string>Microsoft YaHei</string>
</test>
<edit name="rgba" mode="assign">
<const>none</const>
</edit>
<edit name="antialias" mode="assign" >
<bool>true</bool>
</edit>
<edit name="autohint" mode="assign" >
<bool>false</bool>
</edit>
<edit name="hinting" mode="assign" >
<bool>true</bool>
</edit>
<edit name="hintstyle" mode="assign" >
<const>hintfull</const>
</edit>

配置文件我已经在附件中给出,写累了,休息一下,看看反馈意见。
按本文配置好后,到下面链接比较一下效果:
Micorsoft Recommended fonts
觉得不错,贴个图我看看,哪里不清楚,同样贴图让我知道,如果配置文件哪里写的不理想,应该怎么改更好,也让我学习一下,比如我用的是英文界面,中文界面我还没测试。觉得太烂了,也别骂我,水平实在有限。

顺便提一下 Ubuntu 中的 firefox,一定要关闭 PANGO
$ vi /etc/firefox/firefoxrc
MOZ_DISABLE_PANGO=1