Search code examples
javaandroidtextdrawutf-16

drawing text to canvas with UTF-16


I would like to draw this musical note:
𝄞 (U+1D11E) MUSICAL SYMBOL G CLEF

String musicalNote = "\uD834\uDD1E" // UTF-16

canvas.drawText(musicalNote, pos.x, pos.y, paint);

If I output the String musicalNote to the console it will display it correctly.
But if I draw it to the canvas it will draw a rectangle.

I am using the default font of android, I also tried the default monospace font. Could it be that the font doesnt have this character? Or do i need to encode it differently?


Solution

  • The problem was that the font does not support that character.

    It is possible to test if a font has a character, like this:

    String musicalNote = "\uD834\uDD1E" // UTF-16
    paint.setTypeface(Typeface.DEFAULT); // default font
    if (paint.hasGlyph(musicalNote)) {
        // font has glyph
    } else {
        // font does not have glyph
    }