mirror of https://github.com/OpenTTD/OpenTTD
Codechange: reduce code duplication
parent
7b5edba76c
commit
af3df959c2
|
@ -70,15 +70,8 @@ bool GetFontAAState(FontSize size, bool check_blitter)
|
||||||
/* AA is only supported for 32 bpp */
|
/* AA is only supported for 32 bpp */
|
||||||
if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
|
if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
|
||||||
|
|
||||||
switch (size) {
|
return GetFontCacheSubSetting(size)->aa;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (Re)initialize the font cache related things, i.e. load the non-sprite fonts.
|
* (Re)initialize the font cache related things, i.e. load the non-sprite fonts.
|
||||||
|
|
|
@ -218,6 +218,22 @@ struct FontCacheSettings {
|
||||||
|
|
||||||
extern FontCacheSettings _fcsettings;
|
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 InitFontCache(bool monospace);
|
||||||
void UninitFontCache();
|
void UninitFontCache();
|
||||||
bool HasAntialiasedFonts();
|
bool HasAntialiasedFonts();
|
||||||
|
|
|
@ -122,14 +122,7 @@ void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, int pixels)
|
||||||
*/
|
*/
|
||||||
void LoadFreeTypeFont(FontSize fs)
|
void LoadFreeTypeFont(FontSize fs)
|
||||||
{
|
{
|
||||||
FontCacheSubSetting *settings = nullptr;
|
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings->font.empty()) return;
|
if (settings->font.empty()) return;
|
||||||
|
|
||||||
|
@ -197,8 +190,7 @@ void LoadFreeTypeFont(FontSize fs)
|
||||||
|
|
||||||
FT_Done_Face(face);
|
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, FontSizeToName(fs), error);
|
||||||
ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, SIZE_TO_NAME[fs], error);
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
found_face:
|
found_face:
|
||||||
|
|
|
@ -214,6 +214,13 @@ enum FontSize {
|
||||||
};
|
};
|
||||||
DECLARE_POSTFIX_INCREMENT(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.
|
* 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).
|
* Draw the subsprite in the rect (sprite_x_offset + left, sprite_y_offset + top) to (sprite_x_offset + right, sprite_y_offset + bottom).
|
||||||
|
|
|
@ -350,16 +350,7 @@ const Sprite *CoreTextFontCache::InternalGetGlyph(GlyphID key, bool use_aa)
|
||||||
*/
|
*/
|
||||||
void LoadCoreTextFont(FontSize fs)
|
void LoadCoreTextFont(FontSize fs)
|
||||||
{
|
{
|
||||||
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
|
FontCacheSubSetting *settings = GetFontCacheSubSetting(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings->font.empty()) return;
|
if (settings->font.empty()) return;
|
||||||
|
|
||||||
|
@ -395,7 +386,7 @@ void LoadCoreTextFont(FontSize fs)
|
||||||
font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
|
font_ref.reset((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), 0));
|
||||||
CFRetain(font_ref.get());
|
CFRetain(font_ref.get());
|
||||||
} else {
|
} 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) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -580,16 +580,7 @@ void Win32FontCache::ClearFontCache()
|
||||||
*/
|
*/
|
||||||
void LoadWin32Font(FontSize fs)
|
void LoadWin32Font(FontSize fs)
|
||||||
{
|
{
|
||||||
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
|
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (settings->font.empty()) return;
|
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.
|
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 {
|
} 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);
|
HFONT font = CreateFontIndirect(&logfont);
|
||||||
if (font == nullptr) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
DeleteObject(font);
|
DeleteObject(font);
|
||||||
|
|
Loading…
Reference in New Issue