Loading some TrueType Font from TTF file in Java causes FontFormatException: Font name not found

后端 未结 2 1690
别跟我提以往
别跟我提以往 2021-01-14 02:33

I am attempting to create a java.awt.Font instance from a TTF file on my system, but only some fonts are able to load without error. The code below is some test code I found

2条回答
  •  灰色年华
    2021-01-14 03:15

    OK, so after some digging, it turns out that this problem is due to a bug (feature!) in the java.awt implementation. Namely, loading a physical TrueType font fails if the font file's name table does not include family name and full name records.

    To identify the problem, I used GrepCode to trace backward from the relevant exception in OpenJDK's AWT implementation. Once I had discovered the name table issue, I used ttx, a no-frills TrueType metadata editor to add the name entries Java looks for. Ex:

    
      American Typewriter
    
    
      American Typewriter
    
    

    The new TTF files generated by ttx could now be opened by Java. Yay!

提交回复
热议问题