Hook R_TextWidth to properly account for fonticons and material icons

This commit is contained in:
Jan 2021-09-06 17:02:41 +02:00
parent 10964ba059
commit de96cb4b16
2 changed files with 76 additions and 0 deletions

View File

@ -551,6 +551,78 @@ namespace Components
}
}
int TextRenderer::R_TextWidth_Hk(const char* text, int maxChars, Game::Font_s* font)
{
auto lineWidth = 0;
auto maxWidth = 0;
if (maxChars <= 0)
maxChars = 0x7FFFFFFF;
if (text == nullptr)
return 0;
auto count = 0;
while (text && *text && count < maxChars)
{
const auto letter = Game::SEH_ReadCharFromString(&text, nullptr);
if (letter == '\r' || letter == '\n')
{
lineWidth = 0;
}
else
{
if (letter == '^' && text)
{
if (*text >= COLOR_FIRST_CHAR && *text <= COLOR_LAST_CHAR)
{
text++;
continue;
}
if (*text >= '\x01' && *text <= '\x02' && text[1] != '\0' && text[2] != '\0' && text[3] != '\0')
{
const auto width = text[1];
const auto materialNameLength = text[3];
auto v9 = font->pixelHeight * (width - 16) + 16;
auto w = ((((v9 >> 24) & 0x1F) + v9) >> 5);
lineWidth += w;
if (lineWidth > maxWidth)
maxWidth = lineWidth;
text += 4;
for (auto currentLength = 0; currentLength < materialNameLength && *text; currentLength++)
text++;
continue;
}
}
if (letter == ':')
{
FontIconInfo fontIconInfo{};
const char* fontIconEnd = text;
if (IsFontIcon(fontIconEnd, fontIconInfo))
{
lineWidth += static_cast<int>(GetFontIconWidth(fontIconInfo, font, 1.0f));
if (lineWidth > maxWidth)
maxWidth = lineWidth;
text = fontIconEnd;
continue;
}
}
lineWidth += R_GetCharacterGlyph(font, letter)->dx;
if (lineWidth > maxWidth)
maxWidth = lineWidth;
count++;
}
}
return maxWidth;
}
void TextRenderer::UpdateColorTable()
{
if (cg_newColors.get<bool>())
@ -572,5 +644,8 @@ namespace Components
sv_customTextColor = Game::Dvar_RegisterColor("sv_customTextColor", 1, 0.7f, 0, 1, Game::dvar_flag::DVAR_FLAG_REPLICATED, "Color for the extended color code.");
Utils::Hook(0x535410, DrawText2D, HOOK_JUMP).install()->quick();
// Consider material text icons and font icons when calculating text width
Utils::Hook(0x5056C0, R_TextWidth_Hk, HOOK_JUMP).install()->quick();
}
}

View File

@ -86,6 +86,7 @@ namespace Components
static Game::GfxImage* GetFontIconColorMap(const Game::Material* fontIconMaterial);
static bool IsFontIcon(const char*& text, FontIconInfo& fontIcon);
static float GetFontIconWidth(const FontIconInfo& fontIcon, const Game::Font_s* font, float xScale);
static float DrawFontIcon(const FontIconInfo& fontIcon, float x, float y, float sinAngle, float cosAngle, const Game::Font_s* font, float xScale, float yScale, unsigned color);
static float GetMonospaceWidth(Game::Font_s* font, int rendererFlags);