Added proper handling of freeing of font implememtation either when unloading
the freetype plugin or deleting osgText::Font first.
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
*/
|
||||
|
||||
#include "FreeTypeFont.h"
|
||||
#include "FreeTypeLibrary.h"
|
||||
|
||||
#include <osg/Notify>
|
||||
#include <osgDB/WriteFile>
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<FreeTypeLibrary> 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;
|
||||
|
||||
|
||||
@@ -16,10 +16,9 @@
|
||||
|
||||
#include "FreeTypeFont.h"
|
||||
#include <osgText/Font>
|
||||
#include <set>
|
||||
|
||||
//#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<osgText::Font> > FontMap;
|
||||
typedef std::set< FreeTypeFont* > FontImplementationSet;
|
||||
|
||||
|
||||
FT_Library _ftlibrary;
|
||||
|
||||
#ifdef USE_LOCAL_CACHE
|
||||
FontMap _fontMap;
|
||||
#endif
|
||||
FT_Library _ftlibrary;
|
||||
FontImplementationSet _fontImplementationSet;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <osgDB/FileNameUtils>
|
||||
#include <osgDB/FileUtils>
|
||||
#include <osgDB/Registry>
|
||||
#include <osg/Notify>
|
||||
|
||||
#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."<<std::endl;
|
||||
return ReadResult::ERROR_IN_READING_FILE;
|
||||
}
|
||||
|
||||
return freeTypeLibrary->getFont(fileName,0);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user