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