| 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 | #include <config.h> |
| 17 | |
| 18 | #include "FGFontCache.hxx" |
| 19 | |
| 20 | // this is our one in 3rdparty |
| 21 | #include "fnt.h" |
| 22 | |
| 23 | #if defined(HAVE_PUI) |
| 24 | #include "FlightGear_pu.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <simgear/props/props.hxx> |
| 28 | #include <simgear/misc/sg_dir.hxx> |
| 29 | |
| 30 | #include <Main/globals.hxx> |
| 31 | |
| 32 | //////////////////////////////////////////////////////////////////////// |
| 33 | // FGFontCache class. |
| 34 | //////////////////////////////////////////////////////////////////////// |
| 35 | |
| 36 | static std::unique_ptr<FGFontCache> global_fontCacheInstance; |
| 37 | |
| 38 | #if defined(HAVE_PUI) |
| 39 | extern puFont FONT_HELVETICA_14; |
| 40 | extern puFont FONT_SANS_12B; |
| 41 | extern puFont FONT_HELVETICA_12; |
| 42 | #endif |
| 43 | namespace |
| 44 | { |
| 45 | struct GuiFont |
| 46 | { |
| 47 | const std::string name; |
| 48 | puFont *font; |
| 49 | }; |
| 50 | |
| 51 | const std::initializer_list<GuiFont> guiFonts = { |
| 52 | {"default", &PUFONT_HELVETICA_12}, |
| 53 | {"FIXED_8x13", &PUFONT_8_BY_13}, |
| 54 | {"FIXED_9x15", &PUFONT_9_BY_15}, |
| 55 | {"TIMES_10", &PUFONT_TIMES_ROMAN_10}, |
| 56 | {"TIMES_24", &PUFONT_TIMES_ROMAN_24}, |
| 57 | {"HELVETICA_10", &PUFONT_HELVETICA_10}, |
| 58 | {"HELVETICA_18", &PUFONT_HELVETICA_18}, |
| 59 | #if defined(HAVE_PUI) |
| 60 | {"HELVETICA_12", &FONT_HELVETICA_12}, |
| 61 | {"HELVETICA_14", &FONT_HELVETICA_14}, |
| 62 | {"SANS_12B", &FONT_SANS_12B}, |
| 63 | #endif |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | FGFontCache* FGFontCache::instance() |
| 68 | { |
| 69 | if (!global_fontCacheInstance.get()) { |
| 70 | global_fontCacheInstance.reset(new FGFontCache); |
| 71 | } |
| 72 | |
| 73 | return global_fontCacheInstance.get(); |
| 74 | } |
| 75 | |
| 76 | void FGFontCache::shutdown() |
| 77 | { |
| 78 | global_fontCacheInstance.reset(); |
| 79 | } |
| 80 | |
| 81 | FGFontCache::FGFontCache() : |
| 82 | _initialized(false) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | FGFontCache::~FGFontCache() |
| 87 | { |
| 88 | for (auto puFontIt : _cache) { |
| 89 | delete puFontIt.second; |
| 90 | } |
| 91 | |
| 92 | // these were created i initializeFonts |
| 93 | for (auto texFontIt : _texFonts) { |
| 94 | delete texFontIt.second; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | bool FGFontCache::FntParams::operator<(const FntParams& other) const |
| 99 | { |
| 100 | int comp = name.compare(other.name); |
| 101 | if (comp < 0) |
| 102 | return true; |
| 103 | else if (comp > 0) |
| 104 | return false; |
| 105 | if (size < other.size) |
| 106 | return true; |
| 107 | else if (size > other.size) |
| 108 | return false; |
| 109 | return slant < other.slant; |
| 110 | } |
| 111 | |
| 112 | FGFontCache::FontCacheEntry* |
| 113 | FGFontCache::getfnt(const std::string& fontName, float size, float slant) |
| 114 | { |
| 115 | FntParams fntParams(fontName, size, slant); |
| 116 | PuFontMap::iterator i = _cache.find(fntParams); |
| 117 | if (i != _cache.end()) |
| 118 | return i->second; |
| 119 | // fntTexFont s are all preloaded into the _texFonts map |
| 120 | TexFontMap::iterator texi = _texFonts.find(fontName); |
| 121 | fntTexFont* texfont = 0; |
| 122 | puFont* pufont = 0; |
| 123 | if (texi != _texFonts.end()) { |
| 124 | texfont = texi->second; |
| 125 | } else { |
| 126 | auto guifont = std::find_if(guiFonts.begin(), guiFonts.end(), |
| 127 | [&fontName](const GuiFont& gf) { |
| 128 | return gf.name == fontName; |
| 129 | }); |
| 130 | |
| 131 | if (guifont != guiFonts.end()) { |
| 132 | pufont = guifont->font; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | auto f = new FontCacheEntry; |
| 137 | if (pufont) { |
| 138 | f->pufont = pufont; |
| 139 | } else if (texfont) { |
| 140 | f->texfont = texfont; |
| 141 | f->ownsPUFont = true; // ensure it gets cleaned up |
| 142 | f->pufont = new puFont; |
| 143 | f->pufont->initialize(static_cast<fntFont *>(f->texfont), size, slant); |
| 144 | } else { |
| 145 | f->pufont = guiFonts.begin()->font; |
| 146 | } |
| 147 | |
| 148 | // insert into the cache |
| 149 | _cache[fntParams] = f; |
| 150 | return f; |
| 151 | } |
| 152 | |
| 153 | puFont * |
| 154 | FGFontCache::get(const std::string& name, float size, float slant) |
| 155 | { |
| 156 | return getfnt(name, size, slant)->pufont; |
| 157 | } |
| 158 | |
| 159 | fntTexFont * |
| 160 | FGFontCache::getTexFont(const std::string& name, float size, float slant) |
| 161 | { |
| 162 | init(); |
| 163 | return getfnt(name, size, slant)->texfont; |
| 164 | } |
| 165 | |
| 166 | puFont * |
| 167 | FGFontCache::get(SGPropertyNode *node) |
| 168 | { |
| 169 | if (!node) |
| 170 | return get("Helvetica.txf", 15.0, 0.0); |
| 171 | |
| 172 | std::string name = node->getStringValue("name", "Helvetica.txf"); |
| 173 | float size = node->getFloatValue("size", 15.0); |
| 174 | float slant = node->getFloatValue("slant", 0.0); |
| 175 | |
| 176 | return get(name, size, slant); |
| 177 | } |
| 178 | |
| 179 | void FGFontCache::init() |
| 180 | { |
| 181 | if (!_initialized) { |
| 182 | char *envp = ::getenv("FG_FONTS"); |
| 183 | if (envp != NULL) { |
| 184 | _path = SGPath::fromEnv("FG_FONTS"); |
| 185 | } else { |
| 186 | _path = globals->get_fg_root(); |
| 187 | _path.append("Fonts"); |
| 188 | } |
| 189 | _initialized = true; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | SGPath |
| 194 | FGFontCache::getfntpath(const std::string& name) |
| 195 | { |
| 196 | init(); |
| 197 | SGPath path(_path); |
| 198 | if (name != "") { |
| 199 | path.append(name); |
| 200 | if (path.exists()) |
| 201 | return path; |
| 202 | } |
| 203 | |
| 204 | path = SGPath(_path); |
| 205 | path.append("Helvetica.txf"); |
| 206 | |
| 207 | return path; |
| 208 | } |
| 209 | |
| 210 | bool FGFontCache::initializeFonts() |
| 211 | { |
| 212 | static std::string fontext(".txf"); |
| 213 | init(); |
| 214 | |
| 215 | auto dir = simgear::Dir(_path); |
| 216 | for (auto p : dir.children(simgear::Dir::TYPE_FILE, fontext)) { |
| 217 | fntTexFont* f = new fntTexFont; |
| 218 | if (f->load(p)) |
| 219 | _texFonts[p.file()] = f; |
| 220 | else |
| 221 | delete f; |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | FGFontCache::FontCacheEntry::~FontCacheEntry() |
| 228 | { |
| 229 | if (ownsPUFont) { |
| 230 | delete pufont; |
| 231 | } |
| 232 | } |
| 233 |