0

I'm trying to display a string, which contains Cyrillic characters using raylib. So I load a font with codepoints like so:

int codepoints[512] = { 0 };
for (int i = 0; i < 95; i++) codepoints[i] = 32 + i;
for (int i = 0; i < 255; i++) codepoints[96 + i] = 0x400 + i;
Font font = LoadFontEx("arial.ttf", 32, codepoints, 512);

If I draw font.texture on screen, I can see both ASCII and Cyrillic characters on screen. However, I can't make DrawTextEx render those characters, yet DrawTextCodepoint works as I expect it to. For example:

    BeginDrawing();
    ClearBackground(RAYWHITE);

    DrawTextEx(font, "Ё", (Vector2) { 10, 70 }, 32, 0, BLACK); // draws a ? symbol
    DrawTextEx(font, "\u0401", (Vector2) { 10, 40 }, 32, 0, BLACK); // draws a ? symbol
    DrawTextCodepoint(font, 0x0401, (Vector2) { 10, 10 }, 32, BLACK); // draws Ё, as expected       
    EndDrawing();
Dmitriy
  • 1,852
  • 4
  • 15
  • 33

1 Answers1

0

I should have spent just a few more minutes googling for an answer. I'm using Visual Studio 2022, and after looking at this response for a more general question, it dawned on me that I should explicitly mark my string literals as UTF-8 encoded with the u8 prefix. So, this will work perfectly:

DrawTextEx(font, u8"Ё", (Vector2) { 10, 70 }, 32, 0, BLACK); // draws Ё, as expected    
DrawTextEx(font, u8"\u0401", (Vector2) { 10, 40 }, 32, 0, BLACK); // draws Ё, as expected too   
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dmitriy
  • 1,852
  • 4
  • 15
  • 33
  • You are using the `TCHAR` version of `DrawTextEx()`. What you claim will work only if your project [*doesn't* define `UNICODE`](https://docs.microsoft.com/en-us/windows/win32/learnwin32/working-with-strings#unicode-and-ansi-functions) so that `DrawTextEx()` maps to `DrawTextExA()`, AND you explicitly [opt-in to enable UTF-8 in ANSI APIs](https://docs.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page). Otherwise, you need to instead use the Unicode function `DrawTextExW()` (directly, or by defining `UNICODE`) with UTF-16 strings, eg: `DrawTextExW(font, L"Ё", ...);` – Remy Lebeau Aug 09 '22 at 00:44