很多人问我如何显示汉字,总是一个一个地说,很麻烦,特此写在这里——
原因:OpenInventor支持Freetype字体,但汉字采用Unicode编码,故而无法直接正确显示
解决:使用Freetype字体,或者,将Unicode编码转换为Freetype可以识别的字体(使用内置的Freetype引擎)
本文采用第二种方法。
效果两个:
2 微软雅黑 字体
关键代码:
SoSeparator *textSep = new SoSeparator;
textSep->ref();
SoFont *fontType = new SoFont;
fontType->name = "Times New Roman : Italic";
//fontType->name = "微软雅黑.ttf";
fontType->name = "SIMKAI.TTF";
fontType->size = 30;
textSep->addChild(fontType);
// Chinese text
const wchar_t *str = L"Open Inventor程序设计从入门到精通";
SoText2 *textProp = new SoText2;
textProp->justification = SoText2::CENTER;
textProp->string.setValue(str);
textSep->addChild(textProp);
// Build up
root->addChild(textSep);
工程编辑的时候可能会出现错误:
error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __thiscall SoMFString::setValue(unsigned short const *)
这是因为 wchar_t 的问题,解决:
工程属性 C/C++语言 将……视为内置类型 yes