Codechange: reduce code duplication

This commit is contained in:
glx22 2022-12-22 19:41:58 +01:00 committed by Loïc Guilloux
parent 7b5edba76c
commit af3df959c2
6 changed files with 32 additions and 42 deletions

View File

@ -70,16 +70,9 @@ bool GetFontAAState(FontSize size, bool check_blitter)
/* AA is only supported for 32 bpp */
if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
switch (size) {
default: NOT_REACHED();
case FS_NORMAL: return _fcsettings.medium.aa;
case FS_SMALL: return _fcsettings.small.aa;
case FS_LARGE: return _fcsettings.large.aa;
case FS_MONO: return _fcsettings.mono.aa;
}
return GetFontCacheSubSetting(size)->aa;
}
/**
* (Re)initialize the font cache related things, i.e. load the non-sprite fonts.
* @param monospace Whether to initialise the monospace or regular fonts.

View File

@ -218,6 +218,22 @@ struct FontCacheSettings {
extern FontCacheSettings _fcsettings;
/**
* Get the settings of a given font size.
* @param fs The font size to look up.
* @return The settings.
*/
static inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
{
switch (fs) {
default: NOT_REACHED();
case FS_SMALL: return &_fcsettings.small;
case FS_NORMAL: return &_fcsettings.medium;
case FS_LARGE: return &_fcsettings.large;
case FS_MONO: return &_fcsettings.mono;
}
}
void InitFontCache(bool monospace);
void UninitFontCache();
bool HasAntialiasedFonts();

View File

@ -122,14 +122,7 @@ void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, int pixels)
*/
void LoadFreeTypeFont(FontSize fs)
{
FontCacheSubSetting *settings = nullptr;
switch (fs) {
default: NOT_REACHED();
case FS_SMALL: settings = &_fcsettings.small; break;
case FS_NORMAL: settings = &_fcsettings.medium; break;
case FS_LARGE: settings = &_fcsettings.large; break;
case FS_MONO: settings = &_fcsettings.mono; break;
}
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
if (settings->font.empty()) return;
@ -197,8 +190,7 @@ void LoadFreeTypeFont(FontSize fs)
FT_Done_Face(face);
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, SIZE_TO_NAME[fs], error);
ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, FontSizeToName(fs), error);
return;
found_face:

View File

@ -214,6 +214,13 @@ enum FontSize {
};
DECLARE_POSTFIX_INCREMENT(FontSize)
static inline const char *FontSizeToName(FontSize fs)
{
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
assert(fs < FS_END);
return SIZE_TO_NAME[fs];
}
/**
* Used to only draw a part of the sprite.
* Draw the subsprite in the rect (sprite_x_offset + left, sprite_y_offset + top) to (sprite_x_offset + right, sprite_y_offset + bottom).

View File

@ -350,16 +350,7 @@ const Sprite *CoreTextFontCache::InternalGetGlyph(GlyphID key, bool use_aa)
*/
void LoadCoreTextFont(FontSize fs)
{
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
FontCacheSubSetting *settings = nullptr;
switch (fs) {
default: NOT_REACHED();
case FS_SMALL: settings = &_fcsettings.small; break;
case FS_NORMAL: settings = &_fcsettings.medium; break;
case FS_LARGE: settings = &_fcsettings.large; break;
case FS_MONO: settings = &_fcsettings.mono; break;
}
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
if (settings->font.empty()) return;
@ -395,7 +386,7 @@ void LoadCoreTextFont(FontSize fs)
font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
CFRetain(font_ref.get());
} else {
ShowInfoF("Unable to load file '%s' for %s font, using default OS font selection instead", settings->font.c_str(), SIZE_TO_NAME[fs]);
ShowInfoF("Unable to load file '%s' for %s font, using default OS font selection instead", settings->font.c_str(), FontSizeToName(fs));
}
}
}
@ -419,7 +410,7 @@ void LoadCoreTextFont(FontSize fs)
}
if (!font_ref) {
ShowInfoF("Unable to use '%s' for %s font, using sprite font instead", settings->font.c_str(), SIZE_TO_NAME[fs]);
ShowInfoF("Unable to use '%s' for %s font, using sprite font instead", settings->font.c_str(), FontSizeToName(fs));
return;
}

View File

@ -580,16 +580,7 @@ void Win32FontCache::ClearFontCache()
*/
void LoadWin32Font(FontSize fs)
{
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
FontCacheSubSetting *settings = nullptr;
switch (fs) {
case FS_SMALL: settings = &_fcsettings.small; break;
case FS_NORMAL: settings = &_fcsettings.medium; break;
case FS_LARGE: settings = &_fcsettings.large; break;
case FS_MONO: settings = &_fcsettings.mono; break;
default: NOT_REACHED();
}
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
if (settings->font.empty()) return;
@ -647,7 +638,7 @@ void LoadWin32Font(FontSize fs)
logfont.lfWeight = strcasestr(font_name, " bold") != nullptr || strcasestr(font_name, "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
}
} else {
ShowInfoF("Unable to load file '%s' for %s font, using default windows font selection instead", font_name, SIZE_TO_NAME[fs]);
ShowInfoF("Unable to load file '%s' for %s font, using default windows font selection instead", font_name, FontSizeToName(fs));
}
}
}
@ -659,7 +650,7 @@ void LoadWin32Font(FontSize fs)
HFONT font = CreateFontIndirect(&logfont);
if (font == nullptr) {
ShowInfoF("Unable to use '%s' for %s font, Win32 reported error 0x%lX, using sprite font instead", font_name, SIZE_TO_NAME[fs], GetLastError());
ShowInfoF("Unable to use '%s' for %s font, Win32 reported error 0x%lX, using sprite font instead", font_name, FontSizeToName(fs), GetLastError());
return;
}
DeleteObject(font);