From 2718058173d70559ff9c8a428f661b635f137d13 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 2 Nov 2004 11:10:44 +0000 Subject: [PATCH] Removed the font cache from the FreeType plugin, moving the support across to osgDB::Registry. --- examples/osgarchive/osgarchive.cpp | 60 ++++++++++++++++++++- src/osgPlugins/freetype/FreeTypeLibrary.cpp | 7 +++ src/osgPlugins/freetype/FreeTypeLibrary.h | 5 ++ src/osgText/Font.cpp | 2 +- 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/examples/osgarchive/osgarchive.cpp b/examples/osgarchive/osgarchive.cpp index 2c4fa6996..78a2d82db 100644 --- a/examples/osgarchive/osgarchive.cpp +++ b/examples/osgarchive/osgarchive.cpp @@ -16,11 +16,67 @@ #include #include -#include + + #include + #include + #include + #include + +template > +class proxy_basic_streambuf : public std::basic_streambuf<_CharT, _Traits> +{ + public: + + typedef _CharT char_type; + typedef _Traits traits_type; + typedef typename traits_type::int_type int_type; + typedef typename traits_type::pos_type pos_type; + typedef typename traits_type::off_type off_type; + + proxy_basic_streambuf(unsigned int numChars): + _numChars(numChars) {} + + /// Destructor deallocates no buffer space. + virtual ~proxy_basic_streambuf() {} + + unsigned int _numChars; + + protected: + + virtual int_type overflow (int_type __c = traits_type::eof()) + { + if (_numChars==0) + { + putchar('E'); + return traits_type::eof(); + } + + if (__c == traits_type::eof()) return '\n'; traits_type::not_eof(__c); + //putchar(''); + --_numChars; + return putchar(__c); + } +}; + +typedef proxy_basic_streambuf proxy_streambuf; int main( int argc, char **argv ) { + std::ifstream fin("GNUmakefile"); + std::istream& ins = fin; + proxy_streambuf mystreambuf(100); + std::cout.rdbuf(&mystreambuf); + + while (!fin.eof()) + { + std::cout.put(fin.get()); + } + std::cout<<"Exiting normally "<second.get(); font->setImplementation(0); } +#endif FT_Done_FreeType( _ftlibrary); } @@ -51,8 +53,10 @@ FreeTypeLibrary* FreeTypeLibrary::instance() 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 ); @@ -71,7 +75,10 @@ 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 return font; diff --git a/src/osgPlugins/freetype/FreeTypeLibrary.h b/src/osgPlugins/freetype/FreeTypeLibrary.h index 8c6363048..26253159a 100644 --- a/src/osgPlugins/freetype/FreeTypeLibrary.h +++ b/src/osgPlugins/freetype/FreeTypeLibrary.h @@ -17,6 +17,8 @@ #include "FreeTypeFont.h" #include +//#define USE_LOCAL_CACHE + class FreeTypeLibrary { public: @@ -39,7 +41,10 @@ protected: FT_Library _ftlibrary; + +#ifdef USE_LOCAL_CACHE FontMap _fontMap; +#endif }; diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index 0f0d262d7..a9b1ad9e8 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -67,7 +67,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); + osg::Object* object = osgDB::readObjectFile(foundFile, osgDB::Registry::CACHE_OBJECTS); // if the object is a font then return it. osgText::Font* font = dynamic_cast(object);