]> git.mxchange.org Git - flightgear.git/blob - src/GUI/FGFontCache.hxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[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/misc/sg_path.hxx>
19
20 #include <map>
21 #include <algorithm>
22
23 // forward decls
24 class SGPropertyNode;
25 class puFont;
26 class fntTexFont;
27
28 /**
29  * A class to keep all fonts available for future use.
30  * This also assures a font isn't resident more than once.
31  */
32 class FGFontCache {
33 private:
34     FGFontCache(); // private constructor, use singleton instance() accessor
35
36     // The parameters of a request to the cache.
37     struct FntParams
38     {
39         const std::string name;
40         const float size;
41         const float slant;
42         FntParams() : size(0.0f), slant(0.0f) {}
43         FntParams(const FntParams& rhs)
44             : name(rhs.name), size(rhs.size), slant(rhs.slant)
45         {
46         }
47         FntParams(const std::string& name_, float size_, float slant_)
48             : name(name_), size(size_), slant(slant_)
49         {
50         }
51     };
52     struct FntParamsLess
53         : public std::binary_function<const FntParams, const FntParams, bool>
54     {
55         bool operator() (const FntParams& f1, const FntParams& f2) const;
56     };
57     struct fnt {
58         fnt(puFont *pu = 0) : pufont(pu), texfont(0) {}
59         ~fnt();
60         
61         // Font used by plib GUI code
62         puFont *pufont;
63         // TXF font
64         fntTexFont *texfont;
65     };
66     // Path to the font directory
67     SGPath _path;
68
69     typedef std::map<const std::string, fntTexFont*> TexFontMap;
70     typedef std::map<const FntParams, fnt*, FntParamsLess> PuFontMap;
71     TexFontMap _texFonts;
72     PuFontMap _puFonts;
73
74     bool _initialized;
75     struct fnt *getfnt(const std::string& name, float size, float slant);
76     void init();
77
78 public:
79     // note this accesor is NOT thread-safe
80     static FGFontCache* instance();
81
82     ~FGFontCache();
83
84     puFont *get(const std::string& name, float size=15.0, float slant=0.0);
85     puFont *get(SGPropertyNode *node);
86
87     fntTexFont *getTexFont(const std::string& name, float size=15.0, float slant=0.0);
88
89     SGPath getfntpath(const std::string& name);
90     /**
91      * Preload all the fonts in the FlightGear font directory. It is
92      * important to load the font textures early, with the proper
93      * graphics context current, so that no plib (or our own) code
94      * tries to load a font from disk when there's no current graphics
95      * context.
96      */
97     bool initializeFonts();
98 };
99 #endif