在 Apache FOP 中,如何以编程方式设置字体库,并且仍然拥有中的字体?已加载?
我正在使用 Apache FOP 1.0。该代码在不同的服务器上运行,因此我将字体放在每个服务器的实例根目录中。我的计划是以编程方式设置字体库(到服务器实例根目录,加上“/fonts/”),并在我的 fop 配置文件中,设置相对于该字体库的字体路径。下面是创建 FopFactory 的代码片段:
private static final String FONT_BASE = System.getProperty("com.sun.aas.instanceRoot") + "/fonts/";
public FOPWrapperBean() throws Exception {
ClassLoader loader = this.getClass().getClassLoader();
InputStream fopStream = loader.getResourceAsStream("META-INF/fop.xconf");
logger.log(Level.FINE, "InputStream: {0}", fopStream.toString());
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.build(fopStream);
this.fopFactory = FopFactory.newInstance();
this.fopFactory.setUserConfig(cfg);
fopFactory.getFontManager().setFontBaseURL("file://" + FONT_BASE);
logger.log(Level.FINE, "Font base url: {0}", fopFactory.getFontManager().getFontBaseURL());
fopStream.close();
}
fop.xconf 几乎完全是默认的。它包含
<base>.</base>
和
<fonts>
<directory>DejaVuTtf</directory>
</fonts>
(在 {instance-root}/fonts/DejaVuTtf 中有几种字体,如果我只给出绝对路径,我可以正确加载它们 - 但这不适用于拥有多个服务器,每个服务器可能有不同的字体实例根目录)。
如何使用以编程方式确定的路径加载字体?
谢谢!
I'm using Apache FOP 1.0. The code is running on different servers, so I put the fonts in each server's instance root. My plan is to set the font base programmatically (to the server instance root, plus "/fonts/"), and in my fop configuration file, set font paths relative to this font base. Here's the code snippet that makes the FopFactory:
private static final String FONT_BASE = System.getProperty("com.sun.aas.instanceRoot") + "/fonts/";
public FOPWrapperBean() throws Exception {
ClassLoader loader = this.getClass().getClassLoader();
InputStream fopStream = loader.getResourceAsStream("META-INF/fop.xconf");
logger.log(Level.FINE, "InputStream: {0}", fopStream.toString());
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.build(fopStream);
this.fopFactory = FopFactory.newInstance();
this.fopFactory.setUserConfig(cfg);
fopFactory.getFontManager().setFontBaseURL("file://" + FONT_BASE);
logger.log(Level.FINE, "Font base url: {0}", fopFactory.getFontManager().getFontBaseURL());
fopStream.close();
}
fop.xconf is almost entirely default. It contains
<base>.</base>
and
<fonts>
<directory>DejaVuTtf</directory>
</fonts>
(There are several fonts in {instance-root}/fonts/DejaVuTtf , which I can load correctly if I just give an absolute path -- but that doesn't work with having multiple servers, each of which may have different instance root directories).
How do I load in a font with a programmatically-determined path?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的java代码应该可以正常工作,因为FONT_BASE是在运行时确定的,依赖于服务器——我们正在做一些与此非常相似的事情并且它工作正常。也许您的系统属性没有为您提供您认为的目录?
Your java code should work fine, since FONT_BASE is determined at runtime, dependent on the server - we are doing something very similar to this and it works fine. Perhaps your system property is not giving you the directory you think it is?
我决定在加载
fopStream
之后和将其输入到DefaultConfigurationBuilder
之前使用“预处理器”进行一些变量替换I decided to to use a "preprocessor" to do some variable replacement after loading the
fopStream
and before feeding it into theDefaultConfigurationBuilder
我最终以我没有想到的明显方式解决了这个问题:使用系统字体目录。
I ended up solving this problem in the obvious way that didn't occur to me: use the system fonts directory.