6

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.

  • 1
    SDL_ttf came with the following caveat: "This is a sample library which allows you to use TrueType fonts in your SDL applications. It comes with an example program `showfont` which displays an example string for a given TrueType font file." I didn't find a similar caveat for SDL2_ttf, but my experience with SDL_ttf was that there were memory corruptions / crashes that appear when you start to use exotic unicode characters. You can check their bug tracker. I think it's just meant for toy programs -- in my humble experience you are better to just use Freetype directly and forget about SDL_ttf. – Chris Beck Jun 03 '16 at 01:56
  • Thanks. As I'm new to SDL, I thought initially that all SDL_* libraries are officially part of it and of good quality. I also use SDL2_mixer, I hope I have more luck with it. For text rendering I'll look at other libraries. – Michał Brzozowski Jun 04 '16 at 06:52
  • That is exactly what I was wondering lately. Trying to do the same thing: render unicode with SDL_ttf, but it just won't work for characters longer than 2 bytes. Totally correct observation. So often SDL (and related libraries like SDL_ttf) turns out disappointing me. (For example, alpha blending doesn't work once you resize the window.) Bugs are all over the place. – huzzm Jun 09 '16 at 13:25
  • Did you try to render the '€' character? This is a 3 byte long unicode glyph that I was able to render! Why this seems to be the only character longer than 2 bytes that SDL_ttf is able to render correctly - I have no idea... – huzzm Jun 11 '16 at 17:45
  • I ended up using this library, it's just a couple of header and c files. It's rendering everything nicely.https://github.com/akrinke/Font-Stash – Michał Brzozowski Jun 11 '16 at 20:03

2 Answers2

1

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

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

0

There is a trick way around this that I stumbled on. If you use nerd fonts, for example the visa unicode char (1F4B3) would normally not be accessible, but they have it mapped in below FFFF. Where exactly I'm not sure, but you can use the ttfs from https://github.com/ryanoasis/nerd-fonts and get good coverage within 2 bytes.

Goblinhack
  • 2,859
  • 1
  • 26
  • 26