From c3f40e2e67ef0bc1a0828c81f812331c587f84b6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 3 Mar 2005 12:33:07 +0000 Subject: [PATCH] Added improved handling of center justificaion of multiline text --- include/osgText/Text | 6 +- src/osgText/Text.cpp | 128 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 5 deletions(-) diff --git a/include/osgText/Text b/include/osgText/Text index 7206acf90..d9e0d9e39 100644 --- a/include/osgText/Text +++ b/include/osgText/Text @@ -245,6 +245,7 @@ public: struct OSGTEXT_EXPORT GlyphQuads { typedef std::vector Glyphs; + typedef std::vector LineNumbers; typedef std::vector Coords2; typedef std::vector Coords3; typedef std::vector TexCoords; @@ -253,10 +254,10 @@ public: Coords2 _coords; osg::buffered_object _transformedCoords; TexCoords _texcoords; + LineNumbers _lineNumbers; Glyphs getGlyphs() { return _glyphs; } const Glyphs getGlyphs() const { return _glyphs; } - Coords2& getCoords() { return _coords; } const Coords2& getCoords() const { return _coords; } @@ -266,6 +267,9 @@ public: TexCoords& getTexCoords() { return _texcoords; } const TexCoords& getTexCoords() const { return _texcoords; } + + LineNumbers& getLineNumbers() { return _lineNumbers; } + const LineNumbers& getLineNumbers() const { return _lineNumbers; } }; typedef std::map,GlyphQuads> TextureGlyphQuadMap; diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index 72022577f..e1d228aff 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -385,6 +386,8 @@ void Text::computeGlyphRepresentation() deliminatorSet.insert(':'); deliminatorSet.insert('.'); + unsigned int lineNumber = 0; + for(String::iterator itr=_text.begin(); itr!=_text.end(); ) @@ -468,6 +471,7 @@ void Text::computeGlyphRepresentation() GlyphQuads& glyphquad = _textureGlyphQuadMap[glyph->getTexture()->getStateSet()]; glyphquad._glyphs.push_back(glyph); + glyphquad._lineNumbers.push_back(lineNumber); // set up the coords of the quad glyphquad._coords.push_back(local+osg::Vec2(0.0f,height)); @@ -533,17 +537,18 @@ void Text::computeGlyphRepresentation() } break; } + + ++lineNumber; } + // compute the bounding box _textBB.init(); - for(TextureGlyphQuadMap::const_iterator titr=_textureGlyphQuadMap.begin(); titr!=_textureGlyphQuadMap.end(); ++titr) { const GlyphQuads& glyphquad = titr->second; - for(GlyphQuads::Coords2::const_iterator citr = glyphquad._coords.begin(); citr != glyphquad._coords.end(); ++citr) @@ -552,11 +557,126 @@ void Text::computeGlyphRepresentation() } } - if (!_textureGlyphQuadMap.empty()) + if (lineNumber>1) { - setStateSet(const_cast((*_textureGlyphQuadMap.begin()).first.get())); + // account for line justification + typedef std::vector LineDimensions; + LineDimensions minLineCoords(lineNumber, osg::Vec2(FLT_MAX,FLT_MAX)); + LineDimensions maxLineCoords(lineNumber, osg::Vec2(-FLT_MAX,-FLT_MAX)); + + // osg::notify(osg::NOTICE)<<"lineNumber="<second; + const GlyphQuads::Coords2& coords = glyphquad._coords; + + unsigned int coordIndex = 0; + for(GlyphQuads::LineNumbers::const_iterator litr = glyphquad._lineNumbers.begin(); + litr != glyphquad._lineNumbers.end(); + ++litr) + { + unsigned int line = *litr; + osg::Vec2& minLineCoord = minLineCoords[line]; + osg::Vec2& maxLineCoord = maxLineCoords[line]; + for(unsigned int ci=0;ci<4;++ci) + { + const osg::Vec2& coord = coords[coordIndex++]; + if (coord.x()maxLineCoord.x()) maxLineCoord.x()=coord.x(); + if (coord.y()>maxLineCoord.y()) maxLineCoord.y()=coord.y(); + } + } + } + + // osg::notify(osg::NOTICE)<<"Text dimensions min="<<_textBB.xMin()<<" "<<_textBB.yMin()<<" max="<<_textBB.xMax()<<" "<<_textBB.yMax()< LineOffsets; + LineOffsets lineOffsets(lineNumber,osg::Vec2(0.0f,0.0f)); + for(unsigned int li=0;li