java font 字体库,如何从具有多个TTF文件的字体家族中导入自定义的java.awt.Font? (包括一个例子)...

I know that you can import a Font in Java with something like this:

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); 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:

consola.ttf (Plain)

consolab.ttf (Bold)

consolai.ttf (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); isn't the same as using consolab.ttf because the plain font was simply transformed to look like a bold font.

Example Pictures

Here I used the installed font with new Font("Consolas", Font.PLAIN, 20); and 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);):

sRmG0.png

And this is consola.ttf, imported with createFont and derived bold font (both with size 20 like the example above):

1DK0f.png

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).

Is there a way to import all relevant TTFs and then just call 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)?

解决方案

I'm not sure what exactly is the problem. You got all your TTF files and you have to import and register them. Following tests use DejaVu Sans fonts which are not installed on my system.

Test 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.

tUqXz.png

Test 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.

UuMdq.png

You can confirm that correct file is used by looking at font2DHandle.

ggMk0.png

yL9W0.png

I also tested italic and bold italic and both worked as well as Font#createFont(int, InputStream) method.

Above approach works because fonts are mapped by their full name (e.g. Arial, Arial Bold etc.), so as long as your fonts are correctly named you can register multiple members of one family.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值