Moved set/getFont from Text/Text3D into TextBase

This commit is contained in:
Robert Osfield
2010-09-29 11:34:55 +00:00
parent e942cc770a
commit b40acacf53
12 changed files with 78 additions and 149 deletions

View File

@@ -38,30 +38,12 @@ public:
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)); };
/** Set the Font to use to render the text.*/
void setFont(osg::ref_ptr<Font> font);
/** Set the font, loaded from the specified front file, to use to render the text,
* setFont("") sets the use of the default font.
* See the osgText::readFontFile function for how the font file will be located. */
void setFont(const std::string& fontfile);
/** Get the font. Return 0 if default is being used.*/
const Font* getFont() const { return _font.get(); }
void setColor(const osg::Vec4& color);
const osg::Vec4& getColor() const { return _color; }
/**
* Turns off writing to the depth buffer when rendering text. This only affects text
* with no backdrop or text using the DELAYED_DEPTH_WRITES implementation, since
@@ -307,9 +289,6 @@ public:
* for all graphics contexts. */
virtual void releaseGLObjects(osg::State* state=0) const;
// // make Font a friend to allow it set the _font to 0 if the font is
// // forcefully unloaded.
// friend class Font;
public:
// internal structures, variable and methods used for rendering of characters.
@@ -374,7 +353,6 @@ protected:
String::iterator computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last);
// members which have public access.
osg::ref_ptr<Font> _font;
osg::Vec4 _color;
// iternal map used for rendering. Set up by the computeGlyphRepresentation() method.

View File

