1

I found this Q&A : Detect when a unicode character cannot be displayed correctly

But it's not in Java.

I have the following code :

int codePoint=Integer.parseInt(unicodeText,16);
byte eBytes[]=new String(Character.toChars(codePoint)).getBytes(StandardCharsets.UTF_8);
String str=new String(eBytes,Charset.forName("UTF-8"));

JButton button=new JButton(str);

It can display unicode image on a JButton like this :

enter image description here

So if you set unicodeText to something like "1F602", it can display an image on a button.

My questions are :

<1> I tried both Java 8 and Java 12, result is the same, they are missing the same images from certain unicodes, why ? What can be done to make the missing images show up, seems Java upgrade didn't do it.

<2> How can I detect from my Java app which unicodes can't not be displayed ?

Frank
  • 30,590
  • 58
  • 161
  • 244

1 Answers1

1

It depends on the font, so use Font::canDisplayUpTo

So actually, grab your Font and try the following:

JButton button = new JButton(str);
Font font = button.getFont();
int failingIndex = font.canDisplayUpTo(str);
if (failingIndex >= 0) {
  // failingIndex points to the first codepoint in your string that cannot be represented with the font.
} else {
  // This string can be displayed with the given font.
}

So if the font cannot render the characters as expected, use another font that can.

Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137