From fc9c43b48dc3d6036a7e4c0810aad34b82678a24 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 1 Jan 2016 14:33:17 -0600 Subject: [PATCH] Remove FontCache from globals. - access via a singleton instead. --- src/Cockpit/panel.cxx | 6 ++---- src/GUI/FGFontCache.cxx | 33 ++++++++++++++++++++++----------- src/GUI/FGFontCache.hxx | 14 +++++++++----- src/GUI/FGPUIDialog.cxx | 6 ++---- src/GUI/gui.cxx | 2 +- src/GUI/new_gui.cxx | 2 +- src/Instrumentation/HUD/HUD.cxx | 5 ++--- src/Main/globals.cxx | 3 --- src/Main/globals.hxx | 5 ----- src/Viewer/splash.cxx | 10 +++++----- 10 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/Cockpit/panel.cxx b/src/Cockpit/panel.cxx index 6123fab0c..dd2c6c154 100644 --- a/src/Cockpit/panel.cxx +++ b/src/Cockpit/panel.cxx @@ -1072,8 +1072,7 @@ FGTextLayer::draw (osg::State& state) glColor4fv(_color); transform(); - FGFontCache *fc = globals->get_fontcache(); - fntFont* font = fc->getTexFont(_font_name.c_str()); + fntFont* font = FGFontCache::instance()->getTexFont(_font_name); if (!font) { return; // don't crash on missing fonts } @@ -1139,8 +1138,7 @@ void FGTextLayer::setFontName(const std::string &name) { _font_name = name + ".txf"; - FGFontCache *fc = globals->get_fontcache(); - fntFont* font = fc->getTexFont(_font_name.c_str()); + fntFont* font = FGFontCache::instance()->getTexFont(_font_name); if (!font) { SG_LOG(SG_COCKPIT, SG_WARN, "unable to find font:" << name); } diff --git a/src/GUI/FGFontCache.cxx b/src/GUI/FGFontCache.cxx index 937eaeb78..e5a0752a3 100644 --- a/src/GUI/FGFontCache.cxx +++ b/src/GUI/FGFontCache.cxx @@ -34,6 +34,9 @@ // FGFontCache class. //////////////////////////////////////////////////////////////////////// +static std::auto_ptr global_fontCacheInstance; + + extern puFont FONT_HELVETICA_14; extern puFont FONT_SANS_12B; extern puFont FONT_HELVETICA_12; @@ -42,17 +45,17 @@ namespace { struct GuiFont { - const char *name; + const char* name; puFont *font; struct Predicate : public std::unary_function { - Predicate(const char* name_) : name(name_) {} + Predicate(const std::string& name_) : name(name_) {} bool operator() (const GuiFont& f1) const { - return ::strcmp(f1.name, name) == 0; + return (name == f1.name); } - const char* name; + const std::string name; }; }; @@ -73,6 +76,15 @@ const GuiFont guifonts[] = { const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])-1]; } +FGFontCache* FGFontCache::instance() +{ + if (!global_fontCacheInstance.get()) { + global_fontCacheInstance.reset(new FGFontCache); + } + + return global_fontCacheInstance.get(); +} + FGFontCache::FGFontCache() : _initialized(false) { @@ -101,9 +113,8 @@ inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1, } struct FGFontCache::fnt * -FGFontCache::getfnt(const char *name, float size, float slant) +FGFontCache::getfnt(const std::string& fontName, float size, float slant) { - std::string fontName(name); FntParams fntParams(fontName, size, slant); PuFontMap::iterator i = _puFonts.find(fntParams); if (i != _puFonts.end()) @@ -116,7 +127,7 @@ FGFontCache::getfnt(const char *name, float size, float slant) texfont = texi->second; } else { const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd, - GuiFont::Predicate(name)); + GuiFont::Predicate(fontName)); if (guifont != guifontsEnd) { pufont = guifont->font; } @@ -136,13 +147,13 @@ FGFontCache::getfnt(const char *name, float size, float slant) } puFont * -FGFontCache::get(const char *name, float size, float slant) +FGFontCache::get(const std::string& name, float size, float slant) { return getfnt(name, size, slant)->pufont; } fntTexFont * -FGFontCache::getTexFont(const char *name, float size, float slant) +FGFontCache::getTexFont(const std::string& name, float size, float slant) { init(); return getfnt(name, size, slant)->texfont; @@ -176,11 +187,11 @@ void FGFontCache::init() } SGPath -FGFontCache::getfntpath(const char *name) +FGFontCache::getfntpath(const std::string& name) { init(); SGPath path(_path); - if (name && std::string(name) != "") { + if (name != "") { path.append(name); if (path.exists()) return path; diff --git a/src/GUI/FGFontCache.hxx b/src/GUI/FGFontCache.hxx index a07aa7b44..77bab6b5b 100644 --- a/src/GUI/FGFontCache.hxx +++ b/src/GUI/FGFontCache.hxx @@ -31,6 +31,8 @@ class fntTexFont; */ class FGFontCache { private: + FGFontCache(); // private constructor, use singleton instance() accessor + // The parameters of a request to the cache. struct FntParams { @@ -70,19 +72,21 @@ private: PuFontMap _puFonts; bool _initialized; - struct fnt *getfnt(const char *name, float size, float slant); + struct fnt *getfnt(const std::string& name, float size, float slant); void init(); public: - FGFontCache(); + // note this accesor is NOT thread-safe + static FGFontCache* instance(); + ~FGFontCache(); - puFont *get(const char *name, float size=15.0, float slant=0.0); + puFont *get(const std::string& name, float size=15.0, float slant=0.0); puFont *get(SGPropertyNode *node); - fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0); + fntTexFont *getTexFont(const std::string& name, float size=15.0, float slant=0.0); - SGPath getfntpath(const char *name); + SGPath getfntpath(const std::string& name); /** * Preload all the fonts in the FlightGear font directory. It is * important to load the font textures early, with the proper diff --git a/src/GUI/FGPUIDialog.cxx b/src/GUI/FGPUIDialog.cxx index ead9cc91c..30cbef75d 100644 --- a/src/GUI/FGPUIDialog.cxx +++ b/src/GUI/FGPUIDialog.cxx @@ -796,8 +796,7 @@ FGPUIDialog::display (SGPropertyNode *props) SGPropertyNode *fontnode = props->getNode("font"); if (fontnode) { - FGFontCache *fc = globals->get_fontcache(); - _font = fc->get(fontnode); + _font = FGFontCache::instance()->get(fontnode); } else { _font = _gui->getDefaultFont(); } @@ -1092,8 +1091,7 @@ FGPUIDialog::setupObject (puObject *object, SGPropertyNode *props) object->setBorderThickness( props->getIntValue("border", 2) ); if (SGPropertyNode *nft = props->getNode("font", false)) { - FGFontCache *fc = globals->get_fontcache(); - puFont *lfnt = fc->get(nft); + puFont *lfnt = FGFontCache::instance()->get(nft); object->setLabelFont(*lfnt); object->setLegendFont(*lfnt); } else { diff --git a/src/GUI/gui.cxx b/src/GUI/gui.cxx index 2c5ae2345..b8a8f333a 100644 --- a/src/GUI/gui.cxx +++ b/src/GUI/gui.cxx @@ -79,7 +79,7 @@ public: puSetDefaultStyle ( PUSTYLE_SMALL_SHADED ); //PUSTYLE_DEFAULT puSetDefaultColourScheme (0.8, 0.8, 0.9, 1); - FGFontCache *fc = globals->get_fontcache(); + FGFontCache *fc = FGFontCache::instance(); fc->initializeFonts(); puFont *GuiFont = fc->get(globals->get_locale()->getDefaultFont("typewriter.txf"), diff --git a/src/GUI/new_gui.cxx b/src/GUI/new_gui.cxx index d2eb8b140..bb8eb52db 100644 --- a/src/GUI/new_gui.cxx +++ b/src/GUI/new_gui.cxx @@ -415,7 +415,7 @@ NewGUI::setStyle (void) void NewGUI::setupFont (SGPropertyNode *node) { - _font = globals->get_fontcache()->get(node); + _font = FGFontCache::instance()->get(node); puSetDefaultFonts(*_font, *_font); return; } diff --git a/src/Instrumentation/HUD/HUD.cxx b/src/Instrumentation/HUD/HUD.cxx index 766b0fed1..545042e95 100644 --- a/src/Instrumentation/HUD/HUD.cxx +++ b/src/Instrumentation/HUD/HUD.cxx @@ -130,11 +130,10 @@ HUD::~HUD() void HUD::init() { - const char* fontName = 0; - _font_cache = globals->get_fontcache(); + std::string fontName; if (!_font) { fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf"); - _font = _font_cache->getTexFont(fontName); + _font = FGFontCache::instance()->getTexFont(fontName); } if (!_font) throw sg_io_exception("/sim/hud/font/name is not a texture font", diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index 8e8a346a7..42a27bd46 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -48,7 +48,6 @@ #include #include #include -#include #include #include #include @@ -160,7 +159,6 @@ FGGlobals::FGGlobals() : commands( SGCommandMgr::instance() ), channel_options_list( NULL ), initial_waypoints( NULL ), - fontcache ( new FGFontCache ), channellist( NULL ), haveUserSettings(false), _chatter_queue(NULL) @@ -237,7 +235,6 @@ FGGlobals::~FGGlobals() delete channel_options_list; delete initial_waypoints; - delete fontcache; delete channellist; simgear::PropertyObjectBase::setDefaultRoot(NULL); diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index 5c82b364a..4951792c9 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -63,7 +63,6 @@ class FGTileMgr; class FGViewMgr; class FGViewer; class FGRenderer; -class FGFontCache; class FGSampleQueue; namespace simgear { namespace pkg { @@ -124,8 +123,6 @@ private: // and or flight-plan file during initialization string_list *initial_waypoints; - FGFontCache *fontcache; - // Navigational Aids FGTACANList *channellist; @@ -330,8 +327,6 @@ public: FGScenery * get_scenery () const; FGTileMgr * get_tile_mgr () const; - - inline FGFontCache *get_fontcache() const { return fontcache; } inline FGTACANList *get_channellist() const { return channellist; } inline void set_channellist( FGTACANList *c ) { channellist = c; } diff --git a/src/Viewer/splash.cxx b/src/Viewer/splash.cxx index 159eeeb11..bfb4d3625 100644 --- a/src/Viewer/splash.cxx +++ b/src/Viewer/splash.cxx @@ -315,10 +315,10 @@ static osg::Node* fgCreateSplashCamera() stateSet->setTextureAttribute(0, splashTexture); geode->addDrawable(geometry); - + FGFontCache* fontCache = FGFontCache::instance(); osgText::Text* text = new osgText::Text; std::string fn = style->getStringValue("fonts/splash", ""); - text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str()); + text->setFont(fontCache->getfntpath(fn).str()); text->setCharacterSize(0.06); text->setColor(osg::Vec4(1, 1, 1, 1)); text->setPosition(osg::Vec3(0, -0.92, 0)); @@ -329,7 +329,7 @@ static osg::Node* fgCreateSplashCamera() geode->addDrawable(text); osgText::Text* spinnertext = new osgText::Text; - spinnertext->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str()); + spinnertext->setFont(fontCache->getfntpath(fn).str()); spinnertext->setCharacterSize(0.06); spinnertext->setColor(osg::Vec4(1, 1, 1, 1)); spinnertext->setPosition(osg::Vec3(0, -0.97, 0)); @@ -340,7 +340,7 @@ static osg::Node* fgCreateSplashCamera() geode->addDrawable(spinnertext); text = new osgText::Text; - text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str()); + text->setFont(fontCache->getfntpath(fn).str()); text->setCharacterSize(0.08); text->setColor(osg::Vec4(1, 1, 1, 1)); text->setPosition(osg::Vec3(0, 0.92, 0)); @@ -351,7 +351,7 @@ static osg::Node* fgCreateSplashCamera() text = new osgText::Text; - text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str()); + text->setFont(fontCache->getfntpath(fn).str()); text->setCharacterSize(0.06); text->setColor(osg::Vec4(1, 1, 1, 1)); text->setPosition(osg::Vec3(0, 0.82, 0)); -- 2.39.5