| 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 | |
|---|
| 23 | namespace osgText { |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | class OSGTEXT_EXPORT TextBase : public osg::Drawable |
|---|
| 27 | { |
|---|
| 28 | public: |
|---|
| 29 | |
|---|
| 30 | TextBase(); |
|---|
| 31 | TextBase(const TextBase& text,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 32 | |
|---|
| 33 | //virtual osg::Object* cloneType() const { return new Text(); } |
|---|
| 34 | //virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Text(*this,copyop); } |
|---|
| 35 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const TextBase*>(obj)!=NULL; } |
|---|
| 36 | virtual const char* className() const { return "TextBase"; } |
|---|
| 37 | virtual const char* libraryName() const { return "osgText"; } |
|---|
| 38 | |
|---|
| 39 | void setColor(const osg::Vec4& color); |
|---|
| 40 | const osg::Vec4& getColor() const { return _color; } |
|---|
| 41 | |
|---|
| 42 | /** Set the Font to use to render the text. |
|---|
| 43 | * setFont(0) sets the use of the default font.*/ |
|---|
| 44 | virtual void setFont(Font* font=0) { setFont(osg::ref_ptr<Font>(font)); }; |
|---|
| 45 | |
|---|
| 46 | /** Set the Font to use to render the text.*/ |
|---|
| 47 | virtual void setFont(osg::ref_ptr<Font> font); |
|---|
| 48 | |
|---|
| 49 | /** Set the font, loaded from the specified front file, to use to render the text, |
|---|
| 50 | * setFont("") sets the use of the default font. |
|---|
| 51 | * See the osgText::readFontFile function for how the font file will be located. */ |
|---|
| 52 | virtual void setFont(const std::string& fontfile); |
|---|
| 53 | |
|---|
| 54 | /** Get the font. Return 0 if default is being used.*/ |
|---|
| 55 | const Font* getFont() const { return _font.get(); } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /** Set the text style.*/ |
|---|
| 59 | void setStyle(Style* style) { _style = style; } |
|---|
| 60 | /** Get the text style.*/ |
|---|
| 61 | Style* getStyle() { return _style.get(); } |
|---|
| 62 | /** Get the const text style.*/ |
|---|
| 63 | const Style* getStyle() const { return _style.get(); } |
|---|
| 64 | |
|---|
| 65 | /** Get or create the text style.*/ |
|---|
| 66 | Style* getOrCreateStyle() { if (!_style) _style = new Style; return _style.get(); } |
|---|
| 67 | |
|---|
| 68 | /** Set the Font reference width and height resolution in texels. |
|---|
| 69 | * Note, the size may not be supported by current font, |
|---|
| 70 | * the closest supported font size will be selected.*/ |
|---|
| 71 | void setFontResolution(unsigned int width, unsigned int height); |
|---|
| 72 | |
|---|
| 73 | unsigned int getFontWidth() const { return _fontSize.first; } |
|---|
| 74 | unsigned int getFontHeight() const { return _fontSize.second; } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | /** Set the text using a osgText::String.*/ |
|---|
| 78 | void setText(const String& text); |
|---|
| 79 | |
|---|
| 80 | /** Set the text using a std::string, |
|---|
| 81 | * which is converted to an internal TextString.*/ |
|---|
| 82 | void setText(const std::string& text); |
|---|
| 83 | |
|---|
| 84 | /** Set the text using a Unicode encoded std::string, which is converted to an internal TextString. |
|---|
| 85 | * The encoding parameter specificies which Unicode encodeding is used in the std::string. */ |
|---|
| 86 | void setText(const std::string& text,String::Encoding encoding); |
|---|
| 87 | |
|---|
| 88 | /** Set the text using a wchar_t string, |
|---|
| 89 | * which is converted to an internal TextString.*/ |
|---|
| 90 | void setText(const wchar_t* text); |
|---|
| 91 | |
|---|
| 92 | /** Get the text string. |
|---|
| 93 | * Note, if you modify the string you must call Text::update() for |
|---|
| 94 | * the internal glyph reprentation to be updated.*/ |
|---|
| 95 | String& getText() { return _text; } |
|---|
| 96 | |
|---|
| 97 | /** Get the const text string.*/ |
|---|
| 98 | const String& getText() const { return _text; } |
|---|
| 99 | |
|---|
| 100 | /** update internal glyph respresentation used for rendering, |
|---|
| 101 | * and bounding volume.*/ |
|---|
| 102 | void update() { computeGlyphRepresentation(); } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** Set the rendered character size in object coordinates.*/ |
|---|
| 106 | void setCharacterSize(float height); |
|---|
| 107 | |
|---|
| 108 | /** Set the rendered character size in object coordinates.*/ |
|---|
| 109 | void setCharacterSize(float height, float aspectRatio); |
|---|
| 110 | |
|---|
| 111 | float getCharacterHeight() const { return _characterHeight; } |
|---|
| 112 | float getCharacterAspectRatio() const { return _style.valid()? _style->getWidthRatio() : 1.0f; } |
|---|
| 113 | |
|---|
| 114 | enum CharacterSizeMode |
|---|
| 115 | { |
|---|
| 116 | OBJECT_COORDS, /// default |
|---|
| 117 | SCREEN_COORDS, /// internally scale the characters to be constant screen size. |
|---|
| 118 | 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. |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | /** Set how the CharacterSize value relates to the final rendered character.*/ |
|---|
| 122 | void setCharacterSizeMode(CharacterSizeMode mode) { _characterSizeMode = mode; } |
|---|
| 123 | |
|---|
| 124 | /** Get the CharacterSizeMode.*/ |
|---|
| 125 | CharacterSizeMode getCharacterSizeMode() const { return _characterSizeMode; } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | /** Set the maximum width of the text box. |
|---|
| 129 | * With horizontal layouts any characters which do not fit are wrapped around. |
|---|
| 130 | * 0 or negative values indicate that no maximum width is set, lines can be as long as |
|---|
| 131 | * they need be to fit thre required text*/ |
|---|
| 132 | void setMaximumWidth(float maximumWidth); |
|---|
| 133 | |
|---|
| 134 | /** Get the maximim width of the text box.*/ |
|---|
| 135 | float getMaximumWidth() const { return _maximumWidth; } |
|---|
| 136 | |
|---|
| 137 | /** Set the maximum height of the text box. |
|---|
| 138 | * With horizontal layouts any characters which do not fit are wrapped around. |
|---|
| 139 | * 0 or negative values indicate that no maximum height is set, lines can be as long as |
|---|
| 140 | * they need be to fit the required text*/ |
|---|
| 141 | void setMaximumHeight(float maximumHeight); |
|---|
| 142 | |
|---|
| 143 | /** Get the maximum height of the text box.*/ |
|---|
| 144 | float getMaximumHeight() const { return _maximumHeight; } |
|---|
| 145 | |
|---|
| 146 | /** Set the line spacing of the text box, given as a percentage of |
|---|
| 147 | * the character height. The default value is 0 for backward |
|---|
| 148 | * compatibility. For longer paragraphs of text, a value of at |
|---|
| 149 | * least 25% (i.e. set line spacing to 0.25) is recommended. */ |
|---|
| 150 | void setLineSpacing(float lineSpacing); |
|---|
| 151 | |
|---|
| 152 | /** Get the line spacing of the text box. */ |
|---|
| 153 | float getLineSpacing() const { return _lineSpacing; } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | /** Set the position of text.*/ |
|---|
| 158 | void setPosition(const osg::Vec3& pos); |
|---|
| 159 | |
|---|
| 160 | /** Get the position of text.*/ |
|---|
| 161 | const osg::Vec3& getPosition() const { return _position; } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | enum AlignmentType |
|---|
| 165 | { |
|---|
| 166 | LEFT_TOP, |
|---|
| 167 | LEFT_CENTER, |
|---|
| 168 | LEFT_BOTTOM, |
|---|
| 169 | |
|---|
| 170 | CENTER_TOP, |
|---|
| 171 | CENTER_CENTER, |
|---|
| 172 | CENTER_BOTTOM, |
|---|
| 173 | |
|---|
| 174 | RIGHT_TOP, |
|---|
| 175 | RIGHT_CENTER, |
|---|
| 176 | RIGHT_BOTTOM, |
|---|
| 177 | |
|---|
| 178 | LEFT_BASE_LINE, |
|---|
| 179 | CENTER_BASE_LINE, |
|---|
| 180 | RIGHT_BASE_LINE, |
|---|
| 181 | |
|---|
| 182 | LEFT_BOTTOM_BASE_LINE, |
|---|
| 183 | CENTER_BOTTOM_BASE_LINE, |
|---|
| 184 | RIGHT_BOTTOM_BASE_LINE, |
|---|
| 185 | |
|---|
| 186 | BASE_LINE = LEFT_BASE_LINE /// default. |
|---|
| 187 | |
|---|
| 188 | }; |
|---|
| 189 | |
|---|
| 190 | void setAlignment(AlignmentType alignment); |
|---|
| 191 | AlignmentType getAlignment() const { return _alignment; } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | enum AxisAlignment |
|---|
| 195 | { |
|---|
| 196 | XY_PLANE, |
|---|
| 197 | REVERSED_XY_PLANE, |
|---|
| 198 | XZ_PLANE, |
|---|
| 199 | REVERSED_XZ_PLANE, |
|---|
| 200 | YZ_PLANE, |
|---|
| 201 | REVERSED_YZ_PLANE, |
|---|
| 202 | SCREEN, |
|---|
| 203 | USER_DEFINED_ROTATION |
|---|
| 204 | }; |
|---|
| 205 | |
|---|
| 206 | void setAxisAlignment(AxisAlignment axis); |
|---|
| 207 | AxisAlignment getAxisAlignment() const { return _axisAlignment; } |
|---|
| 208 | |
|---|
| 209 | void setRotation(const osg::Quat& quat); |
|---|
| 210 | const osg::Quat& getRotation() const { return _rotation; } |
|---|
| 211 | |
|---|
| 212 | void setAutoRotateToScreen(bool autoRotateToScreen); |
|---|
| 213 | bool getAutoRotateToScreen() const { return _autoRotateToScreen; } |
|---|
| 214 | |
|---|
| 215 | enum Layout |
|---|
| 216 | { |
|---|
| 217 | LEFT_TO_RIGHT, /// default |
|---|
| 218 | RIGHT_TO_LEFT, |
|---|
| 219 | VERTICAL |
|---|
| 220 | }; |
|---|
| 221 | |
|---|
| 222 | void setLayout(Layout layout); |
|---|
| 223 | |
|---|
| 224 | Layout getLayout() const { return _layout; } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | enum DrawModeMask |
|---|
| 228 | { |
|---|
| 229 | TEXT = 1, /// default |
|---|
| 230 | BOUNDINGBOX = 2, |
|---|
| 231 | FILLEDBOUNDINGBOX = 4, |
|---|
| 232 | ALIGNMENT = 8 |
|---|
| 233 | }; |
|---|
| 234 | |
|---|
| 235 | void setDrawMode(unsigned int mode); |
|---|
| 236 | |
|---|
| 237 | unsigned int getDrawMode() const { return _drawMode; } |
|---|
| 238 | |
|---|
| 239 | void setBoundingBoxMargin(float margin); |
|---|
| 240 | |
|---|
| 241 | float getBoundingBoxMargin() const { return _textBBMargin; } |
|---|
| 242 | |
|---|
| 243 | void setBoundingBoxColor(const osg::Vec4& color){ _textBBColor = color; } |
|---|
| 244 | |
|---|
| 245 | const osg::Vec4& getBoundingBoxColor() const { return _textBBColor; } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | void setKerningType(KerningType kerningType) { _kerningType = kerningType; } |
|---|
| 249 | |
|---|
| 250 | KerningType getKerningType() const { return _kerningType; } |
|---|
| 251 | |
|---|
| 252 | /** Get the number of wrapped lines - only valid after computeGlyphRepresentation() has been called, returns 0 otherwise */ |
|---|
| 253 | unsigned int getLineCount() const { return _lineCount; } |
|---|
| 254 | |
|---|
| 255 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| 256 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 257 | |
|---|
| 258 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 259 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 260 | |
|---|
| 261 | /** If State is non-zero, this function releases OpenGL objects for |
|---|
| 262 | * the specified graphics context. Otherwise, releases OpenGL objexts |
|---|
| 263 | * for all graphics contexts. */ |
|---|
| 264 | virtual void releaseGLObjects(osg::State* state=0) const; |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | virtual osg::BoundingBox computeBound() const; |
|---|
| 268 | |
|---|
| 269 | protected: |
|---|
| 270 | |
|---|
| 271 | virtual ~TextBase(); |
|---|
| 272 | |
|---|
| 273 | void positionCursor(const osg::Vec2 & endOfLine_coords, osg::Vec2 & cursor, unsigned int linelength); |
|---|
| 274 | void computePositions(); |
|---|
| 275 | String::iterator computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last); |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | virtual void computePositions(unsigned int contextID) const = 0; |
|---|
| 279 | virtual void computeGlyphRepresentation() = 0; |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | // members which have public access. |
|---|
| 283 | osg::Vec4 _color; |
|---|
| 284 | osg::ref_ptr<Font> _font; |
|---|
| 285 | osg::ref_ptr<Style> _style; |
|---|
| 286 | FontResolution _fontSize; |
|---|
| 287 | float _characterHeight; |
|---|
| 288 | CharacterSizeMode _characterSizeMode; |
|---|
| 289 | float _maximumWidth; |
|---|
| 290 | float _maximumHeight; |
|---|
| 291 | float _lineSpacing; |
|---|
| 292 | |
|---|
| 293 | String _text; |
|---|
| 294 | osg::Vec3 _position; |
|---|
| 295 | AlignmentType _alignment; |
|---|
| 296 | AxisAlignment _axisAlignment; |
|---|
| 297 | osg::Quat _rotation; |
|---|
| 298 | bool _autoRotateToScreen; |
|---|
| 299 | Layout _layout; |
|---|
| 300 | unsigned int _drawMode; |
|---|
| 301 | float _textBBMargin; |
|---|
| 302 | osg::Vec4 _textBBColor; |
|---|
| 303 | KerningType _kerningType; |
|---|
| 304 | unsigned int _lineCount; |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | // internal caches of the positioning of the text. |
|---|
| 309 | |
|---|
| 310 | struct AutoTransformCache |
|---|
| 311 | { |
|---|
| 312 | AutoTransformCache(): |
|---|
| 313 | _traversalNumber(-1), |
|---|
| 314 | _width(0), |
|---|
| 315 | _height(0) {} |
|---|
| 316 | |
|---|
| 317 | int _traversalNumber; |
|---|
| 318 | int _width; |
|---|
| 319 | int _height; |
|---|
| 320 | osg::Vec3 _transformedPosition; |
|---|
| 321 | osg::Matrix _modelview; |
|---|
| 322 | osg::Matrix _projection; |
|---|
| 323 | osg::Matrix _matrix; |
|---|
| 324 | }; |
|---|
| 325 | |
|---|
| 326 | mutable osg::buffered_object<AutoTransformCache> _autoTransformCache; |
|---|
| 327 | mutable osg::Vec3 _offset; |
|---|
| 328 | mutable osg::Vec3 _normal; |
|---|
| 329 | mutable osg::BoundingBox _textBB; |
|---|
| 330 | }; |
|---|
| 331 | |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | #endif |
|---|
| 336 | |
|---|