From 47087585c0a903eca4c89c23f2ed11f93e0f5212 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 6 Mar 2003 21:35:33 +0000 Subject: [PATCH] Added maximum width and maximum height limits to the osgText::Text which automatically wraps text which goes beyond these limits. --- include/osgText/Text | 23 ++++++++++++++ src/osgText/Text.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/include/osgText/Text b/include/osgText/Text index 48caa08b1..6a02e988c 100644 --- a/include/osgText/Text +++ b/include/osgText/Text @@ -83,6 +83,27 @@ public: float getCharacterAspectRatio() const { return _characterAspectRatio; } + + /** Set the maximum width of the text box. + * With horizontal layouts any characters which do not fit are wrapped around. + * 0 or negative values indicate that no maximum width is set, lines can be as long as + * they need be to fit thre required text*/ + void setMaximumWidth(float maximumWidth); + + /** Get the maximim width of the text box.*/ + float getMaximumWidth() const { return _maximumWidth; } + + /** Set the maximum height of the text box. + * With horizontal layouts any characters which do not fit are wrapped around. + * 0 or negative values indicate that no maximum height is set, lines can be as long as + * they need be to fit thre required text*/ + void setMaximumHeight(float maximumHeight); + + /** Get the maximum height of the text box.*/ + float getMaximumHeight() const { return _maximumHeight; } + + + /** Set the position of text.*/ void setPosition(const osg::Vec3& pos); @@ -182,6 +203,8 @@ protected: unsigned int _fontHeight; float _characterHeight; float _characterAspectRatio; + float _maximumWidth; + float _maximumHeight; String _text; osg::Vec3 _position; diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index 05f2e76c0..25dc3e6b5 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -25,6 +25,8 @@ Text::Text(): _fontHeight(32), _characterHeight(32), _characterAspectRatio(1.0f), + _maximumWidth(0.0f), + _maximumHeight(0.0f), _alignment(BASE_LINE), _axisAlignment(XY_PLANE), _rotation(), @@ -42,6 +44,8 @@ Text::Text(const Text& text,const osg::CopyOp& copyop): _fontHeight(text._fontHeight), _characterHeight(text._characterHeight), _characterAspectRatio(text._characterAspectRatio), + _maximumWidth(text._maximumWidth), + _maximumHeight(text._maximumHeight), _text(text._text), _position(text._position), _alignment(text._alignment), @@ -86,6 +90,18 @@ void Text::setCharacterSize(float height,float aspectRatio) computeGlyphRepresentation(); } +void Text::setMaximumWidth(float maximumWidth) +{ + _maximumWidth = maximumWidth; + computeGlyphRepresentation(); +} + +void Text::setMaximumHeight(float maximumHeight) +{ + _maximumHeight = maximumHeight; + computeGlyphRepresentation(); +} + void Text::setText(const String& text) { @@ -270,10 +286,64 @@ void Text::computeGlyphRepresentation() local = cursor; + osg::Vec2 bearing(horizontal?glyph->getHorizontalBearing():glyph->getVerticalBearing()); local.x() += bearing.x() * wr; local.y() += bearing.y() * hr; - + + // check to see if we are still within line if not move to next line. + switch(_layout) + { + case LEFT_TO_RIGHT: + { + if (_maximumWidth>0.0f) + { + if (local.x()+width>_maximumWidth) + { + startOfLine.y() -= _fontHeight; + cursor = startOfLine; + previous_charcode = 0; + + local = cursor; + local.x() += bearing.x() * wr; + local.y() += bearing.y() * hr; + } + } + break; + } + case RIGHT_TO_LEFT: + { + if (_maximumWidth>0.0f) + { + if (local.x()<-_maximumWidth) + { + startOfLine.y() -= _fontHeight; + cursor = startOfLine; + previous_charcode = 0; + + local = cursor; + local.x() += bearing.x() * wr; + local.y() += bearing.y() * hr; + } + } + break; + } + case VERTICAL: + if (_maximumHeight>0.0f) + { + if (local.y()<-_maximumHeight) + { + startOfLine.x() += _fontWidth; + cursor = startOfLine; + previous_charcode = 0; + + local = cursor; + local.x() += bearing.x() * wr; + local.y() += bearing.y() * hr; + } + } + break; + } GlyphQuads& glyphquad = _textureGlyphQuadMap[glyph->getTexture()->getStateSet()];