October 23, 2024
Chicago 12, Melborne City, USA
java

Java Swing – Not able to display Unicode glyphs


I am trying to display few Unicode characters (glyphs) in my application. The characters can be ASCII, CJK, Emojis etc. – should be able to display all the (printable) characters in the Unicode character database. Eventually, the display must happen in a text editor (JTextPane). But many characters are being shown as tofus, which I think is a font issue.

// here "myText" is the Unicode string
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts)
{
  if (font.canDisplayUpTo(myText) < 0) {
    System.out.println(font.getName());
  }
}
// based on the font names in the above output, I will set the font for the editor instance.

I have set myText value as a combination of BMP characters and one supplementary character (that has an associated glyph), like Hi all𐀀. The last character is U+10000. I ran the above code. Got no output. So none of the existing fonts supports this string. Is this correct?

From where does Java AWT identify the fonts installed? Does it check somewhere on my system? If yes, where?

I tried searching over the web and found that I need to select a font that supports all the Unicode glyphs. From my research, I found that GNU Unifont supports the maximum number of glyphs. So I am trying to use it.

Font font = Font.createFont(Font.TRUETYPE_FONT, new File("/path/to/unifont/otf/or/ttf"));
     font = font.deriveFont(Font.PLAIN, 11);

Even with Unifont, I can’t display my string. I notice that there are separate TTF/OTF files for each plane – BMP, supplementary plane 1, supplementary plane 15 etc. I came to know that one TTF can support only 65535 glyphs (because of 16-bit unsigned integer limit). Is this correct?

If the limit is 65535 glyphs, how do I set the font that displays all glyphs? Is it possible to merge two TTFs/OTFs?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video