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 extern puFont FONT_HELVETICA_14;
38 extern puFont FONT_SANS_12B;
47 : public std::unary_function<const GuiFont, bool>
49 Predicate(const char* name_) : name(name_) {}
50 bool operator() (const GuiFont& f1) const
52 return ::strcmp(f1.name, name) == 0;
58 const GuiFont guifonts[] = {
59 { "default", &PUFONT_HELVETICA_12 },
60 { "FIXED_8x13", &PUFONT_8_BY_13 },
61 { "FIXED_9x15", &PUFONT_9_BY_15 },
62 { "TIMES_10", &PUFONT_TIMES_ROMAN_10 },
63 { "TIMES_24", &PUFONT_TIMES_ROMAN_24 },
64 { "HELVETICA_10", &PUFONT_HELVETICA_10 },
65 { "HELVETICA_12", &PUFONT_HELVETICA_12 },
66 { "HELVETICA_14", &FONT_HELVETICA_14 },
67 { "HELVETICA_18", &PUFONT_HELVETICA_18 },
68 { "SANS_12B", &FONT_SANS_12B }
71 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
74 FGFontCache::FGFontCache() :
79 FGFontCache::~FGFontCache()
81 PuFontMap::iterator it, end = _puFonts.end();
82 for (it = _puFonts.begin(); it != end; ++it)
86 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
87 const FntParams& f2) const
89 int comp = f1.name.compare(f2.name);
94 if (f1.size < f2.size)
96 else if (f1.size > f2.size)
98 return f1.slant < f2.slant;
101 struct FGFontCache::fnt *
102 FGFontCache::getfnt(const char *name, float size, float slant)
104 std::string fontName(name);
105 FntParams fntParams(fontName, size, slant);
106 PuFontMap::iterator i = _puFonts.find(fntParams);
107 if (i != _puFonts.end())
109 // fntTexFont s are all preloaded into the _texFonts map
110 TexFontMap::iterator texi = _texFonts.find(fontName);
111 fntTexFont* texfont = 0;
113 if (texi != _texFonts.end()) {
114 texfont = texi->second;
116 const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
117 GuiFont::Predicate(name));
118 if (guifont != guifontsEnd) {
119 pufont = guifont->font;
125 } else if (texfont) {
126 f->texfont = texfont;
127 f->pufont = new puFont;
128 f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
130 f->pufont = guifonts[0].font;
132 _puFonts[fntParams] = f;
137 FGFontCache::get(const char *name, float size, float slant)
139 return getfnt(name, size, slant)->pufont;
143 FGFontCache::getTexFont(const char *name, float size, float slant)
146 return getfnt(name, size, slant)->texfont;
150 FGFontCache::get(SGPropertyNode *node)
153 return get("Helvetica.txf", 15.0, 0.0);
155 const char *name = node->getStringValue("name", "Helvetica.txf");
156 float size = node->getFloatValue("size", 15.0);
157 float slant = node->getFloatValue("slant", 0.0);
159 return get(name, size, slant);
162 void FGFontCache::init()
165 char *envp = ::getenv("FG_FONTS");
169 _path.set(globals->get_fg_root());
170 _path.append("Fonts");
177 FGFontCache::getfntpath(const char *name)
181 if (name && std::string(name) != "") {
187 path = SGPath(_path);
188 path.append("Helvetica.txf");
193 bool FGFontCache::initializeFonts()
195 static std::string fontext("txf");
197 ulDir* fontdir = ulOpenDir(_path.c_str());
200 const ulDirEnt *dirEntry;
201 while ((dirEntry = ulReadDir(fontdir)) != 0) {
203 path.append(dirEntry->d_name);
204 if (path.extension() == fontext) {
205 fntTexFont* f = new fntTexFont;
206 if (f->load((char *)path.c_str()))
207 _texFonts[std::string(dirEntry->d_name)] = f;
216 FGFontCache::fnt::~fnt()