+void FGFontCache::init()
+{
+ if (_initialized) {
+ return;
+ }
+
+ char *envp = ::getenv("FG_FONTS");
+ if (envp != NULL) {
+ _path.set(envp);
+ } else {
+ _path.set(globals->get_fg_root());
+ _path.append("Fonts");
+ }
+ _initialized = true;
+}
+
+SGPath
+FGFontCache::getfntpath(const char *name)
+{
+ init();
+ SGPath path(_path);
+ if (name && std::string(name) != "") {
+ path.append(name);
+ if (path.exists())
+ return path;
+ }
+
+ path = SGPath(_path);
+ path.append("Helvetica.txf");
+ SG_LOG(SG_GENERAL, SG_WARN, "Unknown font name '" << name << "', defaulting to Helvetica");
+ return path;
+}
+
+bool FGFontCache::initializeFonts()
+{
+ static string fontext("txf");
+ init();
+ ulDir* fontdir = ulOpenDir(_path.c_str());
+ if (!fontdir)
+ return false;
+ const ulDirEnt *dirEntry;
+ while ((dirEntry = ulReadDir(fontdir)) != 0) {
+ SGPath path(_path);
+ path.append(dirEntry->d_name);
+ if (path.extension() == fontext) {
+ fntTexFont* f = new fntTexFont;
+ if (f->load((char *)path.c_str())) {
+ // convert font names in the map to lowercase for matching
+ string fontName = boost::to_lower_copy(string(dirEntry->d_name));
+ _texFonts[fontName] = f;
+ } else
+ delete f;
+ }
+ }
+ ulCloseDir(fontdir);
+ return true;
+}
+