为ThinkingRock报表添加字体

[ThinkingRock][]是一款用Java编写的跨平台的GTD工具,我试用过很多GTD工具,在线的、单机的、自架服务器的,转了好几个圈,最后又转回来用它了,主要理由就是方便,该复杂的功能就要复杂,该简单的功能就要简单,可以用Project/Action来组织复杂事务,支持任务分配(当然只是给自己看的,没有协作功能),还算方便够用的报表功能。说到报表就到本文的主题了,软件自带的几个字体都无法显示中文,生成pdf报表之后里面的中文都会变成井字号#,所以需要自己安装中文字体。

参照[官方的字体安装说明](http://www.thinkingrock.com.au/faq.php#faq6),核心意思是该软件的字体读取使用了FOP,而这个FOP需要Font Metrics文件才能工作。FOP是Apache中的一个项目,我没有深入去研究,不过网站上也有[一篇比较清楚的文章教人如何生成这个Font Metrics文件](http://xmlgraphics.apache.org/fop/0.94/fonts.html#custom)。

但文章中的例子,使用库文件的位置和形式有点小问题,-cp后面是各个库文件,要能访问到才行,文中的路径显然不合适;文中各个库文件之间是用分号;间隔,这是在Windows中的写法,放nix下就错了,可以换成冒号:。所以,命令就变成了这个样子,注意所在路径的变化和相对路径:

$ cd /big2/tools/tr-2.0.1/tr/resource/fop
$ ln -s /big2/fonts/xpfonts/simsun.ttf
$ java -cp ../../modules/ext/fop-0.93.jar:../../modules/ext/avalon-framework-4.2.0.jar:../../modules/ext/commons-logging-1.0.4.jar:../../modules/ext/commons-io-1.1.jar org.apache.fop.fonts.apps.TTFReader -ttcname "SimSun" simsun.ttf simsun.xml

如果字体文件中包含多个字体,而你指定了错误的字体名称,系统会给出提示:

TTF Reader for Apache FOP 0.93

Parsing font...
Reading simsun.ttf...
This is a TrueType collection file with 2 fonts
Containing the following fonts: 
SimSun
NSimSun
Error while building XML font metrics file.
java.io.IOException: Name does not exist in the TrueType collection: Simsun

看到没,SimSun和NSimSun俨然就是宋体和新宋体啊。生成成功之后,simsun.xml就是我们得到的Font Metrics文件,所以接下来只需要再修改一下fop.xconf,把新字体的设置加进去就可以了:

<font metrics-url="simsun.xml" kerning="yes" embed-url="simsun.ttf">
  <font-triplet name="SimSun" style="normal" weight="normal"/>
</font>
<font metrics-url="simsun.xml" kerning="yes" embed-url="simsun.ttf">
  <font-triplet name="SimSun" style="normal" weight="bold"/>
</font>
<font metrics-url="simsun.xml" kerning="yes" embed-url="simsun.ttf">
  <font-triplet name="SimSun" style="italic" weight="normal"/>
</font>

好了,现在ThinkingRock的Tools -> Options -> Miscellaneous -> Action Screens中,就可以选择中文字体SimSun了,各种报表pdf中的中文也都会正常显示。

参考

  • [为ThinkingRock的REPORT添加中文字体](http://www.fallleaf.net/content/view/148/9/)
  • [ThinkingRock Hacks:让Report输出中文](http://www.mifengtd.cn/articles/add_fonts_for_thinkingrock_reports.html)

One thought on “为ThinkingRock报表添加字体”

Leave a Reply

Your email address will not be published. Required fields are marked *