Search code examples
c#unicoderaylib

Making Raylib-cs work with extended ascii / unicode above 128 in windows?


I'm teaching C# and game dev to high school students, and we're using Raylib-cs as a simple introduction to graphics APIs and libraries.

We've hit a small snag: We're swedish, and we'd like to use some special non-ascii letters – namely å, ä and ö (lots of swedish words use them). However, I can't get Raylib-cs to display anything above codepoint 127 – at least not using DrawText.

Instead, all I get is ?.

This is on Windows 10, 64-bit, 20H2. Using dotnet 5 (latest) and primarily the Raylib-cs available as a nuget package.

What I've tried so far:

  • DrawText and DrawTextEx. Same result.
  • Loading different fonts, with or without explicit inclusion of codepoints up to 255. Same result.
  • Getting the latest Raylib-cs from the github page. Same result.
  • Running the same code but in a virtual Debian machine. THIS WORKS, so issue seems to be in Windows.
  • Asking a friend who's proficient in C/C++ to try using åäö using Raylib in C++. THIS WORKS, so the issue seems to be specific to Raylib-cs, even though it's just a wrapper?
  • DrawTextCodepoint. THIS WORKS, which means that for some reason the issue is specific to the DrawText methods (and InitWindow). Raylib is supposed to be Unicode-capable, and this proves that at least in theory, it is.

Here's my simple test code (just writes out characters 0-255):

static void Main(string[] args)
{
  int font_size = 10;
  Raylib.InitWindow(800, font_size * 64, "åäö");
  while (!Raylib.WindowShouldClose())
  {
    Raylib.BeginDrawing();
    Raylib.ClearBackground(Color.BEIGE);
    for (int i = 0; i < 255; i++)
    {
      int col = i / 64;
      int x = col * 200;
      int y = (i % 64) * font_size;
      string text = i.ToString() + " | " + ((char)i).ToString();

      Raylib.DrawText(text, x, y, font_size, Color.BLACK);
    }
    Raylib.EndDrawing();
  }
}

Result in windows (at least for me): Window title bar is "???", as are all characters beyond the second column. Result in debian: Window title bar is "åäö", and all characters are drawn as they should.

Has anyone come across this problem? Anyone got (tested) solutions?

Is there some known quirk in how C# specifically on windows handles strings or something?


Solution

  • The way the library imports DrawText is faulty; its default behavior "marshals" string values to fit a legacy character encoding, not necessarily UTF-8: https://github.com/ChrisDill/Raylib-cs/blob/b1f46d33071387800559523950aa131448251461/Raylib-cs/Raylib.cs#L2116 (Specifying CharSet=Unicode probably won't help because the DrawText function doesn't use wide-character strings, but rather ordinary char* pointers interpreted as UTF-8.)

    I see you have posted an issue to the GitHub repository. And fortunately, the library's author suggested two workarounds. As they said, one of them is: "Using [MarshalAs(UnmanagedType.LPUTF8Str)] to marshal strings as UTF8."