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