diff options
-rw-r--r-- | graphics/fontman.cpp | 58 | ||||
-rw-r--r-- | graphics/fontman.h | 14 |
2 files changed, 68 insertions, 4 deletions
diff --git a/graphics/fontman.cpp b/graphics/fontman.cpp index 27fe21ab97..6f1d282d54 100644 --- a/graphics/fontman.cpp +++ b/graphics/fontman.cpp @@ -68,14 +68,29 @@ const struct { { 0, FontManager::kConsoleFont } }; +bool FontManager::assignFontToName(const Common::String &name, const Font *font) { + Common::String lowercaseName = name; + lowercaseName.toLowercase(); + _fontMap[lowercaseName] = font; + return true; +} + +void FontManager::removeFontName(const Common::String &name) { + Common::String lowercaseName = name; + lowercaseName.toLowercase(); + _fontMap.erase(lowercaseName); +} + const Font *FontManager::getFontByName(const Common::String &name) const { for (int i = 0; builtinFontNames[i].name; i++) if (!scumm_stricmp(name.c_str(), builtinFontNames[i].name)) return getFontByUsage(builtinFontNames[i].id); - if (!_fontMap.contains(name)) + Common::String lowercaseName = name; + lowercaseName.toLowercase(); + if (!_fontMap.contains(lowercaseName)) return 0; - return _fontMap[name]; + return _fontMap[lowercaseName]; } const Font *FontManager::getFontByUsage(FontUsage usage) const { @@ -86,11 +101,50 @@ const Font *FontManager::getFontByUsage(FontUsage usage) const { return g_sysfont; case kBigGUIFont: return g_sysfont_big; + case kLocalizedFont: + { + // First try to find a kBigGUIFont + Common::String fontName = getLocalizedFontNameByUsage(kBigGUIFont); + if (!fontName.empty()) { + const Font *font = getFontByName(fontName); + if (font) + return font; + } + // Try kGUIFont + fontName = getLocalizedFontNameByUsage(kGUIFont); + if (!fontName.empty()) { + const Font *font = getFontByName(fontName); + if (font) + return font; + } + // Accept any other font that has the charset in its name + for (Common::HashMap<Common::String, const Font *>::const_iterator it = _fontMap.begin() ; it != _fontMap.end() ; ++it) { + if (it->_key.contains(TransMan.getCurrentCharset())) + return it->_value; + } + // Fallback: return a non localized kGUIFont. + // Maybe we should return a null pointer instead? + return g_sysfont; + } } return 0; } +Common::String FontManager::getLocalizedFontNameByUsage(FontUsage usage) const { + // We look for a name that matches the usage and that ends in .bdf. + // It should also not contain "-ascii" or "-iso-" in its name. + // We take the first name that matches. + for (int i = 0; builtinFontNames[i].name; i++) { + if (builtinFontNames[i].id == usage) { + Common::String fontName(builtinFontNames[i].name); + if (!fontName.contains("-ascii") && !fontName.contains("-iso-") && fontName.contains(".bdf")) + return genLocalizedFontFilename(fontName); + } + } + return Common::String(); +} + Common::String FontManager::genLocalizedFontFilename(const Common::String &filename) const { #ifndef USE_TRANSLATION return filename; diff --git a/graphics/fontman.h b/graphics/fontman.h index e502c9517e..858a733d45 100644 --- a/graphics/fontman.h +++ b/graphics/fontman.h @@ -36,6 +36,7 @@ class Font; class FontManager : public Common::Singleton<FontManager> { public: enum FontUsage { + kLocalizedFont = 0, kConsoleFont = 1, kGUIFont = 2, kBigGUIFont = 3 @@ -56,14 +57,14 @@ public: * @param font the font object * @return true on success, false on failure */ - bool assignFontToName(const Common::String &name, const Font *font) { _fontMap[name] = font; return true; } + bool assignFontToName(const Common::String &name, const Font *font); /** * Removes binding from name to font * * @param name name which should be removed */ - void removeFontName(const Common::String &name) { _fontMap.erase(name); } + void removeFontName(const Common::String &name); /** * Retrieve a font object based on what it is supposed @@ -85,6 +86,15 @@ public: //const Font *getFontBySize(int size???) const; +protected: + /** + * Get the name of the localized font for the given usage. There is no garanty that + * the font exists. If the usage is kLocalizedFont it returns an empty string. + * + * @param usage a FontUsage enum value indicating what the font will be used for. + * @return the name of a localized font or an empty string if no suitable font was found. + */ + Common::String getLocalizedFontNameByUsage(FontUsage usage) const; private: friend class Common::Singleton<SingletonBaseType>; |