]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGText.cxx
Printf format sanitising.
[simgear.git] / simgear / scene / model / SGText.cxx
1 // SGText.cxx - Manage text in the scene graph
2 // Copyright (C) 2009 Torsten Dreyer Torsten (_at_) t3r *dot* de
3 //
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.
8 //
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.
13 //
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.
17 //
18
19 #ifdef HAVE_CONFIG_H
20 #  include <simgear_config.h>
21 #endif
22
23 #include <cstdio>
24
25 #include "SGText.hxx"
26
27 #include <simgear/math/SGMath.hxx>
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/misc/strutils.hxx>
30
31 #include <osg/Geode>
32 #include <osg/MatrixTransform>
33 #include <osgText/Text>
34 #include <osgText/Font>
35
36 using std::string;
37
38 class SGText::UpdateCallback : public osg::NodeCallback {
39 public:
40   UpdateCallback( osgText::Text * aText, SGConstPropertyNode_ptr aProperty, double aScale, double aOffset, bool aTruncate, bool aNumeric, const char * aFormat ) :
41     text( aText ),
42     property( aProperty ),
43     scale( aScale ),
44     offset( aOffset ),
45     truncate( aTruncate ),
46     numeric( aNumeric ),
47     format( simgear::strutils::sanitizePrintfFormat( aFormat ) )
48   {
49     if( format.empty() ) {
50       if( numeric ) format = "%f";
51       else format = "%s";
52     }
53   }
54
55 private:
56   virtual void operator()(osg::Node * node, osg::NodeVisitor *nv );
57   osgText::Text * text;
58   SGConstPropertyNode_ptr property;
59   double scale;
60   double offset;
61   bool truncate;
62   bool numeric;
63   string format;
64 };
65
66 void SGText::UpdateCallback::operator()(osg::Node * node, osg::NodeVisitor *nv ) 
67 {
68   // FIXME:
69   // hopefully the users never specifies bad formats here
70   // this should better be something more robust
71   char buf[256];
72   if( numeric ) {
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 );
76   } else {
77     snprintf( buf, sizeof(buf)-1, format.c_str(), property->getStringValue() );
78   }
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.
83     text->setText( buf );
84     text->update();
85   }
86   traverse( node, nv );
87 }
88
89 osg::Node * SGText::appendText(const SGPropertyNode* configNode, 
90   SGPropertyNode* modelRoot, const osgDB::Options* options)
91 {
92   SGConstPropertyNode_ptr p;
93
94   SG_LOG(SG_GENERAL, SG_DEBUG, "Creating a text object");
95
96   osgText::Text * text = new osgText::Text();
97   osg::Geode * g = new osg::Geode;
98   g->addDrawable( text );
99
100   SGPath path("Fonts" );
101   path.append( configNode->getStringValue( "font", "Helvetica" ));
102   text->setFont( path.str() );
103
104   text->setCharacterSize(configNode->getDoubleValue("character-size", 1.0 ), 
105                          configNode->getDoubleValue("character-aspect-ratio", 1.0 ));
106   
107   if( (p = configNode->getNode( "font-resolution" )) != NULL )
108     text->setFontResolution( p->getIntValue( "width", 32 ), p->getIntValue( "height", 32 ) );
109
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 );
118     } else {
119       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown kerning'" << kerning << "'." );
120     }
121   }
122
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 );
139     } else {
140       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown axis-alignment'" << axisAlignment << "'." );
141     }
142   }
143
144   unsigned drawMode = osgText::Text::TEXT;
145   if( (p = configNode->getNode( "draw-text" )) != NULL && p->getBoolValue() == false )
146     drawMode &= ~ osgText::Text::TEXT;
147
148   if( (p = configNode->getNode( "draw-alignment" )) != NULL && p->getBoolValue() == true )
149     drawMode |= osgText::Text::ALIGNMENT;
150
151   if( (p = configNode->getNode( "draw-boundingbox" )) != NULL && p->getBoolValue() == true )
152     drawMode |= osgText::Text::BOUNDINGBOX;
153
154   text->setDrawMode( drawMode );
155
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 );
184     } else {
185       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown text-alignment '" << alignment <<"'." );
186     }
187   }
188
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 );
197     } else {
198       SG_LOG(SG_GENERAL, SG_ALERT, "ignoring unknown layout '" << layout <<"'." );
199     }
200   }
201   
202   if( (p = configNode->getNode( "max-width" )) != NULL )
203     text->setMaximumWidth( p->getDoubleValue() );
204
205   if( (p = configNode->getNode( "max-height" )) != NULL )
206     text->setMaximumHeight( p->getDoubleValue() );
207
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", "" ) );
212   } else {
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 );
218
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 );
223     }
224   }
225
226   osg::Node * reply = NULL;
227   if( (p = configNode->getNode( "offsets")) == NULL ) {
228     reply = g;
229   } else {
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,
236         osg::Vec3(0, 1, 0),
237         p->getFloatValue("roll-deg", 0.0)*SG_DEGREES_TO_RADIANS,
238         osg::Vec3(1, 0, 0),
239         p->getFloatValue("heading-deg", 0.0)*SG_DEGREES_TO_RADIANS,
240         osg::Vec3(0, 0, 1));
241
242     osg::Matrix tmat;
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));
246
247     align->setMatrix(res_matrix * tmat);
248     align->addChild( g );
249     reply = align;
250   }
251
252   if( (p = configNode->getNode( "name" )) != NULL )
253     reply->setName(p->getStringValue());
254   else
255     reply->setName("text");
256   return reply;
257 }