PD4ML Fonts Tool


To simplify the creation of font mapping files, PD4ML v4.0.16 introduces a handy tool. The tool is implemented as an utility of the interactive PD4ML Converter

How to use it

Run the converter, as described by the link /support-topics/pd4ml-v4-programmers-manual/ and click the [Fonts Tool] button on the status bar of the application window.

It will prompt you for the location of the font directory. Select a directory. We can start with system fonts like C:\Windows\Fonts

Because the system font directory contains an excessive number of fonts that your application doesn’t really need, you can filter them out by entering a comma-separated list of font name patterns in the input field next to the “Filter by Font File Name” button. Apply the filter by pressing the button. The resulting list will include only those fonts whose filenames case-insensitively match the given patterns.

The generated list is editable, so you can manually refine it.

At this point you have four options:

  • Close – is obvious: close the tool and lose the current font mapping configuration
  • Use – use the font configuration only in the current session of the PD4ML Converter, without saving it.
  • Save Mapping File – creates a pd4fonts.properties file that can later be referenced from your application with pd4ml.useTTF() API call. If the chosen mapping file location is different from the original font directory (which is a good idea if you are using the system font directory), pd4fonts.properties will include a link to the font dir, allowing PD4ML to find the actual font files.
  • Save as JAR – packages the listed fonts and the corresponding pd4fonts.properties into a JAR file. Fonts are stored in the “pdffonts” JAR subdirectory by default, so once the JAR is deployed with a PD4ML-enabled application, the font configuration can be referenced with pd4ml.useTTF("java:pdffonts") API call. The subdirectory name can be customized by uncommenting and setting the jar.folder=pdffonts property.

Apache Batik SVG plugin control


PD4ML implements its own basic SVG renderer, and also allows you to use third-party libraries for the task. If it finds Apache Batik SVG Toolkit among the available libraries, it will automatically switch to it.

Batik tries to implement most of the features of SVG. But, unfortunately, it is very sensitive to even minor file format errors, and there is no way to change its error policy: it always throws exceptions, and there is no switch to turn them into warnings, for example.

In such cases, the best workaround would be to disable Batik and let the built-in SVG engine do the rendering.

Java code to disable Batik:

com.pd4ml.plugins.BatikSvgCustomTag.disable = true;

Disable in pd4browser.properties:

svg.batik=false

How to configure PDF fonts


PD4ML PRO, DMS and UA allow you to use all UNICODE characters space of custom TTF/OTF fonts in PDF.

The way TTF embedding is implemented by PD4ML may look complicated at first glance. On practice it is not so; also there are reasons why TTF usage is not as transparent as in regular Java applications.

In Java you can easily instantiate java.awt.Font object for any font face name, obtain the font metrics and to set the font for text output. By PDF generation PD4ML needs an access not only to java.awt.Font object, but to the corresponding physical .ttf file (to parse them and to extract a subset of used glyphs). Unfortunately Java does not offer a way to locate TTF file for a particular java.awt.Font object.

The most straightforward solution was to use font face -> font file mapping file. PD4ML’s default file name for it is pd4fonts.properties

New since v4.0.16: Interactive pd4fonts.properties creation tool: https://pd4ml.com/pd4ml-fonts-tool/

Below are available options how to create and deal with the mapping file or how to avoid a creation of it.

Creation of pd4fonts.properties for a selected set of fonts

  • create fonts/ directory (i.e /path/to/my/fonts/) and copy desired TTF files into it.
  • run pd4fonts.properties generation command
    java -Xmx512m -jar pd4ml.jar -configure.fonts /path/to/my/fonts/
    

As a result it should produce /path/to/my/fonts/pd4fonts.properties.

Now you can refer to it from Java application

pd4ml.useTTF("/path/to/my/fonts/");
// or identically
pd4ml.useTTF("/path/to/my/fonts/pd4fonts.properties");

Creation of pd4fonts.properties for system fonts

In the example above pd4fonts.properties file is stored to the same folder where TTF files are. If you run the command to index system fonts, in most of the cases it fails, as it has no write permission to the system font folder.

A solution is to write pd4fonts.properties to another location:

  • run pd4fonts.properties generation command
    java -Xmx512m -jar pd4ml.jar -configure.fonts c:/windows/fonts/ c:/path/to/my/config
    

As a result it should produce c:/path/to/my/config/pd4fonts.properties with an internal reference to the original font folder c:/windows/fonts/.

Now you may refer to it from Java application

pd4ml.useTTF("c:/path/to/my/config");
// or identically
pd4ml.useTTF("c:/path/to/my/config/pd4fonts.properties");

Creation of pd4fonts.properties on-a-fly

Set generateFontMappingFileIfMissing parameter of useTTF() to true

pd4ml.useTTF("/path/to/my/fonts/", true);

Creation of in-memory font mapping on-a-fly

Typically the method is used, when there is no preconfigured fonts directory available, and a use of the system fonts directory seems to be a good option. An obvious drawback of the idea is a potentially long indexing time of a big number of system fonts.

PD4ML allows to reduce the indexing efforts by limiting a scope of used fonts. fontFileNameFilter parameter can be set to a comma-delimited list of font name patterns:

pd4ml.useTTF("c:/windows/fonts/", "arial,times,courier");

The above code forces PD4ML to index only fonts, whose names contain arial, times or courier.

Creation of a JAR file with fonts

As a rule in Web application contexts you are not allowed to refer local file system resources. That makes the above methods not usable. PD4ML’s solution is to pack the fonts to a JAR file and deploy it with the Web application resources.

  • create fonts/ directory (i.e /path/to/my/fonts/) and copy desired TTF files into it.
  • run pd4fonts.properties generation command
    java -Xmx512m -jar pd4ml.jar -configure.fonts /path/to/my/fonts/
    

    which produces /path/to/my/fonts/pd4fonts.properties

  • pack to JAR
    jar cvf fonts.jar /path/to/my/fonts/
    

After deployment you can refer to it from Java application

pd4ml.useTTF("java:fonts/");

The “java:fonts/” URL addresses fonts/ folder within the JAR.

Web fonts

The @font-face CSS at-rule adds a custom font to a list of available ones; the font can be loaded from either a remote server or a locally-installed font on the user’s own computer.

The approach requires no API calls. All configuration is to be done in HTML/CSS sources.

@font-face {
  font-family: "Consolas";
  src: url("java:/html/rc/FiraMono-Regular.ttf") format("ttf");
}
@font-face { 
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/lato/v11/qIIYRU-oROkIk8vfvxw6QvesZW2xOQ-xsNqO47m55DA.woff) format('woff');
}

Font kerning

Kerning is an addition or reduction of space between two characters (glyphs) of a proportional font. As a rule a rendered text is visually much more pleasing when the kerning is applied.

Font kerning can be enabled with PD4ML API call:

pd4ml.applyKerning(true);

If you run PD4ML as a standalone command line tool, you may force it to apply kerning with -kerning parameter.