@@ -16,7 +16,6 @@
#include <osgText/TextBase>
#include <osgText/Font>
#include <osgText/Style>
namespace osgText {
@@ -82,19 +81,6 @@ public:
// void setBackStateSet(osg::StateSet * backStateSet) { _backStateSet = backStateSet; }
//
/** Set the Font to use to render the text.
* setFont(0) sets the use of the default font.*/
void setFont(Font* font);
/** Set the font, loaded from the specified front file, to use to render the text,
* setFont("") sets the use of the default font.
* See the osgText::readFontFile function for how the font file will be located. */
void setFont(const std::string& fontfile);
/** Get the font. Return 0 if default is being used.*/
const Font* getFont() const { return _font.get(); }
/** Draw the text.*/
@@ -162,7 +148,6 @@ protected:
TextRenderInfo _textRenderInfo;
osg::ref_ptr<Font> _font;
osg::ref_ptr<Style> _style;
RenderMode _renderMode;

View File

@@ -18,6 +18,7 @@
#include <osgText/String>
#include <osgText/KerningType>
#include <osgText/Font>
namespace osgText {
@@ -35,7 +36,21 @@ 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)); };
/** Set the Font to use to render the text.*/
void setFont(osg::ref_ptr<Font> font);
/** Set the font, loaded from the specified front file, to use to render the text,
* setFont("") sets the use of the default font.
* See the osgText::readFontFile function for how the font file will be located. */
void setFont(const std::string& fontfile);
/** Get the font. Return 0 if default is being used.*/
const Font* getFont() const { return _font.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.*/
@@ -248,6 +263,7 @@ protected:
// members which have public access.
osg::ref_ptr<Font> _font;
FontResolution _fontSize;
float _characterHeight;
float _characterAspectRatio;

View File

@@ -46,7 +46,6 @@ Text::Text():
Text::Text(const Text& text,const osg::CopyOp& copyop):
osgText::TextBase(text,copyop),
_font(text._font),
_color(text._color),
_enableDepthWrites(text._enableDepthWrites),
_backdropType(text._backdropType),
@@ -67,28 +66,6 @@ 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 );
}
_font = font;
computeGlyphRepresentation();
}
void Text::setFont(const std::string& fontfile)
{
setFont(readRefFontFile(fontfile));
}
void Text::setColor(const osg::Vec4& color)
{

View File

@@ -19,7 +19,6 @@ namespace osgText
{
Text3D::Text3D():
_font(0),
_style(0),
_renderMode(PER_GLYPH)
{
@@ -27,7 +26,6 @@ Text3D::Text3D():
Text3D::Text3D(const Text3D & text3D, const osg::CopyOp & copyop):
osgText::TextBase(text3D, copyop),
_font(text3D._font),
_style(text3D._style),
_renderMode(text3D._renderMode)
{
@@ -116,19 +114,6 @@ void Text3D::accept(osg::PrimitiveFunctor& pf) const
}
}
void Text3D::setFont(Font* font)
{
_font = font;
computeGlyphRepresentation();
}
void Text3D::setFont(const std::string & fontfile)
{
osg::ref_ptr<Font> font = readRefFontFile(fontfile);
setFont(font.get());
}
String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last)
{
if (_font.valid() == false) return last;

View File

@@ -55,6 +55,7 @@ TextBase::TextBase():
TextBase::TextBase(const TextBase& textBase,const osg::CopyOp& copyop):
osg::Drawable(textBase,copyop),
_font(textBase._font),
_fontSize(textBase._fontSize),
_characterHeight(textBase._characterHeight),
_characterAspectRatio(textBase._characterAspectRatio),
@@ -81,6 +82,29 @@ TextBase::~TextBase()
{
}
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();
}
void TextBase::setFont(const std::string& fontfile)
{
setFont(readRefFontFile(fontfile));
}
void TextBase::setFontResolution(unsigned int width, unsigned int height)
{
_fontSize = FontResolution(width,height);

View File

@@ -1,5 +1,4 @@
#include <osgText/Text>
#include <osgText/Font>
#include <iostream>
#include <string>
@@ -104,14 +103,6 @@ bool Text_readLocalData(osg::Object &obj, osgDB::Input &fr)
bool itAdvanced = false;
if (fr.matchSequence("font %w"))
{
text.setFont(fr[1].getStr());
fr += 2;
itAdvanced = true;
}
// color
if (fr[0].matchWord("color"))
{
@@ -262,11 +253,6 @@ bool Text_writeLocalData(const osg::Object &obj, osgDB::Output &fw)
{
const osgText::Text &text = static_cast<const osgText::Text &>(obj);
if (text.getFont())
{
fw.indent() << "font " << text.getFont()->getFileName() << std::endl;
}
// color
osg::Vec4 c = text.getColor();
fw.indent() << "color " << c.x() << " " << c.y() << " " << c.z() << " " << c.w() << std::endl;

View File

@@ -1,5 +1,4 @@
#include <osgText/Text3D>
#include <osgText/Font>
#include <iostream>
#include <string>
@@ -46,14 +45,6 @@ bool Text3D_readLocalData(osg::Object &obj, osgDB::Input &fr)
osgText::Text3D &text = static_cast<osgText::Text3D &>(obj);
bool itAdvanced = false;
// font
if (fr.matchSequence("font %w"))
{
text.setFont(fr[1].getStr());
fr += 2;
itAdvanced = true;
}
// characterDepth
if (fr[0].matchWord("characterDepth"))
@@ -86,11 +77,6 @@ bool Text3D_writeLocalData(const osg::Object &obj, osgDB::Output &fw)
{
const osgText::Text3D &text = static_cast<const osgText::Text3D &>(obj);
if (text.getFont())
{
fw.indent() << "font " << text.getFont()->getFileName() << std::endl;
}
fw.indent() << "characterDepth " << text.getCharacterDepth() << std::endl;
fw.indent() << "renderMode " << convertRenderModeEnumToString(text.getRenderMode()) << std::endl;

View File

@@ -30,6 +30,13 @@ bool TextBase_readLocalData(osg::Object &obj, osgDB::Input &fr)
osgText::Text &text = static_cast<osgText::Text &>(obj);
bool itAdvanced = false;
if (fr.matchSequence("font %w"))
{
text.setFont(fr[1].getStr());
fr += 2;
itAdvanced = true;
}
if (fr[0].matchWord("fontResolution") || fr[0].matchWord("fontSize"))
{
unsigned int width;
@@ -281,6 +288,11 @@ bool TextBase_writeLocalData(const osg::Object &obj, osgDB::Output &fw)
{
const osgText::Text &text = static_cast<const osgText::Text &>(obj);
if (text.getFont())
{
fw.indent() << "font " << text.getFont()->getFileName() << std::endl;
}
// font resolution
fw.indent() << "fontResolution " << text.getFontWidth() << " " << text.getFontHeight() << std::endl;

View File

@@ -3,26 +3,6 @@
#include <osgDB/InputStream>
#include <osgDB/OutputStream>
// _font
static bool checkFont( const osgText::Text& text )
{
return text.getFont()!=NULL;
}
static bool readFont( osgDB::InputStream& is, osgText::Text& text )
{
std::string fontName; is.readWrappedString( fontName );
text.setFont( osgText::readFontFile(fontName) );
return true;
}
static bool writeFont( osgDB::OutputStream& os, const osgText::Text& text )
{
os.writeWrappedString( text.getFont()->getFileName() );
os << std::endl;
return true;
}
// _backdropHorizontalOffset, _backdropVerticalOffset
static bool checkBackdropOffset( const osgText::Text& text )
{
@@ -78,9 +58,8 @@ REGISTER_OBJECT_WRAPPER( osgText_Text,
osgText::Text,
"osg::Object osg::Drawable osgText::TextBase osgText::Text" )
{
ADD_USER_SERIALIZER( Font ); // _font
ADD_VEC4_SERIALIZER( Color, osg::Vec4() ); // _color
BEGIN_ENUM_SERIALIZER( BackdropType, NONE );
ADD_ENUM_VALUE( DROP_SHADOW_BOTTOM_RIGHT );
ADD_ENUM_VALUE( DROP_SHADOW_CENTER_RIGHT );

View File

@@ -3,33 +3,13 @@
#include <osgDB/InputStream>
#include <osgDB/OutputStream>
static bool checkFont( const osgText::Text3D& text )
{
return text.getFont()!=NULL;
}
static bool readFont( osgDB::InputStream& is, osgText::Text3D& text )
{
std::string fontName; is.readWrappedString( fontName );
text.setFont( osgText::readFontFile(fontName) );
return true;
}
static bool writeFont( osgDB::OutputStream& os, const osgText::Text3D& text )
{
os.writeWrappedString( text.getFont()->getFileName() );
os << std::endl;
return true;
}
REGISTER_OBJECT_WRAPPER( osgText_Text3D,
new osgText::Text3D,
osgText::Text3D,
"osg::Object osg::Drawable osgText::TextBase osgText::Text3D" )
{
ADD_FLOAT_SERIALIZER( CharacterDepth, 1.0f ); // _characterDepth
ADD_USER_SERIALIZER( Font ); // _font
BEGIN_ENUM_SERIALIZER( RenderMode, PER_GLYPH );
ADD_ENUM_VALUE( PER_FACE );
ADD_ENUM_VALUE( PER_GLYPH );

View File

@@ -3,6 +3,26 @@
#include <osgDB/InputStream>
#include <osgDB/OutputStream>
// _font
static bool checkFont( const osgText::TextBase& text )
{
return text.getFont()!=NULL;
}
static bool readFont( osgDB::InputStream& is, osgText::TextBase& text )
{
std::string fontName; is.readWrappedString( fontName );
text.setFont( osgText::readFontFile(fontName) );
return true;
}
static bool writeFont( osgDB::OutputStream& os, const osgText::TextBase& text )
{
os.writeWrappedString( text.getFont()->getFileName() );
os << std::endl;
return true;
}
// _fontSize
static bool checkFontSize( const osgText::TextBase& text )
{
@@ -154,6 +174,7 @@ REGISTER_OBJECT_WRAPPER( osgText_TextBase,
osgText::TextBase,
"osg::Object osg::Drawable osgText::TextBase" )
{
ADD_USER_SERIALIZER( Font ); // _font
ADD_USER_SERIALIZER( FontSize ); // _fontSize
ADD_USER_SERIALIZER( CharacterSize ); // _characterHeight, _characterAspectRatio