object->setBorderThickness( props->getIntValue("border", 2) );
if ( SGPropertyNode *nft = props->getNode("font", false) ) {
- SGPath path( _font_path );
- const char *name = nft->getStringValue("name", "default");
- float size = nft->getFloatValue("size", 13.0);
- float slant = nft->getFloatValue("slant", 0.0);
- path.append( name );
- path.concat( ".txf" );
-
- fntFont *font = new fntTexFont;
- font->load( (char *)path.c_str() );
-
- puFont lfnt(font, size, slant);
- object->setLabelFont( lfnt );
+ FGFontCache *fc = _gui->get_fontcache();
+ puFont *lfnt = fc->get(nft);
+ object->setLabelFont(*lfnt);
}
if (props->hasValue("property")) {
NewGUI::NewGUI ()
- : _font(FONT_HELVETICA_14),
+ : _fontcache(new FGFontCache),
_menubar(new FGMenuBar),
_active_dialog(0)
{
void
NewGUI::setupFont (SGPropertyNode *node)
{
- string fontname = node->getStringValue("name", "Helvetica.txf");
- float size = node->getFloatValue("size", 15.0);
- float slant = node->getFloatValue("slant", 0.0);
-
- int i;
- for (i = 0; guifonts[i].name; i++)
- if (fontname == guifonts[i].name)
- break;
- if (guifonts[i].name)
- _font = *guifonts[i].font;
- else {
- SGPath fontpath;
- char* envp = ::getenv("FG_FONTS");
- if (envp != NULL) {
- fontpath.set(envp);
- } else {
- fontpath.set(globals->get_fg_root());
- fontpath.append("Fonts");
- }
-
- SGPath path(fontpath);
- path.append(fontname);
-
- if (_tex_font.load((char *)path.c_str())) {
- _font.initialize((fntFont *)&_tex_font, size, slant);
- } else {
- _font = *guifonts[0].font;
- fontname = "default";
- }
- }
- puSetDefaultFonts(_font, _font);
- node->setStringValue("name", fontname.c_str());
+ _font = _fontcache->get(node);
+ puSetDefaultFonts(*_font, *_font);
+ return;
}
}
for (int i=0; guifonts[i].name; i++)
- _fonts[guifonts[i].name] = guifonts[i].font;
+ _fonts[guifonts[i].name] = new fnt(guifonts[i].font);
}
FGFontCache::~FGFontCache()
puFont *
FGFontCache::get(const char *name, float size, float slant)
{
- puFont *font;
_itt_t it;
- if ((it = _fonts.find(name)) == _fonts.end())
- {
+ if ((it = _fonts.find(name)) == _fonts.end()) {
SGPath path(_path);
path.append(name);
- fntTexFont tex_font;
- if (tex_font.load((char *)path.c_str()))
- {
- font = new puFont;
- font->initialize((fntFont *)&tex_font, size, slant);
- _fonts[name] = font;
- }
- else
- {
- font = _fonts["default"];
- // puSetDefaultFonts(font, font);
+ fnt *f = new fnt();
+ f->texfont = new fntTexFont;
+ if (f->texfont->load((char *)path.c_str())) {
+ f->pufont = new puFont;
+ f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
+ _fonts[name] = f;
+ return f->pufont;
+
+ } else {
+ delete f;
+ return _fonts["default"]->pufont;
}
- }
- else
- {
- font = it->second;
- }
- return font;
+ } else {
+ return it->second->pufont;
+ }
}
puFont *
return (it != _colors.end()) ? it->second : NULL;
}
- virtual puFont *getDefaultFont() { return &_font; }
+ virtual puFont *getDefaultFont() { return _font; }
/**
}
};
- fntTexFont _tex_font;
- puFont _font;
+ puFont *_font;
map<const char*,FGColor*, ltstr> _colors;
typedef map<const char*,FGColor*, ltstr>::iterator _itt_t;
typedef map<const char*,FGColor*, ltstr>::const_iterator _citt_t;
*/
class FGFontCache {
private:
+ struct fnt {
+ fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
+ ~fnt() { delete pufont; delete texfont; }
+ puFont *pufont;
+ fntTexFont *texfont;
+ };
SGPath _path;
- map<const char*,puFont*> _fonts;
- typedef map<const char*,puFont*>::iterator _itt_t;
+ map<const string,fnt *> _fonts;
+ typedef map<const string,fnt *>::const_iterator _itt_t;
public:
FGFontCache();