1 // A text on the Canvas
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "CanvasText.hxx"
20 #include <simgear/canvas/Canvas.hxx>
21 #include <simgear/canvas/CanvasSystemAdapter.hxx>
22 #include <simgear/misc/parse_color.hxx>
23 #include <osgText/Text>
34 TextOSG(canvas::Text* text);
36 void setCharacterAspect(float aspect);
37 void setFill(const std::string& fill);
38 void setBackgroundColor(const std::string& fill);
40 osg::Vec2 handleHit(float x, float y);
42 virtual osg::BoundingBox computeBound() const;
46 canvas::Text *_text_element;
49 //----------------------------------------------------------------------------
50 Text::TextOSG::TextOSG(canvas::Text* text):
56 //----------------------------------------------------------------------------
57 void Text::TextOSG::setCharacterAspect(float aspect)
59 setCharacterSize(getCharacterHeight(), aspect);
62 //----------------------------------------------------------------------------
63 void Text::TextOSG::setFill(const std::string& fill)
65 // if( fill == "none" )
69 if( parseColor(fill, color) )
73 //----------------------------------------------------------------------------
74 void Text::TextOSG::setBackgroundColor(const std::string& fill)
77 if( parseColor(fill, color) )
78 setBoundingBoxColor( color );
81 //----------------------------------------------------------------------------
82 osg::Vec2 Text::TextOSG::handleHit(float x, float y)
84 float line_height = _characterHeight + _lineSpacing;
86 // TODO check with align other than TOP
87 float first_line_y = -0.5 * _lineSpacing;//_offset.y() - _characterHeight;
88 size_t line = std::max<int>(0, (y - first_line_y) / line_height);
90 if( _textureGlyphQuadMap.empty() )
91 return osg::Vec2(-1, -1);
93 // TODO check when it can be larger
94 assert( _textureGlyphQuadMap.size() == 1 );
96 const GlyphQuads& glyphquad = _textureGlyphQuadMap.begin()->second;
97 const GlyphQuads::Glyphs& glyphs = glyphquad._glyphs;
98 const GlyphQuads::Coords2& coords = glyphquad._coords;
99 const GlyphQuads::LineNumbers& line_numbers = glyphquad._lineNumbers;
101 const float HIT_FRACTION = 0.6;
102 const float character_width = getCharacterHeight()
103 * getCharacterAspectRatio();
105 y = (line + 0.5) * line_height;
107 bool line_found = false;
108 for(size_t i = 0; i < line_numbers.size(); ++i)
110 if( line_numbers[i] != line )
114 if( line_numbers[i] < line )
115 // Wait for the correct line...
118 // We have already passed the correct line -> It's empty...
119 return osg::Vec2(0, y);
122 // Next line and not returned -> not before any character
123 // -> return position after last character of line
124 return osg::Vec2(coords[(i - 1) * 4 + 2].x(), y);
129 // Get threshold for mouse x position for setting cursor before or after
131 float threshold = coords[i * 4].x()
132 + HIT_FRACTION * glyphs[i]->getHorizontalAdvance()
137 if( i == 0 || line_numbers[i - 1] != line )
138 // first character of line
139 x = coords[i * 4].x();
140 else if( coords[(i - 1) * 4].x() == coords[(i - 1) * 4 + 2].x() )
141 // If previous character width is zero set to begin of next character
142 // (Happens eg. with spaces)
143 x = coords[i * 4].x();
145 // position at center between characters
146 x = 0.5 * (coords[(i - 1) * 4 + 2].x() + coords[i * 4].x());
148 return osg::Vec2(x, y);
152 // Nothing found -> return position after last character
156 (_lineCount - 0.5) * line_height
160 //----------------------------------------------------------------------------
161 osg::BoundingBox Text::TextOSG::computeBound() const
163 osg::BoundingBox bb = osgText::Text::computeBound();
167 // TODO bounding box still doesn't seem always right (eg. with center
168 // horizontal alignment not completely accurate)
169 bb._min.y() += _offset.y();
170 bb._max.y() += _offset.y();
172 _text_element->setBoundingBox(bb);
177 //----------------------------------------------------------------------------
178 Text::Text( const CanvasWeakPtr& canvas,
179 SGPropertyNode_ptr node,
180 const Style& parent_style ):
181 Element(canvas, node, parent_style),
182 _text( new Text::TextOSG(this) )
185 _text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS);
186 _text->setAxisAlignment(osgText::Text::USER_DEFINED_ROTATION);
187 _text->setRotation(osg::Quat(osg::PI, osg::X_AXIS));
189 addStyle("fill", &TextOSG::setFill, _text);
190 addStyle("background", &TextOSG::setBackgroundColor, _text);
191 addStyle("character-size",
192 static_cast<void (TextOSG::*)(float)>(&TextOSG::setCharacterSize),
194 addStyle("character-aspect-ratio", &TextOSG::setCharacterAspect, _text);
195 addStyle("padding", &TextOSG::setBoundingBoxMargin, _text);
198 // FILLEDBOUNDINGBOX = 4
200 addStyle<int>("draw-mode", &TextOSG::setDrawMode, _text);
201 addStyle("max-width", &TextOSG::setMaximumWidth, _text);
202 addStyle("font", &Text::setFont, this);
203 addStyle("alignment", &Text::setAlignment, this);
204 addStyle("text", &Text::setText, this);
209 //----------------------------------------------------------------------------
215 //----------------------------------------------------------------------------
216 void Text::setText(const char* text)
218 _text->setText(text, osgText::String::ENCODING_UTF8);
221 //----------------------------------------------------------------------------
222 void Text::setFont(const char* name)
224 _text->setFont( _canvas.lock()->getSystemAdapter()->getFont(name) );
227 //----------------------------------------------------------------------------
228 void Text::setAlignment(const char* align)
230 const std::string align_string(align);
232 #define ENUM_MAPPING(enum_val, string_val) \
233 else if( align_string == string_val )\
234 _text->setAlignment( osgText::Text::enum_val );
235 #include "text-alignment.hxx"
239 if( !align_string.empty() )
244 "canvas::Text: unknown alignment '" << align_string << "'"
246 _text->setAlignment(osgText::Text::LEFT_BASE_LINE);
250 //----------------------------------------------------------------------------
252 const char* Text::getAlignment() const
254 switch( _text->getAlignment() )
256 #define ENUM_MAPPING(enum_val, string_val) \
257 case osgText::Text::enum_val:\
259 #include "text-alignment.hxx"
267 //----------------------------------------------------------------------------
268 void Text::childChanged(SGPropertyNode* child)
270 if( child->getParent() != _node )
273 const std::string& name = child->getNameString();
274 if( name == "hit-y" )
277 _node->getFloatValue("hit-x"),
278 _node->getFloatValue("hit-y")
282 //----------------------------------------------------------------------------
283 void Text::handleHit(float x, float y)
285 const osg::Vec2& pos = _text->handleHit(x, y);
286 _node->setFloatValue("cursor-x", pos.x());
287 _node->setFloatValue("cursor-y", pos.y());
290 } // namespace canvas
291 } // namespace simgear