aboutsummaryrefslogtreecommitdiff
path: root/graphics
diff options
context:
space:
mode:
authorThierry Crozat2011-05-22 21:55:28 +0100
committerThierry Crozat2011-06-06 23:20:10 +0100
commit197ef8f1783bc5eed5c894e46a7c1d6c528db7e1 (patch)
treebf2bcc838ba77c86abd6b1fa7fa5a06490512261 /graphics
parent6b13782967b8b775c359b0ff54e7a17534248dbe (diff)
downloadscummvm-rg350-197ef8f1783bc5eed5c894e46a7c1d6c528db7e1.tar.gz
scummvm-rg350-197ef8f1783bc5eed5c894e46a7c1d6c528db7e1.tar.bz2
scummvm-rg350-197ef8f1783bc5eed5c894e46a7c1d6c528db7e1.zip
GRAPHICS: Add kLocalizedFont in FontManager
This makes it easy to get a localized font for the current TranslationManager charset if one has been loaded. It tries first to find one for the BigGUI or GUI fonts and then looks for any acceptable font. Also only use lower case font name for the FontManager HashMap. This is to avoid issues with the case when looking for a font by its name. There was for example an issue for font helvB12 that is named helvb12 in the scummmodern theme.
Diffstat (limited to 'graphics')
-rw-r--r--graphics/fontman.cpp58
-rw-r--r--graphics/fontman.h14
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>;