在 Apache FOP 中,如何以编程方式设置字体库,并且仍然拥有中的字体?已加载?

发布于 2024-09-19 23:30:40 字数 1296 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2024-09-26 23:30:40

你的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?

意犹 2024-09-26 23:30:40

我决定在加载 fopStream 之后和将其输入到 DefaultConfigurationBuilder 之前使用“预处理器”进行一些变量替换

<fonts>
    <directory>${com.sun.aas.instanceRoot}/fonts/DejaVuTtf</directory>
</fonts>

I decided to to use a "preprocessor" to do some variable replacement after loading the fopStream and before feeding it into the DefaultConfigurationBuilder

<fonts>
    <directory>${com.sun.aas.instanceRoot}/fonts/DejaVuTtf</directory>
</fonts>
悲歌长辞 2024-09-26 23:30:40

我最终以我没有想到的明显方式解决了这个问题:使用系统字体目录。

I ended up solving this problem in the obvious way that didn't occur to me: use the system fonts directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文