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