Search code examples
c++visual-studiounicodefreetype

Can freetype functions accept Unicode filenames?


I have an MSVC project that uses freetype, and now I'm trying to move it to Unicode. But the freetype functions don't accept LPCTSTR arguments for file paths, they want "const char*". So code like

    WINDOWS_FONT WindowsFont;
    // ....
    FT_New_Face (pLibrary, WindowsFont.pszFileName, i, &face); // WindowsFont.pszFileName is LPTSTR

used to work when the project was ascii but not anymore when it's Unicode. Is there a way to make freetype accept Unicode filenames, some preprocessor define to switch it to unicode maybe?


Solution

  • There's no wfopen in C++ standard (2003). Since freetype is meant to be portable, it only uses fopen, which can only accept const char* filenames. So, either load file to memory (or memory map it) and then use FT_New_Memory_Face to create font or convert wchar_t pszFileName into 8-bit encoding, potentially losing characters due to impossible conversion.

    On linux, you could attempt to use setlocale so fopen will accept UTF8 strings, convert wchar_t string to UTF8. However on windows it won't work. So either load file to memory, or convert pszFileName to 8-bit encoding, then pass it to FT_New_Face.