1
0
Fork 0
mirror of https://github.com/k4zmu2a/SpaceCadetPinball.git synced 2024-11-21 16:30:19 +01:00

Compare commits

...

2 commits

Author SHA1 Message Date
Muzychenko Andrey
6ce3f654b1 Refactored System font option. 2022-01-18 12:53:20 +03:00
Gábor Dobra
7a6ae6ac50
Add support for SYSTEM font (#119)
* Add support for SYSTEM font

* Add System Font Name registry option

Issue #87
2022-01-18 09:42:14 +03:00
5 changed files with 161 additions and 40 deletions

View file

@ -16,7 +16,10 @@ int gdrv::use_wing = 0;
int gdrv::grtext_blue = 0;
int gdrv::grtext_green = 0;
int gdrv::grtext_red = -1;
std::string gdrv::fontFamily;
std::string gdrv::fontFilename;
const int* gdrv::fontSizes = nullptr;
int gdrv::fontCharset = DEFAULT_CHARSET;
int gdrv::init(HINSTANCE hInst, HWND hWnd)
{
@ -26,6 +29,9 @@ int gdrv::init(HINSTANCE hInst, HWND hWnd)
hwnd = hWnd;
if (!palette_handle)
palette_handle = CreatePalette(&current_palette);
choose_font();
return 0;
}
@ -33,6 +39,10 @@ int gdrv::uninit()
{
if (palette_handle)
DeleteObject(palette_handle);
if (!fontFilename.empty())
RemoveFontResourceExA(fontFilename.c_str(), FR_PRIVATE, 0);
return 0;
}
@ -426,23 +436,136 @@ void gdrv::ScrollBitmapHorizontal(gdrv_bitmap8* bmp, int xStart)
}
}
int CALLBACK gdrv::find_font_func(
ENUMLOGFONTEXA *lpelfe,
NEWTEXTMETRICEXA *lpntme,
DWORD FontType,
LPARAM lParam
)
{
auto* params = reinterpret_cast<EnumFindFont*>(lParam);
if (strcmp(reinterpret_cast<const char*>(lpelfe->elfFullName), params->name) == 0)
params->found = true;
return 1; // continue enumeration
}
bool gdrv::find_font(const char* fontName)
{
EnumFindFont params;
params.name = fontName;
params.found = false;
LOGFONTA lf{};
strcpy_s(lf.lfFaceName, fontName);
HDC hDC = GetDC(NULL);
EnumFontFamiliesExA(hDC, &lf, (FONTENUMPROCA)&find_font_func, reinterpret_cast<LPARAM>(&params), 0);
ReleaseDC(NULL, hDC);
return params.found;
}
void gdrv::choose_font()
{
// Original font was 16 points, used with lowest table resolution
static const int fontSizes_Arial[3] = { 16, 22, 28 };
static const int fontSizes_EastAsian[3] = { 22, 27, 32 };
static const int fontSizes_System[3] = { 17, 21, 29 };
static const int fontSizes_WinMenu[3] = { 17, 21, 27 };
// Default path: scalable TTF font with AA.
fontCharset = DEFAULT_CHARSET;
auto specialFont = true;
switch (options::Options.Language)
{
case Languages::TraditionalChinese:
fontFamily = "Microsoft JhengHei";
fontSizes = fontSizes_EastAsian;
break;
case Languages::SimplifiedChinese:
fontFamily = "Microsoft YaHei";
fontSizes = fontSizes_EastAsian;
break;
case Languages::Japanese:
fontFamily = "MS UI Gothic";
fontSizes = fontSizes_EastAsian;
break;
case Languages::Korean:
fontFamily = "Gulim";
fontSizes = fontSizes_EastAsian;
break;
default:
fontFamily = "Arial";
fontSizes = fontSizes_Arial;
specialFont = false;
}
// System font does not support some languages.
if (!options::Options.SystemFont || specialFont)
return;
// Alternative path: user defined font or System font.
auto userFont = options::Options.SystemFontName;
if (userFont[0] != '\0' && find_font(userFont))
{
fontFamily = userFont;
fontSizes = fontSizes_WinMenu;
}
else
{
// Original game does no font setup, so it ends up with default System font.
// System is a bitmap font with no AA and limited scaling support.
// Specific .fon selection is needed for improved language support.
fontFamily = "System";
fontSizes = fontSizes_System;
const char* system_font_filename = nullptr;
switch (options::Options.Language)
{
case Languages::Greek:
system_font_filename = "vgasysg.fon";
fontCharset = GREEK_CHARSET;
break;
case Languages::Russian:
system_font_filename = "vgasysr.fon";
fontCharset = RUSSIAN_CHARSET;
break;
case Languages::Turkish:
system_font_filename = "vgasyst.fon";
fontCharset = TURKISH_CHARSET;
break;
case Languages::Danish:
case Languages::Dutch:
case Languages::Norwegian:
case Languages::Swedish:
system_font_filename = "vgasys.fon";
fontCharset = DEFAULT_CHARSET;
break;
case Languages::Czech:
case Languages::Hungarian:
case Languages::Polish:
case Languages::Finnish:
system_font_filename = "vgasyse.fon";
fontCharset = EASTEUROPE_CHARSET;
break;
default:
system_font_filename = "vgasys.fon";
fontCharset = DEFAULT_CHARSET;
break;
}
std::string windir(MAX_PATH, '\0');
DWORD result = GetEnvironmentVariableA("WINDIR", &windir[0], MAX_PATH);
if (result == 0 || result > MAX_PATH) // fails or doesn't fit
windir = "C:\\Windows";
else
windir.resize(result);
fontFilename = windir + "\\Fonts\\" + system_font_filename;
AddFontResourceExA(fontFilename.c_str(), FR_PRIVATE, 0);
}
}
void gdrv::grtext_draw_ttext_in_box(LPCWSTR text, int xOff, int yOff, int width, int height, bool centered)
{
// Original font was 16 points, used with lowest table resolution
static const int fontSizes[3] =
{
16,
22,
28
};
static const int fontSizes_EastAsian[3] =
{
22,
27,
32
};
xOff = static_cast<int>(xOff * fullscrn::ScaleX) + fullscrn::OffsetX;
yOff = static_cast<int>(yOff * fullscrn::ScaleY) + fullscrn::OffsetY;
width = static_cast<int>(width * fullscrn::ScaleX);
@ -464,33 +587,12 @@ void gdrv::grtext_draw_ttext_in_box(LPCWSTR text, int xOff, int yOff, int width,
sscanf_s(fontColor, "%d %d %d", &grtext_red, &grtext_green, &grtext_blue);
}
const char* font = "Arial";
const int* selectedFontSizes = fontSizes;
switch (options::Options.Language)
{
case Languages::TraditionalChinese:
font = "Microsoft JhengHei";
selectedFontSizes = fontSizes_EastAsian;
break;
case Languages::SimplifiedChinese:
font = "Microsoft YaHei";
selectedFontSizes = fontSizes_EastAsian;
break;
case Languages::Japanese:
font = "MS UI Gothic";
break;
case Languages::Korean:
font = "Malgun Gothic";
selectedFontSizes = fontSizes_EastAsian;
break;
}
auto fontSize = static_cast<int>(round(selectedFontSizes[fullscrn::GetResolution()] * fullscrn::ScaleY));
auto fontSize = static_cast<int>(round(fontSizes[fullscrn::GetResolution()] * fullscrn::ScaleY));
// Default font does not scale well
auto hNewFont = CreateFont(fontSize, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, font);
auto hNewFont = CreateFontA(fontSize, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
fontCharset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, fontFamily.c_str());
HFONT hOldFont = static_cast<HFONT>(SelectObject(dc, hNewFont));
int prevMode = SetBkMode(dc, TRANSPARENT);
COLORREF color = SetTextColor(dc, grtext_red | grtext_green << 8 | grtext_blue << 16);

View file

@ -33,6 +33,11 @@ struct LOGPALETTEx256 : LOGPALETTE
}
};
struct EnumFindFont
{
const char* name;
bool found;
};
class gdrv
{
@ -74,8 +79,15 @@ private:
static int grtext_blue;
static int grtext_green;
static int grtext_red;
static std::string fontFamily;
static std::string fontFilename;
static const int* fontSizes;
static int fontCharset;
static int StretchDIBitsScaled(HDC hdc, int xDest, int yDest, int DestWidth, int DestHeight, int xSrc, int ySrc,
int SrcWidth, int SrcHeight, gdrv_bitmap8* bmp, UINT iUsage,
DWORD rop);
static bool find_font(const char* fontName);
static int CALLBACK find_font_func(ENUMLOGFONTEXA* lpelfe, NEWTEXTMETRICEXA* lpntme, DWORD FontType, LPARAM lParam);
static void choose_font();
};

