Interesting Java bugs – Not all TTF fonts work in Java

I debugged a very tricky font issue today which is interesting because it covers a lot of font issues. So let me explain…

In theory a PDF font should embed all fonts (ie include the font data in the PDF file) unless it is one of the 8 font families the PDF file format includes (ie arial, courier, etc). But it does not enforce this so sometimes we can just have a random fonts in the PDF (say OCRB) with no information about it.

So how do we handle this case (especially in Java)?

Here is what we do if the font is not embedded.

1. See if there is a font file with the same name (ie OCRB.ttf or OCRB.otf) in the machines local font folder (ie C:/win/fonts on Windows, /Library/fonts on Mac).

2. See if the user has  setup any aliases (ie use ArialMT.tff for this font).

3. Ask Java if it has a font with this name and get Java to display it.

4. Use Lucida in Java to display it.

It turned out that on the machines having problems, the user had actually installed the font into the machine locally, which put a font file called OCRB______.ttf in the fonts sub-directory and also added the font OCRB to the list of fonts available to Java. So what happens on these machines was

1. No font called OCRB.tff to match

2. No alias

3. Ask Java if the font exists – yes it does. So we ask Java to display it. While Java has a font called OCRB, Java cannot actually use it. So we see no text.

When the user manually renamed the font to OCRB what then happens is it works. The pattern is now:-

1. Match on OCRB.tff and it works in our renderer.

To avoid renaming the file, we have the ability to set aliases, so we add to our code

FontMappings.setFontReplacements();           FontMappings.setSubstitutedFontAliases(“OCRB___”,new String[]{“OCRB”,”OCRBNEW”});

This means that the mapping is

1. No font called OCRB.tff to match

2. But we do have OCRB_____.ttf which we use for OCRB and OCRB

And if OCRB is not installed, we go to Lucida on stage 4.

The issue is that OCRB does not work with Java’s own font engine but it does with our font decoder (probably a CMAP issue).

There is still an issue that because the fonts are not embedded, the PDF will only view exactly as expected if you have your local machine setup which is really a flaw with Adobe not enforcing the PDF file spec…

This post is part of our “Fonts Articles Index” in these articles we explore Fonts.

Related Posts:

  • No Related Posts
The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.