Search code examples
c++sdltruetypesdl-ttf

Is SDL2_ttf incapable of rendering characters that are longer than two bytes?


I'm switching from SFML to SDL2 in my program, and I started using SDL2_ttf to render text in UTF-8 format. I use the function TTF_RenderUTF8_Solid. I noticed that some characters are not rendered correctly anymore. One example is '🜍' (U+1F70D). I verified that the font contains these characters.

I tried using the glfont example program that's included in SDL2_ttf, and it also doesn't render these characters.

I went to the SDL2_ttf source code, and looked into the function TTF_RenderUTF8_Solid in SDL_ttf.c, and noticed this line:

    Uint16 c = UTF8_getch(&text, &textlen);

, while UTF8_getch returns Uint32. So the higher two bytes are discarded (and they aren't read anywhere else in the code), which explains why '🜍' is not rendered (it doesn't fit into Uint16). I changed the type of 'c' to Uint32, and it is assigned correctly 0x1F70D. It still doesn't render though. There are most likely other places where characters longer than two bytes weren't considered.

My question is whether it's just my misunderstanding or a known problem with SDL2_ttf, and if yes, if there's any known fix for this.


Solution

  • I ended up using this library, it seems to render everything as it should.

    https://github.com/akrinke/Font-Stash