| 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 | bool operator<(const FntParams& other) const; |
| 53 | }; |
| 54 | |
| 55 | struct FontCacheEntry { |
| 56 | FontCacheEntry(puFont* pu = 0) : pufont(pu), texfont(0) {} |
| 57 | ~FontCacheEntry(); |
| 58 | |
| 59 | // Font used by plib GUI code |
| 60 | puFont *pufont; |
| 61 | // TXF font |
| 62 | fntTexFont *texfont; |
| 63 | |
| 64 | bool ownsPUFont = false; |
| 65 | }; |
| 66 | |
| 67 | // Path to the font directory |
| 68 | SGPath _path; |
| 69 | |
| 70 | typedef std::map<const std::string, fntTexFont*> TexFontMap; |
| 71 | typedef std::map<const FntParams, FontCacheEntry*> PuFontMap; |
| 72 | TexFontMap _texFonts; |
| 73 | PuFontMap _cache; |
| 74 | |
| 75 | bool _initialized; |
| 76 | FontCacheEntry* getfnt(const std::string& name, float size, float slant); |
| 77 | void init(); |
| 78 | |
| 79 | public: |
| 80 | // note this accesor is NOT thread-safe |
| 81 | static FGFontCache* instance(); |
| 82 | static void shutdown(); |
| 83 | |
| 84 | ~FGFontCache(); |
| 85 | |
| 86 | puFont *get(const std::string& name, float size=15.0, float slant=0.0); |
| 87 | puFont *get(SGPropertyNode *node); |
| 88 | |
| 89 | fntTexFont *getTexFont(const std::string& name, float size=15.0, float slant=0.0); |
| 90 | |
| 91 | SGPath getfntpath(const std::string& name); |
| 92 | /** |
| 93 | * Preload all the fonts in the FlightGear font directory. It is |
| 94 | * important to load the font textures early, with the proper |
| 95 | * graphics context current, so that no plib (or our own) code |
| 96 | * tries to load a font from disk when there's no current graphics |
| 97 | * context. |
| 98 | */ |
| 99 | bool initializeFonts(); |
| 100 | }; |
| 101 | #endif |