本文主要介绍解决在osgEarth中的中文乱码问题。
主要有两个需要注意的点:
1、将字符串从Unicode转换为UTF-8编码:
void unicodeToUTF8(const wstring &src, string& result)
{
int n = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0);
result.resize(n);
::WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0);
}
unicodeToUTF8(L"中国", _strWideName);
2、添加中文字体:
textStyle->font() = "simkai.ttf";
完整代码
//Unicode转UTF-8函数
void unicodeToUTF8(const wstring &src, string& result)
{
int n = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0);
result.resize(n);
::WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0);
}
//显示中文代码
osgEarth::Style style;
osgEarth::Symbology::TextSymbol * textStyle = style.getOrCreateSymbol<osgEarth::Symbology::TextSymbol>();