简体   繁体   English

如何从具有多个TTF文件的Font系列导入自定义java.awt.Font? (包括一个例子)

[英]How to import a custom java.awt.Font from a Font family with multiple TTF files? (An example is included)

I know that you can import a Font in Java with something like this: 我知道您可以使用以下内容导入Java中的Font:

File file = new File(fontPath);
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
// alternative:
// Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(file));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);

Then you would use for example font.deriveFont(Font.PLAIN, 20); 然后你会使用例如font.deriveFont(Font.PLAIN, 20); to get the desired style and size. 获得所需的风格和大小。


Example

But now let's look as an example at the font Consolas , there you have four TTF files: 但现在让我们看一下Consolas字体的例子,那里有四个TTF文件:

  • consola.ttf (Plain) consola.ttf (Plain)
  • consolab.ttf (Bold) consolab.ttf (Bold)
  • consolai.ttf (Italic) consolai.ttf (Italic)
  • consolaz.ttf (Bold & Italic) consolaz.ttf (Bold&Italic)

Of course you could just import consola.ttf with the method stated above, but then using font.deriveFont(Font.BOLD, 20); 当然你可以用上面consola.ttf的方法导入consola.ttf ,然后使用font.deriveFont(Font.BOLD, 20); isn't the same as using consolab.ttf because the plain font was simply transformed to look like a bold font. 是不一样的使用consolab.ttf因为普通的字体简单地转化为看起来像一个大胆的字体。


Example Pictures 示例图片

  1. Here I used the installed font with new Font("Consolas", Font.PLAIN, 20); 这里我用new Font("Consolas", Font.PLAIN, 20);安装了new Font("Consolas", Font.PLAIN, 20); and new Font("Consolas", Font.BOLD, 20); new Font("Consolas", Font.BOLD, 20); (as a side note, if the font is installed on the system you also get the right bold font if you use deriveFont(Font.BOLD); ): (作为旁注,如果您在系统上安装了字体,那么如果使用deriveFont(Font.BOLD);您也会获得正确的粗体字体deriveFont(Font.BOLD); ):

使用已安装的TTF字体

  1. And this is consola.ttf , imported with createFont and derived bold font (both with size 20 like the example above): 这是consola.ttf ,使用createFont和派生的粗体字体导入(两者的大小均为20,如上例所示):

使用导入的TTF和派生的粗体字体


Well when installed it isn't a problem, but I don't expect others to have a custom Font, so I want to put the TTFs into the jar file, so that I can import them during the initialization via getResourceAsStream(path) . 好吧,安装时没问题,但我不希望别人有自定义字体,所以我想把TTF放到jar文件中,以便我可以在初始化期间通过getResourceAsStream(path)导入它们。

Is there a way to import all relevant TTFs and then just call new Font("Custom Font Name", fontStyle, fontSize); 有没有办法导入所有相关的TTF,然后只需调用new Font("Custom Font Name", fontStyle, fontSize); so that it's used like an installed font (Picture 1), and that it doesn't looks like a derived 'fake' bold font (Picture 2)? 所以它像安装的字体一样使用(图1),它看起来不像派生的'假'粗体字(图2)?

I'm not sure what exactly is the problem. 我不确定究竟是什么问题。 You got all your TTF files and you have to import and register them. 你得到了所有的TTF文件,你必须导入并注册它们。 Following tests use DejaVu Sans fonts which are not installed on my system. 以下测试使用未安装在我的系统上的DejaVu Sans字体。


Test 1 测试1

Font f = Font.createFont(Font.TRUETYPE_FONT, new File("dvs.ttf"));

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(f);

Font dvs = new Font("DejaVu Sans", Font.PLAIN, 20);

Here's an image with plain ( dvs ) and derived bold ( dvs.deriveFont(Font.BOLD) ) font. 这是一个带有plain( dvs )和派生粗体dvs.deriveFont(Font.BOLD) )字体的图像。

在此输入图像描述

Test 2 测试2

Font f = Font.createFont(Font.TRUETYPE_FONT, new File("dvs.ttf"));
Font fb = Font.createFont(Font.TRUETYPE_FONT, new File("dvsb.ttf"));

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(f);
ge.registerFont(fb);

Font dvs = new Font("DejaVu Sans", Font.PLAIN, 20);
Font dvsb = new Font("DejaVu Sans", Font.BOLD, 20);

And here's an image with plain ( dvs ) and truly bold ( dvsb ) font. 这是一个普通( dvs )和真正粗体dvsb )字体的图像。

在此输入图像描述

You can confirm that correct file is used by looking at font2DHandle . 您可以通过查看font2DHandle来确认使用了正确的文件。

在此输入图像描述在此输入图像描述


I also tested italic and bold italic and both worked as well as Font#createFont(int, InputStream) method. 我还测试了斜体粗体斜体 ,并且与Font#createFont(int, InputStream)方法一样有效。

Above approach works because fonts are mapped by their full name (eg Arial, Arial Bold etc.), so as long as your fonts are correctly named you can register multiple members of one family. 上面的方法是有效的,因为字体是按其全名映射的(例如Arial,Arial Bold等),因此只要您的字体名称正确,您就可以注册一个系列的多个成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM