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

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

问题描述

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



使用

  GraphicsEnvironment.getLocalGraphicsEnvironment()。getAvailableFontFamilyNames()

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

pre $ font-size(fontName)
$ / code>


我可以得到awt.Font实例。



据我所知Font未连接到实际的PhysicalFont,所以
如何检索ttf字体文件,或从该ttf文件的字节数据从列表中的字体或awt.Font?我试图检索一个物理字体数据或类似的东西。这个数据应该在什么地方?

我需要它的原因是最终使用libGDX
FreeTypeFontGenerator来生成BitmapFont



这个在windows,osx和linux上都能正常工作。

正如@NateS所指出的那样,看来我想达到的是不完全可能的。所以我现在只是分享我的情况下使用的解决方案:



这个类是:
FontManager.java



这允许根据您的系统预先缓存已知系统位置中的现有ttf文件,然后为fontName-> fontFile类型的连接创建一个Map。这然后去偏好,并在下一次运行加载。

已知问题是


  1. awt.Font有一个已知的错误能够在osx系统上读取一些ttf字体家族名称(主要是一些阿拉伯文和中文字体)
  2. 没有在Linux上测试,可能会失败。

  3. 如果你有很多字体,第一次运行可能会很慢。

编写一个本地库是理想的,但时间是本质...


I have some ttf fonts installed on system.

I get that list using

GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()

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

Font.decode(fontName)

I can get awt.Font instance.

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?

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

This has to work on windows, osx and linux.

解决方案

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:

Which is this class: FontManager.java

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.

Known issues are

  1. awt.Font has a known bug that is not able to read some ttf font family names on osx systems (mainly some arabic and chinese fonts)
  2. Did not test on Linux, will probably fail.
  3. First run might be slow if you have many fonts.

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

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

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