]> git.mxchange.org Git - flightgear.git/blob - utils/fgpanel/FGFontCache.cxx
Fix Win32 build, hopefully.
[flightgear.git] / utils / fgpanel / FGFontCache.cxx
1 //
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.
6 // 
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.
11 //
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.
15 //
16 #ifdef HAVE_CONFIG_H
17 #  include <config.h>
18 #endif
19
20 #include "FGGLApplication.hxx"
21 #ifdef HAVE_WINDOWS_H
22 #include <windows.h>
23 #endif
24
25 #include <map>
26 #include <algorithm>
27
28 #include <plib/fnt.h>
29 #include <plib/pu.h>
30
31 #include "ApplicationProperties.hxx"
32 #include "FGFontCache.hxx"
33
34
35 ////////////////////////////////////////////////////////////////////////
36 // FGFontCache class.
37 ////////////////////////////////////////////////////////////////////////
38
39 //extern puFont FONT_HELVETICA_14;
40 //extern puFont FONT_SANS_12B;
41
42 namespace
43 {
44 struct GuiFont
45 {
46     const char *name;
47     puFont *font;
48     struct Predicate
49         : public std::unary_function<const GuiFont, bool>
50     {
51         Predicate(const char* name_) : name(name_) {}
52         bool operator() (const GuiFont& f1) const
53         {
54             return ::strcmp(f1.name, name) == 0;
55         }
56         const char* name;
57     };
58 };
59
60 const GuiFont guifonts[] = {
61     { "default",      &PUFONT_HELVETICA_12 },
62     { "FIXED_8x13",   &PUFONT_8_BY_13 },
63     { "FIXED_9x15",   &PUFONT_9_BY_15 },
64     { "TIMES_10",     &PUFONT_TIMES_ROMAN_10 },
65     { "TIMES_24",     &PUFONT_TIMES_ROMAN_24 },
66     { "HELVETICA_10", &PUFONT_HELVETICA_10 },
67     { "HELVETICA_12", &PUFONT_HELVETICA_12 },
68 //    { "HELVETICA_14", &FONT_HELVETICA_14 },
69     { "HELVETICA_18", &PUFONT_HELVETICA_18 }
70 //    { "SANS_12B",     &FONT_SANS_12B }
71 };
72
73 const GuiFont* guifontsEnd = &guifonts[sizeof(guifonts)/ sizeof(guifonts[0])];
74 }
75
76 FGFontCache::fnt::~fnt()
77 {
78     if (texfont) { 
79         delete pufont; 
80         delete texfont;
81     }
82 }
83
84 FGFontCache::FGFontCache() :
85     _initialized(false)
86 {
87 }
88
89 FGFontCache::~FGFontCache()
90 {
91    PuFontMap::iterator it, end = _puFonts.end();
92    for (it = _puFonts.begin(); it != end; ++it)
93        delete it->second;
94 }
95
96 inline bool FGFontCache::FntParamsLess::operator()(const FntParams& f1,
97                                                    const FntParams& f2) const
98 {
99     int comp = f1.name.compare(f2.name);
100     if (comp < 0)
101         return true;
102     else if (comp > 0)
103         return false;
104     if (f1.size < f2.size)
105         return true;
106     else if (f1.size > f2.size)
107         return false;
108     return f1.slant < f2.slant;
109 }
110
111 struct FGFontCache::fnt *
112 FGFontCache::getfnt(const char *name, float size, float slant)
113 {
114     std::string fontName(name);
115     FntParams fntParams(fontName, size, slant);
116     PuFontMap::iterator i = _puFonts.find(fntParams);
117     if (i != _puFonts.end())
118         return i->second;
119     // fntTexFont s are all preloaded into the _texFonts map
120     TexFontMap::iterator texi = _texFonts.find(fontName);
121     fntTexFont* texfont = 0;
122     puFont* pufont = 0;
123     if (texi != _texFonts.end()) {
124         texfont = texi->second;
125     } else {
126         const GuiFont* guifont = std::find_if(&guifonts[0], guifontsEnd,
127                                               GuiFont::Predicate(name));
128         if (guifont != guifontsEnd) {
129             pufont = guifont->font;
130         }
131     }
132     fnt* f = new fnt;
133     if (pufont) {
134         f->pufont = pufont;
135     } else if (texfont) {
136         f->texfont = texfont;
137         f->pufont = new puFont;
138         f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant);
139     } else {
140         f->pufont = guifonts[0].font;
141     }
142     _puFonts[fntParams] = f;
143     return f;
144 }
145
146 puFont *
147 FGFontCache::get(const char *name, float size, float slant)
148 {
149     return getfnt(name, size, slant)->pufont;
150 }
151
152 fntTexFont *
153 FGFontCache::getTexFont(const char *name, float size, float slant)
154 {
155     init();
156     return getfnt(name, size, slant)->texfont;
157 }
158
159 puFont *
160 FGFontCache::get(SGPropertyNode *node)
161 {
162     if (!node)
163         return get("Helvetica.txf", 15.0, 0.0);
164
165     const char *name = node->getStringValue("name", "Helvetica.txf");
166     float size = node->getFloatValue("size", 15.0);
167     float slant = node->getFloatValue("slant", 0.0);
168
169     return get(name, size, slant);
170 }
171
172 void FGFontCache::init()
173 {
174     if (!_initialized) {
175         char *envp = ::getenv("FG_FONTS");
176         if (envp != NULL) {
177             _path.set(envp);
178         } else {
179             _path.set(ApplicationProperties::GetRootPath("Fonts").str());
180         }
181         _initialized = true;
182     }
183 }
184
185 SGPath
186 FGFontCache::getfntpath(const char *name)
187 {
188     init();
189     SGPath path(_path);
190     if (name && std::string(name) != "") {
191         path.append(name);
192         if (path.exists())
193             return path;
194     }
195
196     path = SGPath(_path);
197     path.append("Helvetica.txf");
198     
199     return path;
200 }
201
202 bool FGFontCache::initializeFonts()
203 {
204     static std::string fontext("txf");
205     init();
206     ulDir* fontdir = ulOpenDir(_path.c_str());
207     if (!fontdir)
208         return false;
209     const ulDirEnt *dirEntry;
210     while ((dirEntry = ulReadDir(fontdir)) != 0) {
211         SGPath path(_path);
212         path.append(dirEntry->d_name);
213         if (path.extension() == fontext) {
214             fntTexFont* f = new fntTexFont;
215             if (f->load((char *)path.c_str()))
216                 _texFonts[std::string(dirEntry->d_name)] = f;
217             else
218                 delete f;
219         }
220     }
221     ulCloseDir(fontdir);
222     return true;
223 }
224
225 // end of new_gui.cxx
226