1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14
#ifndef OSGTEXT_TEXTBASE
15
#define OSGTEXT_TEXTBASE 1
17
#include <osg/Drawable>
19
#include <osgText/String>
20
#include <osgText/KerningType>
25
class OSGTEXT_EXPORT TextBase : public osg::Drawable
30
TextBase(const TextBase& text,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
32
//virtual osg::Object* cloneType() const { return new Text(); }
33
//virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Text(*this,copyop); }
34
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const TextBase*>(obj)!=NULL; }
35
virtual const char* className() const { return "TextBase"; }
36
virtual const char* libraryName() const { return "osgText"; }
39
/** Set the Font reference width and height resolution in texels.
40
* Note, the size may not be supported by current font,
41
* the closest supported font size will be selected.*/
42
void setFontResolution(unsigned int width, unsigned int height);
44
unsigned int getFontWidth() const { return _fontSize.first; }
45
unsigned int getFontHeight() const { return _fontSize.second; }
48
/** Set the text using a osgText::String.*/
49
void setText(const String& text);
51
/** Set the text using a std::string,
52
* which is converted to an internal TextString.*/
53
void setText(const std::string& text);
55
/** Set the text using a Unicode encoded std::string, which is converted to an internal TextString.
56
* The encoding parameter specificies which Unicode encodeding is used in the std::string. */
57
void setText(const std::string& text,String::Encoding encoding);
59
/** Set the text using a wchar_t string,
60
* which is converted to an internal TextString.*/
61
void setText(const wchar_t* text);
63
/** Get the text string.
64
* Note, if you modify the string you must call Text::update() for
65
* the internal glyph reprentation to be updated.*/
66
String& getText() { return _text; }
68
/** Get the const text string.*/
69
const String& getText() const { return _text; }
71
/** update internal glyph respresentation used for rendering,
72
* and bounding volume.*/
73
void update() { computeGlyphRepresentation(); }
76
/** Set the rendered character size in object coordinates.*/
77
void setCharacterSize(float height,float aspectRatio=1.0f);
79
float getCharacterHeight() const { return _characterHeight; }
80
float getCharacterAspectRatio() const { return _characterAspectRatio; }
82
enum CharacterSizeMode
84
OBJECT_COORDS, /// default
85
SCREEN_COORDS, /// internally scale the characters to be constant screen size.
86
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.
89
/** Set how the CharacterSize value relates to the final rendered character.*/
90
void setCharacterSizeMode(CharacterSizeMode mode) { _characterSizeMode = mode; }
92
/** Get the CharacterSizeMode.*/
93
CharacterSizeMode getCharacterSizeMode() const { return _characterSizeMode; }
96
/** Set the maximum width of the text box.
97
* With horizontal layouts any characters which do not fit are wrapped around.
98
* 0 or negative values indicate that no maximum width is set, lines can be as long as
99
* they need be to fit thre required text*/
100
void setMaximumWidth(float maximumWidth);
102
/** Get the maximim width of the text box.*/
103
float getMaximumWidth() const { return _maximumWidth; }
105
/** Set the maximum height of the text box.
106
* With horizontal layouts any characters which do not fit are wrapped around.
107
* 0 or negative values indicate that no maximum height is set, lines can be as long as
108
* they need be to fit the required text*/
109
void setMaximumHeight(float maximumHeight);
111
/** Get the maximum height of the text box.*/
112
float getMaximumHeight() const { return _maximumHeight; }
114
/** Set the line spacing of the text box, given as a percentage of
115
* the character height. The default value is 0 for backward
116
* compatibility. For longer paragraphs of text, a value of at
117
* least 25% (i.e. set line spacing to 0.25) is recommended. */
118
void setLineSpacing(float lineSpacing);
120
/** Get the line spacing of the text box. */
121
float getLineSpacing() const { return _lineSpacing; }
125
/** Set the position of text.*/
126
void setPosition(const osg::Vec3& pos);
128
/** Get the position of text.*/
129
const osg::Vec3& getPosition() const { return _position; }
150
LEFT_BOTTOM_BASE_LINE,
151
CENTER_BOTTOM_BASE_LINE,
152
RIGHT_BOTTOM_BASE_LINE,
154
BASE_LINE = LEFT_BASE_LINE /// default.
158
void setAlignment(AlignmentType alignment);
159
AlignmentType getAlignment() const { return _alignment; }
171
USER_DEFINED_ROTATION
174
void setAxisAlignment(AxisAlignment axis);
175
AxisAlignment getAxisAlignment() const { return _axisAlignment; }
177
void setRotation(const osg::Quat& quat);
178
const osg::Quat& getRotation() const { return _rotation; }
180
void setAutoRotateToScreen(bool autoRotateToScreen);
181
bool getAutoRotateToScreen() const { return _autoRotateToScreen; }
185
LEFT_TO_RIGHT, /// default
190
void setLayout(Layout layout);
192
Layout getLayout() const { return _layout; }
197
TEXT = 1, /// default
202
void setDrawMode(unsigned int mode);
204
unsigned int getDrawMode() const { return _drawMode; }
207
void setKerningType(KerningType kerningType) { _kerningType = kerningType; }
209
KerningType getKerningType() const { return _kerningType; }
211
/** Get the number of wrapped lines - only valid after computeGlyphRepresentation() has been called, returns 0 otherwise */
212
unsigned int getLineCount() const { return _lineCount; }
214
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
215
virtual void setThreadSafeRefUnref(bool threadSafe);
217
/** Resize any per context GLObject buffers to specified size. */
218
virtual void resizeGLObjectBuffers(unsigned int maxSize);
220
/** If State is non-zero, this function releases OpenGL objects for
221
* the specified graphics context. Otherwise, releases OpenGL objexts
222
* for all graphics contexts. */
223
virtual void releaseGLObjects(osg::State* state=0) const;
226
virtual osg::BoundingBox computeBound() const;
232
void positionCursor(const osg::Vec2 & endOfLine_coords, osg::Vec2 & cursor, unsigned int linelength);
233
void computePositions();
234
String::iterator computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last);
237
virtual void computePositions(unsigned int contextID) const = 0;
238
virtual void computeGlyphRepresentation() = 0;
241
// members which have public access.
242
FontResolution _fontSize;
243
float _characterHeight;
244
float _characterAspectRatio;
245
CharacterSizeMode _characterSizeMode;
247
float _maximumHeight;
252
AlignmentType _alignment;
253
AxisAlignment _axisAlignment;
255
bool _autoRotateToScreen;
257
unsigned int _drawMode;
258
KerningType _kerningType;
259
unsigned int _lineCount;
263
// internal caches of the positioning of the text.
265
struct AutoTransformCache
267
AutoTransformCache():
268
_traversalNumber(-1),
272
int _traversalNumber;
275
osg::Vec3 _transformedPosition;
276
osg::Matrix _modelview;
277
osg::Matrix _projection;
281
mutable osg::buffered_object<AutoTransformCache> _autoTransformCache;
282
mutable osg::Vec3 _offset;
283
mutable osg::Vec3 _normal;
284
mutable osg::BoundingBox _textBB;