开发者

In Apache FOP, how do I set the font base programmatically, and still have the fonts from <directory> loaded?

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().getFontBaseUR开发者_StackOverflow社区L());
    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!


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?


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