Changeset - r26673:f6a0bb2e0bfa
[Not reviewed]
master
0 6 0
glx22 - 18 months ago 2022-12-22 18:41:58
glx@openttd.org
Codechange: reduce code duplication
6 files changed with 32 insertions and 42 deletions:
0 comments (0 inline, 0 general)
src/fontcache.cpp
Show inline comments
 
@@ -70,16 +70,9 @@ bool GetFontAAState(FontSize size, bool 
 
	/* 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.
src/fontcache.h
Show inline comments
 
@@ -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();
src/fontcache/freetypefontcache.cpp
Show inline comments
 
@@ -122,14 +122,7 @@ void FreeTypeFontCache::SetFontSize(Font
 
 */
 
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:
src/gfx_type.h
Show inline comments
 
@@ -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).
src/os/macosx/font_osx.cpp
Show inline comments
 
@@ -350,16 +350,7 @@ const Sprite *CoreTextFontCache::Interna
 
 */
 
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;
 
	}
 

	
src/os/windows/font_win32.cpp
Show inline comments
 
@@ -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);
0 comments (0 inline, 0 general)