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>
33 class SGText::UpdateCallback : public osg::NodeCallback {
35 UpdateCallback( osgText::Text * aText, SGConstPropertyNode_ptr aProperty, double aScale, double aOffset, double aTruncate, double aNumeric, const char * aFormat ) :
37 property( aProperty ),
40 truncate( aTruncate ),
44 if( format.size() == 0 ) {
45 if( numeric ) format = "%f";
51 virtual void operator()(osg::Node * node, osg::NodeVisitor *nv );
53 SGConstPropertyNode_ptr property;
61 void SGText::UpdateCallback::operator()(osg::Node * node, osg::NodeVisitor *nv )
64 // hopefully the users never specifies bad formats here
65 // this should better be something more robust
68 double d = property->getDoubleValue() * scale + offset;
69 if (truncate) d = (d < 0) ? -floor(-d) : floor(d);
70 snprintf( buf, sizeof(buf)-1, format.c_str(), d );
72 snprintf( buf, sizeof(buf)-1, format.c_str(), property->getStringValue() );
74 if( text->getText().createUTF8EncodedString().compare( buf ) ) {
75 // be lazy and set the text only if the property has changed.
76 // update() computes the glyph representation which looks
77 // more expensive than a the above string compare.
84 osg::Node * SGText::appendText(const SGPropertyNode* configNode,
85 SGPropertyNode* modelRoot, const osgDB::ReaderWriter::Options* options)
87 SGConstPropertyNode_ptr p;
89 SG_LOG(SG_GENERAL, SG_DEBUG, "Creating a text object");
91 osgText::Text * text = new osgText::Text();
92 osg::Geode * g = new osg::Geode;
93 g->addDrawable( text );
95 SGPath path("Fonts" );
96 path.append( configNode->getStringValue( "font", "Helvetica" ));
97 text->setFont( path.str() );
99 text->setCharacterSize(configNode->getDoubleValue("character-size", 1.0 ),
100 configNode->getDoubleValue("character-aspect-ratio", 1.0 ));
102 if( (p = configNode->getNode( "font-resolution" )) != NULL )
103 text->setFontResolution( p->getIntValue( "width", 32 ), p->getIntValue( "height", 32 ) );
105 if( (p = configNode->getNode( "kerning" )) != NULL ) {
106 string kerning = p->getStringValue();
107 if( kerning.compare( "default" ) == 0 ) {
108 text->setKerningType( osgText::KERNING_DEFAULT );
109 } else if( kerning.compare( "unfitted" ) == 0 ) {
110 text->setKerningType( osgText::KERNING_UNFITTED );
111 } else if( kerning.compare( "none" ) == 0 ) {
112 text->setKerningType( osgText::KERNING_NONE );
114 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown kerning'" << kerning << "'." );
118 if( ( p = configNode->getNode( "axis-alignment" )) != NULL ) {
119 string axisAlignment = p->getStringValue();
120 if( axisAlignment.compare( "xy-plane" ) == 0 ) {
121 text->setAxisAlignment( osgText::Text::XY_PLANE );
122 } else if( axisAlignment.compare( "reversed-xy-plane" ) == 0 ) {
123 text->setAxisAlignment( osgText::Text::REVERSED_XY_PLANE );
124 } else if( axisAlignment.compare( "xz-plane" ) == 0 ) {
125 text->setAxisAlignment( osgText::Text::XZ_PLANE );
126 } else if( axisAlignment.compare( "reversed-xz-plane" ) == 0 ) {
127 text->setAxisAlignment( osgText::Text::REVERSED_XZ_PLANE );
128 } else if( axisAlignment.compare( "yz-plane" ) == 0 ) {
129 text->setAxisAlignment( osgText::Text::YZ_PLANE );
130 } else if( axisAlignment.compare( "reversed-yz-plane" ) == 0 ) {
131 text->setAxisAlignment( osgText::Text::REVERSED_YZ_PLANE );
132 } else if( axisAlignment.compare( "screen" ) == 0 ) {
133 text->setAxisAlignment( osgText::Text::SCREEN );
135 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown axis-alignment'" << axisAlignment << "'." );
139 unsigned drawMode = osgText::Text::TEXT;
140 if( (p = configNode->getNode( "draw-text" )) != NULL && p->getBoolValue() == false )
141 drawMode &= ~ osgText::Text::TEXT;
143 if( (p = configNode->getNode( "draw-alignment" )) != NULL && p->getBoolValue() == true )
144 drawMode |= osgText::Text::ALIGNMENT;
146 if( (p = configNode->getNode( "draw-boundingbox" )) != NULL && p->getBoolValue() == true )
147 drawMode |= osgText::Text::BOUNDINGBOX;
149 text->setDrawMode( drawMode );
151 if( (p = configNode->getNode( "alignment" )) != NULL ) {
152 string alignment = p->getStringValue();
153 if( alignment.compare( "left-top" ) == 0 ) {
154 text->setAlignment( osgText::Text::LEFT_TOP );
155 } else if( alignment.compare( "left-center" ) == 0 ) {
156 text->setAlignment( osgText::Text::LEFT_CENTER );
157 } else if( alignment.compare( "left-bottom" ) == 0 ) {
158 text->setAlignment( osgText::Text::LEFT_BOTTOM );
159 } else if( alignment.compare( "center-top" ) == 0 ) {
160 text->setAlignment( osgText::Text::CENTER_TOP );
161 } else if( alignment.compare( "center-center" ) == 0 ) {
162 text->setAlignment( osgText::Text::CENTER_CENTER );
163 } else if( alignment.compare( "center-bottom" ) == 0 ) {
164 text->setAlignment( osgText::Text::CENTER_BOTTOM );
165 } else if( alignment.compare( "right-top" ) == 0 ) {
166 text->setAlignment( osgText::Text::RIGHT_TOP );
167 } else if( alignment.compare( "right-center" ) == 0 ) {
168 text->setAlignment( osgText::Text::RIGHT_CENTER );
169 } else if( alignment.compare( "right-bottom" ) == 0 ) {
170 text->setAlignment( osgText::Text::RIGHT_BOTTOM );
171 } else if( alignment.compare( "left-baseline" ) == 0 ) {
172 text->setAlignment( osgText::Text::LEFT_BASE_LINE );
173 } else if( alignment.compare( "center-baseline" ) == 0 ) {
174 text->setAlignment( osgText::Text::CENTER_BASE_LINE );
175 } else if( alignment.compare( "right-baseline" ) == 0 ) {
176 text->setAlignment( osgText::Text::RIGHT_BASE_LINE );
177 } else if( alignment.compare( "baseline" ) == 0 ) {
178 text->setAlignment( osgText::Text::BASE_LINE );
180 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown text-alignment '" << alignment <<"'." );
184 if( (p = configNode->getNode( "layout" )) != NULL ) {
185 string layout = p->getStringValue();
186 if( layout.compare( "left-to-right" ) == 0 ) {
187 text->setLayout( osgText::Text::LEFT_TO_RIGHT );
188 } else if( layout.compare( "right-to-left" ) == 0 ) {
189 text->setLayout( osgText::Text::RIGHT_TO_LEFT );
190 } else if( layout.compare( "vertical" ) == 0 ) {
191 text->setLayout( osgText::Text::VERTICAL );
193 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown layout '" << layout <<"'." );
197 if( (p = configNode->getNode( "max-width" )) != NULL )
198 text->setMaximumWidth( p->getDoubleValue() );
200 if( (p = configNode->getNode( "max-height" )) != NULL )
201 text->setMaximumHeight( p->getDoubleValue() );
203 // FIXME: Should we support <chunks><chunk/><chunk/></chunks> here?
204 string type = configNode->getStringValue( "type", "literal" );
205 if( type == "literal" ) {
206 text->setText( configNode->getStringValue( "text", "" ) );
208 SGConstPropertyNode_ptr property = modelRoot->getNode( configNode->getStringValue( "property", "foo" ), true );
209 const char * format = configNode->getStringValue( "format", "" );
210 double scale = configNode->getDoubleValue( "scale", 1.0 );
211 double offset = configNode->getDoubleValue( "offset", 0.0 );
212 bool truncate = configNode->getBoolValue( "truncate", false );
214 if( (p = configNode->getNode( "property")) != NULL ) {
215 p = modelRoot->getNode( p->getStringValue(), true );
216 UpdateCallback * uc = new UpdateCallback( text, property, scale, offset, truncate, type == "number-value", format );
217 g->setUpdateCallback( uc );
221 osg::Node * reply = NULL;
222 if( (p = configNode->getNode( "offsets")) == NULL ) {
225 // Set up the alignment node ("stolen" from animation.cxx)
226 // XXX Order of rotations is probably not correct.
227 osg::MatrixTransform *align = new osg::MatrixTransform;
228 osg::Matrix res_matrix;
229 res_matrix.makeRotate(
230 p->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
232 p->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
234 p->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
238 tmat.makeTranslate(configNode->getFloatValue("offsets/x-m", 0.0),
239 configNode->getFloatValue("offsets/y-m", 0.0),
240 configNode->getFloatValue("offsets/z-m", 0.0));
242 align->setMatrix(res_matrix * tmat);
243 align->addChild( g );
247 if( (p = configNode->getNode( "name" )) != NULL )
248 reply->setName(p->getStringValue());
250 reply->setName("text");