Unified more of the 2D and 3D text setup, fixed bugs in Text3D setup

which address the problems of black 3D text and the kerning causing problems with font positioning.
This commit is contained in:
Robert Osfield
2011-01-11 11:39:31 +00:00
parent d59ac54b31
commit 74cf034404
9 changed files with 174 additions and 247 deletions

View File

@@ -124,7 +124,7 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i
float maximumWidth = _maximumWidth;
float hr = 1.0f;
float wr = 1.0f/getCharacterAspectRatio();
float wr = 1.0f;
for(bool outOfSpace=false;lastChar!=last;++lastChar)
{
@@ -140,76 +140,53 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i
{
const osg::BoundingBox & bb = glyph->getBoundingBox();
if (_layout==RIGHT_TO_LEFT)
{
cursor.x() -= glyph->getHorizontalAdvance() * wr;
}
// adjust cursor position w.r.t any kerning.
if (previous_charcode)
if (kerning && previous_charcode)
{
if (_layout == RIGHT_TO_LEFT)
switch(_layout)
{
cursor.x() -= glyph->getHorizontalAdvance() * wr;
}
if (kerning)
{
switch (_layout)
{
case LEFT_TO_RIGHT:
{
osg::Vec2 delta(_font->getKerning(previous_charcode,charcode,_kerningType));
cursor.x() += delta.x() * wr;
cursor.y() += delta.y() * hr;
break;
}
case RIGHT_TO_LEFT:
{
osg::Vec2 delta(_font->getKerning(charcode,previous_charcode,_kerningType));
cursor.x() -= delta.x() * wr;
cursor.y() -= delta.y() * hr;
break;
}
case VERTICAL:
break; // no kerning when vertical.
}
case LEFT_TO_RIGHT:
{
osg::Vec2 delta(_font->getKerning(previous_charcode,charcode,_kerningType));
cursor.x() += delta.x() * wr;
cursor.y() += delta.y() * hr;
break;
}
case RIGHT_TO_LEFT:
{
osg::Vec2 delta(_font->getKerning(charcode,previous_charcode,_kerningType));
cursor.x() -= delta.x() * wr;
cursor.y() -= delta.y() * hr;
break;
}
case VERTICAL:
break; // no kerning when vertical.
}
}
else
{
switch (_layout)
{
case LEFT_TO_RIGHT:
{
cursor.x() -= glyph->getHorizontalBearing().x() * wr;
break;
}
case RIGHT_TO_LEFT:
{
cursor.x() -= bb.xMax() * wr;
break;
}
case VERTICAL:
break;
}
}
osg::Vec2 local = cursor;
switch(_layout)
{
case LEFT_TO_RIGHT:
{
if (maximumWidth>0.0f && cursor.x()+bb.xMax()>maximumWidth) outOfSpace=true;
if(maximumHeight>0.0f && cursor.y()<-maximumHeight) outOfSpace=true;
if (maximumWidth>0.0f && local.x()+bb.xMax()>maximumWidth) outOfSpace=true;
if(maximumHeight>0.0f && local.y()<-maximumHeight) outOfSpace=true;
break;
}
case RIGHT_TO_LEFT:
{
if (maximumWidth>0.0f && cursor.x()+bb.xMin()<-maximumWidth) outOfSpace=true;
if(maximumHeight>0.0f && cursor.y()<-maximumHeight) outOfSpace=true;
if (maximumWidth>0.0f && local.x()+bb.xMin()<-maximumWidth) outOfSpace=true;
if(maximumHeight>0.0f && local.y()<-maximumHeight) outOfSpace=true;
break;
}
case VERTICAL:
if (maximumHeight>0.0f && cursor.y()<-maximumHeight) outOfSpace=true;
if (maximumHeight>0.0f && local.y()<-maximumHeight) outOfSpace=true;
break;
}
@@ -287,7 +264,7 @@ void Text3D::computeGlyphRepresentation()
float hr = 1.0f;
float wr = 1.0f/getCharacterAspectRatio();
float wr = 1.0f;
osg::Vec2 startOfLine_coords(0.0f,0.0f);
osg::Vec2 cursor(startOfLine_coords);
@@ -300,8 +277,6 @@ void Text3D::computeGlyphRepresentation()
unsigned int lineNumber = 0;
for(String::iterator itr=_text.begin(); itr!=_text.end(); )
{
_textRenderInfo.resize(lineNumber + 1);
@@ -329,70 +304,37 @@ void Text3D::computeGlyphRepresentation()
{
const osg::BoundingBox & bb = glyph->getBoundingBox();
// adjust cursor position w.r.t any kerning.
if (previous_charcode)
if (_layout==RIGHT_TO_LEFT)
{
if (_layout == RIGHT_TO_LEFT)
{
cursor.x() -= glyph->getHorizontalAdvance() * wr;
}
if (kerning)
{
switch (_layout)
{
case LEFT_TO_RIGHT:
{
osg::Vec2 delta(_font->getKerning(previous_charcode,charcode,_kerningType));
cursor.x() += delta.x() * wr;
cursor.y() += delta.y() * hr;
break;
}
case RIGHT_TO_LEFT:
{
osg::Vec2 delta(_font->getKerning(charcode,previous_charcode,_kerningType));
cursor.x() -= delta.x() * wr;
cursor.y() -= delta.y() * hr;
break;
}
case VERTICAL:
break; // no kerning when vertical.
}
}
cursor.x() -= glyph->getHorizontalAdvance() * wr;
}
else
{
switch (_layout)
{
case LEFT_TO_RIGHT:
{
cursor.x() -= glyph->getHorizontalBearing().x() * wr;
break;
}
case RIGHT_TO_LEFT:
{
cursor.x() -= bb.xMax() * wr;
break;
}
case VERTICAL:
{
// cursor.y() += glyph->getVerticalBearing().y();
break;
}
}
// adjust cursor position w.r.t any kerning.
if (kerning && previous_charcode)
{
switch(_layout)
{
case LEFT_TO_RIGHT:
{
osg::Vec2 delta(_font->getKerning(previous_charcode,charcode,_kerningType));
cursor.x() += delta.x() * wr;
cursor.y() += delta.y() * hr;
break;
}
case RIGHT_TO_LEFT:
{
osg::Vec2 delta(_font->getKerning(charcode,previous_charcode,_kerningType));
cursor.x() -= delta.x() * wr;
cursor.y() -= delta.y() * hr;
break;
}
case VERTICAL:
break; // no kerning when vertical.
}
}
local = cursor;
if (_layout==VERTICAL)
{
local.x() += -glyph->getVerticalBearing().x() * wr;
local.y() += -glyph->getVerticalBearing().y() * hr;
}
// move the cursor onto the next character.
// also expand bounding box
switch (_layout)
@@ -400,12 +342,12 @@ void Text3D::computeGlyphRepresentation()
case LEFT_TO_RIGHT:
_textBB.expandBy(osg::Vec3(cursor.x() + bb.xMin()*wr, cursor.y() + bb.yMin()*hr, 0.0f)); //lower left corner
_textBB.expandBy(osg::Vec3(cursor.x() + bb.xMax()*wr, cursor.y() + bb.yMax()*hr, 0.0f)); //upper right corner
cursor.x() += glyph->getHorizontalAdvance();
cursor.x() += glyph->getHorizontalAdvance() * wr;
break;
case VERTICAL:
_textBB.expandBy(osg::Vec3(cursor.x(), cursor.y(), 0.0f)); //upper left corner
_textBB.expandBy(osg::Vec3(cursor.x() + glyph->getWidth()*wr, cursor.y() - glyph->getHeight()*hr, 0.0f)); //lower right corner
cursor.y() -= glyph->getVerticalAdvance();
cursor.y() -= glyph->getVerticalAdvance() * hr;
break;
case RIGHT_TO_LEFT:
_textBB.expandBy(osg::Vec3(cursor.x()+bb.xMax()*wr, cursor.y() + bb.yMax()*hr, 0.0f)); //upper right corner
@@ -534,9 +476,6 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const
osg::State & state = *renderInfo.getState();
unsigned int contextID = state.getContextID();
state.disableColorPointer();
state.Color(_color.r(),_color.g(),_color.b(),_color.a());
// ** save the previous modelview matrix
osg::Matrix previous(state.getModelViewMatrix());
@@ -555,6 +494,8 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const
{
if (_textBB.valid())
{
gl.Color4fv(_color.ptr());
osg::Vec3 c000(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMax()));
osg::Vec3 c100(osg::Vec3(_textBB.xMax(),_textBB.yMin(),_textBB.zMax()));
osg::Vec3 c110(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMax()));
@@ -604,6 +545,8 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const
osg::Vec3 vt(osg::Vec3(_offset.x(),_offset.y()-cursorsize,_offset.z()));
osg::Vec3 vb(osg::Vec3(_offset.x(),_offset.y()+cursorsize,_offset.z()));
gl.Color4fv(_color.ptr());
gl.Begin(GL_LINES);
gl.Vertex3fv(hl.ptr());
gl.Vertex3fv(hr.ptr());
@@ -615,6 +558,9 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const
if (_drawMode & TEXT)
{
state.disableColorPointer();
state.Color(_color.r(),_color.g(),_color.b(),_color.a());
renderInfo.getState()->disableAllVertexArrays();
#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE)