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