In Apache FOP, how to prioritize fonts from custom directory over system fonts?

Viewed 142

I have an XML config that enables auto-detection of system fonts:

<fop version="1.0">
  <fonts>
    <auto-detect/>
  </fonts>
  <!-- ... -->
</fop>

And I configure a custom fonts directory programmatically:

// ...
FopConfParser fopConfParser = new FopConfParser(configFileStream, environmentProfile);
FopFactoryBuilder fopFactoryBuilder = fopConfParser.getFopFactoryBuilder()
fopFactoryBuilder.getFontManager().setResourceResolver(ResourceResolverFactory.createDefaultInternalResourceResolver(fontDir.toURI()));

In principle, this works. However, if a font is present in the system fonts directory as well as in my custom directory, the sytem font is used. This seems to be changed behavior from FOP 1.x to 2.x.

Is there a way to genrally prioritize fonts from custom directory over system fonts? I know that this can be solved with embed-url but I ideally don't want to configure this for every font explicitly.

1 Answers

The Documentation states:

Fonts found as a result of a "directory" tag configuration override fonts found as a result of the "auto-detect" tag being specified.

Accordingly, a <directory> tag could be added to your config file. In order to set it programmatically, you could specify it as a placeholder:

<fop version="1.0">
  <fonts>
    <directory>DYNAMIC_FONTDIR</directory>
    <auto-detect/>
  </fonts>
  <!-- ... -->
</fop>

In order to expand the placeholder, change the code you presented as follows:

String originalConfig = new String(configFileStream.readAllBytes(), StandardCharsets.UTF_8);
String adaptedConfig = originalConfig.replace("DYNAMIC_FONTDIR", fontDir.getAbsolutePath());
InputStream adaptedConfigStream = new ByteArrayInputStream(adaptedConfig.getBytes(StandardCharsets.UTF_8));

FopConfParser fopConfParser = new FopConfParser(adaptedConfigStream, environmentProfile);
// ...
Related