javafontsawtgraphics2dtruetype

Draw Unicodes/UTF-8 characters such as 'ಠ' with AWT Graphics in Java


I'm trying to draw a face with such unicodes:

ಠ_ಠ

However, it produces rectangles instead.

I tried the following:

BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
Font font = new Font("Arial", Font.BOLD, FACE_SIZE);

g.setColor(FACE_COLOR);
g.setFont(font);
g.drawString("ಠ_ಠ", x, y);

I also tried the following methods:

g.drawString(new String("ಠ_ಠ".getBytes("UTF-8"), "UTF-8"), x, y);
g.drawString(StringEscapeUtils.unescapeJava("\\u0ca0\\u005f\\u0ca0"), x, y);

But everything produced the following result:

Image of the result of the previous methods

I also tried on Linux, Windows, and same result. Changing font doesn't help.

In my gradle settings I made sure to have the following:

compileJava.options.encoding = 'UTF-8'
tasks.withType( JavaCompile ) {
    options.encoding = 'UTF-8'
}

I know Arial supports the characters because I can draw them in an image editing software.

If that may help this is in a Spring Boot project.


Solution

  • As the Comments explain…

    Glyph

    Use a font that provides a glyph for each of your characters.

    Your font lacks a glyph for the character.

    String literal

    Do not complicate instantiating a string in Java. A String literal suffices.

    Change this:

    g.drawString(new String("ಠ_ಠ".getBytes("UTF-8"), "UTF-8"), x, y);
    

    … to this:

    g.drawString( "ಠ_ಠ" ) ;