GUI: Rework how the default localized font is managed.
Now we set the default localized font to the "text_default" font of the currently active theme and default to the big GUI font in case none is specified properly.
This commit is contained in:
parent
0d3e79cf77
commit
050b8e3360
4 changed files with 44 additions and 55 deletions
|
@ -72,6 +72,20 @@ const struct {
|
||||||
{ 0, FontManager::kConsoleFont }
|
{ 0, FontManager::kConsoleFont }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool FontManager::setLocalizedFont(const Common::String &name) {
|
||||||
|
Common::String lowercaseName = name;
|
||||||
|
lowercaseName.toLowercase();
|
||||||
|
|
||||||
|
// We only update the localized font in case the name is properly assigned
|
||||||
|
// to a font.
|
||||||
|
if (_fontMap.contains(lowercaseName) && _fontMap.getVal(lowercaseName) != 0) {
|
||||||
|
_localizedFontName = lowercaseName;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool FontManager::assignFontToName(const Common::String &name, const Font *font) {
|
bool FontManager::assignFontToName(const Common::String &name, const Font *font) {
|
||||||
Common::String lowercaseName = name;
|
Common::String lowercaseName = name;
|
||||||
lowercaseName.toLowercase();
|
lowercaseName.toLowercase();
|
||||||
|
@ -103,6 +117,11 @@ void FontManager::removeFontName(const Common::String &name) {
|
||||||
Common::String lowercaseName = name;
|
Common::String lowercaseName = name;
|
||||||
lowercaseName.toLowercase();
|
lowercaseName.toLowercase();
|
||||||
_fontMap.erase(lowercaseName);
|
_fontMap.erase(lowercaseName);
|
||||||
|
|
||||||
|
// In case the current localized font is removed, we fall back to the
|
||||||
|
// default font again.
|
||||||
|
if (_localizedFontName == lowercaseName)
|
||||||
|
_localizedFontName.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Font *FontManager::getFontByName(const Common::String &name) const {
|
const Font *FontManager::getFontByName(const Common::String &name) const {
|
||||||
|
@ -126,51 +145,16 @@ const Font *FontManager::getFontByUsage(FontUsage usage) const {
|
||||||
case kBigGUIFont:
|
case kBigGUIFont:
|
||||||
return g_sysfont_big;
|
return g_sysfont_big;
|
||||||
case kLocalizedFont:
|
case kLocalizedFont:
|
||||||
{
|
// By default use the big font as localized font
|
||||||
// First try to find a kBigGUIFont
|
if (_localizedFontName.empty())
|
||||||
Common::String fontName = getLocalizedFontNameByUsage(kBigGUIFont);
|
return g_sysfont_big;
|
||||||
if (!fontName.empty()) {
|
else
|
||||||
const Font *font = getFontByName(fontName);
|
return _fontMap[_localizedFontName];
|
||||||
if (font)
|
|
||||||
return font;
|
|
||||||
}
|
|
||||||
// Try kGUIFont
|
|
||||||
fontName = getLocalizedFontNameByUsage(kGUIFont);
|
|
||||||
if (!fontName.empty()) {
|
|
||||||
const Font *font = getFontByName(fontName);
|
|
||||||
if (font)
|
|
||||||
return font;
|
|
||||||
}
|
|
||||||
#ifdef USE_TRANSLATION
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
// Fallback: return a non localized kGUIFont.
|
|
||||||
// Maybe we should return a null pointer instead?
|
|
||||||
return g_sysfont;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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 {
|
Common::String FontManager::genLocalizedFontFilename(const Common::String &filename) const {
|
||||||
#ifndef USE_TRANSLATION
|
#ifndef USE_TRANSLATION
|
||||||
return filename;
|
return filename;
|
||||||
|
|
|
@ -42,6 +42,14 @@ public:
|
||||||
kBigGUIFont = 3
|
kBigGUIFont = 3
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the localized font name.
|
||||||
|
*
|
||||||
|
* @param name the name of the localized font.
|
||||||
|
* @return true when the font was present, false otherwise.
|
||||||
|
*/
|
||||||
|
bool setLocalizedFont(const Common::String &name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a font object based on its 'name'.
|
* Retrieve a font object based on its 'name'.
|
||||||
*
|
*
|
||||||
|
@ -96,22 +104,13 @@ public:
|
||||||
|
|
||||||
//const Font *getFontBySize(int size???) const;
|
//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:
|
private:
|
||||||
friend class Common::Singleton<SingletonBaseType>;
|
friend class Common::Singleton<SingletonBaseType>;
|
||||||
FontManager();
|
FontManager();
|
||||||
~FontManager();
|
~FontManager();
|
||||||
|
|
||||||
Common::HashMap<Common::String, const Font *> _fontMap;
|
Common::HashMap<Common::String, const Font *> _fontMap;
|
||||||
|
Common::String _localizedFontName;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -571,14 +571,15 @@ bool ThemeEngine::addFont(TextData textId, const Common::String &file) {
|
||||||
} else {
|
} else {
|
||||||
Common::String localized = FontMan.genLocalizedFontFilename(file);
|
Common::String localized = FontMan.genLocalizedFontFilename(file);
|
||||||
// Try localized fonts
|
// Try localized fonts
|
||||||
_texts[textId]->_fontPtr = loadFont(localized);
|
_texts[textId]->_fontPtr = loadFont(localized, textId == kTextDataDefault);
|
||||||
|
|
||||||
if (!_texts[textId]->_fontPtr) {
|
if (!_texts[textId]->_fontPtr) {
|
||||||
// Try standard fonts
|
// Try standard fonts
|
||||||
_texts[textId]->_fontPtr = loadFont(file);
|
_texts[textId]->_fontPtr = loadFont(file, textId == kTextDataDefault);
|
||||||
|
|
||||||
if (!_texts[textId]->_fontPtr)
|
if (!_texts[textId]->_fontPtr)
|
||||||
error("Couldn't load font '%s'", file.c_str());
|
error("Couldn't load font '%s'", file.c_str());
|
||||||
|
|
||||||
#ifdef USE_TRANSLATION
|
#ifdef USE_TRANSLATION
|
||||||
TransMan.setLanguage("C");
|
TransMan.setLanguage("C");
|
||||||
#endif
|
#endif
|
||||||
|
@ -1385,7 +1386,7 @@ DrawData ThemeEngine::parseDrawDataId(const Common::String &name) const {
|
||||||
* External data loading
|
* External data loading
|
||||||
*********************************************************/
|
*********************************************************/
|
||||||
|
|
||||||
const Graphics::Font *ThemeEngine::loadFont(const Common::String &filename) {
|
const Graphics::Font *ThemeEngine::loadFont(const Common::String &filename, const bool makeLocalizedFont) {
|
||||||
// Try already loaded fonts.
|
// Try already loaded fonts.
|
||||||
const Graphics::Font *font = FontMan.getFontByName(filename);
|
const Graphics::Font *font = FontMan.getFontByName(filename);
|
||||||
if (font)
|
if (font)
|
||||||
|
@ -1417,8 +1418,13 @@ const Graphics::Font *ThemeEngine::loadFont(const Common::String &filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the font is successfully loaded store it in the font manager.
|
// If the font is successfully loaded store it in the font manager.
|
||||||
if (font)
|
if (font) {
|
||||||
FontMan.assignFontToName(filename, font);
|
FontMan.assignFontToName(filename, font);
|
||||||
|
// If this font should be the new default localized font, we set it up
|
||||||
|
// for that.
|
||||||
|
if (makeLocalizedFont)
|
||||||
|
FontMan.setLocalizedFont(filename);
|
||||||
|
}
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -536,7 +536,7 @@ protected:
|
||||||
*/
|
*/
|
||||||
void unloadTheme();
|
void unloadTheme();
|
||||||
|
|
||||||
const Graphics::Font *loadFont(const Common::String &filename);
|
const Graphics::Font *loadFont(const Common::String &filename, const bool makeLocalizedFont);
|
||||||
Common::String genCacheFilename(const Common::String &filename) const;
|
Common::String genCacheFilename(const Common::String &filename) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue