字体库支持 freetype
要使osg、osgearth支持中文,必须要有 freetype,所以在编译它们前,先编译好 freetype。并且在编译osg时,使用freetype库。
环境变量
osg中使用的默认字体是不支持中文的,所以需要设置默认字体。这个可以通过环境变量来修改。
export OSGEARTH_DEFAULT_FONT=simsun.ttc
以上是设置默认字体为仿宋
代码
在代码中,使用字符编码为: utf8
osgEarth::Util::Controls::LabelControl* pLabel =
new osgEarth::Util::Controls::LabelControl();
pLabel->setEncoding(osgText::String::ENCODING_UTF8);
pLabel->setText(tr("Coordinate:").toUtf8().data());
osgEarth::Style pm;
pm.getOrCreate<osgEarth::IconSymbol>()->declutter() = true;
pm.getOrCreate<osgEarth::TextSymbol>()->halo() = osgEarth::Color("#5f5f5f");
pm.getOrCreate<osgEarth::TextSymbol>()->encoding() =
osgEarth::TextSymbol::ENCODING_UTF8;
labelGroup->addChild(new osgEarth::Annotation::PlaceNode(m_MapNode, osgEarth::GeoPoint(geoSRS, start.x(), start.y()), tr("Start").toUtf8().data(), pm));