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/scene/util/parse_color.hxx>
23 #include <simgear/structure/OSGVersion.hxx>
24 #include <osgText/Text>
35 TextOSG(canvas::Text* text);
37 void setCharacterAspect(float aspect);
38 void setFill(const std::string& fill);
39 void setBackgroundColor(const std::string& fill);
41 osg::Vec2 handleHit(const osg::Vec2f& pos);
43 virtual osg::BoundingBox computeBound() const;
47 canvas::Text *_text_element;
49 virtual void computePositions(unsigned int contextID) const;
52 //----------------------------------------------------------------------------
53 Text::TextOSG::TextOSG(canvas::Text* text):
59 //----------------------------------------------------------------------------
60 void Text::TextOSG::setCharacterAspect(float aspect)
62 setCharacterSize(getCharacterHeight(), aspect);
65 //----------------------------------------------------------------------------
66 void Text::TextOSG::setFill(const std::string& fill)
68 // if( fill == "none" )
72 if( parseColor(fill, color) )
76 //----------------------------------------------------------------------------
77 void Text::TextOSG::setBackgroundColor(const std::string& fill)
80 if( parseColor(fill, color) )
81 setBoundingBoxColor( color );
84 //----------------------------------------------------------------------------
85 osg::Vec2 Text::TextOSG::handleHit(const osg::Vec2f& pos)
87 float line_height = _characterHeight + _lineSpacing;
89 // TODO check with align other than TOP
90 float first_line_y = -0.5 * _lineSpacing;//_offset.y() - _characterHeight;
91 size_t line = std::max<int>(0, (pos.y() - first_line_y) / line_height);
93 if( _textureGlyphQuadMap.empty() )
94 return osg::Vec2(-1, -1);
96 // TODO check when it can be larger
97 assert( _textureGlyphQuadMap.size() == 1 );
99 const GlyphQuads& glyphquad = _textureGlyphQuadMap.begin()->second;
100 const GlyphQuads::Glyphs& glyphs = glyphquad._glyphs;
101 const GlyphQuads::Coords2& coords = glyphquad._coords;
102 const GlyphQuads::LineNumbers& line_numbers = glyphquad._lineNumbers;
104 const float HIT_FRACTION = 0.6;
105 const float character_width = getCharacterHeight()
106 * getCharacterAspectRatio();
108 float y = (line + 0.5) * line_height;
110 bool line_found = false;
111 for(size_t i = 0; i < line_numbers.size(); ++i)
113 if( line_numbers[i] != line )
117 if( line_numbers[i] < line )
118 // Wait for the correct line...
121 // We have already passed the correct line -> It's empty...
122 return osg::Vec2(0, y);
125 // Next line and not returned -> not before any character
126 // -> return position after last character of line
127 return osg::Vec2(coords[(i - 1) * 4 + 2].x(), y);
132 // Get threshold for mouse x position for setting cursor before or after
134 float threshold = coords[i * 4].x()
135 + HIT_FRACTION * glyphs[i]->getHorizontalAdvance()
138 if( pos.x() <= threshold )
141 if( i == 0 || line_numbers[i - 1] != line )
142 // first character of line
143 hit.x() = coords[i * 4].x();
144 else if( coords[(i - 1) * 4].x() == coords[(i - 1) * 4 + 2].x() )
145 // If previous character width is zero set to begin of next character
146 // (Happens eg. with spaces)
147 hit.x() = coords[i * 4].x();
149 // position at center between characters
150 hit.x() = 0.5 * (coords[(i - 1) * 4 + 2].x() + coords[i * 4].x());
156 // Nothing found -> return position after last character
160 (_lineCount - 0.5) * line_height
164 //----------------------------------------------------------------------------
165 osg::BoundingBox Text::TextOSG::computeBound() const
167 osg::BoundingBox bb = osgText::Text::computeBound();
171 #if SG_OSG_VERSION_LESS_THAN(3,1,0)
172 // TODO bounding box still doesn't seem always right (eg. with center
173 // horizontal alignment not completely accurate)
174 bb._min.y() += _offset.y();
175 bb._max.y() += _offset.y();
178 _text_element->setBoundingBox(bb);
183 //----------------------------------------------------------------------------
184 void Text::TextOSG::computePositions(unsigned int contextID) const
186 if( _textureGlyphQuadMap.empty() || _layout == VERTICAL )
187 return osgText::Text::computePositions(contextID);
189 // TODO check when it can be larger
190 assert( _textureGlyphQuadMap.size() == 1 );
192 const GlyphQuads& quads = _textureGlyphQuadMap.begin()->second;
193 const GlyphQuads::Glyphs& glyphs = quads._glyphs;
194 const GlyphQuads::Coords2& coords = quads._coords;
195 const GlyphQuads::LineNumbers& line_numbers = quads._lineNumbers;
197 float wr = _characterHeight / getCharacterAspectRatio();
199 size_t cur_line = static_cast<size_t>(-1);
200 for(size_t i = 0; i < glyphs.size(); ++i)
202 // Check horizontal offsets
204 bool first_char = cur_line != line_numbers[i];
205 cur_line = line_numbers[i];
207 bool last_char = (i + 1 == glyphs.size())
208 || (cur_line != line_numbers[i + 1]);
210 if( first_char || last_char )
212 // From osg/src/osgText/Text.cpp:
214 // osg::Vec2 upLeft = local+osg::Vec2(0.0f-fHorizQuadMargin, ...);
215 // osg::Vec2 lowLeft = local+osg::Vec2(0.0f-fHorizQuadMargin, ...);
216 // osg::Vec2 lowRight = local+osg::Vec2(width+fHorizQuadMargin, ...);
217 // osg::Vec2 upRight = local+osg::Vec2(width+fHorizQuadMargin, ...);
219 float left = coords[i * 4].x(),
220 right = coords[i * 4 + 2].x(),
221 width = glyphs[i]->getWidth() * wr;
223 // (local + width + fHoriz) - (local - fHoriz) = width + 2*fHoriz | -width
224 float margin = 0.5f * (right - left - width),
225 cursor_x = left + margin
226 - glyphs[i]->getHorizontalBearing().x() * wr;
230 if( cur_line == 0 || cursor_x < _textBB._min.x() )
231 _textBB._min.x() = cursor_x;
236 float cursor_w = cursor_x + glyphs[i]->getHorizontalAdvance() * wr;
238 if( cur_line == 0 || cursor_w > _textBB._max.x() )
239 _textBB._max.x() = cursor_w;
244 return osgText::Text::computePositions(contextID);
247 //----------------------------------------------------------------------------
248 Text::Text( const CanvasWeakPtr& canvas,
249 const SGPropertyNode_ptr& node,
250 const Style& parent_style,
252 Element(canvas, node, parent_style, parent),
253 _text( new Text::TextOSG(this) )
256 _text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS);
257 _text->setAxisAlignment(osgText::Text::USER_DEFINED_ROTATION);
258 _text->setRotation(osg::Quat(osg::PI, osg::X_AXIS));
260 if( !isInit<Text>() )
262 osg::ref_ptr<TextOSG> Text::*text = &Text::_text;
264 addStyle("fill", "color", &TextOSG::setFill, text);
265 addStyle("background", "color", &TextOSG::setBackgroundColor, text);
266 addStyle("character-size",
269 void (TextOSG::*)(float)
270 > (&TextOSG::setCharacterSize),
272 addStyle("character-aspect-ratio",
274 &TextOSG::setCharacterAspect, text);
275 addStyle("padding", "numeric", &TextOSG::setBoundingBoxMargin, text);
278 // FILLEDBOUNDINGBOX = 4
280 addStyle<int>("draw-mode", "", &TextOSG::setDrawMode, text);
281 addStyle("max-width", "numeric", &TextOSG::setMaximumWidth, text);
282 addStyle("font", "", &Text::setFont);
283 addStyle("alignment", "", &Text::setAlignment);
284 addStyle("text", "", &Text::setText);
290 //----------------------------------------------------------------------------
296 //----------------------------------------------------------------------------
297 void Text::setText(const char* text)
299 _text->setText(text, osgText::String::ENCODING_UTF8);
302 //----------------------------------------------------------------------------
303 void Text::setFont(const char* name)
305 _text->setFont( _canvas.lock()->getSystemAdapter()->getFont(name) );
308 //----------------------------------------------------------------------------
309 void Text::setAlignment(const char* align)
311 const std::string align_string(align);
313 #define ENUM_MAPPING(enum_val, string_val) \
314 else if( align_string == string_val )\
315 _text->setAlignment( osgText::Text::enum_val );
316 #include "text-alignment.hxx"
320 if( !align_string.empty() )
325 "canvas::Text: unknown alignment '" << align_string << "'"
327 _text->setAlignment(osgText::Text::LEFT_BASE_LINE);
331 //----------------------------------------------------------------------------
333 const char* Text::getAlignment() const
335 switch( _text->getAlignment() )
337 #define ENUM_MAPPING(enum_val, string_val) \
338 case osgText::Text::enum_val:\
340 #include "text-alignment.hxx"
348 //----------------------------------------------------------------------------
349 osg::Vec2 Text::getNearestCursor(const osg::Vec2& pos) const
351 return _text->handleHit(pos);
354 } // namespace canvas
355 } // namespace simgear