We have an Applet that can possibly display Chinese text. We are specifying a font for it (Arial), it works fine under both Windows and Mac OSX.
But in Firefox on Linux the Chinese characters are rendered as squares. Is there a way to work around this? Note that we can’t assume the existence of a particular font file on the client.
This indicated that the font does not support Chinese characters (which you probably guessed).
You might find the java.awt.Font.canDisplayUpto() method interesting.
http://www.j2ee.me/javase/6/docs/api/java/awt/Font.html#canDisplayUpTo(java.lang.String)
“Indicates whether or not this Font can display a specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code. If the Font can display all characters, -1 is returned.”
That’s because Arial on Windows and Mac are all Unicode font but it only has Latin-1 charset on Linux. On many Linux distributions, Chinese fonts are optional and there may not be Chinese font available.
A common technique is to searching through all your font to see any of them can display Chinese characters. For example,
<param name="java_arguments" value="-Dfile.encoding=utf-8" />
I found the code here inadequate for my needs.
I needed to test an unknown input string, to determine what font to use, hence, I needed to check every single character. (see below)
By the way, the font.canDisplayUpTo method will not work. It may approve a font, that can only display some of the characters.
So, just use this code below.