如何从java中的系统字体中获取ttf字体数据 [英] How to get ttf font data from system fonts in java

查看:128
本文介绍了如何从java中的系统字体中获取ttf字体数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在系统上安装了一些 ttf 字体.

I have some ttf fonts installed on system.

我得到这个列表使用

GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()

这不仅是 ttf 字体,而且是我猜的所有字体.最终,如果我使用:

This is not only ttf fonts but all fonts I guess. Eventually if I use:

Font.decode(fontName)

我可以得到 awt.Font 实例.

I can get awt.Font instance.

据我所知,Font 没有连接到实际的 PhysicalFont,所以如何从该列表或 awt.Font 中检索 ttf 字体文件或该 ttf 文件中的字体的字节数据?我正在尝试检索物理字体数据或类似的东西.该数据应该在正确的地方吗?

As far as I know Font is not connected to actual PhysicalFont, so how can I retrieve either ttf font file, or byte data from that ttf file for a font from that list or from awt.Font ? I am trying to retrieve a physical font data or anything similar. That data should be somewhere right?

我需要它的原因是最终与 libGDX 一起使用FreeTypeFontGenerator 以生成 BitmapFont

The reason I need it is to eventually use with libGDX FreeTypeFontGenerator in order to generate BitmapFont

这必须适用于 windows、osx 和 linux.

This has to work on windows, osx and linux.

推荐答案

正如@NateS 所指出的,看来我想要实现的目标并不完全可能.

As @NateS pointed out, it appears what I want to achieve is not exactly possible.

所以我现在只分享我在我的案例中使用的解决方案:

So I will just share the solution I used in my case for now:

这个类是什么:FontManager.java

这允许根据您的系统在已知系统位置预缓存现有的 ttf 文件,然后为 fontName->fontFile 类型的连接制作映射.然后进入首选项,并在下次运行时加载.

This allows to pre-cache existing ttf files in known system locations based on your system, and then make a Map for fontName->fontFile type of connection. this then goes to preferences, and is loaded on next run.

已知问题是

  1. awt.Font 有一个已知错误,无法在 osx 系统上读取某些 ttf 字体系列名称(主要是一些阿拉伯文和中文字体)
  2. 未在 Linux 上测试,可能会失败.
  3. 如果您有很多字体,第一次运行可能会很慢.

最好写一个原生库,但是时间很重要...

It would be ideal to write a native lib, but time is of an essence...

这篇关于如何从java中的系统字体中获取ttf字体数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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