Changed the freetype plugin so it keeps its own ref_ptr<Font> to fonts

that it creates, rather than have osgText::Font::readFontFile() use the
osgDB::Registry object cache.

This has been done to prevent problems when users flush the object cache,
which was causing a dangling pointer.
This commit is contained in:
Robert Osfield
2003-11-20 11:07:19 +00:00
parent 84989b06b3
commit 7136a00b9a
3 changed files with 13 additions and 18 deletions

View File

@@ -26,23 +26,17 @@ FreeTypeLibrary::FreeTypeLibrary()
FreeTypeLibrary::~FreeTypeLibrary()
{
// need to remove the implementations from the Fonts here
// just in case the Fonts continue to have external references
// to them, otherwise they will try to point to an object thats
// definiation has been unloaded along with the unload of the FreeType
// plugin.
for(FontMap::iterator itr=_fontMap.begin();
itr!=_fontMap.end();
++itr)
{
FreeTypeFont* freetypefont = itr->second.get();
if (freetypefont->referenceCount()>1)
{
// external references must exist...
itr->second = 0;
freetypefont->_facade->setImplementation(0);
}
else
{
// no external references exist so its safe to delete via set the ref_ptr to 0.
itr->second = 0;
}
osgText::Font* font = itr->second.get();
font->setImplementation(0);
}
FT_Done_FreeType( _ftlibrary);
@@ -58,7 +52,7 @@ osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int
{
FontMap::iterator itr = _fontMap.find(fontfile);
if (itr!=_fontMap.end()) return itr->second->_facade;
if (itr!=_fontMap.end()) return itr->second.get();
FT_Face face; /* handle to face object */
FT_Error error = FT_New_Face( _ftlibrary, fontfile.c_str(), index, &face );
@@ -76,9 +70,9 @@ osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int
}
FreeTypeFont* fontImp = new FreeTypeFont(fontfile,face);
_fontMap[fontfile]=fontImp;
osgText::Font* font = new osgText::Font(fontImp);
_fontMap[fontfile]=font;
return font;
}

View File

@@ -15,6 +15,7 @@
#define OSGTEXT_LIBRARY
#include "FreeTypeFont.h"
#include <osgText/Font>
class FreeTypeLibrary
{
@@ -34,7 +35,7 @@ protected:
* library is via the singleton instance method.*/
FreeTypeLibrary();
typedef std::map< std::string, osg::ref_ptr<FreeTypeFont> > FontMap;
typedef std::map< std::string, osg::ref_ptr<osgText::Font> > FontMap;
FT_Library _ftlibrary;

View File

@@ -65,7 +65,7 @@ osgText::Font* osgText::readFontFile(const std::string& filename)
std::string foundFile = findFontFile(filename);
if (foundFile.empty()) return 0;
osg::Object* object = osgDB::readObjectFile(foundFile,true);
osg::Object* object = osgDB::readObjectFile(foundFile);
// if the object is a font then return it.
osgText::Font* font = dynamic_cast<osgText::Font*>(object);