简体   繁体   English

freetype 函数可以接受 Unicode 个文件名吗?

[英]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*".我有一个使用 freetype 的 MSVC 项目,现在我正试图将它移动到 Unicode。但是 freetype 函数不接受文件路径的 LPCTSTR arguments,它们需要“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?曾经在项目为 ascii 时工作,但在项目为 Unicode 时不再工作。有没有办法让 freetype 接受 Unicode 文件名,一些预处理器定义将其切换为 unicode 也许?

There's no wfopen in C++ standard (2003). C++ 标准 (2003) 中没有 wfopen。 Since freetype is meant to be portable, it only uses fopen, which can only accept const char* filenames.由于 freetype 是可移植的,它只使用 fopen,它只能接受 const char* 文件名。 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.因此,要么将文件加载到 memory(或 memory map),然后使用 FT_New_Memory_Face 创建字体,要么将 wchar_t pszFileName 转换为 8 位编码,可能会因无法转换而丢失字符。

On linux , you could attempt to use setlocale so fopen will accept UTF8 strings, convert wchar_t string to UTF8.在 linux 上,您可以尝试使用setlocale ,这样 fopen 将接受 UTF8 字符串,将 wchar_t 字符串转换为 UTF8。 However on windows it won't work .但是在 windows 上它不起作用 So either load file to memory, or convert pszFileName to 8-bit encoding, then pass it to FT_New_Face.所以要么加载文件到memory,要么将pszFileName转换成8位编码,然后传给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.).您最好的选择可能是遵循框架的约定并导入tchar.h ,然后改用_tfopen (并切换到LPCTSTR_T("your_string")等)。 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.这将使您可以为 Linux 和 Windows 编译相同的代码,几乎没有任何更改,同时在代码中支持 UTF-16 或 UTF-8。

You can use FT_Open_Face method, and it need a FT_Open_Args struct as argument.您可以使用 FT_Open_Face 方法,它需要一个 FT_Open_Args 结构作为参数。 In FT_Open_Args.stream, you can set customs read and close callback, and FreeType can read font data from any stream you want.在FT_Open_Args.stream中可以设置自定义读取和关闭回调,FreeType可以从任意一个stream中读取字体数据。

good luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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