Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heretic and Hexen: check for valid, existing characters in font drawing functions #1201

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/heretic/mn_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,33 @@ static void InitFonts(void)
FontBBaseLump = W_GetNumForName(DEH_String("FONTB_S")) + 1;
}

// [crispy] Check if printable character is existing in FONTA/FONTB sets
// and do a replacement or case correction if needed.

enum {
big_font, small_font
} fontsize_t;

static const char MN_CheckValidChar (char ascii_index, int have_cursor)
{
if ((ascii_index > 'Z' + have_cursor && ascii_index < 'a') || ascii_index > 'z')
{
// Replace "\]^_`" and "{|}~" with spaces,
// allow "[" (cursor symbol) only in small fonts.
return ' ';
}
else if (ascii_index >= 'a' && ascii_index <= 'z')
{
// Force lowercase "a...z" characters to uppercase "A...Z".
return ascii_index + 'A' - 'a';
}
else
{
// Valid char, do not modify it's ASCII index.
return ascii_index;
}
}

//---------------------------------------------------------------------------
//
// PROC MN_DrTextA
Expand All @@ -627,7 +654,9 @@ void MN_DrTextA(const char *text, int x, int y)

while ((c = *text++) != 0)
{
if (c < 33 || c > 91) // [crispy] fail-safe: draw patches above FONTA59 as spaces
c = MN_CheckValidChar(c, small_font); // [crispy] check for valid characters

if (c < 33)
{
x += 5;
}
Expand Down Expand Up @@ -657,7 +686,9 @@ int MN_TextAWidth(const char *text)
width = 0;
while ((c = *text++) != 0)
{
if (c < 33 || c > 91) // [crispy] fail-safe: consider patches above FONTA59 as spaces
c = MN_CheckValidChar(c, small_font); // [crispy] check for valid characters

if (c < 33)
{
width += 5;
}
Expand Down Expand Up @@ -685,7 +716,9 @@ void MN_DrTextB(const char *text, int x, int y)

while ((c = *text++) != 0)
{
if (c < 33 || c > 90) // [crispy] fail-safe: draw patches above FONTB58 as spaces
c = MN_CheckValidChar(c, big_font); // [crispy] check for valid characters

if (c < 33)
{
x += 8;
}
Expand Down Expand Up @@ -715,7 +748,9 @@ int MN_TextBWidth(const char *text)
width = 0;
while ((c = *text++) != 0)
{
if (c < 33 || c > 90) // [crispy] fail-safe: consider patches above FONTB58 as spaces
c = MN_CheckValidChar(c, big_font); // [crispy] check for valid characters

if (c < 33)
{
width += 5;
}
Expand Down Expand Up @@ -950,7 +985,7 @@ static void DrawSaveLoadBottomLine(const Menu_t *menu)
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
MN_DrTextA(filedate, ORIGWIDTH / 2 - MN_TextAWidth(pagestr), y + 10);
MN_DrTextA(filedate, ORIGWIDTH / 2 - MN_TextAWidth(filedate) / 2, y + 10);
}

dp_translation = NULL;
Expand Down
47 changes: 42 additions & 5 deletions src/hexen/mn_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,33 @@ static void InitFonts(void)
FontBBaseLump = W_GetNumForName("FONTB_S") + 1;
}

// [crispy] Check if printable character is existing in FONTA(Y)/FONTB sets
// and do a replacement or case correction if needed.

enum {
big_font, small_font
} fontsize_t;

static const char MN_CheckValidChar (char ascii_index, int have_cursor)
{
if ((ascii_index > 'Z' + have_cursor && ascii_index < 'a') || ascii_index > 'z')
{
// Replace "\]^_`" and "{|}~" with spaces,
// allow "[" (cursor symbol) only in small fonts.
return ' ';
}
else if (ascii_index >= 'a' && ascii_index <= 'z')
{
// Force lowercase "a...z" characters to uppercase "A...Z".
return ascii_index + 'A' - 'a';
}
else
{
// Valid char, do not modify it's ASCII index.
return ascii_index;
}
}

//---------------------------------------------------------------------------
//
// PROC MN_DrTextA
Expand All @@ -558,7 +585,9 @@ void MN_DrTextA(const char *text, int x, int y)

while ((c = *text++) != 0)
{
if (c < 33 || c > 91) // [crispy] fail-safe: draw patches above FONTA59 as spaces
c = MN_CheckValidChar(c, small_font); // [crispy] check for valid characters

if (c < 33)
{
x += 5;
}
Expand All @@ -584,7 +613,9 @@ void MN_DrTextAYellow(const char *text, int x, int y)

while ((c = *text++) != 0)
{
if (c < 33 || c > 91) // [crispy] fail-safe: draw patches above FONTAY59 as spaces
c = MN_CheckValidChar(c, small_font); // [crispy] check for valid characters

if (c < 33)
{
x += 5;
}
Expand Down Expand Up @@ -614,7 +645,9 @@ int MN_TextAWidth(const char *text)
width = 0;
while ((c = *text++) != 0)
{
if (c < 33 || c > 91) // [crispy] fail-safe: consider patches above FONTA(Y)59 as spaces
c = MN_CheckValidChar(c, small_font); // [crispy] check for valid characters

if (c < 33)
{
width += 5;
}
Expand Down Expand Up @@ -642,7 +675,9 @@ void MN_DrTextB(const char *text, int x, int y)

while ((c = *text++) != 0)
{
if (c < 33 || c > 90) // [crispy] fail-safe: draw patches above FONTB58 as spaces
c = MN_CheckValidChar(c, big_font); // [crispy] check for valid characters

if (c < 33)
{
x += 8;
}
Expand Down Expand Up @@ -672,7 +707,9 @@ int MN_TextBWidth(const char *text)
width = 0;
while ((c = *text++) != 0)
{
if (c < 33 || c > 90) // [crispy] fail-safe: consider patches above FONTB58 as spaces
c = MN_CheckValidChar(c, big_font); // [crispy] check for valid characters

if (c < 33)
{
width += 5;
}
Expand Down