Loading some TrueType font from a TTF file in Java raises a FontFormatException: font name not found

I am trying to create an instance of java.awt.Font from a TTF file on my system, but only some fonts can load without errors. The code below is some test code that I found on the Internet. When working on my system, it can successfully download 285 fonts (for example, Arial.ttf), but it does not work for 83 fonts (for example, AmericanTypewriter.ttf).

All errors are of the form FontFormatException: Font name not found without a built-in reason.

Are java.awt.Font and format compatibility issues known? I can not find anything after a lot of google.

 public static void main(String[] args) { String rootPath = "/Library/Fonts"; File root = new File(rootPath); if (root.canRead()) { String[] fontFiles = root.list(); Font font = null; for (String fontFile : fontFiles) { try { System.out.println(fontFile); font = Font.createFont(Font.TRUETYPE_FONT, new File(root + "/" + fontFile)); System.out.println(font); } catch (Exception e) { e.printStackTrace(); } } } } 

My environment is Java 7, OS X Mavericks (10.9.1).

 java version "1.7.0_40" Java(TM) SE Runtime Environment (build 1.7.0_40-b43) Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode) 

Any help here would be greatly appreciated.

+3
source share
2 answers

OK, so after some digging, it turns out that this problem is due to an error (function!) In the implementation of java.awt. Namely, loading the TrueType physical font fails if the table of font file names does not include the names of surnames and full names.

To identify the problem, I used GrepCode to track back from the corresponding exception in the OpenJDK AWT implementation. As soon as I discovered a problem with the name table, I used ttx , a no-frills TrueType metadata editor, to add the names that Java is looking for. Example:

 <namerecord nameID="1" platformID="3" platEncID="1" langID="0x409"> American Typewriter </namerecord> <namerecord nameID="4" platformID="3" platEncID="1" langID="0x409"> American Typewriter </namerecord> 

New TTF files created by ttx can now be opened by Java. Hooray!

+4
source

This is the function of Oracle JDK / OpenJDK ( bug ).

The problem is caused by sun.font.TrueTypeFont and the lack of Mac TrueType font name support.

TrueTypeFont.java contains a check for reading the TrueType font name table to read only the Microsoft platform ID name (platformID == 3) , and not the Mac platform ID (platformID == 1) . In many cases, truetype fonts included in OSX have names with platformID of 1. Because of this, the initNames() method does not set the value for familyName or fullName , and the code after initNames() runs checks to make sure they are null ( that they are, of course), and throws a Font name not found exception.

In the Apple JDK (1.6, and possibly 1.5?), Apple did not use TrueTypeFont as the FontHandler. They had their own implementation called sun.font.AppleNativeFont . You can't even use sun.font.TrueTypeFont in the Apple JDK because it will always use the sun.font.AppleNativeFont handler, which supports both Mac and Microsoft platform identifiers.

It seems that sun.font.AppleNativeFont does not exist in OpenJDK or Oracle JDK, although Apple "donated" the Oracle code.

It would be useful in terms of continuity to include sun.font.AppleNativeFont in OpenJDK / Oracle JDK. It would be even better if TrueTypeFont.java was improved to include Mac TrueType font support.

UPDATE: SOLUTION WORKAROUND.

One of the patches in the comments below was submitted to the OpenJDK repository. If you clone the repository with the correct revision, you can get the corrected files.

hg clone -r 8b05f9b91765 http://hg.openjdk.java.net/jdk7/jdk7/jdk

Copy the following files from jdk/src/shared/classes/sun/font :

 TrueTypeFont.java LCIDMap.java AppleLangID.java 

To the same directory in the trunk of the current JDK (I used jdk8-b132), compile it and you will have Mac font support. You can also apply this patch, which was made after the Mac patch:

http://hg.openjdk.java.net/jdk7/jdk7/jdk/rev/3dabb2f78e73

Voila!

+3
source

Source: https://habr.com/ru/post/1498637/


All Articles