View file

@ -139,6 +139,8 @@ void options::ReadOptions()
Options.TargetUps = get_int(nullptr, "Target UPS", 120);
Options.TargetUps = max(60, Options.TargetUps);
Options.TargetUps = min(Options.TargetUps, 360);
Options.SystemFont = get_int(nullptr, "System Font", false);
get_string(nullptr, "System Font Name", Options.SystemFontName, "", LF_FACESIZE);
auto defaultLanguage = Languages::English;
auto language = static_cast<Languages>(get_int(nullptr, "Language", static_cast<int>(defaultLanguage)));
@ -237,6 +239,8 @@ void options::uninit()
set_int(nullptr, "Screen Resolution", Options.Resolution);
set_int(nullptr, "Uniform scaling", Options.UniformScaling);
set_int(nullptr, "Alternative Render", Options.AlternativeRender);
set_int(nullptr, "System Font", Options.SystemFont);
set_string(nullptr, "System Font Name", Options.SystemFontName);
set_int(nullptr, "Language", static_cast<int>(Options.Language));
set_int(nullptr, "Target UPS", Options.TargetUps);
}

View file

@ -60,6 +60,8 @@ struct optionsStruct
bool UniformScaling;
bool AlternativeRender;
int TargetUps;
bool SystemFont;
char SystemFontName[LF_FACESIZE];
Languages Language;
};

View file

@ -19,6 +19,7 @@
#include <cstdint>
#include <type_traits> /*For control template*/
//#include <cstdlib>
#include <string>
/*Use (void) to silent unused warnings.*/
#define assertm(exp, msg) assert(((void)msg, exp))