无法读取* .ttf文件 [英] Can't Read *.ttf File

查看:1079
本文介绍了无法读取* .ttf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个我正在创建的语言的字典。该语言不使用任何英文字母,所以我创建了自己的字体使用免费的在线软件在 http://www.myscriptfont。 com / ,希望能够使用我的语言符号。制作这个词典的第一步就是导入这个自定义字体,但是它不起作用。这里是我导入它的代码的一部分:

$ $ p $
$ class $ Bank $ b $的构造函数b * /
public Bank()抛出异常
{
//以前的代码创建框架和窗格没有问题

try {
font1 = Font .createFont(Font.TRUETYPE_FONT,new File(fonts / ShoriPart1.ttf));

catch(IOException | FontFormatException e){
}
font1 = font1.deriveFont(Font.PLAIN,20);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font1);
JList fonts = new JList(ge.getAvailableFontFamilyNames());
JOptionPane.showMessageDialog(null,new JScrollPane(fonts));
JLabel l = new JLabel(快速的棕色狐狸跳过懒惰的狗,0123456789);
.setFont(font1);
pane.add(l);
frame.pack();

frame.setLocationRelativeTo(null);
frame.setVisible(true);



$ b $ p
$ b每次我运行这个函数,它抛出了java.io.IOExceptionCan读取字体\ShoriPart1.ttf(在java.awt.Font中)。我用try / catch来试图防止这种情况发生,但是最后只是没有创建字体。字体文件有问题吗?有没有办法解决这个问题在代码中?

编辑:我只是尝试在Microsoft Word中使用字体,并且它工作正常。

$ b $尝试使用 inputStream 将其传递给createFont函数:看看它是否工作!

  InputStream myStream = new BufferedInputStream(new FileInputStream(font.ttf)); 
ttfBase = Font.createFont(Font.TRUETYPE_FONT,myStream);

这发生在我身上,而且流过了这个流。不知道实际的原因,但是

I'm programming a dictionary for a language I'm creating. The language does not use any English letters, so I created my own font using free online software at http://www.myscriptfont.com/, which would hopefully allow me to use my language's symbols. The first step to make this dictionary would then be to import this custom font, but it's not working. Here's the part of the code where I import it:

    /**
     * Constructor for class Bank
     */
    public Bank() throws Exception
    {
        //Previous code creates frame and pane with no problems

        try{
            font1 = Font.createFont(Font.TRUETYPE_FONT, new File("fonts/ShoriPart1.ttf"));
        }
        catch(IOException|FontFormatException e){
        }
        font1 = font1.deriveFont(Font.PLAIN, 20);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font1);
        JList fonts = new JList(ge.getAvailableFontFamilyNames());
        JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
        JLabel l = new JLabel("The quick brown fox jumps over the lazy dog. 0123456789");
        l.setFont(font1);
        pane.add(l);
        frame.pack();

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

Every time I ran this, it threw the java.io.IOException "Can't read fonts\ShoriPart1.ttf (in java.awt.Font)." I used try/catch to try to prevent that, but it ended up just not creating the font at all. Is there something wrong with the font file? Is there a way to fix this in the code?

Edit: I just tried using the font in Microsoft Word, and it worked fine.

解决方案

Try using inputStream to pass it to createFont function: see if it works!

InputStream myStream = new BufferedInputStream(new FileInputStream("font.ttf"));
ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);

It happened to me once and passing the stream worked. Don't know actual reason though

这篇关于无法读取* .ttf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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