Moved handling of character aspect ratio into osgText::Style.

This commit is contained in:
Robert Osfield 2010-09-29 12:45:35 +00:00
parent 9bde24d3d2
commit dab1c79127
6 changed files with 40 additions and 29 deletions

View File

@ -86,6 +86,7 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
osg::ref_ptr<osgText::Bevel> bevel = new osgText::Bevel;
bevel->roundedBevel2(0.25);
style->setBevel(bevel.get());
style->setWidthRatio(0.4f);
osgText::Text3D* text7 = new osgText::Text3D;
text7->setFont("fonts/times.ttf");

View File

@ -43,14 +43,6 @@ public:
META_Object(osgText,Text3D)
/** Set the text style.*/
void setStyle(Style* style) { _style = style; }
/** Get the text style.*/
Style* getStyle() { return _style.get(); }
/** Get the const text style.*/
const Style* getStyle() const { return _style.get(); }
/** Get the Charactere Depth of text. */
float getCharacterDepth() const;
@ -148,7 +140,6 @@ protected:
TextRenderInfo _textRenderInfo;
osg::ref_ptr<Style> _style;
RenderMode _renderMode;

View File

@ -36,6 +36,8 @@ public:
virtual const char* className() const { return "TextBase"; }
virtual const char* libraryName() const { return "osgText"; }
/** Set the Font to use to render the text.
* setFont(0) sets the use of the default font.*/
inline void setFont(Font* font=0) { setFont(osg::ref_ptr<Font>(font)); };
@ -51,6 +53,17 @@ public:
/** Get the font. Return 0 if default is being used.*/
const Font* getFont() const { return _font.get(); }
/** Set the text style.*/
void setStyle(Style* style) { _style = style; }
/** Get the text style.*/
Style* getStyle() { return _style.get(); }
/** Get the const text style.*/
const Style* getStyle() const { return _style.get(); }
/** Get or create the text style.*/
Style* getOrCreateStyle() { if (!_style) _style = new Style; return _style.get(); }
/** Set the Font reference width and height resolution in texels.
* Note, the size may not be supported by current font,
* the closest supported font size will be selected.*/
@ -89,10 +102,13 @@ public:
/** Set the rendered character size in object coordinates.*/
void setCharacterSize(float height,float aspectRatio=1.0f);
void setCharacterSize(float height);
/** Set the rendered character size in object coordinates.*/
void setCharacterSize(float height, float aspectRatio);
float getCharacterHeight() const { return _characterHeight; }
float getCharacterAspectRatio() const { return _characterAspectRatio; }
float getCharacterAspectRatio() const { return _style.valid()? _style->getWidthRatio() : 1.0f; }
enum CharacterSizeMode
{
@ -264,9 +280,9 @@ protected:
// members which have public access.
osg::ref_ptr<Font> _font;
osg::ref_ptr<Style> _style;
FontResolution _fontSize;
float _characterHeight;
float _characterAspectRatio;
CharacterSizeMode _characterSizeMode;
float _maximumWidth;
float _maximumHeight;

View File

@ -89,7 +89,7 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
if (!activefont) return last;
float hr = _characterHeight/getFontHeight();
float wr = hr/_characterAspectRatio;
float wr = hr/getCharacterAspectRatio();
bool kerning = true;
unsigned int previous_charcode = 0;
@ -252,7 +252,7 @@ void Text::computeGlyphRepresentation()
unsigned int lineNumber = 0;
float hr = _characterHeight/getFontHeight();
float wr = hr/_characterAspectRatio;
float wr = hr/getCharacterAspectRatio();
for(String::iterator itr=_text.begin();
itr!=_text.end();
@ -509,7 +509,7 @@ void Text::computeGlyphRepresentation()
}
case VERTICAL:
{
startOfLine_coords.x() += _characterHeight/_characterAspectRatio * (1.0 + _lineSpacing);
startOfLine_coords.x() += _characterHeight/getCharacterAspectRatio() * (1.0 + _lineSpacing);
cursor = startOfLine_coords;
previous_charcode = 0;
// because _lineCount is the max vertical no. of characters....
@ -662,7 +662,7 @@ void Text::computePositions(unsigned int contextID) const
float pixelSizeVector_w = M(3,2)*P23 + M(3,3)*P33;
float pixelSizeVert=(_characterHeight*sqrtf(scale_10.length2()))/(pixelSizeVector_w*0.701f);
float pixelSizeHori=(_characterHeight/_characterAspectRatio*sqrtf(scale_00.length2()))/(pixelSizeVector_w*0.701f);
float pixelSizeHori=(_characterHeight/getCharacterAspectRatio()*sqrtf(scale_00.length2()))/(pixelSizeVector_w*0.701f);
// avoid nasty math by preventing a divide by zero
if (pixelSizeVert == 0.0f)
@ -673,7 +673,7 @@ void Text::computePositions(unsigned int contextID) const
if (_characterSizeMode==SCREEN_COORDS)
{
float scale_font_vert=_characterHeight/pixelSizeVert;
float scale_font_hori=_characterHeight/_characterAspectRatio/pixelSizeHori;
float scale_font_hori=_characterHeight/getCharacterAspectRatio()/pixelSizeHori;
if (P10<0)
scale_font_vert=-scale_font_vert;

View File

@ -19,14 +19,12 @@ namespace osgText
{
Text3D::Text3D():
_style(0),
_renderMode(PER_GLYPH)
{
}
Text3D::Text3D(const Text3D & text3D, const osg::CopyOp & copyop):
osgText::TextBase(text3D, copyop),
_style(text3D._style),
_renderMode(text3D._renderMode)
{
computeGlyphRepresentation();
@ -40,8 +38,7 @@ float Text3D::getCharacterDepth() const
void Text3D::setCharacterDepth(float characterDepth)
{
if (!_style) _style = new Style;
_style->setThicknessRatio(characterDepth / _characterHeight);
getOrCreateStyle()->setThicknessRatio(characterDepth / _characterHeight);
computeGlyphRepresentation();
}
@ -434,7 +431,7 @@ void Text3D::computeGlyphRepresentation()
}
case VERTICAL:
{
startOfLine_coords.x() += _characterHeight / _characterAspectRatio * (1.0 + _lineSpacing);
startOfLine_coords.x() += _characterHeight / getCharacterAspectRatio() * (1.0 + _lineSpacing);
// because _lineCount is the max vertical no. of characters....
_lineCount = (_lineCount >linelength)?_lineCount:linelength;
break;
@ -503,7 +500,7 @@ void Text3D::computePositions(unsigned int contextID) const
osg::Matrix& matrix = atc._matrix;
osg::Vec3 scaleVec(_characterHeight, _characterHeight / _characterAspectRatio, _characterHeight);
osg::Vec3 scaleVec(_characterHeight, _characterHeight / getCharacterAspectRatio(), _characterHeight);
matrix.makeTranslate(-_offset);
matrix.postMultScale(scaleVec);

View File

@ -33,7 +33,6 @@ using namespace osgText;
TextBase::TextBase():
_fontSize(32,32),
_characterHeight(32),
_characterAspectRatio(1.0f),
_characterSizeMode(OBJECT_COORDS),
_maximumWidth(0.0f),
_maximumHeight(0.0f),
@ -56,9 +55,9 @@ TextBase::TextBase():
TextBase::TextBase(const TextBase& textBase,const osg::CopyOp& copyop):
osg::Drawable(textBase,copyop),
_font(textBase._font),
_style(textBase._style),
_fontSize(textBase._fontSize),
_characterHeight(textBase._characterHeight),
_characterAspectRatio(textBase._characterAspectRatio),
_characterSizeMode(textBase._characterSizeMode),
_maximumWidth(textBase._maximumWidth),
_maximumHeight(textBase._maximumHeight),
@ -104,20 +103,27 @@ void TextBase::setFont(const std::string& fontfile)
setFont(readRefFontFile(fontfile));
}
void TextBase::setFontResolution(unsigned int width, unsigned int height)
{
_fontSize = FontResolution(width,height);
computeGlyphRepresentation();
}
void TextBase::setCharacterSize(float height,float aspectRatio)
void TextBase::setCharacterSize(float height)
{
_characterHeight = height;
_characterAspectRatio = aspectRatio;
computeGlyphRepresentation();
}
void TextBase::setCharacterSize(float height, float aspectRatio)
{
if (getCharacterAspectRatio()!=aspectRatio)
{
getOrCreateStyle()->setWidthRatio(aspectRatio);
}
setCharacterSize(height);
}
void TextBase::setMaximumWidth(float maximumWidth)
{
_maximumWidth = maximumWidth;