Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

fonts

detect

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

like image 335
Frank Avatar asked Feb 16 '10 02:02

Frank


2 Answers

Lazy version:

Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()).contains(FONT_NAME)
like image 136
Daniel Centore Avatar answered Sep 17 '22 21:09

Daniel Centore


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

like image 30
amphetamachine Avatar answered Sep 20 '22 21:09

amphetamachine