3
votes

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!

3
In the official doc is written "It is currently not possible to easily configure fonts from Java code."DrDol

3 Answers

1
votes

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?

1
votes

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>
1
votes

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