1 // SGText.cxx - Manage text in the scene graph
2 // Copyright (C) 2009 Torsten Dreyer Torsten (_at_) t3r *dot* de
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # include <simgear_config.h>
25 #include <simgear/math/SGMath.hxx>
26 #include <simgear/misc/sg_path.hxx>
29 #include <osg/MatrixTransform>
30 #include <osgText/Text>
31 #include <osgText/Font>
35 class SGText::UpdateCallback : public osg::NodeCallback {
37 UpdateCallback( osgText::Text * aText, SGConstPropertyNode_ptr aProperty, double aScale, double aOffset, bool aTruncate, bool aNumeric, const char * aFormat ) :
39 property( aProperty ),
42 truncate( aTruncate ),
46 if( format.size() == 0 ) {
47 if( numeric ) format = "%f";
53 virtual void operator()(osg::Node * node, osg::NodeVisitor *nv );
55 SGConstPropertyNode_ptr property;
63 void SGText::UpdateCallback::operator()(osg::Node * node, osg::NodeVisitor *nv )
66 // hopefully the users never specifies bad formats here
67 // this should better be something more robust
70 double d = property->getDoubleValue() * scale + offset;
71 if (truncate) d = (d < 0) ? -floor(-d) : floor(d);
72 snprintf( buf, sizeof(buf)-1, format.c_str(), d );
74 snprintf( buf, sizeof(buf)-1, format.c_str(), property->getStringValue() );
76 if( text->getText().createUTF8EncodedString().compare( buf ) ) {
77 // be lazy and set the text only if the property has changed.
78 // update() computes the glyph representation which looks
79 // more expensive than a the above string compare.
86 osg::Node * SGText::appendText(const SGPropertyNode* configNode,
87 SGPropertyNode* modelRoot, const osgDB::Options* options)
89 SGConstPropertyNode_ptr p;
91 SG_LOG(SG_GENERAL, SG_DEBUG, "Creating a text object");
93 osgText::Text * text = new osgText::Text();
94 osg::Geode * g = new osg::Geode;
95 g->addDrawable( text );
97 SGPath path("Fonts" );
98 path.append( configNode->getStringValue( "font", "Helvetica" ));
99 text->setFont( path.str() );
101 text->setCharacterSize(configNode->getDoubleValue("character-size", 1.0 ),
102 configNode->getDoubleValue("character-aspect-ratio", 1.0 ));
104 if( (p = configNode->getNode( "font-resolution" )) != NULL )
105 text->setFontResolution( p->getIntValue( "width", 32 ), p->getIntValue( "height", 32 ) );
107 if( (p = configNode->getNode( "kerning" )) != NULL ) {
108 string kerning = p->getStringValue();
109 if( kerning.compare( "default" ) == 0 ) {
110 text->setKerningType( osgText::KERNING_DEFAULT );
111 } else if( kerning.compare( "unfitted" ) == 0 ) {
112 text->setKerningType( osgText::KERNING_UNFITTED );
113 } else if( kerning.compare( "none" ) == 0 ) {
114 text->setKerningType( osgText::KERNING_NONE );
116 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown kerning'" << kerning << "'." );
120 if( ( p = configNode->getNode( "axis-alignment" )) != NULL ) {
121 string axisAlignment = p->getStringValue();
122 if( axisAlignment.compare( "xy-plane" ) == 0 ) {
123 text->setAxisAlignment( osgText::Text::XY_PLANE );
124 } else if( axisAlignment.compare( "reversed-xy-plane" ) == 0 ) {
125 text->setAxisAlignment( osgText::Text::REVERSED_XY_PLANE );
126 } else if( axisAlignment.compare( "xz-plane" ) == 0 ) {
127 text->setAxisAlignment( osgText::Text::XZ_PLANE );
128 } else if( axisAlignment.compare( "reversed-xz-plane" ) == 0 ) {
129 text->setAxisAlignment( osgText::Text::REVERSED_XZ_PLANE );
130 } else if( axisAlignment.compare( "yz-plane" ) == 0 ) {
131 text->setAxisAlignment( osgText::Text::YZ_PLANE );
132 } else if( axisAlignment.compare( "reversed-yz-plane" ) == 0 ) {
133 text->setAxisAlignment( osgText::Text::REVERSED_YZ_PLANE );
134 } else if( axisAlignment.compare( "screen" ) == 0 ) {
135 text->setAxisAlignment( osgText::Text::SCREEN );
137 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown axis-alignment'" << axisAlignment << "'." );
141 unsigned drawMode = osgText::Text::TEXT;
142 if( (p = configNode->getNode( "draw-text" )) != NULL && p->getBoolValue() == false )
143 drawMode &= ~ osgText::Text::TEXT;
145 if( (p = configNode->getNode( "draw-alignment" )) != NULL && p->getBoolValue() == true )
146 drawMode |= osgText::Text::ALIGNMENT;
148 if( (p = configNode->getNode( "draw-boundingbox" )) != NULL && p->getBoolValue() == true )
149 drawMode |= osgText::Text::BOUNDINGBOX;
151 text->setDrawMode( drawMode );
153 if( (p = configNode->getNode( "alignment" )) != NULL ) {
154 string alignment = p->getStringValue();
155 if( alignment.compare( "left-top" ) == 0 ) {
156 text->setAlignment( osgText::Text::LEFT_TOP );
157 } else if( alignment.compare( "left-center" ) == 0 ) {
158 text->setAlignment( osgText::Text::LEFT_CENTER );
159 } else if( alignment.compare( "left-bottom" ) == 0 ) {
160 text->setAlignment( osgText::Text::LEFT_BOTTOM );
161 } else if( alignment.compare( "center-top" ) == 0 ) {
162 text->setAlignment( osgText::Text::CENTER_TOP );
163 } else if( alignment.compare( "center-center" ) == 0 ) {
164 text->setAlignment( osgText::Text::CENTER_CENTER );
165 } else if( alignment.compare( "center-bottom" ) == 0 ) {
166 text->setAlignment( osgText::Text::CENTER_BOTTOM );
167 } else if( alignment.compare( "right-top" ) == 0 ) {
168 text->setAlignment( osgText::Text::RIGHT_TOP );
169 } else if( alignment.compare( "right-center" ) == 0 ) {
170 text->setAlignment( osgText::Text::RIGHT_CENTER );
171 } else if( alignment.compare( "right-bottom" ) == 0 ) {
172 text->setAlignment( osgText::Text::RIGHT_BOTTOM );
173 } else if( alignment.compare( "left-baseline" ) == 0 ) {
174 text->setAlignment( osgText::Text::LEFT_BASE_LINE );
175 } else if( alignment.compare( "center-baseline" ) == 0 ) {
176 text->setAlignment( osgText::Text::CENTER_BASE_LINE );
177 } else if( alignment.compare( "right-baseline" ) == 0 ) {
178 text->setAlignment( osgText::Text::RIGHT_BASE_LINE );
179 } else if( alignment.compare( "baseline" ) == 0 ) {
180 text->setAlignment( osgText::Text::BASE_LINE );
182 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown text-alignment '" << alignment <<"'." );
186 if( (p = configNode->getNode( "layout" )) != NULL ) {
187 string layout = p->getStringValue();
188 if( layout.compare( "left-to-right" ) == 0 ) {
189 text->setLayout( osgText::Text::LEFT_TO_RIGHT );
190 } else if( layout.compare( "right-to-left" ) == 0 ) {
191 text->setLayout( osgText::Text::RIGHT_TO_LEFT );
192 } else if( layout.compare( "vertical" ) == 0 ) {
193 text->setLayout( osgText::Text::VERTICAL );
195 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown layout '" << layout <<"'." );
199 if( (p = configNode->getNode( "max-width" )) != NULL )
200 text->setMaximumWidth( p->getDoubleValue() );
202 if( (p = configNode->getNode( "max-height" )) != NULL )
203 text->setMaximumHeight( p->getDoubleValue() );
205 // FIXME: Should we support <chunks><chunk/><chunk/></chunks> here?
206 string type = configNode->getStringValue( "type", "literal" );
207 if( type == "literal" ) {
208 text->setText( configNode->getStringValue( "text", "" ) );
210 SGConstPropertyNode_ptr property = modelRoot->getNode( configNode->getStringValue( "property", "foo" ), true );
211 const char * format = configNode->getStringValue( "format", "" );
212 double scale = configNode->getDoubleValue( "scale", 1.0 );
213 double offset = configNode->getDoubleValue( "offset", 0.0 );
214 bool truncate = configNode->getBoolValue( "truncate", false );
216 if( (p = configNode->getNode( "property")) != NULL ) {
217 p = modelRoot->getNode( p->getStringValue(), true );
218 UpdateCallback * uc = new UpdateCallback( text, property, scale, offset, truncate, type == "number-value", format );
219 g->setUpdateCallback( uc );
223 osg::Node * reply = NULL;
224 if( (p = configNode->getNode( "offsets")) == NULL ) {
227 // Set up the alignment node ("stolen" from animation.cxx)
228 // XXX Order of rotations is probably not correct.
229 osg::MatrixTransform *align = new osg::MatrixTransform;
230 osg::Matrix res_matrix;
231 res_matrix.makeRotate(
232 p->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
234 p->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
236 p->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
240 tmat.makeTranslate(configNode->getFloatValue("offsets/x-m", 0.0),
241 configNode->getFloatValue("offsets/y-m", 0.0),
242 configNode->getFloatValue("offsets/z-m", 0.0));
244 align->setMatrix(res_matrix * tmat);
245 align->addChild( g );
249 if( (p = configNode->getNode( "name" )) != NULL )
250 reply->setName(p->getStringValue());
252 reply->setName("text");