12

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.

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 | 输入".

Frank

Frank
  • 30,590
  • 58
  • 161
  • 244
  • May we assume that you're talking about a Swing application? It wasn't clear from your message which kind of Java application you're talking about. – BalusC Feb 16 '10 at 02:57

3 Answers3

9

java.awt.GraphicsEnvironment.getAvailableFontFamilyNames() can give you a list of the available fonts installed on the current system. You could also use java.awt.GraphicsEnvironment.getAllFonts() to get java.awt.Font objects.

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

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • 1
    In NetBeans one is allowed to choose which Font is used for things like JLabels. I'd like to confirm that just because a font is available to use in NetBeans doesn't necessarily mean it will be installed on the user's machine correct? – fIwJlxSzApHEZIl Oct 28 '14 at 18:35
  • 1
    @advocate This answer is so old, and it's been so long since I used Java, that it might be the case. 4 years is a long time for a Java API to mature. Regardless, the crux of the OP question was how to detect if certain characters could *show up* on a machine, regardless of how fonts are installed/handled in the system. Fonts could be installed programmatically at run time for all this code cares about. – amphetamachine Oct 28 '14 at 19:15
  • indeed it has been a while. i'm speculating that the entire reason there's an API to query for the system installed fronts indicates that 1) java does not install fonts universally across operating systems and that 2) not all fonts are supported by all operating systems. this code in essence allows you to verify the font you'd like to use exists on the users machine. that way you can change if it doesn't. – fIwJlxSzApHEZIl Oct 28 '14 at 23:27
  • 2
    @advocate Usually Java does not install fonts (actually, Oracle JRE has several bundled fonts). Your application can bundle other fonts and use them. See [How can I use a custom font in Java?](http://stackoverflow.com/a/5652385/572834) and [How do you import a font?](http://stackoverflow.com/a/8365030/572834) for more information. The code in the answer allows you: 1) check whether a certain font is available on the system and 2) check whether a font can display a particular character. – Alexey Ivanov Apr 13 '16 at 10:41
2

Lazy version:

Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()).contains(FONT_NAME)
Daniel Centore
  • 3,220
  • 1
  • 18
  • 39
2

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.

Beholder
  • 470
  • 5
  • 7