openscenegraph
TextBase
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSGTEXT_TEXTBASE
15#define OSGTEXT_TEXTBASE 1
16
17#include <osg/Drawable>
18
19#include <osgText/String>
20#include <osgText/KerningType>
21#include <osgText/Font>
22
23namespace osgText {
24
25#define NEW_APPROACH
26
27class OSGTEXT_EXPORT TextBase : public osg::Drawable
28{
29public:
30
31 TextBase();
32 TextBase(const TextBase& text,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
33
34 //virtual osg::Object* cloneType() const { return new Text(); }
35 //virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Text(*this,copyop); }
36 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const TextBase*>(obj)!=NULL; }
37 virtual const char* className() const { return "TextBase"; }
38 virtual const char* libraryName() const { return "osgText"; }
39
40 void setColor(const osg::Vec4& color);
41 const osg::Vec4& getColor() const { return _color; }
42
43 /** Set the Font to use to render the text.
44 * setFont(0) sets the use of the default font.*/
45 virtual void setFont(Font* font=0) { setFont(osg::ref_ptr<Font>(font)); };
46
47 /** Set the Font to use to render the text.*/
48 virtual void setFont(osg::ref_ptr<Font> font);
49
50 /** Set the font, loaded from the specified front file, to use to render the text,
51 * setFont("") sets the use of the default font.
52 * See the osgText::readFontFile function for how the font file will be located. */
53 virtual void setFont(const std::string& fontfile);
54
55 /** Get the font. Return 0 if default is being used.*/
56 Font* getFont() { return _font.get(); }
57
58 /** Get the const font. Return 0 if default is being used.*/
59 const Font* getFont() const { return _font.get(); }
60
61
62 /** Set the text style.*/
63 void setStyle(Style* style) { _style = style; }
64 /** Get the text style.*/
65 Style* getStyle() { return _style.get(); }
66 /** Get the const text style.*/
67 const Style* getStyle() const { return _style.get(); }
68
69 /** Get or create the text style.*/
70 Style* getOrCreateStyle() { if (!_style) _style = new Style; return _style.get(); }
71
72 /** Set the Font reference width and height resolution in texels.
73 * Note, the size may not be supported by current font,
74 * the closest supported font size will be selected.*/
75 void setFontResolution(unsigned int width, unsigned int height);
76
77 unsigned int getFontWidth() const { return _fontSize.first; }
78 unsigned int getFontHeight() const { return _fontSize.second; }
79
80
81 /** Set the text using a osgText::String.*/
82 void setText(const String& text);
83
84 /** Set the text using a std::string,
85 * which is converted to an internal TextString.*/
86 void setText(const std::string& text);
87
88 /** Set the text using a Unicode encoded std::string, which is converted to an internal TextString.
89 * The encoding parameter specificies which Unicode encodeding is used in the std::string. */
90 void setText(const std::string& text,String::Encoding encoding);
91
92 /** Set the text using a wchar_t string,
93 * which is converted to an internal TextString.*/
94 void setText(const wchar_t* text);
95
96 /** Get the text string.
97 * Note, if you modify the string you must call Text::update() for
98 * the internal glyph reprentation to be updated.*/
99 String& getText() { return _text; }
100
101 /** Get the const text string.*/
102 const String& getText() const { return _text; }
103
104 /** update internal glyph respresentation used for rendering,
105 * and bounding volume.*/
106 void update() { computeGlyphRepresentation(); }
107
108
109 /** Set the rendered character size in object coordinates.*/
110 void setCharacterSize(float height);
111
112 /** Set the rendered character size in object coordinates.*/
113 void setCharacterSize(float height, float aspectRatio);
114
115 float getCharacterHeight() const { return _characterHeight; }
116 float getCharacterAspectRatio() const { return _style.valid()? _style->getWidthRatio() : 1.0f; }
117
118 enum CharacterSizeMode
119 {
120 OBJECT_COORDS, /// default
121 SCREEN_COORDS, /// internally scale the characters to be constant screen size.
122 OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT /// text that behavaves like OBJECT_COORDS sized text when a long distance way, but has its screen sized capped automatically when the viewer gets near.
123 };
124
125 /** Set how the CharacterSize value relates to the final rendered character.*/
126 void setCharacterSizeMode(CharacterSizeMode mode) { _characterSizeMode = mode; }
127
128 /** Get the CharacterSizeMode.*/
129 CharacterSizeMode getCharacterSizeMode() const { return _characterSizeMode; }
130
131
132 /** Set the maximum width of the text box.
133 * With horizontal layouts any characters which do not fit are wrapped around.
134 * 0 or negative values indicate that no maximum width is set, lines can be as long as
135 * they need be to fit thre required text*/
136 void setMaximumWidth(float maximumWidth);
137
138 /** Get the maximim width of the text box.*/
139 float getMaximumWidth() const { return _maximumWidth; }
140
141 /** Set the maximum height of the text box.
142 * With horizontal layouts any characters which do not fit are wrapped around.
143 * 0 or negative values indicate that no maximum height is set, lines can be as long as
144 * they need be to fit the required text*/
145 void setMaximumHeight(float maximumHeight);
146
147 /** Get the maximum height of the text box.*/
148 float getMaximumHeight() const { return _maximumHeight; }
149
150 /** Set the line spacing of the text box, given as a percentage of
151 * the character height. The default value is 0 for backward
152 * compatibility. For longer paragraphs of text, a value of at
153 * least 25% (i.e. set line spacing to 0.25) is recommended. */
154 void setLineSpacing(float lineSpacing);
155
156 /** Get the line spacing of the text box. */
157 float getLineSpacing() const { return _lineSpacing; }
158
159
160
161 /** Set the position of text.*/
162 void setPosition(const osg::Vec3& pos);
163
164 /** Get the position of text.*/
165 const osg::Vec3& getPosition() const { return _position; }
166
167
168 enum AlignmentType
169 {
170 LEFT_TOP,
171 LEFT_CENTER,
172 LEFT_BOTTOM,
173
174 CENTER_TOP,
175 CENTER_CENTER,
176 CENTER_BOTTOM,
177
178 RIGHT_TOP,
179 RIGHT_CENTER,
180 RIGHT_BOTTOM,
181
182 LEFT_BASE_LINE,
183 CENTER_BASE_LINE,
184 RIGHT_BASE_LINE,
185
186 LEFT_BOTTOM_BASE_LINE,
187 CENTER_BOTTOM_BASE_LINE,
188 RIGHT_BOTTOM_BASE_LINE,
189
190 BASE_LINE = LEFT_BASE_LINE /// default.
191
192 };
193
194 void setAlignment(AlignmentType alignment);
195 AlignmentType getAlignment() const { return _alignment; }
196
197
198 enum AxisAlignment
199 {
200 XY_PLANE,
201 REVERSED_XY_PLANE,
202 XZ_PLANE,
203 REVERSED_XZ_PLANE,
204 YZ_PLANE,
205 REVERSED_YZ_PLANE,
206 SCREEN,
207 USER_DEFINED_ROTATION
208 };
209
210 void setAxisAlignment(AxisAlignment axis);
211 AxisAlignment getAxisAlignment() const { return _axisAlignment; }
212
213 void setRotation(const osg::Quat& quat);
214 const osg::Quat& getRotation() const { return _rotation; }
215
216 void setAutoRotateToScreen(bool autoRotateToScreen);
217 bool getAutoRotateToScreen() const { return _autoRotateToScreen; }
218
219 enum Layout
220 {
221 LEFT_TO_RIGHT, /// default
222 RIGHT_TO_LEFT,
223 VERTICAL
224 };
225
226 void setLayout(Layout layout);
227
228 Layout getLayout() const { return _layout; }
229
230
231 enum DrawModeMask
232 {
233 TEXT = 1, /// default
234 BOUNDINGBOX = 2,
235 FILLEDBOUNDINGBOX = 4,
236 ALIGNMENT = 8
237 };
238
239 void setDrawMode(unsigned int mode);
240
241 unsigned int getDrawMode() const { return _drawMode; }
242
243 void setBoundingBoxMargin(float margin);
244
245 float getBoundingBoxMargin() const { return _textBBMargin; }
246
247 void setBoundingBoxColor(const osg::Vec4& color){ _textBBColor = color; }
248
249 const osg::Vec4& getBoundingBoxColor() const { return _textBBColor; }
250
251
252 void setKerningType(KerningType kerningType) { _kerningType = kerningType; }
253
254 KerningType getKerningType() const { return _kerningType; }
255
256 /** Get the number of wrapped lines - only valid after computeGlyphRepresentation() has been called, returns 0 otherwise */
257 unsigned int getLineCount() const { return _lineCount; }
258
259 /** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
260 * @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
261 */
262 virtual void compileGLObjects(osg::RenderInfo& renderInfo) const;
263
264 /** Resize any per context GLObject buffers to specified size. */
265 virtual void resizeGLObjectBuffers(unsigned int maxSize);
266
267 /** If State is non-zero, this function releases OpenGL objects for
268 * the specified graphics context. Otherwise, releases OpenGL objexts
269 * for all graphics contexts. */
270 virtual void releaseGLObjects(osg::State* state=0) const;
271
272
273 virtual osg::BoundingBox computeBoundingBox() const;
274
275 typedef osg::ref_ptr<osg::Vec3Array> Coords;
276 Coords& getCoords() { return _coords; }
277 const Coords& getCoords() const { return _coords; }
278
279 void getCoord(unsigned int i, osg::Vec2& c) const { c.set((*_coords)[i].x(), (*_coords)[i].y()); }
280 void getCoord(unsigned int i, osg::Vec3& c) const { c = (*_coords)[i]; }
281
282 /** Get the cached internal matrix used to provide positioning of text. The cached matrix is originally computed by computeMatrix(..). */
283 const osg::Matrix& getMatrix() const { return _matrix; }
284
285 /** compute the matrix that positions the text in model space for the given viewpoint.*/
286 bool computeMatrix(osg::Matrix& matrix, osg::State* state=0) const;
287
288protected:
289
290 virtual ~TextBase();
291
292 virtual osg::StateSet* createStateSet();
293
294 virtual void assignStateSet();
295
296 void initArraysAndBuffers();
297
298 osg::VertexArrayState* createVertexArrayStateImplementation(osg::RenderInfo& renderInfo) const;
299
300 void positionCursor(const osg::Vec2 & endOfLine_coords, osg::Vec2 & cursor, unsigned int linelength);
301 String::iterator computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last);
302 void computePositions();
303 virtual void computePositionsImplementation();
304
305 virtual void computeGlyphRepresentation() = 0;
306
307 typedef osg::ref_ptr<osg::Vec2Array> TexCoords;
308 typedef osg::ref_ptr<osg::Vec4Array> ColorCoords;
309 typedef std::vector< osg::ref_ptr<osg::DrawElements> > Primitives;
310
311
312 // members which have public access.
313 osg::Vec4 _color;
314 osg::ref_ptr<Font> _font;
315 osg::ref_ptr<Font> _fontFallback;
316 osg::ref_ptr<Style> _style;
317 FontResolution _fontSize;
318 float _characterHeight;
319 CharacterSizeMode _characterSizeMode;
320 float _maximumWidth;
321 float _maximumHeight;
322 float _lineSpacing;
323
324 String _text;
325 osg::Vec3 _position;
326 AlignmentType _alignment;
327 AxisAlignment _axisAlignment;
328 osg::Quat _rotation;
329 bool _autoRotateToScreen;
330 Layout _layout;
331 unsigned int _drawMode;
332 float _textBBMargin;
333 osg::Vec4 _textBBColor;
334 KerningType _kerningType;
335 unsigned int _lineCount;
336 bool _glyphNormalized;
337
338 osg::Vec3 _offset;
339 osg::Vec3 _normal;
340 osg::BoundingBox _textBB;
341 osg::BoundingBox _textBBWithMargin;
342
343 mutable osg::Matrix _matrix;
344
345 Primitives _decorationPrimitives;
346
347 void setupDecoration();
348
349 osg::ref_ptr<osg::VertexBufferObject> _vbo;
350 osg::ref_ptr<osg::ElementBufferObject> _ebo;
351
352 Coords _coords;
353 Coords _normals;
354 ColorCoords _colorCoords;
355 TexCoords _texcoords;
356
357 unsigned int addCoord(const osg::Vec2& c) { unsigned int s = _coords->size(); _coords->push_back(osg::Vec3(c.x(), c.y(), 0.0f)); _coords->dirty(); return s; }
358 unsigned int addCoord(const osg::Vec3& c) { unsigned int s = _coords->size(); _coords->push_back(c); _coords->dirty(); return s; }
359
360
361 void addTexCoord(const osg::Vec2& tc) { _texcoords->push_back(tc); _texcoords->dirty(); }
362
363};
364
365}
366
367
368#endif
369