[英]Java: Font does not load (from external file)
In a folder called data , I have a font file called font.ttf . 在名为data的文件夹中,我有一个名为font.ttf的字体文件。
I want to read it in, like this: 我想像这样阅读:
try {
Font f = Font.createFont(Font.TRUETYPE_FONT,
new File("data/font.ttf")).deriveFont(12.0);
} catch (IOException | FontFormatException e) {}
That worked fine up until I removed the same font from the system (it's still in the data folder!). 直到我从系统中删除了相同的字体(它仍然在数据文件夹中!)之前,这种方法都可以正常工作。 Now, it's just showing Java's generic font.
现在,它只是显示Java的通用字体。
Is it even possible to read in a font from a file which is not in the System Fonts folder? 是否甚至可以从“系统字体”文件夹中的文件中读取字体?
You don't seem to be doing anything with that Font f
once you've loaded it in, so you might simply be losing it due to block scoping. 加载后,您似乎并没有对该
Font f
做任何事情,因此由于块作用域,您可能只是在丢失它。
Font fallback = load some system default;
Font myfont = null;
...
try {
File ffile = new File("data/font.ttf");
myfont = Font.createFont(Font.TRUETYPE_FONT, ffile).deriveFont(12);
} catch (FontFormatException ffe) {
System.out.println("Tried to load a bad font...");
ffe.printStackTrace();
} catch (IOException ioe) {
System.out.println("I have no idea what went wrong");
ioe.printStackTrace();
} finally {
loadMyApplication(myfont == null ? fallback : myfont);
}
Or if you absolutely need that font for correct UI building or content rendering, call loadMyApplication
in the try
block, so that any catches prevent your application from loading. 或者,如果您绝对需要该字体来进行正确的UI构建或内容呈现,请在
try
块中调用loadMyApplication
,以便所有捕获都阻止您的应用程序加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.