| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgText/TextBase> |
|---|
| 16 | #include <osgText/Font> |
|---|
| 17 | |
|---|
| 18 | #include <osg/Math> |
|---|
| 19 | #include <osg/GL> |
|---|
| 20 | #include <osg/Notify> |
|---|
| 21 | #include <osg/PolygonOffset> |
|---|
| 22 | #include <osg/TexEnv> |
|---|
| 23 | |
|---|
| 24 | #include <osgUtil/CullVisitor> |
|---|
| 25 | |
|---|
| 26 | #include <osgDB/ReadFile> |
|---|
| 27 | |
|---|
| 28 | using namespace osg; |
|---|
| 29 | using namespace osgText; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | TextBase::TextBase(): |
|---|
| 34 | _color(1.0f,1.0f,1.0f,1.0f), |
|---|
| 35 | _fontSize(32,32), |
|---|
| 36 | _characterHeight(32), |
|---|
| 37 | _characterSizeMode(OBJECT_COORDS), |
|---|
| 38 | _maximumWidth(0.0f), |
|---|
| 39 | _maximumHeight(0.0f), |
|---|
| 40 | _lineSpacing(0.0f), |
|---|
| 41 | _alignment(BASE_LINE), |
|---|
| 42 | _axisAlignment(XY_PLANE), |
|---|
| 43 | _autoRotateToScreen(false), |
|---|
| 44 | _layout(LEFT_TO_RIGHT), |
|---|
| 45 | _drawMode(TEXT), |
|---|
| 46 | _textBBMargin(0.0f), |
|---|
| 47 | _textBBColor(0.0, 0.0, 0.0, 0.5), |
|---|
| 48 | _kerningType(KERNING_DEFAULT), |
|---|
| 49 | _lineCount(0) |
|---|
| 50 | { |
|---|
| 51 | setStateSet(Font::getDefaultFont()->getStateSet()); |
|---|
| 52 | setUseDisplayList(false); |
|---|
| 53 | setSupportsDisplayList(false); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | TextBase::TextBase(const TextBase& textBase,const osg::CopyOp& copyop): |
|---|
| 57 | osg::Drawable(textBase,copyop), |
|---|
| 58 | _color(textBase._color), |
|---|
| 59 | _font(textBase._font), |
|---|
| 60 | _style(textBase._style), |
|---|
| 61 | _fontSize(textBase._fontSize), |
|---|
| 62 | _characterHeight(textBase._characterHeight), |
|---|
| 63 | _characterSizeMode(textBase._characterSizeMode), |
|---|
| 64 | _maximumWidth(textBase._maximumWidth), |
|---|
| 65 | _maximumHeight(textBase._maximumHeight), |
|---|
| 66 | _lineSpacing(textBase._lineSpacing), |
|---|
| 67 | _text(textBase._text), |
|---|
| 68 | _position(textBase._position), |
|---|
| 69 | _alignment(textBase._alignment), |
|---|
| 70 | _axisAlignment(textBase._axisAlignment), |
|---|
| 71 | _rotation(textBase._rotation), |
|---|
| 72 | _autoRotateToScreen(textBase._autoRotateToScreen), |
|---|
| 73 | _layout(textBase._layout), |
|---|
| 74 | _drawMode(textBase._drawMode), |
|---|
| 75 | _textBBMargin(textBase._textBBMargin), |
|---|
| 76 | _textBBColor(textBase._textBBColor), |
|---|
| 77 | _kerningType(textBase._kerningType), |
|---|
| 78 | _lineCount(textBase._lineCount) |
|---|
| 79 | { |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | TextBase::~TextBase() |
|---|
| 83 | { |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void TextBase::setColor(const osg::Vec4& color) |
|---|
| 87 | { |
|---|
| 88 | _color = color; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | void TextBase::setFont(osg::ref_ptr<Font> font) |
|---|
| 93 | { |
|---|
| 94 | if (_font==font) return; |
|---|
| 95 | |
|---|
| 96 | _font = font; |
|---|
| 97 | |
|---|
| 98 | computeGlyphRepresentation(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void TextBase::setFont(const std::string& fontfile) |
|---|
| 102 | { |
|---|
| 103 | setFont(readRefFontFile(fontfile)); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | void TextBase::setFontResolution(unsigned int width, unsigned int height) |
|---|
| 107 | { |
|---|
| 108 | _fontSize = FontResolution(width,height); |
|---|
| 109 | computeGlyphRepresentation(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void TextBase::setCharacterSize(float height) |
|---|
| 113 | { |
|---|
| 114 | _characterHeight = height; |
|---|
| 115 | computeGlyphRepresentation(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void TextBase::setCharacterSize(float height, float aspectRatio) |
|---|
| 119 | { |
|---|
| 120 | if (getCharacterAspectRatio()!=aspectRatio) |
|---|
| 121 | { |
|---|
| 122 | getOrCreateStyle()->setWidthRatio(aspectRatio); |
|---|
| 123 | } |
|---|
| 124 | setCharacterSize(height); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | void TextBase::setMaximumWidth(float maximumWidth) |
|---|
| 128 | { |
|---|
| 129 | _maximumWidth = maximumWidth; |
|---|
| 130 | computeGlyphRepresentation(); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | void TextBase::setMaximumHeight(float maximumHeight) |
|---|
| 134 | { |
|---|
| 135 | _maximumHeight = maximumHeight; |
|---|
| 136 | computeGlyphRepresentation(); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void TextBase::setLineSpacing(float lineSpacing) |
|---|
| 140 | { |
|---|
| 141 | _lineSpacing = lineSpacing; |
|---|
| 142 | computeGlyphRepresentation(); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | void TextBase::setText(const String& text) |
|---|
| 147 | { |
|---|
| 148 | if (_text==text) return; |
|---|
| 149 | |
|---|
| 150 | _text = text; |
|---|
| 151 | computeGlyphRepresentation(); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void TextBase::setText(const std::string& text) |
|---|
| 155 | { |
|---|
| 156 | setText(String(text)); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | void TextBase::setText(const std::string& text,String::Encoding encoding) |
|---|
| 160 | { |
|---|
| 161 | setText(String(text,encoding)); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | void TextBase::setText(const wchar_t* text) |
|---|
| 166 | { |
|---|
| 167 | setText(String(text)); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void TextBase::setPosition(const osg::Vec3& pos) |
|---|
| 171 | { |
|---|
| 172 | if (_position==pos) return; |
|---|
| 173 | |
|---|
| 174 | _position = pos; |
|---|
| 175 | computePositions(); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | void TextBase::setAlignment(AlignmentType alignment) |
|---|
| 179 | { |
|---|
| 180 | if (_alignment==alignment) return; |
|---|
| 181 | |
|---|
| 182 | _alignment = alignment; |
|---|
| 183 | computeGlyphRepresentation(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | void TextBase::setAxisAlignment(AxisAlignment axis) |
|---|
| 187 | { |
|---|
| 188 | _axisAlignment = axis; |
|---|
| 189 | |
|---|
| 190 | switch(axis) |
|---|
| 191 | { |
|---|
| 192 | case XZ_PLANE: |
|---|
| 193 | setAutoRotateToScreen(false); |
|---|
| 194 | setRotation(osg::Quat(osg::inDegrees(90.0f),osg::Vec3(1.0f,0.0f,0.0f))); |
|---|
| 195 | break; |
|---|
| 196 | case REVERSED_XZ_PLANE: |
|---|
| 197 | setAutoRotateToScreen(false); |
|---|
| 198 | setRotation(osg::Quat(osg::inDegrees(180.0f),osg::Vec3(0.0f,1.0f,0.0f))* |
|---|
| 199 | osg::Quat(osg::inDegrees(90.0f),osg::Vec3(1.0f,0.0f,0.0f))); |
|---|
| 200 | break; |
|---|
| 201 | case YZ_PLANE: |
|---|
| 202 | setAutoRotateToScreen(false); |
|---|
| 203 | setRotation(osg::Quat(osg::inDegrees(90.0f),osg::Vec3(1.0f,0.0f,0.0f))* |
|---|
| 204 | osg::Quat(osg::inDegrees(90.0f),osg::Vec3(0.0f,0.0f,1.0f))); |
|---|
| 205 | break; |
|---|
| 206 | case REVERSED_YZ_PLANE: |
|---|
| 207 | setAutoRotateToScreen(false); |
|---|
| 208 | setRotation(osg::Quat(osg::inDegrees(180.0f),osg::Vec3(0.0f,1.0f,0.0f))* |
|---|
| 209 | osg::Quat(osg::inDegrees(90.0f),osg::Vec3(1.0f,0.0f,0.0f))* |
|---|
| 210 | osg::Quat(osg::inDegrees(90.0f),osg::Vec3(0.0f,0.0f,1.0f))); |
|---|
| 211 | break; |
|---|
| 212 | case XY_PLANE: |
|---|
| 213 | setAutoRotateToScreen(false); |
|---|
| 214 | setRotation(osg::Quat()); |
|---|
| 215 | break; |
|---|
| 216 | case REVERSED_XY_PLANE: |
|---|
| 217 | setAutoRotateToScreen(false); |
|---|
| 218 | setRotation(osg::Quat(osg::inDegrees(180.0f),osg::Vec3(0.0f,1.0f,0.0f))); |
|---|
| 219 | break; |
|---|
| 220 | case SCREEN: |
|---|
| 221 | setAutoRotateToScreen(true); |
|---|
| 222 | setRotation(osg::Quat()); |
|---|
| 223 | break; |
|---|
| 224 | default: break; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | void TextBase::setRotation(const osg::Quat& quat) |
|---|
| 229 | { |
|---|
| 230 | _rotation = quat; |
|---|
| 231 | computePositions(); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | void TextBase::setAutoRotateToScreen(bool autoRotateToScreen) |
|---|
| 236 | { |
|---|
| 237 | if (_autoRotateToScreen==autoRotateToScreen) return; |
|---|
| 238 | |
|---|
| 239 | _autoRotateToScreen = autoRotateToScreen; |
|---|
| 240 | computePositions(); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | void TextBase::setLayout(Layout layout) |
|---|
| 245 | { |
|---|
| 246 | if (_layout==layout) return; |
|---|
| 247 | |
|---|
| 248 | _layout = layout; |
|---|
| 249 | computeGlyphRepresentation(); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | void TextBase::setDrawMode(unsigned int mode) |
|---|
| 254 | { |
|---|
| 255 | if (_drawMode==mode) return; |
|---|
| 256 | |
|---|
| 257 | _drawMode=mode; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | void TextBase::setBoundingBoxMargin(float margin) |
|---|
| 262 | { |
|---|
| 263 | if (_textBBMargin == margin) |
|---|
| 264 | return; |
|---|
| 265 | |
|---|
| 266 | _textBBMargin = margin; |
|---|
| 267 | computeGlyphRepresentation(); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | osg::BoundingBox TextBase::computeBound() const |
|---|
| 272 | { |
|---|
| 273 | osg::BoundingBox bbox; |
|---|
| 274 | |
|---|
| 275 | if (_textBB.valid()) |
|---|
| 276 | { |
|---|
| 277 | for(unsigned int i=0;i<_autoTransformCache.size();++i) |
|---|
| 278 | { |
|---|
| 279 | if (_autoTransformCache[i]._traversalNumber>=0) |
|---|
| 280 | { |
|---|
| 281 | osg::Matrix& matrix = _autoTransformCache[i]._matrix; |
|---|
| 282 | bbox.expandBy(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMin())*matrix); |
|---|
| 283 | bbox.expandBy(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMax())*matrix); |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | if (!bbox.valid()) |
|---|
| 289 | { |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | if (_autoRotateToScreen) |
|---|
| 296 | { |
|---|
| 297 | |
|---|
| 298 | double radius = _textBB.radius(); |
|---|
| 299 | osg::Vec3 diagonal(radius, radius, radius); |
|---|
| 300 | bbox.set(_position-diagonal, _position+diagonal); |
|---|
| 301 | } |
|---|
| 302 | else |
|---|
| 303 | { |
|---|
| 304 | osg::Matrix matrix; |
|---|
| 305 | matrix.makeTranslate(-_offset); |
|---|
| 306 | matrix.postMultRotate(_rotation); |
|---|
| 307 | matrix.postMultTranslate(_position); |
|---|
| 308 | bbox.expandBy(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMin())*matrix); |
|---|
| 309 | bbox.expandBy(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMax())*matrix); |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | return bbox; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | void TextBase::computePositions() |
|---|
| 318 | { |
|---|
| 319 | unsigned int size = osg::maximum(osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),_autoTransformCache.size()); |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | for(unsigned int i=0;i<size;++i) |
|---|
| 329 | { |
|---|
| 330 | computePositions(i); |
|---|
| 331 | } |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | void TextBase::setThreadSafeRefUnref(bool threadSafe) |
|---|
| 335 | { |
|---|
| 336 | Drawable::setThreadSafeRefUnref(threadSafe); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | void TextBase::resizeGLObjectBuffers(unsigned int maxSize) |
|---|
| 340 | { |
|---|
| 341 | Drawable::resizeGLObjectBuffers(maxSize); |
|---|
| 342 | |
|---|
| 343 | _autoTransformCache.resize(maxSize); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | void TextBase::releaseGLObjects(osg::State* state) const |
|---|
| 348 | { |
|---|
| 349 | Drawable::releaseGLObjects(state); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | void TextBase::positionCursor(const osg::Vec2 & endOfLine_coords, osg::Vec2 & cursor, unsigned int linelength) |
|---|
| 353 | { |
|---|
| 354 | switch(_layout) |
|---|
| 355 | { |
|---|
| 356 | case LEFT_TO_RIGHT: |
|---|
| 357 | { |
|---|
| 358 | switch (_alignment) |
|---|
| 359 | { |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | case CENTER_TOP: |
|---|
| 368 | case CENTER_CENTER: |
|---|
| 369 | case CENTER_BOTTOM: |
|---|
| 370 | case CENTER_BASE_LINE: |
|---|
| 371 | case CENTER_BOTTOM_BASE_LINE: |
|---|
| 372 | cursor.x() = (cursor.x() - endOfLine_coords.x()) * 0.5f; |
|---|
| 373 | break; |
|---|
| 374 | case RIGHT_TOP: |
|---|
| 375 | case RIGHT_CENTER: |
|---|
| 376 | case RIGHT_BOTTOM: |
|---|
| 377 | case RIGHT_BASE_LINE: |
|---|
| 378 | case RIGHT_BOTTOM_BASE_LINE: |
|---|
| 379 | cursor.x() = cursor.x() - endOfLine_coords.x(); |
|---|
| 380 | break; |
|---|
| 381 | default: |
|---|
| 382 | break; |
|---|
| 383 | } |
|---|
| 384 | break; |
|---|
| 385 | } |
|---|
| 386 | case RIGHT_TO_LEFT: |
|---|
| 387 | { |
|---|
| 388 | switch (_alignment) |
|---|
| 389 | { |
|---|
| 390 | case LEFT_TOP: |
|---|
| 391 | case LEFT_CENTER: |
|---|
| 392 | case LEFT_BOTTOM: |
|---|
| 393 | case LEFT_BASE_LINE: |
|---|
| 394 | case LEFT_BOTTOM_BASE_LINE: |
|---|
| 395 | cursor.x() = 2*cursor.x() - endOfLine_coords.x(); |
|---|
| 396 | break; |
|---|
| 397 | case CENTER_TOP: |
|---|
| 398 | case CENTER_CENTER: |
|---|
| 399 | case CENTER_BOTTOM: |
|---|
| 400 | case CENTER_BASE_LINE: |
|---|
| 401 | case CENTER_BOTTOM_BASE_LINE: |
|---|
| 402 | cursor.x() = cursor.x() + (cursor.x() - endOfLine_coords.x()) * 0.5f; |
|---|
| 403 | break; |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | default: |
|---|
| 412 | break; |
|---|
| 413 | } |
|---|
| 414 | break; |
|---|
| 415 | } |
|---|
| 416 | case VERTICAL: |
|---|
| 417 | { |
|---|
| 418 | switch (_alignment) |
|---|
| 419 | { |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | case LEFT_CENTER: |
|---|
| 432 | case CENTER_CENTER: |
|---|
| 433 | case RIGHT_CENTER: |
|---|
| 434 | cursor.y() = cursor.y() + (cursor.y() - endOfLine_coords.y()) * 0.5f; |
|---|
| 435 | break; |
|---|
| 436 | case LEFT_BOTTOM_BASE_LINE: |
|---|
| 437 | case CENTER_BOTTOM_BASE_LINE: |
|---|
| 438 | case RIGHT_BOTTOM_BASE_LINE: |
|---|
| 439 | cursor.y() = cursor.y() - (linelength * _characterHeight); |
|---|
| 440 | break; |
|---|
| 441 | case LEFT_BOTTOM: |
|---|
| 442 | case CENTER_BOTTOM: |
|---|
| 443 | case RIGHT_BOTTOM: |
|---|
| 444 | cursor.y() = 2*cursor.y() - endOfLine_coords.y(); |
|---|
| 445 | break; |
|---|
| 446 | default: |
|---|
| 447 | break; |
|---|
| 448 | } |
|---|
| 449 | break; |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | } |
|---|