Changing the Font

In this section we will see how to change the default font of pMML2SVG and how to create the metrics file that are necessary to do the transformation.

To add a new font to pMML2SVG, you need to create font metrics files and add them in the XSLT2/ directory of pMML2SVG distribution. These font metrics files are created with ttfreader tool that comes with FOP. To fully support a font, you need to create four XML font metrics files with FOP ttfreader. The first file, named MyFont.xml, contains the normal style font metrics, from MyFont.ttf for example. The second font metrics file, named MyFont-Italic.xml, contains all metrics information about the italic font, from MyFontItalic.ttf for example. The third file, called MyFont-Bold.xml, contains all information about the bold font, from MyFontBol.ttf. And the last file, named MyFont-Bold-Italic.xml contains all metrics information about the bold italic font, from MyFontBolIta.ttf for example.

Since the initial FOP ttfreader did not provide enough precision to draw the character, some modifications have been done to the source code to add more precision to it. The new source code can be found in the ttfreader/ folder coming with the pMML2SVG distribution. To create font metrics files, this tool has to be compiled. It requires a complete FOP distribution to work correctly.

To compile the code, we will use the make script in the ttfreader/ folder. First, you have to modify the first two lines of this script to adapt the script to your local installation. The first line is the path to a FOP distribution and the second line is the Java classpath used in the compilation. This classpath must includes the following libraries: FOP.jar, commons-logging, commons-io and xmlgraphics-commons.

After making this configuration, you can compile the tool using ./make command. This script will produce the TTFReader.jar file. This final JAR file will be used to compile font metrics files.

To create a font metrics file, configuration has to be done in the TTFReader script that comes with pMML2SVG distribution in the ttfreader/ directory. As the make file, the first two lines have to be changed in the same way. The classpath must contain: TTFReader.jar, FOP.jar, commons-logging, commons-io and xmlgraphics-commons. The TTFReader.jar archive must be written before FOP.jar in the classpath.

We can now call TTFReader script to create a metrics file like that:

./TTFReader MyFont.ttf

This command will produce a MyFont.xml file that contains all the metrics that are needed by pMML2SVG. This script can also create a group of TTF files in one shot. For example, if you want to compile your entire font folder, you can run the following command:

./TTFReader $HOME/.fonts/*.ttf

All the newly created XML metrics files will be created in $HOME/.fonts/ folder. You can then copy all these metrics in the XSLT2/ directory of pMML2SVG by using, for example, the following cp command:

cp $HOME/.fonts/*.xml pMML2SVG/XSLT2/

Once these XML metrics files have been copied into XSLT2/ pMML2SVG folder, it will be able to use these new fonts in the transformation.

To tell pMML2SVG that it will use the new fonts, we can simply change the fontName parameter when calling the transformation. For example, the command will be (using the previous test.xml MathML example):

java -jar saxon9.jar -s:test.xml 
     -xsl:pMML2SVG/XSLT2/pmml2svg.xsl -o:test.svg fontName=MyFont

You can also keep the default font and add your font at the beginning of the fonts list, for our example, the parameter fontName will be MyFont,STIXGeneral,STIXSize1. This last modification can be done directly in the stylesheet or using the parameter.

Note that the font has to implement all the mathematical Unicode glyphs to have a correct rendering.