简体   繁体   中英

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?

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.

Your best bet is probably to follow the framework's convention and import tchar.h , then use _tfopen instead (and switching to LPCTSTR , _T("your_string") , etc.). This will let you compile the same code for Linux and Windows with almost no change, while supporting either UTF-16 or UTF-8 in the code.

You can use FT_Open_Face method, and it need a FT_Open_Args struct as argument. In FT_Open_Args.stream, you can set customs read and close callback, and FreeType can read font data from any stream you want.

good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM