ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

fontman.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "graphics/fontman.h"
00024 #include "graphics/font.h"
00025 #include "graphics/fonts/bdf.h"
00026 
00027 #include "common/translation.h"
00028 
00029 namespace Common {
00030 DECLARE_SINGLETON(Graphics::FontManager);
00031 }
00032 
00033 namespace Graphics {
00034 
00035 FORWARD_DECLARE_FONT(g_sysfont);
00036 FORWARD_DECLARE_FONT(g_sysfont_big);
00037 FORWARD_DECLARE_FONT(g_consolefont);
00038 
00039 FontManager::FontManager() {
00040     // This assert should *never* trigger, because
00041     // FontManager is a singleton, thus there is only
00042     // one instance of it per time. (g_sysfont gets
00043     // reset to 0 in the desctructor of this class).
00044     assert(g_sysfont == 0);
00045     INIT_FONT(g_sysfont);
00046     INIT_FONT(g_sysfont_big);
00047     INIT_FONT(g_consolefont);
00048 }
00049 
00050 FontManager::~FontManager() {
00051     for (uint i = 0; i < _ownedFonts.size(); ++i) {
00052         const Font *font = _ownedFonts[i];
00053         if (font == g_sysfont || font == g_sysfont_big || font == g_consolefont)
00054             continue;
00055         delete font;
00056     }
00057 
00058     delete g_sysfont;
00059     g_sysfont = 0;
00060     delete g_sysfont_big;
00061     g_sysfont_big = 0;
00062     delete g_consolefont;
00063     g_consolefont = 0;
00064 }
00065 
00066 const struct {
00067     const char *name;
00068     FontManager::FontUsage id;
00069 } builtinFontNames[] = {
00070     { "builtinConsole", FontManager::kConsoleFont },
00071     { "fixed5x8.bdf", FontManager::kConsoleFont },
00072     { "fixed5x8-iso-8859-1.bdf", FontManager::kConsoleFont },
00073     { "fixed5x8-ascii.bdf", FontManager::kConsoleFont },
00074     { "clR6x12.bdf", FontManager::kGUIFont },
00075     { "clR6x12-iso-8859-1.bdf", FontManager::kGUIFont },
00076     { "clR6x12-ascii.bdf", FontManager::kGUIFont },
00077     { "helvB12.bdf", FontManager::kBigGUIFont },
00078     { "helvB12-iso-8859-1.bdf", FontManager::kBigGUIFont },
00079     { "helvB12-ascii.bdf", FontManager::kBigGUIFont },
00080     { 0, FontManager::kConsoleFont }
00081 };
00082 
00083 bool FontManager::setLocalizedFont(const Common::String &name) {
00084     Common::String lowercaseName = name;
00085     lowercaseName.toLowercase();
00086 
00087     // We only update the localized font in case the name is properly assigned
00088     // to a font.
00089     if (_fontMap.contains(lowercaseName) && _fontMap.getVal(lowercaseName) != 0) {
00090         _localizedFontName = lowercaseName;
00091         return true;
00092     } else {
00093         return false;
00094     }
00095 }
00096 
00097 bool FontManager::assignFontToName(const Common::String &name, const Font *font) {
00098     Common::String lowercaseName = name;
00099     lowercaseName.toLowercase();
00100     _fontMap[lowercaseName] = font;
00101     if (Common::find(_ownedFonts.begin(), _ownedFonts.end(), font) == _ownedFonts.end())
00102         _ownedFonts.push_back(font);
00103     return true;
00104 }
00105 
00106 bool FontManager::setFont(FontUsage usage, const BdfFont *font) {
00107     switch (usage) {
00108     case kConsoleFont:
00109         delete g_consolefont;
00110         g_consolefont = font;
00111         break;
00112     case kGUIFont:
00113         delete g_sysfont;
00114         g_sysfont = font;
00115         break;
00116     case kBigGUIFont:
00117         delete g_sysfont_big;
00118         g_sysfont_big = font;
00119         break;
00120     default:
00121         return false;
00122     }
00123     return true;
00124 }
00125 
00126 void FontManager::removeFontName(const Common::String &name) {
00127     Common::String lowercaseName = name;
00128     lowercaseName.toLowercase();
00129     if (!_fontMap.contains(lowercaseName))
00130         return;
00131 
00132     const Font *font = _fontMap[lowercaseName];
00133     _fontMap.erase(lowercaseName);
00134 
00135     // Check if we still have a copy of this font in the map.
00136     bool stillHasFont = false;
00137     for (Common::HashMap<Common::String, const Font *>::iterator i = _fontMap.begin(); i != _fontMap.end(); ++i) {
00138         if (i->_value != font)
00139             continue;
00140         stillHasFont = true;
00141         break;
00142     }
00143 
00144     if (!stillHasFont) {
00145         // We don't have a copy of the font, so remove it from our list and delete it.
00146         stillHasFont = true;
00147         for (uint i = 0; i < _ownedFonts.size(); ++i) {
00148             if (_ownedFonts[i] != font)
00149                 continue;
00150             stillHasFont = false;
00151             _ownedFonts.remove_at(i);
00152             break;
00153         }
00154         assert(!stillHasFont);
00155         delete font;
00156     }
00157 
00158     // In case the current localized font is removed, we fall back to the
00159     // default font again.
00160     if (_localizedFontName == lowercaseName)
00161         _localizedFontName.clear();
00162 }
00163 
00164 const Font *FontManager::getFontByName(const Common::String &name) const {
00165     for (int i = 0; builtinFontNames[i].name; i++)
00166         if (!scumm_stricmp(name.c_str(), builtinFontNames[i].name))
00167             return getFontByUsage(builtinFontNames[i].id);
00168 
00169     Common::String lowercaseName = name;
00170     lowercaseName.toLowercase();
00171     if (!_fontMap.contains(lowercaseName))
00172         return 0;
00173     return _fontMap[lowercaseName];
00174 }
00175 
00176 const Font *FontManager::getFontByUsage(FontUsage usage) const {
00177     switch (usage) {
00178     case kConsoleFont:
00179         return g_consolefont;
00180     case kGUIFont:
00181         return g_sysfont;
00182     case kBigGUIFont:
00183         return g_sysfont_big;
00184     case kLocalizedFont:
00185         // By default use the big font as localized font
00186         if (_localizedFontName.empty())
00187             return g_sysfont_big;
00188         else
00189             return _fontMap[_localizedFontName];
00190     }
00191 
00192     return 0;
00193 }
00194 
00195 Common::String FontManager::genLocalizedFontFilename(const Common::String &filename) const {
00196 #ifndef USE_TRANSLATION
00197     return filename;
00198 #else
00199     // We will transform the font filename in the following way:
00200     //   name.bdf
00201     //  will become:
00202     //   name-charset.bdf
00203     // Note that name should not contain any dot here!
00204 
00205     // In the first step we look for the dot. In case there is none we will
00206     // return the normal filename.
00207     Common::String::const_iterator dot = Common::find(filename.begin(), filename.end(), '.');
00208     if (dot == filename.end())
00209         return filename;
00210 
00211     // Put the translated font filename string back together.
00212     Common::String result(filename.begin(), dot);
00213     result += '-';
00214     result += TransMan.getCurrentCharset();
00215     result += dot;
00216 
00217     return result;
00218 #endif
00219 }
00220 
00221 } // End of namespace Graphics


Generated on Sat Feb 9 2019 05:00:31 for ResidualVM by doxygen 1.7.1
curved edge   curved edge