简体   繁体   English

如何使用 Java 查找某种字体是否正确安装在机器上

[英]How to find, with Java, if a certain font is installed correctly on a machine

I have a PC notebook running Win Vista, when I first bought it, certain Chinese fonts won't show up, I could only see rectangles, but I played with the control setting for a while, changed some properties, and now it shows Chinese fonts correctly, but I don't remember what I did.我有一台运行Win Vista的PC笔记本,刚买的时候,某些中文字体不显示,只能看到矩形,但我玩了一会儿控件设置,更改了一些属性,现在显示中文字体正确,但我不记得我做了什么。

Now some of my programs displays both English and Chinese, something like this : "Enter | 输入" (The Chinese here also means enter), but if a user doesn't have Chinese fonts installed properly on his machine, he will see something like this : "Enter | [][]", my question is : in Java how to detect if those characters will show up correctly on a certain machine, if not, just display "Enter", if it is, show "Enter | 输入".现在我的一些程序同时显示英文和中文,就像这样:“Enter | 输入”(这里的中文也意味着输入),但是如果用户在他的机器上没有正确安装中文字体,他会看到类似的东西this : "Enter | [][]", 我的问题是:在Java中如何检测这些字符是否会在某台机器上正确显示,如果没有,只显示“Enter”,如果是,则显示“Enter | 输入” ”。

Frank坦率

java.awt.GraphicsEnvironment.getAvailableFontFamilyNames() can give you a list of the available fonts installed on the current system. java.awt.GraphicsEnvironment.getAvailableFontFamilyNames()可以为您提供当前系统上安装的可用字体的列表。 You could also use java.awt.GraphicsEnvironment.getAllFonts() to get java.awt.Font objects. 您还可以使用java.awt.GraphicsEnvironment.getAllFonts()来获取java.awt.Font对象。

Then, you can use java.awt.Font.canDisplay(int) to check whether a Unicode character can be displayed in that font (where the int is the integer representation of the multibyte character). 然后,您可以使用java.awt.Font.canDisplay(int)来检查Unicode字符是否可以以该字体显示(其中int是多字节字符的整数表示)。

懒人版:

Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()).contains(FONT_NAME)

For those who are still interested.对于那些仍然感兴趣的人。 Performance tip: using getAvailableFontFamilyNames(Locale.ROOT) might be significantly faster than just getAvailableFontFamilyNames() because in the latter case locale-aware processing is performed.性能技巧:使用getAvailableFontFamilyNames(Locale.ROOT)可能不仅仅是显著快getAvailableFontFamilyNames()因为在后一种情况下可感知语言区域的处理。

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

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