From af3df959c2124fa988b597124c442af342e9aaeb Mon Sep 17 00:00:00 2001 From: glx22 Date: Thu, 22 Dec 2022 19:41:58 +0100 Subject: [PATCH] Codechange: reduce code duplication --- src/fontcache.cpp | 9 +-------- src/fontcache.h | 16 ++++++++++++++++ src/fontcache/freetypefontcache.cpp | 12 ++---------- src/gfx_type.h | 7 +++++++ src/os/macosx/font_osx.cpp | 15 +++------------ src/os/windows/font_win32.cpp | 15 +++------------ 6 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 91dae57564..5eec40a215 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -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. diff --git a/src/fontcache.h b/src/fontcache.h index 6cde1ab65e..7a86c137f8 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -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(); diff --git a/src/fontcache/freetypefontcache.cpp b/src/fontcache/freetypefontcache.cpp index 26087eee99..621b33fa9d 100644 --- a/src/fontcache/freetypefontcache.cpp +++ b/src/fontcache/freetypefontcache.cpp @@ -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: diff --git a/src/gfx_type.h b/src/gfx_type.h index 932a6cb87d..8a3fc64dfd 100644 --- a/src/gfx_type.h +++ b/src/gfx_type.h @@ -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). diff --git a/src/os/macosx/font_osx.cpp b/src/os/macosx/font_osx.cpp index 1ad4352004..4152c7da0f 100644 --- a/src/os/macosx/font_osx.cpp +++ b/src/os/macosx/font_osx.cpp @@ -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; } diff --git a/src/os/windows/font_win32.cpp b/src/os/windows/font_win32.cpp index 52d63b6938..e6b95222d7 100644 --- a/src/os/windows/font_win32.cpp +++ b/src/os/windows/font_win32.cpp @@ -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);