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>
27 #include <simgear/math/SGMath.hxx>
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/misc/strutils.hxx>
32 #include <osg/MatrixTransform>
33 #include <osgText/Text>
34 #include <osgText/Font>
38 class SGText::UpdateCallback : public osg::NodeCallback {
40 UpdateCallback( osgText::Text * aText, SGConstPropertyNode_ptr aProperty, double aScale, double aOffset, bool aTruncate, bool aNumeric, const char * aFormat ) :
42 property( aProperty ),
45 truncate( aTruncate ),
47 format( simgear::strutils::sanitizePrintfFormat( aFormat ) )
49 if( format.empty() ) {
50 if( numeric ) format = "%f";
56 virtual void operator()(osg::Node * node, osg::NodeVisitor *nv );
58 SGConstPropertyNode_ptr property;
66 void SGText::UpdateCallback::operator()(osg::Node * node, osg::NodeVisitor *nv )
69 // hopefully the users never specifies bad formats here
70 // this should better be something more robust
73 double d = property->getDoubleValue() * scale + offset;
74 if (truncate) d = (d < 0) ? -floor(-d) : floor(d);
75 snprintf( buf, sizeof(buf)-1, format.c_str(), d );
77 snprintf( buf, sizeof(buf)-1, format.c_str(), property->getStringValue() );
79 if( text->getText().createUTF8EncodedString().compare( buf ) ) {
80 // be lazy and set the text only if the property has changed.
81 // update() computes the glyph representation which looks
82 // more expensive than a the above string compare.
89 osg::Node * SGText::appendText(const SGPropertyNode* configNode,
90 SGPropertyNode* modelRoot, const osgDB::Options* options)
92 SGConstPropertyNode_ptr p;
94 SG_LOG(SG_GENERAL, SG_DEBUG, "Creating a text object");
96 osgText::Text * text = new osgText::Text();
97 osg::Geode * g = new osg::Geode;
98 g->addDrawable( text );
100 SGPath path("Fonts" );
101 path.append( configNode->getStringValue( "font", "Helvetica" ));
102 text->setFont( path.str() );
104 text->setCharacterSize(configNode->getDoubleValue("character-size", 1.0 ),
105 configNode->getDoubleValue("character-aspect-ratio", 1.0 ));
107 if( (p = configNode->getNode( "font-resolution" )) != NULL )
108 text->setFontResolution( p->getIntValue( "width", 32 ), p->getIntValue( "height", 32 ) );
110 if( (p = configNode->getNode( "kerning" )) != NULL ) {
111 string kerning = p->getStringValue();
112 if( kerning.compare( "default" ) == 0 ) {
113 text->setKerningType( osgText::KERNING_DEFAULT );
114 } else if( kerning.compare( "unfitted" ) == 0 ) {
115 text->setKerningType( osgText::KERNING_UNFITTED );
116 } else if( kerning.compare( "none" ) == 0 ) {
117 text->setKerningType( osgText::KERNING_NONE );
119 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown kerning'" << kerning << "'." );
123 if( ( p = configNode->getNode( "axis-alignment" )) != NULL ) {
124 string axisAlignment = p->getStringValue();
125 if( axisAlignment.compare( "xy-plane" ) == 0 ) {
126 text->setAxisAlignment( osgText::Text::XY_PLANE );
127 } else if( axisAlignment.compare( "reversed-xy-plane" ) == 0 ) {
128 text->setAxisAlignment( osgText::Text::REVERSED_XY_PLANE );
129 } else if( axisAlignment.compare( "xz-plane" ) == 0 ) {
130 text->setAxisAlignment( osgText::Text::XZ_PLANE );
131 } else if( axisAlignment.compare( "reversed-xz-plane" ) == 0 ) {
132 text->setAxisAlignment( osgText::Text::REVERSED_XZ_PLANE );
133 } else if( axisAlignment.compare( "yz-plane" ) == 0 ) {
134 text->setAxisAlignment( osgText::Text::YZ_PLANE );
135 } else if( axisAlignment.compare( "reversed-yz-plane" ) == 0 ) {
136 text->setAxisAlignment( osgText::Text::REVERSED_YZ_PLANE );
137 } else if( axisAlignment.compare( "screen" ) == 0 ) {
138 text->setAxisAlignment( osgText::Text::SCREEN );
140 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown axis-alignment'" << axisAlignment << "'." );
144 unsigned drawMode = osgText::Text::TEXT;
145 if( (p = configNode->getNode( "draw-text" )) != NULL && p->getBoolValue() == false )
146 drawMode &= ~ osgText::Text::TEXT;
148 if( (p = configNode->getNode( "draw-alignment" )) != NULL && p->getBoolValue() == true )
149 drawMode |= osgText::Text::ALIGNMENT;
151 if( (p = configNode->getNode( "draw-boundingbox" )) != NULL && p->getBoolValue() == true )
152 drawMode |= osgText::Text::BOUNDINGBOX;
154 text->setDrawMode( drawMode );
156 if( (p = configNode->getNode( "alignment" )) != NULL ) {
157 string alignment = p->getStringValue();
158 if( alignment.compare( "left-top" ) == 0 ) {
159 text->setAlignment( osgText::Text::LEFT_TOP );
160 } else if( alignment.compare( "left-center" ) == 0 ) {
161 text->setAlignment( osgText::Text::LEFT_CENTER );
162 } else if( alignment.compare( "left-bottom" ) == 0 ) {
163 text->setAlignment( osgText::Text::LEFT_BOTTOM );
164 } else if( alignment.compare( "center-top" ) == 0 ) {
165 text->setAlignment( osgText::Text::CENTER_TOP );
166 } else if( alignment.compare( "center-center" ) == 0 ) {
167 text->setAlignment( osgText::Text::CENTER_CENTER );
168 } else if( alignment.compare( "center-bottom" ) == 0 ) {
169 text->setAlignment( osgText::Text::CENTER_BOTTOM );
170 } else if( alignment.compare( "right-top" ) == 0 ) {
171 text->setAlignment( osgText::Text::RIGHT_TOP );
172 } else if( alignment.compare( "right-center" ) == 0 ) {
173 text->setAlignment( osgText::Text::RIGHT_CENTER );
174 } else if( alignment.compare( "right-bottom" ) == 0 ) {
175 text->setAlignment( osgText::Text::RIGHT_BOTTOM );
176 } else if( alignment.compare( "left-baseline" ) == 0 ) {
177 text->setAlignment( osgText::Text::LEFT_BASE_LINE );
178 } else if( alignment.compare( "center-baseline" ) == 0 ) {
179 text->setAlignment( osgText::Text::CENTER_BASE_LINE );
180 } else if( alignment.compare( "right-baseline" ) == 0 ) {
181 text->setAlignment( osgText::Text::RIGHT_BASE_LINE );
182 } else if( alignment.compare( "baseline" ) == 0 ) {
183 text->setAlignment( osgText::Text::BASE_LINE );
185 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown text-alignment '" << alignment <<"'." );
189 if( (p = configNode->getNode( "layout" )) != NULL ) {
190 string layout = p->getStringValue();
191 if( layout.compare( "left-to-right" ) == 0 ) {
192 text->setLayout( osgText::Text::LEFT_TO_RIGHT );
193 } else if( layout.compare( "right-to-left" ) == 0 ) {
194 text->setLayout( osgText::Text::RIGHT_TO_LEFT );
195 } else if( layout.compare( "vertical" ) == 0 ) {
196 text->setLayout( osgText::Text::VERTICAL );
198 SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown layout '" << layout <<"'." );
202 if( (p = configNode->getNode( "max-width" )) != NULL )
203 text->setMaximumWidth( p->getDoubleValue() );
205 if( (p = configNode->getNode( "max-height" )) != NULL )
206 text->setMaximumHeight( p->getDoubleValue() );
208 // FIXME: Should we support <chunks><chunk/><chunk/></chunks> here?
209 string type = configNode->getStringValue( "type", "literal" );
210 if( type == "literal" ) {
211 text->setText( configNode->getStringValue( "text", "" ) );
213 SGConstPropertyNode_ptr property = modelRoot->getNode( configNode->getStringValue( "property", "foo" ), true );
214 const char * format = configNode->getStringValue( "format", "" );
215 double scale = configNode->getDoubleValue( "scale", 1.0 );
216 double offset = configNode->getDoubleValue( "offset", 0.0 );
217 bool truncate = configNode->getBoolValue( "truncate", false );
219 if( (p = configNode->getNode( "property")) != NULL ) {
220 p = modelRoot->getNode( p->getStringValue(), true );
221 UpdateCallback * uc = new UpdateCallback( text, property, scale, offset, truncate, type == "number-value", format );
222 g->setUpdateCallback( uc );
226 osg::Node * reply = NULL;
227 if( (p = configNode->getNode( "offsets")) == NULL ) {
230 // Set up the alignment node ("stolen" from animation.cxx)
231 // XXX Order of rotations is probably not correct.
232 osg::MatrixTransform *align = new osg::MatrixTransform;
233 osg::Matrix res_matrix;
234 res_matrix.makeRotate(
235 p->getFloatValue("pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
237 p->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
239 p->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
243 tmat.makeTranslate(configNode->getFloatValue("offsets/x-m", 0.0),
244 configNode->getFloatValue("offsets/y-m", 0.0),
245 configNode->getFloatValue("offsets/z-m", 0.0));
247 align->setMatrix(res_matrix * tmat);
248 align->addChild( g );
252 if( (p = configNode->getNode( "name" )) != NULL )
253 reply->setName(p->getStringValue());
255 reply->setName("text");