2001-11-12 18:04:57 +08:00
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
*
|
2001-11-14 22:09:07 +08:00
|
|
|
* openscenegraph textLib / FTGL
|
2001-11-12 18:04:57 +08:00
|
|
|
*
|
|
|
|
* --------------------------------------------------------------------------
|
2001-11-14 22:09:07 +08:00
|
|
|
*
|
|
|
|
* prog: max rheiner;mrn@paus.ch
|
|
|
|
* date: 4/25/2001 (m/d/y)
|
2001-11-12 18:04:57 +08:00
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <osgText/Font>
|
|
|
|
|
2001-11-19 18:40:42 +08:00
|
|
|
#include <osg/Notify>
|
2001-11-12 18:04:57 +08:00
|
|
|
#include <osgDB/FileUtils>
|
|
|
|
|
|
|
|
#include "FTFace.h"
|
|
|
|
#include "FTGLBitmapFont.h"
|
|
|
|
#include "FTGLPixmapFont.h"
|
|
|
|
#include "FTGLOutlineFont.h"
|
|
|
|
#include "FTGLPolygonFont.h"
|
|
|
|
#include "FTGLTextureFont.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
using namespace osgText;
|
|
|
|
|
|
|
|
std::string findFontFile(const std::string& str)
|
|
|
|
{
|
|
|
|
// try looking in OSGFILEPATH etc first for fonts.
|
2002-06-18 05:50:37 +08:00
|
|
|
std::string filename = osgDB::findDataFile(str);
|
|
|
|
if (!filename.empty()) return std::string(filename);
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2002-05-10 23:42:27 +08:00
|
|
|
|
2002-06-18 05:50:37 +08:00
|
|
|
static osgDB::FilePathList s_FontFilePath;
|
|
|
|
static bool initialized = false;
|
|
|
|
if (!initialized)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2002-06-18 05:50:37 +08:00
|
|
|
initialized = true;
|
|
|
|
#if defined(WIN32)
|
|
|
|
osgDB::Registry::convertStringPathIntoFilePathList(
|
|
|
|
".;C:/winnt/fonts;C:/windows/fonts",
|
|
|
|
s_FontFilePath);
|
|
|
|
|
|
|
|
char *ptr;
|
|
|
|
if ((ptr = getenv( "windir" )))
|
|
|
|
{
|
|
|
|
s_FontFilePath.push_back(ptr);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
osgDB::Registry::convertStringPathIntoFilePathList(
|
|
|
|
".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives",
|
|
|
|
s_FontFilePath);
|
|
|
|
#endif
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2002-06-18 05:50:37 +08:00
|
|
|
|
|
|
|
filename = osgDB::findFileInPath(str,s_FontFilePath);
|
|
|
|
if (!filename.empty()) return filename;
|
|
|
|
|
2001-12-24 22:12:38 +08:00
|
|
|
osg::notify(osg::WARN)<<"Warning: font file \""<<str<<"\" not found."<<std::endl;
|
2001-11-12 18:04:57 +08:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Font
|
|
|
|
Font::
|
|
|
|
Font()
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
_init=false;
|
|
|
|
_font=NULL;
|
|
|
|
_created=false;
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
_pointSize=14;
|
2002-06-12 02:41:57 +08:00
|
|
|
_textureSize=0;
|
2001-11-14 22:09:07 +08:00
|
|
|
_res=72;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Font::
|
|
|
|
init(const std::string& font)
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
_font=NULL;
|
|
|
|
_created=false;
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
open(font);
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
if(_font!=NULL)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Font::
|
|
|
|
~Font()
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
clear();
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2002-06-12 02:41:57 +08:00
|
|
|
void Font::copyAndInvalidate(Font &dest)
|
|
|
|
{
|
|
|
|
// delete destination's font object
|
|
|
|
delete dest._font;
|
|
|
|
|
|
|
|
// copy local data to destination object
|
|
|
|
dest._init = _init;
|
|
|
|
dest._created = _created;
|
|
|
|
dest._font = _font;
|
|
|
|
dest._fontName = _fontName;
|
|
|
|
dest._pointSize = _pointSize;
|
|
|
|
dest._res = _res;
|
|
|
|
dest._textureSize = _textureSize;
|
|
|
|
|
|
|
|
// invalidate this object
|
|
|
|
_init = false;
|
|
|
|
_created = false;
|
|
|
|
_font = 0;
|
|
|
|
_fontName = std::string();
|
|
|
|
}
|
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
bool Font::
|
|
|
|
open(const std::string& font)
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
clear();
|
2001-11-12 18:04:57 +08:00
|
|
|
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
std::string filename = findFontFile(font);
|
|
|
|
if (filename.empty()) return false;
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2001-11-14 22:09:07 +08:00
|
|
|
_font=createFontObj();
|
|
|
|
if( _font!=NULL && _font->Open(filename.c_str()) )
|
|
|
|
{
|
|
|
|
_init=true;
|
|
|
|
_fontName=font;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
bool Font::open(const char* font)
|
|
|
|
{
|
|
|
|
return open(std::string(font));
|
|
|
|
}
|
2002-01-19 06:25:51 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
bool Font::
|
2002-09-02 20:31:35 +08:00
|
|
|
create(osg::State& state,int pointSize,unsigned int res)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
_pointSize=pointSize;
|
|
|
|
_res=res;
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2002-01-19 06:25:51 +08:00
|
|
|
return create(state);
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
bool Font::create(osg::State& state)
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(_init)
|
|
|
|
{
|
2002-01-19 06:25:51 +08:00
|
|
|
if(_font->Created(state.getContextID()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(_font->FaceSize(_pointSize,_res,state.getContextID()))
|
2001-11-14 22:09:07 +08:00
|
|
|
{
|
|
|
|
_created=true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
2002-11-06 23:43:11 +08:00
|
|
|
void Font::output(osg::State& state,const char* text) const
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(_created)
|
2002-01-19 06:25:51 +08:00
|
|
|
_font->render(text,state.getContextID());
|
2001-11-14 22:09:07 +08:00
|
|
|
else
|
2002-11-06 23:43:11 +08:00
|
|
|
{
|
|
|
|
// ahhhh, this is bit doddy, the draw is potentially
|
|
|
|
// modifying the text object, this isn't thread safe.
|
|
|
|
Font* this_non_const = const_cast<Font*>(this);
|
|
|
|
this_non_const->create(state,_pointSize);
|
|
|
|
}
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
void Font::clear()
|
2001-11-12 18:04:57 +08:00
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
_init=false;
|
|
|
|
|
|
|
|
if(_font)
|
|
|
|
{
|
2002-03-27 07:52:52 +08:00
|
|
|
osgDelete _font;
|
2001-11-14 22:09:07 +08:00
|
|
|
_font=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fontName="";
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Font::
|
|
|
|
getWidth(const char* text) const
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(_init && _created)
|
|
|
|
return _font->Advance(text);
|
|
|
|
else
|
|
|
|
return -1;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int Font::
|
|
|
|
getHeight() const
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(_init && _created)
|
|
|
|
return _pointSize;
|
|
|
|
else
|
|
|
|
return -1;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int Font::
|
|
|
|
getDescender() const
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(_init && _created)
|
|
|
|
return _font->Descender();
|
|
|
|
else
|
|
|
|
return -1;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int Font::
|
|
|
|
getAscender() const
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(_init && _created)
|
|
|
|
return _font->Ascender();
|
|
|
|
else
|
|
|
|
return -1;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Font
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BitmapFont
|
|
|
|
|
|
|
|
BitmapFont::
|
2001-11-14 22:09:07 +08:00
|
|
|
BitmapFont(const std::string& font,
|
|
|
|
int point_size):
|
2001-11-12 18:04:57 +08:00
|
|
|
RasterFont()
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(init(font))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_pointSize=point_size;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FTFont* BitmapFont::
|
|
|
|
createFontObj(void)
|
|
|
|
{
|
2002-03-27 07:52:52 +08:00
|
|
|
return (FTFont*)(osgNew FTGLBitmapFont);
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
// BitmapFont
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// PixmapFont
|
|
|
|
|
|
|
|
PixmapFont::
|
2001-11-14 22:09:07 +08:00
|
|
|
PixmapFont(const std::string& font,
|
|
|
|
int point_size):
|
2001-11-12 18:04:57 +08:00
|
|
|
RasterFont(font)
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(init(font))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_pointSize=point_size;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
FTFont* PixmapFont::
|
|
|
|
createFontObj(void)
|
|
|
|
{
|
2002-03-27 07:52:52 +08:00
|
|
|
return (FTFont*)(osgNew FTGLPixmapFont);
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
// PixmapFont
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2002-01-16 18:36:20 +08:00
|
|
|
// TextureFont
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
TextureFont::
|
2001-11-14 22:09:07 +08:00
|
|
|
TextureFont(const std::string& font,
|
|
|
|
int point_size):
|
2001-11-12 18:04:57 +08:00
|
|
|
RasterFont(font)
|
|
|
|
{
|
2002-06-12 02:41:57 +08:00
|
|
|
_textureSize=0;
|
2002-01-16 18:36:20 +08:00
|
|
|
if(init(font))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_pointSize=point_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TextureFont::
|
|
|
|
TextureFont(const std::string& font,
|
|
|
|
int point_size,
|
2002-06-12 02:41:57 +08:00
|
|
|
int textureSize ):
|
2002-01-16 18:36:20 +08:00
|
|
|
RasterFont(font)
|
|
|
|
{
|
2002-06-12 02:41:57 +08:00
|
|
|
_textureSize=textureSize;
|
2001-11-14 22:09:07 +08:00
|
|
|
if(init(font))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_pointSize=point_size;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
|
2002-01-16 18:36:20 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
FTFont* TextureFont::
|
|
|
|
createFontObj(void)
|
|
|
|
{
|
2002-03-27 07:52:52 +08:00
|
|
|
return (FTFont*)(osgNew FTGLTextureFont(_textureSize));
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2002-01-16 18:36:20 +08:00
|
|
|
// TextureFont
|
2001-11-12 18:04:57 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// _FTGLOutlineFont
|
|
|
|
|
|
|
|
OutlineFont::
|
2001-11-14 22:09:07 +08:00
|
|
|
OutlineFont(const std::string& font,
|
|
|
|
int point_size,
|
|
|
|
double precision):
|
2001-11-12 18:04:57 +08:00
|
|
|
VectorFont(font)
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(init(font))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_pointSize=point_size;
|
|
|
|
_precision=precision;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
FTFont* OutlineFont::
|
|
|
|
createFontObj(void)
|
|
|
|
{
|
2002-03-27 07:52:52 +08:00
|
|
|
return (FTFont*)(osgNew FTGLOutlineFont);
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
// _FTGLOutlineFont
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// PolygonFont
|
|
|
|
|
|
|
|
PolygonFont::
|
2001-11-14 22:09:07 +08:00
|
|
|
PolygonFont(const std::string& font,
|
|
|
|
int point_size,
|
|
|
|
double precision):
|
2001-11-12 18:04:57 +08:00
|
|
|
VectorFont(font)
|
|
|
|
{
|
2001-11-14 22:09:07 +08:00
|
|
|
if(init(font))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_pointSize=point_size;
|
|
|
|
_precision=precision;
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2002-01-19 06:25:51 +08:00
|
|
|
PolygonFont::
|
|
|
|
PolygonFont(const char* font,
|
|
|
|
int point_size,
|
|
|
|
double precision):
|
|
|
|
VectorFont(std::string(font))
|
|
|
|
{
|
|
|
|
if(init(font))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_pointSize=point_size;
|
|
|
|
_precision=precision;
|
|
|
|
}
|
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
FTFont* PolygonFont::
|
|
|
|
createFontObj(void)
|
|
|
|
{
|
2002-03-27 07:52:52 +08:00
|
|
|
return (FTFont*)(osgNew FTGLPolygonFont);
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|
2001-11-14 22:09:07 +08:00
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
|
|
|
|
// PolygonFont
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|