从Java中TTF文件加载一些TrueType字体引起FontFormatException此:字体名称未找到 [英] Loading some TrueType Font from TTF file in Java causes FontFormatException: Font name not found

查看:5683
本文介绍了从Java中TTF文件加载一些TrueType字体引起FontFormatException此:字体名称未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的系统上的文件TTF创建java.awt.Font中的实例,但只有一些字体能够无差错加载。下面的code是一些测试code我在网上找到。当我的系统上运行,它能够成功(例如ARIAL.TTF)装载285字体,但未能上83字体(例如AmericanTypewriter.ttf)。

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 online. When run on my system, it is able to load 285 fonts successfully (ex. Arial.ttf), but fails on 83 fonts, (ex. AmericanTypewriter.ttf).

所有错误的形式是 FontFormatException此:字体名称找不到没有嵌入事业

All errors are of the form FontFormatException: Font name not found with no embedded cause.

是否有与java.awt.Font中和格式兼容性已知问题?谷歌搜索多后,我无法找到任何东西。

Are there known issues with java.awt.Font and format compatibility? I can't find anything after much Googling.

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();
                }
            }
        }
    }

我的环境是Java 7中,OS X小牛(10.9.1)。

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)

在这里任何帮助将大大AP preciated。

Any help here would be greatly appreciated.

推荐答案

确定,因此一些挖后,事实证明,这个问题是由于一个bug(功能!)在java.awt实施。也就是说,加载物理TrueType字体如果字体文件的名称表不包括姓和全名的记录失败。

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.

要找出问题所在,我用grep的code到从OpenJDK的的AWT执行相关的异常落后的追查。有一次,我发现了名称表的问题,我用 TTX ,一个没有廉价的TrueType元数据编辑器来添加名称条目的Java查找。例如:

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:

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

由河豚毒素产生新的TTF文件可能现在的Java打开。耶!

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

这篇关于从Java中TTF文件加载一些TrueType字体引起FontFormatException此:字体名称未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