2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License as
4 // published by the Free Software Foundation; either version 2 of the
5 // License, or (at your option) any later version.
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // General Public License for more details.
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "FGFontCache.hxx"
29 #include <simgear/props/props.hxx>
31 #include <Main/globals.hxx>
33 ////////////////////////////////////////////////////////////////////////
35 ////////////////////////////////////////////////////////////////////////
37 static std::auto_ptr<FGFontCache> global_fontCacheInstance;
40 extern puFont FONT_HELVETICA_14;
41 extern puFont FONT_SANS_12B;
42 extern puFont FONT_HELVETICA_12;
51 : public std::unary_function<const GuiFont, bool>
53 Predicate(const std::string& name_) : name(name_) {}
54 bool operator() (const GuiFont& f1) const
56 return (name == f1.name);
58 const std::string name;
62 const GuiFont guifonts[] = {
63 { "default", &PUFONT_HELVETICA_12 },
64 { "FIXED_8x13", &PUFONT_8_BY_13 },
65 { "FIXED_9x15", &PUFONT_9_BY_15 },
66 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
67 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
68 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
69 { "HELVETICA_12", &FONT_HELVETICA_12 },
70 { "HELVETICA_14", &FONT_HELVETICA_14 },
71 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
72 { "SANS_12B", &FONT_SANS_12B },
76 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])-1];
79 FGFontCache* FGFontCache::instance()
81 if (!global_fontCacheInstance.get()) {
82 global_fontCacheInstance.reset(new FGFontCache);
85 return global_fontCacheInstance.get();
88 FGFontCache::FGFontCache() :
93 FGFontCache::~FGFontCache()
95 PuFontMap::iterator it, end = _puFonts.end();
96 for (it = _puFonts.begin(); it != end; ++it)
100 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
101 const FntParams& f2) const
103 int comp = f1.name.compare(f2.name);
108 if (f1.size < f2.size)
110 else if (f1.size > f2.size)
112 return f1.slant < f2.slant;
115 struct FGFontCache::fnt *
116 FGFontCache::getfnt(const std::string& fontName, float size, float slant)
118 FntParams fntParams(fontName, size, slant);
119 PuFontMap::iterator i = _puFonts.find(fntParams);
120 if (i != _puFonts.end())
122 // fntTexFont s are all preloaded into the _texFonts map
123 TexFontMap::iterator texi = _texFonts.find(fontName);
124 fntTexFont* texfont = 0;
126 if (texi != _texFonts.end()) {
127 texfont = texi->second;
129 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
130 GuiFont::Predicate(fontName));
131 if (guifont != guifontsEnd) {
132 pufont = guifont->font;
138 } else if (texfont) {
139 f->texfont = texfont;
140 f->pufont = new puFont;
141 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
143 f->pufont = guifonts[0].font;
145 _puFonts[fntParams] = f;
150 FGFontCache::get(const std::string& name, float size, float slant)
152 return getfnt(name, size, slant)->pufont;
156 FGFontCache::getTexFont(const std::string& name, float size, float slant)
159 return getfnt(name, size, slant)->texfont;
163 FGFontCache::get(SGPropertyNode *node)
166 return get("Helvetica.txf", 15.0, 0.0);
168 const char *name = node->getStringValue("name", "Helvetica.txf");
169 float size = node->getFloatValue("size", 15.0);
170 float slant = node->getFloatValue("slant", 0.0);
172 return get(name, size, slant);
175 void FGFontCache::init()
178 char *envp = ::getenv("FG_FONTS");
182 _path.set(globals->get_fg_root());
183 _path.append("Fonts");
190 FGFontCache::getfntpath(const std::string& name)
200 path = SGPath(_path);
201 path.append("Helvetica.txf");
206 bool FGFontCache::initializeFonts()
208 static std::string fontext("txf");
210 ulDir* fontdir = ulOpenDir(_path.c_str());
213 const ulDirEnt *dirEntry;
214 while ((dirEntry = ulReadDir(fontdir)) != 0) {
216 path.append(dirEntry->d_name);
217 if (path.extension() == fontext) {
218 fntTexFont* f = new fntTexFont;
219 if (f->load((char *)path.c_str()))
220 _texFonts[std::string(dirEntry->d_name)] = f;
229 FGFontCache::fnt::~fnt()