]> git.mxchange.org Git - flightgear.git/blob - src/GUI/FGFontCache.hxx
Clean-up: move autosave.xml loading code to proper method
[flightgear.git] / src / GUI / FGFontCache.hxx
1 //  This program is free software; you can redistribute it and/or
2 //  modify it under the terms of the GNU General Public License as
3 //  published by the Free Software Foundation; either version 2 of the
4 //  License, or (at your option) any later version.
5 // 
6 //  This program is distributed in the hope that it will be useful, but
7 //  WITHOUT ANY WARRANTY; without even the implied warranty of
8 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9 //  General Public License for more details.
10 //
11 //  You should have received a copy of the GNU General Public License
12 //  along with this program; if not, write to the Free Software
13 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14 //
15 #ifndef __FGFONTCACHE_HXX
16 #define __FGFONTCACHE_HXX
17
18 #include <simgear/math/SGMath.hxx>
19 #include <simgear/misc/sg_path.hxx>
20
21 #include <map>
22 #include <algorithm>
23
24 // forward decls
25 class SGPropertyNode;
26 class puFont;
27 class fntTexFont;
28
29 /**
30  * A class to keep all fonts available for future use.
31  * This also assures a font isn't resident more than once.
32  */
33 class FGFontCache {
34 private:
35     // The parameters of a request to the cache.
36     struct FntParams
37     {
38         const std::string name;
39         const float size;
40         const float slant;
41         FntParams() : size(0.0f), slant(0.0f) {}
42         FntParams(const FntParams& rhs)
43             : name(rhs.name), size(rhs.size), slant(rhs.slant)
44         {
45         }
46         FntParams(const std::string& name_, float size_, float slant_)
47             : name(name_), size(size_), slant(slant_)
48         {
49         }
50     };
51     struct FntParamsLess
52         : public std::binary_function<const FntParams, const FntParams, bool>
53     {
54         bool operator() (const FntParams& f1, const FntParams& f2) const;
55     };
56     struct fnt {
57         fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
58         ~fnt();
59         
60         // Font used by plib GUI code
61         puFont *pufont;
62         // TXF font
63         fntTexFont *texfont;
64     };
65     // Path to the font directory
66     SGPath _path;
67
68     typedef std::map<const std::string, fntTexFont*> TexFontMap;
69     typedef std::map<const FntParams, fnt*, FntParamsLess> PuFontMap;
70     TexFontMap _texFonts;
71     PuFontMap _puFonts;
72
73     bool _initialized;
74     struct fnt *getfnt(const char *name, float size, float slant);
75     void init();
76
77 public:
78     FGFontCache();
79     ~FGFontCache();
80
81     puFont *get(const char *name, float size=15.0, float slant=0.0);
82     puFont *get(SGPropertyNode *node);
83
84     fntTexFont *getTexFont(const char *name, float size=15.0, float slant=0.0);
85
86     SGPath getfntpath(const char *name);
87     /**
88      * Preload all the fonts in the FlightGear font directory. It is
89      * important to load the font textures early, with the proper
90      * graphics context current, so that no plib (or our own) code
91      * tries to load a font from disk when there's no current graphics
92      * context.
93      */
94     bool initializeFonts();
95 };
96 #endif