Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Swing does not display Unicode glyphs

Anand Ramarao-OracleOct 24 2024 — edited Oct 24 2024

I am trying to display few Unicode characters (glyphs) in my application. The characters can be ASCII (Basic Latin), 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 (JTextArea). 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 Basic Multilingual Plane (BMP) characters and one supplementary character (that has an associated glyph), like Hi all𐀀. The last character is U+10000.

Question 1: I ran the above code. Got no output. So none of the existing fonts supports this string. Is this correct?

Question 2: 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. I 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.

Question 3: I came to know that one TTF can support only 65535 glyphs (because of 16-bit unsigned integer limit). Is this correct?

Question 4: If the limit is 65535 glyphs, how do I set the font that displays all glyphs?

Question 5: Is it possible to merge two TTFs/OTFs?

Thank you.

Edit:

I understand that I can display ASCII with a font, a set of supplementary characters with another font - but I would like to display all at once - this is my exact problem.

Comments
Post Details
Added on Oct 24 2024
2 comments
402 views