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

@@ -387,6 +387,8 @@ void GlyphTexture::resizeGLObjectBuffers(unsigned int maxSize)
Glyph::Glyph(Font* font, unsigned int glyphCode):
_font(font),
_glyphCode(glyphCode),
_width(1.0f),
_height(1.0f),
_horizontalBearing(0.0f,0.f),
_horizontalAdvance(0.f),
_verticalBearing(0.0f,0.f),
@@ -477,6 +479,8 @@ Glyph3D::Glyph3D(Font* font, unsigned int glyphCode):
osg::Referenced(true),
_font(font),
_glyphCode(glyphCode),
_width(1.0f),
_height(1.0f),
_horizontalBearing(0,0),
_horizontalAdvance(0),
_verticalBearing(0,0),

View File

@@ -64,6 +64,21 @@ Text::~Text()
{
}
void Text::setFont(osg::ref_ptr<Font> font)
{
if (_font==font) return;
osg::StateSet* previousFontStateSet = _font.valid() ? _font->getStateSet() : Font::getDefaultFont()->getStateSet();
osg::StateSet* newFontStateSet = font.valid() ? font->getStateSet() : Font::getDefaultFont()->getStateSet();
if (getStateSet() == previousFontStateSet)
{
setStateSet( newFontStateSet );
}
TextBase::setFont(font);
}
Font* Text::getActiveFont()
{
@@ -80,7 +95,7 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
Font* activefont = getActiveFont();
if (!activefont) return last;
float hr = _characterHeight/getFontHeight();
float hr = _characterHeight;
float wr = hr/getCharacterAspectRatio();
bool kerning = true;
@@ -101,12 +116,7 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
if (glyph)
{
float width = (float)(glyph->s()) * wr;
//float height = (float)(glyph->t()) * hr;
#ifdef TREES_CODE_FOR_MAKING_SPACES_EDITABLE
if (width == 0.0f) width = glyph->getHorizontalAdvance() * wr;
//if (height == 0.0f) height = glyph->getVerticalAdvance() * hr;
#endif
float width = (float)(glyph->getWidth()) * wr;
if (_layout==RIGHT_TO_LEFT)
{
@@ -167,7 +177,7 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
case RIGHT_TO_LEFT: break; // nop.
}
previous_charcode = charcode;
previous_charcode = charcode;
}
@@ -243,7 +253,7 @@ void Text::computeGlyphRepresentation()
unsigned int lineNumber = 0;
float hr = _characterHeight/getFontHeight();
float hr = _characterHeight;
float wr = hr/getCharacterAspectRatio();
for(String::iterator itr=_text.begin();
@@ -369,13 +379,8 @@ void Text::computeGlyphRepresentation()
Glyph* glyph = activefont->getGlyph(_fontSize, charcode);
if (glyph)
{
float width = (float)(glyph->s()) * wr;
float height = (float)(glyph->t()) * hr;
#ifdef TREES_CODE_FOR_MAKING_SPACES_EDITABLE
if (width == 0.0f) width = glyph->getHorizontalAdvance() * wr;
if (height == 0.0f) height = glyph->getVerticalAdvance() * hr;
#endif
float width = (float)(glyph->getWidth()) * wr;
float height = (float)(glyph->getHeight()) * hr;
if (_layout==RIGHT_TO_LEFT)
{
@@ -421,10 +426,12 @@ void Text::computeGlyphRepresentation()
osg::Vec2 mintc = glyph->getMinTexCoord();
osg::Vec2 maxtc = glyph->getMaxTexCoord();
osg::Vec2 vDiff = maxtc - mintc;
float fHorizTCMargin = 1.0f / glyph->getTexture()->getTextureWidth();
float fVertTCMargin = 1.0f / glyph->getTexture()->getTextureHeight();
float fHorizQuadMargin = vDiff.x() == 0.0f ? 0.0f : width * fHorizTCMargin / vDiff.x();
float fVertQuadMargin = vDiff.y() == 0.0f ? 0.0f : height * fVertTCMargin / vDiff.y();
mintc.x() -= fHorizTCMargin;
mintc.y() -= fVertTCMargin;
maxtc.x() += fHorizTCMargin;
@@ -1202,10 +1209,10 @@ void Text::drawImplementation(osg::State& state, const osg::Vec4& colorMultiplie
unsigned int contextID = state.getContextID();
state.applyMode(GL_BLEND,true);
#if 1
state.applyTextureMode(0,GL_TEXTURE_2D,true);
#if 1
state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON);
#else
state.applyTextureMode(0,GL_TEXTURE_2D,false);
state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF);
#endif
#if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
state.applyTextureAttribute(0,getActiveFont()->getTexEnv());
@@ -1341,10 +1348,10 @@ void Text::drawImplementation(osg::State& state, const osg::Vec4& colorMultiplie
}
}
#if 1
state.applyTextureMode(0,GL_TEXTURE_2D,true);
#if 1
state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON);
#else
state.applyTextureMode(0,GL_TEXTURE_2D,false);
state.applyTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF);
#endif
#if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
state.applyTextureAttribute(0,getActiveFont()->getTexEnv());

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)

View File

@@ -93,14 +93,6 @@ void TextBase::setFont(osg::ref_ptr<Font> font)
{
if (_font==font) return;
osg::StateSet* previousFontStateSet = _font.valid() ? _font->getStateSet() : Font::getDefaultFont()->getStateSet();
osg::StateSet* newFontStateSet = font.valid() ? font->getStateSet() : Font::getDefaultFont()->getStateSet();
if (getStateSet() == previousFontStateSet)
{
setStateSet( newFontStateSet );
}
_font = font;
computeGlyphRepresentation();