Defining fonts in the FOP configuration file
The description of fonts in the FOP configuration file is documented in the FOP fonts page. The remaining discussions in this document assume that you have read that page and have familiarized yourself with the concepts and procedures on it.
Automated font registration
Font registration is the process by which FOP reads information about fonts so it can describe the fonts properly as it creates PDF. The information FOP needs about a font are stored in the font name, font weight, and font style attributes. These values are referred to as the font-triplet. FOP also needs to know the location of the font file and, if a font metrics file is needed, the location of the font metrics file.
Automated registration of fonts makes it difficult for you to reference the fonts accurately, so this method is not recommended.
Creating font metrics files (not needed for FOP 0.94 and later)
In FOP 0.94 and later, you do not need to supply metrics files to register fonts in most cases. For FOP 0.94 and later, you can skip to the next section, Individual registration of fonts.
The instructions on the Apache web page are a little sketchy on this part of the installation. For example, to create the metrics files, you are instructed to run the PFMReader for Type 1 fonts and the TTFReader for TrueType fonts using a command line for the PFMReader. The example command line shown on the Apache web site is not exactly right.
I found that I had to list all (or almost all) of the .jar files in the fop/lib directory in the -cp value for this command line rather than just the few specified by the web site. Also, the names of the .jar files shown in the example are generic names. The real names include the version numbers of the jar files.
The Apache instructions assume that you are running the command line from the fop directory.
Once I made these adjustments, my command line for generating the Palatino Linotype metrics file was as follows:
java -cp "build/fop.jar;lib/avalon-framework-4.2.0.jar;lib/commons-logging-1.0.4.jar;lib/commons-io-1.3.1.jar;lib/serializer-2.7.0.jar;lib/xmlgraphics-commons-1.3.1.jar"
org.apache.fop.fonts.apps.TTFReader C:/Windows/Fonts/pala.ttf C:/fontmetrics/ttfpala.xml
In this example, the command reads the font file located at C:/Windows/Fonts/pala.ttf and creates a font metrics file at C:/fontmetrics/ttfpala.xml.
Individual registration of fonts
To register a font in the FOP configuration file, you use multiple <font> elements inside the <fonts> element, as instructed on the Apache web site. In addition to the information on the Apache web site, there are a couple of other points to consider:
- In FOP 0.94 and above, you do not need the metrics-url attribute for most fonts.
- The Apache web site’s example for the use of relative references
is a little misleading. From the Apache examples, it appears that
specifying <font-base>.</font-base> in the <fop> element
will cause FOP to use the directory containing the file as the base
location for relative references. If this were true, I should be
able to put the metrics file in the conf folder with only the filename in
the metrics-url attribute. I could not get that approach to work.
However, I could get FOP to recognize a relative path outside the fop folder. For example, I specified this for my installation of FOP:
<font-base>../../../../../../../Windows/Fonts</font-base>When I established this path as the relative path for fonts, I could enter the filename for a font file in the embed-url attribute without having to specify the entire path to the file. Following is an example for the Eras Bold font:
<font kerning="yes" embed-url="erasbd.ttf">
<font-triplet name="ErasB" style="normal" weight="normal"/>
</font>You can also set the font base URL when executing FOP as an embedded application by using fopFactory.setFontBaseURL. That option is documented in the Apache pages.
- The name attribute in the font-triplet element does not have
to be the name of the font in the font file or the font metrics
file. In fact, you may not want it to be.
For reference, the <font> element is structured like this:
<font kerning="yes" embed-url="file:///C:/myfonts/FTLI_____.pfb">
<font-triplet name="FrutigerLightItalic" style="italic" weight="normal"/>
</font>Note in the preceding sample that the font is FrutigerLightItalic and the font-style is italic. Suppose your XSL-FO code has <p> tags generating the FO element <fo:block font-family="FrutigerLight">. Then, within a given paragraph, the author inserts a <varname> or an <i> element. In the default XSL-FO implementation of the DITA Open Toolkit, <varname> or <i> creates an inline element like this:
<fo:inline font-style="italic">Because no font-family name or font-weight is supplied, the fo:inline element takes the current defaults:
name="FrutigerLight" style="italic" weight="normal"This combination of values does not match the font-triplet combination specified in the preceding font-triplet example (<font-triplet name="FrutigerLightItalic" style="italic" weight="normal"/>). The results would not be what you expect.One solution is to name all style variants of FrutigerLight, FrutigerBold, and so on, just Frutiger (or Fred for that matter), regardless of the official font name in the font file. Then the font variants can be distinguished by the font-weight and font-style attributes, both in the configuration file and the XSL-FO. In general, the naming of fonts with the font-triplet can be tricky—familiarity with the XSL-FO stylesheets in the DITA Open Toolkit is helpful.
-
When you supply font-weight="bold" in the XSL-FO, it is sometimes reported to FOP as font-weight="700", and font-weight="light" is reported to FOP as font-weight="200". In these cases you may need to specify two font-triplet elements to cover both possibilities, as shown in the following code for a bold font:
<font metrics-url="sab.xml" kerning="yes" embed-url="file:///C:/Windows/Fonts/sab____.pfb">
<font-triplet name="Sabon" style="normal" weight="bold"/>
<font-triplet name="Sabon" style="normal" weight="700"/>
</font>In this example, the Sabon Bold font is defined with a font-name of Sabon and a font-weight of bold or 700. When the XSL-FO creates an object with the attributes font-name="Sabon", font-weight="bold", and font-style="normal", FOP will recognize that the font file specified in this font element should be used.
Next Page:
XSL-FO stylesheets and FOP-registered fonts
