diff --git a/src/osgPlugins/freetype/FreeTypeFont.cpp b/src/osgPlugins/freetype/FreeTypeFont.cpp index 83c78d865..be3cb1526 100644 --- a/src/osgPlugins/freetype/FreeTypeFont.cpp +++ b/src/osgPlugins/freetype/FreeTypeFont.cpp @@ -12,6 +12,7 @@ */ #include "FreeTypeFont.h" +#include "FreeTypeLibrary.h" #include #include @@ -24,11 +25,20 @@ FreeTypeFont::FreeTypeFont(const std::string& filename, FT_Face face): FreeTypeFont::~FreeTypeFont() { - if(_face) - { - FT_Done_Face(_face); - _face = 0; - } + if(_face) + { + FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance(); + if (freeTypeLibrary) + { + // remove myself from the local regsitry to ensure that + // not dangling pointers remain + freeTypeLibrary->removeFontImplmentation(this); + + // free the freetype font face itself + FT_Done_Face(_face); + _face = 0; + } + } } void FreeTypeFont::setFontResolution(unsigned int width, unsigned int height) diff --git a/src/osgPlugins/freetype/FreeTypeFont.h b/src/osgPlugins/freetype/FreeTypeFont.h index b18c51cdc..3609b6741 100644 --- a/src/osgPlugins/freetype/FreeTypeFont.h +++ b/src/osgPlugins/freetype/FreeTypeFont.h @@ -34,7 +34,7 @@ public: virtual osgText::Font::Glyph* getGlyph(unsigned int charcode); - virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType _kerningType); + virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType _kerningType); virtual bool hasVertical() const; diff --git a/src/osgPlugins/freetype/FreeTypeLibrary.cpp b/src/osgPlugins/freetype/FreeTypeLibrary.cpp index 27501e609..0061e54cd 100644 --- a/src/osgPlugins/freetype/FreeTypeLibrary.cpp +++ b/src/osgPlugins/freetype/FreeTypeLibrary.cpp @@ -26,38 +26,31 @@ FreeTypeLibrary::FreeTypeLibrary() FreeTypeLibrary::~FreeTypeLibrary() { -#ifdef USE_LOCAL_CACHE // 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) + while(!_fontImplementationSet.empty()) { - osgText::Font* font = itr->second.get(); + FreeTypeFont* fontImplementation = *_fontImplementationSet.begin(); + _fontImplementationSet.erase(_fontImplementationSet.begin()); + osgText::Font* font = fontImplementation->_facade; font->setImplementation(0); } -#endif FT_Done_FreeType( _ftlibrary); } FreeTypeLibrary* FreeTypeLibrary::instance() { - static FreeTypeLibrary s_library; - return &s_library; + static osg::ref_ptr s_library = new FreeTypeLibrary; + return s_library.get(); } osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int index) { -#ifdef USE_LOCAL_CACHE - FontMap::iterator itr = _fontMap.find(fontfile); - if (itr!=_fontMap.end()) return itr->second.get(); -#endif - FT_Face face; /* handle to face object */ FT_Error error = FT_New_Face( _ftlibrary, fontfile.c_str(), index, &face ); if (error == FT_Err_Unknown_File_Format) @@ -76,9 +69,7 @@ osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile,unsigned int FreeTypeFont* fontImp = new FreeTypeFont(fontfile,face); osgText::Font* font = new osgText::Font(fontImp); -#ifdef USE_LOCAL_CACHE - _fontMap[fontfile]=font; -#endif + _fontImplementationSet.insert(fontImp); return font; diff --git a/src/osgPlugins/freetype/FreeTypeLibrary.h b/src/osgPlugins/freetype/FreeTypeLibrary.h index ad8122fba..4a385fc93 100644 --- a/src/osgPlugins/freetype/FreeTypeLibrary.h +++ b/src/osgPlugins/freetype/FreeTypeLibrary.h @@ -16,10 +16,9 @@ #include "FreeTypeFont.h" #include +#include -//#define USE_LOCAL_CACHE - -class FreeTypeLibrary +class FreeTypeLibrary : public osg::Referenced { public: @@ -30,6 +29,8 @@ public: static FreeTypeLibrary* instance(); osgText::Font* getFont(const std::string& fontfile,unsigned int index=0); + + void removeFontImplmentation(FreeTypeFont* fontImpl) { _fontImplementationSet.erase(fontImpl); } protected: @@ -37,14 +38,11 @@ protected: * library is via the singleton instance method.*/ FreeTypeLibrary(); - typedef std::map< std::string, osg::ref_ptr > FontMap; + typedef std::set< FreeTypeFont* > FontImplementationSet; - FT_Library _ftlibrary; - -#ifdef USE_LOCAL_CACHE - FontMap _fontMap; -#endif + FT_Library _ftlibrary; + FontImplementationSet _fontImplementationSet; }; diff --git a/src/osgPlugins/freetype/ReaderWriterFreeType.cpp b/src/osgPlugins/freetype/ReaderWriterFreeType.cpp index d65fc618e..812a2475d 100644 --- a/src/osgPlugins/freetype/ReaderWriterFreeType.cpp +++ b/src/osgPlugins/freetype/ReaderWriterFreeType.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "FreeTypeLibrary.h" @@ -30,9 +31,14 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter std::string fileName = osgDB::findDataFile( file, options ); if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; - osgText::Font* font = FreeTypeLibrary::instance()->getFont(fileName,0); - - return font; + FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance(); + if (!freeTypeLibrary) + { + osg::notify(osg::WARN)<<"Warning:: cannot create freetype font after freetype library has been deleted."<getFont(fileName,0); } };